Skip to content

Commit 68ba2e8

Browse files
authored
Merge pull request libgit2#4956 from pks-t/pks/examples-cgit2-standalone
examples: produce single cgit2 binary
2 parents 9eb098d + 6cf4772 commit 68ba2e8

32 files changed

+316
-569
lines changed

examples/CMakeLists.txt

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
22
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
33

4-
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
5-
ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
6-
SET_TARGET_PROPERTIES(cgit2 PROPERTIES C_STANDARD 90)
4+
FILE(GLOB LG2_SOURCES *.c)
5+
ADD_EXECUTABLE(lg2 ${LG2_SOURCES})
6+
SET_TARGET_PROPERTIES(lg2 PROPERTIES C_STANDARD 90)
77

88
# Ensure that we do not use deprecated functions internally
99
ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
1010

1111
IF(WIN32 OR ANDROID)
12-
TARGET_LINK_LIBRARIES(cgit2 git2)
12+
TARGET_LINK_LIBRARIES(lg2 git2)
1313
ELSE()
14-
TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
14+
TARGET_LINK_LIBRARIES(lg2 git2 pthread)
1515
ENDIF()
16-
17-
FILE(GLOB SRC_EXAMPLE_APPS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c)
18-
FOREACH(src_app ${SRC_EXAMPLE_APPS})
19-
STRING(REPLACE ".c" "" app_name ${src_app})
20-
IF(NOT ${app_name} STREQUAL "common")
21-
ADD_EXECUTABLE(${app_name} ${src_app} "common.c")
22-
TARGET_LINK_LIBRARIES(${app_name} git2)
23-
SET_TARGET_PROPERTIES(${app_name} PROPERTIES C_STANDARD 90)
24-
ENDIF()
25-
ENDFOREACH()

examples/add.c

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
#include "common.h"
1616
#include <assert.h>
1717

