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;
27 if (get_sha1_hex(line
, sha1
) < 0)
29 if (!isspace(line
[40]))
34 if (line
[len
] != '\n')
40 static struct ref_list
*add_ref(const char *name
, const unsigned char *sha1
,
41 int flag
, struct ref_list
*list
)
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
);
52 /* Same as existing entry? */
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
);
70 * Future: need to be in "struct repository"
71 * when doing a full libification.
76 struct ref_list
*loose
;
77 struct ref_list
*packed
;
80 static void free_ref_list(struct ref_list
*list
)
82 struct ref_list
*next
;
83 for ( ; list
; list
= next
) {
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");
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
);
114 list
= add_ref(name
, sha1
, REF_ISPACKED
, 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
));
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];
144 if (de
->d_name
[0] == '.')
146 namelen
= strlen(de
->d_name
);
149 if (has_extension(de
->d_name
, ".lock"))
151 memcpy(ref
+ baselen
, de
->d_name
, namelen
+1);
152 if (stat(git_path("%s", ref
), &st
) < 0)
154 if (S_ISDIR(st
.st_mode
)) {
155 list
= get_ref_dir(ref
, list
);
158 if (!resolve_ref(ref
, sha1
, 1, &flag
)) {
159 error("%s points nowhere!", ref
);
162 list
= add_ref(ref
, sha1
, flag
, 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 */
182 const char *resolve_ref(const char *ref
, unsigned char *sha1
, int reading
, int *flag
)
184 int depth
= MAXDEPTH
, len
;
186 static char ref_buffer
[256];
192 const char *path
= git_path("%s", ref
);
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
207 if (lstat(path
, &st
) < 0) {
208 struct ref_list
*list
= get_packed_refs();
210 if (!strcmp(ref
, list
->name
)) {
211 hashcpy(sha1
, list
->sha1
);
213 *flag
|= REF_ISPACKED
;
218 if (reading
|| errno
!= ENOENT
)
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)) {
229 strcpy(ref_buffer
, buffer
);
232 *flag
|= REF_ISSYMREF
;
238 * Anything else, just open it and try to use it as
241 fd
= open(path
, O_RDONLY
);
244 len
= read(fd
, buffer
, sizeof(buffer
)-1);
248 * Is it a symbolic ref?
250 if (len
< 4 || memcmp("ref:", buffer
, 4))
254 while (len
&& isspace(*buf
))
256 while (len
&& isspace(buf
[len
-1]))
259 memcpy(ref_buffer
, buf
, len
+ 1);
262 *flag
|= REF_ISSYMREF
;
264 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
269 int create_symref(const char *ref_target
, const char *refs_heads_master
)
271 const char *lockpath
;
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
) {
279 if (!symlink(refs_heads_master
, git_HEAD
))
281 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
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
);
290 lockpath
= mkpath("%s.lock", git_HEAD
);
291 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
292 written
= write(fd
, ref
, len
);
294 if (written
!= len
) {
296 error("Unable to write to %s", lockpath
);
299 if (rename(lockpath
, git_HEAD
) < 0) {
301 error("Unable to create %s", git_HEAD
);
304 if (adjust_shared_perm(git_HEAD
)) {
306 error("Unable to fix permissions on %s", lockpath
);
312 int read_ref(const char *ref
, unsigned char *sha1
)
314 if (resolve_ref(ref
, sha1
, 1, NULL
))
319 static int do_for_each_ref(const char *base
, each_ref_fn fn
, int trim
,
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
);
330 packed
= packed
->next
;
338 packed
= packed
->next
;
340 if (strncmp(base
, entry
->name
, trim
))
342 if (is_null_sha1(entry
->sha1
))
344 if (!has_sha1_file(entry
->sha1
)) {
345 error("%s does not point to a valid object!", entry
->name
);
348 retval
= fn(entry
->name
+ trim
, entry
->sha1
,
349 entry
->flag
, cb_data
);
354 packed
= packed
? packed
: loose
;
356 if (!strncmp(base
, packed
->name
, trim
)) {
357 retval
= fn(packed
->name
+ trim
, packed
->sha1
,
358 packed
->flag
, cb_data
);
362 packed
= packed
->next
;
367 int head_ref(each_ref_fn fn
, void *cb_data
)
369 unsigned char sha1
[20];
372 if (resolve_ref("HEAD", sha1
, 1, &flag
))
373 return fn("HEAD", sha1
, flag
, cb_data
);
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
402 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
404 if (check_ref_format(ref
))
406 return read_ref(mkpath("refs/%s", ref
), sha1
);
410 * Make sure "ref" is something reasonable to have under ".git/refs/";
411 * We do not like it if:
413 * - any path component of it begins with ".", or
414 * - it has double dots "..", or
415 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
416 * - it ends with a "/".
419 static inline int bad_ref_char(int ch
)
421 return (((unsigned) ch
) <= ' ' ||
422 ch
== '~' || ch
== '^' || ch
== ':' ||
423 /* 2.13 Pattern Matching Notation */
424 ch
== '?' || ch
== '*' || ch
== '[');
427 int check_ref_format(const char *ref
)
430 const char *cp
= ref
;
434 while ((ch
= *cp
++) == '/')
435 ; /* tolerate duplicated slashes */
437 return -1; /* should not end with slashes */
439 /* we are at the beginning of the path component */
440 if (ch
== '.' || bad_ref_char(ch
))
443 /* scan the rest of the path component */
444 while ((ch
= *cp
++) != 0) {
445 if (bad_ref_char(ch
))
449 if (ch
== '.' && *cp
== '.')
455 return -1; /* at least of form "heads/blah" */
461 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
462 const unsigned char *old_sha1
, int mustexist
)
464 if (!resolve_ref(lock
->ref_name
, lock
->old_sha1
, mustexist
, NULL
)) {
465 error("Can't verify ref %s", lock
->ref_name
);
469 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
470 error("Ref %s is at %s but expected %s", lock
->ref_name
,
471 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
478 static int remove_empty_dir_recursive(char *path
, int len
)
480 DIR *dir
= opendir(path
);
486 if (path
[len
-1] != '/')
488 while ((e
= readdir(dir
)) != NULL
) {
491 if ((e
->d_name
[0] == '.') &&
492 ((e
->d_name
[1] == 0) ||
493 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
494 continue; /* "." and ".." */
496 namlen
= strlen(e
->d_name
);
497 if ((len
+ namlen
< PATH_MAX
) &&
498 strcpy(path
+ len
, e
->d_name
) &&
500 S_ISDIR(st
.st_mode
) &&
501 !remove_empty_dir_recursive(path
, len
+ namlen
))
502 continue; /* happy */
504 /* path too long, stat fails, or non-directory still exists */
516 static int remove_empty_directories(char *file
)
518 /* we want to create a file but there is a directory there;
519 * if that is an empty directory (or a directory that contains
520 * only empty directories), remove them.
523 int len
= strlen(file
);
525 if (len
>= PATH_MAX
) /* path too long ;-) */
528 return remove_empty_dir_recursive(path
, len
);
531 static struct ref_lock
*lock_ref_sha1_basic(const char *ref
, const unsigned char *old_sha1
, int *flag
)
534 const char *orig_ref
= ref
;
535 struct ref_lock
*lock
;
538 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
540 lock
= xcalloc(1, sizeof(struct ref_lock
));
543 ref
= resolve_ref(ref
, lock
->old_sha1
, mustexist
, flag
);
544 if (!ref
&& errno
== EISDIR
) {
545 /* we are trying to lock foo but we used to
546 * have foo/bar which now does not exist;
547 * it is normal for the empty directory 'foo'
550 ref_file
= git_path("%s", orig_ref
);
551 if (remove_empty_directories(ref_file
)) {
553 error("there are still refs under '%s'", orig_ref
);
556 ref
= resolve_ref(orig_ref
, lock
->old_sha1
, mustexist
, flag
);
560 error("unable to resolve reference %s: %s",
561 orig_ref
, strerror(errno
));
564 if (is_null_sha1(lock
->old_sha1
)) {
565 /* The ref did not exist and we are creating it.
566 * Make sure there is no existing ref that is packed
567 * whose name begins with our refname, nor a ref whose
568 * name is a proper prefix of our refname.
570 int namlen
= strlen(ref
); /* e.g. 'foo/bar' */
571 struct ref_list
*list
= get_packed_refs();
573 /* list->name could be 'foo' or 'foo/bar/baz' */
574 int len
= strlen(list
->name
);
575 int cmplen
= (namlen
< len
) ? namlen
: len
;
576 const char *lead
= (namlen
< len
) ? list
->name
: ref
;
578 if (!strncmp(ref
, list
->name
, cmplen
) &&
579 lead
[cmplen
] == '/') {
580 error("'%s' exists; cannot create '%s'",
588 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
590 lock
->ref_name
= xstrdup(ref
);
591 lock
->log_file
= xstrdup(git_path("logs/%s", ref
));
592 ref_file
= git_path("%s", ref
);
593 lock
->force_write
= lstat(ref_file
, &st
) && errno
== ENOENT
;
595 if (safe_create_leading_directories(ref_file
)) {
597 error("unable to create directory for %s", ref_file
);
600 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, ref_file
, 1);
602 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
610 struct ref_lock
*lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
612 char refpath
[PATH_MAX
];
613 if (check_ref_format(ref
))
615 strcpy(refpath
, mkpath("refs/%s", ref
));
616 return lock_ref_sha1_basic(refpath
, old_sha1
, NULL
);
619 struct ref_lock
*lock_any_ref_for_update(const char *ref
, const unsigned char *old_sha1
)
621 return lock_ref_sha1_basic(ref
, old_sha1
, NULL
);
624 static struct lock_file packlock
;
626 static int repack_without_ref(const char *refname
)
628 struct ref_list
*list
, *packed_ref_list
;
632 packed_ref_list
= get_packed_refs();
633 for (list
= packed_ref_list
; list
; list
= list
->next
) {
634 if (!strcmp(refname
, list
->name
)) {
641 memset(&packlock
, 0, sizeof(packlock
));
642 fd
= hold_lock_file_for_update(&packlock
, git_path("packed-refs"), 0);
644 return error("cannot delete '%s' from packed refs", refname
);
646 for (list
= packed_ref_list
; list
; list
= list
->next
) {
647 char line
[PATH_MAX
+ 100];
650 if (!strcmp(refname
, list
->name
))
652 len
= snprintf(line
, sizeof(line
), "%s %s\n",
653 sha1_to_hex(list
->sha1
), list
->name
);
654 /* this should not happen but just being defensive */
655 if (len
> sizeof(line
))
656 die("too long a refname '%s'", list
->name
);
657 write_or_die(fd
, line
, len
);
659 return commit_lock_file(&packlock
);
662 int delete_ref(const char *refname
, unsigned char *sha1
)
664 struct ref_lock
*lock
;
665 int err
, i
, ret
= 0, flag
= 0;
667 lock
= lock_ref_sha1_basic(refname
, sha1
, &flag
);
670 if (!(flag
& REF_ISPACKED
)) {
672 i
= strlen(lock
->lk
->filename
) - 5; /* .lock */
673 lock
->lk
->filename
[i
] = 0;
674 err
= unlink(lock
->lk
->filename
);
677 error("unlink(%s) failed: %s",
678 lock
->lk
->filename
, strerror(errno
));
680 lock
->lk
->filename
[i
] = '.';
682 /* removing the loose one could have resurrected an earlier
683 * packed one. Also, if it was not loose we need to repack
686 ret
|= repack_without_ref(refname
);
688 err
= unlink(lock
->log_file
);
689 if (err
&& errno
!= ENOENT
)
690 fprintf(stderr
, "warning: unlink(%s) failed: %s",
691 lock
->log_file
, strerror(errno
));
692 invalidate_cached_refs();
697 void unlock_ref(struct ref_lock
*lock
)
699 if (lock
->lock_fd
>= 0) {
700 close(lock
->lock_fd
);
701 /* Do not free lock->lk -- atexit() still looks at them */
703 rollback_lock_file(lock
->lk
);
705 free(lock
->ref_name
);
706 free(lock
->log_file
);
710 static int log_ref_write(struct ref_lock
*lock
,
711 const unsigned char *sha1
, const char *msg
)
713 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
714 unsigned maxlen
, len
;
716 const char *committer
;
718 if (log_all_ref_updates
) {
719 if (safe_create_leading_directories(lock
->log_file
) < 0)
720 return error("unable to create directory for %s",
725 logfd
= open(lock
->log_file
, oflags
, 0666);
727 if (!log_all_ref_updates
&& errno
== ENOENT
)
729 return error("Unable to append to %s: %s",
730 lock
->log_file
, strerror(errno
));
733 committer
= git_committer_info(1);
735 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
736 logrec
= xmalloc(maxlen
);
737 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
738 sha1_to_hex(lock
->old_sha1
),
744 maxlen
= strlen(committer
) + 2*40 + 4;
745 logrec
= xmalloc(maxlen
);
746 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
747 sha1_to_hex(lock
->old_sha1
),
751 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
755 return error("Unable to append to %s", lock
->log_file
);
759 int write_ref_sha1(struct ref_lock
*lock
,
760 const unsigned char *sha1
, const char *logmsg
)
762 static char term
= '\n';
766 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
770 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
771 write(lock
->lock_fd
, &term
, 1) != 1
772 || close(lock
->lock_fd
) < 0) {
773 error("Couldn't write %s", lock
->lk
->filename
);
777 invalidate_cached_refs();
778 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
782 if (commit_lock_file(lock
->lk
)) {
783 error("Couldn't set %s", lock
->ref_name
);
792 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
794 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
799 unsigned char logged_sha1
[20];
801 logfile
= git_path("logs/%s", ref
);
802 logfd
= open(logfile
, O_RDONLY
, 0);
804 die("Unable to read log %s: %s", logfile
, strerror(errno
));
807 die("Log %s is empty.", logfile
);
808 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
812 rec
= logend
= logdata
+ st
.st_size
;
813 while (logdata
< rec
) {
814 if (logdata
< rec
&& *(rec
-1) == '\n')
817 while (logdata
< rec
&& *(rec
-1) != '\n') {
823 die("Log %s is corrupt.", logfile
);
824 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
825 if (date
<= at_time
) {
827 if (get_sha1_hex(lastrec
, logged_sha1
))
828 die("Log %s is corrupt.", logfile
);
829 if (get_sha1_hex(rec
+ 41, sha1
))
830 die("Log %s is corrupt.", logfile
);
831 if (hashcmp(logged_sha1
, sha1
)) {
832 tz
= strtoul(tz_c
, NULL
, 10);
834 "warning: Log %s has gap after %s.\n",
835 logfile
, show_rfc2822_date(date
, tz
));
838 else if (date
== at_time
) {
839 if (get_sha1_hex(rec
+ 41, sha1
))
840 die("Log %s is corrupt.", logfile
);
843 if (get_sha1_hex(rec
+ 41, logged_sha1
))
844 die("Log %s is corrupt.", logfile
);
845 if (hashcmp(logged_sha1
, sha1
)) {
846 tz
= strtoul(tz_c
, NULL
, 10);
848 "warning: Log %s unexpectedly ended on %s.\n",
849 logfile
, show_rfc2822_date(date
, tz
));
852 munmap((void*)logdata
, st
.st_size
);
859 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
861 if (rec
== logend
|| *rec
== '\n')
862 die("Log %s is corrupt.", logfile
);
863 date
= strtoul(rec
+ 1, &tz_c
, 10);
864 tz
= strtoul(tz_c
, NULL
, 10);
865 if (get_sha1_hex(logdata
, sha1
))
866 die("Log %s is corrupt.", logfile
);
867 munmap((void*)logdata
, st
.st_size
);
868 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
869 logfile
, show_rfc2822_date(date
, tz
));