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
62020aa8
Commit
62020aa8
authored
Sep 02, 2013
by
Krzysztof Adamski
Committed by
Russell Belfer
Sep 09, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding add example.
parent
e0b4a8ac
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
121 additions
and
0 deletions
+121
-0
examples/add.c
+121
-0
No files found.
examples/add.c
0 → 100644
View file @
62020aa8
#include <git2.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
enum
print_options
{
SKIP
=
1
,
VERBOSE
=
2
,
};
struct
print_payload
{
enum
print_options
options
;
git_repository
*
repo
;
};
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
);
for
(
i
=
0
;
i
<
array
->
count
;
i
++
)
{
array
->
strings
[
i
]
=
argv
[
i
];
}
return
;
}
int
print_matched_cb
(
const
char
*
path
,
const
char
*
matched_pathspec
,
void
*
payload
)
{
(
void
)
matched_pathspec
;
struct
print_payload
p
=
*
(
struct
print_payload
*
)(
payload
);
int
ret
;
git_status_t
status
;
if
(
git_status_file
(
&
status
,
p
.
repo
,
path
))
{
return
-
1
;
//abort
}
if
(
status
&
GIT_STATUS_WT_MODIFIED
||
status
&
GIT_STATUS_WT_NEW
)
{
if
(
p
.
options
&
VERBOSE
||
p
.
options
&
SKIP
)
{
printf
(
"add '%s'
\n
"
,
path
);
}
ret
=
0
;
}
else
{
ret
=
1
;
}
if
(
p
.
options
&
SKIP
)
{
ret
=
1
;
}
return
ret
;
}
int
main
(
int
argc
,
char
**
argv
)
{
git_index_matched_path_cb
matched_cb
=
NULL
;
git_repository
*
repo
=
NULL
;
git_index
*
index
;
git_strarray
array
=
{
0
};
int
i
,
options
=
0
;
struct
print_payload
payload
=
{
0
};
if
(
argc
<
2
)
{
fprintf
(
stderr
,
"usage: add file-spec [file-spec] [...]
\n
"
);
return
1
;
}
for
(
i
=
1
;
i
<
argc
;
++
i
)
{
if
(
argv
[
i
][
0
]
!=
'-'
)
{
break
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--verbose"
)
||
!
strcmp
(
argv
[
i
],
"-v"
))
{
options
|=
VERBOSE
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--dry-run"
)
||
!
strcmp
(
argv
[
i
],
"-n"
))
{
options
|=
SKIP
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--"
))
{
break
;
}
else
{
fprintf
(
stderr
,
"Unsupported option %s.
\n
"
,
argv
[
i
]);
return
1
;
}
}
init_array
(
&
array
,
argc
-
i
,
argv
+
i
);
printf
(
"args:
\n
"
);
for
(
i
=
0
;
i
<
array
.
count
;
i
++
)
{
printf
(
" - %s
\n
"
,
array
.
strings
[
i
]);
}
if
(
git_repository_open
(
&
repo
,
"."
)
<
0
)
{
fprintf
(
stderr
,
"No git repository
\n
"
);
return
1
;
}
if
(
git_repository_index
(
&
index
,
repo
)
<
0
)
{
fprintf
(
stderr
,
"Could not open repository index
\n
"
);
return
1
;
}
matched_cb
=
&
print_matched_cb
;
payload
.
options
=
options
;
payload
.
repo
=
repo
;
git_index_add_all
(
index
,
&
array
,
0
,
matched_cb
,
&
payload
);
git_index_write
(
index
);
git_index_free
(
index
);
git_repository_free
(
repo
);
return
0
;
}
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