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 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
146 if (stat(git_path("%s", path
), &st
) < 0)
148 if (S_ISDIR(st
.st_mode
)) {
149 retval
= do_for_each_ref(path
, fn
, trim
);
154 if (read_ref(git_path("%s", path
), sha1
) < 0) {
155 error("%s points nowhere!", path
);
158 if (!has_sha1_file(sha1
)) {
159 error("%s does not point to a valid "
160 "commit object!", path
);
163 retval
= fn(path
+ trim
, sha1
);
173 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
175 unsigned char sha1
[20];
176 if (!read_ref(git_path("HEAD"), sha1
))
177 return fn("HEAD", sha1
);
181 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
183 return do_for_each_ref("refs", fn
, 0);
186 int for_each_tag_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
188 return do_for_each_ref("refs/tags", fn
, 10);
191 int for_each_branch_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
193 return do_for_each_ref("refs/heads", fn
, 11);
196 int for_each_remote_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
198 return do_for_each_ref("refs/remotes", fn
, 13);
201 static char *ref_file_name(const char *ref
)
203 char *base
= get_refs_directory();
204 int baselen
= strlen(base
);
205 int reflen
= strlen(ref
);
206 char *ret
= xmalloc(baselen
+ 2 + reflen
);
207 sprintf(ret
, "%s/%s", base
, ref
);
211 static char *ref_lock_file_name(const char *ref
)
213 char *base
= get_refs_directory();
214 int baselen
= strlen(base
);
215 int reflen
= strlen(ref
);
216 char *ret
= xmalloc(baselen
+ 7 + reflen
);
217 sprintf(ret
, "%s/%s.lock", base
, ref
);
221 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
223 if (check_ref_format(ref
))
225 return read_ref(git_path("refs/%s", ref
), sha1
);
228 static int lock_ref_file(const char *filename
, const char *lock_filename
,
229 const unsigned char *old_sha1
)
231 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
232 unsigned char current_sha1
[20];
235 return error("Couldn't open lock file for %s: %s",
236 filename
, strerror(errno
));
238 retval
= read_ref(filename
, current_sha1
);
242 unlink(lock_filename
);
243 return error("Could not read the current value of %s",
246 if (memcmp(current_sha1
, old_sha1
, 20)) {
248 unlink(lock_filename
);
249 error("The current value of %s is %s",
250 filename
, sha1_to_hex(current_sha1
));
251 return error("Expected %s",
252 sha1_to_hex(old_sha1
));
257 unlink(lock_filename
);
258 return error("Unexpectedly found a value of %s for %s",
259 sha1_to_hex(current_sha1
), filename
);
265 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
270 if (check_ref_format(ref
))
272 filename
= ref_file_name(ref
);
273 lock_filename
= ref_lock_file_name(ref
);
274 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
280 static int write_ref_file(const char *filename
,
281 const char *lock_filename
, int fd
,
282 const unsigned char *sha1
)
284 char *hex
= sha1_to_hex(sha1
);
286 if (write(fd
, hex
, 40) < 40 ||
287 write(fd
, &term
, 1) < 1) {
288 error("Couldn't write %s", filename
);
293 rename(lock_filename
, filename
);
297 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
304 if (check_ref_format(ref
))
306 filename
= ref_file_name(ref
);
307 lock_filename
= ref_lock_file_name(ref
);
308 if (safe_create_leading_directories(filename
))
309 die("unable to create leading directory for %s", filename
);
310 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
317 * Make sure "ref" is something reasonable to have under ".git/refs/";
318 * We do not like it if:
320 * - any path component of it begins with ".", or
321 * - it has double dots "..", or
322 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
323 * - it ends with a "/".
326 static inline int bad_ref_char(int ch
)
328 return (((unsigned) ch
) <= ' ' ||
329 ch
== '~' || ch
== '^' || ch
== ':' ||
330 /* 2.13 Pattern Matching Notation */
331 ch
== '?' || ch
== '*' || ch
== '[');
334 int check_ref_format(const char *ref
)
337 const char *cp
= ref
;
341 while ((ch
= *cp
++) == '/')
342 ; /* tolerate duplicated slashes */
344 return -1; /* should not end with slashes */
346 /* we are at the beginning of the path component */
347 if (ch
== '.' || bad_ref_char(ch
))
350 /* scan the rest of the path component */
351 while ((ch
= *cp
++) != 0) {
352 if (bad_ref_char(ch
))
356 if (ch
== '.' && *cp
== '.')
362 return -1; /* at least of form "heads/blah" */
368 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
374 if (check_ref_format(ref
))
376 filename
= ref_file_name(ref
);
377 lock_filename
= ref_lock_file_name(ref
);
378 if (safe_create_leading_directories(filename
))
379 die("unable to create leading directory for %s", filename
);
380 fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
382 error("Writing %s", lock_filename
);
385 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);