Add callback data to for_each_ref() family.
[git] / refs.c
1 #include "refs.h"
2 #include "cache.h"
3
4 #include <errno.h>
5
6 struct ref_list {
7         struct ref_list *next;
8         unsigned char sha1[20];
9         char name[FLEX_ARRAY];
10 };
11
12 static const char *parse_ref_line(char *line, unsigned char *sha1)
13 {
14         /*
15          * 42: the answer to everything.
16          *
17          * In this case, it happens to be the answer to
18          *  40 (length of sha1 hex representation)
19          *  +1 (space in between hex and name)
20          *  +1 (newline at the end of the line)
21          */
22         int len = strlen(line) - 42;
23
24         if (len <= 0)
25                 return NULL;
26         if (get_sha1_hex(line, sha1) < 0)
27                 return NULL;
28         if (!isspace(line[40]))
29                 return NULL;
30         line += 41;
31         if (isspace(*line))
32                 return NULL;
33         if (line[len] != '\n')
34                 return NULL;
35         line[len] = 0;
36         return line;
37 }
38
39 static struct ref_list *add_ref(const char *name, const unsigned char *sha1, struct ref_list *list)
40 {
41         int len;
42         struct ref_list **p = &list, *entry;
43
44         /* Find the place to insert the ref into.. */
45         while ((entry = *p) != NULL) {
46                 int cmp = strcmp(entry->name, name);
47                 if (cmp > 0)
48                         break;
49
50                 /* Same as existing entry? */
51                 if (!cmp)
52                         return list;
53                 p = &entry->next;
54         }
55
56         /* Allocate it and add it in.. */
57         len = strlen(name) + 1;
58         entry = xmalloc(sizeof(struct ref_list) + len);
59         hashcpy(entry->sha1, sha1);
60         memcpy(entry->name, name, len);
61         entry->next = *p;
62         *p = entry;
63         return list;
64 }
65
66 static struct ref_list *get_packed_refs(void)
67 {
68         static int did_refs = 0;
69         static struct ref_list *refs = NULL;
70
71         if (!did_refs) {
72                 FILE *f = fopen(git_path("packed-refs"), "r");
73                 if (f) {
74                         struct ref_list *list = NULL;
75                         char refline[PATH_MAX];
76                         while (fgets(refline, sizeof(refline), f)) {
77                                 unsigned char sha1[20];
78                                 const char *name = parse_ref_line(refline, sha1);
79                                 if (!name)
80                                         continue;
81                                 list = add_ref(name, sha1, list);
82                         }
83                         fclose(f);
84                         refs = list;
85                 }
86                 did_refs = 1;
87         }
88         return refs;
89 }
90
91 static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
92 {
93         DIR *dir = opendir(git_path("%s", base));
94
95         if (dir) {
96                 struct dirent *de;
97                 int baselen = strlen(base);
98                 char *ref = xmalloc(baselen + 257);
99
100                 memcpy(ref, base, baselen);
101                 if (baselen && base[baselen-1] != '/')
102                         ref[baselen++] = '/';
103
104                 while ((de = readdir(dir)) != NULL) {
105                         unsigned char sha1[20];
106                         struct stat st;
107                         int namelen;
108
109                         if (de->d_name[0] == '.')
110                                 continue;
111                         namelen = strlen(de->d_name);
112                         if (namelen > 255)
113                                 continue;
114                         if (has_extension(de->d_name, ".lock"))
115                                 continue;
116                         memcpy(ref + baselen, de->d_name, namelen+1);
117                         if (stat(git_path("%s", ref), &st) < 0)
118                                 continue;
119                         if (S_ISDIR(st.st_mode)) {
120                                 list = get_ref_dir(ref, list);
121                                 continue;
122                         }
123                         if (read_ref(ref, sha1) < 0) {
124                                 error("%s points nowhere!", ref);
125                                 continue;
126                         }
127                         list = add_ref(ref, sha1, list);
128                 }
129                 free(ref);
130                 closedir(dir);
131         }
132         return list;
133 }
134
135 static struct ref_list *get_loose_refs(void)
136 {
137         static int did_refs = 0;
138         static struct ref_list *refs = NULL;
139
140         if (!did_refs) {
141                 refs = get_ref_dir("refs", NULL);
142                 did_refs = 1;
143         }
144         return refs;
145 }
146
147 /* We allow "recursive" symbolic refs. Only within reason, though */
148 #define MAXDEPTH 5
149
150 const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
151 {
152         int depth = MAXDEPTH, len;
153         char buffer[256];
154         static char ref_buffer[256];
155
156         for (;;) {
157                 const char *path = git_path("%s", ref);
158                 struct stat st;
159                 char *buf;
160                 int fd;
161
162                 if (--depth < 0)
163                         return NULL;
164
165                 /* Special case: non-existing file.
166                  * Not having the refs/heads/new-branch is OK
167                  * if we are writing into it, so is .git/HEAD
168                  * that points at refs/heads/master still to be
169                  * born.  It is NOT OK if we are resolving for
170                  * reading.
171                  */
172                 if (lstat(path, &st) < 0) {
173                         struct ref_list *list = get_packed_refs();
174                         while (list) {
175                                 if (!strcmp(ref, list->name)) {
176                                         hashcpy(sha1, list->sha1);
177                                         return ref;
178                                 }
179                                 list = list->next;
180                         }
181                         if (reading || errno != ENOENT)
182                                 return NULL;
183                         hashclr(sha1);
184                         return ref;
185                 }
186
187                 /* Follow "normalized" - ie "refs/.." symlinks by hand */
188                 if (S_ISLNK(st.st_mode)) {
189                         len = readlink(path, buffer, sizeof(buffer)-1);
190                         if (len >= 5 && !memcmp("refs/", buffer, 5)) {
191                                 buffer[len] = 0;
192                                 strcpy(ref_buffer, buffer);
193                                 ref = ref_buffer;
194                                 continue;
195                         }
196                 }
197
198                 /*
199                  * Anything else, just open it and try to use it as
200                  * a ref
201                  */
202                 fd = open(path, O_RDONLY);
203                 if (fd < 0)
204                         return NULL;
205                 len = read(fd, buffer, sizeof(buffer)-1);
206                 close(fd);
207
208                 /*
209                  * Is it a symbolic ref?
210                  */
211                 if (len < 4 || memcmp("ref:", buffer, 4))
212                         break;
213                 buf = buffer + 4;
214                 len -= 4;
215                 while (len && isspace(*buf))
216                         buf++, len--;
217                 while (len && isspace(buf[len-1]))
218                         len--;
219                 buf[len] = 0;
220                 memcpy(ref_buffer, buf, len + 1);
221                 ref = ref_buffer;
222         }
223         if (len < 40 || get_sha1_hex(buffer, sha1))
224                 return NULL;
225         return ref;
226 }
227
228 int create_symref(const char *ref_target, const char *refs_heads_master)
229 {
230         const char *lockpath;
231         char ref[1000];
232         int fd, len, written;
233         const char *git_HEAD = git_path("%s", ref_target);
234
235 #ifndef NO_SYMLINK_HEAD
236         if (prefer_symlink_refs) {
237                 unlink(git_HEAD);
238                 if (!symlink(refs_heads_master, git_HEAD))
239                         return 0;
240                 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
241         }
242 #endif
243
244         len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
245         if (sizeof(ref) <= len) {
246                 error("refname too long: %s", refs_heads_master);
247                 return -1;
248         }
249         lockpath = mkpath("%s.lock", git_HEAD);
250         fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); 
251         written = write(fd, ref, len);
252         close(fd);
253         if (written != len) {
254                 unlink(lockpath);
255                 error("Unable to write to %s", lockpath);
256                 return -2;
257         }
258         if (rename(lockpath, git_HEAD) < 0) {
259                 unlink(lockpath);
260                 error("Unable to create %s", git_HEAD);
261                 return -3;
262         }
263         if (adjust_shared_perm(git_HEAD)) {
264                 unlink(lockpath);
265                 error("Unable to fix permissions on %s", lockpath);
266                 return -4;
267         }
268         return 0;
269 }
270
271 int read_ref(const char *ref, unsigned char *sha1)
272 {
273         if (resolve_ref(ref, sha1, 1))
274                 return 0;
275         return -1;
276 }
277
278 static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_data)
279 {
280         int retval;
281         struct ref_list *packed = get_packed_refs();
282         struct ref_list *loose = get_loose_refs();
283
284         while (packed && loose) {
285                 struct ref_list *entry;
286                 int cmp = strcmp(packed->name, loose->name);
287                 if (!cmp) {
288                         packed = packed->next;
289                         continue;
290                 }
291                 if (cmp > 0) {
292                         entry = loose;
293                         loose = loose->next;
294                 } else {
295                         entry = packed;
296                         packed = packed->next;
297                 }
298                 if (strncmp(base, entry->name, trim))
299                         continue;
300                 if (is_null_sha1(entry->sha1))
301                         continue;
302                 if (!has_sha1_file(entry->sha1)) {
303                         error("%s does not point to a valid object!", entry->name);
304                         continue;
305                 }
306                 retval = fn(entry->name + trim, entry->sha1, cb_data);
307                 if (retval)
308                         return retval;
309         }
310
311         packed = packed ? packed : loose;
312         while (packed) {
313                 if (!strncmp(base, packed->name, trim)) {
314                         retval = fn(packed->name + trim, packed->sha1, cb_data);
315                         if (retval)
316                                 return retval;
317                 }
318                 packed = packed->next;
319         }
320         return 0;
321 }
322
323 int head_ref(each_ref_fn fn, void *cb_data)
324 {
325         unsigned char sha1[20];
326         if (!read_ref("HEAD", sha1))
327                 return fn("HEAD", sha1, cb_data);
328         return 0;
329 }
330
331 int for_each_ref(each_ref_fn fn, void *cb_data)
332 {
333         return do_for_each_ref("refs/", fn, 0, cb_data);
334 }
335
336 int for_each_tag_ref(each_ref_fn fn, void *cb_data)
337 {
338         return do_for_each_ref("refs/tags/", fn, 10, cb_data);
339 }
340
341 int for_each_branch_ref(each_ref_fn fn, void *cb_data)
342 {
343         return do_for_each_ref("refs/heads/", fn, 11, cb_data);
344 }
345
346 int for_each_remote_ref(each_ref_fn fn, void *cb_data)
347 {
348         return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
349 }
350
351 /* NEEDSWORK: This is only used by ssh-upload and it should go; the
352  * caller should do resolve_ref or read_ref like everybody else.  Or
353  * maybe everybody else should use get_ref_sha1() instead of doing
354  * read_ref().
355  */
356 int get_ref_sha1(const char *ref, unsigned char *sha1)
357 {
358         if (check_ref_format(ref))
359                 return -1;
360         return read_ref(mkpath("refs/%s", ref), sha1);
361 }
362
363 /*
364  * Make sure "ref" is something reasonable to have under ".git/refs/";
365  * We do not like it if:
366  *
367  * - any path component of it begins with ".", or
368  * - it has double dots "..", or
369  * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
370  * - it ends with a "/".
371  */
372
373 static inline int bad_ref_char(int ch)
374 {
375         return (((unsigned) ch) <= ' ' ||
376                 ch == '~' || ch == '^' || ch == ':' ||
377                 /* 2.13 Pattern Matching Notation */
378                 ch == '?' || ch == '*' || ch == '[');
379 }
380
381 int check_ref_format(const char *ref)
382 {
383         int ch, level;
384         const char *cp = ref;
385
386         level = 0;
387         while (1) {
388                 while ((ch = *cp++) == '/')
389                         ; /* tolerate duplicated slashes */
390                 if (!ch)
391                         return -1; /* should not end with slashes */
392
393                 /* we are at the beginning of the path component */
394                 if (ch == '.' || bad_ref_char(ch))
395                         return -1;
396
397                 /* scan the rest of the path component */
398                 while ((ch = *cp++) != 0) {
399                         if (bad_ref_char(ch))
400                                 return -1;
401                         if (ch == '/')
402                                 break;
403                         if (ch == '.' && *cp == '.')
404                                 return -1;
405                 }
406                 level++;
407                 if (!ch) {
408                         if (level < 2)
409                                 return -1; /* at least of form "heads/blah" */
410                         return 0;
411                 }
412         }
413 }
414
415 static struct ref_lock *verify_lock(struct ref_lock *lock,
416         const unsigned char *old_sha1, int mustexist)
417 {
418         if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist)) {
419                 error("Can't verify ref %s", lock->ref_name);
420                 unlock_ref(lock);
421                 return NULL;
422         }
423         if (hashcmp(lock->old_sha1, old_sha1)) {
424                 error("Ref %s is at %s but expected %s", lock->ref_name,
425                         sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
426                 unlock_ref(lock);
427                 return NULL;
428         }
429         return lock;
430 }
431
432 static struct ref_lock *lock_ref_sha1_basic(const char *ref,
433         int plen,
434         const unsigned char *old_sha1, int mustexist)
435 {
436         char *ref_file;
437         const char *orig_ref = ref;
438         struct ref_lock *lock;
439         struct stat st;
440
441         lock = xcalloc(1, sizeof(struct ref_lock));
442         lock->lock_fd = -1;
443
444         ref = resolve_ref(ref, lock->old_sha1, mustexist);
445         if (!ref) {
446                 int last_errno = errno;
447                 error("unable to resolve reference %s: %s",
448                         orig_ref, strerror(errno));
449                 unlock_ref(lock);
450                 errno = last_errno;
451                 return NULL;
452         }
453         lock->lk = xcalloc(1, sizeof(struct lock_file));
454
455         lock->ref_name = xstrdup(ref);
456         lock->log_file = xstrdup(git_path("logs/%s", ref));
457         ref_file = git_path(ref);
458         lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
459
460         if (safe_create_leading_directories(ref_file))
461                 die("unable to create directory for %s", ref_file);
462         lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
463
464         return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
465 }
466
467 struct ref_lock *lock_ref_sha1(const char *ref,
468         const unsigned char *old_sha1, int mustexist)
469 {
470         char refpath[PATH_MAX];
471         if (check_ref_format(ref))
472                 return NULL;
473         strcpy(refpath, mkpath("refs/%s", ref));
474         return lock_ref_sha1_basic(refpath, strlen(refpath),
475                 old_sha1, mustexist);
476 }
477
478 struct ref_lock *lock_any_ref_for_update(const char *ref,
479         const unsigned char *old_sha1, int mustexist)
480 {
481         return lock_ref_sha1_basic(ref, strlen(ref), old_sha1, mustexist);
482 }
483
484 void unlock_ref(struct ref_lock *lock)
485 {
486         if (lock->lock_fd >= 0) {
487                 close(lock->lock_fd);
488                 /* Do not free lock->lk -- atexit() still looks at them */
489                 if (lock->lk)
490                         rollback_lock_file(lock->lk);
491         }
492         free(lock->ref_name);
493         free(lock->log_file);
494         free(lock);
495 }
496
497 static int log_ref_write(struct ref_lock *lock,
498         const unsigned char *sha1, const char *msg)
499 {
500         int logfd, written, oflags = O_APPEND | O_WRONLY;
501         unsigned maxlen, len;
502         char *logrec;
503         const char *committer;
504
505         if (log_all_ref_updates) {
506                 if (safe_create_leading_directories(lock->log_file) < 0)
507                         return error("unable to create directory for %s",
508                                 lock->log_file);
509                 oflags |= O_CREAT;
510         }
511
512         logfd = open(lock->log_file, oflags, 0666);
513         if (logfd < 0) {
514                 if (!log_all_ref_updates && errno == ENOENT)
515                         return 0;
516                 return error("Unable to append to %s: %s",
517                         lock->log_file, strerror(errno));
518         }
519
520         committer = git_committer_info(1);
521         if (msg) {
522                 maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
523                 logrec = xmalloc(maxlen);
524                 len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
525                         sha1_to_hex(lock->old_sha1),
526                         sha1_to_hex(sha1),
527                         committer,
528                         msg);
529         }
530         else {
531                 maxlen = strlen(committer) + 2*40 + 4;
532                 logrec = xmalloc(maxlen);
533                 len = snprintf(logrec, maxlen, "%s %s %s\n",
534                         sha1_to_hex(lock->old_sha1),
535                         sha1_to_hex(sha1),
536                         committer);
537         }
538         written = len <= maxlen ? write(logfd, logrec, len) : -1;
539         free(logrec);
540         close(logfd);
541         if (written != len)
542                 return error("Unable to append to %s", lock->log_file);
543         return 0;
544 }
545
546 int write_ref_sha1(struct ref_lock *lock,
547         const unsigned char *sha1, const char *logmsg)
548 {
549         static char term = '\n';
550
551         if (!lock)
552                 return -1;
553         if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
554                 unlock_ref(lock);
555                 return 0;
556         }
557         if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
558             write(lock->lock_fd, &term, 1) != 1
559                 || close(lock->lock_fd) < 0) {
560                 error("Couldn't write %s", lock->lk->filename);
561                 unlock_ref(lock);
562                 return -1;
563         }
564         if (log_ref_write(lock, sha1, logmsg) < 0) {
565                 unlock_ref(lock);
566                 return -1;
567         }
568         if (commit_lock_file(lock->lk)) {
569                 error("Couldn't set %s", lock->ref_name);
570                 unlock_ref(lock);
571                 return -1;
572         }
573         lock->lock_fd = -1;
574         unlock_ref(lock);
575         return 0;
576 }
577
578 int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
579 {
580         const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
581         char *tz_c;
582         int logfd, tz;
583         struct stat st;
584         unsigned long date;
585         unsigned char logged_sha1[20];
586
587         logfile = git_path("logs/%s", ref);
588         logfd = open(logfile, O_RDONLY, 0);
589         if (logfd < 0)
590                 die("Unable to read log %s: %s", logfile, strerror(errno));
591         fstat(logfd, &st);
592         if (!st.st_size)
593                 die("Log %s is empty.", logfile);
594         logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
595         close(logfd);
596
597         lastrec = NULL;
598         rec = logend = logdata + st.st_size;
599         while (logdata < rec) {
600                 if (logdata < rec && *(rec-1) == '\n')
601                         rec--;
602                 lastgt = NULL;
603                 while (logdata < rec && *(rec-1) != '\n') {
604                         rec--;
605                         if (*rec == '>')
606                                 lastgt = rec;
607                 }
608                 if (!lastgt)
609                         die("Log %s is corrupt.", logfile);
610                 date = strtoul(lastgt + 1, &tz_c, 10);
611                 if (date <= at_time) {
612                         if (lastrec) {
613                                 if (get_sha1_hex(lastrec, logged_sha1))
614                                         die("Log %s is corrupt.", logfile);
615                                 if (get_sha1_hex(rec + 41, sha1))
616                                         die("Log %s is corrupt.", logfile);
617                                 if (hashcmp(logged_sha1, sha1)) {
618                                         tz = strtoul(tz_c, NULL, 10);
619                                         fprintf(stderr,
620                                                 "warning: Log %s has gap after %s.\n",
621                                                 logfile, show_rfc2822_date(date, tz));
622                                 }
623                         }
624                         else if (date == at_time) {
625                                 if (get_sha1_hex(rec + 41, sha1))
626                                         die("Log %s is corrupt.", logfile);
627                         }
628                         else {
629                                 if (get_sha1_hex(rec + 41, logged_sha1))
630                                         die("Log %s is corrupt.", logfile);
631                                 if (hashcmp(logged_sha1, sha1)) {
632                                         tz = strtoul(tz_c, NULL, 10);
633                                         fprintf(stderr,
634                                                 "warning: Log %s unexpectedly ended on %s.\n",
635                                                 logfile, show_rfc2822_date(date, tz));
636                                 }
637                         }
638                         munmap((void*)logdata, st.st_size);
639                         return 0;
640                 }
641                 lastrec = rec;
642         }
643
644         rec = logdata;
645         while (rec < logend && *rec != '>' && *rec != '\n')
646                 rec++;
647         if (rec == logend || *rec == '\n')
648                 die("Log %s is corrupt.", logfile);
649         date = strtoul(rec + 1, &tz_c, 10);
650         tz = strtoul(tz_c, NULL, 10);
651         if (get_sha1_hex(logdata, sha1))
652                 die("Log %s is corrupt.", logfile);
653         munmap((void*)logdata, st.st_size);
654         fprintf(stderr, "warning: Log %s only goes back to %s.\n",
655                 logfile, show_rfc2822_date(date, tz));
656         return 0;
657 }