6 /* We allow "recursive" symbolic refs. Only within reason, though */
9 #ifndef USE_SYMLINK_HEAD
10 #define USE_SYMLINK_HEAD 1
13 int validate_symref(const char *path
)
16 char *buf
, buffer
[256];
19 if (lstat(path
, &st
) < 0)
22 /* Make sure it is a "refs/.." symlink */
23 if (S_ISLNK(st
.st_mode
)) {
24 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
25 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
31 * Anything else, just open it and try to see if it is a symbolic ref.
33 fd
= open(path
, O_RDONLY
);
36 len
= read(fd
, buffer
, sizeof(buffer
)-1);
40 * Is it a symbolic ref?
42 if (len
< 4 || memcmp("ref:", buffer
, 4))
46 while (len
&& isspace(*buf
))
48 if (len
>= 5 && !memcmp("refs/", buf
, 5))
53 const char *resolve_ref(const char *path
, unsigned char *sha1
, int reading
)
55 int depth
= MAXDEPTH
, len
;
66 /* Special case: non-existing file.
67 * Not having the refs/heads/new-branch is OK
68 * if we are writing into it, so is .git/HEAD
69 * that points at refs/heads/master still to be
70 * born. It is NOT OK if we are resolving for
73 if (lstat(path
, &st
) < 0) {
74 if (reading
|| errno
!= ENOENT
)
80 /* Follow "normalized" - ie "refs/.." symlinks by hand */
81 if (S_ISLNK(st
.st_mode
)) {
82 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
83 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
84 path
= git_path("%.*s", len
, buffer
);
90 * Anything else, just open it and try to use it as
93 fd
= open(path
, O_RDONLY
);
96 len
= read(fd
, buffer
, sizeof(buffer
)-1);
100 * Is it a symbolic ref?
102 if (len
< 4 || memcmp("ref:", buffer
, 4))
106 while (len
&& isspace(*buf
))
108 while (len
&& isspace(buf
[len
-1]))
110 path
= git_path("%.*s", len
, buf
);
112 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
117 int create_symref(const char *git_HEAD
, const char *refs_heads_master
)
119 const char *lockpath
;
121 int fd
, len
, written
;
124 if (!only_use_symrefs
) {
126 if (!symlink(refs_heads_master
, git_HEAD
))
128 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
132 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
133 if (sizeof(ref
) <= len
) {
134 error("refname too long: %s", refs_heads_master
);
137 lockpath
= mkpath("%s.lock", git_HEAD
);
138 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
139 written
= write(fd
, ref
, len
);
141 if (written
!= len
) {
143 error("Unable to write to %s", lockpath
);
146 if (rename(lockpath
, git_HEAD
) < 0) {
148 error("Unable to create %s", git_HEAD
);
154 int read_ref(const char *filename
, unsigned char *sha1
)
156 if (resolve_ref(filename
, sha1
, 1))
161 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
))
164 DIR *dir
= opendir(git_path("%s", base
));
168 int baselen
= strlen(base
);
169 char *path
= xmalloc(baselen
+ 257);
171 if (!strncmp(base
, "./", 2)) {
175 memcpy(path
, base
, baselen
);
176 if (baselen
&& base
[baselen
-1] != '/')
177 path
[baselen
++] = '/';
179 while ((de
= readdir(dir
)) != NULL
) {
180 unsigned char sha1
[20];
184 if (de
->d_name
[0] == '.')
186 namelen
= strlen(de
->d_name
);
189 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
190 if (stat(git_path("%s", path
), &st
) < 0)
192 if (S_ISDIR(st
.st_mode
)) {
193 retval
= do_for_each_ref(path
, fn
);
198 if (read_ref(git_path("%s", path
), sha1
) < 0)
200 if (!has_sha1_file(sha1
))
202 retval
= fn(path
, sha1
);
212 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
214 unsigned char sha1
[20];
215 if (!read_ref(git_path("HEAD"), sha1
))
216 return fn("HEAD", sha1
);
220 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
222 return do_for_each_ref("refs", fn
);
225 static char *ref_file_name(const char *ref
)
227 char *base
= get_refs_directory();
228 int baselen
= strlen(base
);
229 int reflen
= strlen(ref
);
230 char *ret
= xmalloc(baselen
+ 2 + reflen
);
231 sprintf(ret
, "%s/%s", base
, ref
);
235 static char *ref_lock_file_name(const char *ref
)
237 char *base
= get_refs_directory();
238 int baselen
= strlen(base
);
239 int reflen
= strlen(ref
);
240 char *ret
= xmalloc(baselen
+ 7 + reflen
);
241 sprintf(ret
, "%s/%s.lock", base
, ref
);
245 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
247 const char *filename
;
249 if (check_ref_format(ref
))
251 filename
= git_path("refs/%s", ref
);
252 return read_ref(filename
, sha1
);
255 static int lock_ref_file(const char *filename
, const char *lock_filename
,
256 const unsigned char *old_sha1
)
258 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
259 unsigned char current_sha1
[20];
262 return error("Couldn't open lock file for %s: %s",
263 filename
, strerror(errno
));
265 retval
= read_ref(filename
, current_sha1
);
269 unlink(lock_filename
);
270 return error("Could not read the current value of %s",
273 if (memcmp(current_sha1
, old_sha1
, 20)) {
275 unlink(lock_filename
);
276 error("The current value of %s is %s",
277 filename
, sha1_to_hex(current_sha1
));
278 return error("Expected %s",
279 sha1_to_hex(old_sha1
));
284 unlink(lock_filename
);
285 return error("Unexpectedly found a value of %s for %s",
286 sha1_to_hex(current_sha1
), filename
);
292 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
297 if (check_ref_format(ref
))
299 filename
= ref_file_name(ref
);
300 lock_filename
= ref_lock_file_name(ref
);
301 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
307 static int write_ref_file(const char *filename
,
308 const char *lock_filename
, int fd
,
309 const unsigned char *sha1
)
311 char *hex
= sha1_to_hex(sha1
);
313 if (write(fd
, hex
, 40) < 40 ||
314 write(fd
, &term
, 1) < 1) {
315 error("Couldn't write %s\n", filename
);
320 rename(lock_filename
, filename
);
324 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
331 if (check_ref_format(ref
))
333 filename
= ref_file_name(ref
);
334 lock_filename
= ref_lock_file_name(ref
);
335 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
342 * Make sure "ref" is something reasonable to have under ".git/refs/";
343 * We do not like it if:
345 * - any path component of it begins with ".", or
346 * - it has double dots "..", or
347 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
348 * - it ends with a "/".
351 static inline int bad_ref_char(int ch
)
353 return (((unsigned) ch
) <= ' ' ||
354 ch
== '~' || ch
== '^' || ch
== ':');
357 int check_ref_format(const char *ref
)
360 const char *cp
= ref
;
364 while ((ch
= *cp
++) == '/')
365 ; /* tolerate duplicated slashes */
367 return -1; /* should not end with slashes */
369 /* we are at the beginning of the path component */
370 if (ch
== '.' || bad_ref_char(ch
))
373 /* scan the rest of the path component */
374 while ((ch
= *cp
++) != 0) {
375 if (bad_ref_char(ch
))
379 if (ch
== '.' && *cp
== '.')
385 return -1; /* at least of form "heads/blah" */
391 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
397 if (check_ref_format(ref
))
399 filename
= ref_file_name(ref
);
400 lock_filename
= ref_lock_file_name(ref
);
401 fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
403 error("Writing %s", lock_filename
);
406 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);