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]))
31 if (line
[len
] != '\n')
37 static struct ref_list
*add_ref(const char *name
, const unsigned char *sha1
, struct ref_list
*list
)
40 struct ref_list
**p
= &list
, *entry
;
42 /* Find the place to insert the ref into.. */
43 while ((entry
= *p
) != NULL
) {
44 int cmp
= strcmp(entry
->name
, name
);
48 /* Same as existing entry? */
54 /* Allocate it and add it in.. */
55 len
= strlen(name
) + 1;
56 entry
= xmalloc(sizeof(struct ref_list
) + len
);
57 hashcpy(entry
->sha1
, sha1
);
58 memcpy(entry
->name
, name
, len
);
64 static struct ref_list
*get_packed_refs(void)
66 static int did_refs
= 0;
67 static struct ref_list
*refs
= NULL
;
70 FILE *f
= fopen(git_path("packed-refs"), "r");
72 struct ref_list
*list
= NULL
;
73 char refline
[PATH_MAX
];
74 while (fgets(refline
, sizeof(refline
), f
)) {
75 unsigned char sha1
[20];
76 const char *name
= parse_ref_line(refline
, sha1
);
79 list
= add_ref(name
, sha1
, list
);
89 static struct ref_list
*get_ref_dir(const char *base
, struct ref_list
*list
)
91 DIR *dir
= opendir(git_path("%s", base
));
95 int baselen
= strlen(base
);
96 char *path
= xmalloc(baselen
+ 257);
98 memcpy(path
, base
, baselen
);
99 if (baselen
&& base
[baselen
-1] != '/')
100 path
[baselen
++] = '/';
102 while ((de
= readdir(dir
)) != NULL
) {
103 unsigned char sha1
[20];
107 if (de
->d_name
[0] == '.')
109 namelen
= strlen(de
->d_name
);
112 if (has_extension(de
->d_name
, ".lock"))
114 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
115 if (stat(git_path("%s", path
), &st
) < 0)
117 if (S_ISDIR(st
.st_mode
)) {
118 list
= get_ref_dir(path
, list
);
121 if (read_ref(git_path("%s", path
), sha1
) < 0) {
122 error("%s points nowhere!", path
);
125 list
= add_ref(path
, sha1
, list
);
133 static struct ref_list
*get_loose_refs(void)
135 static int did_refs
= 0;
136 static struct ref_list
*refs
= NULL
;
139 refs
= get_ref_dir("refs", NULL
);
145 /* We allow "recursive" symbolic refs. Only within reason, though */
148 const char *resolve_ref(const char *path
, unsigned char *sha1
, int reading
)
150 int depth
= MAXDEPTH
, len
;
161 /* Special case: non-existing file.
162 * Not having the refs/heads/new-branch is OK
163 * if we are writing into it, so is .git/HEAD
164 * that points at refs/heads/master still to be
165 * born. It is NOT OK if we are resolving for
168 if (lstat(path
, &st
) < 0) {
169 if (reading
|| errno
!= ENOENT
)
175 /* Follow "normalized" - ie "refs/.." symlinks by hand */
176 if (S_ISLNK(st
.st_mode
)) {
177 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
178 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
179 path
= git_path("%.*s", len
, buffer
);
185 * Anything else, just open it and try to use it as
188 fd
= open(path
, O_RDONLY
);
191 len
= read(fd
, buffer
, sizeof(buffer
)-1);
195 * Is it a symbolic ref?
197 if (len
< 4 || memcmp("ref:", buffer
, 4))
201 while (len
&& isspace(*buf
))
203 while (len
&& isspace(buf
[len
-1]))
205 path
= git_path("%.*s", len
, buf
);
207 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
212 int create_symref(const char *git_HEAD
, const char *refs_heads_master
)
214 const char *lockpath
;
216 int fd
, len
, written
;
218 #ifndef NO_SYMLINK_HEAD
219 if (prefer_symlink_refs
) {
221 if (!symlink(refs_heads_master
, git_HEAD
))
223 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
227 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
228 if (sizeof(ref
) <= len
) {
229 error("refname too long: %s", refs_heads_master
);
232 lockpath
= mkpath("%s.lock", git_HEAD
);
233 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
234 written
= write(fd
, ref
, len
);
236 if (written
!= len
) {
238 error("Unable to write to %s", lockpath
);
241 if (rename(lockpath
, git_HEAD
) < 0) {
243 error("Unable to create %s", git_HEAD
);
246 if (adjust_shared_perm(git_HEAD
)) {
248 error("Unable to fix permissions on %s", lockpath
);
254 int read_ref(const char *filename
, unsigned char *sha1
)
256 if (resolve_ref(filename
, sha1
, 1))
261 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
), int trim
)
264 struct ref_list
*packed
= get_packed_refs();
265 struct ref_list
*loose
= get_loose_refs();
267 while (packed
&& loose
) {
268 struct ref_list
*entry
;
269 int cmp
= strcmp(packed
->name
, loose
->name
);
271 packed
= packed
->next
;
279 packed
= packed
->next
;
281 if (strncmp(base
, entry
->name
, trim
))
283 retval
= fn(entry
->name
+ trim
, entry
->sha1
);
288 packed
= packed
? packed
: loose
;
290 if (!strncmp(base
, packed
->name
, trim
)) {
291 retval
= fn(packed
->name
+ trim
, packed
->sha1
);
295 packed
= packed
->next
;
300 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
302 unsigned char sha1
[20];
303 if (!read_ref(git_path("HEAD"), sha1
))
304 return fn("HEAD", sha1
);
308 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
310 return do_for_each_ref("refs/", fn
, 0);
313 int for_each_tag_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
315 return do_for_each_ref("refs/tags/", fn
, 10);
318 int for_each_branch_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
320 return do_for_each_ref("refs/heads/", fn
, 11);
323 int for_each_remote_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
325 return do_for_each_ref("refs/remotes/", fn
, 13);
328 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
330 if (check_ref_format(ref
))
332 return read_ref(git_path("refs/%s", ref
), sha1
);
336 * Make sure "ref" is something reasonable to have under ".git/refs/";
337 * We do not like it if:
339 * - any path component of it begins with ".", or
340 * - it has double dots "..", or
341 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
342 * - it ends with a "/".
345 static inline int bad_ref_char(int ch
)
347 return (((unsigned) ch
) <= ' ' ||
348 ch
== '~' || ch
== '^' || ch
== ':' ||
349 /* 2.13 Pattern Matching Notation */
350 ch
== '?' || ch
== '*' || ch
== '[');
353 int check_ref_format(const char *ref
)
356 const char *cp
= ref
;
360 while ((ch
= *cp
++) == '/')
361 ; /* tolerate duplicated slashes */
363 return -1; /* should not end with slashes */
365 /* we are at the beginning of the path component */
366 if (ch
== '.' || bad_ref_char(ch
))
369 /* scan the rest of the path component */
370 while ((ch
= *cp
++) != 0) {
371 if (bad_ref_char(ch
))
375 if (ch
== '.' && *cp
== '.')
381 return -1; /* at least of form "heads/blah" */
387 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
388 const unsigned char *old_sha1
, int mustexist
)
391 int nr
, fd
= open(lock
->ref_file
, O_RDONLY
);
392 if (fd
< 0 && (mustexist
|| errno
!= ENOENT
)) {
393 error("Can't verify ref %s", lock
->ref_file
);
397 nr
= read(fd
, buf
, 40);
399 if (nr
!= 40 || get_sha1_hex(buf
, lock
->old_sha1
) < 0) {
400 error("Can't verify ref %s", lock
->ref_file
);
404 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
405 error("Ref %s is at %s but expected %s", lock
->ref_file
,
406 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
413 static struct ref_lock
*lock_ref_sha1_basic(const char *path
,
415 const unsigned char *old_sha1
, int mustexist
)
417 const char *orig_path
= path
;
418 struct ref_lock
*lock
;
421 lock
= xcalloc(1, sizeof(struct ref_lock
));
424 plen
= strlen(path
) - plen
;
425 path
= resolve_ref(path
, lock
->old_sha1
, mustexist
);
427 int last_errno
= errno
;
428 error("unable to resolve reference %s: %s",
429 orig_path
, strerror(errno
));
434 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
436 lock
->ref_file
= xstrdup(path
);
437 lock
->log_file
= xstrdup(git_path("logs/%s", lock
->ref_file
+ plen
));
438 lock
->force_write
= lstat(lock
->ref_file
, &st
) && errno
== ENOENT
;
440 if (safe_create_leading_directories(lock
->ref_file
))
441 die("unable to create directory for %s", lock
->ref_file
);
442 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, lock
->ref_file
, 1);
444 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
447 struct ref_lock
*lock_ref_sha1(const char *ref
,
448 const unsigned char *old_sha1
, int mustexist
)
450 if (check_ref_format(ref
))
452 return lock_ref_sha1_basic(git_path("refs/%s", ref
),
453 5 + strlen(ref
), old_sha1
, mustexist
);
456 struct ref_lock
*lock_any_ref_for_update(const char *ref
,
457 const unsigned char *old_sha1
, int mustexist
)
459 return lock_ref_sha1_basic(git_path("%s", ref
),
460 strlen(ref
), old_sha1
, mustexist
);
463 void unlock_ref(struct ref_lock
*lock
)
465 if (lock
->lock_fd
>= 0) {
466 close(lock
->lock_fd
);
467 /* Do not free lock->lk -- atexit() still looks at them */
469 rollback_lock_file(lock
->lk
);
471 free(lock
->ref_file
);
472 free(lock
->log_file
);
476 static int log_ref_write(struct ref_lock
*lock
,
477 const unsigned char *sha1
, const char *msg
)
479 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
480 unsigned maxlen
, len
;
482 const char *committer
;
484 if (log_all_ref_updates
) {
485 if (safe_create_leading_directories(lock
->log_file
) < 0)
486 return error("unable to create directory for %s",
491 logfd
= open(lock
->log_file
, oflags
, 0666);
493 if (!log_all_ref_updates
&& errno
== ENOENT
)
495 return error("Unable to append to %s: %s",
496 lock
->log_file
, strerror(errno
));
499 committer
= git_committer_info(1);
501 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
502 logrec
= xmalloc(maxlen
);
503 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
504 sha1_to_hex(lock
->old_sha1
),
510 maxlen
= strlen(committer
) + 2*40 + 4;
511 logrec
= xmalloc(maxlen
);
512 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
513 sha1_to_hex(lock
->old_sha1
),
517 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
521 return error("Unable to append to %s", lock
->log_file
);
525 int write_ref_sha1(struct ref_lock
*lock
,
526 const unsigned char *sha1
, const char *logmsg
)
528 static char term
= '\n';
532 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
536 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
537 write(lock
->lock_fd
, &term
, 1) != 1
538 || close(lock
->lock_fd
) < 0) {
539 error("Couldn't write %s", lock
->lk
->filename
);
543 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
547 if (commit_lock_file(lock
->lk
)) {
548 error("Couldn't set %s", lock
->ref_file
);
557 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
559 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
564 unsigned char logged_sha1
[20];
566 logfile
= git_path("logs/%s", ref
);
567 logfd
= open(logfile
, O_RDONLY
, 0);
569 die("Unable to read log %s: %s", logfile
, strerror(errno
));
572 die("Log %s is empty.", logfile
);
573 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
577 rec
= logend
= logdata
+ st
.st_size
;
578 while (logdata
< rec
) {
579 if (logdata
< rec
&& *(rec
-1) == '\n')
582 while (logdata
< rec
&& *(rec
-1) != '\n') {
588 die("Log %s is corrupt.", logfile
);
589 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
590 if (date
<= at_time
) {
592 if (get_sha1_hex(lastrec
, logged_sha1
))
593 die("Log %s is corrupt.", logfile
);
594 if (get_sha1_hex(rec
+ 41, sha1
))
595 die("Log %s is corrupt.", logfile
);
596 if (hashcmp(logged_sha1
, sha1
)) {
597 tz
= strtoul(tz_c
, NULL
, 10);
599 "warning: Log %s has gap after %s.\n",
600 logfile
, show_rfc2822_date(date
, tz
));
603 else if (date
== at_time
) {
604 if (get_sha1_hex(rec
+ 41, sha1
))
605 die("Log %s is corrupt.", logfile
);
608 if (get_sha1_hex(rec
+ 41, logged_sha1
))
609 die("Log %s is corrupt.", logfile
);
610 if (hashcmp(logged_sha1
, sha1
)) {
611 tz
= strtoul(tz_c
, NULL
, 10);
613 "warning: Log %s unexpectedly ended on %s.\n",
614 logfile
, show_rfc2822_date(date
, tz
));
617 munmap((void*)logdata
, st
.st_size
);
624 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
626 if (rec
== logend
|| *rec
== '\n')
627 die("Log %s is corrupt.", logfile
);
628 date
= strtoul(rec
+ 1, &tz_c
, 10);
629 tz
= strtoul(tz_c
, NULL
, 10);
630 if (get_sha1_hex(logdata
, sha1
))
631 die("Log %s is corrupt.", logfile
);
632 munmap((void*)logdata
, st
.st_size
);
633 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
634 logfile
, show_rfc2822_date(date
, tz
));