Commit 4f24a932f098c011a229c370f76eda11db3e4b4c

Edward Thomson 2021-09-14T07:45:50

Merge pull request #6031 from libgit2/ethomson/extensions Support custom git extensions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
diff --git a/include/git2/common.h b/include/git2/common.h
index d278c01..2ee8290 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -209,7 +209,9 @@ typedef enum {
 	GIT_OPT_GET_MWINDOW_FILE_LIMIT,
 	GIT_OPT_SET_MWINDOW_FILE_LIMIT,
 	GIT_OPT_SET_ODB_PACKED_PRIORITY,
-	GIT_OPT_SET_ODB_LOOSE_PRIORITY
+	GIT_OPT_SET_ODB_LOOSE_PRIORITY,
+	GIT_OPT_GET_EXTENSIONS,
+	GIT_OPT_SET_EXTENSIONS
 } git_libgit2_opt_t;
 
 /**
@@ -431,6 +433,22 @@ typedef enum {
  *      > Override the default priority of the loose ODB backend which
  *      > is added when default backends are assigned to a repository
  *
+ *   opts(GIT_OPT_GET_EXTENSIONS, git_strarray *out)
+ *      > Returns the list of git extensions that are supported.  This
+ *      > is the list of built-in extensions supported by libgit2 and
+ *      > custom extensions that have been added with
+ *      > `GIT_OPT_SET_EXTENSIONS`.  Extensions that have been negated
+ *      > will not be returned.  The returned list should be released
+ *      > with `git_strarray_dispose`.
+ *
+ *   opts(GIT_OPT_SET_EXTENSIONS, const char **extensions, size_t len)
+ *      > Set that the given git extensions are supported by the caller.
+ *      > Extensions supported by libgit2 may be negated by prefixing
+ *      > them with a `!`.  For example: setting extensions to
+ *      > { "!noop", "newext" } indicates that the caller does not want
+ *      > to support repositories with the `noop` extension but does want
+ *      > to support repositories with the `newext` extension.
+ *
  * @param option Option key
  * @param ... value to set the option
  * @return 0 on success, <0 on failure
diff --git a/src/libgit2.c b/src/libgit2.c
index 09f7ab5..cc793b4 100644
--- a/src/libgit2.c
+++ b/src/libgit2.c
@@ -52,6 +52,7 @@ static void libgit2_settings_global_shutdown(void)
 {
 	git__free(git__user_agent);
 	git__free(git__ssl_ciphers);
+	git_repository__free_extensions();
 }
 
 static int git_libgit2_settings_global_init(void)
@@ -367,6 +368,28 @@ int git_libgit2_opts(int key, ...)
 		git_odb__loose_priority = va_arg(ap, int);
 		break;
 
+	case GIT_OPT_SET_EXTENSIONS:
+		{
+			const char **extensions = va_arg(ap, const char **);
+			size_t len = va_arg(ap, size_t);
+			error = git_repository__set_extensions(extensions, len);
+		}
+		break;
+
+	case GIT_OPT_GET_EXTENSIONS:
+		{
+			git_strarray *out = va_arg(ap, git_strarray *);
+			char **extensions;
+			size_t len;
+
+			if ((error = git_repository__extensions(&extensions, &len)) < 0)
+				break;
+
+			out->strings = extensions;
+			out->count = len;
+		}
+		break;
+
 	default:
 		git_error_set(GIT_ERROR_INVALID, "invalid option key");
 		error = -1;
diff --git a/src/repository.c b/src/repository.c
index 4b63a1f..8f0f477 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1427,15 +1427,60 @@ static int check_repositoryformatversion(int *version, git_config *config)
 	return 0;
 }
 
+static const char *builtin_extensions[] = {
+	"noop"
+};
+
+static git_vector user_extensions = GIT_VECTOR_INIT;
+
 static int check_valid_extension(const git_config_entry *entry, void *payload)
 {
+	git_buf cfg = GIT_BUF_INIT;
+	bool reject;
+	const char *extension;
+	size_t i;
+	int error = 0;
+
 	GIT_UNUSED(payload);
 
-	if (!strcmp(entry->name, "extensions.noop"))
-		return 0;
+	git_vector_foreach (&user_extensions, i, extension) {
+		git_buf_clear(&cfg);
+
+		/*
+		 * Users can specify that they don't want to support an
+		 * extension with a '!' prefix.
+		 */
+		if ((reject = (extension[0] == '!')) == true)
+			extension = &extension[1];
+
+		if ((error = git_buf_printf(&cfg, "extensions.%s", extension)) < 0)
+			goto done;
 
