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
f55accce
Unverified
Commit
f55accce
authored
Feb 01, 2018
by
Patrick Steinhardt
Committed by
GitHub
Feb 01, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4040 from tiennou/examples/merge
Merge example
parents
341608dc
33f44db9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
51 additions
and
36 deletions
+51
-36
examples/CMakeLists.txt
+1
-1
examples/common.c
+10
-0
examples/common.h
+5
-0
examples/describe.c
+0
-10
examples/merge.c
+0
-0
examples/network/fetch.c
+0
-7
examples/network/git2.c
+35
-18
No files found.
examples/CMakeLists.txt
View file @
f55accce
LINK_DIRECTORIES
(
${
LIBGIT2_LIBDIRS
}
)
INCLUDE_DIRECTORIES
(
${
LIBGIT2_INCLUDES
}
)
FILE
(
GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h
)
FILE
(
GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h
common.?
)
ADD_EXECUTABLE
(
cgit2
${
SRC_EXAMPLE_GIT2
}
)
IF
(
WIN32 OR ANDROID
)
TARGET_LINK_LIBRARIES
(
cgit2 git2
)
...
...
examples/common.c
View file @
f55accce
...
...
@@ -235,3 +235,13 @@ void treeish_to_tree(
git_object_free
(
obj
);
}
void
*
xrealloc
(
void
*
oldp
,
size_t
newsz
)
{
void
*
p
=
realloc
(
oldp
,
newsz
);
if
(
p
==
NULL
)
{
fprintf
(
stderr
,
"Cannot allocate memory, exiting.
\n
"
);
exit
(
1
);
}
return
p
;
}
examples/common.h
View file @
f55accce
...
...
@@ -103,3 +103,8 @@ extern int diff_output(
*/
extern
void
treeish_to_tree
(
git_tree
**
out
,
git_repository
*
repo
,
const
char
*
treeish
);
/**
* A realloc that exits on failure
*/
extern
void
*
xrealloc
(
void
*
oldp
,
size_t
newsz
);
examples/describe.c
View file @
f55accce
...
...
@@ -47,16 +47,6 @@ typedef struct {
typedef
struct
args_info
args_info
;
static
void
*
xrealloc
(
void
*
oldp
,
size_t
newsz
)
{
void
*
p
=
realloc
(
oldp
,
newsz
);
if
(
p
==
NULL
)
{
fprintf
(
stderr
,
"Cannot allocate memory, exiting.
\n
"
);
exit
(
1
);
}
return
p
;
}
static
void
opts_add_commit
(
describe_options
*
opts
,
const
char
*
commit
)
{
size_t
sz
;
...
...
examples/merge.c
0 → 100644
View file @
f55accce
This diff is collapsed.
Click to expand it.
examples/network/fetch.c
View file @
f55accce
...
...
@@ -8,13 +8,6 @@
# include <unistd.h>
#endif
struct
dl_data
{
git_remote
*
remote
;
git_fetch_options
*
fetch_opts
;
int
ret
;
int
finished
;
};
static
int
progress_cb
(
const
char
*
str
,
int
len
,
void
*
data
)
{
(
void
)
data
;
...
...
examples/network/git2.c
View file @
f55accce
...
...
@@ -2,10 +2,11 @@
#include <stdio.h>
#include <string.h>
#include "../common.h"
#include "common.h"
/
/
This part is not strictly libgit2-dependent, but you can use this
// as a starting point for a git-like tool
/
*
This part is not strictly libgit2-dependent, but you can use this
* as a starting point for a git-like tool */
struct
{
char
*
name
;
...
...
@@ -18,20 +19,12 @@ struct {
{
NULL
,
NULL
}
};
static
int
run_command
(
git_cb
fn
,
int
argc
,
char
**
argv
)
static
int
run_command
(
git_cb
fn
,
git_repository
*
repo
,
struct
args_info
args
)
{
int
error
;
git_repository
*
repo
;
// Before running the actual command, create an instance of the local
// repository and pass it to the function.
error
=
git_repository_open
(
&
repo
,
".git"
);
if
(
error
<
0
)
repo
=
NULL
;
/
/ Run the command. If something goes wrong, print the error message to stderr
error
=
fn
(
repo
,
arg
c
,
argv
);
/
* Run the command. If something goes wrong, print the error message to stderr */
error
=
fn
(
repo
,
arg
s
.
argc
-
args
.
pos
,
&
args
.
argv
[
args
.
pos
]
);
if
(
error
<
0
)
{
if
(
giterr_last
()
==
NULL
)
fprintf
(
stderr
,
"Error without message"
);
...
...
@@ -39,9 +32,6 @@ static int run_command(git_cb fn, int argc, char **argv)
fprintf
(
stderr
,
"Bad news:
\n
%s
\n
"
,
giterr_last
()
->
message
);
}
if
(
repo
)
git_repository_free
(
repo
);
return
!!
error
;
}
...
...
@@ -49,6 +39,10 @@ int main(int argc, char **argv)
{
int
i
;
int
return_code
=
1
;
int
error
;
git_repository
*
repo
;
struct
args_info
args
=
ARGS_INFO_INIT
;
const
char
*
git_dir
=
NULL
;
if
(
argc
<
2
)
{
fprintf
(
stderr
,
"usage: %s <cmd> [repo]
\n
"
,
argv
[
0
]);
...
...
@@ -57,9 +51,30 @@ int main(int argc, char **argv)
git_libgit2_init
();
for
(
args
.
pos
=
1
;
args
.
pos
<
args
.
argc
;
++
args
.
pos
)
{
char
*
a
=
args
.
argv
[
args
.
pos
];
if
(
a
[
0
]
!=
'-'
)
{
/* non-arg */
break
;
}
else
if
(
optional_str_arg
(
&
git_dir
,
&
args
,
"--git-dir"
,
".git"
))
{
continue
;
}
else
if
(
!
strcmp
(
a
,
"--"
))
{
/* arg separator */
break
;
}
}
/* Before running the actual command, create an instance of the local
* repository and pass it to the function. */
error
=
git_repository_open
(
&
repo
,
git_dir
);
if
(
error
<
0
)
repo
=
NULL
;
for
(
i
=
0
;
commands
[
i
].
name
!=
NULL
;
++
i
)
{
if
(
!
strcmp
(
arg
v
[
1
],
commands
[
i
].
name
))
{
return_code
=
run_command
(
commands
[
i
].
fn
,
--
argc
,
++
argv
);
if
(
!
strcmp
(
arg
s
.
argv
[
args
.
pos
],
commands
[
i
].
name
))
{
return_code
=
run_command
(
commands
[
i
].
fn
,
repo
,
args
);
goto
shutdown
;
}
}
...
...
@@ -67,6 +82,8 @@ int main(int argc, char **argv)
fprintf
(
stderr
,
"Command not found: %s
\n
"
,
argv
[
1
]);
shutdown:
git_repository_free
(
repo
);
git_libgit2_shutdown
();
return
return_code
;
...
...
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