6 /* We allow "recursive" symbolic refs. Only within reason, though */
9 #ifndef USE_SYMLINK_HEAD
10 #define USE_SYMLINK_HEAD 1
13 const char *resolve_ref(const char *path
, unsigned char *sha1
, int reading
)
15 int depth
= MAXDEPTH
, len
;
26 /* Special case: non-existing file.
27 * Not having the refs/heads/new-branch is OK
28 * if we are writing into it, so is .git/HEAD
29 * that points at refs/heads/master still to be
30 * born. It is NOT OK if we are resolving for
33 if (lstat(path
, &st
) < 0) {
34 if (reading
|| errno
!= ENOENT
)
40 /* Follow "normalized" - ie "refs/.." symlinks by hand */
41 if (S_ISLNK(st
.st_mode
)) {
42 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
43 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
44 path
= git_path("%.*s", len
, buffer
);
50 * Anything else, just open it and try to use it as
53 fd
= open(path
, O_RDONLY
);
56 len
= read(fd
, buffer
, sizeof(buffer
)-1);
60 * Is it a symbolic ref?
62 if (len
< 4 || memcmp("ref:", buffer
, 4))
66 while (len
&& isspace(*buf
))
68 while (len
&& isspace(buf
[len
-1]))
70 path
= git_path("%.*s", len
, buf
);
72 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
77 int create_symref(const char *git_HEAD
, const char *refs_heads_master
)
84 if (!only_use_symrefs
) {
86 if (!symlink(refs_heads_master
, git_HEAD
))
88 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
92 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
93 if (sizeof(ref
) <= len
) {
94 error("refname too long: %s", refs_heads_master
);
97 lockpath
= mkpath("%s.lock", git_HEAD
);
98 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
99 written
= write(fd
, ref
, len
);
101 if (written
!= len
) {
103 error("Unable to write to %s", lockpath
);
106 if (rename(lockpath
, git_HEAD
) < 0) {
108 error("Unable to create %s", git_HEAD
);
114 int read_ref(const char *filename
, unsigned char *sha1
)
116 if (resolve_ref(filename
, sha1
, 1))
121 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
))
124 DIR *dir
= opendir(git_path("%s", base
));
128 int baselen
= strlen(base
);
129 char *path
= xmalloc(baselen
+ 257);
131 if (!strncmp(base
, "./", 2)) {
135 memcpy(path
, base
, baselen
);
136 if (baselen
&& base
[baselen
-1] != '/')
137 path
[baselen
++] = '/';
139 while ((de
= readdir(dir
)) != NULL
) {
140 unsigned char sha1
[20];
144 if (de
->d_name
[0] == '.')
146 namelen
= strlen(de
->d_name
);
149 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
150 if (stat(git_path("%s", path
), &st
) < 0)
152 if (S_ISDIR(st
.st_mode
)) {
153 retval
= do_for_each_ref(path
, fn
);
158 if (read_ref(git_path("%s", path
), sha1
) < 0)
160 if (!has_sha1_file(sha1
))
162 retval
= fn(path
, sha1
);
172 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
174 unsigned char sha1
[20];
175 if (!read_ref(git_path("HEAD"), sha1
))
176 return fn("HEAD", sha1
);
180 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
182 return do_for_each_ref("refs", fn
);
185 static char *ref_file_name(const char *ref
)
187 char *base
= get_refs_directory();
188 int baselen
= strlen(base
);
189 int reflen
= strlen(ref
);
190 char *ret
= xmalloc(baselen
+ 2 + reflen
);
191 sprintf(ret
, "%s/%s", base
, ref
);
195 static char *ref_lock_file_name(const char *ref
)
197 char *base
= get_refs_directory();
198 int baselen
= strlen(base
);
199 int reflen
= strlen(ref
);
200 char *ret
= xmalloc(baselen
+ 7 + reflen
);
201 sprintf(ret
, "%s/%s.lock", base
, ref
);
205 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
207 const char *filename
;
209 if (check_ref_format(ref
))
211 filename
= git_path("refs/%s", ref
);
212 return read_ref(filename
, sha1
);
215 static int lock_ref_file(const char *filename
, const char *lock_filename
,
216 const unsigned char *old_sha1
)
218 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
219 unsigned char current_sha1
[20];
222 return error("Couldn't open lock file for %s: %s",
223 filename
, strerror(errno
));
225 retval
= read_ref(filename
, current_sha1
);
229 unlink(lock_filename
);
230 return error("Could not read the current value of %s",
233 if (memcmp(current_sha1
, old_sha1
, 20)) {
235 unlink(lock_filename
);
236 error("The current value of %s is %s",
237 filename
, sha1_to_hex(current_sha1
));
238 return error("Expected %s",
239 sha1_to_hex(old_sha1
));
244 unlink(lock_filename
);
245 return error("Unexpectedly found a value of %s for %s",
246 sha1_to_hex(current_sha1
), filename
);
252 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
257 if (check_ref_format(ref
))
259 filename
= ref_file_name(ref
);
260 lock_filename
= ref_lock_file_name(ref
);
261 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
267 static int write_ref_file(const char *filename
,
268 const char *lock_filename
, int fd
,
269 const unsigned char *sha1
)
271 char *hex
= sha1_to_hex(sha1
);
273 if (write(fd
, hex
, 40) < 40 ||
274 write(fd
, &term
, 1) < 1) {
275 error("Couldn't write %s\n", filename
);
280 rename(lock_filename
, filename
);
284 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
291 if (check_ref_format(ref
))
293 filename
= ref_file_name(ref
);
294 lock_filename
= ref_lock_file_name(ref
);
295 if (safe_create_leading_directories(filename
))
296 die("unable to create leading directory for %s", filename
);
297 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
304 * Make sure "ref" is something reasonable to have under ".git/refs/";
305 * We do not like it if:
307 * - any path component of it begins with ".", or
308 * - it has double dots "..", or
309 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
310 * - it ends with a "/".
313 static inline int bad_ref_char(int ch
)
315 return (((unsigned) ch
) <= ' ' ||
316 ch
== '~' || ch
== '^' || ch
== ':' ||
317 /* 2.13 Pattern Matching Notation */
318 ch
== '?' || ch
== '*' || ch
== '[');
321 int check_ref_format(const char *ref
)
324 const char *cp
= ref
;
328 while ((ch
= *cp
++) == '/')
329 ; /* tolerate duplicated slashes */
331 return -1; /* should not end with slashes */
333 /* we are at the beginning of the path component */
334 if (ch
== '.' || bad_ref_char(ch
))
337 /* scan the rest of the path component */
338 while ((ch
= *cp
++) != 0) {
339 if (bad_ref_char(ch
))
343 if (ch
== '.' && *cp
== '.')
349 return -1; /* at least of form "heads/blah" */
355 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
361 if (check_ref_format(ref
))
363 filename
= ref_file_name(ref
);
364 lock_filename
= ref_lock_file_name(ref
);
365 if (safe_create_leading_directories(filename
))
366 die("unable to create leading directory for %s", filename
);
367 fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
369 error("Writing %s", lock_filename
);
372 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);