6 /* We allow "recursive" symbolic refs. Only within reason, though */
9 const char *resolve_ref(const char *path
, unsigned char *sha1
, int reading
)
11 int depth
= MAXDEPTH
, len
;
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
29 if (lstat(path
, &st
) < 0) {
30 if (reading
|| errno
!= ENOENT
)
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
);
46 * Anything else, just open it and try to use it as
49 fd
= open(path
, O_RDONLY
);
52 len
= read(fd
, buffer
, sizeof(buffer
)-1);
56 * Is it a symbolic ref?
58 if (len
< 4 || memcmp("ref:", buffer
, 4))
62 while (len
&& isspace(*buf
))
64 while (len
&& isspace(buf
[len
-1]))
66 path
= git_path("%.*s", len
, buf
);
68 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
73 int create_symref(const char *git_HEAD
, const char *refs_heads_master
)
79 #ifndef NO_SYMLINK_HEAD
80 if (prefer_symlink_refs
) {
82 if (!symlink(refs_heads_master
, git_HEAD
))
84 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
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
);
93 lockpath
= mkpath("%s.lock", git_HEAD
);
94 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
95 written
= write(fd
, ref
, len
);
99 error("Unable to write to %s", lockpath
);
102 if (rename(lockpath
, git_HEAD
) < 0) {
104 error("Unable to create %s", git_HEAD
);
107 if (adjust_shared_perm(git_HEAD
)) {
109 error("Unable to fix permissions on %s", lockpath
);
115 int read_ref(const char *filename
, unsigned char *sha1
)
117 if (resolve_ref(filename
, sha1
, 1))
122 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
), int trim
)
125 DIR *dir
= opendir(git_path("%s", base
));
129 int baselen
= strlen(base
);
130 char *path
= xmalloc(baselen
+ 257);
132 if (!strncmp(base
, "./", 2)) {
136 memcpy(path
, base
, baselen
);
137 if (baselen
&& base
[baselen
-1] != '/')
138 path
[baselen
++] = '/';
140 while ((de
= readdir(dir
)) != NULL
) {
141 unsigned char sha1
[20];
145 if (de
->d_name
[0] == '.')
147 namelen
= strlen(de
->d_name
);
150 if (has_extension(de
->d_name
, ".lock"))
152 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
153 if (stat(git_path("%s", path
), &st
) < 0)
155 if (S_ISDIR(st
.st_mode
)) {
156 retval
= do_for_each_ref(path
, fn
, trim
);
161 if (read_ref(git_path("%s", path
), sha1
) < 0) {
162 error("%s points nowhere!", path
);
165 if (!has_sha1_file(sha1
)) {
166 error("%s does not point to a valid "
167 "commit object!", path
);
170 retval
= fn(path
+ trim
, sha1
);
180 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
182 unsigned char sha1
[20];
183 if (!read_ref(git_path("HEAD"), sha1
))
184 return fn("HEAD", sha1
);
188 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
190 return do_for_each_ref("refs", fn
, 0);
193 int for_each_tag_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
195 return do_for_each_ref("refs/tags", fn
, 10);
198 int for_each_branch_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
200 return do_for_each_ref("refs/heads", fn
, 11);
203 int for_each_remote_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
205 return do_for_each_ref("refs/remotes", fn
, 13);
208 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
210 if (check_ref_format(ref
))
212 return read_ref(git_path("refs/%s", ref
), sha1
);
216 * Make sure "ref" is something reasonable to have under ".git/refs/";
217 * We do not like it if:
219 * - any path component of it begins with ".", or
220 * - it has double dots "..", or
221 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
222 * - it ends with a "/".
225 static inline int bad_ref_char(int ch
)
227 return (((unsigned) ch
) <= ' ' ||
228 ch
== '~' || ch
== '^' || ch
== ':' ||
229 /* 2.13 Pattern Matching Notation */
230 ch
== '?' || ch
== '*' || ch
== '[');
233 int check_ref_format(const char *ref
)
236 const char *cp
= ref
;
240 while ((ch
= *cp
++) == '/')
241 ; /* tolerate duplicated slashes */
243 return -1; /* should not end with slashes */
245 /* we are at the beginning of the path component */
246 if (ch
== '.' || bad_ref_char(ch
))
249 /* scan the rest of the path component */
250 while ((ch
= *cp
++) != 0) {
251 if (bad_ref_char(ch
))
255 if (ch
== '.' && *cp
== '.')
261 return -1; /* at least of form "heads/blah" */
267 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
268 const unsigned char *old_sha1
, int mustexist
)
271 int nr
, fd
= open(lock
->ref_file
, O_RDONLY
);
272 if (fd
< 0 && (mustexist
|| errno
!= ENOENT
)) {
273 error("Can't verify ref %s", lock
->ref_file
);
277 nr
= read(fd
, buf
, 40);
279 if (nr
!= 40 || get_sha1_hex(buf
, lock
->old_sha1
) < 0) {
280 error("Can't verify ref %s", lock
->ref_file
);
284 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
285 error("Ref %s is at %s but expected %s", lock
->ref_file
,
286 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
293 static struct ref_lock
*lock_ref_sha1_basic(const char *path
,
295 const unsigned char *old_sha1
, int mustexist
)
297 const char *orig_path
= path
;
298 struct ref_lock
*lock
;
301 lock
= xcalloc(1, sizeof(struct ref_lock
));
304 plen
= strlen(path
) - plen
;
305 path
= resolve_ref(path
, lock
->old_sha1
, mustexist
);
307 int last_errno
= errno
;
308 error("unable to resolve reference %s: %s",
309 orig_path
, strerror(errno
));
314 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
316 lock
->ref_file
= strdup(path
);
317 lock
->log_file
= strdup(git_path("logs/%s", lock
->ref_file
+ plen
));
318 lock
->force_write
= lstat(lock
->ref_file
, &st
) && errno
== ENOENT
;
320 if (safe_create_leading_directories(lock
->ref_file
))
321 die("unable to create directory for %s", lock
->ref_file
);
322 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, lock
->ref_file
, 1);
324 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
327 struct ref_lock
*lock_ref_sha1(const char *ref
,
328 const unsigned char *old_sha1
, int mustexist
)
330 if (check_ref_format(ref
))
332 return lock_ref_sha1_basic(git_path("refs/%s", ref
),
333 5 + strlen(ref
), old_sha1
, mustexist
);
336 struct ref_lock
*lock_any_ref_for_update(const char *ref
,
337 const unsigned char *old_sha1
, int mustexist
)
339 return lock_ref_sha1_basic(git_path("%s", ref
),
340 strlen(ref
), old_sha1
, mustexist
);
343 void unlock_ref(struct ref_lock
*lock
)
345 if (lock
->lock_fd
>= 0) {
346 close(lock
->lock_fd
);
347 /* Do not free lock->lk -- atexit() still looks at them */
349 rollback_lock_file(lock
->lk
);
352 free(lock
->ref_file
);
354 free(lock
->log_file
);
358 static int log_ref_write(struct ref_lock
*lock
,
359 const unsigned char *sha1
, const char *msg
)
361 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
362 unsigned maxlen
, len
;
364 const char *committer
;
366 if (log_all_ref_updates
) {
367 if (safe_create_leading_directories(lock
->log_file
) < 0)
368 return error("unable to create directory for %s",
373 logfd
= open(lock
->log_file
, oflags
, 0666);
375 if (!log_all_ref_updates
&& errno
== ENOENT
)
377 return error("Unable to append to %s: %s",
378 lock
->log_file
, strerror(errno
));
381 committer
= git_committer_info(1);
383 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
384 logrec
= xmalloc(maxlen
);
385 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
386 sha1_to_hex(lock
->old_sha1
),
392 maxlen
= strlen(committer
) + 2*40 + 4;
393 logrec
= xmalloc(maxlen
);
394 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
395 sha1_to_hex(lock
->old_sha1
),
399 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
403 return error("Unable to append to %s", lock
->log_file
);
407 int write_ref_sha1(struct ref_lock
*lock
,
408 const unsigned char *sha1
, const char *logmsg
)
410 static char term
= '\n';
414 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
418 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
419 write(lock
->lock_fd
, &term
, 1) != 1
420 || close(lock
->lock_fd
) < 0) {
421 error("Couldn't write %s", lock
->lk
->filename
);
425 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
429 if (commit_lock_file(lock
->lk
)) {
430 error("Couldn't set %s", lock
->ref_file
);
439 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
441 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
446 unsigned char logged_sha1
[20];
448 logfile
= git_path("logs/%s", ref
);
449 logfd
= open(logfile
, O_RDONLY
, 0);
451 die("Unable to read log %s: %s", logfile
, strerror(errno
));
454 die("Log %s is empty.", logfile
);
455 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
459 rec
= logend
= logdata
+ st
.st_size
;
460 while (logdata
< rec
) {
461 if (logdata
< rec
&& *(rec
-1) == '\n')
464 while (logdata
< rec
&& *(rec
-1) != '\n') {
470 die("Log %s is corrupt.", logfile
);
471 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
472 if (date
<= at_time
) {
474 if (get_sha1_hex(lastrec
, logged_sha1
))
475 die("Log %s is corrupt.", logfile
);
476 if (get_sha1_hex(rec
+ 41, sha1
))
477 die("Log %s is corrupt.", logfile
);
478 if (hashcmp(logged_sha1
, sha1
)) {
479 tz
= strtoul(tz_c
, NULL
, 10);
481 "warning: Log %s has gap after %s.\n",
482 logfile
, show_rfc2822_date(date
, tz
));
485 else if (date
== at_time
) {
486 if (get_sha1_hex(rec
+ 41, sha1
))
487 die("Log %s is corrupt.", logfile
);
490 if (get_sha1_hex(rec
+ 41, logged_sha1
))
491 die("Log %s is corrupt.", logfile
);
492 if (hashcmp(logged_sha1
, sha1
)) {
493 tz
= strtoul(tz_c
, NULL
, 10);
495 "warning: Log %s unexpectedly ended on %s.\n",
496 logfile
, show_rfc2822_date(date
, tz
));
499 munmap((void*)logdata
, st
.st_size
);
506 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
508 if (rec
== logend
|| *rec
== '\n')
509 die("Log %s is corrupt.", logfile
);
510 date
= strtoul(rec
+ 1, &tz_c
, 10);
511 tz
= strtoul(tz_c
, NULL
, 10);
512 if (get_sha1_hex(logdata
, sha1
))
513 die("Log %s is corrupt.", logfile
);
514 munmap((void*)logdata
, st
.st_size
);
515 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
516 logfile
, show_rfc2822_date(date
, tz
));