Commit 2e0c881670678d18b912b57dd5825fec00167aad

nulltoken 2012-08-26T22:08:22

refs: expose git_reference_normalize_name()

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
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 9e70600..6a8513b 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -376,6 +376,54 @@ GIT_EXTERN(int) git_reference_has_log(git_reference *ref);
  */
 GIT_EXTERN(int) git_reference_is_branch(git_reference *ref);
 
+enum {
+	GIT_REF_FORMAT_NORMAL = 0,
+
+	/**
+	 * Control whether one-level refnames are accepted
+	 * (i.e., refnames that do not contain multiple /-separated
+	 * components)
+	 */
+	GIT_REF_FORMAT_ALLOW_ONELEVEL = (1 << 0),
+
+	/**
+	 * Interpret the provided name as a reference pattern for a
+	 * refspec (as used with remote repositories). If this option
+	 * is enabled, the name is allowed to contain a single * (<star>)
+	 * in place of a one full pathname component
+	 * (e.g., foo/<star>/bar but not foo/bar<star>).
+	 */
+	GIT_REF_FORMAT_REFSPEC_PATTERN = (1 << 1),
+};
+
+/**
+ * Normalize the reference name by removing any leading
+ * slash (/) characters and collapsing runs of adjacent slashes
+ * between name components into a single slash.
+ *
+ * Once normalized, if the reference name is valid, it will be
+ * returned in the user allocated buffer.
+ *
+ * TODO: Implement handling of GIT_REF_FORMAT_REFSPEC_PATTERN
+ *
+ * @param buffer_out The user allocated buffer where the
+ * normalized name will be stored.
+ *
+ * @param buffer_size buffer_out size
+ *
+ * @param name name to be checked.
+ *
+ * @param flags Flags to determine the options to be applied while
+ * checking the validatity of the name.
+ *
+ * @return 0 or an error code.
+ */
+GIT_EXTERN(int) git_reference_normalize_name(
+	char *buffer_out,
+	size_t buffer_size,
+	const char *name,
+	unsigned int flags);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/refs.c b/src/refs.c
index cf55a6f..9fc194c 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -68,11 +68,6 @@ static int reference_path_available(git_repository *repo,
 static int reference_delete(git_reference *ref);
 static int reference_lookup(git_reference *ref);
 
-/* name normalization */
-static int normalize_name(char *buffer_out, size_t out_size,
-	const char *name, int is_oid_ref);
-
-
 void git_reference_free(git_reference *reference)
 {
 	if (reference == NULL)
@@ -1099,9 +1094,12 @@ int git_reference_lookup_resolved(
 	scan->name = git__calloc(GIT_REFNAME_MAX + 1, sizeof(char));
 	GITERR_CHECK_ALLOC(scan->name);
 
-	if ((result = normalize_name(scan->name, GIT_REFNAME_MAX, name, 0)) < 0) {
-		git_reference_free(scan);
-		return result;
+	if ((result = git_reference__normalize_name(
+		scan->name,
+		GIT_REFNAME_MAX,
+		name)) < 0) {
+			git_reference_free(scan);
+			return result;
 	}
 
 	scan->target.symbolic = git__strdup(scan->name);
@@ -1198,8 +1196,11 @@ int git_reference_create_symbolic(
 	char normalized[GIT_REFNAME_MAX];
 	git_reference *ref = NULL;
 
-	if (normalize_name(normalized, sizeof(normalized), name, 0) < 0)
-		return -1;
+	if (git_reference__normalize_name(
+		normalized,
+		sizeof(normalized),
+		name) < 0)
+			return -1;
 
 	if (reference_can_write(repo, normalized, NULL, force) < 0)
 		return -1;
@@ -1234,8 +1235,11 @@ int git_reference_create_oid(
 	git_reference *ref = NULL;
 	char normalized[GIT_REFNAME_MAX];
 
-	if (normalize_name(normalized, sizeof(normalized), name, 1) < 0)
-		return -1;
+	if (git_reference__normalize_name_oid(
+		normalized,
+		sizeof(normalized),
+		name) < 0)
+			return -1;
 
 	if (reference_can_write(repo, normalized, NULL, force) < 0)
 		return -1;
@@ -1314,8 +1318,11 @@ int git_reference_set_target(git_reference *ref, const char *target)
 		return -1;
 	}
 
-	if (normalize_name(normalized, sizeof(normalized), target, 0))
-		return -1;
+	if (git_reference__normalize_name(
+		normalized,
+		sizeof(normalized),
+		target))
+			return -1;
 
 	git__free(ref->target.symbolic);
 	ref->target.symbolic = git__strdup(normalized);
@@ -1327,15 +1334,23 @@ int git_reference_set_target(git_reference *ref, const char *target)
 int git_reference_rename(git_reference *ref, const char *new_name, int force)
 {
 	int result;
+	unsigned int normalization_flags;
 	git_buf aux_path = GIT_BUF_INIT;
 	char normalized[GIT_REFNAME_MAX];
 
 	const char *head_target = NULL;
 	git_reference *head = NULL;
 
-	if (normalize_name(normalized, sizeof(normalized),
-		new_name, ref->flags & GIT_REF_OID) < 0)
-		return -1;
+	normalization_flags = ref->flags & GIT_REF_SYMBOLIC ?
+		GIT_REF_FORMAT_ALLOW_ONELEVEL
+		: GIT_REF_FORMAT_NORMAL;
+
+	if (git_reference_normalize_name(
+		normalized,
+		sizeof(normalized),
+		new_name,
+		normalization_flags) < 0)
+			return -1;
 
 	if (reference_can_write(ref->owner, normalized, ref->name, force) < 0)
 		return -1;
@@ -1565,11 +1580,11 @@ static int is_valid_ref_char(char ch)
 	}
 }
 
-static int normalize_name(
+int git_reference_normalize_name(
 	char *buffer_out,
-	size_t out_size,
+	size_t buffer_size,
 	const char *name,
-	int is_oid_ref)
+	unsigned int flags)
 {
 	const char *name_end, *buffer_out_start;
 	const char *current;
@@ -1577,12 +1592,17 @@ static int normalize_name(
 
 	assert(name && buffer_out);
 
+	if (flags & GIT_REF_FORMAT_REFSPEC_PATTERN) {
+		giterr_set(GITERR_INVALID, "Unimplemented");
+		return -1;
+	}
+
 	buffer_out_start = buffer_out;
 	current = name;
 	name_end = name + strlen(name);
 
 	/* Terminating null byte */
-	out_size--;
+	buffer_size--;
 
 	/* A refname can not be empty */
 	if (name_end == name)
@@ -1592,7 +1612,7 @@ static int normalize_name(
 	if (*(name_end - 1) == '.' || *(name_end - 1) == '/')
 		goto invalid_name;
 
-	while (current < name_end && out_size) {
+	while (current < name_end && buffer_size > 0) {
 		if (!is_valid_ref_char(*current))
 			goto invalid_name;
 
@@ -1615,19 +1635,29 @@ static int normalize_name(
 		}
 
 		if (*current == '/')
-			contains_a_slash = 1;
+			if (buffer_out > buffer_out_start)
+				contains_a_slash = 1;
+			else {
+				current++;
+				continue;
+			}
+
 
 		*buffer_out++ = *current++;
-		out_size--;
+		buffer_size--;
 	}
 
-	if (!out_size)
-		goto invalid_name;
+	if (current < name_end) {
+		giterr_set(
+		GITERR_REFERENCE,
+		"The provided buffer is too short to hold the normalization of '%s'", name);
+		return GIT_EBUFS;
+	}
 
 	/* Object id refname have to contain at least one slash, except
 	 * for HEAD in a detached state or MERGE_HEAD if we're in the
 	 * middle of a merge */
-	if (is_oid_ref &&
+	if (!(flags & GIT_REF_FORMAT_ALLOW_ONELEVEL) &&
 		!contains_a_slash &&
 		strcmp(name, GIT_HEAD_FILE) != 0 &&
 		strcmp(name, GIT_MERGE_HEAD_FILE) != 0 &&
@@ -1640,18 +1670,12 @@ static int normalize_name(
 
 	*buffer_out = '\0';
 
-	/*
-	 * For object id references, name has to start with refs/. Again,
-	 * we need to allow HEAD to be in a detached state.
-	 */
-	if (is_oid_ref && !(git__prefixcmp(buffer_out_start, GIT_REFS_DIR) ||
-		strcmp(buffer_out_start, GIT_HEAD_FILE)))
-		goto invalid_name;
-
 	return 0;
 
 invalid_name:
-	giterr_set(GITERR_REFERENCE, "The given reference name is not valid");
+	giterr_set(
+		GITERR_REFERENCE,
+		"The given reference name '%s' is not valid", name);
 	return -1;
 }
 
@@ -1660,7 +1684,11 @@ int git_reference__normalize_name(
 	size_t out_size,
 	const char *name)
 {
-	return normalize_name(buffer_out, out_size, name, 0);
+	return git_reference_normalize_name(
+		buffer_out,
+		out_size,
+		name,
+		GIT_REF_FORMAT_ALLOW_ONELEVEL);
 }
 
 int git_reference__normalize_name_oid(
@@ -1668,7 +1696,11 @@ int git_reference__normalize_name_oid(
 	size_t out_size,
 	const char *name)
 {
-	return normalize_name(buffer_out, out_size, name, 1);
+	return git_reference_normalize_name(
+		buffer_out,
+		out_size,
+		name,
+		GIT_REF_FORMAT_NORMAL);
 }
 
 #define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC)
diff --git a/tests-clar/refs/normalize.c b/tests-clar/refs/normalize.c
index 135d0a9..4e80e4b 100644
--- a/tests-clar/refs/normalize.c
+++ b/tests-clar/refs/normalize.c
@@ -4,70 +4,111 @@
 #include "git2/reflog.h"
 #include "reflog.h"
 
-
 // Helpers
-static void ensure_refname_normalized(int is_oid_ref,
+static void ensure_refname_normalized(unsigned int flags,
                                       const char *input_refname,
                                       const char *expected_refname)
 {
 	char buffer_out[GIT_REFNAME_MAX];
 
-	if (is_oid_ref)
-		cl_git_pass(git_reference__normalize_name_oid(buffer_out, sizeof(buffer_out), input_refname));
-	else
-		cl_git_pass(git_reference__normalize_name(buffer_out, sizeof(buffer_out), input_refname));
+	cl_git_pass(git_reference_normalize_name(buffer_out, sizeof(buffer_out), input_refname, flags));
 
-   if (expected_refname)
-	   cl_assert(0 == strcmp(buffer_out, expected_refname));
+	cl_assert_equal_i(0, strcmp(buffer_out, expected_refname));
 }
 
-static void ensure_refname_invalid(int is_oid_ref, const char *input_refname)
+static void ensure_refname_invalid(unsigned int flags, const char *input_refname)
 {
 	char buffer_out[GIT_REFNAME_MAX];
 
-   if (is_oid_ref)
-      cl_git_fail(git_reference__normalize_name_oid(buffer_out, sizeof(buffer_out), input_refname));
-   else
-      cl_git_fail(git_reference__normalize_name(buffer_out, sizeof(buffer_out), input_refname));
+	cl_git_fail(git_reference_normalize_name(buffer_out, sizeof(buffer_out), input_refname, flags));
 }
 
-#define OID_REF 1
-#define SYM_REF 0
-
+void test_refs_normalize__can_normalize_a_direct_reference_name(void)
+{
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs/dummy/a", "refs/dummy/a");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs/stash", "refs/stash");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs/tags/a", "refs/tags/a");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/a/b", "refs/heads/a/b");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/a./b", "refs/heads/a./b");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/v@ation", "refs/heads/v@ation");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "/refs///heads///a", "refs/heads/a");
+}
 
+void test_refs_normalize__can_normalize_some_specific_one_level_direct_reference_names(void)
+{
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "HEAD", "HEAD");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "MERGE_HEAD", "MERGE_HEAD");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "FETCH_HEAD", "FETCH_HEAD");
+}
 
-void test_refs_normalize__direct(void)
+void test_refs_normalize__cannot_normalize_any_direct_reference_name(void)
 {
-   // normalize a direct (OID) reference name
-	ensure_refname_invalid(OID_REF, "a");
-	ensure_refname_invalid(OID_REF, "");
-	ensure_refname_invalid(OID_REF, "refs/heads/a/");
-	ensure_refname_invalid(OID_REF, "refs/heads/a.");
-	ensure_refname_invalid(OID_REF, "refs/heads/a.lock");
-	ensure_refname_normalized(OID_REF, "refs/dummy/a", NULL);
-	ensure_refname_normalized(OID_REF, "refs/stash", NULL);
-	ensure_refname_normalized(OID_REF, "refs/tags/a", "refs/tags/a");
-	ensure_refname_normalized(OID_REF, "refs/heads/a/b", "refs/heads/a/b");
-	ensure_refname_normalized(OID_REF, "refs/heads/a./b", "refs/heads/a./b");
-	ensure_refname_invalid(OID_REF, "refs/heads/foo?bar");
-	ensure_refname_invalid(OID_REF, "refs/heads\foo");
-	ensure_refname_normalized(OID_REF, "refs/heads/v@ation", "refs/heads/v@ation");
-	ensure_refname_normalized(OID_REF, "refs///heads///a", "refs/heads/a");
-	ensure_refname_invalid(OID_REF, "refs/heads/.a/b");
-	ensure_refname_invalid(OID_REF, "refs/heads/foo/../bar");
-	ensure_refname_invalid(OID_REF, "refs/heads/foo..bar");
-	ensure_refname_invalid(OID_REF, "refs/heads/./foo");
-	ensure_refname_invalid(OID_REF, "refs/heads/v@{ation");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "a");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "/a");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "//a");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/a/");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/a.");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/a.lock");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/foo?bar");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads\foo");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/v@ation", "refs/heads/v@ation");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_NORMAL, "refs///heads///a", "refs/heads/a");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/.a/b");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/foo/../bar");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/foo..bar");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/./foo");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "refs/heads/v@{ation");
 }
 
 void test_refs_normalize__symbolic(void)
 {
-   // normalize a symbolic reference name
-	ensure_refname_normalized(SYM_REF, "a", "a");
-	ensure_refname_normalized(SYM_REF, "a/b", "a/b");
-	ensure_refname_normalized(SYM_REF, "refs///heads///a", "refs/heads/a");
-	ensure_refname_invalid(SYM_REF, "");
-	ensure_refname_invalid(SYM_REF, "heads\foo");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "heads\foo");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "///");
+
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "a", "a");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "a/b", "a/b");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs///heads///a", "refs/heads/a");
+
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "HEAD", "HEAD");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "MERGE_HEAD", "MERGE_HEAD");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "FETCH_HEAD", "FETCH_HEAD");
 }
 
 /* Ported from JGit, BSD licence.
@@ -77,31 +118,42 @@ void test_refs_normalize__jgit_suite(void)
    // tests borrowed from JGit
 
 /* EmptyString */
