Skip to content

Commit 567649f

Browse files
author
Vicent Martí
committed
Merge pull request libgit2#1916 from libgit2/simplify-examples
Fix examples to make the important stuff more obvious
2 parents 948f00b + 4f62d55 commit 567649f

16 files changed

+1255
-950
lines changed

examples/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ ENDIF()
99
FILE(GLOB SRC_EXAMPLE_APPS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c)
1010
FOREACH(src_app ${SRC_EXAMPLE_APPS})
1111
STRING(REPLACE ".c" "" app_name ${src_app})
12-
ADD_EXECUTABLE(${app_name} ${src_app})
13-
TARGET_LINK_LIBRARIES(${app_name} git2)
12+
IF(NOT ${app_name} STREQUAL "common")
13+
ADD_EXECUTABLE(${app_name} ${src_app} "common.c")
14+
TARGET_LINK_LIBRARIES(${app_name} git2)
15+
ENDIF()
1416
ENDFOREACH()

examples/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ APPS = general showindex diff rev-list cat-file status log rev-parse init
88
all: $(APPS)
99

1010
% : %.c
11-
$(CC) -o $@ $(CFLAGS) $< $(LFLAGS)
11+
$(CC) -o $@ common.c $(CFLAGS) $< $(LFLAGS)
1212

1313
clean:
1414
$(RM) $(APPS)

examples/README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
libgit2 examples
22
================
33

4-
These examples are meant as thin, easy-to-read snippets for Docurium
5-
(https://github.com/github/docurium) rather than full-blown
6-
implementations of Git commands. They are not vetted as carefully
7-
for bugs, error handling, or cross-platform compatibility as the
8-
rest of the code in libgit2, so copy with some caution.
4+
These examples are a mixture of basic emulation of core Git command line
5+
functions and simple snippets demonstrating libgit2 API usage (for use
6+
with Docurium). As a whole, they are not vetted carefully for bugs, error
7+
handling, and cross-platform compatibility in the same manner as the rest
8+
of the code in libgit2, so copy with caution.
99

10-
For HTML versions, check "Examples" at http://libgit2.github.com/libgit2
10+
That being said, you are welcome to copy code from these examples as
11+
desired when using libgit2.
1112

13+
For annotated HTML versions, see the "Examples" section of:
14+
15+
http://libgit2.github.com/libgit2
16+
17+
such as:
18+
19+
http://libgit2.github.com/libgit2/ex/HEAD/general.html

examples/add.c

+69-60
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
#include <git2.h>
2-
#include <stdio.h>
3-
#include <string.h>
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "common.h"
49
#include <assert.h>
510

611
enum print_options {
@@ -14,19 +19,49 @@ struct print_payload {
1419
git_repository *repo;
1520
};
1621

17-
void init_array(git_strarray *array, int argc, char **argv)
22+
/* Forward declarations for helpers */
23+
static void parse_opts(int *options, int *count, int argc, char *argv[]);
24+
void init_array(git_strarray *array, int argc, char **argv);
25+
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
26+
27+
int main (int argc, char** argv)
1828
{
19-
unsigned int i;
29+
git_index_matched_path_cb matched_cb = NULL;
30+
git_repository *repo = NULL;
31+
git_index *index;
32+
git_strarray array = {0};
33+
int options = 0, count = 0;
34+
struct print_payload payload = {0};
2035

21-
array->count = argc;
22-
array->strings = malloc(sizeof(char*) * array->count);
23-
assert(array->strings!=NULL);
36+
git_threads_init();
2437

25-
for(i=0; i<array->count; i++) {
26-
array->strings[i]=argv[i];
38+
parse_opts(&options, &count, argc, argv);
39+
40+
init_array(&array, argc-count, argv+count);
41+
42+
check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
43+
check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
44+
45+
if (options&VERBOSE || options&SKIP) {
46+
matched_cb = &print_matched_cb;
2747
}
2848

29-
return;
49+
payload.options = options;
50+
payload.repo = repo;
51+
52+
if (options&UPDATE) {
53+
git_index_update_all(index, &array, matched_cb, &payload);
54+
} else {
55+
git_index_add_all(index, &array, 0, matched_cb, &payload);
56+
}
57+
58+
git_index_write(index);
59+
git_index_free(index);
60+
git_repository_free(repo);
61+
62+
git_threads_shutdown();
63+
64+
return 0;
3065
}
3166

3267
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
@@ -55,36 +90,46 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
5590
return ret;
5691
}
5792

93+
void init_array(git_strarray *array, int argc, char **argv)
94+
{
95+
unsigned int i;
96+
97+
array->count = argc;
98+
array->strings = malloc(sizeof(char*) * array->count);
99+
assert(array->strings!=NULL);
100+
101+
for(i=0; i<array->count; i++) {
102+
array->strings[i]=argv[i];
103+
}
104+
105+
return;
106+
}
107+
58108
void print_usage(void)
59109
{
60110
fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
61111
fprintf(stderr, "\t-n, --dry-run dry run\n");
62112
fprintf(stderr, "\t-v, --verbose be verbose\n");
63113
fprintf(stderr, "\t-u, --update update tracked files\n");
114+
exit(1);
64115
}
65116

66-
67-
int main (int argc, char** argv)
117+
static void parse_opts(int *options, int *count, int argc, char *argv[])
68118
{
69-
git_index_matched_path_cb matched_cb = NULL;
70-
git_repository *repo = NULL;
71-
git_index *index;
72-
git_strarray array = {0};
73-
int i, options = 0;
74-
struct print_payload payload = {0};
119+
int i;
75120

76121
for (i = 1; i < argc; ++i) {
77122
if (argv[i][0] != '-') {
78123
break;
79124
}
80125
else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
81-
options |= VERBOSE;
126+
*options |= VERBOSE;
82127
}
83128
else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
84-
options |= SKIP;
129+
*options |= SKIP;
85130
}
86131
else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
87-
options |= UPDATE;
132+
*options |= UPDATE;
88133
}
89134
else if(!strcmp(argv[i], "-h")) {
90135
print_usage();
@@ -97,47 +142,11 @@ int main (int argc, char** argv)
97142
else {
98143
fprintf(stderr, "Unsupported option %s.\n", argv[i]);
99144
print_usage();
100-
return 1;
101145
}
102146
}
103147

104-
if (argc<=i) {
148+
if (argc<=i)
105149
print_usage();
106-
return 1;
107-
}
108150

109-
git_threads_init();
110-
111-
init_array(&array, argc-i, argv+i);
112-
113-
if (git_repository_open(&repo, ".") < 0) {
114-
fprintf(stderr, "No git repository\n");
115-
return 1;
116-
}
117-
118-
if (git_repository_index(&index, repo) < 0) {
119-
fprintf(stderr, "Could not open repository index\n");
120-
return 1;
121-
}
122-
123-
if (options&VERBOSE || options&SKIP) {
124-
matched_cb = &print_matched_cb;
125-
}
126-
127-
payload.options = options;
128-
payload.repo = repo;
129-
130-
if (options&UPDATE) {
131-
git_index_update_all(index, &array, matched_cb, &payload);
132-
} else {
133-
git_index_add_all(index, &array, 0, matched_cb, &payload);
134-
}
135-
136-
git_index_write(index);
137-
git_index_free(index);
138-
git_repository_free(repo);
139-
140-
git_threads_shutdown();
141-
142-
return 0;
151+
*count = i;
143152
}

0 commit comments

Comments
 (0)