8 unsigned char sha1
[20];
12 static const char *parse_ref_line(char *line
, unsigned char *sha1
)
15 * 42: the answer to everything.
17 * In this case, it happens to be the answer to
18 * 40 (length of sha1 hex representation)
19 * +1 (space in between hex and name)
20 * +1 (newline at the end of the line)
22 int len
= strlen(line
) - 42;
26 if (get_sha1_hex(line
, sha1
) < 0)
28 if (!isspace(line
[40]))
33 if (line
[len
] != '\n')
39 static struct ref_list
*add_ref(const char *name
, const unsigned char *sha1
, struct ref_list
*list
)
42 struct ref_list
**p
= &list
, *entry
;
44 /* Find the place to insert the ref into.. */
45 while ((entry
= *p
) != NULL
) {
46 int cmp
= strcmp(entry
->name
, name
);
50 /* Same as existing entry? */
56 /* Allocate it and add it in.. */
57 len
= strlen(name
) + 1;
58 entry
= xmalloc(sizeof(struct ref_list
) + len
);
59 hashcpy(entry
->sha1
, sha1
);
60 memcpy(entry
->name
, name
, len
);
66 static struct ref_list
*get_packed_refs(void)
68 static int did_refs
= 0;
69 static struct ref_list
*refs
= NULL
;
72 FILE *f
= fopen(git_path("packed-refs"), "r");
74 struct ref_list
*list
= NULL
;
75 char refline
[PATH_MAX
];
76 while (fgets(refline
, sizeof(refline
), f
)) {
77 unsigned char sha1
[20];
78 const char *name
= parse_ref_line(refline
, sha1
);
81 list
= add_ref(name
, sha1
, list
);
91 static struct ref_list
*get_ref_dir(const char *base
, struct ref_list
*list
)
93 DIR *dir
= opendir(git_path("%s", base
));
97 int baselen
= strlen(base
);
98 char *ref
= xmalloc(baselen
+ 257);
100 memcpy(ref
, base
, baselen
);
101 if (baselen
&& base
[baselen
-1] != '/')
102 ref
[baselen
++] = '/';
104 while ((de
= readdir(dir
)) != NULL
) {
105 unsigned char sha1
[20];
109 if (de
->d_name
[0] == '.')
111 namelen
= strlen(de
->d_name
);
114 if (has_extension(de
->d_name
, ".lock"))
116 memcpy(ref
+ baselen
, de
->d_name
, namelen
+1);
117 if (stat(git_path("%s", ref
), &st
) < 0)
119 if (S_ISDIR(st
.st_mode
)) {
120 list
= get_ref_dir(ref
, list
);
123 if (read_ref(ref
, sha1
) < 0) {
124 error("%s points nowhere!", ref
);
127 list
= add_ref(ref
, sha1
, list
);
135 static struct ref_list
*get_loose_refs(void)
137 static int did_refs
= 0;
138 static struct ref_list
*refs
= NULL
;
141 refs
= get_ref_dir("refs", NULL
);
147 /* We allow "recursive" symbolic refs. Only within reason, though */
150 const char *resolve_ref(const char *ref
, unsigned char *sha1
, int reading
)
152 int depth
= MAXDEPTH
, len
;
154 static char ref_buffer
[256];
157 const char *path
= git_path("%s", ref
);
165 /* Special case: non-existing file.
166 * Not having the refs/heads/new-branch is OK
167 * if we are writing into it, so is .git/HEAD
168 * that points at refs/heads/master still to be
169 * born. It is NOT OK if we are resolving for
172 if (lstat(path
, &st
) < 0) {
173 struct ref_list
*list
= get_packed_refs();
175 if (!strcmp(ref
, list
->name
)) {
176 hashcpy(sha1
, list
->sha1
);
181 if (reading
|| errno
!= ENOENT
)
187 /* Follow "normalized" - ie "refs/.." symlinks by hand */
188 if (S_ISLNK(st
.st_mode
)) {
189 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
190 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
192 strcpy(ref_buffer
, buffer
);
199 * Anything else, just open it and try to use it as
202 fd
= open(path
, O_RDONLY
);
205 len
= read(fd
, buffer
, sizeof(buffer
)-1);
209 * Is it a symbolic ref?
211 if (len
< 4 || memcmp("ref:", buffer
, 4))
215 while (len
&& isspace(*buf
))
217 while (len
&& isspace(buf
[len
-1]))
220 memcpy(ref_buffer
, buf
, len
+ 1);
223 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
228 int create_symref(const char *ref_target
, const char *refs_heads_master
)
230 const char *lockpath
;
232 int fd
, len
, written
;
233 const char *git_HEAD
= git_path("%s", ref_target
);
235 #ifndef NO_SYMLINK_HEAD
236 if (prefer_symlink_refs
) {
238 if (!symlink(refs_heads_master
, git_HEAD
))
240 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
244 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
245 if (sizeof(ref
) <= len
) {
246 error("refname too long: %s", refs_heads_master
);
249 lockpath
= mkpath("%s.lock", git_HEAD
);
250 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
251 written
= write(fd
, ref
, len
);
253 if (written
!= len
) {
255 error("Unable to write to %s", lockpath
);
258 if (rename(lockpath
, git_HEAD
) < 0) {
260 error("Unable to create %s", git_HEAD
);
263 if (adjust_shared_perm(git_HEAD
)) {
265 error("Unable to fix permissions on %s", lockpath
);
271 int read_ref(const char *ref
, unsigned char *sha1
)
273 if (resolve_ref(ref
, sha1
, 1))
278 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
), int trim
)
281 struct ref_list
*packed
= get_packed_refs();
282 struct ref_list
*loose
= get_loose_refs();
284 while (packed
&& loose
) {
285 struct ref_list
*entry
;
286 int cmp
= strcmp(packed
->name
, loose
->name
);
288 packed
= packed
->next
;
296 packed
= packed
->next
;
298 if (strncmp(base
, entry
->name
, trim
))
300 if (is_null_sha1(entry
->sha1
))
302 if (!has_sha1_file(entry
->sha1
)) {
303 error("%s does not point to a valid object!", entry
->name
);
306 retval
= fn(entry
->name
+ trim
, entry
->sha1
);
311 packed
= packed
? packed
: loose
;
313 if (!strncmp(base
, packed
->name
, trim
)) {
314 retval
= fn(packed
->name
+ trim
, packed
->sha1
);
318 packed
= packed
->next
;
323 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
325 unsigned char sha1
[20];
326 if (!read_ref("HEAD", sha1
))
327 return fn("HEAD", sha1
);
331 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
333 return do_for_each_ref("refs/", fn
, 0);
336 int for_each_tag_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
338 return do_for_each_ref("refs/tags/", fn
, 10);
341 int for_each_branch_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
343 return do_for_each_ref("refs/heads/", fn
, 11);
346 int for_each_remote_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
348 return do_for_each_ref("refs/remotes/", fn
, 13);
351 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
353 if (check_ref_format(ref
))
355 return read_ref(mkpath("refs/%s", ref
), sha1
);
359 * Make sure "ref" is something reasonable to have under ".git/refs/";
360 * We do not like it if:
362 * - any path component of it begins with ".", or
363 * - it has double dots "..", or
364 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
365 * - it ends with a "/".
368 static inline int bad_ref_char(int ch
)
370 return (((unsigned) ch
) <= ' ' ||
371 ch
== '~' || ch
== '^' || ch
== ':' ||
372 /* 2.13 Pattern Matching Notation */
373 ch
== '?' || ch
== '*' || ch
== '[');
376 int check_ref_format(const char *ref
)
379 const char *cp
= ref
;
383 while ((ch
= *cp
++) == '/')
384 ; /* tolerate duplicated slashes */
386 return -1; /* should not end with slashes */
388 /* we are at the beginning of the path component */
389 if (ch
== '.' || bad_ref_char(ch
))
392 /* scan the rest of the path component */
393 while ((ch
= *cp
++) != 0) {
394 if (bad_ref_char(ch
))
398 if (ch
== '.' && *cp
== '.')
404 return -1; /* at least of form "heads/blah" */
410 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
411 const unsigned char *old_sha1
, int mustexist
)
413 if (!resolve_ref(lock
->ref_name
, lock
->old_sha1
, mustexist
)) {
414 error("Can't verify ref %s", lock
->ref_name
);
418 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
419 error("Ref %s is at %s but expected %s", lock
->ref_name
,
420 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
427 static struct ref_lock
*lock_ref_sha1_basic(const char *ref
,
429 const unsigned char *old_sha1
, int mustexist
)
432 const char *orig_ref
= ref
;
433 struct ref_lock
*lock
;
436 lock
= xcalloc(1, sizeof(struct ref_lock
));
439 ref
= resolve_ref(ref
, lock
->old_sha1
, mustexist
);
441 int last_errno
= errno
;
442 error("unable to resolve reference %s: %s",
443 orig_ref
, strerror(errno
));
448 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
450 lock
->ref_name
= xstrdup(ref
);
451 lock
->log_file
= xstrdup(git_path("logs/%s", ref
));
452 ref_file
= git_path(ref
);
453 lock
->force_write
= lstat(ref_file
, &st
) && errno
== ENOENT
;
455 if (safe_create_leading_directories(ref_file
))
456 die("unable to create directory for %s", ref_file
);
457 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, ref_file
, 1);
459 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
462 struct ref_lock
*lock_ref_sha1(const char *ref
,
463 const unsigned char *old_sha1
, int mustexist
)
465 char refpath
[PATH_MAX
];
466 if (check_ref_format(ref
))
468 strcpy(refpath
, mkpath("refs/%s", ref
));
469 return lock_ref_sha1_basic(refpath
, strlen(refpath
),
470 old_sha1
, mustexist
);
473 struct ref_lock
*lock_any_ref_for_update(const char *ref
,
474 const unsigned char *old_sha1
, int mustexist
)
476 return lock_ref_sha1_basic(ref
, strlen(ref
), old_sha1
, mustexist
);
479 void unlock_ref(struct ref_lock
*lock
)
481 if (lock
->lock_fd
>= 0) {
482 close(lock
->lock_fd
);
483 /* Do not free lock->lk -- atexit() still looks at them */
485 rollback_lock_file(lock
->lk
);
487 free(lock
->ref_name
);
488 free(lock
->log_file
);
492 static int log_ref_write(struct ref_lock
*lock
,
493 const unsigned char *sha1
, const char *msg
)
495 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
496 unsigned maxlen
, len
;
498 const char *committer
;
500 if (log_all_ref_updates
) {
501 if (safe_create_leading_directories(lock
->log_file
) < 0)
502 return error("unable to create directory for %s",
507 logfd
= open(lock
->log_file
, oflags
, 0666);
509 if (!log_all_ref_updates
&& errno
== ENOENT
)
511 return error("Unable to append to %s: %s",
512 lock
->log_file
, strerror(errno
));
515 committer
= git_committer_info(1);
517 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
518 logrec
= xmalloc(maxlen
);
519 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
520 sha1_to_hex(lock
->old_sha1
),
526 maxlen
= strlen(committer
) + 2*40 + 4;
527 logrec
= xmalloc(maxlen
);
528 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
529 sha1_to_hex(lock
->old_sha1
),
533 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
537 return error("Unable to append to %s", lock
->log_file
);
541 int write_ref_sha1(struct ref_lock
*lock
,
542 const unsigned char *sha1
, const char *logmsg
)
544 static char term
= '\n';
548 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
552 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
553 write(lock
->lock_fd
, &term
, 1) != 1
554 || close(lock
->lock_fd
) < 0) {
555 error("Couldn't write %s", lock
->lk
->filename
);
559 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
563 if (commit_lock_file(lock
->lk
)) {
564 error("Couldn't set %s", lock
->ref_name
);
573 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
575 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
580 unsigned char logged_sha1
[20];
582 logfile
= git_path("logs/%s", ref
);
583 logfd
= open(logfile
, O_RDONLY
, 0);
585 die("Unable to read log %s: %s", logfile
, strerror(errno
));
588 die("Log %s is empty.", logfile
);
589 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
593 rec
= logend
= logdata
+ st
.st_size
;
594 while (logdata
< rec
) {
595 if (logdata
< rec
&& *(rec
-1) == '\n')
598 while (logdata
< rec
&& *(rec
-1) != '\n') {
604 die("Log %s is corrupt.", logfile
);
605 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
606 if (date
<= at_time
) {
608 if (get_sha1_hex(lastrec
, logged_sha1
))
609 die("Log %s is corrupt.", logfile
);
610 if (get_sha1_hex(rec
+ 41, sha1
))
611 die("Log %s is corrupt.", logfile
);
612 if (hashcmp(logged_sha1
, sha1
)) {
613 tz
= strtoul(tz_c
, NULL
, 10);
615 "warning: Log %s has gap after %s.\n",
616 logfile
, show_rfc2822_date(date
, tz
));
619 else if (date
== at_time
) {
620 if (get_sha1_hex(rec
+ 41, sha1
))
621 die("Log %s is corrupt.", logfile
);
624 if (get_sha1_hex(rec
+ 41, logged_sha1
))
625 die("Log %s is corrupt.", logfile
);
626 if (hashcmp(logged_sha1
, sha1
)) {
627 tz
= strtoul(tz_c
, NULL
, 10);
629 "warning: Log %s unexpectedly ended on %s.\n",
630 logfile
, show_rfc2822_date(date
, tz
));
633 munmap((void*)logdata
, st
.st_size
);
640 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
642 if (rec
== logend
|| *rec
== '\n')
643 die("Log %s is corrupt.", logfile
);
644 date
= strtoul(rec
+ 1, &tz_c
, 10);
645 tz
= strtoul(tz_c
, NULL
, 10);
646 if (get_sha1_hex(logdata
, sha1
))
647 die("Log %s is corrupt.", logfile
);
648 munmap((void*)logdata
, st
.st_size
);
649 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
650 logfile
, show_rfc2822_date(date
, tz
));