-	ensure_refname_invalid(SYM_REF, "");
-	ensure_refname_invalid(SYM_REF, "/");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "/");
 
 /* MustHaveTwoComponents */
-	ensure_refname_invalid(OID_REF, "master");
-	ensure_refname_normalized(SYM_REF, "heads/master", "heads/master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_NORMAL, "master");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "heads/master", "heads/master");
 
 /* ValidHead */
 
-	ensure_refname_normalized(SYM_REF, "refs/heads/master", "refs/heads/master");
-	ensure_refname_normalized(SYM_REF, "refs/heads/pu", "refs/heads/pu");
-	ensure_refname_normalized(SYM_REF, "refs/heads/z", "refs/heads/z");
-	ensure_refname_normalized(SYM_REF, "refs/heads/FoO", "refs/heads/FoO");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master", "refs/heads/master");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/pu", "refs/heads/pu");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/z", "refs/heads/z");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/FoO", "refs/heads/FoO");
 
 /* ValidTag */
-	ensure_refname_normalized(SYM_REF, "refs/tags/v1.0", "refs/tags/v1.0");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/tags/v1.0", "refs/tags/v1.0");
 
 /* NoLockSuffix */
-	ensure_refname_invalid(SYM_REF, "refs/heads/master.lock");
+	ensure_refname_invalid(GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master.lock");
 
 /* NoDirectorySuffix */
-	ensure_refname_invalid(SYM_REF, "refs/heads/master/");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master/");
 
 /* NoSpace */
-	ensure_refname_invalid(SYM_REF, "refs/heads/i haz space");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/i haz space");
 
 /* NoAsciiControlCharacters */
 	{
@@ -112,89 +164,153 @@ void test_refs_normalize__jgit_suite(void)
 			strncpy(buffer + 15, (const char *)&c, 1);
 			strncpy(buffer + 16, "er", 2);
 			buffer[18 - 1] = '\0';
-			ensure_refname_invalid(SYM_REF, buffer);
+			ensure_refname_invalid(GIT_REF_FORMAT_ALLOW_ONELEVEL, buffer);
 		}
 	}
 
 /* NoBareDot */
