6 /* We allow "recursive" symbolic refs. Only within reason, though */
9 const char *resolve_ref(const char *path
, unsigned char *sha1
, int reading
)
11 int depth
= MAXDEPTH
, len
;
22 /* Special case: non-existing file.
23 * Not having the refs/heads/new-branch is OK
24 * if we are writing into it, so is .git/HEAD
25 * that points at refs/heads/master still to be
26 * born. It is NOT OK if we are resolving for
29 if (lstat(path
, &st
) < 0) {
30 if (reading
|| errno
!= ENOENT
)
36 /* Follow "normalized" - ie "refs/.." symlinks by hand */
37 if (S_ISLNK(st
.st_mode
)) {
38 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
39 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
40 path
= git_path("%.*s", len
, buffer
);
46 * Anything else, just open it and try to use it as
49 fd
= open(path
, O_RDONLY
);
52 len
= read(fd
, buffer
, sizeof(buffer
)-1);
56 * Is it a symbolic ref?
58 if (len
< 4 || memcmp("ref:", buffer
, 4))
62 while (len
&& isspace(*buf
))
64 while (len
&& isspace(buf
[len
-1]))
66 path
= git_path("%.*s", len
, buf
);
68 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
73 int create_symref(const char *git_HEAD
, const char *refs_heads_master
)
79 #ifndef NO_SYMLINK_HEAD
80 if (prefer_symlink_refs
) {
82 if (!symlink(refs_heads_master
, git_HEAD
))
84 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
88 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
89 if (sizeof(ref
) <= len
) {
90 error("refname too long: %s", refs_heads_master
);
93 lockpath
= mkpath("%s.lock", git_HEAD
);
94 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
95 written
= write(fd
, ref
, len
);
99 error("Unable to write to %s", lockpath
);
102 if (rename(lockpath
, git_HEAD
) < 0) {
104 error("Unable to create %s", git_HEAD
);
110 int read_ref(const char *filename
, unsigned char *sha1
)
112 if (resolve_ref(filename
, sha1
, 1))
117 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
), int trim
)
120 DIR *dir
= opendir(git_path("%s", base
));
124 int baselen
= strlen(base
);
125 char *path
= xmalloc(baselen
+ 257);
127 if (!strncmp(base
, "./", 2)) {
131 memcpy(path
, base
, baselen
);
132 if (baselen
&& base
[baselen
-1] != '/')
133 path
[baselen
++] = '/';
135 while ((de
= readdir(dir
)) != NULL
) {
136 unsigned char sha1
[20];
140 if (de
->d_name
[0] == '.')
142 namelen
= strlen(de
->d_name
);
145 if (namelen
>5 && !strcmp(de
->d_name
+namelen
-5,".lock"))
147 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
148 if (stat(git_path("%s", path
), &st
) < 0)
150 if (S_ISDIR(st
.st_mode
)) {
151 retval
= do_for_each_ref(path
, fn
, trim
);
156 if (read_ref(git_path("%s", path
), sha1
) < 0) {
157 error("%s points nowhere!", path
);
160 if (!has_sha1_file(sha1
)) {
161 error("%s does not point to a valid "
162 "commit object!", path
);
165 retval
= fn(path
+ trim
, sha1
);
175 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
177 unsigned char sha1
[20];
178 if (!read_ref(git_path("HEAD"), sha1
))
179 return fn("HEAD", sha1
);
183 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
185 return do_for_each_ref("refs", fn
, 0);
188 int for_each_tag_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
190 return do_for_each_ref("refs/tags", fn
, 10);
193 int for_each_branch_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
195 return do_for_each_ref("refs/heads", fn
, 11);
198 int for_each_remote_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
200 return do_for_each_ref("refs/remotes", fn
, 13);
203 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
205 if (check_ref_format(ref
))
207 return read_ref(git_path("refs/%s", ref
), sha1
);
211 * Make sure "ref" is something reasonable to have under ".git/refs/";
212 * We do not like it if:
214 * - any path component of it begins with ".", or
215 * - it has double dots "..", or
216 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
217 * - it ends with a "/".
220 static inline int bad_ref_char(int ch
)
222 return (((unsigned) ch
) <= ' ' ||
223 ch
== '~' || ch
== '^' || ch
== ':' ||
224 /* 2.13 Pattern Matching Notation */
225 ch
== '?' || ch
== '*' || ch
== '[');
228 int check_ref_format(const char *ref
)
231 const char *cp
= ref
;
235 while ((ch
= *cp
++) == '/')
236 ; /* tolerate duplicated slashes */
238 return -1; /* should not end with slashes */
240 /* we are at the beginning of the path component */
241 if (ch
== '.' || bad_ref_char(ch
))
244 /* scan the rest of the path component */
245 while ((ch
= *cp
++) != 0) {
246 if (bad_ref_char(ch
))
250 if (ch
== '.' && *cp
== '.')
256 return -1; /* at least of form "heads/blah" */
262 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
263 const unsigned char *old_sha1
, int mustexist
)
266 int nr
, fd
= open(lock
->ref_file
, O_RDONLY
);
267 if (fd
< 0 && (mustexist
|| errno
!= ENOENT
)) {
268 error("Can't verify ref %s", lock
->ref_file
);
272 nr
= read(fd
, buf
, 40);
274 if (nr
!= 40 || get_sha1_hex(buf
, lock
->old_sha1
) < 0) {
275 error("Can't verify ref %s", lock
->ref_file
);
279 if (memcmp(lock
->old_sha1
, old_sha1
, 20)) {
280 error("Ref %s is at %s but expected %s", lock
->ref_file
,
281 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
288 static struct ref_lock
*lock_ref_sha1_basic(const char *path
,
290 const unsigned char *old_sha1
, int mustexist
)
292 struct ref_lock
*lock
;
295 lock
= xcalloc(1, sizeof(struct ref_lock
));
298 plen
= strlen(path
) - plen
;
299 path
= resolve_ref(path
, lock
->old_sha1
, mustexist
);
304 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
306 lock
->ref_file
= strdup(path
);
307 lock
->log_file
= strdup(git_path("logs/%s", lock
->ref_file
+ plen
));
308 lock
->force_write
= lstat(lock
->ref_file
, &st
) && errno
== ENOENT
;
310 if (safe_create_leading_directories(lock
->ref_file
))
311 die("unable to create directory for %s", lock
->ref_file
);
312 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, lock
->ref_file
);
313 if (lock
->lock_fd
< 0) {
314 error("Couldn't open lock file %s: %s",
315 lock
->lk
->filename
, strerror(errno
));
320 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
323 struct ref_lock
*lock_ref_sha1(const char *ref
,
324 const unsigned char *old_sha1
, int mustexist
)
326 if (check_ref_format(ref
))
328 return lock_ref_sha1_basic(git_path("refs/%s", ref
),
329 5 + strlen(ref
), old_sha1
, mustexist
);
332 struct ref_lock
*lock_any_ref_for_update(const char *ref
,
333 const unsigned char *old_sha1
, int mustexist
)
335 return lock_ref_sha1_basic(git_path("%s", ref
),
336 strlen(ref
), old_sha1
, mustexist
);
339 void unlock_ref(struct ref_lock
*lock
)
341 if (lock
->lock_fd
>= 0) {
342 close(lock
->lock_fd
);
343 /* Do not free lock->lk -- atexit() still looks at them */
345 rollback_lock_file(lock
->lk
);
348 free(lock
->ref_file
);
350 free(lock
->log_file
);
354 static int log_ref_write(struct ref_lock
*lock
,
355 const unsigned char *sha1
, const char *msg
)
357 int logfd
, written
, oflags
= O_APPEND
| O_WRONLY
;
358 unsigned maxlen
, len
;
360 const char *comitter
;
362 if (log_all_ref_updates
) {
363 if (safe_create_leading_directories(lock
->log_file
) < 0)
364 return error("unable to create directory for %s",
369 logfd
= open(lock
->log_file
, oflags
, 0666);
371 if (!log_all_ref_updates
&& errno
== ENOENT
)
373 return error("Unable to append to %s: %s",
374 lock
->log_file
, strerror(errno
));
378 comitter
= git_committer_info(1);
380 maxlen
= strlen(comitter
) + strlen(msg
) + 2*40 + 5;
381 logrec
= xmalloc(maxlen
);
382 len
= snprintf(logrec
, maxlen
, "%s %s %s\t%s\n",
383 sha1_to_hex(lock
->old_sha1
),
389 maxlen
= strlen(comitter
) + 2*40 + 4;
390 logrec
= xmalloc(maxlen
);
391 len
= snprintf(logrec
, maxlen
, "%s %s %s\n",
392 sha1_to_hex(lock
->old_sha1
),
396 written
= len
<= maxlen
? write(logfd
, logrec
, len
) : -1;
400 return error("Unable to append to %s", lock
->log_file
);
404 int write_ref_sha1(struct ref_lock
*lock
,
405 const unsigned char *sha1
, const char *logmsg
)
407 static char term
= '\n';
411 if (!lock
->force_write
&& !memcmp(lock
->old_sha1
, sha1
, 20)) {
415 if (write(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
416 write(lock
->lock_fd
, &term
, 1) != 1
417 || close(lock
->lock_fd
) < 0) {
418 error("Couldn't write %s", lock
->lk
->filename
);
422 if (log_ref_write(lock
, sha1
, logmsg
) < 0) {
426 if (commit_lock_file(lock
->lk
)) {
427 error("Couldn't set %s", lock
->ref_file
);
436 int read_ref_at(const char *ref
, unsigned long at_time
, unsigned char *sha1
)
438 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
443 unsigned char logged_sha1
[20];
445 logfile
= git_path("logs/%s", ref
);
446 logfd
= open(logfile
, O_RDONLY
, 0);
448 die("Unable to read log %s: %s", logfile
, strerror(errno
));
451 die("Log %s is empty.", logfile
);
452 logdata
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
456 rec
= logend
= logdata
+ st
.st_size
;
457 while (logdata
< rec
) {
458 if (logdata
< rec
&& *(rec
-1) == '\n')
461 while (logdata
< rec
&& *(rec
-1) != '\n') {
467 die("Log %s is corrupt.", logfile
);
468 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
469 if (date
<= at_time
) {
471 if (get_sha1_hex(lastrec
, logged_sha1
))
472 die("Log %s is corrupt.", logfile
);
473 if (get_sha1_hex(rec
+ 41, sha1
))
474 die("Log %s is corrupt.", logfile
);
475 if (memcmp(logged_sha1
, sha1
, 20)) {
476 tz
= strtoul(tz_c
, NULL
, 10);
478 "warning: Log %s has gap after %s.\n",
479 logfile
, show_rfc2822_date(date
, tz
));
482 else if (date
== at_time
) {
483 if (get_sha1_hex(rec
+ 41, sha1
))
484 die("Log %s is corrupt.", logfile
);
487 if (get_sha1_hex(rec
+ 41, logged_sha1
))
488 die("Log %s is corrupt.", logfile
);
489 if (memcmp(logged_sha1
, sha1
, 20)) {
490 tz
= strtoul(tz_c
, NULL
, 10);
492 "warning: Log %s unexpectedly ended on %s.\n",
493 logfile
, show_rfc2822_date(date
, tz
));
496 munmap((void*)logdata
, st
.st_size
);
503 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
505 if (rec
== logend
|| *rec
== '\n')
506 die("Log %s is corrupt.", logfile
);
507 date
= strtoul(rec
+ 1, &tz_c
, 10);
508 tz
= strtoul(tz_c
, NULL
, 10);
509 if (get_sha1_hex(logdata
, sha1
))
510 die("Log %s is corrupt.", logfile
);
511 munmap((void*)logdata
, st
.st_size
);
512 fprintf(stderr
, "warning: Log %s only goes back to %s.\n",
513 logfile
, show_rfc2822_date(date
, tz
));