7 /* We allow "recursive" symbolic refs. Only within reason, though */
10 #ifndef USE_SYMLINK_HEAD
11 #define USE_SYMLINK_HEAD 1
14 int validate_symref(const char *path
)
17 char *buf
, buffer
[256];
20 if (lstat(path
, &st
) < 0)
23 /* Make sure it is a "refs/.." symlink */
24 if (S_ISLNK(st
.st_mode
)) {
25 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
26 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
32 * Anything else, just open it and try to see if it is a symbolic ref.
34 fd
= open(path
, O_RDONLY
);
37 len
= read(fd
, buffer
, sizeof(buffer
)-1);
41 * Is it a symbolic ref?
43 if (len
< 4 || memcmp("ref:", buffer
, 4))
47 while (len
&& isspace(*buf
))
49 if (len
>= 5 && !memcmp("refs/", buf
, 5))
54 const char *resolve_ref(const char *path
, unsigned char *sha1
, int reading
)
56 int depth
= MAXDEPTH
, len
;
67 /* Special case: non-existing file.
68 * Not having the refs/heads/new-branch is OK
69 * if we are writing into it, so is .git/HEAD
70 * that points at refs/heads/master still to be
71 * born. It is NOT OK if we are resolving for
74 if (lstat(path
, &st
) < 0) {
75 if (reading
|| errno
!= ENOENT
)
81 /* Follow "normalized" - ie "refs/.." symlinks by hand */
82 if (S_ISLNK(st
.st_mode
)) {
83 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
84 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
85 path
= git_path("%.*s", len
, buffer
);
91 * Anything else, just open it and try to use it as
94 fd
= open(path
, O_RDONLY
);
97 len
= read(fd
, buffer
, sizeof(buffer
)-1);
101 * Is it a symbolic ref?
103 if (len
< 4 || memcmp("ref:", buffer
, 4))
107 while (len
&& isspace(*buf
))
109 while (len
&& isspace(buf
[len
-1]))
111 path
= git_path("%.*s", len
, buf
);
113 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
118 int create_symref(const char *git_HEAD
, const char *refs_heads_master
)
122 return symlink(refs_heads_master
, git_HEAD
);
124 const char *lockpath
;
126 int fd
, len
, written
;
128 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
129 if (sizeof(ref
) <= len
) {
130 error("refname too long: %s", refs_heads_master
);
133 lockpath
= mkpath("%s.lock", git_HEAD
);
134 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
135 written
= write(fd
, ref
, len
);
137 if (written
!= len
) {
139 error("Unable to write to %s", lockpath
);
142 if (rename(lockpath
, git_HEAD
) < 0) {
144 error("Unable to create %s", git_HEAD
);
151 int read_ref(const char *filename
, unsigned char *sha1
)
153 if (resolve_ref(filename
, sha1
, 1))
158 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
))
161 DIR *dir
= opendir(git_path("%s", base
));
165 int baselen
= strlen(base
);
166 char *path
= xmalloc(baselen
+ 257);
168 if (!strncmp(base
, "./", 2)) {
172 memcpy(path
, base
, baselen
);
173 if (baselen
&& base
[baselen
-1] != '/')
174 path
[baselen
++] = '/';
176 while ((de
= readdir(dir
)) != NULL
) {
177 unsigned char sha1
[20];
181 if (de
->d_name
[0] == '.')
183 namelen
= strlen(de
->d_name
);
186 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
187 if (stat(git_path("%s", path
), &st
) < 0)
189 if (S_ISDIR(st
.st_mode
)) {
190 retval
= do_for_each_ref(path
, fn
);
195 if (read_ref(git_path("%s", path
), sha1
) < 0)
197 if (!has_sha1_file(sha1
))
199 retval
= fn(path
, sha1
);
209 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
211 unsigned char sha1
[20];
212 if (!read_ref(git_path("HEAD"), sha1
))
213 return fn("HEAD", sha1
);
217 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
219 return do_for_each_ref("refs", fn
);
222 static char *ref_file_name(const char *ref
)
224 char *base
= get_refs_directory();
225 int baselen
= strlen(base
);
226 int reflen
= strlen(ref
);
227 char *ret
= xmalloc(baselen
+ 2 + reflen
);
228 sprintf(ret
, "%s/%s", base
, ref
);
232 static char *ref_lock_file_name(const char *ref
)
234 char *base
= get_refs_directory();
235 int baselen
= strlen(base
);
236 int reflen
= strlen(ref
);
237 char *ret
= xmalloc(baselen
+ 7 + reflen
);
238 sprintf(ret
, "%s/%s.lock", base
, ref
);
242 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
244 const char *filename
;
246 if (check_ref_format(ref
))
248 filename
= git_path("refs/%s", ref
);
249 return read_ref(filename
, sha1
);
252 static int lock_ref_file(const char *filename
, const char *lock_filename
,
253 const unsigned char *old_sha1
)
255 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
256 unsigned char current_sha1
[20];
259 return error("Couldn't open lock file for %s: %s",
260 filename
, strerror(errno
));
262 retval
= read_ref(filename
, current_sha1
);
266 unlink(lock_filename
);
267 return error("Could not read the current value of %s",
270 if (memcmp(current_sha1
, old_sha1
, 20)) {
272 unlink(lock_filename
);
273 error("The current value of %s is %s",
274 filename
, sha1_to_hex(current_sha1
));
275 return error("Expected %s",
276 sha1_to_hex(old_sha1
));
281 unlink(lock_filename
);
282 return error("Unexpectedly found a value of %s for %s",
283 sha1_to_hex(current_sha1
), filename
);
289 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
294 if (check_ref_format(ref
))
296 filename
= ref_file_name(ref
);
297 lock_filename
= ref_lock_file_name(ref
);
298 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
304 static int write_ref_file(const char *filename
,
305 const char *lock_filename
, int fd
,
306 const unsigned char *sha1
)
308 char *hex
= sha1_to_hex(sha1
);
310 if (write(fd
, hex
, 40) < 40 ||
311 write(fd
, &term
, 1) < 1) {
312 error("Couldn't write %s\n", filename
);
317 rename(lock_filename
, filename
);
321 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
328 if (check_ref_format(ref
))
330 filename
= ref_file_name(ref
);
331 lock_filename
= ref_lock_file_name(ref
);
332 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
338 int check_ref_format(const char *ref
)
341 if (ref
[0] == '.' || ref
[0] == '/')
343 middle
= strchr(ref
, '/');
344 if (!middle
|| !middle
[1])
346 if (strchr(middle
+ 1, '/'))
351 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
357 if (check_ref_format(ref
))
359 filename
= ref_file_name(ref
);
360 lock_filename
= ref_lock_file_name(ref
);
361 fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
363 error("Writing %s", lock_filename
);
366 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);