Skip to content

Commit ead1078

Browse files
committed
examples: create common lg2 executable
Inside of our networking example code, we have a git2 executable that acts as an entry point to all the different network examples. As such, it is kind of the same like the normal git(1) executable in that it simply arbitrates to the respective subcommands. Let's extend this approach and merge all examples into a single standalone lg2 executable. Instead of building an executable for all the existing examples we have, we now bundle them all inside of the lg2 one and let them be callable via subcommands. In the process, we can get rid of duplicated library initialization, deinitialization and repository discovery code. Instead of having each subcommand handle these on its own, we simply do it inside of the single main function now.
1 parent 7562422 commit ead1078

27 files changed

+173
-294
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 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: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,18 @@ static void parse_opts(int *options, int *count, int argc, char *argv[]);
3131
void init_array(git_strarray *array, int argc, char **argv);
3232
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
3333

34-
int main (int argc, char** argv)
34+
int lg2_add(git_repository *repo, int argc, char** argv)
3535
{
3636
git_index_matched_path_cb matched_cb = NULL;
37-
git_repository *repo = NULL;
3837
git_index *index;
3938
git_strarray array = {0};
4039
int options = 0, count = 0;
4140
struct print_payload payload = {0};
4241

43-
git_libgit2_init();
44-
4542
parse_opts(&options, &count, argc, argv);
4643

4744
init_array(&array, argc-count, argv+count);
4845

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

5248
if (options&VERBOSE || options&SKIP) {
@@ -64,9 +60,6 @@ int main (int argc, char** argv)
6460

6561
git_index_write(index);
6662
git_index_free(index);
67-
git_repository_free(repo);
68-
69-
git_libgit2_shutdown();
7063

7164
return 0;
7265
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../common.h"
1+
#include "common.h"
22

33
typedef struct progress_data {
44
git_transfer_progress fetch_progress;
@@ -63,7 +63,7 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa
6363
}
6464

6565

66-
int do_clone(git_repository *repo, int argc, char **argv)
66+
int lg2_clone(git_repository *repo, int argc, char **argv)
6767
{
6868
progress_data pd = {{0}};
6969
git_repository *cloned_repo = NULL;

examples/common.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,32 @@
2626
#endif
2727
#endif
2828

29+
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
2930
#define UNUSED(x) (void)(x)
3031

32+
extern int lg2_add(git_repository *repo, int argc, char **argv);
33+
extern int lg2_blame(git_repository *repo, int argc, char **argv);
34+
extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
35+
extern int lg2_checkout(git_repository *repo, int argc, char **argv);
36+
extern int lg2_clone(git_repository *repo, int argc, char **argv);
37+
extern int lg2_describe(git_repository *repo, int argc, char **argv);
38+
extern int lg2_diff(git_repository *repo, int argc, char **argv);
39+
extern int lg2_fetch(git_repository *repo, int argc, char **argv);
40+
extern int lg2_for_each_ref(git_repository *repo, int argc, char **argv);
41+
extern int lg2_general(git_repository *repo, int argc, char **argv);
42+
extern int lg2_index_pack(git_repository *repo, int argc, char **argv);
43+
extern int lg2_init(git_repository *repo, int argc, char **argv);
44+
extern int lg2_log(git_repository *repo, int argc, char **argv);
45+
extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
46+
extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
47+
extern int lg2_merge(git_repository *repo, int argc, char **argv);
48+
extern int lg2_remote(git_repository *repo, int argc, char **argv);
49+
extern int lg2_rev_list(git_repository *repo, int argc, char **argv);
50+
extern int lg2_rev_parse(git_repository *repo, int argc, char **argv);
51+
extern int lg2_show_index(git_repository *repo, int argc, char **argv);
52+
extern int lg2_status(git_repository *repo, int argc, char **argv);
53+
extern int lg2_tag(git_repository *repo, int argc, char **argv);
54+
3155
/**
3256
* Check libgit2 error code, printing error to stderr on failure and
3357
* exiting the program.
@@ -142,8 +166,3 @@ extern int cred_acquire_cb(git_cred **out,
142166
const char *username_from_url,
143167
unsigned int allowed_types,
144168
void *payload);
145-
146-
extern int ls_remote(git_repository *repo, int argc, char **argv);
147-
extern int fetch(git_repository *repo, int argc, char **argv);
148-
extern int index_pack(git_repository *repo, int argc, char **argv);
149-
extern int do_clone(git_repository *repo, int argc, char **argv);

examples/describe.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,14 @@ static void describe_options_init(describe_options *opts)
152152
git_describe_init_format_options(&opts->format_options, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
153153
}
154154

155-
int main(int argc, char **argv)
155+
int lg2_describe(git_repository *repo, int argc, char **argv)
156156
{
157-
git_repository *repo;
158157
describe_options opts;
159158

160-
git_libgit2_init();
161-
162-
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
163-
"Could not open repository", NULL);
164-
165159
describe_options_init(&opts);
166160
parse_options(&opts, argc, argv);
167161

168162
do_describe(repo, &opts);
169163

170-
git_repository_free(repo);
171-
git_libgit2_shutdown();
172-
173164
return 0;
174165
}

examples/diff.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,17 @@ static int color_printer(
6767
const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
6868
static void diff_print_stats(git_diff *diff, struct opts *o);
6969

70-
int main(int argc, char *argv[])
70+
int lg2_diff(git_repository *repo, int argc, char *argv[])
7171
{
72-
git_repository *repo = NULL;
7372
git_tree *t1 = NULL, *t2 = NULL;
7473
git_diff *diff;
7574
struct opts o = {
7675
GIT_DIFF_OPTIONS_INIT, GIT_DIFF_FIND_OPTIONS_INIT,
7776
-1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
7877
};
7978

80-
git_libgit2_init();
81-
8279
parse_opts(&o, argc, argv);
8380

84-
check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
85-
"Could not open repository", o.dir);
86-
8781
/**
8882
* Possible argument patterns:
8983
*
@@ -157,13 +151,9 @@ int main(int argc, char *argv[])
157151
}
158152

159153
/** Cleanup before exiting. */
160-
161154
git_diff_free(diff);
162155
git_tree_free(t1);
163156
git_tree_free(t2);
164-
git_repository_free(repo);
165-
166-
git_libgit2_shutdown();
167157

168158
return 0;
169159
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../common.h"
1+
#include "common.h"
22

33
static int progress_cb(const char *str, int len, void *data)
44
{
@@ -54,7 +54,7 @@ static int transfer_progress_cb(const git_transfer_progress *stats, void *payloa
5454
}
5555

5656
/** Entry point for this command */
57-
int fetch(git_repository *repo, int argc, char **argv)
57+
int lg2_fetch(git_repository *repo, int argc, char **argv)
5858
{
5959
git_remote *remote = NULL;
6060
const git_transfer_progress *stats;

0 commit comments

Comments
 (0)