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 create_symref(const char *ref_target
, const char *refs_heads_master
)
314 const char *lockpath
;
316 int fd
, len
, written
;
317 const char *git_HEAD
= git_path("%s", ref_target
);
319 #ifndef NO_SYMLINK_HEAD
320 if (prefer_symlink_refs
) {
322 if (!symlink(refs_heads_master
, git_HEAD
))
324 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
328 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
329 if (sizeof(ref
) <= len
) {
330 error("refname too long: %s", refs_heads_master
);
333 lockpath
= mkpath("%s.lock", git_HEAD
);
334 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
335 written
= write_in_full(fd
, ref
, len
);
337 if (written
!= len
) {
339 error("Unable to write to %s", lockpath
);
342 if (rename(lockpath
, git_HEAD
) < 0) {
344 error("Unable to create %s", git_HEAD
);
347 if (adjust_shared_perm(git_HEAD
)) {
349 error("Unable to fix permissions on %s", lockpath
);
355 int read_ref(const char *ref
, unsigned char *sha1
)
357 if (resolve_ref(ref
, sha1
, 1, NULL
))
362 static int do_one_ref(const char *base
, each_ref_fn fn
, int trim
,
363 void *cb_data
, struct ref_list
*entry
)
365 if (strncmp(base
, entry
->name
, trim
))
367 if (is_null_sha1(entry
->sha1
))
369 if (!has_sha1_file(entry
->sha1
)) {
370 error("%s does not point to a valid object!", entry
->name
);
373 return fn(entry
->name
+ trim
, entry
->sha1
, entry
->flag
, cb_data
);
376 int peel_ref(const char *ref
, unsigned char *sha1
)
379 unsigned char base
[20];
382 if (!resolve_ref(ref
, base
, 1, &flag
))
385 if ((flag
& REF_ISPACKED
)) {
386 struct ref_list
*list
= get_packed_refs();
389 if (!strcmp(list
->name
, ref
)) {
390 if (list
->flag
& REF_KNOWS_PEELED
) {
391 hashcpy(sha1
, list
->peeled
);
394 /* older pack-refs did not leave peeled ones */
401 /* fallback - callers should not call this for unpacked refs */
402 o
= parse_object(base
);
403 if (o
->type
== OBJ_TAG
) {
404 o
= deref_tag(o
, ref
, 0);
406 hashcpy(sha1
, o
->sha1
);
413 static int do_for_each_ref(const char *base
, each_ref_fn fn
, int trim
,
417 struct ref_list
*packed
= get_packed_refs();
418 struct ref_list
*loose
= get_loose_refs();
420 while (packed
&& loose
) {
421 struct ref_list
*entry
;
422 int cmp
= strcmp(packed
->name
, loose
->name
);
424 packed
= packed
->next
;
432 packed
= packed
->next
;
434 retval
= do_one_ref(base
, fn
, trim
, cb_data
, entry
);
439 for (packed
= packed
? packed
: loose
; packed
; packed
= packed
->next
) {
440 retval
= do_one_ref(base
, fn
, trim
, cb_data
, packed
);
447 int head_ref(each_ref_fn fn
, void *cb_data
)
449 unsigned char sha1
[20];
452 if (resolve_ref("HEAD", sha1
, 1, &flag
))
453 return fn("HEAD", sha1
, flag
, cb_data
);
457 int for_each_ref(each_ref_fn fn
, void *cb_data
)
459 return do_for_each_ref("refs/", fn
, 0, cb_data
);
462 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
464 return do_for_each_ref("refs/tags/", fn
, 10, cb_data
);
467 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
469 return do_for_each_ref("refs/heads/", fn
, 11, cb_data
);
472 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
474 return do_for_each_ref("refs/remotes/", fn
, 13, cb_data
);
477 /* NEEDSWORK: This is only used by ssh-upload and it should go; the
478 * caller should do resolve_ref or read_ref like everybody else. Or
479 * maybe everybody else should use get_ref_sha1() instead of doing
482 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
484 if (check_ref_format(ref
))
486 return read_ref(mkpath("refs/%s", ref
), sha1
);
490 * Make sure "ref" is something reasonable to have under ".git/refs/";
491 * We do not like it if:
493 * - any path component of it begins with ".", or
494 * - it has double dots "..", or
495 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
496 * - it ends with a "/".
499 static inline int bad_ref_char(int ch
)
501 return (((unsigned) ch
) <= ' ' ||
502 ch
== '~' || ch
== '^' || ch
== ':' ||
503 /* 2.13 Pattern Matching Notation */
504 ch
== '?' || ch
== '*' || ch
== '[');
507 int check_ref_format(const char *ref
)
510 const char *cp
= ref
;
514 while ((ch
= *cp
++) == '/')
515 ; /* tolerate duplicated slashes */
517 return -1; /* should not end with slashes */
519 /* we are at the beginning of the path component */
520 if (ch
== '.' || bad_ref_char(ch
))
523 /* scan the rest of the path component */
524 while ((ch
= *cp
++) != 0) {
525 if (bad_ref_char(ch
))
529 if (ch
== '.' && *cp
== '.')
535 return -2; /* at least of form "heads/blah" */
541 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
542 const unsigned char *old_sha1
, int mustexist
)
544 if (!resolve_ref(lock
->ref_name
, lock
->old_sha1
, mustexist
, NULL
)) {
545 error("Can't verify ref %s", lock
->ref_name
);
549 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
550 error("Ref %s is at %s but expected %s", lock
->ref_name
,
551 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
558 static int remove_empty_dir_recursive(char *path
, int len
)
560 DIR *dir
= opendir(path
);
566 if (path
[len
-1] != '/')
568 while ((e
= readdir(dir
)) != NULL
) {
571 if ((e
->d_name
[0] == '.') &&
572 ((e
->d_name
[1] == 0) ||
573 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
574 continue; /* "." and ".." */
576 namlen
= strlen(e
->d_name
);
577 if ((len
+ namlen
< PATH_MAX
) &&
578 strcpy(path
+ len
, e
->d_name
) &&
580 S_ISDIR(st
.st_mode
) &&
581 !remove_empty_dir_recursive(path
, len
+ namlen
))
582 continue; /* happy */
584 /* path too long, stat fails, or non-directory still exists */
596 static int remove_empty_directories(char *file
)
598 /* we want to create a file but there is a directory there;
599 * if that is an empty directory (or a directory that contains
600 * only empty directories), remove them.
603 int len
= strlen(file
);
605 if (len
>= PATH_MAX
) /* path too long ;-) */
608 return remove_empty_dir_recursive(path
, len
);
611 static int is_refname_available(const char *ref
, const char *oldref
,
612 struct ref_list
*list
, int quiet
)
614 int namlen
= strlen(ref
); /* e.g. 'foo/bar' */
616 /* list->name could be 'foo' or 'foo/bar/baz' */
617 if (!oldref
|| strcmp(oldref
, list
->name
)) {
618 int len
= strlen(list
->name
);
619 int cmplen
= (namlen
< len
) ? namlen
: len
;
620 const char *lead
= (namlen
< len
) ? list
->name
: ref
;
621 if (!strncmp(ref
, list
->name
, cmplen
) &&
622 lead
[cmplen
] == '/') {
624 error("'%s' exists; cannot create '%s'",
634 static struct ref_lock
*lock_ref_sha1_basic(const char *ref
, const unsigned char *old_sha1
, int *flag
)
637 const char *orig_ref
= ref
;
638 struct ref_lock
*lock
;
641 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
643 lock
= xcalloc(1, sizeof(struct ref_lock
));
646 ref
= resolve_ref(ref
, lock
->old_sha1
, mustexist
, flag
);
647 if (!ref
&& errno
== EISDIR
) {
648 /* we are trying to lock foo but we used to
649 * have foo/bar which now does not exist;
650 * it is normal for the empty directory 'foo'
653 ref_file
= git_path("%s", orig_ref
);
654 if (remove_empty_directories(ref_file
)) {
656 error("there are still refs under '%s'", orig_ref
);
659 ref
= resolve_ref(orig_ref
, lock
->old_sha1
, mustexist
, flag
);
663 error("unable to resolve reference %s: %s",
664 orig_ref
, strerror(errno
));
667 /* When the ref did not exist and we are creating it,
668 * make sure there is no existing ref that is packed
669 * whose name begins with our refname, nor a ref whose
670 * name is a proper prefix of our refname.
672 if (is_null_sha1(lock
->old_sha1
) &&
673 !is_refname_available(ref
, NULL
, get_packed_refs(), 0))
676 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
678 lock
->ref_name
= xstrdup(ref
);
679 lock
->log_file
= xstrdup(git_path("logs/%s", ref
));
680 ref_file
= git_path("%s", ref
);
681 lock
->force_write
= lstat(ref_file
, &st
) && errno
== ENOENT
;
683 if (safe_create_leading_directories(ref_file
)) {
685 error("unable to create directory for %s", ref_file
);
688 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, ref_file
, 1);
690 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
698 struct ref_lock
*lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
700 char refpath
[PATH_MAX
];
701 if (check_ref_format(ref
))
703 strcpy(refpath
, mkpath("refs/%s", ref
));
704 return lock_ref_sha1_basic(refpath
, old_sha1
, NULL
);
707 struct ref_lock
*lock_any_ref_for_update(const char *ref
, const unsigned char *old_sha1
)
709 return lock_ref_sha1_basic(ref
, old_sha1
, NULL
);
712 static struct lock_file packlock
;
714 static int repack_without_ref(const char *refname
)
716 struct ref_list
*list
, *packed_ref_list
;
720 packed_ref_list
= get_packed_refs();
721 for (list
= packed_ref_list
; list
; list
= list
->next
) {
722 if (!strcmp(refname
, list
->name
)) {
729 fd
= hold_lock_file_for_update(&packlock
, git_path("packed-refs"), 0);
731 return error("cannot delete '%s' from packed refs", refname
);
733 for (list
= packed_ref_list
; list
; list
= list
->next
) {
734 char line
[PATH_MAX
+ 100];
737 if (!strcmp(refname
, list
->name
))
739 len
= snprintf(line
, sizeof(line
), "%s %s\n",
740 sha1_to_hex(list
->sha1
), list
->name
);
741 /* this should not happen but just being defensive */
742 if (len
> sizeof(line
))
743 die("too long a refname '%s'", list
->name
);
744 write_or_die(fd
, line
, len
);
746 return commit_lock_file(&packlock
);
749 int delete_ref(const char *refname
, unsigned char *sha1
)
751 struct ref_lock
*lock
;
752 int err
, i
, ret
= 0, flag
= 0;
754 lock
= lock_ref_sha1_basic(refname
, sha1
, &flag
);
757 if (!(flag
& REF_ISPACKED
)) {
759 i
= strlen(lock
->lk
->filename
) - 5; /* .lock */
760 lock
->lk
->filename
[i
] = 0;
761 err
= unlink(lock
->lk
->filename
);
764 error("unlink(%s) failed: %s",
765 lock
->lk
->filename
, strerror(errno
));
767 lock
->lk
->filename
[i
] = '.';
769 /* removing the loose one could have resurrected an earlier
770 * packed one. Also, if it was not loose we need to repack
773 ret
|= repack_without_ref(refname
);
775 err
= unlink(lock
->log_file
);
776 if (err
&& errno
!= ENOENT
)
777 fprintf(stderr
, "warning: unlink(%s) failed: %s",
778 lock
->log_file
, strerror(errno
));
779 invalidate_cached_refs();
784 int rename_ref(const char *oldref
, const char *newref
, const char *logmsg
)
786 static const char renamed_ref
[] = "RENAMED-REF";
787 unsigned char sha1
[20], orig_sha1
[20];
788 int flag
= 0, logmoved
= 0;
789 struct ref_lock
*lock
;
791 int log
= !lstat(git_path("logs/%s", oldref
), &loginfo
);
793 if (S_ISLNK(loginfo
.st_mode
))
794 return error("reflog for %s is a symlink", oldref
);
796 if (!resolve_ref(oldref
, orig_sha1
, 1, &flag
))
797 return error("refname %s not found", oldref
);
799 if (!is_refname_available(newref
, oldref
, get_packed_refs(), 0))
802 if (!is_refname_available(newref
, oldref
, get_loose_refs(), 0))
805 lock
= lock_ref_sha1_basic(renamed_ref
, NULL
, NULL
);
807 return error("unable to lock %s", renamed_ref
);
808 lock
->force_write
= 1;
809 if (write_ref_sha1(lock
, orig_sha1
, logmsg
))
810 return error("unable to save current sha1 in %s", renamed_ref
);
812 if (log
&& rename(git_path("logs/%s", oldref
), git_path("tmp-renamed-log")))
813 return error("unable to move logfile logs/%s to tmp-renamed-log: %s",
814 oldref
, strerror(errno
));
816 if (delete_ref(oldref
, orig_sha1
)) {
817 error("unable to delete old %s", oldref
);
821 if (resolve_ref(newref
, sha1
, 1, &flag
) && delete_ref(newref
, sha1
)) {
823 if (remove_empty_directories(git_path("%s", newref
))) {
824 error("Directory not empty: %s", newref
);
828 error("unable to delete existing %s", newref
);
833 if (log
&& safe_create_leading_directories(git_path("logs/%s", newref
))) {
834 error("unable to create directory for %s", newref
);
839 if (log
&& rename(git_path("tmp-renamed-log"), git_path("logs/%s", newref
))) {
840 if (errno
==EISDIR
|| errno
==ENOTDIR
) {
842 * rename(a, b) when b is an existing
843 * directory ought to result in ISDIR, but
844 * Solaris 5.8 gives ENOTDIR. Sheesh.
846 if (remove_empty_directories(git_path("logs/%s", newref
))) {
847 error("Directory not empty: logs/%s", newref
);
852 error("unable to move logfile tmp-renamed-log to logs/%s: %s",
853 newref
, strerror(errno
));
859 lock
= lock_ref_sha1_basic(newref
, NULL
, NULL
);
861 error("unable to lock %s for update", newref
);
865 lock
->force_write
= 1;
866 hashcpy(lock
->old_sha1
, orig_sha1
);
867 if (write_ref_sha1(lock
, orig_sha1
, logmsg
)) {
868 error("unable to write current sha1 into %s", newref
);
872 if (!strncmp(oldref
, "refs/heads/", 11) &&
873 !strncmp(newref
, "refs/heads/", 11)) {
874 char oldsection
[1024], newsection
[1024];
876 snprintf(oldsection
, 1024, "branch.%s", oldref
+ 11);
877 snprintf(newsection
, 1024, "branch.%s", newref
+ 11);
878 if (git_config_rename_section(oldsection
, newsection
) < 0)
885 lock
= lock_ref_sha1_basic(oldref
, NULL
, NULL
);
887 error("unable to lock %s for rollback", oldref
);
891 lock
->force_write
= 1;
892 flag
= log_all_ref_updates
;
893 log_all_ref_updates
= 0;
894 if (write_ref_sha1(lock
, orig_sha1
, NULL
))
895 error("unable to write current sha1 into %s", oldref
);
896 log_all_ref_updates
= flag
;
899 if (logmoved
&& rename(git_path("logs/%s", newref
), git_path("logs/%s", oldref
)))
900 error("unable to restore logfile %s from %s: %s",
901 oldref
, newref
, strerror(errno
));
902 if (!logmoved
&& log
&&
903 rename(git_path("tmp-renamed-log"), git_path("logs/%s", oldref
)))
904 error("unable to restore logfile %s from tmp-renamed-log: %s",
905 oldref
, strerror(errno
));
910 void unlock_ref(struct ref_lock
*lock
)
912 if (lock
->lock_fd
>= 0) {
913 close(lock
->lock_fd
);
914 /* Do not free lock->lk -- atexit() still looks at them */
916 rollback_lock_file(lock
->lk
);
918 free(lock
->ref_name
);
919 free(lock
->log_file
);
923 static int log_ref_write(struct ref_lock
*lock
,
924 const unsigned char *sha1
, const char *msg
)
926 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
927 unsigned maxlen
, len
;
929 const char *committer
;
931 if (log_all_ref_updates
< 0)
932 log_all_ref_updates
= !is_bare_repository();
934 if (log_all_ref_updates
&&
935 (!strncmp(lock
->ref_name
, "refs/heads/", 11) ||
936 !strncmp(lock
->ref_name
, "refs/remotes/", 13))) {
937 if (safe_create_leading_directories(lock
->log_file
) < 0)
938 return error("unable to create directory for %s",
943 logfd
= open(lock
->log_file
, oflags
, 0666);
945 if (!(oflags
& O_CREAT
) && errno
== ENOENT
)
948 if ((oflags
& O_CREAT
) && errno
== EISDIR
) {
949 if (remove_empty_directories(lock
->log_file
)) {
950 return error("There are still logs under '%s'",
953 logfd
= open(lock
->log_file
, oflags
, 0666);
957 return error("Unable to append to %s: %s",
958 lock
->log_file
, strerror(errno
));
961 committer
= git_committer_info(1);
963 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
964 logrec
= xmalloc(maxlen
);
965 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
966 sha1_to_hex(lock
->old_sha1
),
972 maxlen
= strlen(committer
) + 2*40 + 4;
973 logrec
= xmalloc(maxlen
);
974 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
975 sha1_to_hex(lock
->old_sha1
),
979 written
= len
<= maxlen
? write_in_full(logfd
, logrec
, len
) : -1;
983 return error("Unable to append to %s", lock
->log_file
);
987 int write_ref_sha1(struct ref_lock
*lock
,
988 const unsigned char *sha1
, const char *logmsg
)
990 static char term
= '\n';
994 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
998 if (write_in_full(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
999 write_in_full(lock
->lock_fd
, &term
, 1) != 1
1000 || close(lock
->lock_fd
) < 0) {
1001 error("Couldn't write %s", lock
->lk
->filename
);
1005 invalidate_cached_refs();
1006 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
1010 if (commit_lock_file(lock
->lk
)) {
1011 error("Couldn't set %s", lock
->ref_name
);
1020 int read_ref_at(const char *ref
, unsigned long at_time
, int cnt
, unsigned char *sha1
)
1022 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
1024 int logfd
, tz
, reccnt
= 0;
1027 unsigned char logged_sha1
[20];
1030 logfile
= git_path("logs/%s", ref
);
1031 logfd
= open(logfile
, O_RDONLY
, 0);
1033 die("Unable to read log %s: %s", logfile
, strerror(errno
));
1036 die("Log %s is empty.", logfile
);
1037 log_mapped
= xmmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
1038 logdata
= log_mapped
;
1042 rec
= logend
= logdata
+ st
.st_size
;
1043 while (logdata
< rec
) {
1045 if (logdata
< rec
&& *(rec
-1) == '\n')
1048 while (logdata
< rec
&& *(rec
-1) != '\n') {
1054 die("Log %s is corrupt.", logfile
);
1055 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
1056 if (date
<= at_time
|| cnt
== 0) {
1058 if (get_sha1_hex(lastrec
, logged_sha1
))
1059 die("Log %s is corrupt.", logfile
);
1060 if (get_sha1_hex(rec
+ 41, sha1
))
1061 die("Log %s is corrupt.", logfile
);
1062 if (hashcmp(logged_sha1
, sha1
)) {
1063 tz
= strtoul(tz_c
, NULL
, 10);
1065 "warning: Log %s has gap after %s.\n",
1066 logfile
, show_rfc2822_date(date
, tz
));
1069 else if (date
== at_time
) {
1070 if (get_sha1_hex(rec
+ 41, sha1
))
1071 die("Log %s is corrupt.", logfile
);
1074 if (get_sha1_hex(rec
+ 41, logged_sha1
))
1075 die("Log %s is corrupt.", logfile
);
1076 if (hashcmp(logged_sha1
, sha1
)) {
1077 tz
= strtoul(tz_c
, NULL
, 10);
1079 "warning: Log %s unexpectedly ended on %s.\n",
1080 logfile
, show_rfc2822_date(date
, tz
));
1083 munmap(log_mapped
, st
.st_size
);
1092 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
1094 if (rec
== logend
|| *rec
== '\n')
1095 die("Log %s is corrupt.", logfile
);
1096 date
= strtoul(rec
+ 1, &tz_c
, 10);
1097 tz
= strtoul(tz_c
, NULL
, 10);
1098 if (get_sha1_hex(logdata
, sha1
))
1099 die("Log %s is corrupt.", logfile
);
1100 munmap(log_mapped
, st
.st_size
);
1102 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
1103 logfile
, show_rfc2822_date(date
, tz
));
1105 fprintf(stderr
, "warning: Log %s only has %d entries.\n",
1110 int for_each_reflog_ent(const char *ref
, each_reflog_ent_fn fn
, void *cb_data
)
1112 const char *logfile
;
1117 logfile
= git_path("logs/%s", ref
);
1118 logfp
= fopen(logfile
, "r");
1121 while (fgets(buf
, sizeof(buf
), logfp
)) {
1122 unsigned char osha1
[20], nsha1
[20];
1123 char *email_end
, *message
;
1124 unsigned long timestamp
;
1127 /* old SP new SP name <email> SP time TAB msg LF */
1129 if (len
< 83 || buf
[len
-1] != '\n' ||
1130 get_sha1_hex(buf
, osha1
) || buf
[40] != ' ' ||
1131 get_sha1_hex(buf
+ 41, nsha1
) || buf
[81] != ' ' ||
1132 !(email_end
= strchr(buf
+ 82, '>')) ||
1133 email_end
[1] != ' ' ||
1134 !(timestamp
= strtoul(email_end
+ 2, &message
, 10)) ||
1135 !message
|| message
[0] != ' ' ||
1136 (message
[1] != '+' && message
[1] != '-') ||
1137 !isdigit(message
[2]) || !isdigit(message
[3]) ||
1138 !isdigit(message
[4]) || !isdigit(message
[5]) ||
1140 continue; /* corrupt? */
1141 email_end
[1] = '\0';
1142 tz
= strtol(message
+ 1, NULL
, 10);
1144 ret
= fn(osha1
, nsha1
, buf
+82, timestamp
, tz
, message
, cb_data
);