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