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 *ref = xmalloc(baselen + 257);
98 memcpy(ref, base, baselen);
99 if (baselen && base[baselen-1] != '/')
100 ref[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(ref + baselen, de->d_name, namelen+1);
115 if (stat(git_path("%s", ref), &st) < 0)
117 if (S_ISDIR(st.st_mode)) {
118 list = get_ref_dir(ref, list);
121 if (read_ref(ref, sha1) < 0) {
122 error("%s points nowhere!", ref);
125 list = add_ref(ref, 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 *ref, unsigned char *sha1, int reading)
150 int depth = MAXDEPTH, len;
152 static char ref_buffer[256];
155 const char *path = git_path("%s", ref);
163 /* Special case: non-existing file.
164 * Not having the refs/heads/new-branch is OK
165 * if we are writing into it, so is .git/HEAD
166 * that points at refs/heads/master still to be
167 * born. It is NOT OK if we are resolving for
170 if (lstat(path, &st) < 0) {
171 if (reading || errno != ENOENT)
177 /* Follow "normalized" - ie "refs/.." symlinks by hand */
178 if (S_ISLNK(st.st_mode)) {
179 len = readlink(path, buffer, sizeof(buffer)-1);
180 if (len >= 5 && !memcmp("refs/", buffer, 5)) {
182 strcpy(ref_buffer, buffer);
189 * Anything else, just open it and try to use it as
192 fd = open(path, O_RDONLY);
195 len = read(fd, buffer, sizeof(buffer)-1);
199 * Is it a symbolic ref?
201 if (len < 4 || memcmp("ref:", buffer, 4))
205 while (len && isspace(*buf))
207 while (len && isspace(buf[len-1]))
210 memcpy(ref_buffer, buf, len + 1);
213 if (len < 40 || get_sha1_hex(buffer, sha1))
218 int create_symref(const char *ref_target, const char *refs_heads_master)
220 const char *lockpath;
222 int fd, len, written;
223 const char *git_HEAD = git_path("%s", ref_target);
225 #ifndef NO_SYMLINK_HEAD
226 if (prefer_symlink_refs) {
228 if (!symlink(refs_heads_master, git_HEAD))
230 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
234 len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
235 if (sizeof(ref) <= len) {
236 error("refname too long: %s", refs_heads_master);
239 lockpath = mkpath("%s.lock", git_HEAD);
240 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
241 written = write(fd, ref, len);
243 if (written != len) {
245 error("Unable to write to %s", lockpath);
248 if (rename(lockpath, git_HEAD) < 0) {
250 error("Unable to create %s", git_HEAD);
253 if (adjust_shared_perm(git_HEAD)) {
255 error("Unable to fix permissions on %s", lockpath);
261 int read_ref(const char *ref, unsigned char *sha1)
263 if (resolve_ref(ref, sha1, 1))
268 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
271 struct ref_list *packed = get_packed_refs();
272 struct ref_list *loose = get_loose_refs();
274 while (packed && loose) {
275 struct ref_list *entry;
276 int cmp = strcmp(packed->name, loose->name);
278 packed = packed->next;
286 packed = packed->next;
288 if (strncmp(base, entry->name, trim))
290 if (is_null_sha1(entry->sha1))
292 if (!has_sha1_file(entry->sha1)) {
293 error("%s does not point to a valid object!", entry->name);
296 retval = fn(entry->name + trim, entry->sha1);
301 packed = packed ? packed : loose;
303 if (!strncmp(base, packed->name, trim)) {
304 retval = fn(packed->name + trim, packed->sha1);
308 packed = packed->next;
313 int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
315 unsigned char sha1[20];
316 if (!read_ref("HEAD", sha1))
317 return fn("HEAD", sha1);
321 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
323 return do_for_each_ref("refs/", fn, 0);
326 int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
328 return do_for_each_ref("refs/tags/", fn, 10);
331 int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
333 return do_for_each_ref("refs/heads/", fn, 11);
336 int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
338 return do_for_each_ref("refs/remotes/", fn, 13);
341 int get_ref_sha1(const char *ref, unsigned char *sha1)
343 if (check_ref_format(ref))
345 return read_ref(mkpath("refs/%s", ref), sha1);
349 * Make sure "ref" is something reasonable to have under ".git/refs/";
350 * We do not like it if:
352 * - any path component of it begins with ".", or
353 * - it has double dots "..", or
354 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
355 * - it ends with a "/".
358 static inline int bad_ref_char(int ch)
360 return (((unsigned) ch) <= ' ' ||
361 ch == '~' || ch == '^' || ch == ':' ||
362 /* 2.13 Pattern Matching Notation */
363 ch == '?' || ch == '*' || ch == '[');
366 int check_ref_format(const char *ref)
369 const char *cp = ref;
373 while ((ch = *cp++) == '/')
374 ; /* tolerate duplicated slashes */
376 return -1; /* should not end with slashes */
378 /* we are at the beginning of the path component */
379 if (ch == '.' || bad_ref_char(ch))
382 /* scan the rest of the path component */
383 while ((ch = *cp++) != 0) {
384 if (bad_ref_char(ch))
388 if (ch == '.' && *cp == '.')
394 return -1; /* at least of form "heads/blah" */
400 static struct ref_lock *verify_lock(struct ref_lock *lock,
401 const unsigned char *old_sha1, int mustexist)
404 int nr, fd = open(lock->ref_file, O_RDONLY);
405 if (fd < 0 && (mustexist || errno != ENOENT)) {
406 error("Can't verify ref %s", lock->ref_file);
410 nr = read(fd, buf, 40);
412 if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) {
413 error("Can't verify ref %s", lock->ref_file);
417 if (hashcmp(lock->old_sha1, old_sha1)) {
418 error("Ref %s is at %s but expected %s", lock->ref_file,
419 sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
426 static struct ref_lock *lock_ref_sha1_basic(const char *ref,
428 const unsigned char *old_sha1, int mustexist)
430 const char *orig_ref = ref;
431 struct ref_lock *lock;
434 lock = xcalloc(1, sizeof(struct ref_lock));
437 ref = resolve_ref(ref, lock->old_sha1, mustexist);
439 int last_errno = errno;
440 error("unable to resolve reference %s: %s",
441 orig_ref, strerror(errno));
446 lock->lk = xcalloc(1, sizeof(struct lock_file));
448 lock->ref_file = xstrdup(git_path("%s", ref));
449 lock->log_file = xstrdup(git_path("logs/%s", ref));
450 lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
452 if (safe_create_leading_directories(lock->ref_file))
453 die("unable to create directory for %s", lock->ref_file);
454 lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file, 1);
456 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
459 struct ref_lock *lock_ref_sha1(const char *ref,
460 const unsigned char *old_sha1, int mustexist)
462 if (check_ref_format(ref))
464 return lock_ref_sha1_basic(mkpath("refs/%s", ref),
465 5 + strlen(ref), old_sha1, mustexist);
468 struct ref_lock *lock_any_ref_for_update(const char *ref,
469 const unsigned char *old_sha1, int mustexist)
471 return lock_ref_sha1_basic(ref, strlen(ref), old_sha1, mustexist);
474 void unlock_ref(struct ref_lock *lock)
476 if (lock->lock_fd >= 0) {
477 close(lock->lock_fd);
478 /* Do not free lock->lk -- atexit() still looks at them */
480 rollback_lock_file(lock->lk);
482 free(lock->ref_file);
483 free(lock->log_file);
487 static int log_ref_write(struct ref_lock *lock,
488 const unsigned char *sha1, const char *msg)
490 int logfd, written, oflags = O_APPEND | O_WRONLY;
491 unsigned maxlen, len;
493 const char *committer;
495 if (log_all_ref_updates) {
496 if (safe_create_leading_directories(lock->log_file) < 0)
497 return error("unable to create directory for %s",
502 logfd = open(lock->log_file, oflags, 0666);
504 if (!log_all_ref_updates && errno == ENOENT)
506 return error("Unable to append to %s: %s",
507 lock->log_file, strerror(errno));
510 committer = git_committer_info(1);
512 maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
513 logrec = xmalloc(maxlen);
514 len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
515 sha1_to_hex(lock->old_sha1),
521 maxlen = strlen(committer) + 2*40 + 4;
522 logrec = xmalloc(maxlen);
523 len = snprintf(logrec, maxlen, "%s %s %s\n",
524 sha1_to_hex(lock->old_sha1),
528 written = len <= maxlen ? write(logfd, logrec, len) : -1;
532 return error("Unable to append to %s", lock->log_file);
536 int write_ref_sha1(struct ref_lock *lock,
537 const unsigned char *sha1, const char *logmsg)
539 static char term = '\n';
543 if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
547 if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
548 write(lock->lock_fd, &term, 1) != 1
549 || close(lock->lock_fd) < 0) {
550 error("Couldn't write %s", lock->lk->filename);
554 if (log_ref_write(lock, sha1, logmsg) < 0) {
558 if (commit_lock_file(lock->lk)) {
559 error("Couldn't set %s", lock->ref_file);
568 int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
570 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
575 unsigned char logged_sha1[20];
577 logfile = git_path("logs/%s", ref);
578 logfd = open(logfile, O_RDONLY, 0);
580 die("Unable to read log %s: %s", logfile, strerror(errno));
583 die("Log %s is empty.", logfile);
584 logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
588 rec = logend = logdata + st.st_size;
589 while (logdata < rec) {
590 if (logdata < rec && *(rec-1) == '\n')
593 while (logdata < rec && *(rec-1) != '\n') {
599 die("Log %s is corrupt.", logfile);
600 date = strtoul(lastgt + 1, &tz_c, 10);
601 if (date <= at_time) {
603 if (get_sha1_hex(lastrec, logged_sha1))
604 die("Log %s is corrupt.", logfile);
605 if (get_sha1_hex(rec + 41, sha1))
606 die("Log %s is corrupt.", logfile);
607 if (hashcmp(logged_sha1, sha1)) {
608 tz = strtoul(tz_c, NULL, 10);
610 "warning: Log %s has gap after %s.\n",
611 logfile, show_rfc2822_date(date, tz));
614 else if (date == at_time) {
615 if (get_sha1_hex(rec + 41, sha1))
616 die("Log %s is corrupt.", logfile);
619 if (get_sha1_hex(rec + 41, logged_sha1))
620 die("Log %s is corrupt.", logfile);
621 if (hashcmp(logged_sha1, sha1)) {
622 tz = strtoul(tz_c, NULL, 10);
624 "warning: Log %s unexpectedly ended on %s.\n",
625 logfile, show_rfc2822_date(date, tz));
628 munmap((void*)logdata, st.st_size);
635 while (rec < logend && *rec != '>' && *rec != '\n')
637 if (rec == logend || *rec == '\n')
638 die("Log %s is corrupt.", logfile);
639 date = strtoul(rec + 1, &tz_c, 10);
640 tz = strtoul(tz_c, NULL, 10);
641 if (get_sha1_hex(logdata, sha1))
642 die("Log %s is corrupt.", logfile);
643 munmap((void*)logdata, st.st_size);
644 fprintf(stderr, "warning: Log %s only goes back to %s.\n",
645 logfile, show_rfc2822_date(date, tz));