8 unsigned char flag
; /* ISSYMREF? ISPACKED? */
9 unsigned char sha1
[20];
10 char name
[FLEX_ARRAY
];
13 static const char *parse_ref_line(char *line
, unsigned char *sha1
)
16 * 42: the answer to everything.
18 * In this case, it happens to be the answer to
19 * 40 (length of sha1 hex representation)
20 * +1 (space in between hex and name)
21 * +1 (newline at the end of the line)
23 int len
= strlen(line
) - 42;
27 if (get_sha1_hex(line
, sha1
) < 0)
29 if (!isspace(line
[40]))
34 if (line
[len
] != '\n')
40 static struct ref_list
*add_ref(const char *name
, const unsigned char *sha1
,
41 int flag
, struct ref_list
*list
)
44 struct ref_list
**p
= &list
, *entry
;
46 /* Find the place to insert the ref into.. */
47 while ((entry
= *p
) != NULL
) {
48 int cmp
= strcmp(entry
->name
, name
);
52 /* Same as existing entry? */
58 /* Allocate it and add it in.. */
59 len
= strlen(name
) + 1;
60 entry
= xmalloc(sizeof(struct ref_list
) + len
);
61 hashcpy(entry
->sha1
, sha1
);
62 memcpy(entry
->name
, name
, len
);
69 static struct ref_list
*get_packed_refs(void)
71 static int did_refs
= 0;
72 static struct ref_list
*refs
= NULL
;
75 FILE *f
= fopen(git_path("packed-refs"), "r");
77 struct ref_list
*list
= NULL
;
78 char refline
[PATH_MAX
];
79 while (fgets(refline
, sizeof(refline
), f
)) {
80 unsigned char sha1
[20];
81 const char *name
= parse_ref_line(refline
, sha1
);
84 list
= add_ref(name
, sha1
, REF_ISPACKED
, list
);
94 static struct ref_list
*get_ref_dir(const char *base
, struct ref_list
*list
)
96 DIR *dir
= opendir(git_path("%s", base
));
100 int baselen
= strlen(base
);
101 char *ref
= xmalloc(baselen
+ 257);
103 memcpy(ref
, base
, baselen
);
104 if (baselen
&& base
[baselen
-1] != '/')
105 ref
[baselen
++] = '/';
107 while ((de
= readdir(dir
)) != NULL
) {
108 unsigned char sha1
[20];
113 if (de
->d_name
[0] == '.')
115 namelen
= strlen(de
->d_name
);
118 if (has_extension(de
->d_name
, ".lock"))
120 memcpy(ref
+ baselen
, de
->d_name
, namelen
+1);
121 if (stat(git_path("%s", ref
), &st
) < 0)
123 if (S_ISDIR(st
.st_mode
)) {
124 list
= get_ref_dir(ref
, list
);
127 if (!resolve_ref(ref
, sha1
, 1, &flag
)) {
128 error("%s points nowhere!", ref
);
131 list
= add_ref(ref
, sha1
, flag
, list
);
139 static struct ref_list
*get_loose_refs(void)
141 static int did_refs
= 0;
142 static struct ref_list
*refs
= NULL
;
145 refs
= get_ref_dir("refs", NULL
);
151 /* We allow "recursive" symbolic refs. Only within reason, though */
154 const char *resolve_ref(const char *ref
, unsigned char *sha1
, int reading
, int *flag
)
156 int depth
= MAXDEPTH
, len
;
158 static char ref_buffer
[256];
164 const char *path
= git_path("%s", ref
);
172 /* Special case: non-existing file.
173 * Not having the refs/heads/new-branch is OK
174 * if we are writing into it, so is .git/HEAD
175 * that points at refs/heads/master still to be
176 * born. It is NOT OK if we are resolving for
179 if (lstat(path
, &st
) < 0) {
180 struct ref_list
*list
= get_packed_refs();
182 if (!strcmp(ref
, list
->name
)) {
183 hashcpy(sha1
, list
->sha1
);
185 *flag
|= REF_ISPACKED
;
190 if (reading
|| errno
!= ENOENT
)
196 /* Follow "normalized" - ie "refs/.." symlinks by hand */
197 if (S_ISLNK(st
.st_mode
)) {
198 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
199 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
201 strcpy(ref_buffer
, buffer
);
204 *flag
|= REF_ISSYMREF
;
210 * Anything else, just open it and try to use it as
213 fd
= open(path
, O_RDONLY
);
216 len
= read(fd
, buffer
, sizeof(buffer
)-1);
220 * Is it a symbolic ref?
222 if (len
< 4 || memcmp("ref:", buffer
, 4))
226 while (len
&& isspace(*buf
))
228 while (len
&& isspace(buf
[len
-1]))
231 memcpy(ref_buffer
, buf
, len
+ 1);
234 *flag
|= REF_ISSYMREF
;
236 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
241 int create_symref(const char *ref_target
, const char *refs_heads_master
)
243 const char *lockpath
;
245 int fd
, len
, written
;
246 const char *git_HEAD
= git_path("%s", ref_target
);
248 #ifndef NO_SYMLINK_HEAD
249 if (prefer_symlink_refs
) {
251 if (!symlink(refs_heads_master
, git_HEAD
))
253 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
257 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
258 if (sizeof(ref
) <= len
) {
259 error("refname too long: %s", refs_heads_master
);
262 lockpath
= mkpath("%s.lock", git_HEAD
);
263 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
264 written
= write(fd
, ref
, len
);
266 if (written
!= len
) {
268 error("Unable to write to %s", lockpath
);
271 if (rename(lockpath
, git_HEAD
) < 0) {
273 error("Unable to create %s", git_HEAD
);
276 if (adjust_shared_perm(git_HEAD
)) {
278 error("Unable to fix permissions on %s", lockpath
);
284 int read_ref(const char *ref
, unsigned char *sha1
)
286 if (resolve_ref(ref
, sha1
, 1, NULL
))
291 static int do_for_each_ref(const char *base
, each_ref_fn fn
, int trim
,
295 struct ref_list
*packed
= get_packed_refs();
296 struct ref_list
*loose
= get_loose_refs();
298 while (packed
&& loose
) {
299 struct ref_list
*entry
;
300 int cmp
= strcmp(packed
->name
, loose
->name
);
302 packed
= packed
->next
;
310 packed
= packed
->next
;
312 if (strncmp(base
, entry
->name
, trim
))
314 if (is_null_sha1(entry
->sha1
))
316 if (!has_sha1_file(entry
->sha1
)) {
317 error("%s does not point to a valid object!", entry
->name
);
320 retval
= fn(entry
->name
+ trim
, entry
->sha1
,
321 entry
->flag
, cb_data
);
326 packed
= packed
? packed
: loose
;
328 if (!strncmp(base
, packed
->name
, trim
)) {
329 retval
= fn(packed
->name
+ trim
, packed
->sha1
,
330 packed
->flag
, cb_data
);
334 packed
= packed
->next
;
339 int head_ref(each_ref_fn fn
, void *cb_data
)
341 unsigned char sha1
[20];
344 if (resolve_ref("HEAD", sha1
, 1, &flag
))
345 return fn("HEAD", sha1
, flag
, cb_data
);
349 int for_each_ref(each_ref_fn fn
, void *cb_data
)
351 return do_for_each_ref("refs/", fn
, 0, cb_data
);
354 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
356 return do_for_each_ref("refs/tags/", fn
, 10, cb_data
);
359 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
361 return do_for_each_ref("refs/heads/", fn
, 11, cb_data
);
364 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
366 return do_for_each_ref("refs/remotes/", fn
, 13, cb_data
);
369 /* NEEDSWORK: This is only used by ssh-upload and it should go; the
370 * caller should do resolve_ref or read_ref like everybody else. Or
371 * maybe everybody else should use get_ref_sha1() instead of doing
374 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
376 if (check_ref_format(ref
))
378 return read_ref(mkpath("refs/%s", ref
), sha1
);
381 int delete_ref(const char *refname
, unsigned char *sha1
)
383 struct ref_lock
*lock
;
386 lock
= lock_any_ref_for_update(refname
, sha1
);
389 i
= strlen(lock
->lk
->filename
) - 5; /* .lock */
390 lock
->lk
->filename
[i
] = 0;
391 err
= unlink(lock
->lk
->filename
);
394 error("unlink(%s) failed: %s",
395 lock
->lk
->filename
, strerror(errno
));
397 lock
->lk
->filename
[i
] = '.';
399 err
= unlink(lock
->log_file
);
400 if (err
&& errno
!= ENOENT
)
401 fprintf(stderr
, "warning: unlink(%s) failed: %s",
402 lock
->log_file
, strerror(errno
));
408 * Make sure "ref" is something reasonable to have under ".git/refs/";
409 * We do not like it if:
411 * - any path component of it begins with ".", or
412 * - it has double dots "..", or
413 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
414 * - it ends with a "/".
417 static inline int bad_ref_char(int ch
)
419 return (((unsigned) ch
) <= ' ' ||
420 ch
== '~' || ch
== '^' || ch
== ':' ||
421 /* 2.13 Pattern Matching Notation */
422 ch
== '?' || ch
== '*' || ch
== '[');
425 int check_ref_format(const char *ref
)
428 const char *cp
= ref
;
432 while ((ch
= *cp
++) == '/')
433 ; /* tolerate duplicated slashes */
435 return -1; /* should not end with slashes */
437 /* we are at the beginning of the path component */
438 if (ch
== '.' || bad_ref_char(ch
))
441 /* scan the rest of the path component */
442 while ((ch
= *cp
++) != 0) {
443 if (bad_ref_char(ch
))
447 if (ch
== '.' && *cp
== '.')
453 return -1; /* at least of form "heads/blah" */
459 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
460 const unsigned char *old_sha1
, int mustexist
)
462 if (!resolve_ref(lock
->ref_name
, lock
->old_sha1
, mustexist
, NULL
)) {
463 error("Can't verify ref %s", lock
->ref_name
);
467 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
468 error("Ref %s is at %s but expected %s", lock
->ref_name
,
469 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
476 static struct ref_lock
*lock_ref_sha1_basic(const char *ref
, const unsigned char *old_sha1
)
479 const char *orig_ref
= ref
;
480 struct ref_lock
*lock
;
482 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
484 lock
= xcalloc(1, sizeof(struct ref_lock
));
487 ref
= resolve_ref(ref
, lock
->old_sha1
, mustexist
, NULL
);
489 int last_errno
= errno
;
490 error("unable to resolve reference %s: %s",
491 orig_ref
, strerror(errno
));
496 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
498 lock
->ref_name
= xstrdup(ref
);
499 lock
->log_file
= xstrdup(git_path("logs/%s", ref
));
500 ref_file
= git_path("%s", ref
);
501 lock
->force_write
= lstat(ref_file
, &st
) && errno
== ENOENT
;
503 if (safe_create_leading_directories(ref_file
))
504 die("unable to create directory for %s", ref_file
);
505 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, ref_file
, 1);
507 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
510 struct ref_lock
*lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
512 char refpath
[PATH_MAX
];
513 if (check_ref_format(ref
))
515 strcpy(refpath
, mkpath("refs/%s", ref
));
516 return lock_ref_sha1_basic(refpath
, old_sha1
);
519 struct ref_lock
*lock_any_ref_for_update(const char *ref
, const unsigned char *old_sha1
)
521 return lock_ref_sha1_basic(ref
, old_sha1
);
524 void unlock_ref(struct ref_lock
*lock
)
526 if (lock
->lock_fd
>= 0) {
527 close(lock
->lock_fd
);
528 /* Do not free lock->lk -- atexit() still looks at them */
530 rollback_lock_file(lock
->lk
);
532 free(lock
->ref_name
);
533 free(lock
->log_file
);
537 static int log_ref_write(struct ref_lock
*lock
,
538 const unsigned char *sha1
, const char *msg
)
540 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
541 unsigned maxlen
, len
;
543 const char *committer
;
545 if (log_all_ref_updates
) {
546 if (safe_create_leading_directories(lock
->log_file
) < 0)
547 return error("unable to create directory for %s",
552 logfd
= open(lock
->log_file
, oflags
, 0666);
554 if (!log_all_ref_updates
&& errno
== ENOENT
)
556 return error("Unable to append to %s: %s",
557 lock
->log_file
, strerror(errno
));
560 committer
= git_committer_info(1);
562 maxlen
= strlen(committer
) + strlen(msg
) + 2*40 + 5;
563 logrec
= xmalloc(maxlen
);
564 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
565 sha1_to_hex(lock
->old_sha1
),
571 maxlen
= strlen(committer
) + 2*40 + 4;
572 logrec
= xmalloc(maxlen
);
573 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
574 sha1_to_hex(lock
->old_sha1
),
578 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
582 return error("Unable to append to %s", lock
->log_file
);
586 int write_ref_sha1(struct ref_lock
*lock
,
587 const unsigned char *sha1
, const char *logmsg
)
589 static char term
= '\n';
593 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
597 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
598 write(lock
->lock_fd
, &term
, 1) != 1
599 || close(lock
->lock_fd
) < 0) {
600 error("Couldn't write %s", lock
->lk
->filename
);
604 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
608 if (commit_lock_file(lock
->lk
)) {
609 error("Couldn't set %s", lock
->ref_name
);
618 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
620 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
625 unsigned char logged_sha1
[20];
627 logfile
= git_path("logs/%s", ref
);
628 logfd
= open(logfile
, O_RDONLY
, 0);
630 die("Unable to read log %s: %s", logfile
, strerror(errno
));
633 die("Log %s is empty.", logfile
);
634 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
638 rec
= logend
= logdata
+ st
.st_size
;
639 while (logdata
< rec
) {
640 if (logdata
< rec
&& *(rec
-1) == '\n')
643 while (logdata
< rec
&& *(rec
-1) != '\n') {
649 die("Log %s is corrupt.", logfile
);
650 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
651 if (date
<= at_time
) {
653 if (get_sha1_hex(lastrec
, logged_sha1
))
654 die("Log %s is corrupt.", logfile
);
655 if (get_sha1_hex(rec
+ 41, sha1
))
656 die("Log %s is corrupt.", logfile
);
657 if (hashcmp(logged_sha1
, sha1
)) {
658 tz
= strtoul(tz_c
, NULL
, 10);
660 "warning: Log %s has gap after %s.\n",
661 logfile
, show_rfc2822_date(date
, tz
));
664 else if (date
== at_time
) {
665 if (get_sha1_hex(rec
+ 41, sha1
))
666 die("Log %s is corrupt.", logfile
);
669 if (get_sha1_hex(rec
+ 41, logged_sha1
))
670 die("Log %s is corrupt.", logfile
);
671 if (hashcmp(logged_sha1
, sha1
)) {
672 tz
= strtoul(tz_c
, NULL
, 10);
674 "warning: Log %s unexpectedly ended on %s.\n",
675 logfile
, show_rfc2822_date(date
, tz
));
678 munmap((void*)logdata
, st
.st_size
);
685 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
687 if (rec
== logend
|| *rec
== '\n')
688 die("Log %s is corrupt.", logfile
);
689 date
= strtoul(rec
+ 1, &tz_c
, 10);
690 tz
= strtoul(tz_c
, NULL
, 10);
691 if (get_sha1_hex(logdata
, sha1
))
692 die("Log %s is corrupt.", logfile
);
693 munmap((void*)logdata
, st
.st_size
);
694 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
695 logfile
, show_rfc2822_date(date
, tz
));