7 /* We allow "recursive" symbolic refs. Only within reason, though */
10 const char *resolve_ref(const char *path
, unsigned char *sha1
, int reading
)
12 int depth
= MAXDEPTH
, len
;
23 /* Special case: non-existing file.
24 * Not having the refs/heads/new-branch is OK
25 * if we are writing into it, so is .git/HEAD
26 * that points at refs/heads/master still to be
27 * born. It is NOT OK if we are resolving for
30 if (lstat(path
, &st
) < 0) {
31 if (reading
|| errno
!= ENOENT
)
37 /* Follow "normalized" - ie "refs/.." symlinks by hand */
38 if (S_ISLNK(st
.st_mode
)) {
39 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
40 if (len
>= 5 && !memcmp("refs/", buffer
, 5)) {
41 path
= git_path("%.*s", len
, buffer
);
47 * Anything else, just open it and try to use it as
50 fd
= open(path
, O_RDONLY
);
53 len
= read(fd
, buffer
, sizeof(buffer
)-1);
57 * Is it a symbolic ref?
59 if (len
< 4 || memcmp("ref:", buffer
, 4))
63 while (len
&& isspace(*buf
))
65 while (len
&& isspace(buf
[len
-1]))
67 path
= git_path("%.*s", len
, buf
);
69 if (len
< 40 || get_sha1_hex(buffer
, sha1
))
74 int read_ref(const char *filename
, unsigned char *sha1
)
76 if (resolve_ref(filename
, sha1
, 1))
81 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
))
84 DIR *dir
= opendir(git_path("%s", base
));
88 int baselen
= strlen(base
);
89 char *path
= xmalloc(baselen
+ 257);
91 if (!strncmp(base
, "./", 2)) {
95 memcpy(path
, base
, baselen
);
96 if (baselen
&& base
[baselen
-1] != '/')
97 path
[baselen
++] = '/';
99 while ((de
= readdir(dir
)) != NULL
) {
100 unsigned char sha1
[20];
104 if (de
->d_name
[0] == '.')
106 namelen
= strlen(de
->d_name
);
109 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
110 if (stat(git_path("%s", path
), &st
) < 0)
112 if (S_ISDIR(st
.st_mode
)) {
113 retval
= do_for_each_ref(path
, fn
);
118 if (read_ref(git_path("%s", path
), sha1
) < 0)
120 if (!has_sha1_file(sha1
))
122 retval
= fn(path
, sha1
);
132 int head_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
134 unsigned char sha1
[20];
135 if (!read_ref(git_path("HEAD"), sha1
))
136 return fn("HEAD", sha1
);
140 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
142 return do_for_each_ref("refs", fn
);
145 static char *ref_file_name(const char *ref
)
147 char *base
= get_refs_directory();
148 int baselen
= strlen(base
);
149 int reflen
= strlen(ref
);
150 char *ret
= xmalloc(baselen
+ 2 + reflen
);
151 sprintf(ret
, "%s/%s", base
, ref
);
155 static char *ref_lock_file_name(const char *ref
)
157 char *base
= get_refs_directory();
158 int baselen
= strlen(base
);
159 int reflen
= strlen(ref
);
160 char *ret
= xmalloc(baselen
+ 7 + reflen
);
161 sprintf(ret
, "%s/%s.lock", base
, ref
);
165 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
167 const char *filename
;
169 if (check_ref_format(ref
))
171 filename
= git_path("refs/%s", ref
);
172 return read_ref(filename
, sha1
);
175 static int lock_ref_file(const char *filename
, const char *lock_filename
,
176 const unsigned char *old_sha1
)
178 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
179 unsigned char current_sha1
[20];
182 return error("Couldn't open lock file for %s: %s",
183 filename
, strerror(errno
));
185 retval
= read_ref(filename
, current_sha1
);
189 unlink(lock_filename
);
190 return error("Could not read the current value of %s",
193 if (memcmp(current_sha1
, old_sha1
, 20)) {
195 unlink(lock_filename
);
196 error("The current value of %s is %s",
197 filename
, sha1_to_hex(current_sha1
));
198 return error("Expected %s",
199 sha1_to_hex(old_sha1
));
204 unlink(lock_filename
);
205 return error("Unexpectedly found a value of %s for %s",
206 sha1_to_hex(current_sha1
), filename
);
212 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
217 if (check_ref_format(ref
))
219 filename
= ref_file_name(ref
);
220 lock_filename
= ref_lock_file_name(ref
);
221 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
227 static int write_ref_file(const char *filename
,
228 const char *lock_filename
, int fd
,
229 const unsigned char *sha1
)
231 char *hex
= sha1_to_hex(sha1
);
233 if (write(fd
, hex
, 40) < 40 ||
234 write(fd
, &term
, 1) < 1) {
235 error("Couldn't write %s\n", filename
);
240 rename(lock_filename
, filename
);
244 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
251 if (check_ref_format(ref
))
253 filename
= ref_file_name(ref
);
254 lock_filename
= ref_lock_file_name(ref
);
255 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
261 int check_ref_format(const char *ref
)
264 if (ref
[0] == '.' || ref
[0] == '/')
266 middle
= strchr(ref
, '/');
267 if (!middle
|| !middle
[1])
269 if (strchr(middle
+ 1, '/'))
274 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
280 if (check_ref_format(ref
))
282 filename
= ref_file_name(ref
);
283 lock_filename
= ref_lock_file_name(ref
);
284 fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
286 error("Writing %s", lock_filename
);
289 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);