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