Commit 41954a49c12a72eda3b3fe02c2752f6831b5dbf9

Russell Belfer 2013-03-18T14:19:35

Switch search paths to classic delimited strings This switches the APIs for setting and getting the global/system search paths from using git_strarray to using a simple string with GIT_PATH_LIST_SEPARATOR delimited paths, just as the environment PATH variable would contain. This makes it simpler to get and set the value. I also added code to expand "$PATH" when setting a new value to embed the old value of the path. This means that I no longer require separate actions to PREPEND to the value.

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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
diff --git a/include/git2/common.h b/include/git2/common.h
index 137ffa0..b8c3e42 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -131,7 +131,6 @@ enum {
 	GIT_OPT_SET_MWINDOW_MAPPED_LIMIT,
 	GIT_OPT_GET_SEARCH_PATH,
 	GIT_OPT_SET_SEARCH_PATH,
-	GIT_OPT_PREPEND_SEARCH_PATH,
 };
 
 /**
@@ -152,25 +151,21 @@ enum {
  *		Set the maximum amount of memory that can be mapped at any time
  *		by the library
  *
- *	opts(GIT_OPT_GET_SEARCH_PATH, const git_strarray **, int level)
- *		Get a strarray of the search path for a given level of config
- *		data.  "level" must be one of GIT_CONFIG_LEVEL_SYSTEM,
- *		GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG.  The search
- *		path applies to shared attributes and ignore files, too.
+ *	opts(GIT_OPT_GET_SEARCH_PATH, int level, char *out, size_t len)
+ *		Get the search path for a given level of config data.  "level" must
+ *		be one of GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_GLOBAL, or
+ *		GIT_CONFIG_LEVEL_XDG.  The search path is written to the `out`
+ *		buffer up to size `len`.  Returns GIT_EBUFS if buffer is too small.
  *
- *	opts(GIT_OPT_SET_SEARCH_PATH, int level, const git_strarray *)
- *		Set the search path for a given level of config data.  Passing
- *		NULL for the git_strarray pointer resets the search path to the
- *		default (which is generally based on environment variables).
- *		"level" must be one of GIT_CONFIG_LEVEL_SYSTEM,
- *		GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG.  The search
- *		path applies to shared attributes and ignore files, too.
- *
- *	opts(GIT_OPT_PREPEND_SEARCH_PATH, int level, const git_strarray *)
- *		Prepend new directories to the search path for a given level of
- *		config data.  "level" must be one of GIT_CONFIG_LEVEL_SYSTEM,
- *		GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG.  The search
- *		path applies to shared attributes and ignore files, too.
+ *	opts(GIT_OPT_SET_SEARCH_PATH, int level, const char *path)
+ *		Set the search path for a level of config data.  The search path
+ *		applied to shared attributes and ignore files, too.
+ *      - `path` lists directories delimited by GIT_PATH_LIST_SEPARATOR.
+ *		  Pass NULL to reset to the default (generally based on environment
+ *		  variables).  Use magic path `$PATH` to include the old value
+ *		  of the path (if you want to prepend or append, for instance).
+ *		- `level` must be GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_GLOBAL,
+ *		  or GIT_CONFIG_LEVEL_XDG.
  *
  * @param option Option key
  * @param ... value to set the option
diff --git a/src/config.c b/src/config.c
index 85db0ab..8be5da6 100644
--- a/src/config.c
+++ b/src/config.c
@@ -521,7 +521,7 @@ static int git_config__find_file_to_path(
 
 	if (path.size >= outlen) {
 		giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
-		error = -1;
+		error = GIT_EBUFS;
 		goto done;
 	}
 
diff --git a/src/fileops.c b/src/fileops.c
index 4ed46ce..9700eed 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -559,48 +559,46 @@ clean_up:
 }
 
 
-static int git_futils_guess_system_dirs(git_strarray *out)
+static int git_futils_guess_system_dirs(git_buf *out)
 {
 #ifdef GIT_WIN32
 	return win32_find_system_dirs(out);
 #else
-	return git_strarray_set(out, 1, "/etc");
+	return git_buf_sets(out, "/etc");
 #endif
 }
 
-static int git_futils_guess_global_dirs(git_strarray *out)
+static int git_futils_guess_global_dirs(git_buf *out)
 {
 #ifdef GIT_WIN32
 	return win32_find_global_dirs(out);
 #else
-	return git_strarray_set(out, 1, getenv("HOME"));
+	return git_buf_sets(out, getenv("HOME"));
 #endif
 }
 
-static int git_futils_guess_xdg_dirs(git_strarray *out)
+static int git_futils_guess_xdg_dirs(git_buf *out)
 {
 #ifdef GIT_WIN32
 	return win32_find_xdg_dirs(out);
 #else
-	int error = 0;
-	git_buf xdg = GIT_BUF_INIT;
 	const char *env = NULL;
 
 	if ((env = getenv("XDG_CONFIG_HOME")) != NULL)
-		git_buf_joinpath(&xdg, env, "git");
+		return git_buf_joinpath(out, env, "git");
 	else if ((env = getenv("HOME")) != NULL)
-		git_buf_joinpath(&xdg, env, ".config/git");
-
-	error = git_strarray_set(out, 1, xdg.ptr);
-	git_buf_free(&xdg);
+		return git_buf_joinpath(out, env, ".config/git");
 
-	return error;
+	git_buf_clear(out);
+	return 0;
 #endif
 }
 
-typedef int (*git_futils_dirs_guess_cb)(git_strarray *out);
+typedef int (*git_futils_dirs_guess_cb)(git_buf *out);
+
+static git_buf git_futils__dirs[GIT_FUTILS_DIR__MAX] =
+	{ GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
 
-static git_strarray git_futils__dirs[GIT_FUTILS_DIR__MAX];
 static git_futils_dirs_guess_cb git_futils__dir_guess[GIT_FUTILS_DIR__MAX] = {
 	git_futils_guess_system_dirs,
 	git_futils_guess_global_dirs,
@@ -615,53 +613,105 @@ static int git_futils_check_selector(git_futils_dir_t which)
 	return -1;
 }
 
-int git_futils_dirs_get(const git_strarray **out, git_futils_dir_t which)
+int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which)
 {
-	if (!out) {
-		giterr_set(GITERR_INVALID, "Output git_strarray not provided");
-		return -1;
-	}
+	assert(out);
 
 	*out = NULL;
 
 	GITERR_CHECK_ERROR(git_futils_check_selector(which));
 
-	if (!git_futils__dirs[which].count) {
-		int error = git_futils__dir_guess[which](&git_futils__dirs[which]);
-		if (error < 0)
-			return error;
-	}
+	if (!git_buf_len(&git_futils__dirs[which]))
+		GITERR_CHECK_ERROR(
+			git_futils__dir_guess[which](&git_futils__dirs[which]));
 
 	*out = &git_futils__dirs[which];
 	return 0;
 }
 
-int git_futils_dirs_set(
-	git_futils_dir_t which, const git_strarray *dirs, bool replace)
+int git_futils_dirs_get_str(char *out, size_t outlen, git_futils_dir_t which)
+{
+	const git_buf *path = NULL;
+
+	GITERR_CHECK_ERROR(git_futils_check_selector(which));
+	GITERR_CHECK_ERROR(git_futils_dirs_get(&path, which));
+
+	if (!out || path->size >= outlen) {
+		giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
+		return GIT_EBUFS;
+	}
+
+	git_buf_copy_cstr(out, outlen, path);
+	return 0;
+}
+
+#define PATH_MAGIC "$PATH"
+
+int git_futils_dirs_set(git_futils_dir_t which, const char *search_path)
 {
+	const char *expand_path = NULL;
+	git_buf merge = GIT_BUF_INIT;
+
 	GITERR_CHECK_ERROR(git_futils_check_selector(which));
 
-	if (replace)
-		git_strarray_free(&git_futils__dirs[which]);
+	if (search_path != NULL)
+		expand_path = strstr(search_path, PATH_MAGIC);
 
-	/* init with defaults if it hasn't been done yet, but ignore error */
-	else if (!git_futils__dirs[which].count)
+	/* init with default if not yet done and needed (ignoring error) */
+	if ((!search_path || expand_path) &&
+		!git_buf_len(&git_futils__dirs[which]))
 		git_futils__dir_guess[which](&git_futils__dirs[which]);
 
