Commit 324602514fec5ba04fa236c67d633f9b18ad9845

Russell Belfer 2013-03-18T15:54:35

Fixes and cleanups Get rid of some dead code, tighten things up a bit, and fix a bug with core::env test.

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
424
425
426
427
428
429
430
431
diff --git a/include/git2/strarray.h b/include/git2/strarray.h
index df34a5b..d338eb7 100644
--- a/include/git2/strarray.h
+++ b/include/git2/strarray.h
@@ -52,32 +52,6 @@ GIT_EXTERN(void) git_strarray_free(git_strarray *array);
  */
 GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src);
 
-/**
- * Initialize a string array from a list of strings
- *
- * Note: target is overwritten and hence should be empty, otherwise its
- * contents are leaked.  Call git_strarray_free() if necessary.
- *
- * @param tgt target
- * @param count number of strings to follow
- * @return 0 on success, <0 on allocation failure
- */
-GIT_EXTERN(int) git_strarray_set(git_strarray *tgt, size_t count, ...);
-
-/**
- * Insert a strarray into the beginning of another
- *
- * In this case, tgt is an existing (initialized) strarray and the result
- * will be reallocated with all the strings in src inserted before all of
- * the existing strings in tgt.  Strings in src will be strdup'ed, so
- * you should still `git_strarray_free()` src when you are done with it.
- *
- * @param tgt strarray to update
- * @param src strarray to copy from
- * @return 0 on success, <0 on allocation failure (tgt will be unchanged)
- */
-GIT_EXTERN(int) git_strarray_prepend(git_strarray *tgt, const git_strarray *src);
-
 
 /** @} */
 GIT_END_DECL
