Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
3be09b6c
Commit
3be09b6c
authored
Jun 18, 2019
by
Alberto Fanjul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Compare buffers in diff example
parent
398412cc
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
4 deletions
+98
-4
examples/common.c
+42
-0
examples/common.h
+8
-0
examples/diff.c
+48
-4
No files found.
examples/common.c
View file @
3be09b6c
...
...
@@ -14,6 +14,14 @@
#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
# include <io.h>
#else
# include <fcntl.h>
# include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
...
...
@@ -391,3 +399,37 @@ out:
free
(
pubkey
);
return
error
;
}
char
*
read_file
(
const
char
*
path
)
{
ssize_t
total
=
0
;
char
*
buf
=
NULL
;
struct
stat
st
;
int
fd
=
-
1
;
if
((
fd
=
open
(
path
,
O_RDONLY
))
<
0
||
fstat
(
fd
,
&
st
)
<
0
)
goto
out
;
if
((
buf
=
malloc
(
st
.
st_size
+
1
))
==
NULL
)
goto
out
;
while
(
total
<
st
.
st_size
)
{
ssize_t
bytes
=
read
(
fd
,
buf
+
total
,
st
.
st_size
-
total
);
if
(
bytes
<=
0
)
{
if
(
errno
==
EAGAIN
||
errno
==
EINTR
)
continue
;
free
(
buf
);
buf
=
NULL
;
goto
out
;
}
total
+=
bytes
;
}
buf
[
total
]
=
'\0'
;
out:
if
(
fd
>=
0
)
close
(
fd
);
return
buf
;
}
examples/common.h
View file @
3be09b6c
...
...
@@ -64,6 +64,14 @@ extern int lg2_tag(git_repository *repo, int argc, char **argv);
extern
void
check_lg2
(
int
error
,
const
char
*
message
,
const
char
*
extra
);
/**
* Read a file into a buffer
*
* @param path The path to the file that shall be read
* @return NUL-terminated buffer if the file was successfully read, NULL-pointer otherwise
*/
extern
char
*
read_file
(
const
char
*
path
);
/**
* Exit the program, printing error to stderr
*/
extern
void
fatal
(
const
char
*
message
,
const
char
*
extra
);
...
...
examples/diff.c
View file @
3be09b6c
...
...
@@ -52,6 +52,7 @@ struct opts {
git_diff_options
diffopts
;
git_diff_find_options
findopts
;
int
color
;
int
no_index
;
int
cache
;
int
output
;
git_diff_format_t
format
;
...
...
@@ -66,14 +67,16 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
static
int
color_printer
(
const
git_diff_delta
*
,
const
git_diff_hunk
*
,
const
git_diff_line
*
,
void
*
);
static
void
diff_print_stats
(
git_diff
*
diff
,
struct
opts
*
o
);
static
void
compute_diff_no_index
(
git_diff
**
diff
,
struct
opts
*
o
);
int
lg2_diff
(
git_repository
*
repo
,
int
argc
,
char
*
argv
[])
{
git_tree
*
t1
=
NULL
,
*
t2
=
NULL
;
git_diff
*
diff
;
struct
opts
o
=
{
GIT_DIFF_OPTIONS_INIT
,
GIT_DIFF_FIND_OPTIONS_INIT
,
-
1
,
0
,
0
,
GIT_DIFF_FORMAT_PATCH
,
NULL
,
NULL
,
"."
-
1
,
-
1
,
0
,
0
,
GIT_DIFF_FORMAT_PATCH
,
NULL
,
NULL
,
"."
};
parse_opts
(
&
o
,
argc
,
argv
);
...
...
@@ -86,12 +89,16 @@ int lg2_diff(git_repository *repo, int argc, char *argv[])
* * <sha1>
* * --cached
* * --nocache (don't use index data in diff at all)
* * --no-index <file1> <file2>
* * nothing
*
* Currently ranged arguments like <sha1>..<sha2> and <sha1>...<sha2>
* are not supported in this example
*/
if
(
o
.
no_index
>=
0
)
{
compute_diff_no_index
(
&
diff
,
&
o
);
}
else
{
if
(
o
.
treeish1
)
treeish_to_tree
(
&
t1
,
repo
,
o
.
treeish1
);
if
(
o
.
treeish2
)
...
...
@@ -129,6 +136,7 @@ int lg2_diff(git_repository *repo, int argc, char *argv[])
check_lg2
(
git_diff_find_similar
(
diff
,
&
o
.
findopts
),
"finding renames and copies"
,
NULL
);
}
/** Generate simple output using libgit2 display helper. */
...
...
@@ -158,6 +166,38 @@ int lg2_diff(git_repository *repo, int argc, char *argv[])
return
0
;
}
static
void
compute_diff_no_index
(
git_diff
**
diff
,
struct
opts
*
o
)
{
git_patch
*
patch
=
NULL
;
char
*
file1_str
=
NULL
;
char
*
file2_str
=
NULL
;
git_buf
buf
=
{
0
};
if
(
!
o
->
treeish1
||
!
o
->
treeish2
)
{
usage
(
"two files should be provided as arguments"
,
NULL
);
}
file1_str
=
read_file
(
o
->
treeish1
);
if
(
file1_str
==
NULL
)
{
usage
(
"file cannot be read"
,
o
->
treeish1
);
}
file2_str
=
read_file
(
o
->
treeish2
);
if
(
file2_str
==
NULL
)
{
usage
(
"file cannot be read"
,
o
->
treeish2
);
}
check_lg2
(
git_patch_from_buffers
(
&
patch
,
file1_str
,
strlen
(
file1_str
),
o
->
treeish1
,
file2_str
,
strlen
(
file2_str
),
o
->
treeish2
,
&
o
->
diffopts
),
"patch buffers"
,
NULL
);
check_lg2
(
git_patch_to_buf
(
&
buf
,
patch
),
"patch to buf"
,
NULL
);
check_lg2
(
git_diff_from_buffer
(
diff
,
buf
.
ptr
,
buf
.
size
),
"diff from patch"
,
NULL
);
git_patch_free
(
patch
);
git_buf_dispose
(
&
buf
);
free
(
file1_str
);
free
(
file2_str
);
}
static
void
usage
(
const
char
*
message
,
const
char
*
arg
)
{
if
(
message
&&
arg
)
...
...
@@ -223,9 +263,10 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
o
->
output
|=
OUTPUT_DIFF
;
o
->
format
=
GIT_DIFF_FORMAT_PATCH
;
}
else
if
(
!
strcmp
(
a
,
"--cached"
))
else
if
(
!
strcmp
(
a
,
"--cached"
))
{
o
->
cache
=
CACHE_ONLY
;
else
if
(
!
strcmp
(
a
,
"--nocache"
))
if
(
o
->
no_index
>=
0
)
usage
(
"--cached and --no-index are incompatible"
,
NULL
);
}
else
if
(
!
strcmp
(
a
,
"--nocache"
))
o
->
cache
=
CACHE_NONE
;
else
if
(
!
strcmp
(
a
,
"--name-only"
)
||
!
strcmp
(
a
,
"--format=name"
))
o
->
format
=
GIT_DIFF_FORMAT_NAME_ONLY
;
...
...
@@ -238,7 +279,10 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
o
->
format
=
GIT_DIFF_FORMAT_RAW
;
o
->
diffopts
.
id_abbrev
=
40
;
}
else
if
(
!
strcmp
(
a
,
"--color"
))
else
if
(
!
strcmp
(
a
,
"--no-index"
))
{
o
->
no_index
=
0
;
if
(
o
->
cache
==
CACHE_ONLY
)
usage
(
"--cached and --no-index are incompatible"
,
NULL
);
}
else
if
(
!
strcmp
(
a
,
"--color"
))
o
->
color
=
0
;
else
if
(
!
strcmp
(
a
,
"--no-color"
))
o
->
color
=
-
1
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment