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