6 /* ISSYMREF=01 and ISPACKED=02 are public interfaces */
7 #define REF_KNOWS_PEELED 04
10 struct ref_list
*next
;
11 unsigned char flag
; /* ISSYMREF? ISPACKED? */
12 unsigned char sha1
[20];
13 unsigned char peeled
[20];
14 char name
[FLEX_ARRAY
];
17 static const char *parse_ref_line(char *line
, unsigned char *sha1
)
20 * 42: the answer to everything.
22 * In this case, it happens to be the answer to
23 * 40 (length of sha1 hex representation)
24 * +1 (space in between hex and name)
25 * +1 (newline at the end of the line)
27 int len
= strlen(line
) - 42;
31 if (get_sha1_hex(line
, sha1
) < 0)
33 if (!isspace(line
[40]))
38 if (line
[len
] != '\n')
45 static struct ref_list
*add_ref(const char *name
, const unsigned char *sha1
,
46 int flag
, struct ref_list
*list
,
47 struct ref_list
**new_entry
)
50 struct ref_list
**p
= &list
, *entry
;
52 /* Find the place to insert the ref into.. */
53 while ((entry
= *p
) != NULL
) {
54 int cmp
= strcmp(entry
->name
, name
);
58 /* Same as existing entry? */
67 /* Allocate it and add it in.. */
68 len
= strlen(name
) + 1;
69 entry
= xmalloc(sizeof(struct ref_list
) + len
);
70 hashcpy(entry
->sha1
, sha1
);
71 hashclr(entry
->peeled
);
72 memcpy(entry
->name
, name
, len
);
82 * Future: need to be in "struct repository"
83 * when doing a full libification.
88 struct ref_list
*loose
;
89 struct ref_list
*packed
;
92 static void free_ref_list(struct ref_list
*list
)
94 struct ref_list
*next
;
95 for ( ; list
; list
= next
) {
101 static void invalidate_cached_refs(void)
103 struct cached_refs
*ca
= &cached_refs
;
105 if (ca
->did_loose
&& ca
->loose
)
106 free_ref_list(ca
->loose
);
107 if (ca
->did_packed
&& ca
->packed
)
108 free_ref_list(ca
->packed
);
109 ca
->loose
= ca
->packed
= NULL
;
110 ca
->did_loose
= ca
->did_packed
= 0;
113 static void read_packed_refs(FILE *f
, struct cached_refs
*cached_refs
)
115 struct ref_list
*list
= NULL
;
116 struct ref_list
*last
= NULL
;
117 char refline
[PATH_MAX
];
118 int flag
= REF_ISPACKED
;
120 while (fgets(refline
, sizeof(refline
), f
)) {
121 unsigned char sha1
[20];
123 static const char header
[] = "# pack-refs with:";
125 if (!strncmp(refline
, header
, sizeof(header
)-1)) {
126 const char *traits
= refline
+ sizeof(header
) - 1;
127 if (strstr(traits
, " peeled "))
128 flag
|= REF_KNOWS_PEELED
;
129 /* perhaps other traits later as well */
133 name
= parse_ref_line(refline
, sha1
);
135 list
= add_ref(name
, sha1
, flag
, list
, &last
);
140 strlen(refline
) == 42 &&
141 refline
[41] == '\n' &&
142 !get_sha1_hex(refline
+ 1, sha1
))
143 hashcpy(last
->peeled
, sha1
);
145 cached_refs
->packed
= list
;
148 static struct ref_list
*get_packed_refs(void)
150 if (!cached_refs
.did_packed
) {
151 FILE *f
= fopen(git_path("packed-refs"), "r");
152 cached_refs
.packed
= NULL
;
154 read_packed_refs(f
, &cached_refs
);
157 cached_refs
.did_packed
= 1;
159 return cached_refs
.packed
;
162 static struct ref_list
*get_ref_dir(const char *base
, struct ref_list
*list
)
164 DIR *dir
= opendir(git_path("%s", base
));
168 int baselen
= strlen(base
);
169 char *ref
= xmalloc(baselen
+ 257);
171 memcpy(ref
, base
, baselen
);
172 if (baselen
&& base
[baselen
-1] != '/')
173 ref
[baselen
++] = '/';
175 while ((de
= readdir(dir
)) != NULL
) {
176 unsigned char sha1
[20];
181 if (de
->d_name
[0] == '.')
183 namelen
= strlen(de
->d_name
);
186 if (has_extension(de
->d_name
, ".lock"))
188 memcpy(ref
+ baselen
, de
->d_name
, namelen
+1);
189 if (stat(git_path("%s", ref
), &st
) < 0)
191 if (S_ISDIR(st
.st_mode
)) {
192 list
= get_ref_dir(ref
, list
);
195 if (!resolve_ref(ref
, sha1
, 1, &flag
)) {
196 error("%s points nowhere!", ref
);
199 list
= add_ref(ref
, sha1
, flag
, list
, NULL
);
207 static struct ref_list
*get_loose_refs(void)
209 if (!cached_refs
.did_loose
) {
210 cached_refs
.loose
= get_ref_dir("refs", NULL
);
211 cached_refs
.did_loose
= 1;
213 return cached_refs
.loose
;
216 /* We allow "recursive" symbolic refs. Only within reason, though */
219 const char *resolve_ref(const char *ref
, unsigned char *sha1
, int reading
, int *flag
)
221 int depth
= MAXDEPTH
, len
;
223 static char ref_buffer
[256];
229 const char *path
= git_path("%s", ref
);
237 /* Special case: non-existing file.
238 * Not having the refs/heads/new-branch is OK
239 * if we are writing into it, so is .git/HEAD
240 * that points at refs/heads/master still to be
241 * born. It is NOT OK if we are resolving for
244 if (lstat(path
, &st
) < 0) {
245 struct ref_list
*list
= get_packed_refs();
247 if (!strcmp(ref
, list
->name
)) {
248 hashcpy(sha1
, list
->sha1
);
250 *flag
|= REF_ISPACKED
;
255 if (reading
|| errno
!= ENOENT
)
261 /* Follow "normalized" - ie "refs/.." symlinks by hand */
262 if (S_ISLNK(st
.st_mode
)) {
263 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
264 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
266 strcpy(ref_buffer
, buffer
);
269 *flag
|= REF_ISSYMREF
;
274 /* Is it a directory? */
275 if (S_ISDIR(st
.st_mode
)) {
281 * Anything else, just open it and try to use it as
284 fd
= open(path
, O_RDONLY
);
287 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
291 * Is it a symbolic ref?
293 if (len
< 4 || memcmp("ref:", buffer
, 4))
297 while (len
&& isspace(*buf
))
299 while (len
&& isspace(buf
[len
-1]))
302 memcpy(ref_buffer
, buf
, len
+ 1);
305 *flag
|= REF_ISSYMREF
;
307 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
312 int read_ref(const char *ref
, unsigned char *sha1
)
314 if (resolve_ref(ref
, sha1
, 1, NULL
))
319 static int do_one_ref(const char *base
, each_ref_fn fn
, int trim
,
320 void *cb_data
, struct ref_list
*entry
)
322 if (strncmp(base
, entry
->name
, trim
))
324 if (is_null_sha1(entry
->sha1
))
326 if (!has_sha1_file(entry
->sha1
)) {
327 error("%s does not point to a valid object!", entry
->name
);
330 return fn(entry
->name
+ trim
, entry
->sha1
, entry
->flag
, cb_data
);
333 int peel_ref(const char *ref
, unsigned char *sha1
)
336 unsigned char base
[20];
339 if (!resolve_ref(ref
, base
, 1, &flag
))
342 if ((flag
& REF_ISPACKED
)) {
343 struct ref_list
*list
= get_packed_refs();
346 if (!strcmp(list
->name
, ref
)) {
347 if (list
->flag
& REF_KNOWS_PEELED
) {
348 hashcpy(sha1
, list
->peeled
);
351 /* older pack-refs did not leave peeled ones */
358 /* fallback - callers should not call this for unpacked refs */
359 o
= parse_object(base
);
360 if (o
->type
== OBJ_TAG
) {
361 o
= deref_tag(o
, ref
, 0);
363 hashcpy(sha1
, o
->sha1
);
370 static int do_for_each_ref(const char *base
, each_ref_fn fn
, int trim
,
374 struct ref_list
*packed
= get_packed_refs();
375 struct ref_list
*loose
= get_loose_refs();
377 while (packed
&& loose
) {
378 struct ref_list
*entry
;
379 int cmp
= strcmp(packed
->name
, loose
->name
);
381 packed
= packed
->next
;
389 packed
= packed
->next
;
391 retval
= do_one_ref(base
, fn
, trim
, cb_data
, entry
);
396 for (packed
= packed
? packed
: loose
; packed
; packed
= packed
->next
) {
397 retval
= do_one_ref(base
, fn
, trim
, cb_data
, packed
);
404 int head_ref(each_ref_fn fn
, void *cb_data
)
406 unsigned char sha1
[20];
409 if (resolve_ref("HEAD", sha1
, 1, &flag
))
410 return fn("HEAD", sha1
, flag
, cb_data
);
414 int for_each_ref(each_ref_fn fn
, void *cb_data
)
416 return do_for_each_ref("refs/", fn
, 0, cb_data
);
419 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
421 return do_for_each_ref("refs/tags/", fn
, 10, cb_data
);
424 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
426 return do_for_each_ref("refs/heads/", fn
, 11, cb_data
);
429 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
431 return do_for_each_ref("refs/remotes/", fn
, 13, cb_data
);
434 /* NEEDSWORK: This is only used by ssh-upload and it should go; the
435 * caller should do resolve_ref or read_ref like everybody else. Or
436 * maybe everybody else should use get_ref_sha1() instead of doing
439 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
441 if (check_ref_format(ref
))
443 return read_ref(mkpath("refs/%s", ref
), sha1
);
447 * Make sure "ref" is something reasonable to have under ".git/refs/";
448 * We do not like it if:
450 * - any path component of it begins with ".", or
451 * - it has double dots "..", or
452 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
453 * - it ends with a "/".
456 static inline int bad_ref_char(int ch
)
458 return (((unsigned) ch
) <= ' ' ||
459 ch
== '~' || ch
== '^' || ch
== ':' ||
460 /* 2.13 Pattern Matching Notation */
461 ch
== '?' || ch
== '*' || ch
== '[');
464 int check_ref_format(const char *ref
)
467 const char *cp
= ref
;
471 while ((ch
= *cp
++) == '/')
472 ; /* tolerate duplicated slashes */
474 return -1; /* should not end with slashes */
476 /* we are at the beginning of the path component */
477 if (ch
== '.' || bad_ref_char(ch
))
480 /* scan the rest of the path component */
481 while ((ch
= *cp
++) != 0) {
482 if (bad_ref_char(ch
))
486 if (ch
== '.' && *cp
== '.')
492 return -2; /* at least of form "heads/blah" */
498 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
499 const unsigned char *old_sha1
, int mustexist
)
501 if (!resolve_ref(lock
->ref_name
, lock
->old_sha1
, mustexist
, NULL
)) {
502 error("Can't verify ref %s", lock
->ref_name
);
506 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
507 error("Ref %s is at %s but expected %s", lock
->ref_name
,
508 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
515 static int remove_empty_dir_recursive(char *path
, int len
)
517 DIR *dir
= opendir(path
);
523 if (path
[len
-1] != '/')
525 while ((e
= readdir(dir
)) != NULL
) {
528 if ((e
->d_name
[0] == '.') &&
529 ((e
->d_name
[1] == 0) ||
530 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
531 continue; /* "." and ".." */
533 namlen
= strlen(e
->d_name
);
534 if ((len
+ namlen
< PATH_MAX
) &&
535 strcpy(path
+ len
, e
->d_name
) &&
537 S_ISDIR(st
.st_mode
) &&
538 !remove_empty_dir_recursive(path
, len
+ namlen
))
539 continue; /* happy */
541 /* path too long, stat fails, or non-directory still exists */
553 static int remove_empty_directories(char *file
)
555 /* we want to create a file but there is a directory there;
556 * if that is an empty directory (or a directory that contains
557 * only empty directories), remove them.
560 int len
= strlen(file
);
562 if (len
>= PATH_MAX
) /* path too long ;-) */
565 return remove_empty_dir_recursive(path
, len
);
568 static int is_refname_available(const char *ref
, const char *oldref
,
569 struct ref_list
*list
, int quiet
)
571 int namlen
= strlen(ref
); /* e.g. 'foo/bar' */
573 /* list->name could be 'foo' or 'foo/bar/baz' */
574 if (!oldref
|| strcmp(oldref
, list
->name
)) {
575 int len
= strlen(list
->name
);
576 int cmplen
= (namlen
< len
) ? namlen
: len
;
577 const char *lead
= (namlen
< len
) ? list
->name
: ref
;
578 if (!strncmp(ref
, list
->name
, cmplen
) &&
579 lead
[cmplen
] == '/') {
581 error("'%s' exists; cannot create '%s'",
591 static struct ref_lock
*lock_ref_sha1_basic(const char *ref
, const unsigned char *old_sha1
, int *flag
)
594 const char *orig_ref
= ref
;
595 struct ref_lock
*lock
;
598 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
600 lock
= xcalloc(1, sizeof(struct ref_lock
));
603 ref
= resolve_ref(ref
, lock
->old_sha1
, mustexist
, flag
);
604 if (!ref
&& errno
== EISDIR
) {
605 /* we are trying to lock foo but we used to
606 * have foo/bar which now does not exist;
607 * it is normal for the empty directory 'foo'
610 ref_file
= git_path("%s", orig_ref
);
611 if (remove_empty_directories(ref_file
)) {
613 error("there are still refs under '%s'", orig_ref
);
616 ref
= resolve_ref(orig_ref
, lock
->old_sha1
, mustexist
, flag
);
620 error("unable to resolve reference %s: %s",
621 orig_ref
, strerror(errno
));
624 /* When the ref did not exist and we are creating it,
625 * make sure there is no existing ref that is packed
626 * whose name begins with our refname, nor a ref whose
627 * name is a proper prefix of our refname.
629 if (is_null_sha1(lock
->old_sha1
) &&
630 !is_refname_available(ref
, NULL
, get_packed_refs(), 0))
633 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
635 lock
->ref_name
= xstrdup(ref
);
636 lock
->orig_ref_name
= xstrdup(orig_ref
);
637 ref_file
= git_path("%s", ref
);
638 lock
->force_write
= lstat(ref_file
, &st
) && errno
== ENOENT
;
640 if (safe_create_leading_directories(ref_file
)) {
642 error("unable to create directory for %s", ref_file
);
645 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, ref_file
, 1);
647 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
655 struct ref_lock
*lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
657 char refpath
[PATH_MAX
];
658 if (check_ref_format(ref
))
660 strcpy(refpath
, mkpath("refs/%s", ref
));
661 return lock_ref_sha1_basic(refpath
, old_sha1
, NULL
);
664 struct ref_lock
*lock_any_ref_for_update(const char *ref
, const unsigned char *old_sha1
)
666 if (check_ref_format(ref
) == -1)
668 return lock_ref_sha1_basic(ref
, old_sha1
, NULL
);
671 static struct lock_file packlock
;
673 static int repack_without_ref(const char *refname
)
675 struct ref_list
*list
, *packed_ref_list
;
679 packed_ref_list
= get_packed_refs();
680 for (list
= packed_ref_list
; list
; list
= list
->next
) {
681 if (!strcmp(refname
, list
->name
)) {
688 fd
= hold_lock_file_for_update(&packlock
, git_path("packed-refs"), 0);
690 return error("cannot delete '%s' from packed refs", refname
);
692 for (list
= packed_ref_list
; list
; list
= list
->next
) {
693 char line
[PATH_MAX
+ 100];
696 if (!strcmp(refname
, list
->name
))
698 len
= snprintf(line
, sizeof(line
), "%s %s\n",
699 sha1_to_hex(list
->sha1
), list
->name
);
700 /* this should not happen but just being defensive */
701 if (len
> sizeof(line
))
702 die("too long a refname '%s'", list
->name
);
703 write_or_die(fd
, line
, len
);
705 return commit_lock_file(&packlock
);
708 int delete_ref(const char *refname
, unsigned char *sha1
)
710 struct ref_lock
*lock
;
711 int err
, i
, ret
= 0, flag
= 0;
713 lock
= lock_ref_sha1_basic(refname
, sha1
, &flag
);
716 if (!(flag
& REF_ISPACKED
)) {
718 i
= strlen(lock
->lk
->filename
) - 5; /* .lock */
719 lock
->lk
->filename
[i
] = 0;
720 err
= unlink(lock
->lk
->filename
);
723 error("unlink(%s) failed: %s",
724 lock
->lk
->filename
, strerror(errno
));
726 lock
->lk
->filename
[i
] = '.';
728 /* removing the loose one could have resurrected an earlier
729 * packed one. Also, if it was not loose we need to repack
732 ret
|= repack_without_ref(refname
);
734 err
= unlink(git_path("logs/%s", lock
->ref_name
));
735 if (err
&& errno
!= ENOENT
)
736 fprintf(stderr
, "warning: unlink(%s) failed: %s",
737 git_path("logs/%s", lock
->ref_name
), strerror(errno
));
738 invalidate_cached_refs();
743 int rename_ref(const char *oldref
, const char *newref
, const char *logmsg
)
745 static const char renamed_ref
[] = "RENAMED-REF";
746 unsigned char sha1
[20], orig_sha1
[20];
747 int flag
= 0, logmoved
= 0;
748 struct ref_lock
*lock
;
750 int log
= !lstat(git_path("logs/%s", oldref
), &loginfo
);
752 if (S_ISLNK(loginfo
.st_mode
))
753 return error("reflog for %s is a symlink", oldref
);
755 if (!resolve_ref(oldref
, orig_sha1
, 1, &flag
))
756 return error("refname %s not found", oldref
);
758 if (!is_refname_available(newref
, oldref
, get_packed_refs(), 0))
761 if (!is_refname_available(newref
, oldref
, get_loose_refs(), 0))
764 lock
= lock_ref_sha1_basic(renamed_ref
, NULL
, NULL
);
766 return error("unable to lock %s", renamed_ref
);
767 lock
->force_write
= 1;
768 if (write_ref_sha1(lock
, orig_sha1
, logmsg
))
769 return error("unable to save current sha1 in %s", renamed_ref
);
771 if (log
&& rename(git_path("logs/%s", oldref
), git_path("tmp-renamed-log")))
772 return error("unable to move logfile logs/%s to tmp-renamed-log: %s",
773 oldref
, strerror(errno
));
775 if (delete_ref(oldref
, orig_sha1
)) {
776 error("unable to delete old %s", oldref
);
780 if (resolve_ref(newref
, sha1
, 1, &flag
) && delete_ref(newref
, sha1
)) {
782 if (remove_empty_directories(git_path("%s", newref
))) {
783 error("Directory not empty: %s", newref
);
787 error("unable to delete existing %s", newref
);
792 if (log
&& safe_create_leading_directories(git_path("logs/%s", newref
))) {
793 error("unable to create directory for %s", newref
);
798 if (log
&& rename(git_path("tmp-renamed-log"), git_path("logs/%s", newref
))) {
800 if (errno
== EEXIST
) {
802 if (stat(git_path("logs/%s", newref
), &st
) == 0 && S_ISDIR(st
.st_mode
))
808 if (errno
==EISDIR
|| errno
==ENOTDIR
) {
810 * rename(a, b) when b is an existing
811 * directory ought to result in ISDIR, but
812 * Solaris 5.8 gives ENOTDIR. Sheesh.
814 if (remove_empty_directories(git_path("logs/%s", newref
))) {
815 error("Directory not empty: logs/%s", newref
);
820 error("unable to move logfile tmp-renamed-log to logs/%s: %s",
821 newref
, strerror(errno
));
827 lock
= lock_ref_sha1_basic(newref
, NULL
, NULL
);
829 error("unable to lock %s for update", newref
);
833 lock
->force_write
= 1;
834 hashcpy(lock
->old_sha1
, orig_sha1
);
835 if (write_ref_sha1(lock
, orig_sha1
, logmsg
)) {
836 error("unable to write current sha1 into %s", newref
);
840 if (!prefixcmp(oldref
, "refs/heads/") &&
841 !prefixcmp(newref
, "refs/heads/")) {
842 char oldsection
[1024], newsection
[1024];
844 snprintf(oldsection
, 1024, "branch.%s", oldref
+ 11);
845 snprintf(newsection
, 1024, "branch.%s", newref
+ 11);
846 if (git_config_rename_section(oldsection
, newsection
) < 0)
853 lock
= lock_ref_sha1_basic(oldref
, NULL
, NULL
);
855 error("unable to lock %s for rollback", oldref
);
859 lock
->force_write
= 1;
860 flag
= log_all_ref_updates
;
861 log_all_ref_updates
= 0;
862 if (write_ref_sha1(lock
, orig_sha1
, NULL
))
863 error("unable to write current sha1 into %s", oldref
);
864 log_all_ref_updates
= flag
;
867 if (logmoved
&& rename(git_path("logs/%s", newref
), git_path("logs/%s", oldref
)))
868 error("unable to restore logfile %s from %s: %s",
869 oldref
, newref
, strerror(errno
));
870 if (!logmoved
&& log
&&
871 rename(git_path("tmp-renamed-log"), git_path("logs/%s", oldref
)))
872 error("unable to restore logfile %s from tmp-renamed-log: %s",
873 oldref
, strerror(errno
));
878 void unlock_ref(struct ref_lock
*lock
)
880 if (lock
->lock_fd
>= 0) {
881 close(lock
->lock_fd
);
882 /* Do not free lock->lk -- atexit() still looks at them */
884 rollback_lock_file(lock
->lk
);
886 free(lock
->ref_name
);
887 free(lock
->orig_ref_name
);
891 static int log_ref_write(const char *ref_name
, const unsigned char *old_sha1
,
892 const unsigned char *new_sha1
, const char *msg
)
894 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
895 unsigned maxlen
, len
;
897 char *log_file
, *logrec
;
898 const char *committer
;
900 if (log_all_ref_updates
< 0)
901 log_all_ref_updates
= !is_bare_repository();
903 log_file
= git_path("logs/%s", ref_name
);
905 if (log_all_ref_updates
&&
906 (!prefixcmp(ref_name
, "refs/heads/") ||
907 !prefixcmp(ref_name
, "refs/remotes/") ||
908 !strcmp(ref_name
, "HEAD"))) {
909 if (safe_create_leading_directories(log_file
) < 0)
910 return error("unable to create directory for %s",
915 logfd
= open(log_file
, oflags
, 0666);
917 if (!(oflags
& O_CREAT
) && errno
== ENOENT
)
921 if ((oflags
& O_CREAT
) && errno
== EACCES
) {
923 if (stat(log_file
, &st
) == 0 && S_ISDIR(st
.st_mode
))
929 if ((oflags
& O_CREAT
) && errno
== EISDIR
) {
930 if (remove_empty_directories(log_file
)) {
931 return error("There are still logs under '%s'",
934 logfd
= open(log_file
, oflags
, 0666);
938 return error("Unable to append to %s: %s",
939 log_file
, strerror(errno
));
944 /* clean up the message and make sure it is a single line */
949 const char *ep
= strchr(msg
, '\n');
953 msglen
= strlen(msg
);
957 committer
= git_committer_info(-1);
958 maxlen
= strlen(committer
) + msglen
+ 100;
959 logrec
= xmalloc(maxlen
);
960 len
= sprintf(logrec
, "%s %s %s\n",
961 sha1_to_hex(old_sha1
),
962 sha1_to_hex(new_sha1
),
965 len
+= sprintf(logrec
+ len
- 1, "\t%.*s\n", msglen
, msg
) - 1;
966 written
= len
<= maxlen
? write_in_full(logfd
, logrec
, len
) : -1;
970 return error("Unable to append to %s", log_file
);
974 int write_ref_sha1(struct ref_lock
*lock
,
975 const unsigned char *sha1
, const char *logmsg
)
977 static char term
= '\n';
981 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
985 if (write_in_full(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
986 write_in_full(lock
->lock_fd
, &term
, 1) != 1
987 || close(lock
->lock_fd
) < 0) {
988 error("Couldn't write %s", lock
->lk
->filename
);
992 invalidate_cached_refs();
993 if (log_ref_write(lock
->ref_name
, lock
->old_sha1
, sha1
, logmsg
) < 0 ||
994 (strcmp(lock
->ref_name
, lock
->orig_ref_name
) &&
995 log_ref_write(lock
->orig_ref_name
, lock
->old_sha1
, sha1
, logmsg
) < 0)) {
999 if (commit_lock_file(lock
->lk
)) {
1000 error("Couldn't set %s", lock
->ref_name
);
1009 int create_symref(const char *ref_target
, const char *refs_heads_master
,
1012 const char *lockpath
;
1014 int fd
, len
, written
;
1015 char *git_HEAD
= xstrdup(git_path("%s", ref_target
));
1016 unsigned char old_sha1
[20], new_sha1
[20];
1018 if (logmsg
&& read_ref(ref_target
, old_sha1
))
1021 if (safe_create_leading_directories(git_HEAD
) < 0)
1022 return error("unable to create directory for %s", git_HEAD
);
1024 #ifndef NO_SYMLINK_HEAD
1025 if (prefer_symlink_refs
) {
1027 if (!symlink(refs_heads_master
, git_HEAD
))
1029 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
1033 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
1034 if (sizeof(ref
) <= len
) {
1035 error("refname too long: %s", refs_heads_master
);
1036 goto error_free_return
;
1038 lockpath
= mkpath("%s.lock", git_HEAD
);
1039 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
1041 error("Unable to open %s for writing", lockpath
);
1042 goto error_free_return
;
1044 written
= write_in_full(fd
, ref
, len
);
1046 if (written
!= len
) {
1047 error("Unable to write to %s", lockpath
);
1048 goto error_unlink_return
;
1051 if (rename(lockpath
, git_HEAD
) < 0) {
1052 error("Unable to create %s", git_HEAD
);
1053 goto error_unlink_return
;
1055 if (adjust_shared_perm(git_HEAD
)) {
1056 error("Unable to fix permissions on %s", lockpath
);
1057 error_unlink_return
:
1065 if (logmsg
&& !read_ref(refs_heads_master
, new_sha1
))
1066 log_ref_write(ref_target
, old_sha1
, new_sha1
, logmsg
);
1072 static char *ref_msg(const char *line
, const char *endp
)
1078 for (ep
= line
; ep
< endp
&& *ep
!= '\n'; ep
++)
1080 msg
= xmalloc(ep
- line
+ 1);
1081 memcpy(msg
, line
, ep
- line
);
1086 int read_ref_at(const char *ref
, unsigned long at_time
, int cnt
, unsigned char *sha1
, char **msg
, unsigned long *cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
1088 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
1090 int logfd
, tz
, reccnt
= 0;
1093 unsigned char logged_sha1
[20];
1096 logfile
= git_path("logs/%s", ref
);
1097 logfd
= open(logfile
, O_RDONLY
, 0);
1099 die("Unable to read log %s: %s", logfile
, strerror(errno
));
1102 die("Log %s is empty.", logfile
);
1103 log_mapped
= xmmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
1104 logdata
= log_mapped
;
1108 rec
= logend
= logdata
+ st
.st_size
;
1109 while (logdata
< rec
) {
1111 if (logdata
< rec
&& *(rec
-1) == '\n')
1114 while (logdata
< rec
&& *(rec
-1) != '\n') {
1120 die("Log %s is corrupt.", logfile
);
1121 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
1122 if (date
<= at_time
|| cnt
== 0) {
1123 tz
= strtoul(tz_c
, NULL
, 10);
1125 *msg
= ref_msg(rec
, logend
);
1127 *cutoff_time
= date
;
1131 *cutoff_cnt
= reccnt
- 1;
1133 if (get_sha1_hex(lastrec
, logged_sha1
))
1134 die("Log %s is corrupt.", logfile
);
1135 if (get_sha1_hex(rec
+ 41, sha1
))
1136 die("Log %s is corrupt.", logfile
);
1137 if (hashcmp(logged_sha1
, sha1
)) {
1139 "warning: Log %s has gap after %s.\n",
1140 logfile
, show_rfc2822_date(date
, tz
));
1143 else if (date
== at_time
) {
1144 if (get_sha1_hex(rec
+ 41, sha1
))
1145 die("Log %s is corrupt.", logfile
);
1148 if (get_sha1_hex(rec
+ 41, logged_sha1
))
1149 die("Log %s is corrupt.", logfile
);
1150 if (hashcmp(logged_sha1
, sha1
)) {
1152 "warning: Log %s unexpectedly ended on %s.\n",
1153 logfile
, show_rfc2822_date(date
, tz
));
1156 munmap(log_mapped
, st
.st_size
);
1165 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
1167 if (rec
== logend
|| *rec
== '\n')
1168 die("Log %s is corrupt.", logfile
);
1169 date
= strtoul(rec
+ 1, &tz_c
, 10);
1170 tz
= strtoul(tz_c
, NULL
, 10);
1171 if (get_sha1_hex(logdata
, sha1
))
1172 die("Log %s is corrupt.", logfile
);
1174 *msg
= ref_msg(logdata
, logend
);
1175 munmap(log_mapped
, st
.st_size
);
1178 *cutoff_time
= date
;
1182 *cutoff_cnt
= reccnt
;
1186 int for_each_reflog_ent(const char *ref
, each_reflog_ent_fn fn
, void *cb_data
)
1188 const char *logfile
;
1193 logfile
= git_path("logs/%s", ref
);
1194 logfp
= fopen(logfile
, "r");
1197 while (fgets(buf
, sizeof(buf
), logfp
)) {
1198 unsigned char osha1
[20], nsha1
[20];
1199 char *email_end
, *message
;
1200 unsigned long timestamp
;
1203 /* old SP new SP name <email> SP time TAB msg LF */
1205 if (len
< 83 || buf
[len
-1] != '\n' ||
1206 get_sha1_hex(buf
, osha1
) || buf
[40] != ' ' ||
1207 get_sha1_hex(buf
+ 41, nsha1
) || buf
[81] != ' ' ||
1208 !(email_end
= strchr(buf
+ 82, '>')) ||
1209 email_end
[1] != ' ' ||
1210 !(timestamp
= strtoul(email_end
+ 2, &message
, 10)) ||
1211 !message
|| message
[0] != ' ' ||
1212 (message
[1] != '+' && message
[1] != '-') ||
1213 !isdigit(message
[2]) || !isdigit(message
[3]) ||
1214 !isdigit(message
[4]) || !isdigit(message
[5]))
1215 continue; /* corrupt? */
1216 email_end
[1] = '\0';
1217 tz
= strtol(message
+ 1, NULL
, 10);
1218 if (message
[6] != '\t')
1222 ret
= fn(osha1
, nsha1
, buf
+82, timestamp
, tz
, message
, cb_data
);
1230 static int do_for_each_reflog(const char *base
, each_ref_fn fn
, void *cb_data
)
1232 DIR *dir
= opendir(git_path("logs/%s", base
));
1237 int baselen
= strlen(base
);
1238 char *log
= xmalloc(baselen
+ 257);
1240 memcpy(log
, base
, baselen
);
1241 if (baselen
&& base
[baselen
-1] != '/')
1242 log
[baselen
++] = '/';
1244 while ((de
= readdir(dir
)) != NULL
) {
1248 if (de
->d_name
[0] == '.')
1250 namelen
= strlen(de
->d_name
);
1253 if (has_extension(de
->d_name
, ".lock"))
1255 memcpy(log
+ baselen
, de
->d_name
, namelen
+1);
1256 if (stat(git_path("logs/%s", log
), &st
) < 0)
1258 if (S_ISDIR(st
.st_mode
)) {
1259 retval
= do_for_each_reflog(log
, fn
, cb_data
);
1261 unsigned char sha1
[20];
1262 if (!resolve_ref(log
, sha1
, 0, NULL
))
1263 retval
= error("bad ref for %s", log
);
1265 retval
= fn(log
, sha1
, 0, cb_data
);
1278 int for_each_reflog(each_ref_fn fn
, void *cb_data
)
1280 return do_for_each_reflog("", fn
, cb_data
);