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
b6720018
Commit
b6720018
authored
Jan 17, 2018
by
Etienne Samson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: Switch to the nifty argv helpers from common
parent
45f58409
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
19 deletions
+36
-19
examples/CMakeLists.txt
+1
-1
examples/network/git2.c
+35
-18
No files found.
examples/CMakeLists.txt
View file @
b6720018
LINK_DIRECTORIES
(
${
LIBGIT2_LIBDIRS
}
)
LINK_DIRECTORIES
(
${
LIBGIT2_LIBDIRS
}
)
INCLUDE_DIRECTORIES
(
${
LIBGIT2_INCLUDES
}
)
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
}
)
ADD_EXECUTABLE
(
cgit2
${
SRC_EXAMPLE_GIT2
}
)
IF
(
WIN32 OR ANDROID
)
IF
(
WIN32 OR ANDROID
)
TARGET_LINK_LIBRARIES
(
cgit2 git2
)
TARGET_LINK_LIBRARIES
(
cgit2 git2
)
...
...
examples/network/git2.c
View file @
b6720018
...
@@ -2,10 +2,11 @@
...
@@ -2,10 +2,11 @@
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include "../common.h"
#include "common.h"
#include "common.h"
/
/
This part is not strictly libgit2-dependent, but you can use this
/
*
This part is not strictly libgit2-dependent, but you can use this
// as a starting point for a git-like tool
* as a starting point for a git-like tool */
struct
{
struct
{
char
*
name
;
char
*
name
;
...
@@ -18,20 +19,12 @@ struct {
...
@@ -18,20 +19,12 @@ struct {
{
NULL
,
NULL
}
{
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
;
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
/
* Run the command. If something goes wrong, print the error message to stderr */
error
=
fn
(
repo
,
arg
c
,
argv
);
error
=
fn
(
repo
,
arg
s
.
argc
-
args
.
pos
,
&
args
.
argv
[
args
.
pos
]
);
if
(
error
<
0
)
{
if
(
error
<
0
)
{
if
(
giterr_last
()
==
NULL
)
if
(
giterr_last
()
==
NULL
)
fprintf
(
stderr
,
"Error without message"
);
fprintf
(
stderr
,
"Error without message"
);
...
@@ -39,9 +32,6 @@ static int run_command(git_cb fn, int argc, char **argv)
...
@@ -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
);
fprintf
(
stderr
,
"Bad news:
\n
%s
\n
"
,
giterr_last
()
->
message
);
}
}
if
(
repo
)
git_repository_free
(
repo
);
return
!!
error
;
return
!!
error
;
}
}
...
@@ -49,6 +39,10 @@ int main(int argc, char **argv)
...
@@ -49,6 +39,10 @@ int main(int argc, char **argv)
{
{
int
i
;
int
i
;
int
return_code
=
1
;
int
return_code
=
1
;
int
error
;
git_repository
*
repo
;
struct
args_info
args
=
ARGS_INFO_INIT
;
const
char
*
git_dir
=
NULL
;
if
(
argc
<
2
)
{
if
(
argc
<
2
)
{
fprintf
(
stderr
,
"usage: %s <cmd> [repo]
\n
"
,
argv
[
0
]);
fprintf
(
stderr
,
"usage: %s <cmd> [repo]
\n
"
,
argv
[
0
]);
...
@@ -57,9 +51,30 @@ int main(int argc, char **argv)
...
@@ -57,9 +51,30 @@ int main(int argc, char **argv)
git_libgit2_init
();
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
)
{
for
(
i
=
0
;
commands
[
i
].
name
!=
NULL
;
++
i
)
{
if
(
!
strcmp
(
arg
v
[
1
],
commands
[
i
].
name
))
{
if
(
!
strcmp
(
arg
s
.
argv
[
args
.
pos
],
commands
[
i
].
name
))
{
return_code
=
run_command
(
commands
[
i
].
fn
,
--
argc
,
++
argv
);
return_code
=
run_command
(
commands
[
i
].
fn
,
repo
,
args
);
goto
shutdown
;
goto
shutdown
;
}
}
}
}
...
@@ -67,6 +82,8 @@ int main(int argc, char **argv)
...
@@ -67,6 +82,8 @@ int main(int argc, char **argv)
fprintf
(
stderr
,
"Command not found: %s
\n
"
,
argv
[
1
]);
fprintf
(
stderr
,
"Command not found: %s
\n
"
,
argv
[
1
]);
shutdown:
shutdown:
git_repository_free
(
repo
);
git_libgit2_shutdown
();
git_libgit2_shutdown
();
return
return_code
;
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