+		if (strcmp(entry->name, cfg.ptr) == 0) {
+			if (reject)
+				goto fail;
+
+			goto done;
+		}
+	}
+
+	for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) {
+		extension = builtin_extensions[i];
+
+		if ((error = git_buf_printf(&cfg, "extensions.%s", extension)) < 0)
+			goto done;
+
+		if (strcmp(entry->name, cfg.ptr) == 0)
+			goto done;
+	}
+
+fail:
 	git_error_set(GIT_ERROR_REPOSITORY, "unsupported extension name %s", entry->name);
-	return -1;
+	error = -1;
+
+done:
+	git_buf_dispose(&cfg);
+	return error;
 }
 
 static int check_extensions(git_config *config, int version)
@@ -1446,6 +1491,70 @@ static int check_extensions(git_config *config, int version)
 	return git_config_foreach_match(config, "^extensions\\.", check_valid_extension, NULL);
 }
 
+int git_repository__extensions(char ***out, size_t *out_len)
+{
+	git_vector extensions;
+	const char *builtin, *user;
+	char *extension;
+	size_t i, j;
+
+	if (git_vector_init(&extensions, 8, NULL) < 0)
+		return -1;
+
+	for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) {
+		bool match = false;
+
+		builtin = builtin_extensions[i];
+
+		git_vector_foreach (&user_extensions, j, user) {
+			if (user[0] == '!' && strcmp(builtin, &user[1]) == 0) {
+				match = true;
+				break;
+			}
+		}
+
+		if (match)
+			continue;
+
+		if ((extension = git__strdup(builtin)) == NULL ||
+		    git_vector_insert(&extensions, extension) < 0)
+			return -1;
+	}
+
+	git_vector_foreach (&user_extensions, i, user) {
+		if (user[0] == '!')
+			continue;
+
+		if ((extension = git__strdup(user)) == NULL ||
+		    git_vector_insert(&extensions, extension) < 0)
+			return -1;
+	}
+
+	*out = (char **)git_vector_detach(out_len, NULL, &extensions);
+	return 0;
+}
+
+int git_repository__set_extensions(const char **extensions, size_t len)
+{
+	char *extension;
+	size_t i;
+
+	git_repository__free_extensions();
+
+	for (i = 0; i < len; i++) {
+		if ((extension = git__strdup(extensions[i])) == NULL ||
+		    git_vector_insert(&user_extensions, extension) < 0)
+			return -1;
+	}
+
+	return 0;
+}
+
+void git_repository__free_extensions(void)
+{
+	git_vector_free_deep(&user_extensions);
+}
+
 int git_repository_create_head(const char *git_dir, const char *ref_name)
 {
 	git_buf ref_path = GIT_BUF_INIT;
diff --git a/src/repository.h b/src/repository.h
index f48dd9e..cbc1601 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -249,4 +249,8 @@ int git_repository_initialbranch(git_buf *out, git_repository *repo);
  */
 int git_repository_workdir_path(git_buf *out, git_repository *repo, const char *path);
 
+int git_repository__extensions(char ***out, size_t *out_len);
+int git_repository__set_extensions(const char **extensions, size_t len);
+void git_repository__free_extensions(void);
+
 #endif
diff --git a/tests/core/opts.c b/tests/core/opts.c
index 72408cb..e8f65d5 100644
--- a/tests/core/opts.c
+++ b/tests/core/opts.c
@@ -1,6 +1,11 @@
 #include "clar_libgit2.h"
 #include "cache.h"
 
+void test_core_opts__cleanup(void)
+{
+	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, NULL, 0));
+}
+
 void test_core_opts__readwrite(void)
 {
 	size_t old_val = 0;
@@ -23,3 +28,44 @@ void test_core_opts__invalid_option(void)
 	cl_git_fail(git_libgit2_opts(-1, "foobar"));
 }
 