-	ensure_refname_invalid(SYM_REF, "refs/heads/.");
-	ensure_refname_invalid(SYM_REF, "refs/heads/..");
-	ensure_refname_invalid(SYM_REF, "refs/heads/./master");
-	ensure_refname_invalid(SYM_REF, "refs/heads/../master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/.");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/..");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/./master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/../master");
 
 /* NoLeadingOrTrailingDot */
-	ensure_refname_invalid(SYM_REF, ".");
-	ensure_refname_invalid(SYM_REF, "refs/heads/.bar");
-	ensure_refname_invalid(SYM_REF, "refs/heads/..bar");
-	ensure_refname_invalid(SYM_REF, "refs/heads/bar.");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, ".");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/.bar");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/..bar");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/bar.");
 
 /* ContainsDot */
-	ensure_refname_normalized(SYM_REF, "refs/heads/m.a.s.t.e.r", "refs/heads/m.a.s.t.e.r");
-	ensure_refname_invalid(SYM_REF, "refs/heads/master..pu");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/m.a.s.t.e.r", "refs/heads/m.a.s.t.e.r");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master..pu");
 
 /* NoMagicRefCharacters */
-	ensure_refname_invalid(SYM_REF, "refs/heads/master^");
-	ensure_refname_invalid(SYM_REF, "refs/heads/^master");
-	ensure_refname_invalid(SYM_REF, "^refs/heads/master");
-
-	ensure_refname_invalid(SYM_REF, "refs/heads/master~");
-	ensure_refname_invalid(SYM_REF, "refs/heads/~master");
-	ensure_refname_invalid(SYM_REF, "~refs/heads/master");
-
-	ensure_refname_invalid(SYM_REF, "refs/heads/master:");
-	ensure_refname_invalid(SYM_REF, "refs/heads/:master");
-	ensure_refname_invalid(SYM_REF, ":refs/heads/master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master^");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/^master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "^refs/heads/master");
+
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master~");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/~master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "~refs/heads/master");
+
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master:");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/:master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, ":refs/heads/master");
 
 /* ShellGlob */
