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