18+
/**
19+
* The following example demonstrates how to add files with libgit2.
20+
*
21+
* It will use the repository in the current working directory, and act
22+
* on files passed as its parameters.
23+
*
24+
* Recognized options are:
25+
* -v/--verbose: show the file's status after acting on it.
26+
* -n/--dry-run: do not actually change the index.
27+
* -u/--update: update the index instead of adding to it.
28+
*/
29+
1830
enum print_options {
1931
SKIP = 1,
2032
VERBOSE = 2,
@@ -31,65 +43,66 @@ static void parse_opts(int *options, int *count, int argc, char *argv[]);
3143
void init_array(git_strarray *array, int argc, char **argv);
3244
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
3345

34-
int main (int argc, char** argv)
46+
int lg2_add(git_repository *repo, int argc, char** argv)
3547
{
3648
git_index_matched_path_cb matched_cb = NULL;
37-
git_repository *repo = NULL;
3849
git_index *index;
3950
git_strarray array = {0};
4051
int options = 0, count = 0;
4152
struct print_payload payload = {0};
4253

43-
git_libgit2_init();
44-
4554
parse_opts(&options, &count, argc, argv);
46-
4755
init_array(&array, argc-count, argv+count);
4856

49-
check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
5057
check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
5158

52-
if (options&VERBOSE || options&SKIP) {
59+
/* Setup a callback if the requested options need it */
60+
if ((options & VERBOSE) || (options & SKIP)) {
5361
matched_cb = &print_matched_cb;
5462
}
5563

64+
/* Perform the requested action with the index and files */
5665
payload.options = options;
5766
payload.repo = repo;
5867

59-
if (options&UPDATE) {
68+
if (options & UPDATE) {
6069
git_index_update_all(index, &array, matched_cb, &payload);
6170
} else {
6271
git_index_add_all(index, &array, 0, matched_cb, &payload);
6372
}
6473

74+
/* Cleanup memory */
6575
git_index_write(index);
6676
git_index_free(index);
67-
git_repository_free(repo);
68-
69-
git_libgit2_shutdown();
7077

7178
return 0;
7279
}
7380

81+
/*
82+
* This callback is called for each file under consideration by
83+
* git_index_(update|add)_all above.
84+
* It makes uses of the callback's ability to abort the action.
85+
*/
7486
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
7587
{
7688
struct print_payload p = *(struct print_payload*)(payload);
7789
int ret;
7890
unsigned status;
7991
(void)matched_pathspec;
8092

93+
/* Get the file status */
8194
if (git_status_file(&status, p.repo, path)) {
8295
return -1;
8396
}
8497

85-
if (status & GIT_STATUS_WT_MODIFIED || status & GIT_STATUS_WT_NEW) {
98+
if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
8699
printf("add '%s'\n", path);
87100
ret = 0;
88101
} else {
89102
ret = 1;
90103
}
91104

92-
if(p.options & SKIP) {
105+
if ((p.options & SKIP)) {
93106
ret = 1;
94107
}
95108

@@ -101,11 +114,11 @@ void init_array(git_strarray *array, int argc, char **argv)
101114
unsigned int i;
102115

103116
array->count = argc;
104-
array->strings = malloc(sizeof(char*) * array->count);
105-
assert(array->strings!=NULL);
117+
array->strings = calloc(array->count, sizeof(char *));
118+
assert(array->strings != NULL);
106119

107-
for(i=0; i<array->count; i++) {
108-
array->strings[i]=argv[i];
120+
for (i = 0; i < array->count; i++) {
121+
array->strings[i] = argv[i];
109122
}
110123

111124
return;
@@ -125,33 +138,27 @@ static void parse_opts(int *options, int *count, int argc, char *argv[])
125138
int i;
126139

127140
for (i = 1; i < argc; ++i) {
128-
if (argv[i][0] != '-') {
141+
if (argv[i][0] != '-')
129142
break;
130-
}
131-
else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
143+
else if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v"))
132144
*options |= VERBOSE;
133-
}
134-
else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
145+
else if (!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n"))
135146
*options |= SKIP;
136-
}
137-
else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
147+
else if (!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u"))
138148
*options |= UPDATE;
139-
}
140-
else if(!strcmp(argv[i], "-h")) {
149+
else if (!strcmp(argv[i], "-h")) {
141150
print_usage();
142151
break;
143-
}
144-
else if(!strcmp(argv[i], "--")) {
152+
} else if (!strcmp(argv[i], "--")) {
145153
i++;
146154
break;
147-
}
148-
else {
155+
} else {
149156
fprintf(stderr, "Unsupported option %s.\n", argv[i]);
150157
print_usage();
151158
}
152159
}
153160

154-
if (argc<=i)
161+
if (argc <= i)
155162
print_usage();
156163

157164
*count = i;

examples/blame.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,24 @@ struct opts {
3535
};
3636
static void parse_opts(struct opts *o, int argc, char *argv[]);
3737

38-
int main(int argc, char *argv[])
38+
int lg2_blame(git_repository *repo, int argc, char *argv[])
3939
{
4040
int line, break_on_null_hunk;
4141
size_t i, rawsize;
4242
char spec[1024] = {0};
4343
struct opts o = {0};
4444
const char *rawdata;
45-
git_repository *repo = NULL;
4645
git_revspec revspec = {0};
4746
git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
4847
git_blame *blame = NULL;
4948
git_blob *blob;
5049
git_object *obj;
5150

52-
git_libgit2_init();
53-
5451
parse_opts(&o, argc, argv);
5552
if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
5653
if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
5754
if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT;
5855

59-
/** Open the repository. */
60-
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository", NULL);
61-
6256
/**
6357
* The commit range comes in "commitish" form. Use the rev-parse API to
6458
* nail down the end points.
@@ -131,9 +125,6 @@ int main(int argc, char *argv[])
131125
/** Cleanup. */
132126
git_blob_free(blob);
133127
git_blame_free(blame);
134-
git_repository_free(repo);
135-
136-
git_libgit2_shutdown();
137128

138129
return 0;
139130
}

examples/cat-file.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,14 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
120120

121121

122122
/** Entry point for this command */
123-
int main(int argc, char *argv[])
123+
int lg2_cat_file(git_repository *repo, int argc, char *argv[])
124124
{
125-
git_repository *repo;
126125
struct opts o = { ".", NULL, 0, 0 };
127126
git_object *obj = NULL;
128127
char oidstr[GIT_OID_HEXSZ + 1];
129128

130-
git_libgit2_init();
131-
132129
parse_opts(&o, argc, argv);
133130

134-
check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
135-
"Could not open repository", NULL);
136131
check_lg2(git_revparse_single(&obj, repo, o.rev),
137132
"Could not resolve", o.rev);
138133

@@ -188,9 +183,6 @@ int main(int argc, char *argv[])
188183
}
189184

190185
git_object_free(obj);
191-
git_repository_free(repo);
192-
193-
git_libgit2_shutdown();
194186

195187
return 0;
196188
}

examples/checkout.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ static int perform_checkout_ref(git_repository *repo, git_annotated_commit *targ
172172
}
173173

174174
/** That example's entry point */
175-
int main(int argc, char **argv)
175+
int lg2_checkout(git_repository *repo, int argc, char **argv)
176176
{
177-
git_repository *repo = NULL;
178177
struct args_info args = ARGS_INFO_INIT;
179178
checkout_options opts;
180179
git_repository_state_t state;
@@ -185,15 +184,6 @@ int main(int argc, char **argv)
185184
/** Parse our command line options */
186185
parse_options(&path, &opts, &args);
187186

188-
/** Initialize the library */
189-
err = git_libgit2_init();
190-
if (!err)
191-
check_lg2(err, "Failed to initialize libgit2", NULL);
192-
193-
/** Open the repository corresponding to the options */
194-
check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
195-
"Could not open repository", NULL);
196-
197187
/** Make sure we're not about to checkout while something else is going on */
198188
state = git_repository_state(repo);
199189
if (state != GIT_REPOSITORY_STATE_NONE) {
@@ -228,8 +218,5 @@ int main(int argc, char **argv)
228218
cleanup:
229219
git_annotated_commit_free(checkout_target);
230220

231-
git_repository_free(repo);
232-
git_libgit2_shutdown();
233-
234221
return err;
235222
}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
#include "common.h"
2-
#include <git2.h>
3-
#include <git2/clone.h>
4-
#include <stdio.h>
5-
#include <stdlib.h>
6-
#include <string.h>
7-
#ifndef _WIN32
8-
# include <pthread.h>
9-
# include <unistd.h>
10-
#endif
112

123
typedef struct progress_data {
134
git_transfer_progress fetch_progress;
@@ -72,7 +63,7 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa
7263
}
7364

7465

75-
int do_clone(git_repository *repo, int argc, char **argv)
66+
int lg2_clone(git_repository *repo, int argc, char **argv)
7667
{
7768
progress_data pd = {{0}};
7869
git_repository *cloned_repo = NULL;

0 commit comments

Comments
 (0)