]>
Commit | Line | Data |
---|---|---|
95fc7512 DB |
1 | #include "refs.h" |
2 | #include "cache.h" | |
3 | ||
4 | #include <errno.h> | |
5 | ||
e1e22e37 LT |
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; | |
434cd0cd LT |
31 | if (isspace(*line)) |
32 | return NULL; | |
e1e22e37 LT |
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); | |
ed378ec7 | 98 | char *ref = xmalloc(baselen + 257); |
e1e22e37 | 99 | |
ed378ec7 | 100 | memcpy(ref, base, baselen); |
e1e22e37 | 101 | if (baselen && base[baselen-1] != '/') |
ed378ec7 | 102 | ref[baselen++] = '/'; |
e1e22e37 LT |
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; | |
ed378ec7 LT |
116 | memcpy(ref + baselen, de->d_name, namelen+1); |
117 | if (stat(git_path("%s", ref), &st) < 0) | |
e1e22e37 LT |
118 | continue; |
119 | if (S_ISDIR(st.st_mode)) { | |
ed378ec7 | 120 | list = get_ref_dir(ref, list); |
e1e22e37 LT |
121 | continue; |
122 | } | |
ed378ec7 LT |
123 | if (read_ref(ref, sha1) < 0) { |
124 | error("%s points nowhere!", ref); | |
e1e22e37 LT |
125 | continue; |
126 | } | |
ed378ec7 | 127 | list = add_ref(ref, sha1, list); |
e1e22e37 | 128 | } |
ed378ec7 | 129 | free(ref); |
e1e22e37 LT |
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 | ||
ca8db142 LT |
147 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
148 | #define MAXDEPTH 5 | |
149 | ||
ed378ec7 | 150 | const char *resolve_ref(const char *ref, unsigned char *sha1, int reading) |
8a65ff76 | 151 | { |
a876ed83 JH |
152 | int depth = MAXDEPTH, len; |
153 | char buffer[256]; | |
ed378ec7 | 154 | static char ref_buffer[256]; |
ca8db142 | 155 | |
a876ed83 | 156 | for (;;) { |
ed378ec7 | 157 | const char *path = git_path("%s", ref); |
a876ed83 JH |
158 | struct stat st; |
159 | char *buf; | |
160 | int fd; | |
8a65ff76 | 161 | |
a876ed83 JH |
162 | if (--depth < 0) |
163 | return NULL; | |
ca8db142 | 164 | |
a876ed83 JH |
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) { | |
434cd0cd LT |
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 | } | |
a876ed83 JH |
181 | if (reading || errno != ENOENT) |
182 | return NULL; | |
e702496e | 183 | hashclr(sha1); |
ed378ec7 | 184 | return ref; |
a876ed83 | 185 | } |
ca8db142 | 186 | |
a876ed83 JH |
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)) { | |
ed378ec7 LT |
191 | buffer[len] = 0; |
192 | strcpy(ref_buffer, buffer); | |
193 | ref = ref_buffer; | |
a876ed83 JH |
194 | continue; |
195 | } | |
ca8db142 | 196 | } |
a876ed83 JH |
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])) | |
ed378ec7 LT |
218 | len--; |
219 | buf[len] = 0; | |
220 | memcpy(ref_buffer, buf, len + 1); | |
221 | ref = ref_buffer; | |
8a65ff76 | 222 | } |
a876ed83 JH |
223 | if (len < 40 || get_sha1_hex(buffer, sha1)) |
224 | return NULL; | |
ed378ec7 | 225 | return ref; |
a876ed83 JH |
226 | } |
227 | ||
ed378ec7 | 228 | int create_symref(const char *ref_target, const char *refs_heads_master) |
8098a178 | 229 | { |
8098a178 JH |
230 | const char *lockpath; |
231 | char ref[1000]; | |
232 | int fd, len, written; | |
ed378ec7 | 233 | const char *git_HEAD = git_path("%s", ref_target); |
8098a178 | 234 | |
9f0bb90d JH |
235 | #ifndef NO_SYMLINK_HEAD |
236 | if (prefer_symlink_refs) { | |
f8348be3 JS |
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 | } | |
303958dc JS |
242 | #endif |
243 | ||
8098a178 JH |
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 | } | |
138086a7 JH |
263 | if (adjust_shared_perm(git_HEAD)) { |
264 | unlink(lockpath); | |
265 | error("Unable to fix permissions on %s", lockpath); | |
266 | return -4; | |
267 | } | |
8098a178 | 268 | return 0; |
8098a178 JH |
269 | } |
270 | ||
ed378ec7 | 271 | int read_ref(const char *ref, unsigned char *sha1) |
a876ed83 | 272 | { |
ed378ec7 | 273 | if (resolve_ref(ref, sha1, 1)) |
a876ed83 JH |
274 | return 0; |
275 | return -1; | |
8a65ff76 LT |
276 | } |
277 | ||
a62be77f | 278 | static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim) |
8a65ff76 | 279 | { |
e1e22e37 LT |
280 | int retval; |
281 | struct ref_list *packed = get_packed_refs(); | |
282 | struct ref_list *loose = get_loose_refs(); | |
8a65ff76 | 283 | |
e1e22e37 LT |
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; | |
6cada6a9 | 290 | } |
e1e22e37 LT |
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; | |
b37a562a LT |
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 | } | |
e1e22e37 LT |
306 | retval = fn(entry->name + trim, entry->sha1); |
307 | if (retval) | |
308 | return retval; | |
309 | } | |
8a65ff76 | 310 | |
e1e22e37 LT |
311 | packed = packed ? packed : loose; |
312 | while (packed) { | |
313 | if (!strncmp(base, packed->name, trim)) { | |
314 | retval = fn(packed->name + trim, packed->sha1); | |
8a65ff76 | 315 | if (retval) |
e1e22e37 | 316 | return retval; |
8a65ff76 | 317 | } |
e1e22e37 | 318 | packed = packed->next; |
8a65ff76 | 319 | } |
e1e22e37 | 320 | return 0; |
8a65ff76 LT |
321 | } |
322 | ||
723c31fe LT |
323 | int head_ref(int (*fn)(const char *path, const unsigned char *sha1)) |
324 | { | |
325 | unsigned char sha1[20]; | |
ed378ec7 | 326 | if (!read_ref("HEAD", sha1)) |
99a0a6e0 | 327 | return fn("HEAD", sha1); |
2f34ba32 | 328 | return 0; |
723c31fe LT |
329 | } |
330 | ||
944d8589 | 331 | int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1)) |
8a65ff76 | 332 | { |
e1e22e37 | 333 | return do_for_each_ref("refs/", fn, 0); |
a62be77f SE |
334 | } |
335 | ||
336 | int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1)) | |
337 | { | |
e1e22e37 | 338 | return do_for_each_ref("refs/tags/", fn, 10); |
a62be77f SE |
339 | } |
340 | ||
341 | int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1)) | |
342 | { | |
e1e22e37 | 343 | return do_for_each_ref("refs/heads/", fn, 11); |
a62be77f SE |
344 | } |
345 | ||
346 | int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1)) | |
347 | { | |
e1e22e37 | 348 | return do_for_each_ref("refs/remotes/", fn, 13); |
8a65ff76 LT |
349 | } |
350 | ||
95fc7512 DB |
351 | int get_ref_sha1(const char *ref, unsigned char *sha1) |
352 | { | |
95fc7512 DB |
353 | if (check_ref_format(ref)) |
354 | return -1; | |
ed378ec7 | 355 | return read_ref(mkpath("refs/%s", ref), sha1); |
95fc7512 DB |
356 | } |
357 | ||
03feddd6 JH |
358 | /* |
359 | * Make sure "ref" is something reasonable to have under ".git/refs/"; | |
360 | * We do not like it if: | |
361 | * | |
362 | * - any path component of it begins with ".", or | |
363 | * - it has double dots "..", or | |
364 | * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or | |
365 | * - it ends with a "/". | |
366 | */ | |
367 | ||
368 | static inline int bad_ref_char(int ch) | |
369 | { | |
370 | return (((unsigned) ch) <= ' ' || | |
68283999 JH |
371 | ch == '~' || ch == '^' || ch == ':' || |
372 | /* 2.13 Pattern Matching Notation */ | |
373 | ch == '?' || ch == '*' || ch == '['); | |
03feddd6 JH |
374 | } |
375 | ||
95fc7512 DB |
376 | int check_ref_format(const char *ref) |
377 | { | |
03feddd6 JH |
378 | int ch, level; |
379 | const char *cp = ref; | |
380 | ||
381 | level = 0; | |
382 | while (1) { | |
383 | while ((ch = *cp++) == '/') | |
384 | ; /* tolerate duplicated slashes */ | |
385 | if (!ch) | |
386 | return -1; /* should not end with slashes */ | |
387 | ||
388 | /* we are at the beginning of the path component */ | |
389 | if (ch == '.' || bad_ref_char(ch)) | |
390 | return -1; | |
391 | ||
392 | /* scan the rest of the path component */ | |
393 | while ((ch = *cp++) != 0) { | |
394 | if (bad_ref_char(ch)) | |
395 | return -1; | |
396 | if (ch == '/') | |
397 | break; | |
398 | if (ch == '.' && *cp == '.') | |
399 | return -1; | |
400 | } | |
401 | level++; | |
402 | if (!ch) { | |
403 | if (level < 2) | |
404 | return -1; /* at least of form "heads/blah" */ | |
405 | return 0; | |
406 | } | |
407 | } | |
95fc7512 DB |
408 | } |
409 | ||
e5f38ec3 | 410 | static struct ref_lock *verify_lock(struct ref_lock *lock, |
4bd18c43 SP |
411 | const unsigned char *old_sha1, int mustexist) |
412 | { | |
434cd0cd LT |
413 | if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist)) { |
414 | error("Can't verify ref %s", lock->ref_name); | |
4bd18c43 SP |
415 | unlock_ref(lock); |
416 | return NULL; | |
417 | } | |
a89fccd2 | 418 | if (hashcmp(lock->old_sha1, old_sha1)) { |
434cd0cd | 419 | error("Ref %s is at %s but expected %s", lock->ref_name, |
4bd18c43 SP |
420 | sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1)); |
421 | unlock_ref(lock); | |
422 | return NULL; | |
423 | } | |
424 | return lock; | |
425 | } | |
426 | ||
ed378ec7 | 427 | static struct ref_lock *lock_ref_sha1_basic(const char *ref, |
4bd18c43 SP |
428 | int plen, |
429 | const unsigned char *old_sha1, int mustexist) | |
430 | { | |
434cd0cd | 431 | char *ref_file; |
ed378ec7 | 432 | const char *orig_ref = ref; |
4bd18c43 | 433 | struct ref_lock *lock; |
732232a1 | 434 | struct stat st; |
4bd18c43 SP |
435 | |
436 | lock = xcalloc(1, sizeof(struct ref_lock)); | |
437 | lock->lock_fd = -1; | |
438 | ||
ed378ec7 LT |
439 | ref = resolve_ref(ref, lock->old_sha1, mustexist); |
440 | if (!ref) { | |
818f477c SP |
441 | int last_errno = errno; |
442 | error("unable to resolve reference %s: %s", | |
ed378ec7 | 443 | orig_ref, strerror(errno)); |
4bd18c43 | 444 | unlock_ref(lock); |
818f477c | 445 | errno = last_errno; |
4bd18c43 SP |
446 | return NULL; |
447 | } | |
c33d5174 | 448 | lock->lk = xcalloc(1, sizeof(struct lock_file)); |
4bd18c43 | 449 | |
434cd0cd | 450 | lock->ref_name = xstrdup(ref); |
ed378ec7 | 451 | lock->log_file = xstrdup(git_path("logs/%s", ref)); |
434cd0cd LT |
452 | ref_file = git_path(ref); |
453 | lock->force_write = lstat(ref_file, &st) && errno == ENOENT; | |
4bd18c43 | 454 | |
434cd0cd LT |
455 | if (safe_create_leading_directories(ref_file)) |
456 | die("unable to create directory for %s", ref_file); | |
457 | lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1); | |
4bd18c43 SP |
458 | |
459 | return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock; | |
460 | } | |
461 | ||
e5f38ec3 | 462 | struct ref_lock *lock_ref_sha1(const char *ref, |
4bd18c43 | 463 | const unsigned char *old_sha1, int mustexist) |
95fc7512 | 464 | { |
53cce84c | 465 | char refpath[PATH_MAX]; |
95fc7512 | 466 | if (check_ref_format(ref)) |
4bd18c43 | 467 | return NULL; |
53cce84c PB |
468 | strcpy(refpath, mkpath("refs/%s", ref)); |
469 | return lock_ref_sha1_basic(refpath, strlen(refpath), | |
470 | old_sha1, mustexist); | |
4bd18c43 SP |
471 | } |
472 | ||
e5f38ec3 | 473 | struct ref_lock *lock_any_ref_for_update(const char *ref, |
4bd18c43 SP |
474 | const unsigned char *old_sha1, int mustexist) |
475 | { | |
ed378ec7 | 476 | return lock_ref_sha1_basic(ref, strlen(ref), old_sha1, mustexist); |
4bd18c43 SP |
477 | } |
478 | ||
e5f38ec3 | 479 | void unlock_ref(struct ref_lock *lock) |
4bd18c43 SP |
480 | { |
481 | if (lock->lock_fd >= 0) { | |
482 | close(lock->lock_fd); | |
c33d5174 JH |
483 | /* Do not free lock->lk -- atexit() still looks at them */ |
484 | if (lock->lk) | |
485 | rollback_lock_file(lock->lk); | |
4bd18c43 | 486 | } |
434cd0cd | 487 | free(lock->ref_name); |
4cac42b1 | 488 | free(lock->log_file); |
4bd18c43 SP |
489 | free(lock); |
490 | } | |
491 | ||
6de08ae6 SP |
492 | static int log_ref_write(struct ref_lock *lock, |
493 | const unsigned char *sha1, const char *msg) | |
494 | { | |
495 | int logfd, written, oflags = O_APPEND | O_WRONLY; | |
496 | unsigned maxlen, len; | |
497 | char *logrec; | |
ff4c8485 | 498 | const char *committer; |
6de08ae6 SP |
499 | |
500 | if (log_all_ref_updates) { | |
501 | if (safe_create_leading_directories(lock->log_file) < 0) | |
502 | return error("unable to create directory for %s", | |
503 | lock->log_file); | |
504 | oflags |= O_CREAT; | |
505 | } | |
506 | ||
507 | logfd = open(lock->log_file, oflags, 0666); | |
508 | if (logfd < 0) { | |
509 | if (!log_all_ref_updates && errno == ENOENT) | |
510 | return 0; | |
511 | return error("Unable to append to %s: %s", | |
512 | lock->log_file, strerror(errno)); | |
513 | } | |
514 | ||
ff4c8485 | 515 | committer = git_committer_info(1); |
6de08ae6 | 516 | if (msg) { |
ff4c8485 | 517 | maxlen = strlen(committer) + strlen(msg) + 2*40 + 5; |
6de08ae6 SP |
518 | logrec = xmalloc(maxlen); |
519 | len = snprintf(logrec, maxlen, "%s %s %s\t%s\n", | |
520 | sha1_to_hex(lock->old_sha1), | |
521 | sha1_to_hex(sha1), | |
ff4c8485 | 522 | committer, |
6de08ae6 | 523 | msg); |
e5f38ec3 JH |
524 | } |
525 | else { | |
ff4c8485 | 526 | maxlen = strlen(committer) + 2*40 + 4; |
6de08ae6 SP |
527 | logrec = xmalloc(maxlen); |
528 | len = snprintf(logrec, maxlen, "%s %s %s\n", | |
529 | sha1_to_hex(lock->old_sha1), | |
530 | sha1_to_hex(sha1), | |
ff4c8485 | 531 | committer); |
6de08ae6 SP |
532 | } |
533 | written = len <= maxlen ? write(logfd, logrec, len) : -1; | |
534 | free(logrec); | |
535 | close(logfd); | |
536 | if (written != len) | |
537 | return error("Unable to append to %s", lock->log_file); | |
538 | return 0; | |
539 | } | |
540 | ||
4bd18c43 SP |
541 | int write_ref_sha1(struct ref_lock *lock, |
542 | const unsigned char *sha1, const char *logmsg) | |
543 | { | |
544 | static char term = '\n'; | |
545 | ||
546 | if (!lock) | |
95fc7512 | 547 | return -1; |
a89fccd2 | 548 | if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) { |
4bd18c43 SP |
549 | unlock_ref(lock); |
550 | return 0; | |
95fc7512 | 551 | } |
4bd18c43 SP |
552 | if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 || |
553 | write(lock->lock_fd, &term, 1) != 1 | |
554 | || close(lock->lock_fd) < 0) { | |
c33d5174 | 555 | error("Couldn't write %s", lock->lk->filename); |
4bd18c43 SP |
556 | unlock_ref(lock); |
557 | return -1; | |
558 | } | |
6de08ae6 SP |
559 | if (log_ref_write(lock, sha1, logmsg) < 0) { |
560 | unlock_ref(lock); | |
561 | return -1; | |
562 | } | |
c33d5174 | 563 | if (commit_lock_file(lock->lk)) { |
434cd0cd | 564 | error("Couldn't set %s", lock->ref_name); |
4bd18c43 SP |
565 | unlock_ref(lock); |
566 | return -1; | |
567 | } | |
568 | lock->lock_fd = -1; | |
569 | unlock_ref(lock); | |
570 | return 0; | |
95fc7512 | 571 | } |
d556fae2 SP |
572 | |
573 | int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1) | |
574 | { | |
e5229042 | 575 | const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec; |
d556fae2 SP |
576 | char *tz_c; |
577 | int logfd, tz; | |
578 | struct stat st; | |
579 | unsigned long date; | |
e5229042 | 580 | unsigned char logged_sha1[20]; |
d556fae2 SP |
581 | |
582 | logfile = git_path("logs/%s", ref); | |
583 | logfd = open(logfile, O_RDONLY, 0); | |
584 | if (logfd < 0) | |
585 | die("Unable to read log %s: %s", logfile, strerror(errno)); | |
586 | fstat(logfd, &st); | |
587 | if (!st.st_size) | |
588 | die("Log %s is empty.", logfile); | |
589 | logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0); | |
590 | close(logfd); | |
591 | ||
e5229042 | 592 | lastrec = NULL; |
d556fae2 SP |
593 | rec = logend = logdata + st.st_size; |
594 | while (logdata < rec) { | |
595 | if (logdata < rec && *(rec-1) == '\n') | |
596 | rec--; | |
e5229042 SP |
597 | lastgt = NULL; |
598 | while (logdata < rec && *(rec-1) != '\n') { | |
d556fae2 | 599 | rec--; |
e5229042 SP |
600 | if (*rec == '>') |
601 | lastgt = rec; | |
602 | } | |
603 | if (!lastgt) | |
d556fae2 | 604 | die("Log %s is corrupt.", logfile); |
e5229042 | 605 | date = strtoul(lastgt + 1, &tz_c, 10); |
d556fae2 | 606 | if (date <= at_time) { |
e5229042 SP |
607 | if (lastrec) { |
608 | if (get_sha1_hex(lastrec, logged_sha1)) | |
609 | die("Log %s is corrupt.", logfile); | |
610 | if (get_sha1_hex(rec + 41, sha1)) | |
611 | die("Log %s is corrupt.", logfile); | |
a89fccd2 | 612 | if (hashcmp(logged_sha1, sha1)) { |
e5229042 SP |
613 | tz = strtoul(tz_c, NULL, 10); |
614 | fprintf(stderr, | |
615 | "warning: Log %s has gap after %s.\n", | |
616 | logfile, show_rfc2822_date(date, tz)); | |
617 | } | |
e5f38ec3 JH |
618 | } |
619 | else if (date == at_time) { | |
e5229042 SP |
620 | if (get_sha1_hex(rec + 41, sha1)) |
621 | die("Log %s is corrupt.", logfile); | |
e5f38ec3 JH |
622 | } |
623 | else { | |
e5229042 SP |
624 | if (get_sha1_hex(rec + 41, logged_sha1)) |
625 | die("Log %s is corrupt.", logfile); | |
a89fccd2 | 626 | if (hashcmp(logged_sha1, sha1)) { |
e5229042 SP |
627 | tz = strtoul(tz_c, NULL, 10); |
628 | fprintf(stderr, | |
629 | "warning: Log %s unexpectedly ended on %s.\n", | |
630 | logfile, show_rfc2822_date(date, tz)); | |
631 | } | |
632 | } | |
d556fae2 SP |
633 | munmap((void*)logdata, st.st_size); |
634 | return 0; | |
635 | } | |
e5229042 | 636 | lastrec = rec; |
d556fae2 SP |
637 | } |
638 | ||
e5229042 SP |
639 | rec = logdata; |
640 | while (rec < logend && *rec != '>' && *rec != '\n') | |
641 | rec++; | |
642 | if (rec == logend || *rec == '\n') | |
d556fae2 | 643 | die("Log %s is corrupt.", logfile); |
e5229042 | 644 | date = strtoul(rec + 1, &tz_c, 10); |
d556fae2 SP |
645 | tz = strtoul(tz_c, NULL, 10); |
646 | if (get_sha1_hex(logdata, sha1)) | |
647 | die("Log %s is corrupt.", logfile); | |
648 | munmap((void*)logdata, st.st_size); | |
649 | fprintf(stderr, "warning: Log %s only goes back to %s.\n", | |
650 | logfile, show_rfc2822_date(date, tz)); | |
651 | return 0; | |
652 | } |