refs: minor restructuring of cached refs data.
[git/dscho.git] / refs.c
blob6ee5f96943d59243871c4158c06795e978bb8d8d
1 #include "refs.h"
2 #include "cache.h"
4 #include <errno.h>
6 struct ref_list {
7 struct ref_list *next;
8 unsigned char flag; /* ISSYMREF? ISPACKED? */
9 unsigned char sha1[20];
10 char name[FLEX_ARRAY];
13 static const char *parse_ref_line(char *line, unsigned char *sha1)
16 * 42: the answer to everything.
18 * In this case, it happens to be the answer to
19 * 40 (length of sha1 hex representation)
20 * +1 (space in between hex and name)
21 * +1 (newline at the end of the line)
23 int len = strlen(line) - 42;
25 if (len <= 0)
26 return NULL;
27 if (get_sha1_hex(line, sha1) < 0)
28 return NULL;
29 if (!isspace(line[40]))
30 return NULL;
31 line += 41;
32 if (isspace(*line))
33 return NULL;
34 if (line[len] != '\n')
35 return NULL;
36 line[len] = 0;
37 return line;
40 static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
41 int flag, struct ref_list *list)
43 int len;
44 struct ref_list **p = &list, *entry;
46 /* Find the place to insert the ref into.. */
47 while ((entry = *p) != NULL) {
48 int cmp = strcmp(entry->name, name);
49 if (cmp > 0)
50 break;
52 /* Same as existing entry? */
53 if (!cmp)
54 return list;
55 p = &entry->next;
58 /* Allocate it and add it in.. */
59 len = strlen(name) + 1;
60 entry = xmalloc(sizeof(struct ref_list) + len);
61 hashcpy(entry->sha1, sha1);
62 memcpy(entry->name, name, len);
63 entry->flag = flag;
64 entry->next = *p;
65 *p = entry;
66 return list;
70 * Future: need to be in "struct repository"
71 * when doing a full libification.
73 struct cached_refs {
74 char did_loose;
75 char did_packed;
76 struct ref_list *loose;
77 struct ref_list *packed;
78 } cached_refs;
80 static void free_ref_list(struct ref_list *list)
82 struct ref_list *next;
83 for ( ; list; list = next) {
84 next = list->next;
85 free(list);
89 static void invalidate_cached_refs(void)
91 struct cached_refs *ca = &cached_refs;
93 if (ca->did_loose && ca->loose)
94 free_ref_list(ca->loose);
95 if (ca->did_packed && ca->packed)
96 free_ref_list(ca->packed);
97 ca->loose = ca->packed = NULL;
98 ca->did_loose = ca->did_packed = 0;
101 static struct ref_list *get_packed_refs(void)
103 if (!cached_refs.did_packed) {
104 struct ref_list *refs = NULL;
105 FILE *f = fopen(git_path("packed-refs"), "r");
106 if (f) {
107 struct ref_list *list = NULL;
108 char refline[PATH_MAX];
109 while (fgets(refline, sizeof(refline), f)) {
110 unsigned char sha1[20];
111 const char *name = parse_ref_line(refline, sha1);
112 if (!name)
113 continue;
114 list = add_ref(name, sha1, REF_ISPACKED, list);
116 fclose(f);
117 refs = list;
119 cached_refs.packed = refs;
120 cached_refs.did_packed = 1;
122 return cached_refs.packed;
125 static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
127 DIR *dir = opendir(git_path("%s", base));
129 if (dir) {
130 struct dirent *de;
131 int baselen = strlen(base);
132 char *ref = xmalloc(baselen + 257);
134 memcpy(ref, base, baselen);
135 if (baselen && base[baselen-1] != '/')
136 ref[baselen++] = '/';
138 while ((de = readdir(dir)) != NULL) {
139 unsigned char sha1[20];
140 struct stat st;
141 int flag;
142 int namelen;
144 if (de->d_name[0] == '.')
145 continue;
146 namelen = strlen(de->d_name);
147 if (namelen > 255)
148 continue;
149 if (has_extension(de->d_name, ".lock"))
150 continue;
151 memcpy(ref + baselen, de->d_name, namelen+1);
152 if (stat(git_path("%s", ref), &st) < 0)
153 continue;
154 if (S_ISDIR(st.st_mode)) {
155 list = get_ref_dir(ref, list);
156 continue;
158 if (!resolve_ref(ref, sha1, 1, &flag)) {
159 error("%s points nowhere!", ref);
160 continue;
162 list = add_ref(ref, sha1, flag, list);
164 free(ref);
165 closedir(dir);
167 return list;
170 static struct ref_list *get_loose_refs(void)
172 if (!cached_refs.did_loose) {
173 cached_refs.loose = get_ref_dir("refs", NULL);
174 cached_refs.did_loose = 1;
176 return cached_refs.loose;
179 /* We allow "recursive" symbolic refs. Only within reason, though */
180 #define MAXDEPTH 5
182 const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
184 int depth = MAXDEPTH, len;
185 char buffer[256];
186 static char ref_buffer[256];
188 if (flag)
189 *flag = 0;
191 for (;;) {
192 const char *path = git_path("%s", ref);
193 struct stat st;
194 char *buf;
195 int fd;
197 if (--depth < 0)
198 return NULL;
200 /* Special case: non-existing file.
201 * Not having the refs/heads/new-branch is OK
202 * if we are writing into it, so is .git/HEAD
203 * that points at refs/heads/master still to be
204 * born. It is NOT OK if we are resolving for
205 * reading.
207 if (lstat(path, &st) < 0) {
208 struct ref_list *list = get_packed_refs();
209 while (list) {
210 if (!strcmp(ref, list->name)) {
211 hashcpy(sha1, list->sha1);
212 if (flag)
213 *flag |= REF_ISPACKED;
214 return ref;
216 list = list->next;
218 if (reading || errno != ENOENT)
219 return NULL;
220 hashclr(sha1);
221 return ref;
224 /* Follow "normalized" - ie "refs/.." symlinks by hand */
225 if (S_ISLNK(st.st_mode)) {
226 len = readlink(path, buffer, sizeof(buffer)-1);
227 if (len >= 5 && !memcmp("refs/", buffer, 5)) {
228 buffer[len] = 0;
229 strcpy(ref_buffer, buffer);
230 ref = ref_buffer;
231 if (flag)
232 *flag |= REF_ISSYMREF;
233 continue;
238 * Anything else, just open it and try to use it as
239 * a ref
241 fd = open(path, O_RDONLY);
242 if (fd < 0)
243 return NULL;
244 len = read(fd, buffer, sizeof(buffer)-1);
245 close(fd);
248 * Is it a symbolic ref?
250 if (len < 4 || memcmp("ref:", buffer, 4))
251 break;
252 buf = buffer + 4;
253 len -= 4;
254 while (len && isspace(*buf))
255 buf++, len--;
256 while (len && isspace(buf[len-1]))
257 len--;
258 buf[len] = 0;
259 memcpy(ref_buffer, buf, len + 1);
260 ref = ref_buffer;
261 if (flag)
262 *flag |= REF_ISSYMREF;
264 if (len < 40 || get_sha1_hex(buffer, sha1))
265 return NULL;
266 return ref;
269 int create_symref(const char *ref_target, const char *refs_heads_master)
271 const char *lockpath;
272 char ref[1000];
273 int fd, len, written;
274 const char *git_HEAD = git_path("%s", ref_target);
276 #ifndef NO_SYMLINK_HEAD
277 if (prefer_symlink_refs) {
278 unlink(git_HEAD);
279 if (!symlink(refs_heads_master, git_HEAD))
280 return 0;
281 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
283 #endif
285 len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
286 if (sizeof(ref) <= len) {
287 error("refname too long: %s", refs_heads_master);
288 return -1;
290 lockpath = mkpath("%s.lock", git_HEAD);
291 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
292 written = write(fd, ref, len);
293 close(fd);
294 if (written != len) {
295 unlink(lockpath);
296 error("Unable to write to %s", lockpath);
297 return -2;
299 if (rename(lockpath, git_HEAD) < 0) {
300 unlink(lockpath);
301 error("Unable to create %s", git_HEAD);
302 return -3;
304 if (adjust_shared_perm(git_HEAD)) {
305 unlink(lockpath);
306 error("Unable to fix permissions on %s", lockpath);
307 return -4;
309 return 0;
312 int read_ref(const char *ref, unsigned char *sha1)
314 if (resolve_ref(ref, sha1, 1, NULL))
315 return 0;
316 return -1;
319 static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
320 void *cb_data)
322 int retval;
323 struct ref_list *packed = get_packed_refs();
324 struct ref_list *loose = get_loose_refs();
326 while (packed && loose) {
327 struct ref_list *entry;
328 int cmp = strcmp(packed->name, loose->name);
329 if (!cmp) {
330 packed = packed->next;
331 continue;
333 if (cmp > 0) {
334 entry = loose;
335 loose = loose->next;
336 } else {
337 entry = packed;
338 packed = packed->next;
340 if (strncmp(base, entry->name, trim))
341 continue;
342 if (is_null_sha1(entry->sha1))
343 continue;
344 if (!has_sha1_file(entry->sha1)) {
345 error("%s does not point to a valid object!", entry->name);
346 continue;
348 retval = fn(entry->name + trim, entry->sha1,
349 entry->flag, cb_data);
350 if (retval)
351 return retval;
354 packed = packed ? packed : loose;
355 while (packed) {
356 if (!strncmp(base, packed->name, trim)) {
357 retval = fn(packed->name + trim, packed->sha1,
358 packed->flag, cb_data);
359 if (retval)
360 return retval;
362 packed = packed->next;
364 return 0;
367 int head_ref(each_ref_fn fn, void *cb_data)
369 unsigned char sha1[20];
370 int flag;
372 if (resolve_ref("HEAD", sha1, 1, &flag))
373 return fn("HEAD", sha1, flag, cb_data);
374 return 0;
377 int for_each_ref(each_ref_fn fn, void *cb_data)
379 return do_for_each_ref("refs/", fn, 0, cb_data);
382 int for_each_tag_ref(each_ref_fn fn, void *cb_data)
384 return do_for_each_ref("refs/tags/", fn, 10, cb_data);
387 int for_each_branch_ref(each_ref_fn fn, void *cb_data)
389 return do_for_each_ref("refs/heads/", fn, 11, cb_data);
392 int for_each_remote_ref(each_ref_fn fn, void *cb_data)
394 return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
397 /* NEEDSWORK: This is only used by ssh-upload and it should go; the
398 * caller should do resolve_ref or read_ref like everybody else. Or
399 * maybe everybody else should use get_ref_sha1() instead of doing
400 * read_ref().
402 int get_ref_sha1(const char *ref, unsigned char *sha1)
404 if (check_ref_format(ref))
405 return -1;
406 return read_ref(mkpath("refs/%s", ref), sha1);
409 int delete_ref(const char *refname, unsigned char *sha1)
411 struct ref_lock *lock;
412 int err, i, ret = 0;
414 lock = lock_any_ref_for_update(refname, sha1);
415 if (!lock)
416 return 1;
417 i = strlen(lock->lk->filename) - 5; /* .lock */
418 lock->lk->filename[i] = 0;
419 err = unlink(lock->lk->filename);
420 if (err) {
421 ret = 1;
422 error("unlink(%s) failed: %s",
423 lock->lk->filename, strerror(errno));
425 lock->lk->filename[i] = '.';
427 err = unlink(lock->log_file);
428 if (err && errno != ENOENT)
429 fprintf(stderr, "warning: unlink(%s) failed: %s",
430 lock->log_file, strerror(errno));
432 invalidate_cached_refs();
433 return ret;
437 * Make sure "ref" is something reasonable to have under ".git/refs/";
438 * We do not like it if:
440 * - any path component of it begins with ".", or
441 * - it has double dots "..", or
442 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
443 * - it ends with a "/".
446 static inline int bad_ref_char(int ch)
448 return (((unsigned) ch) <= ' ' ||
449 ch == '~' || ch == '^' || ch == ':' ||
450 /* 2.13 Pattern Matching Notation */
451 ch == '?' || ch == '*' || ch == '[');
454 int check_ref_format(const char *ref)
456 int ch, level;
457 const char *cp = ref;
459 level = 0;
460 while (1) {
461 while ((ch = *cp++) == '/')
462 ; /* tolerate duplicated slashes */
463 if (!ch)
464 return -1; /* should not end with slashes */
466 /* we are at the beginning of the path component */
467 if (ch == '.' || bad_ref_char(ch))
468 return -1;
470 /* scan the rest of the path component */
471 while ((ch = *cp++) != 0) {
472 if (bad_ref_char(ch))
473 return -1;
474 if (ch == '/')
475 break;
476 if (ch == '.' && *cp == '.')
477 return -1;
479 level++;
480 if (!ch) {
481 if (level < 2)
482 return -1; /* at least of form "heads/blah" */
483 return 0;
488 static struct ref_lock *verify_lock(struct ref_lock *lock,
489 const unsigned char *old_sha1, int mustexist)
491 if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
492 error("Can't verify ref %s", lock->ref_name);
493 unlock_ref(lock);
494 return NULL;
496 if (hashcmp(lock->old_sha1, old_sha1)) {
497 error("Ref %s is at %s but expected %s", lock->ref_name,
498 sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
499 unlock_ref(lock);
500 return NULL;
502 return lock;
505 static int remove_empty_dir_recursive(char *path, int len)
507 DIR *dir = opendir(path);
508 struct dirent *e;
509 int ret = 0;
511 if (!dir)
512 return -1;
513 if (path[len-1] != '/')
514 path[len++] = '/';
515 while ((e = readdir(dir)) != NULL) {
516 struct stat st;
517 int namlen;
518 if ((e->d_name[0] == '.') &&
519 ((e->d_name[1] == 0) ||
520 ((e->d_name[1] == '.') && e->d_name[2] == 0)))
521 continue; /* "." and ".." */
523 namlen = strlen(e->d_name);
524 if ((len + namlen < PATH_MAX) &&
525 strcpy(path + len, e->d_name) &&
526 !lstat(path, &st) &&
527 S_ISDIR(st.st_mode) &&
528 remove_empty_dir_recursive(path, len + namlen))
529 continue; /* happy */
531 /* path too long, stat fails, or non-directory still exists */
532 ret = -1;
533 break;
535 closedir(dir);
536 if (!ret) {
537 path[len] = 0;
538 ret = rmdir(path);
540 return ret;
543 static int remove_empty_directories(char *file)
545 /* we want to create a file but there is a directory there;
546 * if that is an empty directory (or a directory that contains
547 * only empty directories), remove them.
549 char path[PATH_MAX];
550 int len = strlen(file);
552 if (len >= PATH_MAX) /* path too long ;-) */
553 return -1;
554 strcpy(path, file);
555 return remove_empty_dir_recursive(path, len);
558 static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1)
560 char *ref_file;
561 const char *orig_ref = ref;
562 struct ref_lock *lock;
563 struct stat st;
564 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
566 lock = xcalloc(1, sizeof(struct ref_lock));
567 lock->lock_fd = -1;
569 ref = resolve_ref(ref, lock->old_sha1, mustexist, NULL);
570 if (!ref && errno == EISDIR) {
571 /* we are trying to lock foo but we used to
572 * have foo/bar which now does not exist;
573 * it is normal for the empty directory 'foo'
574 * to remain.
576 ref_file = git_path("%s", orig_ref);
577 if (remove_empty_directories(ref_file))
578 die("there are still refs under '%s'", orig_ref);
579 ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, NULL);
581 if (!ref) {
582 int last_errno = errno;
583 error("unable to resolve reference %s: %s",
584 orig_ref, strerror(errno));
585 unlock_ref(lock);
586 errno = last_errno;
587 return NULL;
589 lock->lk = xcalloc(1, sizeof(struct lock_file));
591 lock->ref_name = xstrdup(ref);
592 lock->log_file = xstrdup(git_path("logs/%s", ref));
593 ref_file = git_path("%s", ref);
594 lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
596 if (safe_create_leading_directories(ref_file))
597 die("unable to create directory for %s", ref_file);
598 lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
600 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
603 struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
605 char refpath[PATH_MAX];
606 if (check_ref_format(ref))
607 return NULL;
608 strcpy(refpath, mkpath("refs/%s", ref));
609 return lock_ref_sha1_basic(refpath, old_sha1);
612 struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1)
614 return lock_ref_sha1_basic(ref, old_sha1);
617 void unlock_ref(struct ref_lock *lock)
619 if (lock->lock_fd >= 0) {
620 close(lock->lock_fd);
621 /* Do not free lock->lk -- atexit() still looks at them */
622 if (lock->lk)
623 rollback_lock_file(lock->lk);
625 free(lock->ref_name);
626 free(lock->log_file);
627 free(lock);
630 static int log_ref_write(struct ref_lock *lock,
631 const unsigned char *sha1, const char *msg)
633 int logfd, written, oflags = O_APPEND | O_WRONLY;
634 unsigned maxlen, len;
635 char *logrec;
636 const char *committer;
638 if (log_all_ref_updates) {
639 if (safe_create_leading_directories(lock->log_file) < 0)
640 return error("unable to create directory for %s",
641 lock->log_file);
642 oflags |= O_CREAT;
645 logfd = open(lock->log_file, oflags, 0666);
646 if (logfd < 0) {
647 if (!log_all_ref_updates && errno == ENOENT)
648 return 0;
649 return error("Unable to append to %s: %s",
650 lock->log_file, strerror(errno));
653 committer = git_committer_info(1);
654 if (msg) {
655 maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
656 logrec = xmalloc(maxlen);
657 len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
658 sha1_to_hex(lock->old_sha1),
659 sha1_to_hex(sha1),
660 committer,
661 msg);
663 else {
664 maxlen = strlen(committer) + 2*40 + 4;
665 logrec = xmalloc(maxlen);
666 len = snprintf(logrec, maxlen, "%s %s %s\n",
667 sha1_to_hex(lock->old_sha1),
668 sha1_to_hex(sha1),
669 committer);
671 written = len <= maxlen ? write(logfd, logrec, len) : -1;
672 free(logrec);
673 close(logfd);
674 if (written != len)
675 return error("Unable to append to %s", lock->log_file);
676 return 0;
679 int write_ref_sha1(struct ref_lock *lock,
680 const unsigned char *sha1, const char *logmsg)
682 static char term = '\n';
684 if (!lock)
685 return -1;
686 if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
687 unlock_ref(lock);
688 return 0;
690 if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
691 write(lock->lock_fd, &term, 1) != 1
692 || close(lock->lock_fd) < 0) {
693 error("Couldn't write %s", lock->lk->filename);
694 unlock_ref(lock);
695 return -1;
697 invalidate_cached_refs();
698 if (log_ref_write(lock, sha1, logmsg) < 0) {
699 unlock_ref(lock);
700 return -1;
702 if (commit_lock_file(lock->lk)) {
703 error("Couldn't set %s", lock->ref_name);
704 unlock_ref(lock);
705 return -1;
707 lock->lock_fd = -1;
708 unlock_ref(lock);
709 return 0;
712 int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
714 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
715 char *tz_c;
716 int logfd, tz;
717 struct stat st;
718 unsigned long date;
719 unsigned char logged_sha1[20];
721 logfile = git_path("logs/%s", ref);
722 logfd = open(logfile, O_RDONLY, 0);
723 if (logfd < 0)
724 die("Unable to read log %s: %s", logfile, strerror(errno));
725 fstat(logfd, &st);
726 if (!st.st_size)
727 die("Log %s is empty.", logfile);
728 logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
729 close(logfd);
731 lastrec = NULL;
732 rec = logend = logdata + st.st_size;
733 while (logdata < rec) {
734 if (logdata < rec && *(rec-1) == '\n')
735 rec--;
736 lastgt = NULL;
737 while (logdata < rec && *(rec-1) != '\n') {
738 rec--;
739 if (*rec == '>')
740 lastgt = rec;
742 if (!lastgt)
743 die("Log %s is corrupt.", logfile);
744 date = strtoul(lastgt + 1, &tz_c, 10);
745 if (date <= at_time) {
746 if (lastrec) {
747 if (get_sha1_hex(lastrec, logged_sha1))
748 die("Log %s is corrupt.", logfile);
749 if (get_sha1_hex(rec + 41, sha1))
750 die("Log %s is corrupt.", logfile);
751 if (hashcmp(logged_sha1, sha1)) {
752 tz = strtoul(tz_c, NULL, 10);
753 fprintf(stderr,
754 "warning: Log %s has gap after %s.\n",
755 logfile, show_rfc2822_date(date, tz));
758 else if (date == at_time) {
759 if (get_sha1_hex(rec + 41, sha1))
760 die("Log %s is corrupt.", logfile);
762 else {
763 if (get_sha1_hex(rec + 41, logged_sha1))
764 die("Log %s is corrupt.", logfile);
765 if (hashcmp(logged_sha1, sha1)) {
766 tz = strtoul(tz_c, NULL, 10);
767 fprintf(stderr,
768 "warning: Log %s unexpectedly ended on %s.\n",
769 logfile, show_rfc2822_date(date, tz));
772 munmap((void*)logdata, st.st_size);
773 return 0;
775 lastrec = rec;
778 rec = logdata;
779 while (rec < logend && *rec != '>' && *rec != '\n')
780 rec++;
781 if (rec == logend || *rec == '\n')
782 die("Log %s is corrupt.", logfile);
783 date = strtoul(rec + 1, &tz_c, 10);
784 tz = strtoul(tz_c, NULL, 10);
785 if (get_sha1_hex(logdata, sha1))
786 die("Log %s is corrupt.", logfile);
787 munmap((void*)logdata, st.st_size);
788 fprintf(stderr, "warning: Log %s only goes back to %s.\n",
789 logfile, show_rfc2822_date(date, tz));
790 return 0;