Replace "echo -n" with printf in shell scripts.
[git/git.git] / refs.c
CommitLineData
95fc7512 1#include "cache.h"
85023577 2#include "refs.h"
cf0adba7
JH
3#include "object.h"
4#include "tag.h"
95fc7512 5
f4204ab9
JH
6/* ISSYMREF=01 and ISPACKED=02 are public interfaces */
7#define REF_KNOWS_PEELED 04
8
e1e22e37
LT
9struct ref_list {
10 struct ref_list *next;
f4204ab9 11 unsigned char flag; /* ISSYMREF? ISPACKED? */
e1e22e37 12 unsigned char sha1[20];
f4204ab9 13 unsigned char peeled[20];
e1e22e37
LT
14 char name[FLEX_ARRAY];
15};
16
f4204ab9 17static const char *parse_ref_line(char *line, unsigned char *sha1)
e1e22e37
LT
18{
19 /*
20 * 42: the answer to everything.
21 *
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)
26 */
27 int len = strlen(line) - 42;
28
29 if (len <= 0)
30 return NULL;
31 if (get_sha1_hex(line, sha1) < 0)
32 return NULL;
33 if (!isspace(line[40]))
34 return NULL;
35 line += 41;
f4204ab9
JH
36 if (isspace(*line))
37 return NULL;
e1e22e37
LT
38 if (line[len] != '\n')
39 return NULL;
40 line[len] = 0;
cf0adba7 41
e1e22e37
LT
42 return line;
43}
44
8da19775 45static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
f4204ab9
JH
46 int flag, struct ref_list *list,
47 struct ref_list **new_entry)
e1e22e37
LT
48{
49 int len;
50 struct ref_list **p = &list, *entry;
51
52 /* Find the place to insert the ref into.. */
53 while ((entry = *p) != NULL) {
54 int cmp = strcmp(entry->name, name);
55 if (cmp > 0)
56 break;
57
58 /* Same as existing entry? */
f4204ab9
JH
59 if (!cmp) {
60 if (new_entry)
61 *new_entry = entry;
e1e22e37 62 return list;
f4204ab9 63 }
e1e22e37
LT
64 p = &entry->next;
65 }
66
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);
f4204ab9 71 hashclr(entry->peeled);
e1e22e37 72 memcpy(entry->name, name, len);
8da19775 73 entry->flag = flag;
e1e22e37
LT
74 entry->next = *p;
75 *p = entry;
f4204ab9
JH
76 if (new_entry)
77 *new_entry = entry;
e1e22e37
LT
78 return list;
79}
80
5e290ff7
JH
81/*
82 * Future: need to be in "struct repository"
83 * when doing a full libification.
84 */
85struct cached_refs {
86 char did_loose;
87 char did_packed;
88 struct ref_list *loose;
89 struct ref_list *packed;
90} cached_refs;
91
92static void free_ref_list(struct ref_list *list)
93{
94 struct ref_list *next;
95 for ( ; list; list = next) {
96 next = list->next;
97 free(list);
98 }
99}
100
101static void invalidate_cached_refs(void)
e1e22e37 102{
5e290ff7
JH
103 struct cached_refs *ca = &cached_refs;
104
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;
111}
e1e22e37 112
f4204ab9
JH
113static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
114{
115 struct ref_list *list = NULL;
116 struct ref_list *last = NULL;
117 char refline[PATH_MAX];
118 int flag = REF_ISPACKED;
119
120 while (fgets(refline, sizeof(refline), f)) {
121 unsigned char sha1[20];
122 const char *name;
123 static const char header[] = "# pack-refs with:";
124
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 */
130 continue;
131 }
132
133 name = parse_ref_line(refline, sha1);
134 if (name) {
135 list = add_ref(name, sha1, flag, list, &last);
136 continue;
137 }
138 if (last &&
139 refline[0] == '^' &&
140 strlen(refline) == 42 &&
141 refline[41] == '\n' &&
142 !get_sha1_hex(refline + 1, sha1))
143 hashcpy(last->peeled, sha1);
144 }
145 cached_refs->packed = list;
146}
147
5e290ff7
JH
148static struct ref_list *get_packed_refs(void)
149{
150 if (!cached_refs.did_packed) {
e1e22e37 151 FILE *f = fopen(git_path("packed-refs"), "r");
f4204ab9 152 cached_refs.packed = NULL;
e1e22e37 153 if (f) {
f4204ab9 154 read_packed_refs(f, &cached_refs);
e1e22e37 155 fclose(f);
e1e22e37 156 }
5e290ff7 157 cached_refs.did_packed = 1;
e1e22e37 158 }
5e290ff7 159 return cached_refs.packed;
e1e22e37
LT
160}
161
162static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
163{
164 DIR *dir = opendir(git_path("%s", base));
165
166 if (dir) {
167 struct dirent *de;
168 int baselen = strlen(base);
ed378ec7 169 char *ref = xmalloc(baselen + 257);
e1e22e37 170
ed378ec7 171 memcpy(ref, base, baselen);
e1e22e37 172 if (baselen && base[baselen-1] != '/')
ed378ec7 173 ref[baselen++] = '/';
e1e22e37
LT
174
175 while ((de = readdir(dir)) != NULL) {
176 unsigned char sha1[20];
177 struct stat st;
8da19775 178 int flag;
e1e22e37
LT
179 int namelen;
180
181 if (de->d_name[0] == '.')
182 continue;
183 namelen = strlen(de->d_name);
184 if (namelen > 255)
185 continue;
186 if (has_extension(de->d_name, ".lock"))
187 continue;
ed378ec7
LT
188 memcpy(ref + baselen, de->d_name, namelen+1);
189 if (stat(git_path("%s", ref), &st) < 0)
e1e22e37
LT
190 continue;
191 if (S_ISDIR(st.st_mode)) {
ed378ec7 192 list = get_ref_dir(ref, list);
e1e22e37
LT
193 continue;
194 }
8da19775 195 if (!resolve_ref(ref, sha1, 1, &flag)) {
ed378ec7 196 error("%s points nowhere!", ref);
e1e22e37
LT
197 continue;
198 }
f4204ab9 199 list = add_ref(ref, sha1, flag, list, NULL);
e1e22e37 200 }
ed378ec7 201 free(ref);
e1e22e37
LT
202 closedir(dir);
203 }
204 return list;
205}
206
207static struct ref_list *get_loose_refs(void)
208{
5e290ff7
JH
209 if (!cached_refs.did_loose) {
210 cached_refs.loose = get_ref_dir("refs", NULL);
211 cached_refs.did_loose = 1;
e1e22e37 212 }
5e290ff7 213 return cached_refs.loose;
e1e22e37
LT
214}
215
ca8db142
LT
216/* We allow "recursive" symbolic refs. Only within reason, though */
217#define MAXDEPTH 5
218
8da19775 219const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
8a65ff76 220{
a876ed83
JH
221 int depth = MAXDEPTH, len;
222 char buffer[256];
ed378ec7 223 static char ref_buffer[256];
ca8db142 224
8da19775
JH
225 if (flag)
226 *flag = 0;
227
a876ed83 228 for (;;) {
ed378ec7 229 const char *path = git_path("%s", ref);
a876ed83
JH
230 struct stat st;
231 char *buf;
232 int fd;
8a65ff76 233
a876ed83
JH
234 if (--depth < 0)
235 return NULL;
ca8db142 236
a876ed83
JH
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
242 * reading.
243 */
244 if (lstat(path, &st) < 0) {
434cd0cd
LT
245 struct ref_list *list = get_packed_refs();
246 while (list) {
f4204ab9 247 if (!strcmp(ref, list->name)) {
434cd0cd 248 hashcpy(sha1, list->sha1);
8da19775
JH
249 if (flag)
250 *flag |= REF_ISPACKED;
434cd0cd
LT
251 return ref;
252 }
253 list = list->next;
254 }
a876ed83
JH
255 if (reading || errno != ENOENT)
256 return NULL;
e702496e 257 hashclr(sha1);
ed378ec7 258 return ref;
a876ed83 259 }
ca8db142 260
a876ed83
JH
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)) {
ed378ec7
LT
265 buffer[len] = 0;
266 strcpy(ref_buffer, buffer);
267 ref = ref_buffer;
8da19775
JH
268 if (flag)
269 *flag |= REF_ISSYMREF;
a876ed83
JH
270 continue;
271 }
ca8db142 272 }
a876ed83 273
7a21632f
DS
274 /* Is it a directory? */
275 if (S_ISDIR(st.st_mode)) {
276 errno = EISDIR;
277 return NULL;
278 }
279
a876ed83
JH
280 /*
281 * Anything else, just open it and try to use it as
282 * a ref
283 */
284 fd = open(path, O_RDONLY);
285 if (fd < 0)
286 return NULL;
93d26e4c 287 len = read_in_full(fd, buffer, sizeof(buffer)-1);
a876ed83
JH
288 close(fd);
289
290 /*
291 * Is it a symbolic ref?
292 */
293 if (len < 4 || memcmp("ref:", buffer, 4))
294 break;
295 buf = buffer + 4;
296 len -= 4;
297 while (len && isspace(*buf))
298 buf++, len--;
299 while (len && isspace(buf[len-1]))
ed378ec7
LT
300 len--;
301 buf[len] = 0;
302 memcpy(ref_buffer, buf, len + 1);
303 ref = ref_buffer;
8da19775
JH
304 if (flag)
305 *flag |= REF_ISSYMREF;
8a65ff76 306 }
a876ed83
JH
307 if (len < 40 || get_sha1_hex(buffer, sha1))
308 return NULL;
ed378ec7 309 return ref;
a876ed83
JH
310}
311
ed378ec7 312int create_symref(const char *ref_target, const char *refs_heads_master)
8098a178 313{
8098a178
JH
314 const char *lockpath;
315 char ref[1000];
316 int fd, len, written;
ed378ec7 317 const char *git_HEAD = git_path("%s", ref_target);
8098a178 318
9f0bb90d
JH
319#ifndef NO_SYMLINK_HEAD
320 if (prefer_symlink_refs) {
f8348be3
JS
321 unlink(git_HEAD);
322 if (!symlink(refs_heads_master, git_HEAD))
323 return 0;
324 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
325 }
303958dc
JS
326#endif
327
8098a178
JH
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);
331 return -1;
332 }
333 lockpath = mkpath("%s.lock", git_HEAD);
334 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
93822c22 335 written = write_in_full(fd, ref, len);
8098a178
JH
336 close(fd);
337 if (written != len) {
338 unlink(lockpath);
339 error("Unable to write to %s", lockpath);
340 return -2;
341 }
342 if (rename(lockpath, git_HEAD) < 0) {
343 unlink(lockpath);
344 error("Unable to create %s", git_HEAD);
345 return -3;
346 }
138086a7
JH
347 if (adjust_shared_perm(git_HEAD)) {
348 unlink(lockpath);
349 error("Unable to fix permissions on %s", lockpath);
350 return -4;
351 }
8098a178 352 return 0;
8098a178
JH
353}
354
ed378ec7 355int read_ref(const char *ref, unsigned char *sha1)
a876ed83 356{
8da19775 357 if (resolve_ref(ref, sha1, 1, NULL))
a876ed83
JH
358 return 0;
359 return -1;
8a65ff76
LT
360}
361
ef06b918
JH
362static int do_one_ref(const char *base, each_ref_fn fn, int trim,
363 void *cb_data, struct ref_list *entry)
364{
365 if (strncmp(base, entry->name, trim))
366 return 0;
367 if (is_null_sha1(entry->sha1))
368 return 0;
369 if (!has_sha1_file(entry->sha1)) {
370 error("%s does not point to a valid object!", entry->name);
371 return 0;
372 }
373 return fn(entry->name + trim, entry->sha1, entry->flag, cb_data);
374}
375
cf0adba7
JH
376int peel_ref(const char *ref, unsigned char *sha1)
377{
378 int flag;
379 unsigned char base[20];
380 struct object *o;
381
382 if (!resolve_ref(ref, base, 1, &flag))
383 return -1;
384
385 if ((flag & REF_ISPACKED)) {
386 struct ref_list *list = get_packed_refs();
cf0adba7
JH
387
388 while (list) {
f4204ab9
JH
389 if (!strcmp(list->name, ref)) {
390 if (list->flag & REF_KNOWS_PEELED) {
391 hashcpy(sha1, list->peeled);
392 return 0;
393 }
394 /* older pack-refs did not leave peeled ones */
395 break;
cf0adba7
JH
396 }
397 list = list->next;
398 }
cf0adba7
JH
399 }
400
f4204ab9 401 /* fallback - callers should not call this for unpacked refs */
cf0adba7
JH
402 o = parse_object(base);
403 if (o->type == OBJ_TAG) {
404 o = deref_tag(o, ref, 0);
405 if (o) {
406 hashcpy(sha1, o->sha1);
407 return 0;
408 }
409 }
410 return -1;
411}
412
8da19775
JH
413static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
414 void *cb_data)
8a65ff76 415{
e1e22e37
LT
416 int retval;
417 struct ref_list *packed = get_packed_refs();
418 struct ref_list *loose = get_loose_refs();
8a65ff76 419
e1e22e37
LT
420 while (packed && loose) {
421 struct ref_list *entry;
422 int cmp = strcmp(packed->name, loose->name);
423 if (!cmp) {
424 packed = packed->next;
425 continue;
6cada6a9 426 }
e1e22e37
LT
427 if (cmp > 0) {
428 entry = loose;
429 loose = loose->next;
430 } else {
431 entry = packed;
432 packed = packed->next;
433 }
ef06b918 434 retval = do_one_ref(base, fn, trim, cb_data, entry);
e1e22e37
LT
435 if (retval)
436 return retval;
437 }
8a65ff76 438
ef06b918
JH
439 for (packed = packed ? packed : loose; packed; packed = packed->next) {
440 retval = do_one_ref(base, fn, trim, cb_data, packed);
441 if (retval)
442 return retval;
8a65ff76 443 }
e1e22e37 444 return 0;
8a65ff76
LT
445}
446
cb5d709f 447int head_ref(each_ref_fn fn, void *cb_data)
723c31fe
LT
448{
449 unsigned char sha1[20];
8da19775
JH
450 int flag;
451
452 if (resolve_ref("HEAD", sha1, 1, &flag))
453 return fn("HEAD", sha1, flag, cb_data);
2f34ba32 454 return 0;
723c31fe
LT
455}
456
cb5d709f 457int for_each_ref(each_ref_fn fn, void *cb_data)
8a65ff76 458{
cb5d709f 459 return do_for_each_ref("refs/", fn, 0, cb_data);
a62be77f
SE
460}
461
cb5d709f 462int for_each_tag_ref(each_ref_fn fn, void *cb_data)
a62be77f 463{
cb5d709f 464 return do_for_each_ref("refs/tags/", fn, 10, cb_data);
a62be77f
SE
465}
466
cb5d709f 467int for_each_branch_ref(each_ref_fn fn, void *cb_data)
a62be77f 468{
cb5d709f 469 return do_for_each_ref("refs/heads/", fn, 11, cb_data);
a62be77f
SE
470}
471
cb5d709f 472int for_each_remote_ref(each_ref_fn fn, void *cb_data)
a62be77f 473{
cb5d709f 474 return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
8a65ff76
LT
475}
476
cb5d709f
JH
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
480 * read_ref().
481 */
95fc7512
DB
482int get_ref_sha1(const char *ref, unsigned char *sha1)
483{
95fc7512
DB
484 if (check_ref_format(ref))
485 return -1;
ed378ec7 486 return read_ref(mkpath("refs/%s", ref), sha1);
95fc7512
DB
487}
488
03feddd6
JH
489/*
490 * Make sure "ref" is something reasonable to have under ".git/refs/";
491 * We do not like it if:
492 *
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 "/".
497 */
498
499static inline int bad_ref_char(int ch)
500{
501 return (((unsigned) ch) <= ' ' ||
68283999
JH
502 ch == '~' || ch == '^' || ch == ':' ||
503 /* 2.13 Pattern Matching Notation */
504 ch == '?' || ch == '*' || ch == '[');
03feddd6
JH
505}
506
95fc7512
DB
507int check_ref_format(const char *ref)
508{
03feddd6
JH
509 int ch, level;
510 const char *cp = ref;
511
512 level = 0;
513 while (1) {
514 while ((ch = *cp++) == '/')
515 ; /* tolerate duplicated slashes */
516 if (!ch)
517 return -1; /* should not end with slashes */
518
519 /* we are at the beginning of the path component */
520 if (ch == '.' || bad_ref_char(ch))
521 return -1;
522
523 /* scan the rest of the path component */
524 while ((ch = *cp++) != 0) {
525 if (bad_ref_char(ch))
526 return -1;
527 if (ch == '/')
528 break;
529 if (ch == '.' && *cp == '.')
530 return -1;
531 }
532 level++;
533 if (!ch) {
534 if (level < 2)
37adac76 535 return -2; /* at least of form "heads/blah" */
03feddd6
JH
536 return 0;
537 }
538 }
95fc7512
DB
539}
540
e5f38ec3 541static struct ref_lock *verify_lock(struct ref_lock *lock,
4bd18c43
SP
542 const unsigned char *old_sha1, int mustexist)
543{
8da19775 544 if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
434cd0cd 545 error("Can't verify ref %s", lock->ref_name);
4bd18c43
SP
546 unlock_ref(lock);
547 return NULL;
548 }
a89fccd2 549 if (hashcmp(lock->old_sha1, old_sha1)) {
434cd0cd 550 error("Ref %s is at %s but expected %s", lock->ref_name,
4bd18c43
SP
551 sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
552 unlock_ref(lock);
553 return NULL;
554 }
555 return lock;
556}
557
bc7127ef
JH
558static int remove_empty_dir_recursive(char *path, int len)
559{
560 DIR *dir = opendir(path);
561 struct dirent *e;
562 int ret = 0;
563
564 if (!dir)
565 return -1;
566 if (path[len-1] != '/')
567 path[len++] = '/';
568 while ((e = readdir(dir)) != NULL) {
569 struct stat st;
570 int namlen;
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 ".." */
575
576 namlen = strlen(e->d_name);
577 if ((len + namlen < PATH_MAX) &&
578 strcpy(path + len, e->d_name) &&
579 !lstat(path, &st) &&
580 S_ISDIR(st.st_mode) &&
28bed6ea 581 !remove_empty_dir_recursive(path, len + namlen))
bc7127ef
JH
582 continue; /* happy */
583
584 /* path too long, stat fails, or non-directory still exists */
585 ret = -1;
586 break;
587 }
588 closedir(dir);
589 if (!ret) {
590 path[len] = 0;
591 ret = rmdir(path);
592 }
593 return ret;
594}
595
596static int remove_empty_directories(char *file)
597{
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.
601 */
602 char path[PATH_MAX];
603 int len = strlen(file);
604
605 if (len >= PATH_MAX) /* path too long ;-) */
606 return -1;
607 strcpy(path, file);
608 return remove_empty_dir_recursive(path, len);
609}
610
c976d415
LH
611static int is_refname_available(const char *ref, const char *oldref,
612 struct ref_list *list, int quiet)
613{
614 int namlen = strlen(ref); /* e.g. 'foo/bar' */
615 while (list) {
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] == '/') {
623 if (!quiet)
624 error("'%s' exists; cannot create '%s'",
625 list->name, ref);
626 return 0;
627 }
628 }
629 list = list->next;
630 }
631 return 1;
632}
633
c0277d15 634static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
4bd18c43 635{
434cd0cd 636 char *ref_file;
ed378ec7 637 const char *orig_ref = ref;
4bd18c43 638 struct ref_lock *lock;
732232a1 639 struct stat st;
5cc3cef9 640 int last_errno = 0;
4431fcc4 641 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
4bd18c43
SP
642
643 lock = xcalloc(1, sizeof(struct ref_lock));
644 lock->lock_fd = -1;
645
c0277d15 646 ref = resolve_ref(ref, lock->old_sha1, mustexist, flag);
bc7127ef
JH
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'
651 * to remain.
652 */
653 ref_file = git_path("%s", orig_ref);
5cc3cef9
JH
654 if (remove_empty_directories(ref_file)) {
655 last_errno = errno;
656 error("there are still refs under '%s'", orig_ref);
657 goto error_return;
658 }
c0277d15 659 ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, flag);
bc7127ef 660 }
ed378ec7 661 if (!ref) {
5cc3cef9 662 last_errno = errno;
818f477c 663 error("unable to resolve reference %s: %s",
ed378ec7 664 orig_ref, strerror(errno));
5cc3cef9 665 goto error_return;
4bd18c43 666 }
c976d415
LH
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.
671 */
672 if (is_null_sha1(lock->old_sha1) &&
673 !is_refname_available(ref, NULL, get_packed_refs(), 0))
674 goto error_return;
22a3844e 675
c33d5174 676 lock->lk = xcalloc(1, sizeof(struct lock_file));
4bd18c43 677
434cd0cd 678 lock->ref_name = xstrdup(ref);
ed378ec7 679 lock->log_file = xstrdup(git_path("logs/%s", ref));
7c1a278d 680 ref_file = git_path("%s", ref);
434cd0cd 681 lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
4bd18c43 682
5cc3cef9
JH
683 if (safe_create_leading_directories(ref_file)) {
684 last_errno = errno;
685 error("unable to create directory for %s", ref_file);
686 goto error_return;
687 }
434cd0cd 688 lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
4bd18c43
SP
689
690 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
5cc3cef9
JH
691
692 error_return:
693 unlock_ref(lock);
694 errno = last_errno;
695 return NULL;
4bd18c43
SP
696}
697
4431fcc4 698struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
95fc7512 699{
53cce84c 700 char refpath[PATH_MAX];
95fc7512 701 if (check_ref_format(ref))
4bd18c43 702 return NULL;
53cce84c 703 strcpy(refpath, mkpath("refs/%s", ref));
c0277d15 704 return lock_ref_sha1_basic(refpath, old_sha1, NULL);
4bd18c43
SP
705}
706
4431fcc4 707struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1)
4bd18c43 708{
c0277d15
JH
709 return lock_ref_sha1_basic(ref, old_sha1, NULL);
710}
711
26a063a1
JH
712static struct lock_file packlock;
713
c0277d15
JH
714static int repack_without_ref(const char *refname)
715{
716 struct ref_list *list, *packed_ref_list;
717 int fd;
718 int found = 0;
c0277d15
JH
719
720 packed_ref_list = get_packed_refs();
721 for (list = packed_ref_list; list; list = list->next) {
722 if (!strcmp(refname, list->name)) {
723 found = 1;
724 break;
725 }
726 }
727 if (!found)
728 return 0;
c0277d15
JH
729 fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0);
730 if (fd < 0)
731 return error("cannot delete '%s' from packed refs", refname);
732
733 for (list = packed_ref_list; list; list = list->next) {
734 char line[PATH_MAX + 100];
735 int len;
736
737 if (!strcmp(refname, list->name))
738 continue;
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);
745 }
746 return commit_lock_file(&packlock);
747}
748
749int delete_ref(const char *refname, unsigned char *sha1)
750{
751 struct ref_lock *lock;
752 int err, i, ret = 0, flag = 0;
753
754 lock = lock_ref_sha1_basic(refname, sha1, &flag);
755 if (!lock)
756 return 1;
757 if (!(flag & REF_ISPACKED)) {
758 /* loose */
759 i = strlen(lock->lk->filename) - 5; /* .lock */
760 lock->lk->filename[i] = 0;
761 err = unlink(lock->lk->filename);
762 if (err) {
763 ret = 1;
764 error("unlink(%s) failed: %s",
765 lock->lk->filename, strerror(errno));
766 }
767 lock->lk->filename[i] = '.';
768 }
769 /* removing the loose one could have resurrected an earlier
770 * packed one. Also, if it was not loose we need to repack
771 * without it.
772 */
773 ret |= repack_without_ref(refname);
774
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();
780 unlock_ref(lock);
781 return ret;
4bd18c43
SP
782}
783
678d0f4c 784int rename_ref(const char *oldref, const char *newref, const char *logmsg)
c976d415
LH
785{
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;
c976d415 790 struct stat loginfo;
16c2bfbb 791 int log = !lstat(git_path("logs/%s", oldref), &loginfo);
c976d415
LH
792
793 if (S_ISLNK(loginfo.st_mode))
794 return error("reflog for %s is a symlink", oldref);
795
796 if (!resolve_ref(oldref, orig_sha1, 1, &flag))
797 return error("refname %s not found", oldref);
798
799 if (!is_refname_available(newref, oldref, get_packed_refs(), 0))
800 return 1;
801
802 if (!is_refname_available(newref, oldref, get_loose_refs(), 0))
803 return 1;
804
c976d415
LH
805 lock = lock_ref_sha1_basic(renamed_ref, NULL, NULL);
806 if (!lock)
807 return error("unable to lock %s", renamed_ref);
808 lock->force_write = 1;
678d0f4c 809 if (write_ref_sha1(lock, orig_sha1, logmsg))
c976d415
LH
810 return error("unable to save current sha1 in %s", renamed_ref);
811
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));
815
816 if (delete_ref(oldref, orig_sha1)) {
817 error("unable to delete old %s", oldref);
818 goto rollback;
819 }
820
821 if (resolve_ref(newref, sha1, 1, &flag) && delete_ref(newref, sha1)) {
822 if (errno==EISDIR) {
823 if (remove_empty_directories(git_path("%s", newref))) {
824 error("Directory not empty: %s", newref);
825 goto rollback;
826 }
827 } else {
828 error("unable to delete existing %s", newref);
829 goto rollback;
830 }
831 }
832
833 if (log && safe_create_leading_directories(git_path("logs/%s", newref))) {
834 error("unable to create directory for %s", newref);
835 goto rollback;
836 }
837
838 retry:
839 if (log && rename(git_path("tmp-renamed-log"), git_path("logs/%s", newref))) {
840 if (errno==EISDIR) {
841 if (remove_empty_directories(git_path("logs/%s", newref))) {
842 error("Directory not empty: logs/%s", newref);
843 goto rollback;
844 }
845 goto retry;
846 } else {
847 error("unable to move logfile tmp-renamed-log to logs/%s: %s",
848 newref, strerror(errno));
849 goto rollback;
850 }
851 }
852 logmoved = log;
853
854 lock = lock_ref_sha1_basic(newref, NULL, NULL);
855 if (!lock) {
856 error("unable to lock %s for update", newref);
857 goto rollback;
858 }
859
860 lock->force_write = 1;
861 hashcpy(lock->old_sha1, orig_sha1);
678d0f4c 862 if (write_ref_sha1(lock, orig_sha1, logmsg)) {
c976d415
LH
863 error("unable to write current sha1 into %s", newref);
864 goto rollback;
865 }
866
dc81c58c
JS
867 if (!strncmp(oldref, "refs/heads/", 11) &&
868 !strncmp(newref, "refs/heads/", 11)) {
869 char oldsection[1024], newsection[1024];
870
871 snprintf(oldsection, 1024, "branch.%s", oldref + 11);
872 snprintf(newsection, 1024, "branch.%s", newref + 11);
873 if (git_config_rename_section(oldsection, newsection) < 0)
874 return 1;
875 }
876
c976d415
LH
877 return 0;
878
879 rollback:
880 lock = lock_ref_sha1_basic(oldref, NULL, NULL);
881 if (!lock) {
882 error("unable to lock %s for rollback", oldref);
883 goto rollbacklog;
884 }
885
886 lock->force_write = 1;
887 flag = log_all_ref_updates;
888 log_all_ref_updates = 0;
889 if (write_ref_sha1(lock, orig_sha1, NULL))
890 error("unable to write current sha1 into %s", oldref);
891 log_all_ref_updates = flag;
892
893 rollbacklog:
894 if (logmoved && rename(git_path("logs/%s", newref), git_path("logs/%s", oldref)))
895 error("unable to restore logfile %s from %s: %s",
896 oldref, newref, strerror(errno));
897 if (!logmoved && log &&
898 rename(git_path("tmp-renamed-log"), git_path("logs/%s", oldref)))
899 error("unable to restore logfile %s from tmp-renamed-log: %s",
900 oldref, strerror(errno));
901
902 return 1;
903}
904
e5f38ec3 905void unlock_ref(struct ref_lock *lock)
4bd18c43
SP
906{
907 if (lock->lock_fd >= 0) {
908 close(lock->lock_fd);
c33d5174
JH
909 /* Do not free lock->lk -- atexit() still looks at them */
910 if (lock->lk)
911 rollback_lock_file(lock->lk);
4bd18c43 912 }
434cd0cd 913 free(lock->ref_name);
4cac42b1 914 free(lock->log_file);
4bd18c43
SP
915 free(lock);
916}
917
6de08ae6
SP
918static int log_ref_write(struct ref_lock *lock,
919 const unsigned char *sha1, const char *msg)
920{
921 int logfd, written, oflags = O_APPEND | O_WRONLY;
922 unsigned maxlen, len;
923 char *logrec;
ff4c8485 924 const char *committer;
6de08ae6 925
510c5a8e 926 if (log_all_ref_updates < 0)
7d1864ce 927 log_all_ref_updates = !is_bare_repository();
510c5a8e 928
4057deb5 929 if (log_all_ref_updates &&
e19b9dda
JH
930 (!strncmp(lock->ref_name, "refs/heads/", 11) ||
931 !strncmp(lock->ref_name, "refs/remotes/", 13))) {
6de08ae6
SP
932 if (safe_create_leading_directories(lock->log_file) < 0)
933 return error("unable to create directory for %s",
934 lock->log_file);
935 oflags |= O_CREAT;
936 }
937
938 logfd = open(lock->log_file, oflags, 0666);
939 if (logfd < 0) {
1974bf62 940 if (!(oflags & O_CREAT) && errno == ENOENT)
6de08ae6 941 return 0;
3b463c3f
JH
942
943 if ((oflags & O_CREAT) && errno == EISDIR) {
944 if (remove_empty_directories(lock->log_file)) {
945 return error("There are still logs under '%s'",
946 lock->log_file);
947 }
948 logfd = open(lock->log_file, oflags, 0666);
949 }
950
951 if (logfd < 0)
952 return error("Unable to append to %s: %s",
953 lock->log_file, strerror(errno));
6de08ae6
SP
954 }
955
ff4c8485 956 committer = git_committer_info(1);
6de08ae6 957 if (msg) {
ff4c8485 958 maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
6de08ae6
SP
959 logrec = xmalloc(maxlen);
960 len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
961 sha1_to_hex(lock->old_sha1),
962 sha1_to_hex(sha1),
ff4c8485 963 committer,
6de08ae6 964 msg);
e5f38ec3
JH
965 }
966 else {
ff4c8485 967 maxlen = strlen(committer) + 2*40 + 4;
6de08ae6
SP
968 logrec = xmalloc(maxlen);
969 len = snprintf(logrec, maxlen, "%s %s %s\n",
970 sha1_to_hex(lock->old_sha1),
971 sha1_to_hex(sha1),
ff4c8485 972 committer);
6de08ae6 973 }
93822c22 974 written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
6de08ae6
SP
975 free(logrec);
976 close(logfd);
977 if (written != len)
978 return error("Unable to append to %s", lock->log_file);
979 return 0;
980}
981
4bd18c43
SP
982int write_ref_sha1(struct ref_lock *lock,
983 const unsigned char *sha1, const char *logmsg)
984{
985 static char term = '\n';
986
987 if (!lock)
95fc7512 988 return -1;
a89fccd2 989 if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
4bd18c43
SP
990 unlock_ref(lock);
991 return 0;
95fc7512 992 }
93822c22
AW
993 if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
994 write_in_full(lock->lock_fd, &term, 1) != 1
4bd18c43 995 || close(lock->lock_fd) < 0) {
c33d5174 996 error("Couldn't write %s", lock->lk->filename);
4bd18c43
SP
997 unlock_ref(lock);
998 return -1;
999 }
5e290ff7 1000 invalidate_cached_refs();
6de08ae6
SP
1001 if (log_ref_write(lock, sha1, logmsg) < 0) {
1002 unlock_ref(lock);
1003 return -1;
1004 }
c33d5174 1005 if (commit_lock_file(lock->lk)) {
434cd0cd 1006 error("Couldn't set %s", lock->ref_name);
4bd18c43
SP
1007 unlock_ref(lock);
1008 return -1;
1009 }
1010 lock->lock_fd = -1;
1011 unlock_ref(lock);
1012 return 0;
95fc7512 1013}
d556fae2 1014
ab2a1a32 1015int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1)
d556fae2 1016{
e5229042 1017 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
d556fae2 1018 char *tz_c;
e29cb53a 1019 int logfd, tz, reccnt = 0;
d556fae2
SP
1020 struct stat st;
1021 unsigned long date;
e5229042 1022 unsigned char logged_sha1[20];
d556fae2
SP
1023
1024 logfile = git_path("logs/%s", ref);
1025 logfd = open(logfile, O_RDONLY, 0);
1026 if (logfd < 0)
1027 die("Unable to read log %s: %s", logfile, strerror(errno));
1028 fstat(logfd, &st);
1029 if (!st.st_size)
1030 die("Log %s is empty.", logfile);
c4712e45 1031 logdata = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
d556fae2
SP
1032 close(logfd);
1033
e5229042 1034 lastrec = NULL;
d556fae2
SP
1035 rec = logend = logdata + st.st_size;
1036 while (logdata < rec) {
e29cb53a 1037 reccnt++;
d556fae2
SP
1038 if (logdata < rec && *(rec-1) == '\n')
1039 rec--;
e5229042
SP
1040 lastgt = NULL;
1041 while (logdata < rec && *(rec-1) != '\n') {
d556fae2 1042 rec--;
e5229042
SP
1043 if (*rec == '>')
1044 lastgt = rec;
1045 }
1046 if (!lastgt)
d556fae2 1047 die("Log %s is corrupt.", logfile);
e5229042 1048 date = strtoul(lastgt + 1, &tz_c, 10);
ab2a1a32 1049 if (date <= at_time || cnt == 0) {
e5229042
SP
1050 if (lastrec) {
1051 if (get_sha1_hex(lastrec, logged_sha1))
1052 die("Log %s is corrupt.", logfile);
1053 if (get_sha1_hex(rec + 41, sha1))
1054 die("Log %s is corrupt.", logfile);
a89fccd2 1055 if (hashcmp(logged_sha1, sha1)) {
e5229042
SP
1056 tz = strtoul(tz_c, NULL, 10);
1057 fprintf(stderr,
1058 "warning: Log %s has gap after %s.\n",
1059 logfile, show_rfc2822_date(date, tz));
1060 }
e5f38ec3
JH
1061 }
1062 else if (date == at_time) {
e5229042
SP
1063 if (get_sha1_hex(rec + 41, sha1))
1064 die("Log %s is corrupt.", logfile);
e5f38ec3
JH
1065 }
1066 else {
e5229042
SP
1067 if (get_sha1_hex(rec + 41, logged_sha1))
1068 die("Log %s is corrupt.", logfile);
a89fccd2 1069 if (hashcmp(logged_sha1, sha1)) {
e5229042
SP
1070 tz = strtoul(tz_c, NULL, 10);
1071 fprintf(stderr,
1072 "warning: Log %s unexpectedly ended on %s.\n",
1073 logfile, show_rfc2822_date(date, tz));
1074 }
1075 }
d556fae2
SP
1076 munmap((void*)logdata, st.st_size);
1077 return 0;
1078 }
e5229042 1079 lastrec = rec;
ab2a1a32
JH
1080 if (cnt > 0)
1081 cnt--;
d556fae2
SP
1082 }
1083
e5229042
SP
1084 rec = logdata;
1085 while (rec < logend && *rec != '>' && *rec != '\n')
1086 rec++;
1087 if (rec == logend || *rec == '\n')
d556fae2 1088 die("Log %s is corrupt.", logfile);
e5229042 1089 date = strtoul(rec + 1, &tz_c, 10);
d556fae2
SP
1090 tz = strtoul(tz_c, NULL, 10);
1091 if (get_sha1_hex(logdata, sha1))
1092 die("Log %s is corrupt.", logfile);
1093 munmap((void*)logdata, st.st_size);
e29cb53a
JH
1094 if (at_time)
1095 fprintf(stderr, "warning: Log %s only goes back to %s.\n",
1096 logfile, show_rfc2822_date(date, tz));
1097 else
1098 fprintf(stderr, "warning: Log %s only has %d entries.\n",
1099 logfile, reccnt);
d556fae2
SP
1100 return 0;
1101}
2ff81662 1102
883d60fa 1103int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
2ff81662
JH
1104{
1105 const char *logfile;
1106 FILE *logfp;
1107 char buf[1024];
1108
1109 logfile = git_path("logs/%s", ref);
1110 logfp = fopen(logfile, "r");
1111 if (!logfp)
883d60fa 1112 return -1;
2ff81662
JH
1113 while (fgets(buf, sizeof(buf), logfp)) {
1114 unsigned char osha1[20], nsha1[20];
883d60fa
JS
1115 char *email_end, *message;
1116 unsigned long timestamp;
1117 int len, ret, tz;
2ff81662
JH
1118
1119 /* old SP new SP name <email> SP time TAB msg LF */
1120 len = strlen(buf);
1121 if (len < 83 || buf[len-1] != '\n' ||
1122 get_sha1_hex(buf, osha1) || buf[40] != ' ' ||
883d60fa
JS
1123 get_sha1_hex(buf + 41, nsha1) || buf[81] != ' ' ||
1124 !(email_end = strchr(buf + 82, '>')) ||
1125 email_end[1] != ' ' ||
1126 !(timestamp = strtoul(email_end + 2, &message, 10)) ||
1127 !message || message[0] != ' ' ||
1128 (message[1] != '+' && message[1] != '-') ||
1129 !isdigit(message[2]) || !isdigit(message[3]) ||
1130 !isdigit(message[4]) || !isdigit(message[5]) ||
1131 message[6] != '\t')
2ff81662 1132 continue; /* corrupt? */
883d60fa
JS
1133 email_end[1] = '\0';
1134 tz = strtol(message + 1, NULL, 10);
1135 message += 7;
1136 ret = fn(osha1, nsha1, buf+82, timestamp, tz, message, cb_data);
1137 if (ret)
1138 return ret;
2ff81662
JH
1139 }
1140 fclose(logfp);
883d60fa 1141 return 0;
2ff81662 1142}
e29cb53a 1143