Merge branch 'fixes'
[git/jnareb-git.git] / refs.c
blobd7f8dfddf4979c0dda2b888435e6c343b29409ad
1 #include "refs.h"
2 #include "cache.h"
4 #include <errno.h>
5 #include <ctype.h>
7 /* We allow "recursive" symbolic refs. Only within reason, though */
8 #define MAXDEPTH 5
10 #ifndef USE_SYMLINK_HEAD
11 #define USE_SYMLINK_HEAD 1
12 #endif
14 int validate_symref(const char *path)
16 struct stat st;
17 char *buf, buffer[256];
18 int len, fd;
20 if (lstat(path, &st) < 0)
21 return -1;
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))
27 return 0;
28 return -1;
32 * Anything else, just open it and try to see if it is a symbolic ref.
34 fd = open(path, O_RDONLY);
35 if (fd < 0)
36 return -1;
37 len = read(fd, buffer, sizeof(buffer)-1);
38 close(fd);
41 * Is it a symbolic ref?
43 if (len < 4 || memcmp("ref:", buffer, 4))
44 return -1;
45 buf = buffer + 4;
46 len -= 4;
47 while (len && isspace(*buf))
48 buf++, len--;
49 if (len >= 5 && !memcmp("refs/", buf, 5))
50 return 0;
51 return -1;
54 const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
56 int depth = MAXDEPTH, len;
57 char buffer[256];
59 for (;;) {
60 struct stat st;
61 char *buf;
62 int fd;
64 if (--depth < 0)
65 return NULL;
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
72 * reading.
74 if (lstat(path, &st) < 0) {
75 if (reading || errno != ENOENT)
76 return NULL;
77 memset(sha1, 0, 20);
78 return path;
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);
86 continue;
91 * Anything else, just open it and try to use it as
92 * a ref
94 fd = open(path, O_RDONLY);
95 if (fd < 0)
96 return NULL;
97 len = read(fd, buffer, sizeof(buffer)-1);
98 close(fd);
101 * Is it a symbolic ref?
103 if (len < 4 || memcmp("ref:", buffer, 4))
104 break;
105 buf = buffer + 4;
106 len -= 4;
107 while (len && isspace(*buf))
108 buf++, len--;
109 while (len && isspace(buf[len-1]))
110 buf[--len] = 0;
111 path = git_path("%.*s", len, buf);
113 if (len < 40 || get_sha1_hex(buffer, sha1))
114 return NULL;
115 return path;
118 int create_symref(const char *git_HEAD, const char *refs_heads_master)
120 #if USE_SYMLINK_HEAD
121 unlink(git_HEAD);
122 return symlink(refs_heads_master, git_HEAD);
123 #else
124 const char *lockpath;
125 char ref[1000];
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);
131 return -1;
133 lockpath = mkpath("%s.lock", git_HEAD);
134 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
135 written = write(fd, ref, len);
136 close(fd);
137 if (written != len) {
138 unlink(lockpath);
139 error("Unable to write to %s", lockpath);
140 return -2;
142 if (rename(lockpath, git_HEAD) < 0) {
143 unlink(lockpath);
144 error("Unable to create %s", git_HEAD);
145 return -3;
147 return 0;
148 #endif
151 int read_ref(const char *filename, unsigned char *sha1)
153 if (resolve_ref(filename, sha1, 1))
154 return 0;
155 return -1;
158 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
160 int retval = 0;
161 DIR *dir = opendir(git_path("%s", base));
163 if (dir) {
164 struct dirent *de;
165 int baselen = strlen(base);
166 char *path = xmalloc(baselen + 257);
168 if (!strncmp(base, "./", 2)) {
169 base += 2;
170 baselen -= 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];
178 struct stat st;
179 int namelen;
181 if (de->d_name[0] == '.')
182 continue;
183 namelen = strlen(de->d_name);
184 if (namelen > 255)
185 continue;
186 memcpy(path + baselen, de->d_name, namelen+1);
187 if (stat(git_path("%s", path), &st) < 0)
188 continue;
189 if (S_ISDIR(st.st_mode)) {
190 retval = do_for_each_ref(path, fn);
191 if (retval)
192 break;
193 continue;
195 if (read_ref(git_path("%s", path), sha1) < 0)
196 continue;
197 if (!has_sha1_file(sha1))
198 continue;
199 retval = fn(path, sha1);
200 if (retval)
201 break;
203 free(path);
204 closedir(dir);
206 return retval;
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);
214 return 0;
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);
229 return ret;
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);
239 return ret;
242 int get_ref_sha1(const char *ref, unsigned char *sha1)
244 const char *filename;
246 if (check_ref_format(ref))
247 return -1;
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];
257 int retval;
258 if (fd < 0) {
259 return error("Couldn't open lock file for %s: %s",
260 filename, strerror(errno));
262 retval = read_ref(filename, current_sha1);
263 if (old_sha1) {
264 if (retval) {
265 close(fd);
266 unlink(lock_filename);
267 return error("Could not read the current value of %s",
268 filename);
270 if (memcmp(current_sha1, old_sha1, 20)) {
271 close(fd);
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));
278 } else {
279 if (!retval) {
280 close(fd);
281 unlink(lock_filename);
282 return error("Unexpectedly found a value of %s for %s",
283 sha1_to_hex(current_sha1), filename);
286 return fd;
289 int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
291 char *filename;
292 char *lock_filename;
293 int retval;
294 if (check_ref_format(ref))
295 return -1;
296 filename = ref_file_name(ref);
297 lock_filename = ref_lock_file_name(ref);
298 retval = lock_ref_file(filename, lock_filename, old_sha1);
299 free(filename);
300 free(lock_filename);
301 return retval;
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);
309 char term = '\n';
310 if (write(fd, hex, 40) < 40 ||
311 write(fd, &term, 1) < 1) {
312 error("Couldn't write %s\n", filename);
313 close(fd);
314 return -1;
316 close(fd);
317 rename(lock_filename, filename);
318 return 0;
321 int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
323 char *filename;
324 char *lock_filename;
325 int retval;
326 if (fd < 0)
327 return -1;
328 if (check_ref_format(ref))
329 return -1;
330 filename = ref_file_name(ref);
331 lock_filename = ref_lock_file_name(ref);
332 retval = write_ref_file(filename, lock_filename, fd, sha1);
333 free(filename);
334 free(lock_filename);
335 return retval;
339 * Make sure "ref" is something reasonable to have under ".git/refs/";
340 * We do not like it if:
342 * - any path component of it begins with ".", or
343 * - it has double dots "..", or
344 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
345 * - it ends with a "/".
348 static inline int bad_ref_char(int ch)
350 return (((unsigned) ch) <= ' ' ||
351 ch == '~' || ch == '^' || ch == ':');
354 int check_ref_format(const char *ref)
356 int ch, level;
357 const char *cp = ref;
359 level = 0;
360 while (1) {
361 while ((ch = *cp++) == '/')
362 ; /* tolerate duplicated slashes */
363 if (!ch)
364 return -1; /* should not end with slashes */
366 /* we are at the beginning of the path component */
367 if (ch == '.' || bad_ref_char(ch))
368 return -1;
370 /* scan the rest of the path component */
371 while ((ch = *cp++) != 0) {
372 if (bad_ref_char(ch))
373 return -1;
374 if (ch == '/')
375 break;
376 if (ch == '.' && *cp == '.')
377 return -1;
379 level++;
380 if (!ch) {
381 if (level < 2)
382 return -1; /* at least of form "heads/blah" */
383 return 0;
388 int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
390 char *filename;
391 char *lock_filename;
392 int fd;
393 int retval;
394 if (check_ref_format(ref))
395 return -1;
396 filename = ref_file_name(ref);
397 lock_filename = ref_lock_file_name(ref);
398 fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
399 if (fd < 0) {
400 error("Writing %s", lock_filename);
401 perror("Open");
403 retval = write_ref_file(filename, lock_filename, fd, sha1);
404 free(filename);
405 free(lock_filename);
406 return retval;