-	ensure_refname_invalid(SYM_REF, "refs/heads/master?");
-	ensure_refname_invalid(SYM_REF, "refs/heads/?master");
-	ensure_refname_invalid(SYM_REF, "?refs/heads/master");
-
-	ensure_refname_invalid(SYM_REF, "refs/heads/master[");
-	ensure_refname_invalid(SYM_REF, "refs/heads/[master");
-	ensure_refname_invalid(SYM_REF, "[refs/heads/master");
-
-	ensure_refname_invalid(SYM_REF, "refs/heads/master*");
-	ensure_refname_invalid(SYM_REF, "refs/heads/*master");
-	ensure_refname_invalid(SYM_REF, "*refs/heads/master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master?");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/?master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "?refs/heads/master");
+
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master[");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/[master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "[refs/heads/master");
+
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master*");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/*master");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "*refs/heads/master");
 
 /* ValidSpecialCharacters */
-	ensure_refname_normalized(SYM_REF, "refs/heads/!", "refs/heads/!");
-	ensure_refname_normalized(SYM_REF, "refs/heads/\"", "refs/heads/\"");
-	ensure_refname_normalized(SYM_REF, "refs/heads/#", "refs/heads/#");
-	ensure_refname_normalized(SYM_REF, "refs/heads/$", "refs/heads/$");
-	ensure_refname_normalized(SYM_REF, "refs/heads/%", "refs/heads/%");
-	ensure_refname_normalized(SYM_REF, "refs/heads/&", "refs/heads/&");
-	ensure_refname_normalized(SYM_REF, "refs/heads/'", "refs/heads/'");
-	ensure_refname_normalized(SYM_REF, "refs/heads/(", "refs/heads/(");
-	ensure_refname_normalized(SYM_REF, "refs/heads/)", "refs/heads/)");
-	ensure_refname_normalized(SYM_REF, "refs/heads/+", "refs/heads/+");
-	ensure_refname_normalized(SYM_REF, "refs/heads/,", "refs/heads/,");
-	ensure_refname_normalized(SYM_REF, "refs/heads/-", "refs/heads/-");
-	ensure_refname_normalized(SYM_REF, "refs/heads/;", "refs/heads/;");
-	ensure_refname_normalized(SYM_REF, "refs/heads/<", "refs/heads/<");
-	ensure_refname_normalized(SYM_REF, "refs/heads/=", "refs/heads/=");
-	ensure_refname_normalized(SYM_REF, "refs/heads/>", "refs/heads/>");
-	ensure_refname_normalized(SYM_REF, "refs/heads/@", "refs/heads/@");
-	ensure_refname_normalized(SYM_REF, "refs/heads/]", "refs/heads/]");
-	ensure_refname_normalized(SYM_REF, "refs/heads/_", "refs/heads/_");
-	ensure_refname_normalized(SYM_REF, "refs/heads/`", "refs/heads/`");
-	ensure_refname_normalized(SYM_REF, "refs/heads/{", "refs/heads/{");
-	ensure_refname_normalized(SYM_REF, "refs/heads/|", "refs/heads/|");
-	ensure_refname_normalized(SYM_REF, "refs/heads/}", "refs/heads/}");
+	ensure_refname_normalized
+		(GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/!", "refs/heads/!");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/\"", "refs/heads/\"");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/#", "refs/heads/#");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/$", "refs/heads/$");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/%", "refs/heads/%");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/&", "refs/heads/&");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/'", "refs/heads/'");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/(", "refs/heads/(");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/)", "refs/heads/)");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/+", "refs/heads/+");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/,", "refs/heads/,");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/-", "refs/heads/-");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/;", "refs/heads/;");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/<", "refs/heads/<");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/=", "refs/heads/=");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/>", "refs/heads/>");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/@", "refs/heads/@");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/]", "refs/heads/]");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/_", "refs/heads/_");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/`", "refs/heads/`");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/{", "refs/heads/{");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/|", "refs/heads/|");
+	ensure_refname_normalized(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/}", "refs/heads/}");
 
 	// This is valid on UNIX, but not on Windows
 	// hence we make in invalid due to non-portability
 	//
