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 if (is_null_sha1(entry
->sha1
))
285 if (!has_sha1_file(entry
->sha1
)) {
286 error("%s does not point to a valid object!", entry
->name
);
289 retval
= fn(entry
->name
+ trim
, entry
->sha1
);
294 packed
= packed
? packed
: loose
;
296 if (!strncmp(base
, packed
->name
, trim
)) {
297 retval
= fn(packed
->name
+ trim
, packed
->sha1
);
301 packed
= packed
->next
;
306 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
308 unsigned char sha1
[20];
309 if (!read_ref(git_path("HEAD"), sha1
))
310 return fn("HEAD", sha1
);
314 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
316 return do_for_each_ref("refs/", fn
, 0);
319 int for_each_tag_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
321 return do_for_each_ref("refs/tags/", fn
, 10);
324 int for_each_branch_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
326 return do_for_each_ref("refs/heads/", fn
, 11);
329 int for_each_remote_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
331 return do_for_each_ref("refs/remotes/", fn
, 13);
334 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
336 if (check_ref_format(ref
))
338 return read_ref(git_path("refs/%s", ref
), sha1
);
342 * Make sure "ref" is something reasonable to have under ".git/refs/";
343 * We do not like it if:
345 * - any path component of it begins with ".", or
346 * - it has double dots "..", or
347 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
348 * - it ends with a "/".
351 static inline int bad_ref_char(int ch
)
353 return (((unsigned) ch
) <= ' ' ||
354 ch
== '~' || ch
== '^' || ch
== ':' ||
355 /* 2.13 Pattern Matching Notation */
356 ch
== '?' || ch
== '*' || ch
== '[');
359 int check_ref_format(const char *ref
)
362 const char *cp
= ref
;
366 while ((ch
= *cp
++) == '/')
367 ; /* tolerate duplicated slashes */
369 return -1; /* should not end with slashes */
371 /* we are at the beginning of the path component */
372 if (ch
== '.' || bad_ref_char(ch
))
375 /* scan the rest of the path component */
376 while ((ch
= *cp
++) != 0) {
377 if (bad_ref_char(ch
))
381 if (ch
== '.' && *cp
== '.')
387 return -1; /* at least of form "heads/blah" */
393 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
394 const unsigned char *old_sha1
, int mustexist
)
397 int nr
, fd
= open(lock
->ref_file
, O_RDONLY
);
398 if (fd
< 0 && (mustexist
|| errno
!= ENOENT
)) {
399 error("Can't verify ref %s", lock
->ref_file
);
403 nr
= read(fd
, buf
, 40);
405 if (nr
!= 40 || get_sha1_hex(buf
, lock
->old_sha1
) < 0) {
406 error("Can't verify ref %s", lock
->ref_file
);
410 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
411 error("Ref %s is at %s but expected %s", lock
->ref_file
,
412 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
419 static struct ref_lock
*lock_ref_sha1_basic(const char *path
,
421 const unsigned char *old_sha1
, int mustexist
)
423 const char *orig_path
= path
;
424 struct ref_lock
*lock
;
427 lock
= xcalloc(1, sizeof(struct ref_lock
));
430 plen
= strlen(path
) - plen
;
431 path
= resolve_ref(path
, lock
->old_sha1
, mustexist
);
433 int last_errno
= errno
;
434 error("unable to resolve reference %s: %s",
435 orig_path
, strerror(errno
));
440 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
442 lock
->ref_file
= xstrdup(path
);
443 lock
->log_file
= xstrdup(git_path("logs/%s", lock
->ref_file
+ plen
));
444 lock
->force_write
= lstat(lock
->ref_file
, &st
) && errno
== ENOENT
;
446 if (safe_create_leading_directories(lock
->ref_file
))
447 die("unable to create directory for %s", lock
->ref_file
);
448 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, lock
->ref_file
, 1);
450 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
453 struct ref_lock
*lock_ref_sha1(const char *ref
,
454 const unsigned char *old_sha1
, int mustexist
)
456 if (check_ref_format(ref
))
458 return lock_ref_sha1_basic(git_path("refs/%s", ref
),
459 5 + strlen(ref
), old_sha1
, mustexist
);
462 struct ref_lock
*lock_any_ref_for_update(const char *ref
,
463 const unsigned char *old_sha1
, int mustexist
)
465 return lock_ref_sha1_basic(git_path("%s", ref
),
466 strlen(ref
), old_sha1
, mustexist
);
469 void unlock_ref(struct ref_lock
*lock
)
471 if (lock
->lock_fd
>= 0) {
472 close(lock
->lock_fd
);
473 /* Do not free lock->lk -- atexit() still looks at them */
475 rollback_lock_file(lock
->lk
);
477 free(lock
->ref_file
);
478 free(lock
->log_file
);
482 static int log_ref_write(struct ref_lock
*lock
,
483 const unsigned char *sha1
, const char *msg
)
485 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
486 unsigned maxlen
, len
;
488 const char *committer
;
490 if (log_all_ref_updates
) {
491 if (safe_create_leading_directories(lock
->log_file
) < 0)
492 return error("unable to create directory for %s",
497 logfd
= open(lock
->log_file
, oflags
, 0666);
499 if (!log_all_ref_updates
&& errno
== ENOENT
)
501 return error("Unable to append to %s: %s",
502 lock
->log_file
, strerror(errno
));
505 committer
= git_committer_info(1);
507 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
508 logrec
= xmalloc(maxlen
);
509 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
510 sha1_to_hex(lock
->old_sha1
),
516 maxlen
= strlen(committer
) + 2*40 + 4;
517 logrec
= xmalloc(maxlen
);
518 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
519 sha1_to_hex(lock
->old_sha1
),
523 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
527 return error("Unable to append to %s", lock
->log_file
);
531 int write_ref_sha1(struct ref_lock
*lock
,
532 const unsigned char *sha1
, const char *logmsg
)
534 static char term
= '\n';
538 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
542 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
543 write(lock
->lock_fd
, &term
, 1) != 1
544 || close(lock
->lock_fd
) < 0) {
545 error("Couldn't write %s", lock
->lk
->filename
);
549 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
553 if (commit_lock_file(lock
->lk
)) {
554 error("Couldn't set %s", lock
->ref_file
);
563 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
565 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
570 unsigned char logged_sha1
[20];
572 logfile
= git_path("logs/%s", ref
);
573 logfd
= open(logfile
, O_RDONLY
, 0);
575 die("Unable to read log %s: %s", logfile
, strerror(errno
));
578 die("Log %s is empty.", logfile
);
579 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
583 rec
= logend
= logdata
+ st
.st_size
;
584 while (logdata
< rec
) {
585 if (logdata
< rec
&& *(rec
-1) == '\n')
588 while (logdata
< rec
&& *(rec
-1) != '\n') {
594 die("Log %s is corrupt.", logfile
);
595 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
596 if (date
<= at_time
) {
598 if (get_sha1_hex(lastrec
, logged_sha1
))
599 die("Log %s is corrupt.", logfile
);
600 if (get_sha1_hex(rec
+ 41, sha1
))
601 die("Log %s is corrupt.", logfile
);
602 if (hashcmp(logged_sha1
, sha1
)) {
603 tz
= strtoul(tz_c
, NULL
, 10);
605 "warning: Log %s has gap after %s.\n",
606 logfile
, show_rfc2822_date(date
, tz
));
609 else if (date
== at_time
) {
610 if (get_sha1_hex(rec
+ 41, sha1
))
611 die("Log %s is corrupt.", logfile
);
614 if (get_sha1_hex(rec
+ 41, logged_sha1
))
615 die("Log %s is corrupt.", logfile
);
616 if (hashcmp(logged_sha1
, sha1
)) {
617 tz
= strtoul(tz_c
, NULL
, 10);
619 "warning: Log %s unexpectedly ended on %s.\n",
620 logfile
, show_rfc2822_date(date
, tz
));
623 munmap((void*)logdata
, st
.st_size
);
630 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
632 if (rec
== logend
|| *rec
== '\n')
633 die("Log %s is corrupt.", logfile
);
634 date
= strtoul(rec
+ 1, &tz_c
, 10);
635 tz
= strtoul(tz_c
, NULL
, 10);
636 if (get_sha1_hex(logdata
, sha1
))
637 die("Log %s is corrupt.", logfile
);
638 munmap((void*)logdata
, st
.st_size
);
639 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
640 logfile
, show_rfc2822_date(date
, tz
));