8 /* ISSYMREF=01 and ISPACKED=02 are public interfaces */
9 #define REF_KNOWS_PEELED 04
12 struct ref_list
*next
;
13 unsigned char flag
; /* ISSYMREF? ISPACKED? */
14 unsigned char sha1
[20];
15 unsigned char peeled
[20];
16 char name
[FLEX_ARRAY
];
19 static const char *parse_ref_line(char *line
, unsigned char *sha1
)
22 * 42: the answer to everything.
24 * In this case, it happens to be the answer to
25 * 40 (length of sha1 hex representation)
26 * +1 (space in between hex and name)
27 * +1 (newline at the end of the line)
29 int len
= strlen(line
) - 42;
33 if (get_sha1_hex(line
, sha1
) < 0)
35 if (!isspace(line
[40]))
40 if (line
[len
] != '\n')
47 static struct ref_list
*add_ref(const char *name
, const unsigned char *sha1
,
48 int flag
, struct ref_list
*list
,
49 struct ref_list
**new_entry
)
52 struct ref_list
**p
= &list
, *entry
;
54 /* Find the place to insert the ref into.. */
55 while ((entry
= *p
) != NULL
) {
56 int cmp
= strcmp(entry
->name
, name
);
60 /* Same as existing entry? */
69 /* Allocate it and add it in.. */
70 len
= strlen(name
) + 1;
71 entry
= xmalloc(sizeof(struct ref_list
) + len
);
72 hashcpy(entry
->sha1
, sha1
);
73 hashclr(entry
->peeled
);
74 memcpy(entry
->name
, name
, len
);
84 * Future: need to be in "struct repository"
85 * when doing a full libification.
90 struct ref_list
*loose
;
91 struct ref_list
*packed
;
94 static void free_ref_list(struct ref_list
*list
)
96 struct ref_list
*next
;
97 for ( ; list
; list
= next
) {
103 static void invalidate_cached_refs(void)
105 struct cached_refs
*ca
= &cached_refs
;
107 if (ca
->did_loose
&& ca
->loose
)
108 free_ref_list(ca
->loose
);
109 if (ca
->did_packed
&& ca
->packed
)
110 free_ref_list(ca
->packed
);
111 ca
->loose
= ca
->packed
= NULL
;
112 ca
->did_loose
= ca
->did_packed
= 0;
115 static void read_packed_refs(FILE *f
, struct cached_refs
*cached_refs
)
117 struct ref_list
*list
= NULL
;
118 struct ref_list
*last
= NULL
;
119 char refline
[PATH_MAX
];
120 int flag
= REF_ISPACKED
;
122 while (fgets(refline
, sizeof(refline
), f
)) {
123 unsigned char sha1
[20];
125 static const char header
[] = "# pack-refs with:";
127 if (!strncmp(refline
, header
, sizeof(header
)-1)) {
128 const char *traits
= refline
+ sizeof(header
) - 1;
129 if (strstr(traits
, " peeled "))
130 flag
|= REF_KNOWS_PEELED
;
131 /* perhaps other traits later as well */
135 name
= parse_ref_line(refline
, sha1
);
137 list
= add_ref(name
, sha1
, flag
, list
, &last
);
142 strlen(refline
) == 42 &&
143 refline
[41] == '\n' &&
144 !get_sha1_hex(refline
+ 1, sha1
))
145 hashcpy(last
->peeled
, sha1
);
147 cached_refs
->packed
= list
;
150 static struct ref_list
*get_packed_refs(void)
152 if (!cached_refs
.did_packed
) {
153 FILE *f
= fopen(git_path("packed-refs"), "r");
154 cached_refs
.packed
= NULL
;
156 read_packed_refs(f
, &cached_refs
);
159 cached_refs
.did_packed
= 1;
161 return cached_refs
.packed
;
164 static struct ref_list
*get_ref_dir(const char *base
, struct ref_list
*list
)
166 DIR *dir
= opendir(git_path("%s", base
));
170 int baselen
= strlen(base
);
171 char *ref
= xmalloc(baselen
+ 257);
173 memcpy(ref
, base
, baselen
);
174 if (baselen
&& base
[baselen
-1] != '/')
175 ref
[baselen
++] = '/';
177 while ((de
= readdir(dir
)) != NULL
) {
178 unsigned char sha1
[20];
183 if (de
->d_name
[0] == '.')
185 namelen
= strlen(de
->d_name
);
188 if (has_extension(de
->d_name
, ".lock"))
190 memcpy(ref
+ baselen
, de
->d_name
, namelen
+1);
191 if (stat(git_path("%s", ref
), &st
) < 0)
193 if (S_ISDIR(st
.st_mode
)) {
194 list
= get_ref_dir(ref
, list
);
197 if (!resolve_ref(ref
, sha1
, 1, &flag
)) {
198 error("%s points nowhere!", ref
);
201 list
= add_ref(ref
, sha1
, flag
, list
, NULL
);
209 static struct ref_list
*get_loose_refs(void)
211 if (!cached_refs
.did_loose
) {
212 cached_refs
.loose
= get_ref_dir("refs", NULL
);
213 cached_refs
.did_loose
= 1;
215 return cached_refs
.loose
;
218 /* We allow "recursive" symbolic refs. Only within reason, though */
221 const char *resolve_ref(const char *ref
, unsigned char *sha1
, int reading
, int *flag
)
223 int depth
= MAXDEPTH
, len
;
225 static char ref_buffer
[256];
231 const char *path
= git_path("%s", ref
);
239 /* Special case: non-existing file.
240 * Not having the refs/heads/new-branch is OK
241 * if we are writing into it, so is .git/HEAD
242 * that points at refs/heads/master still to be
243 * born. It is NOT OK if we are resolving for
246 if (lstat(path
, &st
) < 0) {
247 struct ref_list
*list
= get_packed_refs();
249 if (!strcmp(ref
, list
->name
)) {
250 hashcpy(sha1
, list
->sha1
);
252 *flag
|= REF_ISPACKED
;
257 if (reading
|| errno
!= ENOENT
)
263 /* Follow "normalized" - ie "refs/.." symlinks by hand */
264 if (S_ISLNK(st
.st_mode
)) {
265 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
266 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
268 strcpy(ref_buffer
, buffer
);
271 *flag
|= REF_ISSYMREF
;
276 /* Is it a directory? */
277 if (S_ISDIR(st
.st_mode
)) {
283 * Anything else, just open it and try to use it as
286 fd
= open(path
, O_RDONLY
);
289 len
= read(fd
, buffer
, sizeof(buffer
)-1);
293 * Is it a symbolic ref?
295 if (len
< 4 || memcmp("ref:", buffer
, 4))
299 while (len
&& isspace(*buf
))
301 while (len
&& isspace(buf
[len
-1]))
304 memcpy(ref_buffer
, buf
, len
+ 1);
307 *flag
|= REF_ISSYMREF
;
309 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
314 int create_symref(const char *ref_target
, const char *refs_heads_master
)
316 const char *lockpath
;
318 int fd
, len
, written
;
319 const char *git_HEAD
= git_path("%s", ref_target
);
321 #ifndef NO_SYMLINK_HEAD
322 if (prefer_symlink_refs
) {
324 if (!symlink(refs_heads_master
, git_HEAD
))
326 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
330 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
331 if (sizeof(ref
) <= len
) {
332 error("refname too long: %s", refs_heads_master
);
335 lockpath
= mkpath("%s.lock", git_HEAD
);
336 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
337 written
= write(fd
, ref
, len
);
339 if (written
!= len
) {
341 error("Unable to write to %s", lockpath
);
344 if (rename(lockpath
, git_HEAD
) < 0) {
346 error("Unable to create %s", git_HEAD
);
349 if (adjust_shared_perm(git_HEAD
)) {
351 error("Unable to fix permissions on %s", lockpath
);
357 int read_ref(const char *ref
, unsigned char *sha1
)
359 if (resolve_ref(ref
, sha1
, 1, NULL
))
364 static int do_one_ref(const char *base
, each_ref_fn fn
, int trim
,
365 void *cb_data
, struct ref_list
*entry
)
367 if (strncmp(base
, entry
->name
, trim
))
369 if (is_null_sha1(entry
->sha1
))
371 if (!has_sha1_file(entry
->sha1
)) {
372 error("%s does not point to a valid object!", entry
->name
);
375 return fn(entry
->name
+ trim
, entry
->sha1
, entry
->flag
, cb_data
);
378 int peel_ref(const char *ref
, unsigned char *sha1
)
381 unsigned char base
[20];
384 if (!resolve_ref(ref
, base
, 1, &flag
))
387 if ((flag
& REF_ISPACKED
)) {
388 struct ref_list
*list
= get_packed_refs();
391 if (!strcmp(list
->name
, ref
)) {
392 if (list
->flag
& REF_KNOWS_PEELED
) {
393 hashcpy(sha1
, list
->peeled
);
396 /* older pack-refs did not leave peeled ones */
403 /* fallback - callers should not call this for unpacked refs */
404 o
= parse_object(base
);
405 if (o
->type
== OBJ_TAG
) {
406 o
= deref_tag(o
, ref
, 0);
408 hashcpy(sha1
, o
->sha1
);
415 static int do_for_each_ref(const char *base
, each_ref_fn fn
, int trim
,
419 struct ref_list
*packed
= get_packed_refs();
420 struct ref_list
*loose
= get_loose_refs();
422 while (packed
&& loose
) {
423 struct ref_list
*entry
;
424 int cmp
= strcmp(packed
->name
, loose
->name
);
426 packed
= packed
->next
;
434 packed
= packed
->next
;
436 retval
= do_one_ref(base
, fn
, trim
, cb_data
, entry
);
441 for (packed
= packed
? packed
: loose
; packed
; packed
= packed
->next
) {
442 retval
= do_one_ref(base
, fn
, trim
, cb_data
, packed
);
449 int head_ref(each_ref_fn fn
, void *cb_data
)
451 unsigned char sha1
[20];
454 if (resolve_ref("HEAD", sha1
, 1, &flag
))
455 return fn("HEAD", sha1
, flag
, cb_data
);
459 int for_each_ref(each_ref_fn fn
, void *cb_data
)
461 return do_for_each_ref("refs/", fn
, 0, cb_data
);
464 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
466 return do_for_each_ref("refs/tags/", fn
, 10, cb_data
);
469 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
471 return do_for_each_ref("refs/heads/", fn
, 11, cb_data
);
474 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
476 return do_for_each_ref("refs/remotes/", fn
, 13, cb_data
);
479 /* NEEDSWORK: This is only used by ssh-upload and it should go; the
480 * caller should do resolve_ref or read_ref like everybody else. Or
481 * maybe everybody else should use get_ref_sha1() instead of doing
484 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
486 if (check_ref_format(ref
))
488 return read_ref(mkpath("refs/%s", ref
), sha1
);
492 * Make sure "ref" is something reasonable to have under ".git/refs/";
493 * We do not like it if:
495 * - any path component of it begins with ".", or
496 * - it has double dots "..", or
497 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
498 * - it ends with a "/".
501 static inline int bad_ref_char(int ch
)
503 return (((unsigned) ch
) <= ' ' ||
504 ch
== '~' || ch
== '^' || ch
== ':' ||
505 /* 2.13 Pattern Matching Notation */
506 ch
== '?' || ch
== '*' || ch
== '[');
509 int check_ref_format(const char *ref
)
512 const char *cp
= ref
;
516 while ((ch
= *cp
++) == '/')
517 ; /* tolerate duplicated slashes */
519 return -1; /* should not end with slashes */
521 /* we are at the beginning of the path component */
522 if (ch
== '.' || bad_ref_char(ch
))
525 /* scan the rest of the path component */
526 while ((ch
= *cp
++) != 0) {
527 if (bad_ref_char(ch
))
531 if (ch
== '.' && *cp
== '.')
537 return -1; /* at least of form "heads/blah" */
543 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
544 const unsigned char *old_sha1
, int mustexist
)
546 if (!resolve_ref(lock
->ref_name
, lock
->old_sha1
, mustexist
, NULL
)) {
547 error("Can't verify ref %s", lock
->ref_name
);
551 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
552 error("Ref %s is at %s but expected %s", lock
->ref_name
,
553 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
560 static int remove_empty_dir_recursive(char *path
, int len
)
562 DIR *dir
= opendir(path
);
568 if (path
[len
-1] != '/')
570 while ((e
= readdir(dir
)) != NULL
) {
573 if ((e
->d_name
[0] == '.') &&
574 ((e
->d_name
[1] == 0) ||
575 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
576 continue; /* "." and ".." */
578 namlen
= strlen(e
->d_name
);
579 if ((len
+ namlen
< PATH_MAX
) &&
580 strcpy(path
+ len
, e
->d_name
) &&
582 S_ISDIR(st
.st_mode
) &&
583 !remove_empty_dir_recursive(path
, len
+ namlen
))
584 continue; /* happy */
586 /* path too long, stat fails, or non-directory still exists */
598 static int remove_empty_directories(char *file
)
600 /* we want to create a file but there is a directory there;
601 * if that is an empty directory (or a directory that contains
602 * only empty directories), remove them.
605 int len
= strlen(file
);
607 if (len
>= PATH_MAX
) /* path too long ;-) */
610 return remove_empty_dir_recursive(path
, len
);
613 static struct ref_lock
*lock_ref_sha1_basic(const char *ref
, const unsigned char *old_sha1
, int *flag
)
616 const char *orig_ref
= ref
;
617 struct ref_lock
*lock
;
620 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
622 lock
= xcalloc(1, sizeof(struct ref_lock
));
625 ref
= resolve_ref(ref
, lock
->old_sha1
, mustexist
, flag
);
626 if (!ref
&& errno
== EISDIR
) {
627 /* we are trying to lock foo but we used to
628 * have foo/bar which now does not exist;
629 * it is normal for the empty directory 'foo'
632 ref_file
= git_path("%s", orig_ref
);
633 if (remove_empty_directories(ref_file
)) {
635 error("there are still refs under '%s'", orig_ref
);
638 ref
= resolve_ref(orig_ref
, lock
->old_sha1
, mustexist
, flag
);
642 error("unable to resolve reference %s: %s",
643 orig_ref
, strerror(errno
));
646 if (is_null_sha1(lock
->old_sha1
)) {
647 /* The ref did not exist and we are creating it.
648 * Make sure there is no existing ref that is packed
649 * whose name begins with our refname, nor a ref whose
650 * name is a proper prefix of our refname.
652 int namlen
= strlen(ref
); /* e.g. 'foo/bar' */
653 struct ref_list
*list
= get_packed_refs();
655 /* list->name could be 'foo' or 'foo/bar/baz' */
656 int len
= strlen(list
->name
);
657 int cmplen
= (namlen
< len
) ? namlen
: len
;
658 const char *lead
= (namlen
< len
) ? list
->name
: ref
;
660 if (!strncmp(ref
, list
->name
, cmplen
) &&
661 lead
[cmplen
] == '/') {
662 error("'%s' exists; cannot create '%s'",
670 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
672 lock
->ref_name
= xstrdup(ref
);
673 lock
->log_file
= xstrdup(git_path("logs/%s", ref
));
674 ref_file
= git_path("%s", ref
);
675 lock
->force_write
= lstat(ref_file
, &st
) && errno
== ENOENT
;
677 if (safe_create_leading_directories(ref_file
)) {
679 error("unable to create directory for %s", ref_file
);
682 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, ref_file
, 1);
684 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
692 struct ref_lock
*lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
694 char refpath
[PATH_MAX
];
695 if (check_ref_format(ref
))
697 strcpy(refpath
, mkpath("refs/%s", ref
));
698 return lock_ref_sha1_basic(refpath
, old_sha1
, NULL
);
701 struct ref_lock
*lock_any_ref_for_update(const char *ref
, const unsigned char *old_sha1
)
703 return lock_ref_sha1_basic(ref
, old_sha1
, NULL
);
706 static struct lock_file packlock
;
708 static int repack_without_ref(const char *refname
)
710 struct ref_list
*list
, *packed_ref_list
;
714 packed_ref_list
= get_packed_refs();
715 for (list
= packed_ref_list
; list
; list
= list
->next
) {
716 if (!strcmp(refname
, list
->name
)) {
723 memset(&packlock
, 0, sizeof(packlock
));
724 fd
= hold_lock_file_for_update(&packlock
, git_path("packed-refs"), 0);
726 return error("cannot delete '%s' from packed refs", refname
);
728 for (list
= packed_ref_list
; list
; list
= list
->next
) {
729 char line
[PATH_MAX
+ 100];
732 if (!strcmp(refname
, list
->name
))
734 len
= snprintf(line
, sizeof(line
), "%s %s\n",
735 sha1_to_hex(list
->sha1
), list
->name
);
736 /* this should not happen but just being defensive */
737 if (len
> sizeof(line
))
738 die("too long a refname '%s'", list
->name
);
739 write_or_die(fd
, line
, len
);
741 return commit_lock_file(&packlock
);
744 int delete_ref(const char *refname
, unsigned char *sha1
)
746 struct ref_lock
*lock
;
747 int err
, i
, ret
= 0, flag
= 0;
749 lock
= lock_ref_sha1_basic(refname
, sha1
, &flag
);
752 if (!(flag
& REF_ISPACKED
)) {
754 i
= strlen(lock
->lk
->filename
) - 5; /* .lock */
755 lock
->lk
->filename
[i
] = 0;
756 err
= unlink(lock
->lk
->filename
);
759 error("unlink(%s) failed: %s",
760 lock
->lk
->filename
, strerror(errno
));
762 lock
->lk
->filename
[i
] = '.';
764 /* removing the loose one could have resurrected an earlier
765 * packed one. Also, if it was not loose we need to repack
768 ret
|= repack_without_ref(refname
);
770 err
= unlink(lock
->log_file
);
771 if (err
&& errno
!= ENOENT
)
772 fprintf(stderr
, "warning: unlink(%s) failed: %s",
773 lock
->log_file
, strerror(errno
));
774 invalidate_cached_refs();
779 void unlock_ref(struct ref_lock
*lock
)
781 if (lock
->lock_fd
>= 0) {
782 close(lock
->lock_fd
);
783 /* Do not free lock->lk -- atexit() still looks at them */
785 rollback_lock_file(lock
->lk
);
787 free(lock
->ref_name
);
788 free(lock
->log_file
);
792 static int log_ref_write(struct ref_lock
*lock
,
793 const unsigned char *sha1
, const char *msg
)
795 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
796 unsigned maxlen
, len
;
798 const char *committer
;
800 if (log_all_ref_updates
&&
801 !strncmp(lock
->ref_name
, "refs/heads/", 11)) {
802 if (safe_create_leading_directories(lock
->log_file
) < 0)
803 return error("unable to create directory for %s",
808 logfd
= open(lock
->log_file
, oflags
, 0666);
810 if (!(oflags
& O_CREAT
) && errno
== ENOENT
)
813 if ((oflags
& O_CREAT
) && errno
== EISDIR
) {
814 if (remove_empty_directories(lock
->log_file
)) {
815 return error("There are still logs under '%s'",
818 logfd
= open(lock
->log_file
, oflags
, 0666);
822 return error("Unable to append to %s: %s",
823 lock
->log_file
, strerror(errno
));
826 committer
= git_committer_info(1);
828 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
829 logrec
= xmalloc(maxlen
);
830 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
831 sha1_to_hex(lock
->old_sha1
),
837 maxlen
= strlen(committer
) + 2*40 + 4;
838 logrec
= xmalloc(maxlen
);
839 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
840 sha1_to_hex(lock
->old_sha1
),
844 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
848 return error("Unable to append to %s", lock
->log_file
);
852 int write_ref_sha1(struct ref_lock
*lock
,
853 const unsigned char *sha1
, const char *logmsg
)
855 static char term
= '\n';
859 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
863 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
864 write(lock
->lock_fd
, &term
, 1) != 1
865 || close(lock
->lock_fd
) < 0) {
866 error("Couldn't write %s", lock
->lk
->filename
);
870 invalidate_cached_refs();
871 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
875 if (commit_lock_file(lock
->lk
)) {
876 error("Couldn't set %s", lock
->ref_name
);
885 int read_ref_at(const char *ref
, unsigned long at_time
, int cnt
, unsigned char *sha1
)
887 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
892 unsigned char logged_sha1
[20];
894 logfile
= git_path("logs/%s", ref
);
895 logfd
= open(logfile
, O_RDONLY
, 0);
897 die("Unable to read log %s: %s", logfile
, strerror(errno
));
900 die("Log %s is empty.", logfile
);
901 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
905 rec
= logend
= logdata
+ st
.st_size
;
906 while (logdata
< rec
) {
907 if (logdata
< rec
&& *(rec
-1) == '\n')
910 while (logdata
< rec
&& *(rec
-1) != '\n') {
916 die("Log %s is corrupt.", logfile
);
917 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
918 if (date
<= at_time
|| cnt
== 0) {
920 if (get_sha1_hex(lastrec
, logged_sha1
))
921 die("Log %s is corrupt.", logfile
);
922 if (get_sha1_hex(rec
+ 41, sha1
))
923 die("Log %s is corrupt.", logfile
);
924 if (hashcmp(logged_sha1
, sha1
)) {
925 tz
= strtoul(tz_c
, NULL
, 10);
927 "warning: Log %s has gap after %s.\n",
928 logfile
, show_rfc2822_date(date
, tz
));
931 else if (date
== at_time
) {
932 if (get_sha1_hex(rec
+ 41, sha1
))
933 die("Log %s is corrupt.", logfile
);
936 if (get_sha1_hex(rec
+ 41, logged_sha1
))
937 die("Log %s is corrupt.", logfile
);
938 if (hashcmp(logged_sha1
, sha1
)) {
939 tz
= strtoul(tz_c
, NULL
, 10);
941 "warning: Log %s unexpectedly ended on %s.\n",
942 logfile
, show_rfc2822_date(date
, tz
));
945 munmap((void*)logdata
, st
.st_size
);
954 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
956 if (rec
== logend
|| *rec
== '\n')
957 die("Log %s is corrupt.", logfile
);
958 date
= strtoul(rec
+ 1, &tz_c
, 10);
959 tz
= strtoul(tz_c
, NULL
, 10);
960 if (get_sha1_hex(logdata
, sha1
))
961 die("Log %s is corrupt.", logfile
);
962 munmap((void*)logdata
, st
.st_size
);
963 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
964 logfile
, show_rfc2822_date(date
, tz
));