-	return git_strarray_prepend(&git_futils__dirs[which], dirs);
+	/* if $PATH is not referenced, then just set the path */
+	if (!expand_path)
+		return git_buf_sets(&git_futils__dirs[which], search_path);
+
+	/* otherwise set to join(before $PATH, old value, after $PATH) */
+	if (expand_path > search_path)
+		git_buf_set(&merge, search_path, expand_path - search_path);
+
+	if (git_buf_len(&git_futils__dirs[which]))
+		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
+			merge.ptr, git_futils__dirs[which].ptr);
+
+	expand_path += strlen(PATH_MAGIC);
+	if (*expand_path)
+		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
+
+	git_buf_swap(&git_futils__dirs[which], &merge);
+	git_buf_free(&merge);
+
+	return git_buf_oom(&git_futils__dirs[which]) ? -1 : 0;
+}
+
+void git_futils_dirs_free(void)
+{
+	int i;
+	for (i = 0; i < GIT_FUTILS_DIR__MAX; ++i)
+		git_buf_free(&git_futils__dirs[i]);
 }
 
 static int git_futils_find_in_dirlist(
 	git_buf *path, const char *name, git_futils_dir_t which, const char *label)
 {
-	size_t i;
-	const git_strarray *syspaths;
+	size_t len;
+	const char *scan, *next = NULL;
+	const git_buf *syspath;
 
-	GITERR_CHECK_ERROR(git_futils_dirs_get(&syspaths, which));
+	GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which));
 
