98
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;
101
148
static struct ref_list *get_packed_refs(void)
103
150
if (!cached_refs.did_packed) {
104
struct ref_list *refs = NULL;
105
151
FILE *f = fopen(git_path("packed-refs"), "r");
152
cached_refs.packed = NULL;
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);
154
read_packed_refs(f, &cached_refs);
119
cached_refs.packed = refs;
120
157
cached_refs.did_packed = 1;
122
159
return cached_refs.packed;
275
int create_symref(const char *ref_target, const char *refs_heads_master)
277
const char *lockpath;
279
int fd, len, written;
280
const char *git_HEAD = git_path("%s", ref_target);
282
#ifndef NO_SYMLINK_HEAD
283
if (prefer_symlink_refs) {
285
if (!symlink(refs_heads_master, git_HEAD))
287
fprintf(stderr, "no symlink - falling back to symbolic ref\n");
291
len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
292
if (sizeof(ref) <= len) {
293
error("refname too long: %s", refs_heads_master);
296
lockpath = mkpath("%s.lock", git_HEAD);
297
fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
298
written = write(fd, ref, len);
300
if (written != len) {
302
error("Unable to write to %s", lockpath);
305
if (rename(lockpath, git_HEAD) < 0) {
307
error("Unable to create %s", git_HEAD);
310
if (adjust_shared_perm(git_HEAD)) {
312
error("Unable to fix permissions on %s", lockpath);
318
312
int read_ref(const char *ref, unsigned char *sha1)
320
314
if (resolve_ref(ref, sha1, 1, NULL))
336
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);
339
370
static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
534
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'",
537
591
static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
567
621
orig_ref, strerror(errno));
568
622
goto error_return;
570
if (is_null_sha1(lock->old_sha1)) {
571
/* The ref did not exist and we are creating it.
572
* Make sure there is no existing ref that is packed
573
* whose name begins with our refname, nor a ref whose
574
* name is a proper prefix of our refname.
576
int namlen = strlen(ref); /* e.g. 'foo/bar' */
577
struct ref_list *list = get_packed_refs();
579
/* list->name could be 'foo' or 'foo/bar/baz' */
580
int len = strlen(list->name);
581
int cmplen = (namlen < len) ? namlen : len;
582
const char *lead = (namlen < len) ? list->name : ref;
584
if (!strncmp(ref, list->name, cmplen) &&
585
lead[cmplen] == '/') {
586
error("'%s' exists; cannot create '%s'",
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))
594
633
lock->lk = xcalloc(1, sizeof(struct lock_file));
596
635
lock->ref_name = xstrdup(ref);
597
lock->log_file = xstrdup(git_path("logs/%s", ref));
636
lock->orig_ref_name = xstrdup(orig_ref);
598
637
ref_file = git_path("%s", ref);
599
638
lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
692
732
ret |= repack_without_ref(refname);
694
err = unlink(lock->log_file);
734
err = unlink(git_path("logs/%s", lock->ref_name));
695
735
if (err && errno != ENOENT)
696
736
fprintf(stderr, "warning: unlink(%s) failed: %s",
697
lock->log_file, strerror(errno));
737
git_path("logs/%s", lock->ref_name), strerror(errno));
698
738
invalidate_cached_refs();
699
739
unlock_ref(lock);
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))) {
799
if (errno==EISDIR || errno==ENOTDIR) {
801
* rename(a, b) when b is an existing
802
* directory ought to result in ISDIR, but
803
* Solaris 5.8 gives ENOTDIR. Sheesh.
805
if (remove_empty_directories(git_path("logs/%s", newref))) {
806
error("Directory not empty: logs/%s", newref);
811
error("unable to move logfile tmp-renamed-log to logs/%s: %s",
812
newref, strerror(errno));
818
lock = lock_ref_sha1_basic(newref, NULL, NULL);
820
error("unable to lock %s for update", newref);
824
lock->force_write = 1;
825
hashcpy(lock->old_sha1, orig_sha1);
826
if (write_ref_sha1(lock, orig_sha1, logmsg)) {
827
error("unable to write current sha1 into %s", newref);
834
lock = lock_ref_sha1_basic(oldref, NULL, NULL);
836
error("unable to lock %s for rollback", oldref);
840
lock->force_write = 1;
841
flag = log_all_ref_updates;
842
log_all_ref_updates = 0;
843
if (write_ref_sha1(lock, orig_sha1, NULL))
844
error("unable to write current sha1 into %s", oldref);
845
log_all_ref_updates = flag;
848
if (logmoved && rename(git_path("logs/%s", newref), git_path("logs/%s", oldref)))
849
error("unable to restore logfile %s from %s: %s",
850
oldref, newref, strerror(errno));
851
if (!logmoved && log &&
852
rename(git_path("tmp-renamed-log"), git_path("logs/%s", oldref)))
853
error("unable to restore logfile %s from tmp-renamed-log: %s",
854
oldref, strerror(errno));
703
859
void unlock_ref(struct ref_lock *lock)
705
861
if (lock->lock_fd >= 0) {
709
865
rollback_lock_file(lock->lk);
711
867
free(lock->ref_name);
712
free(lock->log_file);
868
free(lock->orig_ref_name);
716
static int log_ref_write(struct ref_lock *lock,
717
const unsigned char *sha1, const char *msg)
872
static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
873
const unsigned char *new_sha1, const char *msg)
719
875
int logfd, written, oflags = O_APPEND | O_WRONLY;
720
876
unsigned maxlen, len;
878
char *log_file, *logrec;
722
879
const char *committer;
881
if (log_all_ref_updates < 0)
882
log_all_ref_updates = !is_bare_repository();
884
log_file = git_path("logs/%s", ref_name);
724
886
if (log_all_ref_updates &&
725
!strncmp(lock->ref_name, "refs/heads/", 11)) {
726
if (safe_create_leading_directories(lock->log_file) < 0)
887
(!prefixcmp(ref_name, "refs/heads/") ||
888
!prefixcmp(ref_name, "refs/remotes/") ||
889
!strcmp(ref_name, "HEAD"))) {
890
if (safe_create_leading_directories(log_file) < 0)
727
891
return error("unable to create directory for %s",
729
893
oflags |= O_CREAT;
732
logfd = open(lock->log_file, oflags, 0666);
896
logfd = open(log_file, oflags, 0666);
734
898
if (!(oflags & O_CREAT) && errno == ENOENT)
737
901
if ((oflags & O_CREAT) && errno == EISDIR) {
738
if (remove_empty_directories(lock->log_file)) {
902
if (remove_empty_directories(log_file)) {
739
903
return error("There are still logs under '%s'",
742
logfd = open(lock->log_file, oflags, 0666);
906
logfd = open(log_file, oflags, 0666);
746
910
return error("Unable to append to %s: %s",
747
lock->log_file, strerror(errno));
911
log_file, strerror(errno));
750
committer = git_committer_info(1);
914
adjust_shared_perm(log_file);
752
maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
753
logrec = xmalloc(maxlen);
754
len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
755
sha1_to_hex(lock->old_sha1),
761
maxlen = strlen(committer) + 2*40 + 4;
762
logrec = xmalloc(maxlen);
763
len = snprintf(logrec, maxlen, "%s %s %s\n",
764
sha1_to_hex(lock->old_sha1),
768
written = len <= maxlen ? write(logfd, logrec, len) : -1;
918
/* clean up the message and make sure it is a single line */
923
const char *ep = strchr(msg, '\n');
927
msglen = strlen(msg);
931
committer = git_committer_info(-1);
932
maxlen = strlen(committer) + msglen + 100;
933
logrec = xmalloc(maxlen);
934
len = sprintf(logrec, "%s %s %s\n",
935
sha1_to_hex(old_sha1),
936
sha1_to_hex(new_sha1),
939
len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
940
written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
771
943
if (written != len)
772
return error("Unable to append to %s", lock->log_file);
944
return error("Unable to append to %s", log_file);
784
956
unlock_ref(lock);
787
if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
788
write(lock->lock_fd, &term, 1) != 1
959
if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
960
write_in_full(lock->lock_fd, &term, 1) != 1
789
961
|| close(lock->lock_fd) < 0) {
790
962
error("Couldn't write %s", lock->lk->filename);
791
963
unlock_ref(lock);
794
966
invalidate_cached_refs();
795
if (log_ref_write(lock, sha1, logmsg) < 0) {
967
if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
968
(strcmp(lock->ref_name, lock->orig_ref_name) &&
969
log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
796
970
unlock_ref(lock);
973
if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
975
* Special hack: If a branch is updated directly and HEAD
976
* points to it (may happen on the remote side of a push
977
* for example) then logically the HEAD reflog should be
979
* A generic solution implies reverse symref information,
980
* but finding all symrefs pointing to the given branch
981
* would be rather costly for this rare event (the direct
982
* update of a branch) to be worth it. So let's cheat and
983
* check with HEAD only which should cover 99% of all usage
984
* scenarios (even 100% of the default ones).
986
unsigned char head_sha1[20];
988
const char *head_ref;
989
head_ref = resolve_ref("HEAD", head_sha1, 1, &head_flag);
990
if (head_ref && (head_flag & REF_ISSYMREF) &&
991
!strcmp(head_ref, lock->ref_name))
992
log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
799
994
if (commit_lock_file(lock->lk)) {
800
995
error("Couldn't set %s", lock->ref_name);
801
996
unlock_ref(lock);
809
int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1)
1004
int create_symref(const char *ref_target, const char *refs_heads_master,
1007
const char *lockpath;
1009
int fd, len, written;
1010
char *git_HEAD = xstrdup(git_path("%s", ref_target));
1011
unsigned char old_sha1[20], new_sha1[20];
1013
if (logmsg && read_ref(ref_target, old_sha1))
1016
if (safe_create_leading_directories(git_HEAD) < 0)
1017
return error("unable to create directory for %s", git_HEAD);
1019
#ifndef NO_SYMLINK_HEAD
1020
if (prefer_symlink_refs) {
1022
if (!symlink(refs_heads_master, git_HEAD))
1024
fprintf(stderr, "no symlink - falling back to symbolic ref\n");
1028
len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
1029
if (sizeof(ref) <= len) {
1030
error("refname too long: %s", refs_heads_master);
1031
goto error_free_return;
1033
lockpath = mkpath("%s.lock", git_HEAD);
1034
fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
1036
error("Unable to open %s for writing", lockpath);
1037
goto error_free_return;
1039
written = write_in_full(fd, ref, len);
1041
if (written != len) {
1042
error("Unable to write to %s", lockpath);
1043
goto error_unlink_return;
1045
if (rename(lockpath, git_HEAD) < 0) {
1046
error("Unable to create %s", git_HEAD);
1047
goto error_unlink_return;
1049
if (adjust_shared_perm(git_HEAD)) {
1050
error("Unable to fix permissions on %s", lockpath);
1051
error_unlink_return:
1058
#ifndef NO_SYMLINK_HEAD
1061
if (logmsg && !read_ref(refs_heads_master, new_sha1))
1062
log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
1068
static char *ref_msg(const char *line, const char *endp)
1074
for (ep = line; ep < endp && *ep != '\n'; ep++)
1076
msg = xmalloc(ep - line + 1);
1077
memcpy(msg, line, ep - line);
1082
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)
811
1084
const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
1086
int logfd, tz, reccnt = 0;
815
1088
unsigned long date;
816
1089
unsigned char logged_sha1[20];
818
1093
logfile = git_path("logs/%s", ref);
819
1094
logfd = open(logfile, O_RDONLY, 0);
840
1118
die("Log %s is corrupt.", logfile);
841
1119
date = strtoul(lastgt + 1, &tz_c, 10);
842
1120
if (date <= at_time || cnt == 0) {
1121
tz = strtoul(tz_c, NULL, 10);
1123
*msg = ref_msg(rec, logend);
1125
*cutoff_time = date;
1129
*cutoff_cnt = reccnt - 1;
844
1131
if (get_sha1_hex(lastrec, logged_sha1))
845
1132
die("Log %s is corrupt.", logfile);
846
1133
if (get_sha1_hex(rec + 41, sha1))
847
1134
die("Log %s is corrupt.", logfile);
848
1135
if (hashcmp(logged_sha1, sha1)) {
849
tz = strtoul(tz_c, NULL, 10);
851
1137
"warning: Log %s has gap after %s.\n",
852
1138
logfile, show_rfc2822_date(date, tz));
883
1168
tz = strtoul(tz_c, NULL, 10);
884
1169
if (get_sha1_hex(logdata, sha1))
885
1170
die("Log %s is corrupt.", logfile);
886
munmap((void*)logdata, st.st_size);
887
fprintf(stderr, "warning: Log %s only goes back to %s.\n",
888
logfile, show_rfc2822_date(date, tz));
1172
*msg = ref_msg(logdata, logend);
1173
munmap(log_mapped, mapsz);
1176
*cutoff_time = date;
1180
*cutoff_cnt = reccnt;
1184
int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
1186
const char *logfile;
1191
logfile = git_path("logs/%s", ref);
1192
logfp = fopen(logfile, "r");
1195
while (fgets(buf, sizeof(buf), logfp)) {
1196
unsigned char osha1[20], nsha1[20];
1197
char *email_end, *message;
1198
unsigned long timestamp;
1201
/* old SP new SP name <email> SP time TAB msg LF */
1203
if (len < 83 || buf[len-1] != '\n' ||
1204
get_sha1_hex(buf, osha1) || buf[40] != ' ' ||
1205
get_sha1_hex(buf + 41, nsha1) || buf[81] != ' ' ||
1206
!(email_end = strchr(buf + 82, '>')) ||
1207
email_end[1] != ' ' ||
1208
!(timestamp = strtoul(email_end + 2, &message, 10)) ||
1209
!message || message[0] != ' ' ||
1210
(message[1] != '+' && message[1] != '-') ||
1211
!isdigit(message[2]) || !isdigit(message[3]) ||
1212
!isdigit(message[4]) || !isdigit(message[5]))
1213
continue; /* corrupt? */
1214
email_end[1] = '\0';
1215
tz = strtol(message + 1, NULL, 10);
1216
if (message[6] != '\t')
1220
ret = fn(osha1, nsha1, buf+82, timestamp, tz, message, cb_data);
1228
static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
1230
DIR *dir = opendir(git_path("logs/%s", base));
1235
int baselen = strlen(base);
1236
char *log = xmalloc(baselen + 257);
1238
memcpy(log, base, baselen);
1239
if (baselen && base[baselen-1] != '/')
1240
log[baselen++] = '/';
1242
while ((de = readdir(dir)) != NULL) {
1246
if (de->d_name[0] == '.')
1248
namelen = strlen(de->d_name);
1251
if (has_extension(de->d_name, ".lock"))
1253
memcpy(log + baselen, de->d_name, namelen+1);
1254
if (stat(git_path("logs/%s", log), &st) < 0)
1256
if (S_ISDIR(st.st_mode)) {
1257
retval = do_for_each_reflog(log, fn, cb_data);
1259
unsigned char sha1[20];
1260
if (!resolve_ref(log, sha1, 0, NULL))
1261
retval = error("bad ref for %s", log);
1263
retval = fn(log, sha1, 0, cb_data);
1276
int for_each_reflog(each_ref_fn fn, void *cb_data)
1278
return do_for_each_reflog("", fn, cb_data);