Fix ref log parsing so it works properly.
[git.git] / refs.c
1 #include "refs.h"
2 #include "cache.h"
3
4 #include <errno.h>
5
6 /* We allow "recursive" symbolic refs. Only within reason, though */
7 #define MAXDEPTH 5
8
9 const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
10 {
11         int depth = MAXDEPTH, len;
12         char buffer[256];
13
14         for (;;) {
15                 struct stat st;
16                 char *buf;
17                 int fd;
18
19                 if (--depth < 0)
20                         return NULL;
21
22                 /* Special case: non-existing file.
23                  * Not having the refs/heads/new-branch is OK
24                  * if we are writing into it, so is .git/HEAD
25                  * that points at refs/heads/master still to be
26                  * born.  It is NOT OK if we are resolving for
27                  * reading.
28                  */
29                 if (lstat(path, &st) < 0) {
30                         if (reading || errno != ENOENT)
31                                 return NULL;
32                         memset(sha1, 0, 20);
33                         return path;
34                 }
35
36                 /* Follow "normalized" - ie "refs/.." symlinks by hand */
37                 if (S_ISLNK(st.st_mode)) {
38                         len = readlink(path, buffer, sizeof(buffer)-1);
39                         if (len >= 5 && !memcmp("refs/", buffer, 5)) {
40                                 path = git_path("%.*s", len, buffer);
41                                 continue;
42                         }
43                 }
44
45                 /*
46                  * Anything else, just open it and try to use it as
47                  * a ref
48                  */
49                 fd = open(path, O_RDONLY);
50                 if (fd < 0)
51                         return NULL;
52                 len = read(fd, buffer, sizeof(buffer)-1);
53                 close(fd);
54
55                 /*
56                  * Is it a symbolic ref?
57                  */
58                 if (len < 4 || memcmp("ref:", buffer, 4))
59                         break;
60                 buf = buffer + 4;
61                 len -= 4;
62                 while (len && isspace(*buf))
63                         buf++, len--;
64                 while (len && isspace(buf[len-1]))
65                         buf[--len] = 0;
66                 path = git_path("%.*s", len, buf);
67         }
68         if (len < 40 || get_sha1_hex(buffer, sha1))
69                 return NULL;
70         return path;
71 }
72
73 int create_symref(const char *git_HEAD, const char *refs_heads_master)
74 {
75         const char *lockpath;
76         char ref[1000];
77         int fd, len, written;
78
79 #ifndef NO_SYMLINK_HEAD
80         if (prefer_symlink_refs) {
81                 unlink(git_HEAD);
82                 if (!symlink(refs_heads_master, git_HEAD))
83                         return 0;
84                 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
85         }
86 #endif
87
88         len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
89         if (sizeof(ref) <= len) {
90                 error("refname too long: %s", refs_heads_master);
91                 return -1;
92         }
93         lockpath = mkpath("%s.lock", git_HEAD);
94         fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); 
95         written = write(fd, ref, len);
96         close(fd);
97         if (written != len) {
98                 unlink(lockpath);
99                 error("Unable to write to %s", lockpath);
100                 return -2;
101         }
102         if (rename(lockpath, git_HEAD) < 0) {
103                 unlink(lockpath);
104                 error("Unable to create %s", git_HEAD);
105                 return -3;
106         }
107         return 0;
108 }
109
110 int read_ref(const char *filename, unsigned char *sha1)
111 {
112         if (resolve_ref(filename, sha1, 1))
113                 return 0;
114         return -1;
115 }
116
117 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
118 {
119         int retval = 0;
120         DIR *dir = opendir(git_path("%s", base));
121
122         if (dir) {
123                 struct dirent *de;
124                 int baselen = strlen(base);
125                 char *path = xmalloc(baselen + 257);
126
127                 if (!strncmp(base, "./", 2)) {
128                         base += 2;
129                         baselen -= 2;
130                 }
131                 memcpy(path, base, baselen);
132                 if (baselen && base[baselen-1] != '/')
133                         path[baselen++] = '/';
134
135                 while ((de = readdir(dir)) != NULL) {
136                         unsigned char sha1[20];
137                         struct stat st;
138                         int namelen;
139
140                         if (de->d_name[0] == '.')
141                                 continue;
142                         namelen = strlen(de->d_name);
143                         if (namelen > 255)
144                                 continue;
145                         memcpy(path + baselen, de->d_name, namelen+1);
146                         if (stat(git_path("%s", path), &st) < 0)
147                                 continue;
148                         if (S_ISDIR(st.st_mode)) {
149                                 retval = do_for_each_ref(path, fn, trim);
150                                 if (retval)
151                                         break;
152                                 continue;
153                         }
154                         if (read_ref(git_path("%s", path), sha1) < 0) {
155                                 error("%s points nowhere!", path);
156                                 continue;
157                         }
158                         if (!has_sha1_file(sha1)) {
159                                 error("%s does not point to a valid "
160                                       "commit object!", path);
161                                 continue;
162                         }
163                         retval = fn(path + trim, sha1);
164                         if (retval)
165                                 break;
166                 }
167                 free(path);
168                 closedir(dir);
169         }
170         return retval;
171 }
172
173 int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
174 {
175         unsigned char sha1[20];
176         if (!read_ref(git_path("HEAD"), sha1))
177                 return fn("HEAD", sha1);
178         return 0;
179 }
180
181 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
182 {
183         return do_for_each_ref("refs", fn, 0);
184 }
185
186 int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
187 {
188         return do_for_each_ref("refs/tags", fn, 10);
189 }
190
191 int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
192 {
193         return do_for_each_ref("refs/heads", fn, 11);
194 }
195
196 int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
197 {
198         return do_for_each_ref("refs/remotes", fn, 13);
199 }
200
201 int get_ref_sha1(const char *ref, unsigned char *sha1)
202 {
203         if (check_ref_format(ref))
204                 return -1;
205         return read_ref(git_path("refs/%s", ref), sha1);
206 }
207
208 /*
209  * Make sure "ref" is something reasonable to have under ".git/refs/";
210  * We do not like it if:
211  *
212  * - any path component of it begins with ".", or
213  * - it has double dots "..", or
214  * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
215  * - it ends with a "/".
216  */
217
218 static inline int bad_ref_char(int ch)
219 {
220         return (((unsigned) ch) <= ' ' ||
221                 ch == '~' || ch == '^' || ch == ':' ||
222                 /* 2.13 Pattern Matching Notation */
223                 ch == '?' || ch == '*' || ch == '[');
224 }
225
226 int check_ref_format(const char *ref)
227 {
228         int ch, level;
229         const char *cp = ref;
230
231         level = 0;
232         while (1) {
233                 while ((ch = *cp++) == '/')
234                         ; /* tolerate duplicated slashes */
235                 if (!ch)
236                         return -1; /* should not end with slashes */
237
238                 /* we are at the beginning of the path component */
239                 if (ch == '.' || bad_ref_char(ch))
240                         return -1;
241
242                 /* scan the rest of the path component */
243                 while ((ch = *cp++) != 0) {
244                         if (bad_ref_char(ch))
245                                 return -1;
246                         if (ch == '/')
247                                 break;
248                         if (ch == '.' && *cp == '.')
249                                 return -1;
250                 }
251                 level++;
252                 if (!ch) {
253                         if (level < 2)
254                                 return -1; /* at least of form "heads/blah" */
255                         return 0;
256                 }
257         }
258 }
259
260 static struct ref_lock* verify_lock(struct ref_lock *lock,
261         const unsigned char *old_sha1, int mustexist)
262 {
263         char buf[40];
264         int nr, fd = open(lock->ref_file, O_RDONLY);
265         if (fd < 0 && (mustexist || errno != ENOENT)) {
266                 error("Can't verify ref %s", lock->ref_file);
267                 unlock_ref(lock);
268                 return NULL;
269         }
270         nr = read(fd, buf, 40);
271         close(fd);
272         if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) {
273                 error("Can't verify ref %s", lock->ref_file);
274                 unlock_ref(lock);
275                 return NULL;
276         }
277         if (memcmp(lock->old_sha1, old_sha1, 20)) {
278                 error("Ref %s is at %s but expected %s", lock->ref_file,
279                         sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
280                 unlock_ref(lock);
281                 return NULL;
282         }
283         return lock;
284 }
285
286 static struct ref_lock* lock_ref_sha1_basic(const char *path,
287         int plen,
288         const unsigned char *old_sha1, int mustexist)
289 {
290         struct ref_lock *lock;
291
292         lock = xcalloc(1, sizeof(struct ref_lock));
293         lock->lock_fd = -1;
294
295         plen = strlen(path) - plen;
296         path = resolve_ref(path, lock->old_sha1, mustexist);
297         if (!path) {
298                 error("Can't read ref %s", path);
299                 unlock_ref(lock);
300                 return NULL;
301         }
302
303         lock->ref_file = strdup(path);
304         lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file));
305         lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
306
307         if (safe_create_leading_directories(lock->lock_file))
308                 die("unable to create directory for %s", lock->lock_file);
309         lock->lock_fd = open(lock->lock_file,
310                 O_WRONLY | O_CREAT | O_EXCL, 0666);
311         if (lock->lock_fd < 0) {
312                 error("Couldn't open lock file %s: %s",
313                         lock->lock_file, strerror(errno));
314                 unlock_ref(lock);
315                 return NULL;
316         }
317
318         return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
319 }
320
321 struct ref_lock* lock_ref_sha1(const char *ref,
322         const unsigned char *old_sha1, int mustexist)
323 {
324         if (check_ref_format(ref))
325                 return NULL;
326         return lock_ref_sha1_basic(git_path("refs/%s", ref),
327                 strlen(ref), old_sha1, mustexist);
328 }
329
330 struct ref_lock* lock_any_ref_for_update(const char *ref,
331         const unsigned char *old_sha1, int mustexist)
332 {
333         return lock_ref_sha1_basic(git_path("%s", ref),
334                 strlen(ref), old_sha1, mustexist);
335 }
336
337 void unlock_ref (struct ref_lock *lock)
338 {
339         if (lock->lock_fd >= 0) {
340                 close(lock->lock_fd);
341                 unlink(lock->lock_file);
342         }
343         if (lock->ref_file)
344                 free(lock->ref_file);
345         if (lock->lock_file)
346                 free(lock->lock_file);
347         if (lock->log_file)
348                 free(lock->log_file);
349         free(lock);
350 }
351
352 static int log_ref_write(struct ref_lock *lock,
353         const unsigned char *sha1, const char *msg)
354 {
355         int logfd, written, oflags = O_APPEND | O_WRONLY;
356         unsigned maxlen, len;
357         char *logrec;
358         const char *comitter;
359
360         if (log_all_ref_updates) {
361                 if (safe_create_leading_directories(lock->log_file) < 0)
362                         return error("unable to create directory for %s",
363                                 lock->log_file);
364                 oflags |= O_CREAT;
365         }
366
367         logfd = open(lock->log_file, oflags, 0666);
368         if (logfd < 0) {
369                 if (!log_all_ref_updates && errno == ENOENT)
370                         return 0;
371                 return error("Unable to append to %s: %s",
372                         lock->log_file, strerror(errno));
373         }
374
375         setup_ident();
376         comitter = git_committer_info(1);
377         if (msg) {
378                 maxlen = strlen(comitter) + strlen(msg) + 2*40 + 5;
379                 logrec = xmalloc(maxlen);
380                 len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
381                         sha1_to_hex(lock->old_sha1),
382                         sha1_to_hex(sha1),
383                         comitter,
384                         msg);
385         } else {
386                 maxlen = strlen(comitter) + 2*40 + 4;
387                 logrec = xmalloc(maxlen);
388                 len = snprintf(logrec, maxlen, "%s %s %s\n",
389                         sha1_to_hex(lock->old_sha1),
390                         sha1_to_hex(sha1),
391                         comitter);
392         }
393         written = len <= maxlen ? write(logfd, logrec, len) : -1;
394         free(logrec);
395         close(logfd);
396         if (written != len)
397                 return error("Unable to append to %s", lock->log_file);
398         return 0;
399 }
400
401 int write_ref_sha1(struct ref_lock *lock,
402         const unsigned char *sha1, const char *logmsg)
403 {
404         static char term = '\n';
405
406         if (!lock)
407                 return -1;
408         if (!memcmp(lock->old_sha1, sha1, 20)) {
409                 unlock_ref(lock);
410                 return 0;
411         }
412         if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
413             write(lock->lock_fd, &term, 1) != 1
414                 || close(lock->lock_fd) < 0) {
415                 error("Couldn't write %s", lock->lock_file);
416                 unlock_ref(lock);
417                 return -1;
418         }
419         if (log_ref_write(lock, sha1, logmsg) < 0) {
420                 unlock_ref(lock);
421                 return -1;
422         }
423         if (rename(lock->lock_file, lock->ref_file) < 0) {
424                 error("Couldn't set %s", lock->ref_file);
425                 unlock_ref(lock);
426                 return -1;
427         }
428         lock->lock_fd = -1;
429         unlock_ref(lock);
430         return 0;
431 }
432
433 int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
434 {
435         const char *logfile, *logdata, *logend, *rec, *c;
436         char *tz_c;
437         int logfd, tz;
438         struct stat st;
439         unsigned long date;
440
441         logfile = git_path("logs/%s", ref);
442         logfd = open(logfile, O_RDONLY, 0);
443         if (logfd < 0)
444                 die("Unable to read log %s: %s", logfile, strerror(errno));
445         fstat(logfd, &st);
446         if (!st.st_size)
447                 die("Log %s is empty.", logfile);
448         logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
449         close(logfd);
450
451         rec = logend = logdata + st.st_size;
452         while (logdata < rec) {
453                 if (logdata < rec && *(rec-1) == '\n')
454                         rec--;
455                 while (logdata < rec && *(rec-1) != '\n')
456                         rec--;
457                 c = rec;
458                 while (c < logend && *c != '>' && *c != '\n')
459                         c++;
460                 if (c == logend || *c == '\n')
461                         die("Log %s is corrupt.", logfile);
462                 date = strtoul(c + 1, NULL, 10);
463                 if (date <= at_time) {
464                         if (get_sha1_hex(rec + 41, sha1))
465                                 die("Log %s is corrupt.", logfile);
466                         munmap((void*)logdata, st.st_size);
467                         return 0;
468                 }
469         }
470
471         c = logdata;
472         while (c < logend && *c != '>' && *c != '\n')
473                 c++;
474         if (c == logend || *c == '\n')
475                 die("Log %s is corrupt.", logfile);
476         date = strtoul(c, &tz_c, 10);
477         tz = strtoul(tz_c, NULL, 10);
478         if (get_sha1_hex(logdata, sha1))
479                 die("Log %s is corrupt.", logfile);
480         munmap((void*)logdata, st.st_size);
481         fprintf(stderr, "warning: Log %s only goes back to %s.\n",
482                 logfile, show_rfc2822_date(date, tz));
483         return 0;
484 }