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
);
45 /* Is it a directory? */
46 if (S_ISDIR(st
.st_mode
)) {
52 * Anything else, just open it and try to use it as
55 fd
= open(path
, O_RDONLY
);
58 len
= read(fd
, buffer
, sizeof(buffer
)-1);
62 * Is it a symbolic ref?
64 if (len
< 4 || memcmp("ref:", buffer
, 4))
68 while (len
&& isspace(*buf
))
70 while (len
&& isspace(buf
[len
-1]))
72 path
= git_path("%.*s", len
, buf
);
74 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
79 int create_symref(const char *git_HEAD
, const char *refs_heads_master
)
85 #ifndef NO_SYMLINK_HEAD
86 if (prefer_symlink_refs
) {
88 if (!symlink(refs_heads_master
, git_HEAD
))
90 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
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
);
99 lockpath
= mkpath("%s.lock", git_HEAD
);
100 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
101 written
= write(fd
, ref
, len
);
103 if (written
!= len
) {
105 error("Unable to write to %s", lockpath
);
108 if (rename(lockpath
, git_HEAD
) < 0) {
110 error("Unable to create %s", git_HEAD
);
113 if (adjust_shared_perm(git_HEAD
)) {
115 error("Unable to fix permissions on %s", lockpath
);
121 int read_ref(const char *filename
, unsigned char *sha1
)
123 if (resolve_ref(filename
, sha1
, 1))
128 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
), int trim
)
131 DIR *dir
= opendir(git_path("%s", base
));
135 int baselen
= strlen(base
);
136 char *path
= xmalloc(baselen
+ 257);
138 if (!strncmp(base
, "./", 2)) {
142 memcpy(path
, base
, baselen
);
143 if (baselen
&& base
[baselen
-1] != '/')
144 path
[baselen
++] = '/';
146 while ((de
= readdir(dir
)) != NULL
) {
147 unsigned char sha1
[20];
151 if (de
->d_name
[0] == '.')
153 namelen
= strlen(de
->d_name
);
156 if (has_extension(de
->d_name
, ".lock"))
158 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
159 if (stat(git_path("%s", path
), &st
) < 0)
161 if (S_ISDIR(st
.st_mode
)) {
162 retval
= do_for_each_ref(path
, fn
, trim
);
167 if (read_ref(git_path("%s", path
), sha1
) < 0) {
168 error("%s points nowhere!", path
);
171 if (!has_sha1_file(sha1
)) {
172 error("%s does not point to a valid "
173 "commit object!", path
);
176 retval
= fn(path
+ trim
, sha1
);
186 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
188 unsigned char sha1
[20];
189 if (!read_ref(git_path("HEAD"), sha1
))
190 return fn("HEAD", sha1
);
194 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
196 return do_for_each_ref("refs", fn
, 0);
199 int for_each_tag_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
201 return do_for_each_ref("refs/tags", fn
, 10);
204 int for_each_branch_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
206 return do_for_each_ref("refs/heads", fn
, 11);
209 int for_each_remote_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
211 return do_for_each_ref("refs/remotes", fn
, 13);
214 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
216 if (check_ref_format(ref
))
218 return read_ref(git_path("refs/%s", ref
), sha1
);
222 * Make sure "ref" is something reasonable to have under ".git/refs/";
223 * We do not like it if:
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 "/".
231 static inline int bad_ref_char(int ch
)
233 return (((unsigned) ch
) <= ' ' ||
234 ch
== '~' || ch
== '^' || ch
== ':' ||
235 /* 2.13 Pattern Matching Notation */
236 ch
== '?' || ch
== '*' || ch
== '[');
239 int check_ref_format(const char *ref
)
242 const char *cp
= ref
;
246 while ((ch
= *cp
++) == '/')
247 ; /* tolerate duplicated slashes */
249 return -1; /* should not end with slashes */
251 /* we are at the beginning of the path component */
252 if (ch
== '.' || bad_ref_char(ch
))
255 /* scan the rest of the path component */
256 while ((ch
= *cp
++) != 0) {
257 if (bad_ref_char(ch
))
261 if (ch
== '.' && *cp
== '.')
267 return -1; /* at least of form "heads/blah" */
273 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
274 const unsigned char *old_sha1
, int mustexist
)
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
);
283 nr
= read(fd
, buf
, 40);
285 if (nr
!= 40 || get_sha1_hex(buf
, lock
->old_sha1
) < 0) {
286 error("Can't verify ref %s", lock
->ref_file
);
290 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
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
));
299 static struct ref_lock
*lock_ref_sha1_basic(const char *path
,
301 const unsigned char *old_sha1
, int mustexist
)
303 const char *orig_path
= path
;
304 struct ref_lock
*lock
;
307 lock
= xcalloc(1, sizeof(struct ref_lock
));
310 plen
= strlen(path
) - plen
;
311 path
= resolve_ref(path
, lock
->old_sha1
, mustexist
);
313 int last_errno
= errno
;
314 error("unable to resolve reference %s: %s",
315 orig_path
, strerror(errno
));
320 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
322 lock
->ref_file
= xstrdup(path
);
323 lock
->log_file
= xstrdup(git_path("logs/%s", lock
->ref_file
+ plen
));
324 lock
->force_write
= lstat(lock
->ref_file
, &st
) && errno
== ENOENT
;
326 if (safe_create_leading_directories(lock
->ref_file
))
327 die("unable to create directory for %s", lock
->ref_file
);
328 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, lock
->ref_file
, 1);
330 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
333 struct ref_lock
*lock_ref_sha1(const char *ref
,
334 const unsigned char *old_sha1
, int mustexist
)
336 if (check_ref_format(ref
))
338 return lock_ref_sha1_basic(git_path("refs/%s", ref
),
339 5 + strlen(ref
), old_sha1
, mustexist
);
342 struct ref_lock
*lock_any_ref_for_update(const char *ref
,
343 const unsigned char *old_sha1
, int mustexist
)
345 return lock_ref_sha1_basic(git_path("%s", ref
),
346 strlen(ref
), old_sha1
, mustexist
);
349 void unlock_ref(struct ref_lock
*lock
)
351 if (lock
->lock_fd
>= 0) {
352 close(lock
->lock_fd
);
353 /* Do not free lock->lk -- atexit() still looks at them */
355 rollback_lock_file(lock
->lk
);
357 free(lock
->ref_file
);
358 free(lock
->log_file
);
362 static int log_ref_write(struct ref_lock
*lock
,
363 const unsigned char *sha1
, const char *msg
)
365 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
366 unsigned maxlen
, len
;
368 const char *committer
;
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",
377 logfd
= open(lock
->log_file
, oflags
, 0666);
379 if (!log_all_ref_updates
&& errno
== ENOENT
)
381 return error("Unable to append to %s: %s",
382 lock
->log_file
, strerror(errno
));
385 committer
= git_committer_info(1);
387 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
388 logrec
= xmalloc(maxlen
);
389 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
390 sha1_to_hex(lock
->old_sha1
),
396 maxlen
= strlen(committer
) + 2*40 + 4;
397 logrec
= xmalloc(maxlen
);
398 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
399 sha1_to_hex(lock
->old_sha1
),
403 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
407 return error("Unable to append to %s", lock
->log_file
);
411 int write_ref_sha1(struct ref_lock
*lock
,
412 const unsigned char *sha1
, const char *logmsg
)
414 static char term
= '\n';
418 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
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) {
425 error("Couldn't write %s", lock
->lk
->filename
);
429 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
433 if (commit_lock_file(lock
->lk
)) {
434 error("Couldn't set %s", lock
->ref_file
);
443 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
445 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
450 unsigned char logged_sha1
[20];
452 logfile
= git_path("logs/%s", ref
);
453 logfd
= open(logfile
, O_RDONLY
, 0);
455 die("Unable to read log %s: %s", logfile
, strerror(errno
));
458 die("Log %s is empty.", logfile
);
459 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
463 rec
= logend
= logdata
+ st
.st_size
;
464 while (logdata
< rec
) {
465 if (logdata
< rec
&& *(rec
-1) == '\n')
468 while (logdata
< rec
&& *(rec
-1) != '\n') {
474 die("Log %s is corrupt.", logfile
);
475 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
476 if (date
<= at_time
) {
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
);
482 if (hashcmp(logged_sha1
, sha1
)) {
483 tz
= strtoul(tz_c
, NULL
, 10);
485 "warning: Log %s has gap after %s.\n",
486 logfile
, show_rfc2822_date(date
, tz
));
489 else if (date
== at_time
) {
490 if (get_sha1_hex(rec
+ 41, sha1
))
491 die("Log %s is corrupt.", logfile
);
494 if (get_sha1_hex(rec
+ 41, logged_sha1
))
495 die("Log %s is corrupt.", logfile
);
496 if (hashcmp(logged_sha1
, sha1
)) {
497 tz
= strtoul(tz_c
, NULL
, 10);
499 "warning: Log %s unexpectedly ended on %s.\n",
500 logfile
, show_rfc2822_date(date
, tz
));
503 munmap((void*)logdata
, st
.st_size
);
510 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
512 if (rec
== logend
|| *rec
== '\n')
513 die("Log %s is corrupt.", logfile
);
514 date
= strtoul(rec
+ 1, &tz_c
, 10);
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
));