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
e568bedf
Commit
e568bedf
authored
Oct 30, 2013
by
Ben Straub
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add.c example: deploy helpers, reorg
parent
a8422f92
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
57 deletions
+61
-57
examples/add.c
+61
-57
No files found.
examples/add.c
View file @
e568bedf
...
...
@@ -14,19 +14,49 @@ struct print_payload {
git_repository
*
repo
;
};
void
init_array
(
git_strarray
*
array
,
int
argc
,
char
**
argv
)
/* Forward declarations for helpers */
static
void
parse_opts
(
int
*
options
,
int
*
count
,
int
argc
,
char
*
argv
[]);
void
init_array
(
git_strarray
*
array
,
int
argc
,
char
**
argv
);
int
print_matched_cb
(
const
char
*
path
,
const
char
*
matched_pathspec
,
void
*
payload
);
int
main
(
int
argc
,
char
**
argv
)
{
unsigned
int
i
;
git_index_matched_path_cb
matched_cb
=
NULL
;
git_repository
*
repo
=
NULL
;
git_index
*
index
;
git_strarray
array
=
{
0
};
int
options
=
0
,
count
=
0
;
struct
print_payload
payload
=
{
0
};
array
->
count
=
argc
;
array
->
strings
=
malloc
(
sizeof
(
char
*
)
*
array
->
count
);
assert
(
array
->
strings
!=
NULL
);
git_threads_init
();
for
(
i
=
0
;
i
<
array
->
count
;
i
++
)
{
array
->
strings
[
i
]
=
argv
[
i
];
parse_opts
(
&
options
,
&
count
,
argc
,
argv
);
init_array
(
&
array
,
argc
-
count
,
argv
+
count
);
check_lg2
(
git_repository_open
(
&
repo
,
"."
),
"No git repository"
,
NULL
);
check_lg2
(
git_repository_index
(
&
index
,
repo
),
"Could not open repository index"
,
NULL
);
if
(
options
&
VERBOSE
||
options
&
SKIP
)
{
matched_cb
=
&
print_matched_cb
;
}
return
;
payload
.
options
=
options
;
payload
.
repo
=
repo
;
if
(
options
&
UPDATE
)
{
git_index_update_all
(
index
,
&
array
,
matched_cb
,
&
payload
);
}
else
{
git_index_add_all
(
index
,
&
array
,
0
,
matched_cb
,
&
payload
);
}
git_index_write
(
index
);
git_index_free
(
index
);
git_repository_free
(
repo
);
git_threads_shutdown
();
return
0
;
}
int
print_matched_cb
(
const
char
*
path
,
const
char
*
matched_pathspec
,
void
*
payload
)
...
...
@@ -55,36 +85,46 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
return
ret
;
}
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
;
}
void
print_usage
(
void
)
{
fprintf
(
stderr
,
"usage: add [options] [--] file-spec [file-spec] [...]
\n\n
"
);
fprintf
(
stderr
,
"
\t
-n, --dry-run dry run
\n
"
);
fprintf
(
stderr
,
"
\t
-v, --verbose be verbose
\n
"
);
fprintf
(
stderr
,
"
\t
-u, --update update tracked files
\n
"
);
exit
(
1
);
}
int
main
(
int
argc
,
char
**
argv
)
static
void
parse_opts
(
int
*
options
,
int
*
count
,
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
};
int
i
;
for
(
i
=
1
;
i
<
argc
;
++
i
)
{
if
(
argv
[
i
][
0
]
!=
'-'
)
{
break
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--verbose"
)
||
!
strcmp
(
argv
[
i
],
"-v"
))
{
options
|=
VERBOSE
;
*
options
|=
VERBOSE
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--dry-run"
)
||
!
strcmp
(
argv
[
i
],
"-n"
))
{
options
|=
SKIP
;
*
options
|=
SKIP
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--update"
)
||
!
strcmp
(
argv
[
i
],
"-u"
))
{
options
|=
UPDATE
;
*
options
|=
UPDATE
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"-h"
))
{
print_usage
();
...
...
@@ -97,47 +137,11 @@ int main (int argc, char** argv)
else
{
fprintf
(
stderr
,
"Unsupported option %s.
\n
"
,
argv
[
i
]);
print_usage
();
return
1
;
}
}
if
(
argc
<=
i
)
{
if
(
argc
<=
i
)
print_usage
();
return
1
;
}
git_threads_init
();
init_array
(
&
array
,
argc
-
i
,
argv
+
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
;
}
if
(
options
&
VERBOSE
||
options
&
SKIP
)
{
matched_cb
=
&
print_matched_cb
;
}
payload
.
options
=
options
;
payload
.
repo
=
repo
;
if
(
options
&
UPDATE
)
{
git_index_update_all
(
index
,
&
array
,
matched_cb
,
&
payload
);
}
else
{
git_index_add_all
(
index
,
&
array
,
0
,
matched_cb
,
&
payload
);
}
git_index_write
(
index
);
git_index_free
(
index
);
git_repository_free
(
repo
);
git_threads_shutdown
();
return
0
;
*
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