forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefs.c
1835 lines (1618 loc) · 43.6 KB
/
refs.c
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cache.h"
#include "refs.h"
#include "object.h"
#include "tag.h"
#include "dir.h"
/* ISSYMREF=01 and ISPACKED=02 are public interfaces */
#define REF_KNOWS_PEELED 04
#define REF_BROKEN 010
struct ref_list {
struct ref_list *next;
unsigned char flag; /* ISSYMREF? ISPACKED? */
unsigned char sha1[20];
unsigned char peeled[20];
char name[FLEX_ARRAY];
};
static const char *parse_ref_line(char *line, unsigned char *sha1)
{
/*
* 42: the answer to everything.
*
* In this case, it happens to be the answer to
* 40 (length of sha1 hex representation)
* +1 (space in between hex and name)
* +1 (newline at the end of the line)
*/
int len = strlen(line) - 42;
if (len <= 0)
return NULL;
if (get_sha1_hex(line, sha1) < 0)
return NULL;
if (!isspace(line[40]))
return NULL;
line += 41;
if (isspace(*line))
return NULL;
if (line[len] != '\n')
return NULL;
line[len] = 0;
return line;
}
static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
int flag, struct ref_list *list,
struct ref_list **new_entry)
{
int len;
struct ref_list *entry;
/* Allocate it and add it in.. */
len = strlen(name) + 1;
entry = xmalloc(sizeof(struct ref_list) + len);
hashcpy(entry->sha1, sha1);
hashclr(entry->peeled);
memcpy(entry->name, name, len);
entry->flag = flag;
entry->next = list;
if (new_entry)
*new_entry = entry;
return entry;
}
/* merge sort the ref list */
static struct ref_list *sort_ref_list(struct ref_list *list)
{
int psize, qsize, last_merge_count, cmp;
struct ref_list *p, *q, *l, *e;
struct ref_list *new_list = list;
int k = 1;
int merge_count = 0;
if (!list)
return list;
do {
last_merge_count = merge_count;
merge_count = 0;
psize = 0;
p = new_list;
q = new_list;
new_list = NULL;
l = NULL;
while (p) {
merge_count++;
while (psize < k && q->next) {
q = q->next;
psize++;
}
qsize = k;
while ((psize > 0) || (qsize > 0 && q)) {
if (qsize == 0 || !q) {
e = p;
p = p->next;
psize--;
} else if (psize == 0) {
e = q;
q = q->next;
qsize--;
} else {
cmp = strcmp(q->name, p->name);
if (cmp < 0) {
e = q;
q = q->next;
qsize--;
} else if (cmp > 0) {
e = p;
p = p->next;
psize--;
} else {
if (hashcmp(q->sha1, p->sha1))
die("Duplicated ref, and SHA1s don't match: %s",
q->name);
warning("Duplicated ref: %s", q->name);
e = q;
q = q->next;
qsize--;
free(e);
e = p;
p = p->next;
psize--;
}
}
e->next = NULL;
if (l)
l->next = e;
if (!new_list)
new_list = e;
l = e;
}
p = q;
};
k = k * 2;
} while ((last_merge_count != merge_count) || (last_merge_count != 1));
return new_list;
}
/*
* Future: need to be in "struct repository"
* when doing a full libification.
*/
static struct cached_refs {
char did_loose;
char did_packed;
struct ref_list *loose;
struct ref_list *packed;
} cached_refs;
static struct ref_list *current_ref;
static struct ref_list *extra_refs;
static void free_ref_list(struct ref_list *list)
{
struct ref_list *next;
for ( ; list; list = next) {
next = list->next;
free(list);
}
}
static void invalidate_cached_refs(void)
{
struct cached_refs *ca = &cached_refs;
if (ca->did_loose && ca->loose)
free_ref_list(ca->loose);
if (ca->did_packed && ca->packed)
free_ref_list(ca->packed);
ca->loose = ca->packed = NULL;
ca->did_loose = ca->did_packed = 0;
}
static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
{
struct ref_list *list = NULL;
struct ref_list *last = NULL;
char refline[PATH_MAX];
int flag = REF_ISPACKED;
while (fgets(refline, sizeof(refline), f)) {
unsigned char sha1[20];
const char *name;
static const char header[] = "# pack-refs with:";
if (!strncmp(refline, header, sizeof(header)-1)) {
const char *traits = refline + sizeof(header) - 1;
if (strstr(traits, " peeled "))
flag |= REF_KNOWS_PEELED;
/* perhaps other traits later as well */
continue;
}
name = parse_ref_line(refline, sha1);
if (name) {
list = add_ref(name, sha1, flag, list, &last);
continue;
}
if (last &&
refline[0] == '^' &&
strlen(refline) == 42 &&
refline[41] == '\n' &&
!get_sha1_hex(refline + 1, sha1))
hashcpy(last->peeled, sha1);
}
cached_refs->packed = sort_ref_list(list);
}
void add_extra_ref(const char *name, const unsigned char *sha1, int flag)
{
extra_refs = add_ref(name, sha1, flag, extra_refs, NULL);
}
void clear_extra_refs(void)
{
free_ref_list(extra_refs);
extra_refs = NULL;
}
static struct ref_list *get_packed_refs(void)
{
if (!cached_refs.did_packed) {
FILE *f = fopen(git_path("packed-refs"), "r");
cached_refs.packed = NULL;
if (f) {
read_packed_refs(f, &cached_refs);
fclose(f);
}
cached_refs.did_packed = 1;
}
return cached_refs.packed;
}
static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
{
DIR *dir = opendir(git_path("%s", base));
if (dir) {
struct dirent *de;
int baselen = strlen(base);
char *ref = xmalloc(baselen + 257);
memcpy(ref, base, baselen);
if (baselen && base[baselen-1] != '/')
ref[baselen++] = '/';
while ((de = readdir(dir)) != NULL) {
unsigned char sha1[20];
struct stat st;
int flag;
int namelen;
if (de->d_name[0] == '.')
continue;
namelen = strlen(de->d_name);
if (namelen > 255)
continue;
if (has_extension(de->d_name, ".lock"))
continue;
memcpy(ref + baselen, de->d_name, namelen+1);
if (stat(git_path("%s", ref), &st) < 0)
continue;
if (S_ISDIR(st.st_mode)) {
list = get_ref_dir(ref, list);
continue;
}
if (!resolve_ref(ref, sha1, 1, &flag)) {
hashclr(sha1);
flag |= REF_BROKEN;
}
list = add_ref(ref, sha1, flag, list, NULL);
}
free(ref);
closedir(dir);
}
return sort_ref_list(list);
}
struct warn_if_dangling_data {
FILE *fp;
const char *refname;
const char *msg_fmt;
};
static int warn_if_dangling_symref(const char *refname, const unsigned char *sha1,
int flags, void *cb_data)
{
struct warn_if_dangling_data *d = cb_data;
const char *resolves_to;
unsigned char junk[20];
if (!(flags & REF_ISSYMREF))
return 0;
resolves_to = resolve_ref(refname, junk, 0, NULL);
if (!resolves_to || strcmp(resolves_to, d->refname))
return 0;
fprintf(d->fp, d->msg_fmt, refname);
return 0;
}
void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
{
struct warn_if_dangling_data data = { fp, refname, msg_fmt };
for_each_rawref(warn_if_dangling_symref, &data);
}
static struct ref_list *get_loose_refs(void)
{
if (!cached_refs.did_loose) {
cached_refs.loose = get_ref_dir("refs", NULL);
cached_refs.did_loose = 1;
}
return cached_refs.loose;
}
/* We allow "recursive" symbolic refs. Only within reason, though */
#define MAXDEPTH 5
#define MAXREFLEN (1024)
static int resolve_gitlink_packed_ref(char *name, int pathlen, const char *refname, unsigned char *result)
{
FILE *f;
struct cached_refs refs;
struct ref_list *ref;
int retval;
strcpy(name + pathlen, "packed-refs");
f = fopen(name, "r");
if (!f)
return -1;
read_packed_refs(f, &refs);
fclose(f);
ref = refs.packed;
retval = -1;
while (ref) {
if (!strcmp(ref->name, refname)) {
retval = 0;
memcpy(result, ref->sha1, 20);
break;
}
ref = ref->next;
}
free_ref_list(refs.packed);
return retval;
}
static int resolve_gitlink_ref_recursive(char *name, int pathlen, const char *refname, unsigned char *result, int recursion)
{
int fd, len = strlen(refname);
char buffer[128], *p;
if (recursion > MAXDEPTH || len > MAXREFLEN)
return -1;
memcpy(name + pathlen, refname, len+1);
fd = open(name, O_RDONLY);
if (fd < 0)
return resolve_gitlink_packed_ref(name, pathlen, refname, result);
len = read(fd, buffer, sizeof(buffer)-1);
close(fd);
if (len < 0)
return -1;
while (len && isspace(buffer[len-1]))
len--;
buffer[len] = 0;
/* Was it a detached head or an old-fashioned symlink? */
if (!get_sha1_hex(buffer, result))
return 0;
/* Symref? */
if (strncmp(buffer, "ref:", 4))
return -1;
p = buffer + 4;
while (isspace(*p))
p++;
return resolve_gitlink_ref_recursive(name, pathlen, p, result, recursion+1);
}
int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *result)
{
int len = strlen(path), retval;
char *gitdir;
const char *tmp;
while (len && path[len-1] == '/')
len--;
if (!len)
return -1;
gitdir = xmalloc(len + MAXREFLEN + 8);
memcpy(gitdir, path, len);
memcpy(gitdir + len, "/.git", 6);
len += 5;
tmp = read_gitfile_gently(gitdir);
if (tmp) {
free(gitdir);
len = strlen(tmp);
gitdir = xmalloc(len + MAXREFLEN + 3);
memcpy(gitdir, tmp, len);
}
gitdir[len] = '/';
gitdir[++len] = '\0';
retval = resolve_gitlink_ref_recursive(gitdir, len, refname, result, 0);
free(gitdir);
return retval;
}
/*
* If the "reading" argument is set, this function finds out what _object_
* the ref points at by "reading" the ref. The ref, if it is not symbolic,
* has to exist, and if it is symbolic, it has to point at an existing ref,
* because the "read" goes through the symref to the ref it points at.
*
* The access that is not "reading" may often be "writing", but does not
* have to; it can be merely checking _where it leads to_. If it is a
* prelude to "writing" to the ref, a write to a symref that points at
* yet-to-be-born ref will create the real ref pointed by the symref.
* reading=0 allows the caller to check where such a symref leads to.
*/
const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
{
int depth = MAXDEPTH;
ssize_t len;
char buffer[256];
static char ref_buffer[256];
if (flag)
*flag = 0;
for (;;) {
char path[PATH_MAX];
struct stat st;
char *buf;
int fd;
if (--depth < 0)
return NULL;
git_snpath(path, sizeof(path), "%s", ref);
/* Special case: non-existing file. */
if (lstat(path, &st) < 0) {
struct ref_list *list = get_packed_refs();
while (list) {
if (!strcmp(ref, list->name)) {
hashcpy(sha1, list->sha1);
if (flag)
*flag |= REF_ISPACKED;
return ref;
}
list = list->next;
}
if (reading || errno != ENOENT)
return NULL;
hashclr(sha1);
return ref;
}
/* Follow "normalized" - ie "refs/.." symlinks by hand */
if (S_ISLNK(st.st_mode)) {
len = readlink(path, buffer, sizeof(buffer)-1);
if (len >= 5 && !memcmp("refs/", buffer, 5)) {
buffer[len] = 0;
strcpy(ref_buffer, buffer);
ref = ref_buffer;
if (flag)
*flag |= REF_ISSYMREF;
continue;
}
}
/* Is it a directory? */
if (S_ISDIR(st.st_mode)) {
errno = EISDIR;
return NULL;
}
/*
* Anything else, just open it and try to use it as
* a ref
*/
fd = open(path, O_RDONLY);
if (fd < 0)
return NULL;
len = read_in_full(fd, buffer, sizeof(buffer)-1);
close(fd);
/*
* Is it a symbolic ref?
*/
if (len < 4 || memcmp("ref:", buffer, 4))
break;
buf = buffer + 4;
len -= 4;
while (len && isspace(*buf))
buf++, len--;
while (len && isspace(buf[len-1]))
len--;
buf[len] = 0;
memcpy(ref_buffer, buf, len + 1);
ref = ref_buffer;
if (flag)
*flag |= REF_ISSYMREF;
}
if (len < 40 || get_sha1_hex(buffer, sha1))
return NULL;
return ref;
}
/* The argument to filter_refs */
struct ref_filter {
const char *pattern;
each_ref_fn *fn;
void *cb_data;
};
int read_ref(const char *ref, unsigned char *sha1)
{
if (resolve_ref(ref, sha1, 1, NULL))
return 0;
return -1;
}
#define DO_FOR_EACH_INCLUDE_BROKEN 01
static int do_one_ref(const char *base, each_ref_fn fn, int trim,
int flags, void *cb_data, struct ref_list *entry)
{
if (strncmp(base, entry->name, trim))
return 0;
if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
if (entry->flag & REF_BROKEN)
return 0; /* ignore dangling symref */
if (!has_sha1_file(entry->sha1)) {
error("%s does not point to a valid object!", entry->name);
return 0;
}
}
current_ref = entry;
return fn(entry->name + trim, entry->sha1, entry->flag, cb_data);
}
static int filter_refs(const char *ref, const unsigned char *sha, int flags,
void *data)
{
struct ref_filter *filter = (struct ref_filter *)data;
if (fnmatch(filter->pattern, ref, 0))
return 0;
return filter->fn(ref, sha, flags, filter->cb_data);
}
int peel_ref(const char *ref, unsigned char *sha1)
{
int flag;
unsigned char base[20];
struct object *o;
if (current_ref && (current_ref->name == ref
|| !strcmp(current_ref->name, ref))) {
if (current_ref->flag & REF_KNOWS_PEELED) {
hashcpy(sha1, current_ref->peeled);
return 0;
}
hashcpy(base, current_ref->sha1);
goto fallback;
}
if (!resolve_ref(ref, base, 1, &flag))
return -1;
if ((flag & REF_ISPACKED)) {
struct ref_list *list = get_packed_refs();
while (list) {
if (!strcmp(list->name, ref)) {
if (list->flag & REF_KNOWS_PEELED) {
hashcpy(sha1, list->peeled);
return 0;
}
/* older pack-refs did not leave peeled ones */
break;
}
list = list->next;
}
}
fallback:
o = parse_object(base);
if (o && o->type == OBJ_TAG) {
o = deref_tag(o, ref, 0);
if (o) {
hashcpy(sha1, o->sha1);
return 0;
}
}
return -1;
}
static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
int flags, void *cb_data)
{
int retval = 0;
struct ref_list *packed = get_packed_refs();
struct ref_list *loose = get_loose_refs();
struct ref_list *extra;
for (extra = extra_refs; extra; extra = extra->next)
retval = do_one_ref(base, fn, trim, flags, cb_data, extra);
while (packed && loose) {
struct ref_list *entry;
int cmp = strcmp(packed->name, loose->name);
if (!cmp) {
packed = packed->next;
continue;
}
if (cmp > 0) {
entry = loose;
loose = loose->next;
} else {
entry = packed;
packed = packed->next;
}
retval = do_one_ref(base, fn, trim, flags, cb_data, entry);
if (retval)
goto end_each;
}
for (packed = packed ? packed : loose; packed; packed = packed->next) {
retval = do_one_ref(base, fn, trim, flags, cb_data, packed);
if (retval)
goto end_each;
}
end_each:
current_ref = NULL;
return retval;
}
int head_ref(each_ref_fn fn, void *cb_data)
{
unsigned char sha1[20];
int flag;
if (resolve_ref("HEAD", sha1, 1, &flag))
return fn("HEAD", sha1, flag, cb_data);
return 0;
}
int for_each_ref(each_ref_fn fn, void *cb_data)
{
return do_for_each_ref("refs/", fn, 0, 0, cb_data);
}
int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
{
return do_for_each_ref(prefix, fn, strlen(prefix), 0, cb_data);
}
int for_each_tag_ref(each_ref_fn fn, void *cb_data)
{
return for_each_ref_in("refs/tags/", fn, cb_data);
}
int for_each_branch_ref(each_ref_fn fn, void *cb_data)
{
return for_each_ref_in("refs/heads/", fn, cb_data);
}
int for_each_remote_ref(each_ref_fn fn, void *cb_data)
{
return for_each_ref_in("refs/remotes/", fn, cb_data);
}
int for_each_replace_ref(each_ref_fn fn, void *cb_data)
{
return do_for_each_ref("refs/replace/", fn, 13, 0, cb_data);
}
int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
const char *prefix, void *cb_data)
{
struct strbuf real_pattern = STRBUF_INIT;
struct ref_filter filter;
int ret;
if (!prefix && prefixcmp(pattern, "refs/"))
strbuf_addstr(&real_pattern, "refs/");
else if (prefix)
strbuf_addstr(&real_pattern, prefix);
strbuf_addstr(&real_pattern, pattern);
if (!has_glob_specials(pattern)) {
/* Append implied '/' '*' if not present. */
if (real_pattern.buf[real_pattern.len - 1] != '/')
strbuf_addch(&real_pattern, '/');
/* No need to check for '*', there is none. */
strbuf_addch(&real_pattern, '*');
}
filter.pattern = real_pattern.buf;
filter.fn = fn;
filter.cb_data = cb_data;
ret = for_each_ref(filter_refs, &filter);
strbuf_release(&real_pattern);
return ret;
}
int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
{
return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
}
int for_each_rawref(each_ref_fn fn, void *cb_data)
{
return do_for_each_ref("refs/", fn, 0,
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
}
/*
* Make sure "ref" is something reasonable to have under ".git/refs/";
* We do not like it if:
*
* - any path component of it begins with ".", or
* - it has double dots "..", or
* - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
* - it ends with a "/".
* - it ends with ".lock"
* - it contains a "\" (backslash)
*/
static inline int bad_ref_char(int ch)
{
if (((unsigned) ch) <= ' ' ||
ch == '~' || ch == '^' || ch == ':' || ch == '\\')
return 1;
/* 2.13 Pattern Matching Notation */
if (ch == '?' || ch == '[') /* Unsupported */
return 1;
if (ch == '*') /* Supported at the end */
return 2;
return 0;
}
int check_ref_format(const char *ref)
{
int ch, level, bad_type, last;
int ret = CHECK_REF_FORMAT_OK;
const char *cp = ref;
level = 0;
while (1) {
while ((ch = *cp++) == '/')
; /* tolerate duplicated slashes */
if (!ch)
/* should not end with slashes */
return CHECK_REF_FORMAT_ERROR;
/* we are at the beginning of the path component */
if (ch == '.')
return CHECK_REF_FORMAT_ERROR;
bad_type = bad_ref_char(ch);
if (bad_type) {
if (bad_type == 2 && (!*cp || *cp == '/') &&
ret == CHECK_REF_FORMAT_OK)
ret = CHECK_REF_FORMAT_WILDCARD;
else
return CHECK_REF_FORMAT_ERROR;
}
last = ch;
/* scan the rest of the path component */
while ((ch = *cp++) != 0) {
bad_type = bad_ref_char(ch);
if (bad_type)
return CHECK_REF_FORMAT_ERROR;
if (ch == '/')
break;
if (last == '.' && ch == '.')
return CHECK_REF_FORMAT_ERROR;
if (last == '@' && ch == '{')
return CHECK_REF_FORMAT_ERROR;
last = ch;
}
level++;
if (!ch) {
if (ref <= cp - 2 && cp[-2] == '.')
return CHECK_REF_FORMAT_ERROR;
if (level < 2)
return CHECK_REF_FORMAT_ONELEVEL;
if (has_extension(ref, ".lock"))
return CHECK_REF_FORMAT_ERROR;
return ret;
}
}
}
const char *prettify_refname(const char *name)
{
return name + (
!prefixcmp(name, "refs/heads/") ? 11 :
!prefixcmp(name, "refs/tags/") ? 10 :
!prefixcmp(name, "refs/remotes/") ? 13 :
0);
}
const char *ref_rev_parse_rules[] = {
"%.*s",
"refs/%.*s",
"refs/tags/%.*s",
"refs/heads/%.*s",
"refs/remotes/%.*s",
"refs/remotes/%.*s/HEAD",
NULL
};
const char *ref_fetch_rules[] = {
"%.*s",
"refs/%.*s",
"refs/heads/%.*s",
NULL
};
int refname_match(const char *abbrev_name, const char *full_name, const char **rules)
{
const char **p;
const int abbrev_name_len = strlen(abbrev_name);
for (p = rules; *p; p++) {
if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
return 1;
}
}
return 0;
}
static struct ref_lock *verify_lock(struct ref_lock *lock,
const unsigned char *old_sha1, int mustexist)
{
if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
error("Can't verify ref %s", lock->ref_name);
unlock_ref(lock);
return NULL;
}
if (hashcmp(lock->old_sha1, old_sha1)) {
error("Ref %s is at %s but expected %s", lock->ref_name,
sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
unlock_ref(lock);
return NULL;
}
return lock;
}
static int remove_empty_directories(const char *file)
{
/* we want to create a file but there is a directory there;
* if that is an empty directory (or a directory that contains
* only empty directories), remove them.
*/
struct strbuf path;
int result;
strbuf_init(&path, 20);
strbuf_addstr(&path, file);
result = remove_dir_recursively(&path, REMOVE_DIR_EMPTY_ONLY);
strbuf_release(&path);
return result;
}
static int is_refname_available(const char *ref, const char *oldref,
struct ref_list *list, int quiet)
{
int namlen = strlen(ref); /* e.g. 'foo/bar' */
while (list) {
/* list->name could be 'foo' or 'foo/bar/baz' */
if (!oldref || strcmp(oldref, list->name)) {
int len = strlen(list->name);
int cmplen = (namlen < len) ? namlen : len;
const char *lead = (namlen < len) ? list->name : ref;
if (!strncmp(ref, list->name, cmplen) &&
lead[cmplen] == '/') {
if (!quiet)
error("'%s' exists; cannot create '%s'",
list->name, ref);
return 0;
}
}
list = list->next;
}
return 1;
}
static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int flags, int *type_p)
{
char *ref_file;
const char *orig_ref = ref;
struct ref_lock *lock;
int last_errno = 0;
int type, lflags;
int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
int missing = 0;
lock = xcalloc(1, sizeof(struct ref_lock));
lock->lock_fd = -1;
ref = resolve_ref(ref, lock->old_sha1, mustexist, &type);
if (!ref && errno == EISDIR) {
/* we are trying to lock foo but we used to
* have foo/bar which now does not exist;
* it is normal for the empty directory 'foo'
* to remain.
*/
ref_file = git_path("%s", orig_ref);
if (remove_empty_directories(ref_file)) {
last_errno = errno;
error("there are still refs under '%s'", orig_ref);
goto error_return;
}
ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, &type);
}
if (type_p)
*type_p = type;
if (!ref) {
last_errno = errno;
error("unable to resolve reference %s: %s",
orig_ref, strerror(errno));
goto error_return;
}
missing = is_null_sha1(lock->old_sha1);
/* When the ref did not exist and we are creating it,
* make sure there is no existing ref that is packed
* whose name begins with our refname, nor a ref whose
* name is a proper prefix of our refname.
*/
if (missing &&
!is_refname_available(ref, NULL, get_packed_refs(), 0)) {
last_errno = ENOTDIR;
goto error_return;
}
lock->lk = xcalloc(1, sizeof(struct lock_file));
lflags = LOCK_DIE_ON_ERROR;
if (flags & REF_NODEREF) {
ref = orig_ref;
lflags |= LOCK_NODEREF;
}
lock->ref_name = xstrdup(ref);
lock->orig_ref_name = xstrdup(orig_ref);
ref_file = git_path("%s", ref);
if (missing)
lock->force_write = 1;
if ((flags & REF_NODEREF) && (type & REF_ISSYMREF))
lock->force_write = 1;
if (safe_create_leading_directories(ref_file)) {
last_errno = errno;
error("unable to create directory for %s", ref_file);
goto error_return;
}
lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags);
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
error_return:
unlock_ref(lock);
errno = last_errno;
return NULL;
}
struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
{
char refpath[PATH_MAX];
if (check_ref_format(ref))
return NULL;
strcpy(refpath, mkpath("refs/%s", ref));
return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);
}