Make ref resolution saner
[git/git.git] / refs.c
CommitLineData
95fc7512
DB
1#include "refs.h"
2#include "cache.h"
3
4#include <errno.h>
5
e1e22e37
LT
6struct ref_list {
7 struct ref_list *next;
8 unsigned char sha1[20];
9 char name[FLEX_ARRAY];
10};
11
12static const char *parse_ref_line(char *line, unsigned char *sha1)
13{
14 /*
15 * 42: the answer to everything.
16 *
17 * In this case, it happens to be the answer to
18 * 40 (length of sha1 hex representation)
19 * +1 (space in between hex and name)
20 * +1 (newline at the end of the line)
21 */
22 int len = strlen(line) - 42;
23
24 if (len <= 0)
25 return NULL;
26 if (get_sha1_hex(line, sha1) < 0)
27 return NULL;
28 if (!isspace(line[40]))
29 return NULL;
30 line += 41;
31 if (line[len] != '\n')
32 return NULL;
33 line[len] = 0;
34 return line;
35}
36
37static struct ref_list *add_ref(const char *name, const unsigned char *sha1, struct ref_list *list)
38{
39 int len;
40 struct ref_list **p = &list, *entry;
41
42 /* Find the place to insert the ref into.. */
43 while ((entry = *p) != NULL) {
44 int cmp = strcmp(entry->name, name);
45 if (cmp > 0)
46 break;
47
48 /* Same as existing entry? */
49 if (!cmp)
50 return list;
51 p = &entry->next;
52 }
53
54 /* Allocate it and add it in.. */
55 len = strlen(name) + 1;
56 entry = xmalloc(sizeof(struct ref_list) + len);
57 hashcpy(entry->sha1, sha1);
58 memcpy(entry->name, name, len);
59 entry->next = *p;
60 *p = entry;
61 return list;
62}
63
64static struct ref_list *get_packed_refs(void)
65{
66 static int did_refs = 0;
67 static struct ref_list *refs = NULL;
68
69 if (!did_refs) {
70 FILE *f = fopen(git_path("packed-refs"), "r");
71 if (f) {
72 struct ref_list *list = NULL;
73 char refline[PATH_MAX];
74 while (fgets(refline, sizeof(refline), f)) {
75 unsigned char sha1[20];
76 const char *name = parse_ref_line(refline, sha1);
77 if (!name)
78 continue;
79 list = add_ref(name, sha1, list);
80 }
81 fclose(f);
82 refs = list;
83 }
84 did_refs = 1;
85 }
86 return refs;
87}
88
89static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
90{
91 DIR *dir = opendir(git_path("%s", base));
92
93 if (dir) {
94 struct dirent *de;
95 int baselen = strlen(base);
ed378ec7 96 char *ref = xmalloc(baselen + 257);
e1e22e37 97
ed378ec7 98 memcpy(ref, base, baselen);
e1e22e37 99 if (baselen && base[baselen-1] != '/')
ed378ec7 100 ref[baselen++] = '/';
e1e22e37
LT
101
102 while ((de = readdir(dir)) != NULL) {
103 unsigned char sha1[20];
104 struct stat st;
105 int namelen;
106
107 if (de->d_name[0] == '.')
108 continue;
109 namelen = strlen(de->d_name);
110 if (namelen > 255)
111 continue;
112 if (has_extension(de->d_name, ".lock"))
113 continue;
ed378ec7
LT
114 memcpy(ref + baselen, de->d_name, namelen+1);
115 if (stat(git_path("%s", ref), &st) < 0)
e1e22e37
LT
116 continue;
117 if (S_ISDIR(st.st_mode)) {
ed378ec7 118 list = get_ref_dir(ref, list);
e1e22e37
LT
119 continue;
120 }
ed378ec7
LT
121 if (read_ref(ref, sha1) < 0) {
122 error("%s points nowhere!", ref);
e1e22e37
LT
123 continue;
124 }
ed378ec7 125 list = add_ref(ref, sha1, list);
e1e22e37 126 }
ed378ec7 127 free(ref);
e1e22e37
LT
128 closedir(dir);
129 }
130 return list;
131}
132
133static struct ref_list *get_loose_refs(void)
134{
135 static int did_refs = 0;
136 static struct ref_list *refs = NULL;
137
138 if (!did_refs) {
139 refs = get_ref_dir("refs", NULL);
140 did_refs = 1;
141 }
142 return refs;
143}
144
ca8db142
LT
145/* We allow "recursive" symbolic refs. Only within reason, though */
146#define MAXDEPTH 5
147
ed378ec7 148const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
8a65ff76 149{
a876ed83
JH
150 int depth = MAXDEPTH, len;
151 char buffer[256];
ed378ec7 152 static char ref_buffer[256];
ca8db142 153
a876ed83 154 for (;;) {
ed378ec7 155 const char *path = git_path("%s", ref);
a876ed83
JH
156 struct stat st;
157 char *buf;
158 int fd;
8a65ff76 159
a876ed83
JH
160 if (--depth < 0)
161 return NULL;
ca8db142 162
a876ed83
JH
163 /* Special case: non-existing file.
164 * Not having the refs/heads/new-branch is OK
165 * if we are writing into it, so is .git/HEAD
166 * that points at refs/heads/master still to be
167 * born. It is NOT OK if we are resolving for
168 * reading.
169 */
170 if (lstat(path, &st) < 0) {
171 if (reading || errno != ENOENT)
172 return NULL;
e702496e 173 hashclr(sha1);
ed378ec7 174 return ref;
a876ed83 175 }
ca8db142 176
a876ed83
JH
177 /* Follow "normalized" - ie "refs/.." symlinks by hand */
178 if (S_ISLNK(st.st_mode)) {
179 len = readlink(path, buffer, sizeof(buffer)-1);
180 if (len >= 5 && !memcmp("refs/", buffer, 5)) {
ed378ec7
LT
181 buffer[len] = 0;
182 strcpy(ref_buffer, buffer);
183 ref = ref_buffer;
a876ed83
JH
184 continue;
185 }
ca8db142 186 }
a876ed83
JH
187
188 /*
189 * Anything else, just open it and try to use it as
190 * a ref
191 */
192 fd = open(path, O_RDONLY);
193 if (fd < 0)
194 return NULL;
195 len = read(fd, buffer, sizeof(buffer)-1);
196 close(fd);
197
198 /*
199 * Is it a symbolic ref?
200 */
201 if (len < 4 || memcmp("ref:", buffer, 4))
202 break;
203 buf = buffer + 4;
204 len -= 4;
205 while (len && isspace(*buf))
206 buf++, len--;
207 while (len && isspace(buf[len-1]))
ed378ec7
LT
208 len--;
209 buf[len] = 0;
210 memcpy(ref_buffer, buf, len + 1);
211 ref = ref_buffer;
8a65ff76 212 }
a876ed83
JH
213 if (len < 40 || get_sha1_hex(buffer, sha1))
214 return NULL;
ed378ec7 215 return ref;
a876ed83
JH
216}
217
ed378ec7 218int create_symref(const char *ref_target, const char *refs_heads_master)
8098a178 219{
8098a178
JH
220 const char *lockpath;
221 char ref[1000];
222 int fd, len, written;
ed378ec7 223 const char *git_HEAD = git_path("%s", ref_target);
8098a178 224
9f0bb90d
JH
225#ifndef NO_SYMLINK_HEAD
226 if (prefer_symlink_refs) {
f8348be3
JS
227 unlink(git_HEAD);
228 if (!symlink(refs_heads_master, git_HEAD))
229 return 0;
230 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
231 }
303958dc
JS
232#endif
233
8098a178
JH
234 len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
235 if (sizeof(ref) <= len) {
236 error("refname too long: %s", refs_heads_master);
237 return -1;
238 }
239 lockpath = mkpath("%s.lock", git_HEAD);
240 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
241 written = write(fd, ref, len);
242 close(fd);
243 if (written != len) {
244 unlink(lockpath);
245 error("Unable to write to %s", lockpath);
246 return -2;
247 }
248 if (rename(lockpath, git_HEAD) < 0) {
249 unlink(lockpath);
250 error("Unable to create %s", git_HEAD);
251 return -3;
252 }
138086a7
JH
253 if (adjust_shared_perm(git_HEAD)) {
254 unlink(lockpath);
255 error("Unable to fix permissions on %s", lockpath);
256 return -4;
257 }
8098a178 258 return 0;
8098a178
JH
259}
260
ed378ec7 261int read_ref(const char *ref, unsigned char *sha1)
a876ed83 262{
ed378ec7 263 if (resolve_ref(ref, sha1, 1))
a876ed83
JH
264 return 0;
265 return -1;
8a65ff76
LT
266}
267
a62be77f 268static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
8a65ff76 269{
e1e22e37
LT
270 int retval;
271 struct ref_list *packed = get_packed_refs();
272 struct ref_list *loose = get_loose_refs();
8a65ff76 273
e1e22e37
LT
274 while (packed && loose) {
275 struct ref_list *entry;
276 int cmp = strcmp(packed->name, loose->name);
277 if (!cmp) {
278 packed = packed->next;
279 continue;
6cada6a9 280 }
e1e22e37
LT
281 if (cmp > 0) {
282 entry = loose;
283 loose = loose->next;
284 } else {
285 entry = packed;
286 packed = packed->next;
287 }
288 if (strncmp(base, entry->name, trim))
289 continue;
b37a562a
LT
290 if (is_null_sha1(entry->sha1))
291 continue;
292 if (!has_sha1_file(entry->sha1)) {
293 error("%s does not point to a valid object!", entry->name);
294 continue;
295 }
e1e22e37
LT
296 retval = fn(entry->name + trim, entry->sha1);
297 if (retval)
298 return retval;
299 }
8a65ff76 300
e1e22e37
LT
301 packed = packed ? packed : loose;
302 while (packed) {
303 if (!strncmp(base, packed->name, trim)) {
304 retval = fn(packed->name + trim, packed->sha1);
8a65ff76 305 if (retval)
e1e22e37 306 return retval;
8a65ff76 307 }
e1e22e37 308 packed = packed->next;
8a65ff76 309 }
e1e22e37 310 return 0;
8a65ff76
LT
311}
312
723c31fe
LT
313int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
314{
315 unsigned char sha1[20];
ed378ec7 316 if (!read_ref("HEAD", sha1))
99a0a6e0 317 return fn("HEAD", sha1);
2f34ba32 318 return 0;
723c31fe
LT
319}
320
944d8589 321int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
8a65ff76 322{
e1e22e37 323 return do_for_each_ref("refs/", fn, 0);
a62be77f
SE
324}
325
326int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
327{
e1e22e37 328 return do_for_each_ref("refs/tags/", fn, 10);
a62be77f
SE
329}
330
331int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
332{
e1e22e37 333 return do_for_each_ref("refs/heads/", fn, 11);
a62be77f
SE
334}
335
336int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
337{
e1e22e37 338 return do_for_each_ref("refs/remotes/", fn, 13);
8a65ff76
LT
339}
340
95fc7512
DB
341int get_ref_sha1(const char *ref, unsigned char *sha1)
342{
95fc7512
DB
343 if (check_ref_format(ref))
344 return -1;
ed378ec7 345 return read_ref(mkpath("refs/%s", ref), sha1);
95fc7512
DB
346}
347
03feddd6
JH
348/*
349 * Make sure "ref" is something reasonable to have under ".git/refs/";
350 * We do not like it if:
351 *
352 * - any path component of it begins with ".", or
353 * - it has double dots "..", or
354 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
355 * - it ends with a "/".
356 */
357
358static inline int bad_ref_char(int ch)
359{
360 return (((unsigned) ch) <= ' ' ||
68283999
JH
361 ch == '~' || ch == '^' || ch == ':' ||
362 /* 2.13 Pattern Matching Notation */
363 ch == '?' || ch == '*' || ch == '[');
03feddd6
JH
364}
365
95fc7512
DB
366int check_ref_format(const char *ref)
367{
03feddd6
JH
368 int ch, level;
369 const char *cp = ref;
370
371 level = 0;
372 while (1) {
373 while ((ch = *cp++) == '/')
374 ; /* tolerate duplicated slashes */
375 if (!ch)
376 return -1; /* should not end with slashes */
377
378 /* we are at the beginning of the path component */
379 if (ch == '.' || bad_ref_char(ch))
380 return -1;
381
382 /* scan the rest of the path component */
383 while ((ch = *cp++) != 0) {
384 if (bad_ref_char(ch))
385 return -1;
386 if (ch == '/')
387 break;
388 if (ch == '.' && *cp == '.')
389 return -1;
390 }
391 level++;
392 if (!ch) {
393 if (level < 2)
394 return -1; /* at least of form "heads/blah" */
395 return 0;
396 }
397 }
95fc7512
DB
398}
399
e5f38ec3 400static struct ref_lock *verify_lock(struct ref_lock *lock,
4bd18c43
SP
401 const unsigned char *old_sha1, int mustexist)
402{
403 char buf[40];
404 int nr, fd = open(lock->ref_file, O_RDONLY);
405 if (fd < 0 && (mustexist || errno != ENOENT)) {
406 error("Can't verify ref %s", lock->ref_file);
407 unlock_ref(lock);
408 return NULL;
409 }
410 nr = read(fd, buf, 40);
411 close(fd);
412 if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) {
413 error("Can't verify ref %s", lock->ref_file);
414 unlock_ref(lock);
415 return NULL;
416 }
a89fccd2 417 if (hashcmp(lock->old_sha1, old_sha1)) {
4bd18c43
SP
418 error("Ref %s is at %s but expected %s", lock->ref_file,
419 sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
420 unlock_ref(lock);
421 return NULL;
422 }
423 return lock;
424}
425
ed378ec7 426static struct ref_lock *lock_ref_sha1_basic(const char *ref,
4bd18c43
SP
427 int plen,
428 const unsigned char *old_sha1, int mustexist)
429{
ed378ec7 430 const char *orig_ref = ref;
4bd18c43 431 struct ref_lock *lock;
732232a1 432 struct stat st;
4bd18c43
SP
433
434 lock = xcalloc(1, sizeof(struct ref_lock));
435 lock->lock_fd = -1;
436
ed378ec7
LT
437 ref = resolve_ref(ref, lock->old_sha1, mustexist);
438 if (!ref) {
818f477c
SP
439 int last_errno = errno;
440 error("unable to resolve reference %s: %s",
ed378ec7 441 orig_ref, strerror(errno));
4bd18c43 442 unlock_ref(lock);
818f477c 443 errno = last_errno;
4bd18c43
SP
444 return NULL;
445 }
c33d5174 446 lock->lk = xcalloc(1, sizeof(struct lock_file));
4bd18c43 447
ed378ec7
LT
448 lock->ref_file = xstrdup(git_path("%s", ref));
449 lock->log_file = xstrdup(git_path("logs/%s", ref));
8fe92775 450 lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
4bd18c43 451
c33d5174
JH
452 if (safe_create_leading_directories(lock->ref_file))
453 die("unable to create directory for %s", lock->ref_file);
40aaae88 454 lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file, 1);
4bd18c43
SP
455
456 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
457}
458
e5f38ec3 459struct ref_lock *lock_ref_sha1(const char *ref,
4bd18c43 460 const unsigned char *old_sha1, int mustexist)
95fc7512 461{
95fc7512 462 if (check_ref_format(ref))
4bd18c43 463 return NULL;
ed378ec7 464 return lock_ref_sha1_basic(mkpath("refs/%s", ref),
d0740d92 465 5 + strlen(ref), old_sha1, mustexist);
4bd18c43
SP
466}
467
e5f38ec3 468struct ref_lock *lock_any_ref_for_update(const char *ref,
4bd18c43
SP
469 const unsigned char *old_sha1, int mustexist)
470{
ed378ec7 471 return lock_ref_sha1_basic(ref, strlen(ref), old_sha1, mustexist);
4bd18c43
SP
472}
473
e5f38ec3 474void unlock_ref(struct ref_lock *lock)
4bd18c43
SP
475{
476 if (lock->lock_fd >= 0) {
477 close(lock->lock_fd);
c33d5174
JH
478 /* Do not free lock->lk -- atexit() still looks at them */
479 if (lock->lk)
480 rollback_lock_file(lock->lk);
4bd18c43 481 }
4cac42b1
JH
482 free(lock->ref_file);
483 free(lock->log_file);
4bd18c43
SP
484 free(lock);
485}
486
6de08ae6
SP
487static int log_ref_write(struct ref_lock *lock,
488 const unsigned char *sha1, const char *msg)
489{
490 int logfd, written, oflags = O_APPEND | O_WRONLY;
491 unsigned maxlen, len;
492 char *logrec;
ff4c8485 493 const char *committer;
6de08ae6
SP
494
495 if (log_all_ref_updates) {
496 if (safe_create_leading_directories(lock->log_file) < 0)
497 return error("unable to create directory for %s",
498 lock->log_file);
499 oflags |= O_CREAT;
500 }
501
502 logfd = open(lock->log_file, oflags, 0666);
503 if (logfd < 0) {
504 if (!log_all_ref_updates && errno == ENOENT)
505 return 0;
506 return error("Unable to append to %s: %s",
507 lock->log_file, strerror(errno));
508 }
509
ff4c8485 510 committer = git_committer_info(1);
6de08ae6 511 if (msg) {
ff4c8485 512 maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
6de08ae6
SP
513 logrec = xmalloc(maxlen);
514 len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
515 sha1_to_hex(lock->old_sha1),
516 sha1_to_hex(sha1),
ff4c8485 517 committer,
6de08ae6 518 msg);
e5f38ec3
JH
519 }
520 else {
ff4c8485 521 maxlen = strlen(committer) + 2*40 + 4;
6de08ae6
SP
522 logrec = xmalloc(maxlen);
523 len = snprintf(logrec, maxlen, "%s %s %s\n",
524 sha1_to_hex(lock->old_sha1),
525 sha1_to_hex(sha1),
ff4c8485 526 committer);
6de08ae6
SP
527 }
528 written = len <= maxlen ? write(logfd, logrec, len) : -1;
529 free(logrec);
530 close(logfd);
531 if (written != len)
532 return error("Unable to append to %s", lock->log_file);
533 return 0;
534}
535
4bd18c43
SP
536int write_ref_sha1(struct ref_lock *lock,
537 const unsigned char *sha1, const char *logmsg)
538{
539 static char term = '\n';
540
541 if (!lock)
95fc7512 542 return -1;
a89fccd2 543 if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
4bd18c43
SP
544 unlock_ref(lock);
545 return 0;
95fc7512 546 }
4bd18c43
SP
547 if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
548 write(lock->lock_fd, &term, 1) != 1
549 || close(lock->lock_fd) < 0) {
c33d5174 550 error("Couldn't write %s", lock->lk->filename);
4bd18c43
SP
551 unlock_ref(lock);
552 return -1;
553 }
6de08ae6
SP
554 if (log_ref_write(lock, sha1, logmsg) < 0) {
555 unlock_ref(lock);
556 return -1;
557 }
c33d5174 558 if (commit_lock_file(lock->lk)) {
4bd18c43
SP
559 error("Couldn't set %s", lock->ref_file);
560 unlock_ref(lock);
561 return -1;
562 }
563 lock->lock_fd = -1;
564 unlock_ref(lock);
565 return 0;
95fc7512 566}
d556fae2
SP
567
568int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
569{
e5229042 570 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
d556fae2
SP
571 char *tz_c;
572 int logfd, tz;
573 struct stat st;
574 unsigned long date;
e5229042 575 unsigned char logged_sha1[20];
d556fae2
SP
576
577 logfile = git_path("logs/%s", ref);
578 logfd = open(logfile, O_RDONLY, 0);
579 if (logfd < 0)
580 die("Unable to read log %s: %s", logfile, strerror(errno));
581 fstat(logfd, &st);
582 if (!st.st_size)
583 die("Log %s is empty.", logfile);
584 logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
585 close(logfd);
586
e5229042 587 lastrec = NULL;
d556fae2
SP
588 rec = logend = logdata + st.st_size;
589 while (logdata < rec) {
590 if (logdata < rec && *(rec-1) == '\n')
591 rec--;
e5229042
SP
592 lastgt = NULL;
593 while (logdata < rec && *(rec-1) != '\n') {
d556fae2 594 rec--;
e5229042
SP
595 if (*rec == '>')
596 lastgt = rec;
597 }
598 if (!lastgt)
d556fae2 599 die("Log %s is corrupt.", logfile);
e5229042 600 date = strtoul(lastgt + 1, &tz_c, 10);
d556fae2 601 if (date <= at_time) {
e5229042
SP
602 if (lastrec) {
603 if (get_sha1_hex(lastrec, logged_sha1))
604 die("Log %s is corrupt.", logfile);
605 if (get_sha1_hex(rec + 41, sha1))
606 die("Log %s is corrupt.", logfile);
a89fccd2 607 if (hashcmp(logged_sha1, sha1)) {
e5229042
SP
608 tz = strtoul(tz_c, NULL, 10);
609 fprintf(stderr,
610 "warning: Log %s has gap after %s.\n",
611 logfile, show_rfc2822_date(date, tz));
612 }
e5f38ec3
JH
613 }
614 else if (date == at_time) {
e5229042
SP
615 if (get_sha1_hex(rec + 41, sha1))
616 die("Log %s is corrupt.", logfile);
e5f38ec3
JH
617 }
618 else {
e5229042
SP
619 if (get_sha1_hex(rec + 41, logged_sha1))
620 die("Log %s is corrupt.", logfile);
a89fccd2 621 if (hashcmp(logged_sha1, sha1)) {
e5229042
SP
622 tz = strtoul(tz_c, NULL, 10);
623 fprintf(stderr,
624 "warning: Log %s unexpectedly ended on %s.\n",
625 logfile, show_rfc2822_date(date, tz));
626 }
627 }
d556fae2
SP
628 munmap((void*)logdata, st.st_size);
629 return 0;
630 }
e5229042 631 lastrec = rec;
d556fae2
SP
632 }
633
e5229042
SP
634 rec = logdata;
635 while (rec < logend && *rec != '>' && *rec != '\n')
636 rec++;
637 if (rec == logend || *rec == '\n')
d556fae2 638 die("Log %s is corrupt.", logfile);
e5229042 639 date = strtoul(rec + 1, &tz_c, 10);
d556fae2
SP
640 tz = strtoul(tz_c, NULL, 10);
641 if (get_sha1_hex(logdata, sha1))
642 die("Log %s is corrupt.", logfile);
643 munmap((void*)logdata, st.st_size);
644 fprintf(stderr, "warning: Log %s only goes back to %s.\n",
645 logfile, show_rfc2822_date(date, tz));
646 return 0;
647}