git-log (internal): add approxidate.
[git] / refs.c
1 #include "refs.h"
2 #include "cache.h"
3
4 #include <errno.h>
5
6 /* We allow "recursive" symbolic refs. Only within reason, though */
7 #define MAXDEPTH 5
8
9 const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
10 {
11         int depth = MAXDEPTH, len;
12         char buffer[256];
13
14         for (;;) {
15                 struct stat st;
16                 char *buf;
17                 int fd;
18
19                 if (--depth < 0)
20                         return NULL;
21
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
27                  * reading.
28                  */
29                 if (lstat(path, &st) < 0) {
30                         if (reading || errno != ENOENT)
31                                 return NULL;
32                         memset(sha1, 0, 20);
33                         return path;
34                 }
35
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);
41                                 continue;
42                         }
43                 }
44
45                 /*
46                  * Anything else, just open it and try to use it as
47                  * a ref
48                  */
49                 fd = open(path, O_RDONLY);
50                 if (fd < 0)
51                         return NULL;
52                 len = read(fd, buffer, sizeof(buffer)-1);
53                 close(fd);
54
55                 /*
56                  * Is it a symbolic ref?
57                  */
58                 if (len < 4 || memcmp("ref:", buffer, 4))
59                         break;
60                 buf = buffer + 4;
61                 len -= 4;
62                 while (len && isspace(*buf))
63                         buf++, len--;
64                 while (len && isspace(buf[len-1]))
65                         buf[--len] = 0;
66                 path = git_path("%.*s", len, buf);
67         }
68         if (len < 40 || get_sha1_hex(buffer, sha1))
69                 return NULL;
70         return path;
71 }
72
73 int create_symref(const char *git_HEAD, const char *refs_heads_master)
74 {
75         const char *lockpath;
76         char ref[1000];
77         int fd, len, written;
78
79 #ifdef USE_SYMLINK_HEAD
80         if (!only_use_symrefs) {
81                 unlink(git_HEAD);
82                 if (!symlink(refs_heads_master, git_HEAD))
83                         return 0;
84                 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
85         }
86 #endif
87
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);
91                 return -1;
92         }
93         lockpath = mkpath("%s.lock", git_HEAD);
94         fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); 
95         written = write(fd, ref, len);
96         close(fd);
97         if (written != len) {
98                 unlink(lockpath);
99                 error("Unable to write to %s", lockpath);
100                 return -2;
101         }
102         if (rename(lockpath, git_HEAD) < 0) {
103                 unlink(lockpath);
104                 error("Unable to create %s", git_HEAD);
105                 return -3;
106         }
107         return 0;
108 }
109
110 int read_ref(const char *filename, unsigned char *sha1)
111 {
112         if (resolve_ref(filename, sha1, 1))
113                 return 0;
114         return -1;
115 }
116
117 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
118 {
119         int retval = 0;
120         DIR *dir = opendir(git_path("%s", base));
121
122         if (dir) {
123                 struct dirent *de;
124                 int baselen = strlen(base);
125                 char *path = xmalloc(baselen + 257);
126
127                 if (!strncmp(base, "./", 2)) {
128                         base += 2;
129                         baselen -= 2;
130                 }
131                 memcpy(path, base, baselen);
132                 if (baselen && base[baselen-1] != '/')
133                         path[baselen++] = '/';
134
135                 while ((de = readdir(dir)) != NULL) {
136                         unsigned char sha1[20];
137                         struct stat st;
138                         int namelen;
139
140                         if (de->d_name[0] == '.')
141                                 continue;
142                         namelen = strlen(de->d_name);
143                         if (namelen > 255)
144                                 continue;
145                         memcpy(path + baselen, de->d_name, namelen+1);
146                         if (stat(git_path("%s", path), &st) < 0)
147                                 continue;
148                         if (S_ISDIR(st.st_mode)) {
149                                 retval = do_for_each_ref(path, fn);
150                                 if (retval)
151                                         break;
152                                 continue;
153                         }
154                         if (read_ref(git_path("%s", path), sha1) < 0)
155                                 continue;
156                         if (!has_sha1_file(sha1))
157                                 continue;
158                         retval = fn(path, sha1);
159                         if (retval)
160                                 break;
161                 }
162                 free(path);
163                 closedir(dir);
164         }
165         return retval;
166 }
167
168 int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
169 {
170         unsigned char sha1[20];
171         if (!read_ref(git_path("HEAD"), sha1))
172                 return fn("HEAD", sha1);
173         return 0;
174 }
175
176 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
177 {
178         return do_for_each_ref("refs", fn);
179 }
180
181 static char *ref_file_name(const char *ref)
182 {
183         char *base = get_refs_directory();
184         int baselen = strlen(base);
185         int reflen = strlen(ref);
186         char *ret = xmalloc(baselen + 2 + reflen);
187         sprintf(ret, "%s/%s", base, ref);
188         return ret;
189 }
190
191 static char *ref_lock_file_name(const char *ref)
192 {
193         char *base = get_refs_directory();
194         int baselen = strlen(base);
195         int reflen = strlen(ref);
196         char *ret = xmalloc(baselen + 7 + reflen);
197         sprintf(ret, "%s/%s.lock", base, ref);
198         return ret;
199 }
200
201 int get_ref_sha1(const char *ref, unsigned char *sha1)
202 {
203         const char *filename;
204
205         if (check_ref_format(ref))
206                 return -1;
207         filename = git_path("refs/%s", ref);
208         return read_ref(filename, sha1);
209 }
210
211 static int lock_ref_file(const char *filename, const char *lock_filename,
212                          const unsigned char *old_sha1)
213 {
214         int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
215         unsigned char current_sha1[20];
216         int retval;
217         if (fd < 0) {
218                 return error("Couldn't open lock file for %s: %s",
219                              filename, strerror(errno));
220         }
221         retval = read_ref(filename, current_sha1);
222         if (old_sha1) {
223                 if (retval) {
224                         close(fd);
225                         unlink(lock_filename);
226                         return error("Could not read the current value of %s",
227                                      filename);
228                 }
229                 if (memcmp(current_sha1, old_sha1, 20)) {
230                         close(fd);
231                         unlink(lock_filename);
232                         error("The current value of %s is %s",
233                               filename, sha1_to_hex(current_sha1));
234                         return error("Expected %s",
235                                      sha1_to_hex(old_sha1));
236                 }
237         } else {
238                 if (!retval) {
239                         close(fd);
240                         unlink(lock_filename);
241                         return error("Unexpectedly found a value of %s for %s",
242                                      sha1_to_hex(current_sha1), filename);
243                 }
244         }
245         return fd;
246 }
247
248 int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
249 {
250         char *filename;
251         char *lock_filename;
252         int retval;
253         if (check_ref_format(ref))
254                 return -1;
255         filename = ref_file_name(ref);
256         lock_filename = ref_lock_file_name(ref);
257         retval = lock_ref_file(filename, lock_filename, old_sha1);
258         free(filename);
259         free(lock_filename);
260         return retval;
261 }
262
263 static int write_ref_file(const char *filename,
264                           const char *lock_filename, int fd,
265                           const unsigned char *sha1)
266 {
267         char *hex = sha1_to_hex(sha1);
268         char term = '\n';
269         if (write(fd, hex, 40) < 40 ||
270             write(fd, &term, 1) < 1) {
271                 error("Couldn't write %s", filename);
272                 close(fd);
273                 return -1;
274         }
275         close(fd);
276         rename(lock_filename, filename);
277         return 0;
278 }
279
280 int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
281 {
282         char *filename;
283         char *lock_filename;
284         int retval;
285         if (fd < 0)
286                 return -1;
287         if (check_ref_format(ref))
288                 return -1;
289         filename = ref_file_name(ref);
290         lock_filename = ref_lock_file_name(ref);
291         if (safe_create_leading_directories(filename))
292                 die("unable to create leading directory for %s", filename);
293         retval = write_ref_file(filename, lock_filename, fd, sha1);
294         free(filename);
295         free(lock_filename);
296         return retval;
297 }
298
299 /*
300  * Make sure "ref" is something reasonable to have under ".git/refs/";
301  * We do not like it if:
302  *
303  * - any path component of it begins with ".", or
304  * - it has double dots "..", or
305  * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
306  * - it ends with a "/".
307  */
308
309 static inline int bad_ref_char(int ch)
310 {
311         return (((unsigned) ch) <= ' ' ||
312                 ch == '~' || ch == '^' || ch == ':' ||
313                 /* 2.13 Pattern Matching Notation */
314                 ch == '?' || ch == '*' || ch == '[');
315 }
316
317 int check_ref_format(const char *ref)
318 {
319         int ch, level;
320         const char *cp = ref;
321
322         level = 0;
323         while (1) {
324                 while ((ch = *cp++) == '/')
325                         ; /* tolerate duplicated slashes */
326                 if (!ch)
327                         return -1; /* should not end with slashes */
328
329                 /* we are at the beginning of the path component */
330                 if (ch == '.' || bad_ref_char(ch))
331                         return -1;
332
333                 /* scan the rest of the path component */
334                 while ((ch = *cp++) != 0) {
335                         if (bad_ref_char(ch))
336                                 return -1;
337                         if (ch == '/')
338                                 break;
339                         if (ch == '.' && *cp == '.')
340                                 return -1;
341                 }
342                 level++;
343                 if (!ch) {
344                         if (level < 2)
345                                 return -1; /* at least of form "heads/blah" */
346                         return 0;
347                 }
348         }
349 }
350
351 int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
352 {
353         char *filename;
354         char *lock_filename;
355         int fd;
356         int retval;
357         if (check_ref_format(ref))
358                 return -1;
359         filename = ref_file_name(ref);
360         lock_filename = ref_lock_file_name(ref);
361         if (safe_create_leading_directories(filename))
362                 die("unable to create leading directory for %s", filename);
363         fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
364         if (fd < 0) {
365                 error("Writing %s", lock_filename);
366                 perror("Open");
367         }
368         retval = write_ref_file(filename, lock_filename, fd, sha1);
369         free(filename);
370         free(lock_filename);
371         return retval;
372 }