Make "for_each_ref()" always use the "canonical" refname.
[git] / refs.c
1 #include "refs.h"
2 #include "cache.h"
3
4 #include <errno.h>
5
6 static int read_ref(const char *refname, unsigned char *sha1)
7 {
8         int ret = -1;
9         int fd = open(git_path(refname), O_RDONLY);
10
11         if (fd >= 0) {
12                 char buffer[60];
13                 if (read(fd, buffer, sizeof(buffer)) >= 40)
14                         ret = get_sha1_hex(buffer, sha1);
15                 close(fd);
16         }
17         return ret;
18 }
19
20 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
21 {
22         int retval = 0;
23         DIR *dir = opendir(git_path(base));
24
25         if (dir) {
26                 struct dirent *de;
27                 int baselen = strlen(base);
28                 char *path = xmalloc(baselen + 257);
29
30                 if (!strncmp(base, "./", 2)) {
31                         base += 2;
32                         baselen -= 2;
33                 }
34                 memcpy(path, base, baselen);
35                 if (baselen && base[baselen-1] != '/')
36                         path[baselen++] = '/';
37
38                 while ((de = readdir(dir)) != NULL) {
39                         unsigned char sha1[20];
40                         struct stat st;
41                         int namelen;
42
43                         if (de->d_name[0] == '.')
44                                 continue;
45                         namelen = strlen(de->d_name);
46                         if (namelen > 255)
47                                 continue;
48                         memcpy(path + baselen, de->d_name, namelen+1);
49                         if (lstat(git_path(path), &st) < 0)
50                                 continue;
51                         if (S_ISDIR(st.st_mode)) {
52                                 retval = do_for_each_ref(path, fn);
53                                 if (retval)
54                                         break;
55                                 continue;
56                         }
57                         if (read_ref(path, sha1) < 0)
58                                 continue;
59                         if (!has_sha1_file(sha1))
60                                 continue;
61                         retval = fn(path, sha1);
62                         if (retval)
63                                 break;
64                 }
65                 free(path);
66                 closedir(dir);
67         }
68         return retval;
69 }
70
71 int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
72 {
73         unsigned char sha1[20];
74         if (!read_ref("HEAD", sha1))
75                 return fn("HEAD", sha1);
76         return 0;
77 }
78
79 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
80 {
81         return do_for_each_ref("refs", fn);
82 }
83
84 static char *ref_file_name(const char *ref)
85 {
86         char *base = get_refs_directory();
87         int baselen = strlen(base);
88         int reflen = strlen(ref);
89         char *ret = xmalloc(baselen + 2 + reflen);
90         sprintf(ret, "%s/%s", base, ref);
91         return ret;
92 }
93
94 static char *ref_lock_file_name(const char *ref)
95 {
96         char *base = get_refs_directory();
97         int baselen = strlen(base);
98         int reflen = strlen(ref);
99         char *ret = xmalloc(baselen + 7 + reflen);
100         sprintf(ret, "%s/%s.lock", base, ref);
101         return ret;
102 }
103
104 static int read_ref_file(const char *filename, unsigned char *sha1) {
105         int fd = open(filename, O_RDONLY);
106         char hex[41];
107         if (fd < 0) {
108                 return error("Couldn't open %s\n", filename);
109         }
110         if ((read(fd, hex, 41) < 41) ||
111             (hex[40] != '\n') ||
112             get_sha1_hex(hex, sha1)) {
113                 error("Couldn't read a hash from %s\n", filename);
114                 close(fd);
115                 return -1;
116         }
117         close(fd);
118         return 0;
119 }
120
121 int get_ref_sha1(const char *ref, unsigned char *sha1)
122 {
123         char *filename;
124         int retval;
125         if (check_ref_format(ref))
126                 return -1;
127         filename = ref_file_name(ref);
128         retval = read_ref_file(filename, sha1);
129         free(filename);
130         return retval;
131 }
132
133 static int lock_ref_file(const char *filename, const char *lock_filename,
134                          const unsigned char *old_sha1)
135 {
136         int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
137         unsigned char current_sha1[20];
138         int retval;
139         if (fd < 0) {
140                 return error("Couldn't open lock file for %s: %s",
141                              filename, strerror(errno));
142         }
143         retval = read_ref_file(filename, current_sha1);
144         if (old_sha1) {
145                 if (retval) {
146                         close(fd);
147                         unlink(lock_filename);
148                         return error("Could not read the current value of %s",
149                                      filename);
150                 }
151                 if (memcmp(current_sha1, old_sha1, 20)) {
152                         close(fd);
153                         unlink(lock_filename);
154                         error("The current value of %s is %s",
155                               filename, sha1_to_hex(current_sha1));
156                         return error("Expected %s",
157                                      sha1_to_hex(old_sha1));
158                 }
159         } else {
160                 if (!retval) {
161                         close(fd);
162                         unlink(lock_filename);
163                         return error("Unexpectedly found a value of %s for %s",
164                                      sha1_to_hex(current_sha1), filename);
165                 }
166         }
167         return fd;
168 }
169
170 int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
171 {
172         char *filename;
173         char *lock_filename;
174         int retval;
175         if (check_ref_format(ref))
176                 return -1;
177         filename = ref_file_name(ref);
178         lock_filename = ref_lock_file_name(ref);
179         retval = lock_ref_file(filename, lock_filename, old_sha1);
180         free(filename);
181         free(lock_filename);
182         return retval;
183 }
184
185 static int write_ref_file(const char *filename,
186                           const char *lock_filename, int fd,
187                           const unsigned char *sha1)
188 {
189         char *hex = sha1_to_hex(sha1);
190         char term = '\n';
191         if (write(fd, hex, 40) < 40 ||
192             write(fd, &term, 1) < 1) {
193                 error("Couldn't write %s\n", filename);
194                 close(fd);
195                 return -1;
196         }
197         close(fd);
198         rename(lock_filename, filename);
199         return 0;
200 }
201
202 int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
203 {
204         char *filename;
205         char *lock_filename;
206         int retval;
207         if (fd < 0)
208                 return -1;
209         if (check_ref_format(ref))
210                 return -1;
211         filename = ref_file_name(ref);
212         lock_filename = ref_lock_file_name(ref);
213         retval = write_ref_file(filename, lock_filename, fd, sha1);
214         free(filename);
215         free(lock_filename);
216         return retval;
217 }
218
219 int check_ref_format(const char *ref)
220 {
221         char *middle;
222         if (ref[0] == '.' || ref[0] == '/')
223                 return -1;
224         middle = strchr(ref, '/');
225         if (!middle || !middle[1])
226                 return -1;
227         if (strchr(middle + 1, '/'))
228                 return -1;
229         return 0;
230 }
231
232 int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
233 {
234         char *filename;
235         char *lock_filename;
236         int fd;
237         int retval;
238         if (check_ref_format(ref))
239                 return -1;
240         filename = ref_file_name(ref);
241         lock_filename = ref_lock_file_name(ref);
242         fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
243         if (fd < 0) {
244                 error("Writing %s", lock_filename);
245                 perror("Open");
246         }
247         retval = write_ref_file(filename, lock_filename, fd, sha1);
248         free(filename);
249         free(lock_filename);
250         return retval;
251 }