[PATCH] Add "git grep" helper
[git/jnareb-git/bp-gitweb.git] / refs.c
blob161018097def05f717ae20e59e3df6cc1b01ab77
1 #include "refs.h"
2 #include "cache.h"
4 #include <errno.h>
6 static int read_ref(const char *refname, unsigned char *sha1)
8 int ret = -1;
9 int fd = open(git_path("%s", refname), O_RDONLY);
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);
17 return ret;
20 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
22 int retval = 0;
23 DIR *dir = opendir(git_path("%s", base));
25 if (dir) {
26 struct dirent *de;
27 int baselen = strlen(base);
28 char *path = xmalloc(baselen + 257);
30 if (!strncmp(base, "./", 2)) {
31 base += 2;
32 baselen -= 2;
34 memcpy(path, base, baselen);
35 if (baselen && base[baselen-1] != '/')
36 path[baselen++] = '/';
38 while ((de = readdir(dir)) != NULL) {
39 unsigned char sha1[20];
40 struct stat st;
41 int namelen;
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 (stat(git_path("%s", 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;
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;
65 free(path);
66 closedir(dir);
68 return retval;
71 int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
73 unsigned char sha1[20];
74 if (!read_ref("HEAD", sha1))
75 return fn("HEAD", sha1);
76 return 0;
79 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
81 return do_for_each_ref("refs", fn);
84 static char *ref_file_name(const char *ref)
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;
94 static char *ref_lock_file_name(const char *ref)
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;
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);
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;
117 close(fd);
118 return 0;
121 int get_ref_sha1(const char *ref, unsigned char *sha1)
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;
133 static int lock_ref_file(const char *filename, const char *lock_filename,
134 const unsigned char *old_sha1)
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));
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);
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));
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);
167 return fd;
170 int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
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;
185 static int write_ref_file(const char *filename,
186 const char *lock_filename, int fd,
187 const unsigned char *sha1)
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;
197 close(fd);
198 rename(lock_filename, filename);
199 return 0;
202 int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
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;
219 int check_ref_format(const char *ref)
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;
232 int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
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");
247 retval = write_ref_file(filename, lock_filename, fd, sha1);
248 free(filename);
249 free(lock_filename);
250 return retval;