diff --git a/src/fileops.c b/src/fileops.c
index 9700eed..fc9fca0 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -562,7 +562,7 @@ clean_up:
 static int git_futils_guess_system_dirs(git_buf *out)
 {
 #ifdef GIT_WIN32
-	return win32_find_system_dirs(out);
+	return git_win32__find_system_dirs(out);
 #else
 	return git_buf_sets(out, "/etc");
 #endif
@@ -571,7 +571,7 @@ static int git_futils_guess_system_dirs(git_buf *out)
 static int git_futils_guess_global_dirs(git_buf *out)
 {
 #ifdef GIT_WIN32
-	return win32_find_global_dirs(out);
+	return git_win32__find_global_dirs(out);
 #else
 	return git_buf_sets(out, getenv("HOME"));
 #endif
@@ -580,7 +580,7 @@ static int git_futils_guess_global_dirs(git_buf *out)
 static int git_futils_guess_xdg_dirs(git_buf *out)
 {
 #ifdef GIT_WIN32
-	return win32_find_xdg_dirs(out);
+	return git_win32__find_xdg_dirs(out);
 #else
 	const char *env = NULL;
 
diff --git a/src/util.c b/src/util.c
index ba867b4..f5b4a1d 100644
--- a/src/util.c
+++ b/src/util.c
@@ -112,78 +112,32 @@ void git_strarray_free(git_strarray *array)
 
 int git_strarray_copy(git_strarray *tgt, const git_strarray *src)
 {
-	assert(tgt && src);
-
-	memset(tgt, 0, sizeof(*tgt));
-	return git_strarray_prepend(tgt, src);
-}
-
-int git_strarray_set(git_strarray *tgt, size_t count, ...)
-{
 	size_t i;
-	va_list ap;
 
-	assert(tgt);
+	assert(tgt && src);
 
 	memset(tgt, 0, sizeof(*tgt));
 
-	if (!count)
+	if (!src->count)
 		return 0;
 
-	tgt->strings = git__calloc(count, sizeof(char *));
+	tgt->strings = git__calloc(src->count, sizeof(char *));
 	GITERR_CHECK_ALLOC(tgt->strings);
 
-	va_start(ap, count);
-	for (i = 0; i < count; ++i) {
-		const char *str = va_arg(ap, const char *);
-		if (!str)
+	for (i = 0; i < src->count; ++i) {
+		if (!src->strings[i])
 			continue;
 
-		tgt->strings[tgt->count] = git__strdup(str);
+		tgt->strings[tgt->count] = git__strdup(src->strings[i]);
 		if (!tgt->strings[tgt->count]) {
 			git_strarray_free(tgt);
-			va_end(ap);
+			memset(tgt, 0, sizeof(*tgt));
 			return -1;
 		}
 
 		tgt->count++;
 	}
-	va_end(ap);
-
-	return 0;
-}
-
-int git_strarray_prepend(git_strarray *tgt, const git_strarray *src)
-{
-	size_t i;
-	git_strarray merge;
-
-	if (!src || !src->count)
-		return 0;
-
-	merge.count = 0;
-	merge.strings = git__calloc(tgt->count + src->count, sizeof(char *));
-	GITERR_CHECK_ALLOC(merge.strings);
-
-	for (i = 0; i < src->count; ++i) {
-		if (!src->strings[i])
-			continue;
-
-		merge.strings[merge.count] = git__strdup(src->strings[i]);
-		if (!merge.strings[merge.count]) {
-			git_strarray_free(&merge);
-			return -1;
-		}
-
-		merge.count++;
-	}
-
-	for (i = 0; i < tgt->count; ++i)
-		if (tgt->strings[i])
-			merge.strings[merge.count++] = tgt->strings[i];
 
-	git__free(tgt->strings);
-	memcpy(tgt, &merge, sizeof(merge));
 	return 0;
 }
 
diff --git a/src/win32/findfile.c b/src/win32/findfile.c
index 6cdea85..bc36b6b 100644
--- a/src/win32/findfile.c
+++ b/src/win32/findfile.c
@@ -17,7 +17,7 @@
 #define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
 #endif
 
-int win32_expand_path(struct win32_path *s_root, const wchar_t *templ)
+int git_win32__expand_path(struct git_win32__path *s_root, const wchar_t *templ)
 {
 	s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH);
 	return s_root->len ? 0 : -1;
@@ -33,8 +33,8 @@ static int win32_path_utf16_to_8(git_buf *path_utf8, const wchar_t *path_utf16)
 	return git_buf_sets(path_utf8, temp_utf8);
 }
 
-int win32_find_file(
-	git_buf *path, const struct win32_path *root, const char *filename)
+int git_win32__find_file(
+	git_buf *path, const struct git_win32__path *root, const char *filename)
 {
 	size_t len, alloc_len;
 	wchar_t *file_utf16 = NULL;
@@ -89,7 +89,7 @@ static wchar_t* win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen)
 static int win32_find_git_in_path(git_buf *buf, const wchar_t *gitexe)
 {
 	wchar_t *env = _wgetenv(L"PATH"), lastch;
-	struct win32_path root;
+	struct git_win32__path root;
 	size_t gitexe_len = wcslen(gitexe);
 
 	if (!env)
@@ -126,7 +126,7 @@ static int win32_find_git_in_registry(
 {
 	HKEY hKey;
 	DWORD dwType = REG_SZ;
-	struct win32_path path16;
+	struct git_win32__path path16;
 
 	assert(buf);
 
@@ -158,13 +158,13 @@ static int win32_find_git_in_registry(
 static int win32_find_existing_dirs(
 	git_buf *out, const wchar_t *tmpl[], char *temp[])
 {
-	struct win32_path path16;
+	struct git_win32__path path16;
 	git_buf buf = GIT_BUF_INIT;
 
 	git_buf_clear(out);
 
 	for (; *tmpl != NULL; tmpl++) {
-		if (!win32_expand_path(&path16, *tmpl) &&
+		if (!git_win32__expand_path(&path16, *tmpl) &&
 			path16.path[0] != L'%' &&
 			!_waccess(path16.path, F_OK))
 		{
@@ -180,7 +180,7 @@ static int win32_find_existing_dirs(
 	return (git_buf_oom(out) ? -1 : 0);
 }
 
-int win32_find_system_dirs(git_buf *out)
+int git_win32__find_system_dirs(git_buf *out)
 {
 	git_buf buf = GIT_BUF_INIT;
 
@@ -207,7 +207,7 @@ int win32_find_system_dirs(git_buf *out)
 	return (git_buf_oom(out) ? -1 : 0);
 }
 
-int win32_find_global_dirs(git_buf *out)
+int git_win32__find_global_dirs(git_buf *out)
 {
 	char *temp[3];
 	static const wchar_t *global_tmpls[4] = {
@@ -220,7 +220,7 @@ int win32_find_global_dirs(git_buf *out)
 	return win32_find_existing_dirs(out, global_tmpls, temp);
 }
 
-int win32_find_xdg_dirs(git_buf *out)
+int git_win32__find_xdg_dirs(git_buf *out)
 {
 	char *temp[6];
 	static const wchar_t *global_tmpls[7] = {
diff --git a/src/win32/findfile.h b/src/win32/findfile.h
index 300bd16..fc79e1b 100644
--- a/src/win32/findfile.h
+++ b/src/win32/findfile.h
@@ -8,19 +8,20 @@
 #ifndef INCLUDE_git_findfile_h__
 #define INCLUDE_git_findfile_h__
 
-struct win32_path {
+struct git_win32__path {
 	wchar_t path[MAX_PATH];
 	DWORD len;
 };
 
-extern int win32_expand_path(struct win32_path *s_root, const wchar_t *templ);
+extern int git_win32__expand_path(
+	struct git_win32__path *s_root, const wchar_t *templ);
 
-extern int win32_find_file(
-	git_buf *path, const struct win32_path *root, const char *filename);
+extern int git_win32__find_file(
+	git_buf *path, const struct git_win32__path *root, const char *filename);
 
-extern int win32_find_system_dirs(git_buf *out);
-extern int win32_find_global_dirs(git_buf *out);
-extern int win32_find_xdg_dirs(git_buf *out);
+extern int git_win32__find_system_dirs(git_buf *out);
+extern int git_win32__find_global_dirs(git_buf *out);
+extern int git_win32__find_xdg_dirs(git_buf *out);
 
 #endif
 
diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c
index d684f4c..0fa6472 100644
--- a/tests-clar/core/env.c
+++ b/tests-clar/core/env.c
@@ -32,7 +32,7 @@ void test_core_env__initialize(void)
 	for (i = 0; i < NUM_VARS; ++i) {
 		const char *original = cl_getenv(env_vars[i]);
 #ifdef GIT_WIN32
-		env_save[i] = original;
+		env_save[i] = (char *)original;
 #else
 		env_save[i] = original ? git__strdup(original) : NULL;
 #endif
@@ -81,7 +81,9 @@ static void setenv_and_check(const char *name, const char *value)
 
 	check = cl_getenv(name);
 	cl_assert_equal_s(value, check);
+#ifdef GIT_WIN32
 	git__free(check);
+#endif
 }
 
 void test_core_env__0(void)
@@ -212,6 +214,43 @@ void test_core_env__1(void)
 	git_buf_free(&path);
 }
 
+static void check_global_searchpath(
+	const char *path, int position, const char *file, git_buf *temp)
+{
+	char out[GIT_PATH_MAX];
+
+	/* build and set new path */
+	if (position < 0)
+		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH"));
+	else if (position > 0)
+		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path));
+	else
+		cl_git_pass(git_buf_sets(temp, path));
+
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr));
+
+	/* get path and make sure $PATH expansion worked */
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out)));
+
+	if (position < 0)
+		cl_assert(git__prefixcmp(out, path) == 0);
+	else if (position > 0)
+		cl_assert(git__suffixcmp(out, path) == 0);
+	else
+		cl_assert_equal_s(out, path);
+
+	/* find file using new path */
+	cl_git_pass(git_futils_find_global_file(temp, file));
+
+	/* reset path and confirm file not found */
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
+	cl_assert_equal_i(
+		GIT_ENOTFOUND, git_futils_find_global_file(temp, file));
+}
+
 void test_core_env__2(void)
 {
 	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
@@ -219,7 +258,6 @@ void test_core_env__2(void)
 	char **val;
 	const char *testname = "alternate";
 	size_t testlen = strlen(testname);
-	char out[GIT_PATH_MAX];
 
 	strncpy(testfile, testname, sizeof(testfile));
 	cl_assert_equal_s(testname, testfile);
@@ -237,9 +275,8 @@ void test_core_env__2(void)
 
 		cl_git_pass(git_path_prettify(&path, *val, NULL));
 
-		/* vary testfile name in each directory so accidentally leaving
-		 * an environment variable set from a previous iteration won't
-		 * accidentally make this test pass...
+		/* vary testfile name so any sloppiness is resetting variables or
+		 * deleting files won't accidentally make a test pass.
 		 */
 		testfile[testlen] = tidx++;
 		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
@@ -250,63 +287,14 @@ void test_core_env__2(void)
 		cl_assert_equal_i(
 			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
 
-		/* set search path */
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
-
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out)));
-		cl_assert_equal_s(out, path.ptr);
-
-		cl_git_pass(git_futils_find_global_file(&found, testfile));
-
-		/* reset */
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
-		cl_assert_equal_i(
-			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
-
-		/* try prepend behavior */
-		cl_git_pass(git_buf_putc(&path, GIT_PATH_LIST_SEPARATOR));
-		cl_git_pass(git_buf_puts(&path, "$PATH"));
-
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
-
-		git_buf_rtruncate_at_char(&path, GIT_PATH_LIST_SEPARATOR);
-
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out)));
-		cl_assert(git__prefixcmp(out, path.ptr) == 0);
-
-		cl_git_pass(git_futils_find_global_file(&found, testfile));
-
-		/* reset */
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
-		cl_assert_equal_i(
-			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
-
-		/* try append behavior */
-		cl_git_pass(git_buf_join(
-			&found, GIT_PATH_LIST_SEPARATOR, "$PATH", path.ptr));
-
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, found.ptr));
-
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out)));
-		cl_assert(git__suffixcmp(out, path.ptr) == 0);
-
-		cl_git_pass(git_futils_find_global_file(&found, testfile));
-
-		/* reset */
-		cl_git_pass(git_libgit2_opts(
-			GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
+		/* try plain, append $PATH, and prepend $PATH */
+		check_global_searchpath(path.ptr,  0, testfile, &found);
+		check_global_searchpath(path.ptr, -1, testfile, &found);
+		check_global_searchpath(path.ptr,  1, testfile, &found);
 
+		/* cleanup */
 		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
 		(void)p_unlink(path.ptr);
-
 		(void)p_rmdir(*val);
 	}