-	ensure_refname_invalid(SYM_REF, "refs/heads/\\");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/\\");
 
 /* UnicodeNames */
 	/*
 	 * Currently this fails.
-	 * ensure_refname_normalized(SYM_REF, "refs/heads/\u00e5ngstr\u00f6m", "refs/heads/\u00e5ngstr\u00f6m");
+	 * ensure_refname_normalized(GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/\u00e5ngstr\u00f6m", "refs/heads/\u00e5ngstr\u00f6m");
 	 */
 
 /* RefLogQueryIsValidRef */
-	ensure_refname_invalid(SYM_REF, "refs/heads/master@{1}");
-	ensure_refname_invalid(SYM_REF, "refs/heads/master@{1.hour.ago}");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master@{1}");
+	ensure_refname_invalid(
+		GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master@{1.hour.ago}");
+}
+
+void test_refs_normalize__buffer_has_to_be_big_enough_to_hold_the_normalized_version(void)
+{
+	char buffer_out[21];
+
+	cl_git_pass(git_reference_normalize_name(
+		buffer_out, 21, "//refs//heads/long///name", GIT_REF_FORMAT_NORMAL));
+	cl_git_fail(git_reference_normalize_name(
+		buffer_out, 20, "//refs//heads/long///name", GIT_REF_FORMAT_NORMAL));
 }