]>
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 | ||
5e290ff7 JH |
69 | /* |
70 | * Future: need to be in "struct repository" | |
71 | * when doing a full libification. | |
72 | */ | |
73 | struct cached_refs { | |
74 | char did_loose; | |
75 | char did_packed; | |
76 | struct ref_list *loose; | |
77 | struct ref_list *packed; | |
78 | } cached_refs; | |
79 | ||
80 | static void free_ref_list(struct ref_list *list) | |
81 | { | |
82 | struct ref_list *next; | |
83 | for ( ; list; list = next) { | |
84 | next = list->next; | |
85 | free(list); | |
86 | } | |
87 | } | |
88 | ||
89 | static void invalidate_cached_refs(void) | |
e1e22e37 | 90 | { |
5e290ff7 JH |
91 | struct cached_refs *ca = &cached_refs; |
92 | ||
93 | if (ca->did_loose && ca->loose) | |
94 | free_ref_list(ca->loose); | |
95 | if (ca->did_packed && ca->packed) | |
96 | free_ref_list(ca->packed); | |
97 | ca->loose = ca->packed = NULL; | |
98 | ca->did_loose = ca->did_packed = 0; | |
99 | } | |
e1e22e37 | 100 | |
5e290ff7 JH |
101 | static struct ref_list *get_packed_refs(void) |
102 | { | |
103 | if (!cached_refs.did_packed) { | |
104 | struct ref_list *refs = NULL; | |
e1e22e37 LT |
105 | FILE *f = fopen(git_path("packed-refs"), "r"); |
106 | if (f) { | |
107 | struct ref_list *list = NULL; | |
108 | char refline[PATH_MAX]; | |
109 | while (fgets(refline, sizeof(refline), f)) { | |
110 | unsigned char sha1[20]; | |
111 | const char *name = parse_ref_line(refline, sha1); | |
112 | if (!name) | |
113 | continue; | |
8da19775 | 114 | list = add_ref(name, sha1, REF_ISPACKED, list); |
e1e22e37 LT |
115 | } |
116 | fclose(f); | |
117 | refs = list; | |
118 | } | |
5e290ff7 JH |
119 | cached_refs.packed = refs; |
120 | cached_refs.did_packed = 1; | |
e1e22e37 | 121 | } |
5e290ff7 | 122 | return cached_refs.packed; |
e1e22e37 LT |
123 | } |
124 | ||
125 | static struct ref_list *get_ref_dir(const char *base, struct ref_list *list) | |
126 | { | |
127 | DIR *dir = opendir(git_path("%s", base)); | |
128 | ||
129 | if (dir) { | |
130 | struct dirent *de; | |
131 | int baselen = strlen(base); | |
ed378ec7 | 132 | char *ref = xmalloc(baselen + 257); |
e1e22e37 | 133 | |
ed378ec7 | 134 | memcpy(ref, base, baselen); |
e1e22e37 | 135 | if (baselen && base[baselen-1] != '/') |
ed378ec7 | 136 | ref[baselen++] = '/'; |
e1e22e37 LT |
137 | |
138 | while ((de = readdir(dir)) != NULL) { | |
139 | unsigned char sha1[20]; | |
140 | struct stat st; | |
8da19775 | 141 | int flag; |
e1e22e37 LT |
142 | int namelen; |
143 | ||
144 | if (de->d_name[0] == '.') | |
145 | continue; | |
146 | namelen = strlen(de->d_name); | |
147 | if (namelen > 255) | |
148 | continue; | |
149 | if (has_extension(de->d_name, ".lock")) | |
150 | continue; | |
ed378ec7 LT |
151 | memcpy(ref + baselen, de->d_name, namelen+1); |
152 | if (stat(git_path("%s", ref), &st) < 0) | |
e1e22e37 LT |
153 | continue; |
154 | if (S_ISDIR(st.st_mode)) { | |
ed378ec7 | 155 | list = get_ref_dir(ref, list); |
e1e22e37 LT |
156 | continue; |
157 | } | |
8da19775 | 158 | if (!resolve_ref(ref, sha1, 1, &flag)) { |
ed378ec7 | 159 | error("%s points nowhere!", ref); |
e1e22e37 LT |
160 | continue; |
161 | } | |
8da19775 | 162 | list = add_ref(ref, sha1, flag, list); |
e1e22e37 | 163 | } |
ed378ec7 | 164 | free(ref); |
e1e22e37 LT |
165 | closedir(dir); |
166 | } | |
167 | return list; | |
168 | } | |
169 | ||
170 | static struct ref_list *get_loose_refs(void) | |
171 | { | |
5e290ff7 JH |
172 | if (!cached_refs.did_loose) { |
173 | cached_refs.loose = get_ref_dir("refs", NULL); | |
174 | cached_refs.did_loose = 1; | |
e1e22e37 | 175 | } |
5e290ff7 | 176 | return cached_refs.loose; |
e1e22e37 LT |
177 | } |
178 | ||
ca8db142 LT |
179 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
180 | #define MAXDEPTH 5 | |
181 | ||
8da19775 | 182 | const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag) |
8a65ff76 | 183 | { |
a876ed83 JH |
184 | int depth = MAXDEPTH, len; |
185 | char buffer[256]; | |
ed378ec7 | 186 | static char ref_buffer[256]; |
ca8db142 | 187 | |
8da19775 JH |
188 | if (flag) |
189 | *flag = 0; | |
190 | ||
a876ed83 | 191 | for (;;) { |
ed378ec7 | 192 | const char *path = git_path("%s", ref); |
a876ed83 JH |
193 | struct stat st; |
194 | char *buf; | |
195 | int fd; | |
8a65ff76 | 196 | |
a876ed83 JH |
197 | if (--depth < 0) |
198 | return NULL; | |
ca8db142 | 199 | |
a876ed83 JH |
200 | /* Special case: non-existing file. |
201 | * Not having the refs/heads/new-branch is OK | |
202 | * if we are writing into it, so is .git/HEAD | |
203 | * that points at refs/heads/master still to be | |
204 | * born. It is NOT OK if we are resolving for | |
205 | * reading. | |
206 | */ | |
207 | if (lstat(path, &st) < 0) { | |
434cd0cd LT |
208 | struct ref_list *list = get_packed_refs(); |
209 | while (list) { | |
210 | if (!strcmp(ref, list->name)) { | |
211 | hashcpy(sha1, list->sha1); | |
8da19775 JH |
212 | if (flag) |
213 | *flag |= REF_ISPACKED; | |
434cd0cd LT |
214 | return ref; |
215 | } | |
216 | list = list->next; | |
217 | } | |
a876ed83 JH |
218 | if (reading || errno != ENOENT) |
219 | return NULL; | |
e702496e | 220 | hashclr(sha1); |
ed378ec7 | 221 | return ref; |
a876ed83 | 222 | } |
ca8db142 | 223 | |
a876ed83 JH |
224 | /* Follow "normalized" - ie "refs/.." symlinks by hand */ |
225 | if (S_ISLNK(st.st_mode)) { | |
226 | len = readlink(path, buffer, sizeof(buffer)-1); | |
227 | if (len >= 5 && !memcmp("refs/", buffer, 5)) { | |
ed378ec7 LT |
228 | buffer[len] = 0; |
229 | strcpy(ref_buffer, buffer); | |
230 | ref = ref_buffer; | |
8da19775 JH |
231 | if (flag) |
232 | *flag |= REF_ISSYMREF; | |
a876ed83 JH |
233 | continue; |
234 | } | |
ca8db142 | 235 | } |
a876ed83 | 236 | |
7a21632f DS |
237 | /* Is it a directory? */ |
238 | if (S_ISDIR(st.st_mode)) { | |
239 | errno = EISDIR; | |
240 | return NULL; | |
241 | } | |
242 | ||
a876ed83 JH |
243 | /* |
244 | * Anything else, just open it and try to use it as | |
245 | * a ref | |
246 | */ | |
247 | fd = open(path, O_RDONLY); | |
248 | if (fd < 0) | |
249 | return NULL; | |
250 | len = read(fd, buffer, sizeof(buffer)-1); | |
251 | close(fd); | |
252 | ||
253 | /* | |
254 | * Is it a symbolic ref? | |
255 | */ | |
256 | if (len < 4 || memcmp("ref:", buffer, 4)) | |
257 | break; | |
258 | buf = buffer + 4; | |
259 | len -= 4; | |
260 | while (len && isspace(*buf)) | |
261 | buf++, len--; | |
262 | while (len && isspace(buf[len-1])) | |
ed378ec7 LT |
263 | len--; |
264 | buf[len] = 0; | |
265 | memcpy(ref_buffer, buf, len + 1); | |
266 | ref = ref_buffer; | |
8da19775 JH |
267 | if (flag) |
268 | *flag |= REF_ISSYMREF; | |
8a65ff76 | 269 | } |
a876ed83 JH |
270 | if (len < 40 || get_sha1_hex(buffer, sha1)) |
271 | return NULL; | |
ed378ec7 | 272 | return ref; |
a876ed83 JH |
273 | } |
274 | ||
ed378ec7 | 275 | int create_symref(const char *ref_target, const char *refs_heads_master) |
8098a178 | 276 | { |
8098a178 JH |
277 | const char *lockpath; |
278 | char ref[1000]; | |
279 | int fd, len, written; | |
ed378ec7 | 280 | const char *git_HEAD = git_path("%s", ref_target); |
8098a178 | 281 | |
9f0bb90d JH |
282 | #ifndef NO_SYMLINK_HEAD |
283 | if (prefer_symlink_refs) { | |
f8348be3 JS |
284 | unlink(git_HEAD); |
285 | if (!symlink(refs_heads_master, git_HEAD)) | |
286 | return 0; | |
287 | fprintf(stderr, "no symlink - falling back to symbolic ref\n"); | |
288 | } | |
303958dc JS |
289 | #endif |
290 | ||
8098a178 JH |
291 | len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master); |
292 | if (sizeof(ref) <= len) { | |
293 | error("refname too long: %s", refs_heads_master); | |
294 | return -1; | |
295 | } | |
296 | lockpath = mkpath("%s.lock", git_HEAD); | |
297 | fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
298 | written = write(fd, ref, len); | |
299 | close(fd); | |
300 | if (written != len) { | |
301 | unlink(lockpath); | |
302 | error("Unable to write to %s", lockpath); | |
303 | return -2; | |
304 | } | |
305 | if (rename(lockpath, git_HEAD) < 0) { | |
306 | unlink(lockpath); | |
307 | error("Unable to create %s", git_HEAD); | |
308 | return -3; | |
309 | } | |
138086a7 JH |
310 | if (adjust_shared_perm(git_HEAD)) { |
311 | unlink(lockpath); | |
312 | error("Unable to fix permissions on %s", lockpath); | |
313 | return -4; | |
314 | } | |
8098a178 | 315 | return 0; |
8098a178 JH |
316 | } |
317 | ||
ed378ec7 | 318 | int read_ref(const char *ref, unsigned char *sha1) |
a876ed83 | 319 | { |
8da19775 | 320 | if (resolve_ref(ref, sha1, 1, NULL)) |
a876ed83 JH |
321 | return 0; |
322 | return -1; | |
8a65ff76 LT |
323 | } |
324 | ||
8da19775 JH |
325 | static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, |
326 | void *cb_data) | |
8a65ff76 | 327 | { |
e1e22e37 LT |
328 | int retval; |
329 | struct ref_list *packed = get_packed_refs(); | |
330 | struct ref_list *loose = get_loose_refs(); | |
8a65ff76 | 331 | |
e1e22e37 LT |
332 | while (packed && loose) { |
333 | struct ref_list *entry; | |
334 | int cmp = strcmp(packed->name, loose->name); | |
335 | if (!cmp) { | |
336 | packed = packed->next; | |
337 | continue; | |
6cada6a9 | 338 | } |
e1e22e37 LT |
339 | if (cmp > 0) { |
340 | entry = loose; | |
341 | loose = loose->next; | |
342 | } else { | |
343 | entry = packed; | |
344 | packed = packed->next; | |
345 | } | |
346 | if (strncmp(base, entry->name, trim)) | |
347 | continue; | |
b37a562a LT |
348 | if (is_null_sha1(entry->sha1)) |
349 | continue; | |
350 | if (!has_sha1_file(entry->sha1)) { | |
351 | error("%s does not point to a valid object!", entry->name); | |
352 | continue; | |
353 | } | |
8da19775 JH |
354 | retval = fn(entry->name + trim, entry->sha1, |
355 | entry->flag, cb_data); | |
e1e22e37 LT |
356 | if (retval) |
357 | return retval; | |
358 | } | |
8a65ff76 | 359 | |
e1e22e37 LT |
360 | packed = packed ? packed : loose; |
361 | while (packed) { | |
362 | if (!strncmp(base, packed->name, trim)) { | |
8da19775 JH |
363 | retval = fn(packed->name + trim, packed->sha1, |
364 | packed->flag, cb_data); | |
8a65ff76 | 365 | if (retval) |
e1e22e37 | 366 | return retval; |
8a65ff76 | 367 | } |
e1e22e37 | 368 | packed = packed->next; |
8a65ff76 | 369 | } |
e1e22e37 | 370 | return 0; |
8a65ff76 LT |
371 | } |
372 | ||
cb5d709f | 373 | int head_ref(each_ref_fn fn, void *cb_data) |
723c31fe LT |
374 | { |
375 | unsigned char sha1[20]; | |
8da19775 JH |
376 | int flag; |
377 | ||
378 | if (resolve_ref("HEAD", sha1, 1, &flag)) | |
379 | return fn("HEAD", sha1, flag, cb_data); | |
2f34ba32 | 380 | return 0; |
723c31fe LT |
381 | } |
382 | ||
cb5d709f | 383 | int for_each_ref(each_ref_fn fn, void *cb_data) |
8a65ff76 | 384 | { |
cb5d709f | 385 | return do_for_each_ref("refs/", fn, 0, cb_data); |
a62be77f SE |
386 | } |
387 | ||
cb5d709f | 388 | int for_each_tag_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 389 | { |
cb5d709f | 390 | return do_for_each_ref("refs/tags/", fn, 10, cb_data); |
a62be77f SE |
391 | } |
392 | ||
cb5d709f | 393 | int for_each_branch_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 394 | { |
cb5d709f | 395 | return do_for_each_ref("refs/heads/", fn, 11, cb_data); |
a62be77f SE |
396 | } |
397 | ||
cb5d709f | 398 | int for_each_remote_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 399 | { |
cb5d709f | 400 | return do_for_each_ref("refs/remotes/", fn, 13, cb_data); |
8a65ff76 LT |
401 | } |
402 | ||
cb5d709f JH |
403 | /* NEEDSWORK: This is only used by ssh-upload and it should go; the |
404 | * caller should do resolve_ref or read_ref like everybody else. Or | |
405 | * maybe everybody else should use get_ref_sha1() instead of doing | |
406 | * read_ref(). | |
407 | */ | |
95fc7512 DB |
408 | int get_ref_sha1(const char *ref, unsigned char *sha1) |
409 | { | |
95fc7512 DB |
410 | if (check_ref_format(ref)) |
411 | return -1; | |
ed378ec7 | 412 | return read_ref(mkpath("refs/%s", ref), sha1); |
95fc7512 DB |
413 | } |
414 | ||
03feddd6 JH |
415 | /* |
416 | * Make sure "ref" is something reasonable to have under ".git/refs/"; | |
417 | * We do not like it if: | |
418 | * | |
419 | * - any path component of it begins with ".", or | |
420 | * - it has double dots "..", or | |
421 | * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or | |
422 | * - it ends with a "/". | |
423 | */ | |
424 | ||
425 | static inline int bad_ref_char(int ch) | |
426 | { | |
427 | return (((unsigned) ch) <= ' ' || | |
68283999 JH |
428 | ch == '~' || ch == '^' || ch == ':' || |
429 | /* 2.13 Pattern Matching Notation */ | |
430 | ch == '?' || ch == '*' || ch == '['); | |
03feddd6 JH |
431 | } |
432 | ||
95fc7512 DB |
433 | int check_ref_format(const char *ref) |
434 | { | |
03feddd6 JH |
435 | int ch, level; |
436 | const char *cp = ref; | |
437 | ||
438 | level = 0; | |
439 | while (1) { | |
440 | while ((ch = *cp++) == '/') | |
441 | ; /* tolerate duplicated slashes */ | |
442 | if (!ch) | |
443 | return -1; /* should not end with slashes */ | |
444 | ||
445 | /* we are at the beginning of the path component */ | |
446 | if (ch == '.' || bad_ref_char(ch)) | |
447 | return -1; | |
448 | ||
449 | /* scan the rest of the path component */ | |
450 | while ((ch = *cp++) != 0) { | |
451 | if (bad_ref_char(ch)) | |
452 | return -1; | |
453 | if (ch == '/') | |
454 | break; | |
455 | if (ch == '.' && *cp == '.') | |
456 | return -1; | |
457 | } | |
458 | level++; | |
459 | if (!ch) { | |
460 | if (level < 2) | |
461 | return -1; /* at least of form "heads/blah" */ | |
462 | return 0; | |
463 | } | |
464 | } | |
95fc7512 DB |
465 | } |
466 | ||
e5f38ec3 | 467 | static struct ref_lock *verify_lock(struct ref_lock *lock, |
4bd18c43 SP |
468 | const unsigned char *old_sha1, int mustexist) |
469 | { | |
8da19775 | 470 | if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) { |
434cd0cd | 471 | error("Can't verify ref %s", lock->ref_name); |
4bd18c43 SP |
472 | unlock_ref(lock); |
473 | return NULL; | |
474 | } | |
a89fccd2 | 475 | if (hashcmp(lock->old_sha1, old_sha1)) { |
434cd0cd | 476 | error("Ref %s is at %s but expected %s", lock->ref_name, |
4bd18c43 SP |
477 | sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1)); |
478 | unlock_ref(lock); | |
479 | return NULL; | |
480 | } | |
481 | return lock; | |
482 | } | |
483 | ||
bc7127ef JH |
484 | static int remove_empty_dir_recursive(char *path, int len) |
485 | { | |
486 | DIR *dir = opendir(path); | |
487 | struct dirent *e; | |
488 | int ret = 0; | |
489 | ||
490 | if (!dir) | |
491 | return -1; | |
492 | if (path[len-1] != '/') | |
493 | path[len++] = '/'; | |
494 | while ((e = readdir(dir)) != NULL) { | |
495 | struct stat st; | |
496 | int namlen; | |
497 | if ((e->d_name[0] == '.') && | |
498 | ((e->d_name[1] == 0) || | |
499 | ((e->d_name[1] == '.') && e->d_name[2] == 0))) | |
500 | continue; /* "." and ".." */ | |
501 | ||
502 | namlen = strlen(e->d_name); | |
503 | if ((len + namlen < PATH_MAX) && | |
504 | strcpy(path + len, e->d_name) && | |
505 | !lstat(path, &st) && | |
506 | S_ISDIR(st.st_mode) && | |
28bed6ea | 507 | !remove_empty_dir_recursive(path, len + namlen)) |
bc7127ef JH |
508 | continue; /* happy */ |
509 | ||
510 | /* path too long, stat fails, or non-directory still exists */ | |
511 | ret = -1; | |
512 | break; | |
513 | } | |
514 | closedir(dir); | |
515 | if (!ret) { | |
516 | path[len] = 0; | |
517 | ret = rmdir(path); | |
518 | } | |
519 | return ret; | |
520 | } | |
521 | ||
522 | static int remove_empty_directories(char *file) | |
523 | { | |
524 | /* we want to create a file but there is a directory there; | |
525 | * if that is an empty directory (or a directory that contains | |
526 | * only empty directories), remove them. | |
527 | */ | |
528 | char path[PATH_MAX]; | |
529 | int len = strlen(file); | |
530 | ||
531 | if (len >= PATH_MAX) /* path too long ;-) */ | |
532 | return -1; | |
533 | strcpy(path, file); | |
534 | return remove_empty_dir_recursive(path, len); | |
535 | } | |
536 | ||
c0277d15 | 537 | static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag) |
4bd18c43 | 538 | { |
434cd0cd | 539 | char *ref_file; |
ed378ec7 | 540 | const char *orig_ref = ref; |
4bd18c43 | 541 | struct ref_lock *lock; |
732232a1 | 542 | struct stat st; |
5cc3cef9 | 543 | int last_errno = 0; |
4431fcc4 | 544 | int mustexist = (old_sha1 && !is_null_sha1(old_sha1)); |
4bd18c43 SP |
545 | |
546 | lock = xcalloc(1, sizeof(struct ref_lock)); | |
547 | lock->lock_fd = -1; | |
548 | ||
c0277d15 | 549 | ref = resolve_ref(ref, lock->old_sha1, mustexist, flag); |
bc7127ef JH |
550 | if (!ref && errno == EISDIR) { |
551 | /* we are trying to lock foo but we used to | |
552 | * have foo/bar which now does not exist; | |
553 | * it is normal for the empty directory 'foo' | |
554 | * to remain. | |
555 | */ | |
556 | ref_file = git_path("%s", orig_ref); | |
5cc3cef9 JH |
557 | if (remove_empty_directories(ref_file)) { |
558 | last_errno = errno; | |
559 | error("there are still refs under '%s'", orig_ref); | |
560 | goto error_return; | |
561 | } | |
c0277d15 | 562 | ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, flag); |
bc7127ef | 563 | } |
ed378ec7 | 564 | if (!ref) { |
5cc3cef9 | 565 | last_errno = errno; |
818f477c | 566 | error("unable to resolve reference %s: %s", |
ed378ec7 | 567 | orig_ref, strerror(errno)); |
5cc3cef9 | 568 | goto error_return; |
4bd18c43 | 569 | } |
22a3844e JH |
570 | if (is_null_sha1(lock->old_sha1)) { |
571 | /* The ref did not exist and we are creating it. | |
572 | * Make sure there is no existing ref that is packed | |
573 | * whose name begins with our refname, nor a ref whose | |
574 | * name is a proper prefix of our refname. | |
575 | */ | |
576 | int namlen = strlen(ref); /* e.g. 'foo/bar' */ | |
577 | struct ref_list *list = get_packed_refs(); | |
578 | while (list) { | |
579 | /* list->name could be 'foo' or 'foo/bar/baz' */ | |
580 | int len = strlen(list->name); | |
581 | int cmplen = (namlen < len) ? namlen : len; | |
582 | const char *lead = (namlen < len) ? list->name : ref; | |
583 | ||
584 | if (!strncmp(ref, list->name, cmplen) && | |
585 | lead[cmplen] == '/') { | |
586 | error("'%s' exists; cannot create '%s'", | |
587 | list->name, ref); | |
588 | goto error_return; | |
589 | } | |
590 | list = list->next; | |
591 | } | |
592 | } | |
593 | ||
c33d5174 | 594 | lock->lk = xcalloc(1, sizeof(struct lock_file)); |
4bd18c43 | 595 | |
434cd0cd | 596 | lock->ref_name = xstrdup(ref); |
ed378ec7 | 597 | lock->log_file = xstrdup(git_path("logs/%s", ref)); |
7c1a278d | 598 | ref_file = git_path("%s", ref); |
434cd0cd | 599 | lock->force_write = lstat(ref_file, &st) && errno == ENOENT; |
4bd18c43 | 600 | |
5cc3cef9 JH |
601 | if (safe_create_leading_directories(ref_file)) { |
602 | last_errno = errno; | |
603 | error("unable to create directory for %s", ref_file); | |
604 | goto error_return; | |
605 | } | |
434cd0cd | 606 | lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1); |
4bd18c43 SP |
607 | |
608 | return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock; | |
5cc3cef9 JH |
609 | |
610 | error_return: | |
611 | unlock_ref(lock); | |
612 | errno = last_errno; | |
613 | return NULL; | |
4bd18c43 SP |
614 | } |
615 | ||
4431fcc4 | 616 | struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1) |
95fc7512 | 617 | { |
53cce84c | 618 | char refpath[PATH_MAX]; |
95fc7512 | 619 | if (check_ref_format(ref)) |
4bd18c43 | 620 | return NULL; |
53cce84c | 621 | strcpy(refpath, mkpath("refs/%s", ref)); |
c0277d15 | 622 | return lock_ref_sha1_basic(refpath, old_sha1, NULL); |
4bd18c43 SP |
623 | } |
624 | ||
4431fcc4 | 625 | struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1) |
4bd18c43 | 626 | { |
c0277d15 JH |
627 | return lock_ref_sha1_basic(ref, old_sha1, NULL); |
628 | } | |
629 | ||
26a063a1 JH |
630 | static struct lock_file packlock; |
631 | ||
c0277d15 JH |
632 | static int repack_without_ref(const char *refname) |
633 | { | |
634 | struct ref_list *list, *packed_ref_list; | |
635 | int fd; | |
636 | int found = 0; | |
c0277d15 JH |
637 | |
638 | packed_ref_list = get_packed_refs(); | |
639 | for (list = packed_ref_list; list; list = list->next) { | |
640 | if (!strcmp(refname, list->name)) { | |
641 | found = 1; | |
642 | break; | |
643 | } | |
644 | } | |
645 | if (!found) | |
646 | return 0; | |
647 | memset(&packlock, 0, sizeof(packlock)); | |
648 | fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0); | |
649 | if (fd < 0) | |
650 | return error("cannot delete '%s' from packed refs", refname); | |
651 | ||
652 | for (list = packed_ref_list; list; list = list->next) { | |
653 | char line[PATH_MAX + 100]; | |
654 | int len; | |
655 | ||
656 | if (!strcmp(refname, list->name)) | |
657 | continue; | |
658 | len = snprintf(line, sizeof(line), "%s %s\n", | |
659 | sha1_to_hex(list->sha1), list->name); | |
660 | /* this should not happen but just being defensive */ | |
661 | if (len > sizeof(line)) | |
662 | die("too long a refname '%s'", list->name); | |
663 | write_or_die(fd, line, len); | |
664 | } | |
665 | return commit_lock_file(&packlock); | |
666 | } | |
667 | ||
668 | int delete_ref(const char *refname, unsigned char *sha1) | |
669 | { | |
670 | struct ref_lock *lock; | |
671 | int err, i, ret = 0, flag = 0; | |
672 | ||
673 | lock = lock_ref_sha1_basic(refname, sha1, &flag); | |
674 | if (!lock) | |
675 | return 1; | |
676 | if (!(flag & REF_ISPACKED)) { | |
677 | /* loose */ | |
678 | i = strlen(lock->lk->filename) - 5; /* .lock */ | |
679 | lock->lk->filename[i] = 0; | |
680 | err = unlink(lock->lk->filename); | |
681 | if (err) { | |
682 | ret = 1; | |
683 | error("unlink(%s) failed: %s", | |
684 | lock->lk->filename, strerror(errno)); | |
685 | } | |
686 | lock->lk->filename[i] = '.'; | |
687 | } | |
688 | /* removing the loose one could have resurrected an earlier | |
689 | * packed one. Also, if it was not loose we need to repack | |
690 | * without it. | |
691 | */ | |
692 | ret |= repack_without_ref(refname); | |
693 | ||
694 | err = unlink(lock->log_file); | |
695 | if (err && errno != ENOENT) | |
696 | fprintf(stderr, "warning: unlink(%s) failed: %s", | |
697 | lock->log_file, strerror(errno)); | |
698 | invalidate_cached_refs(); | |
699 | unlock_ref(lock); | |
700 | return ret; | |
4bd18c43 SP |
701 | } |
702 | ||
e5f38ec3 | 703 | void unlock_ref(struct ref_lock *lock) |
4bd18c43 SP |
704 | { |
705 | if (lock->lock_fd >= 0) { | |
706 | close(lock->lock_fd); | |
c33d5174 JH |
707 | /* Do not free lock->lk -- atexit() still looks at them */ |
708 | if (lock->lk) | |
709 | rollback_lock_file(lock->lk); | |
4bd18c43 | 710 | } |
434cd0cd | 711 | free(lock->ref_name); |
4cac42b1 | 712 | free(lock->log_file); |
4bd18c43 SP |
713 | free(lock); |
714 | } | |
715 | ||
6de08ae6 SP |
716 | static int log_ref_write(struct ref_lock *lock, |
717 | const unsigned char *sha1, const char *msg) | |
718 | { | |
719 | int logfd, written, oflags = O_APPEND | O_WRONLY; | |
720 | unsigned maxlen, len; | |
721 | char *logrec; | |
ff4c8485 | 722 | const char *committer; |
6de08ae6 | 723 | |
4057deb5 JH |
724 | if (log_all_ref_updates && |
725 | !strncmp(lock->ref_name, "refs/heads/", 11)) { | |
6de08ae6 SP |
726 | if (safe_create_leading_directories(lock->log_file) < 0) |
727 | return error("unable to create directory for %s", | |
728 | lock->log_file); | |
729 | oflags |= O_CREAT; | |
730 | } | |
731 | ||
732 | logfd = open(lock->log_file, oflags, 0666); | |
733 | if (logfd < 0) { | |
1974bf62 | 734 | if (!(oflags & O_CREAT) && errno == ENOENT) |
6de08ae6 SP |
735 | return 0; |
736 | return error("Unable to append to %s: %s", | |
737 | lock->log_file, strerror(errno)); | |
738 | } | |
739 | ||
ff4c8485 | 740 | committer = git_committer_info(1); |
6de08ae6 | 741 | if (msg) { |
ff4c8485 | 742 | maxlen = strlen(committer) + strlen(msg) + 2*40 + 5; |
6de08ae6 SP |
743 | logrec = xmalloc(maxlen); |
744 | len = snprintf(logrec, maxlen, "%s %s %s\t%s\n", | |
745 | sha1_to_hex(lock->old_sha1), | |
746 | sha1_to_hex(sha1), | |
ff4c8485 | 747 | committer, |
6de08ae6 | 748 | msg); |
e5f38ec3 JH |
749 | } |
750 | else { | |
ff4c8485 | 751 | maxlen = strlen(committer) + 2*40 + 4; |
6de08ae6 SP |
752 | logrec = xmalloc(maxlen); |
753 | len = snprintf(logrec, maxlen, "%s %s %s\n", | |
754 | sha1_to_hex(lock->old_sha1), | |
755 | sha1_to_hex(sha1), | |
ff4c8485 | 756 | committer); |
6de08ae6 SP |
757 | } |
758 | written = len <= maxlen ? write(logfd, logrec, len) : -1; | |
759 | free(logrec); | |
760 | close(logfd); | |
761 | if (written != len) | |
762 | return error("Unable to append to %s", lock->log_file); | |
763 | return 0; | |
764 | } | |
765 | ||
4bd18c43 SP |
766 | int write_ref_sha1(struct ref_lock *lock, |
767 | const unsigned char *sha1, const char *logmsg) | |
768 | { | |
769 | static char term = '\n'; | |
770 | ||
771 | if (!lock) | |
95fc7512 | 772 | return -1; |
a89fccd2 | 773 | if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) { |
4bd18c43 SP |
774 | unlock_ref(lock); |
775 | return 0; | |
95fc7512 | 776 | } |
4bd18c43 SP |
777 | if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 || |
778 | write(lock->lock_fd, &term, 1) != 1 | |
779 | || close(lock->lock_fd) < 0) { | |
c33d5174 | 780 | error("Couldn't write %s", lock->lk->filename); |
4bd18c43 SP |
781 | unlock_ref(lock); |
782 | return -1; | |
783 | } | |
5e290ff7 | 784 | invalidate_cached_refs(); |
6de08ae6 SP |
785 | if (log_ref_write(lock, sha1, logmsg) < 0) { |
786 | unlock_ref(lock); | |
787 | return -1; | |
788 | } | |
c33d5174 | 789 | if (commit_lock_file(lock->lk)) { |
434cd0cd | 790 | error("Couldn't set %s", lock->ref_name); |
4bd18c43 SP |
791 | unlock_ref(lock); |
792 | return -1; | |
793 | } | |
794 | lock->lock_fd = -1; | |
795 | unlock_ref(lock); | |
796 | return 0; | |
95fc7512 | 797 | } |
d556fae2 SP |
798 | |
799 | int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1) | |
800 | { | |
e5229042 | 801 | const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec; |
d556fae2 SP |
802 | char *tz_c; |
803 | int logfd, tz; | |
804 | struct stat st; | |
805 | unsigned long date; | |
e5229042 | 806 | unsigned char logged_sha1[20]; |
d556fae2 SP |
807 | |
808 | logfile = git_path("logs/%s", ref); | |
809 | logfd = open(logfile, O_RDONLY, 0); | |
810 | if (logfd < 0) | |
811 | die("Unable to read log %s: %s", logfile, strerror(errno)); | |
812 | fstat(logfd, &st); | |
813 | if (!st.st_size) | |
814 | die("Log %s is empty.", logfile); | |
815 | logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0); | |
816 | close(logfd); | |
817 | ||
e5229042 | 818 | lastrec = NULL; |
d556fae2 SP |
819 | rec = logend = logdata + st.st_size; |
820 | while (logdata < rec) { | |
821 | if (logdata < rec && *(rec-1) == '\n') | |
822 | rec--; | |
e5229042 SP |
823 | lastgt = NULL; |
824 | while (logdata < rec && *(rec-1) != '\n') { | |
d556fae2 | 825 | rec--; |
e5229042 SP |
826 | if (*rec == '>') |
827 | lastgt = rec; | |
828 | } | |
829 | if (!lastgt) | |
d556fae2 | 830 | die("Log %s is corrupt.", logfile); |
e5229042 | 831 | date = strtoul(lastgt + 1, &tz_c, 10); |
d556fae2 | 832 | if (date <= at_time) { |
e5229042 SP |
833 | if (lastrec) { |
834 | if (get_sha1_hex(lastrec, logged_sha1)) | |
835 | die("Log %s is corrupt.", logfile); | |
836 | if (get_sha1_hex(rec + 41, sha1)) | |
837 | die("Log %s is corrupt.", logfile); | |
a89fccd2 | 838 | if (hashcmp(logged_sha1, sha1)) { |
e5229042 SP |
839 | tz = strtoul(tz_c, NULL, 10); |
840 | fprintf(stderr, | |
841 | "warning: Log %s has gap after %s.\n", | |
842 | logfile, show_rfc2822_date(date, tz)); | |
843 | } | |
e5f38ec3 JH |
844 | } |
845 | else if (date == at_time) { | |
e5229042 SP |
846 | if (get_sha1_hex(rec + 41, sha1)) |
847 | die("Log %s is corrupt.", logfile); | |
e5f38ec3 JH |
848 | } |
849 | else { | |
e5229042 SP |
850 | if (get_sha1_hex(rec + 41, logged_sha1)) |
851 | die("Log %s is corrupt.", logfile); | |
a89fccd2 | 852 | if (hashcmp(logged_sha1, sha1)) { |
e5229042 SP |
853 | tz = strtoul(tz_c, NULL, 10); |
854 | fprintf(stderr, | |
855 | "warning: Log %s unexpectedly ended on %s.\n", | |
856 | logfile, show_rfc2822_date(date, tz)); | |
857 | } | |
858 | } | |
d556fae2 SP |
859 | munmap((void*)logdata, st.st_size); |
860 | return 0; | |
861 | } | |
e5229042 | 862 | lastrec = rec; |
d556fae2 SP |
863 | } |
864 | ||
e5229042 SP |
865 | rec = logdata; |
866 | while (rec < logend && *rec != '>' && *rec != '\n') | |
867 | rec++; | |
868 | if (rec == logend || *rec == '\n') | |
d556fae2 | 869 | die("Log %s is corrupt.", logfile); |
e5229042 | 870 | date = strtoul(rec + 1, &tz_c, 10); |
d556fae2 SP |
871 | tz = strtoul(tz_c, NULL, 10); |
872 | if (get_sha1_hex(logdata, sha1)) | |
873 | die("Log %s is corrupt.", logfile); | |
874 | munmap((void*)logdata, st.st_size); | |
875 | fprintf(stderr, "warning: Log %s only goes back to %s.\n", | |
876 | logfile, show_rfc2822_date(date, tz)); | |
877 | return 0; | |
878 | } |