+void test_core_opts__extensions_query(void)
+{
+	git_strarray out = { 0 };
+
+	cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));
+
+	cl_assert_equal_sz(out.count, 1);
+	cl_assert_equal_s("noop", out.strings[0]);
+
+	git_strarray_dispose(&out);
+}
+
+void test_core_opts__extensions_add(void)
+{
+	const char *in[] = { "foo" };
+	git_strarray out = { 0 };
+
+	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
+	cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));
+
+	cl_assert_equal_sz(out.count, 2);
+	cl_assert_equal_s("noop", out.strings[0]);
+	cl_assert_equal_s("foo", out.strings[1]);
+
+	git_strarray_dispose(&out);
+}
+
+void test_core_opts__extensions_remove(void)
+{
+	const char *in[] = { "bar", "!negate", "!noop", "baz" };
+	git_strarray out = { 0 };
+
+	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
+	cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));
+
+	cl_assert_equal_sz(out.count, 2);
+	cl_assert_equal_s("bar", out.strings[0]);
+	cl_assert_equal_s("baz", out.strings[1]);
+
+	git_strarray_dispose(&out);
+}
diff --git a/tests/repo/extensions.c b/tests/repo/extensions.c
new file mode 100644
index 0000000..e7772ac
--- /dev/null
+++ b/tests/repo/extensions.c
@@ -0,0 +1,72 @@
+#include "clar_libgit2.h"
+#include "futils.h"
+#include "sysdir.h"
+#include <ctype.h>
+
+git_repository *repo;
+
+void test_repo_extensions__initialize(void)
+{
+	git_config *config;
+
+	repo = cl_git_sandbox_init("empty_bare.git");
+
+	cl_git_pass(git_repository_config(&config, repo));
+	cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
+	git_config_free(config);
+}
+
+void test_repo_extensions__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, NULL, 0));
+}
+
+void test_repo_extensions__builtin(void)
+{
+	git_repository *extended;
+
+	cl_repo_set_string(repo, "extensions.noop", "foobar");
+
+	cl_git_pass(git_repository_open(&extended, "empty_bare.git"));
+	cl_assert(git_repository_path(extended) != NULL);
+	cl_assert(git__suffixcmp(git_repository_path(extended), "/") == 0);
+	git_repository_free(extended);
+}
+
+void test_repo_extensions__negate_builtin(void)
+{
+	const char *in[] = { "foo", "!noop", "baz" };
+	git_repository *extended;
+
+	cl_repo_set_string(repo, "extensions.noop", "foobar");
+
+	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
+
+	cl_git_fail(git_repository_open(&extended, "empty_bare.git"));
+	git_repository_free(extended);
+}
+
+void test_repo_extensions__unsupported(void)
+{
+	git_repository *extended = NULL;
+
+	cl_repo_set_string(repo, "extensions.unknown", "foobar");
+
+	cl_git_fail(git_repository_open(&extended, "empty_bare.git"));
+	git_repository_free(extended);
+}
+
+void test_repo_extensions__adds_extension(void)
+{
+	const char *in[] = { "foo", "!noop", "newextension", "baz" };
+	git_repository *extended;
+
+	cl_repo_set_string(repo, "extensions.newextension", "foobar");
+	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
+
+	cl_git_pass(git_repository_open(&extended, "empty_bare.git"));
+	cl_assert(git_repository_path(extended) != NULL);
+	cl_assert(git__suffixcmp(git_repository_path(extended), "/") == 0);
+	git_repository_free(extended);
+}
diff --git a/tests/repo/open.c b/tests/repo/open.c
index 881a23d..bd60c12 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -42,48 +42,6 @@ void test_repo_open__format_version_1(void)
 	git_repository_free(repo);
 }
 
-void test_repo_open__format_version_1_with_valid_extension(void)
-{
-	git_repository *repo;
-	git_config *config;
-
-	repo = cl_git_sandbox_init("empty_bare.git");
-
-	cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
-	cl_git_pass(git_repository_config(&config, repo));
-
-	cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
-	cl_git_pass(git_config_set_int32(config, "extensions.noop", 1));
-
-	git_config_free(config);
-	git_repository_free(repo);
-
-	cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
-	cl_assert(git_repository_path(repo) != NULL);
-	cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
-	git_repository_free(repo);
-}
-
-void test_repo_open__format_version_1_with_invalid_extension(void)
-{
-	git_repository *repo;
-	git_config *config;
-
-	repo = cl_git_sandbox_init("empty_bare.git");
-
-	cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
-	cl_git_pass(git_repository_config(&config, repo));
-
-	cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
-	cl_git_pass(git_config_set_int32(config, "extensions.invalid", 1));
-
-	git_config_free(config);
-	git_repository_free(repo);
-
-	cl_git_fail(git_repository_open(&repo, "empty_bare.git"));
-	git_repository_free(repo);
-}
-
 void test_repo_open__standard_empty_repo_through_gitdir(void)
 {
 	git_repository *repo;