-	for (i = 0; i < syspaths->count; ++i) {
-		GITERR_CHECK_ERROR(
-			git_buf_joinpath(path, syspaths->strings[i], name));
+	for (scan = git_buf_cstr(syspath); scan; scan = next) {
+		for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR);
+			 next && next > scan && next[-1] == '\\';
+			 next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR))
+			/* find unescaped separator or end of string */;
+
+		len = next ? (size_t)(next++ - scan) : strlen(scan);
+		if (!len)
+			continue;
+
+		GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
+		GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
 
 		if (git_path_exists(path->ptr))
 			return 0;
diff --git a/src/fileops.h b/src/fileops.h
index 57ab918..627a692 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -306,25 +306,42 @@ typedef enum {
 } git_futils_dir_t;
 
 /**
- * Get the strarray of search paths for global/system files
+ * Get the search path for global/system/xdg files
  *
- * @param out git_strarray of search paths
+ * @param out pointer to git_buf containing search path
  * @param which which list of paths to return
- * @return 0 on success, <0 on failure (allocation error)
+ * @return 0 on success, <0 on failure
+ */
+extern int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which);
+
+/**
+ * Get search path into a preallocated buffer
+ *
+ * @param out String buffer to write into
+ * @param outlen Size of string buffer
+ * @param which Which search path to return
+ * @return 0 on success, GIT_EBUFS if out is too small, <0 on other failure
  */
-extern int git_futils_dirs_get(
-	const git_strarray **out, git_futils_dir_t which);
+
+extern int git_futils_dirs_get_str(
+	char *out, size_t outlen, git_futils_dir_t which);
 
 /**
- * Set or prepend strarray of search paths for global/system files
+ * Set search paths for global/system/xdg files
+ *
+ * The first occurrence of the magic string "$PATH" in the new value will
+ * be replaced with the old value of the search path.
  *
- * @param which which list of paths to modify
- * @param dirs new list of search paths
- * @param replace true to replace old, false to prepend to old
+ * @param which Which search path to modify
+ * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR)
  * @return 0 on success, <0 on failure (allocation error)
  */
-extern int git_futils_dirs_set(
-	git_futils_dir_t which, const git_strarray *dirs, bool replace);
+extern int git_futils_dirs_set(git_futils_dir_t which, const char *paths);
+
+/**
+ * Release / reset all search paths
+ */
+extern void git_futils_dirs_free(void);
 
 /**
  * Create a "fake" symlink (text file containing the target path).
diff --git a/src/global.c b/src/global.c
index 4d37fa1..b7fd8e2 100644
--- a/src/global.c
+++ b/src/global.c
@@ -7,7 +7,8 @@
 #include "common.h"
 #include "global.h"
 #include "hash.h"
-#include "git2/threads.h" 
+#include "fileops.h"
+#include "git2/threads.h"
 #include "thread-utils.h"
 
 
@@ -82,6 +83,7 @@ void git_threads_shutdown(void)
 
 	/* Shut down any subsystems that have global state */
 	git_hash_global_shutdown();
+	git_futils_dirs_free();
 }
 
 git_global_st *git__global_state(void)
@@ -139,6 +141,7 @@ void git_threads_shutdown(void)
 
 	/* Shut down any subsystems that have global state */
 	git_hash_global_shutdown();
+	git_futils_dirs_free();
 }
 
 git_global_st *git__global_state(void)
@@ -171,7 +174,9 @@ int git_threads_init(void)
 
 void git_threads_shutdown(void)
 {
-	/* noop */
+	/* Shut down any subsystems that have global state */
+	git_hash_global_shutdown();
+	git_futils_dirs_free();
 }
 
 git_global_st *git__global_state(void)
diff --git a/src/util.c b/src/util.c
index f481646..ba867b4 100644
--- a/src/util.c
+++ b/src/util.c
@@ -39,7 +39,7 @@ int git_libgit2_capabilities()
 extern size_t git_mwindow__window_size;
 extern size_t git_mwindow__mapped_limit;
 
