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
6cf47726
Commit
6cf47726
authored
Jan 15, 2019
by
Etienne Samson
Committed by
Patrick Steinhardt
Feb 15, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples/add: add explanatory comments and reformat
parent
106998fc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
23 deletions
+37
-23
examples/add.c
+37
-23
No files found.
examples/add.c
View file @
6cf47726
...
...
@@ -15,6 +15,18 @@
#include "common.h"
#include <assert.h>
/**
* The following example demonstrates how to add files with libgit2.
*
* It will use the repository in the current working directory, and act
* on files passed as its parameters.
*
* Recognized options are:
* -v/--verbose: show the file's status after acting on it.
* -n/--dry-run: do not actually change the index.
* -u/--update: update the index instead of adding to it.
*/
enum
print_options
{
SKIP
=
1
,
VERBOSE
=
2
,
...
...
@@ -40,30 +52,37 @@ int lg2_add(git_repository *repo, int argc, char** argv)
struct
print_payload
payload
=
{
0
};
parse_opts
(
&
options
,
&
count
,
argc
,
argv
);
init_array
(
&
array
,
argc
-
count
,
argv
+
count
);
check_lg2
(
git_repository_index
(
&
index
,
repo
),
"Could not open repository index"
,
NULL
);
if
(
options
&
VERBOSE
||
options
&
SKIP
)
{
/* Setup a callback if the requested options need it */
if
((
options
&
VERBOSE
)
||
(
options
&
SKIP
))
{
matched_cb
=
&
print_matched_cb
;
}
/* Perform the requested action with the index and files */
payload
.
options
=
options
;
payload
.
repo
=
repo
;
if
(
options
&
UPDATE
)
{
if
(
options
&
UPDATE
)
{
git_index_update_all
(
index
,
&
array
,
matched_cb
,
&
payload
);
}
else
{
git_index_add_all
(
index
,
&
array
,
0
,
matched_cb
,
&
payload
);
}
/* Cleanup memory */
git_index_write
(
index
);
git_index_free
(
index
);
return
0
;
}
/*
* This callback is called for each file under consideration by
* git_index_(update|add)_all above.
* It makes uses of the callback's ability to abort the action.
*/
int
print_matched_cb
(
const
char
*
path
,
const
char
*
matched_pathspec
,
void
*
payload
)
{
struct
print_payload
p
=
*
(
struct
print_payload
*
)(
payload
);
...
...
@@ -71,18 +90,19 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
unsigned
status
;
(
void
)
matched_pathspec
;
/* Get the file status */
if
(
git_status_file
(
&
status
,
p
.
repo
,
path
))
{
return
-
1
;
}
if
(
status
&
GIT_STATUS_WT_MODIFIED
||
status
&
GIT_STATUS_WT_NEW
)
{
if
(
(
status
&
GIT_STATUS_WT_MODIFIED
)
||
(
status
&
GIT_STATUS_WT_NEW
)
)
{
printf
(
"add '%s'
\n
"
,
path
);
ret
=
0
;
}
else
{
ret
=
1
;
}
if
(
p
.
options
&
SKIP
)
{
if
((
p
.
options
&
SKIP
)
)
{
ret
=
1
;
}
...
...
@@ -94,11 +114,11 @@ void init_array(git_strarray *array, int argc, char **argv)
unsigned
int
i
;
array
->
count
=
argc
;
array
->
strings
=
malloc
(
sizeof
(
char
*
)
*
array
->
count
);
assert
(
array
->
strings
!=
NULL
);
array
->
strings
=
calloc
(
array
->
count
,
sizeof
(
char
*
)
);
assert
(
array
->
strings
!=
NULL
);
for
(
i
=
0
;
i
<
array
->
count
;
i
++
)
{
array
->
strings
[
i
]
=
argv
[
i
];
for
(
i
=
0
;
i
<
array
->
count
;
i
++
)
{
array
->
strings
[
i
]
=
argv
[
i
];
}
return
;
...
...
@@ -118,33 +138,27 @@ static void parse_opts(int *options, int *count, int argc, char *argv[])
int
i
;
for
(
i
=
1
;
i
<
argc
;
++
i
)
{
if
(
argv
[
i
][
0
]
!=
'-'
)
{
if
(
argv
[
i
][
0
]
!=
'-'
)
break
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--verbose"
)
||
!
strcmp
(
argv
[
i
],
"-v"
))
{
else
if
(
!
strcmp
(
argv
[
i
],
"--verbose"
)
||
!
strcmp
(
argv
[
i
],
"-v"
))
*
options
|=
VERBOSE
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--dry-run"
)
||
!
strcmp
(
argv
[
i
],
"-n"
))
{
else
if
(
!
strcmp
(
argv
[
i
],
"--dry-run"
)
||
!
strcmp
(
argv
[
i
],
"-n"
))
*
options
|=
SKIP
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--update"
)
||
!
strcmp
(
argv
[
i
],
"-u"
))
{
else
if
(
!
strcmp
(
argv
[
i
],
"--update"
)
||
!
strcmp
(
argv
[
i
],
"-u"
))
*
options
|=
UPDATE
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"-h"
))
{
else
if
(
!
strcmp
(
argv
[
i
],
"-h"
))
{
print_usage
();
break
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--"
))
{
}
else
if
(
!
strcmp
(
argv
[
i
],
"--"
))
{
i
++
;
break
;
}
else
{
}
else
{
fprintf
(
stderr
,
"Unsupported option %s.
\n
"
,
argv
[
i
]);
print_usage
();
}
}
if
(
argc
<=
i
)
if
(
argc
<=
i
)
print_usage
();
*
count
=
i
;
...
...
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