-static int convert_config_level_to_futils_dir(int config_level)
+static int config_level_to_futils_dir(int config_level)
 {
 	int val = -1;
 
@@ -80,24 +80,18 @@ int git_libgit2_opts(int key, ...)
 		break;
 
 	case GIT_OPT_GET_SEARCH_PATH:
-		{
-			const git_strarray **out = va_arg(ap, const git_strarray **);
-			int which = convert_config_level_to_futils_dir(va_arg(ap, int));
+		if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0) {
+			char *out = va_arg(ap, char *);
+			size_t outlen = va_arg(ap, size_t);
 
-			error = (which < 0) ? which : git_futils_dirs_get(out, which);
-			break;
+			error = git_futils_dirs_get_str(out, outlen, error);
 		}
+		break;
 
 	case GIT_OPT_SET_SEARCH_PATH:
-	case GIT_OPT_PREPEND_SEARCH_PATH:
-		{
-			int which = convert_config_level_to_futils_dir(va_arg(ap, int));
-			const git_strarray *dirs = va_arg(ap, git_strarray *);
-
-			error = (which < 0) ? which : git_futils_dirs_set(
-				which, dirs, key == GIT_OPT_SET_SEARCH_PATH);
-			break;
-		}
+		if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0)
+			error = git_futils_dirs_set(error, va_arg(ap, const char *));
+		break;
 	}
 
 	va_end(ap);
diff --git a/src/win32/findfile.c b/src/win32/findfile.c
index d3351b1..6cdea85 100644
--- a/src/win32/findfile.c
+++ b/src/win32/findfile.c
@@ -155,75 +155,59 @@ static int win32_find_git_in_registry(
 	return path16.len ? 0 : GIT_ENOTFOUND;
 }
 
-static int win32_copy_to_strarray(
-	git_strarray *out, size_t count, char **strings)
-{
-	size_t i, realcount;
-
-	if (!count)
-		return 0;
-
-	for (i = 0, realcount = 0; i < count; ++i)
-		if (strings[i]) realcount++;
-
-	out->strings = git__calloc(realcount, sizeof(char *));
-	GITERR_CHECK_ALLOC(out->strings);
-
-	for (i = 0, out->count = 0; i < count; ++i)
-		if (strings[i])
-			out->strings[out->count++] = strings[i];
-
-	return 0;
-}
-
 static int win32_find_existing_dirs(
-	git_strarray *out, const wchar_t *tmpl[], char *temp[])
+	git_buf *out, const wchar_t *tmpl[], char *temp[])
 {
 	struct win32_path path16;
 	git_buf buf = GIT_BUF_INIT;
-	size_t count;
 
-	for (count = 0; *tmpl != NULL; tmpl++) {
+	git_buf_clear(out);
+
+	for (; *tmpl != NULL; tmpl++) {
 		if (!win32_expand_path(&path16, *tmpl) &&
 			path16.path[0] != L'%' &&
 			!_waccess(path16.path, F_OK))
 		{
 			win32_path_utf16_to_8(&buf, path16.path);
-			temp[count++] = git_buf_detach(&buf);
+
+			if (buf.size)
+				git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
 		}
 	}
 
-	return win32_copy_to_strarray(out, count, temp);
+	git_buf_free(&buf);
+
+	return (git_buf_oom(out) ? -1 : 0);
 }
 
-int win32_find_system_dirs(git_strarray *out)
+int win32_find_system_dirs(git_buf *out)
 {
-	char *strings[4];
-	size_t count = 0;
 	git_buf buf = GIT_BUF_INIT;
 
-	memset(out, 0, sizeof(*out));
-
 	/* directories where git.exe & git.cmd are found */
-	if (!win32_find_git_in_path(&buf, L"git.exe"))
-		strings[count++] = git_buf_detach(&buf);
+	if (!win32_find_git_in_path(&buf, L"git.exe") && buf.size)
+		git_buf_set(out, buf.ptr, buf.size);
+	else
+		git_buf_clear(out);
 
-	if (!win32_find_git_in_path(&buf, L"git.cmd"))
-		strings[count++] = git_buf_detach(&buf);
+	if (!win32_find_git_in_path(&buf, L"git.cmd") && buf.size)
+		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
 
 	/* directories where git is installed according to registry */
 	if (!win32_find_git_in_registry(
-			&buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL))
-		strings[count++] = git_buf_detach(&buf);
+			&buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL) && buf.size)
+		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
 
 	if (!win32_find_git_in_registry(
-			&buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL))
-		strings[count++] = git_buf_detach(&buf);
+			&buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL) && buf.size)
+		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
+
+	git_buf_free(&buf);
 
-	return win32_copy_to_strarray(out, count, strings);
+	return (git_buf_oom(out) ? -1 : 0);
 }
 
-int win32_find_global_dirs(git_strarray *out)
+int win32_find_global_dirs(git_buf *out)
 {
 	char *temp[3];
 	static const wchar_t *global_tmpls[4] = {
@@ -236,7 +220,7 @@ int win32_find_global_dirs(git_strarray *out)
 	return win32_find_existing_dirs(out, global_tmpls, temp);
 }
 
-int win32_find_xdg_dirs(git_strarray *out)
+int 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 f8a13c3..300bd16 100644
--- a/src/win32/findfile.h
+++ b/src/win32/findfile.h
@@ -18,9 +18,9 @@ extern int win32_expand_path(struct 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 win32_find_system_dirs(git_strarray *out);
-extern int win32_find_global_dirs(git_strarray *out);
-extern int win32_find_xdg_dirs(git_strarray *out);
+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);
 
 #endif
 
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index 698aa90..8033cdc 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -136,7 +136,7 @@ int cl_rename(const char *source, const char *dest)
 #include <stdlib.h>
 char *cl_getenv(const char *name)
 {
-   return getenv(name);
+	return getenv(name);
 }
 
 int cl_setenv(const char *name, const char *value)
diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c
index 7c36f39..d684f4c 100644
--- a/tests-clar/core/env.c
+++ b/tests-clar/core/env.c
@@ -29,8 +29,24 @@ static char *home_values[] = {
 void test_core_env__initialize(void)
 {
 	int i;
-	for (i = 0; i < NUM_VARS; ++i)
-		env_save[i] = cl_getenv(env_vars[i]);
+	for (i = 0; i < NUM_VARS; ++i) {
+		const char *original = cl_getenv(env_vars[i]);
+#ifdef GIT_WIN32
+		env_save[i] = original;
+#else
+		env_save[i] = original ? git__strdup(original) : NULL;
+#endif
+	}
+}
+
+static void reset_global_search_path(void)
+{
+	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL));
+}
+
+static void reset_system_search_path(void)
+{
+	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL));
 }
 
 void test_core_env__cleanup(void)
@@ -40,9 +56,7 @@ void test_core_env__cleanup(void)
 
 	for (i = 0; i < NUM_VARS; ++i) {
 		cl_setenv(env_vars[i], env_save[i]);
-#ifdef GIT_WIN32
 		git__free(env_save[i]);
-#endif
 		env_save[i] = NULL;
 	}
 
@@ -55,8 +69,8 @@ void test_core_env__cleanup(void)
 	}
 
 	/* reset search paths to default */
-	git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL, true);
-	git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL, true);
+	reset_global_search_path();
+	reset_system_search_path();
 }
 
 static void setenv_and_check(const char *name, const char *value)
@@ -64,21 +78,10 @@ static void setenv_and_check(const char *name, const char *value)
 	char *check;
 
 	cl_git_pass(cl_setenv(name, value));
+
 	check = cl_getenv(name);
 	cl_assert_equal_s(value, check);
-#ifdef GIT_WIN32
 	git__free(check);
-#endif
-}
-
-static void reset_global_search_path(void)
-{
-	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL, true));
-}
-
-static void reset_system_search_path(void)
-{
-	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL, true));
 }
 
 void test_core_env__0(void)
@@ -216,7 +219,7 @@ void test_core_env__2(void)
 	char **val;
 	const char *testname = "alternate";
 	size_t testlen = strlen(testname);
-	git_strarray arr;
+	char out[GIT_PATH_MAX];
 
 	strncpy(testfile, testname, sizeof(testfile));
 	cl_assert_equal_s(testname, testfile);
@@ -227,7 +230,7 @@ void test_core_env__2(void)
 		 * we are on a filesystem that doesn't support the
 		 * characters in question and skip this test...
 		 */
-		if (p_mkdir(*val, 0777) != 0) {
+		if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
 			*val = ""; /* mark as not created */
 			continue;
 		}
@@ -243,31 +246,67 @@ void test_core_env__2(void)
 		cl_git_mkfile(path.ptr, "find me");
 		git_buf_rtruncate_at_char(&path, '/');
 
-		arr.count = 1;
-		arr.strings = &path.ptr;
-
+		/* default should be NOTFOUND */
 		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_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &arr));
+			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_PREPEND_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &arr));
+			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));
 
+		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
+		(void)p_unlink(path.ptr);
+
 		(void)p_rmdir(*val);
 	}