7 /* We allow "recursive" symbolic refs. Only within reason, though */
10 int read_ref(const char *filename, unsigned char *sha1)
15 while ((fd = open(filename, O_RDONLY)) >= 0) {
17 int len = read(fd, buffer, sizeof(buffer)-1);
24 while (len && isspace(buffer[len-1]))
27 if (!strncmp(buffer, "ref:", 4)) {
34 while (len && isspace(*buf))
36 filename = git_path("%.*s", len, buf);
40 ret = get_sha1_hex(buffer, sha1);
46 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
49 DIR *dir = opendir(git_path("%s", base));
53 int baselen = strlen(base);
54 char *path = xmalloc(baselen + 257);
56 if (!strncmp(base, "./", 2)) {
60 memcpy(path, base, baselen);
61 if (baselen && base[baselen-1] != '/')
62 path[baselen++] = '/';
64 while ((de = readdir(dir)) != NULL) {
65 unsigned char sha1[20];
69 if (de->d_name[0] == '.')
71 namelen = strlen(de->d_name);
74 memcpy(path + baselen, de->d_name, namelen+1);
75 if (stat(git_path("%s", path), &st) < 0)
77 if (S_ISDIR(st.st_mode)) {
78 retval = do_for_each_ref(path, fn);
83 if (read_ref(git_path("%s", path), sha1) < 0)
85 if (!has_sha1_file(sha1))
87 retval = fn(path, sha1);
97 int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
99 unsigned char sha1[20];
100 if (!read_ref(git_path("HEAD"), sha1))
101 return fn("HEAD", sha1);
105 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
107 return do_for_each_ref("refs", fn);
110 static char *ref_file_name(const char *ref)
112 char *base = get_refs_directory();
113 int baselen = strlen(base);
114 int reflen = strlen(ref);
115 char *ret = xmalloc(baselen + 2 + reflen);
116 sprintf(ret, "%s/%s", base, ref);
120 static char *ref_lock_file_name(const char *ref)
122 char *base = get_refs_directory();
123 int baselen = strlen(base);
124 int reflen = strlen(ref);
125 char *ret = xmalloc(baselen + 7 + reflen);
126 sprintf(ret, "%s/%s.lock", base, ref);
130 int get_ref_sha1(const char *ref, unsigned char *sha1)
132 const char *filename;
134 if (check_ref_format(ref))
136 filename = git_path("refs/%s", ref);
137 return read_ref(filename, sha1);
140 static int lock_ref_file(const char *filename, const char *lock_filename,
141 const unsigned char *old_sha1)
143 int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
144 unsigned char current_sha1[20];
147 return error("Couldn't open lock file for %s: %s",
148 filename, strerror(errno));
150 retval = read_ref(filename, current_sha1);
154 unlink(lock_filename);
155 return error("Could not read the current value of %s",
158 if (memcmp(current_sha1, old_sha1, 20)) {
160 unlink(lock_filename);
161 error("The current value of %s is %s",
162 filename, sha1_to_hex(current_sha1));
163 return error("Expected %s",
164 sha1_to_hex(old_sha1));
169 unlink(lock_filename);
170 return error("Unexpectedly found a value of %s for %s",
171 sha1_to_hex(current_sha1), filename);
177 int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
182 if (check_ref_format(ref))
184 filename = ref_file_name(ref);
185 lock_filename = ref_lock_file_name(ref);
186 retval = lock_ref_file(filename, lock_filename, old_sha1);
192 static int write_ref_file(const char *filename,
193 const char *lock_filename, int fd,
194 const unsigned char *sha1)
196 char *hex = sha1_to_hex(sha1);
198 if (write(fd, hex, 40) < 40 ||
199 write(fd, &term, 1) < 1) {
200 error("Couldn't write %s\n", filename);
205 rename(lock_filename, filename);
209 int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
216 if (check_ref_format(ref))
218 filename = ref_file_name(ref);
219 lock_filename = ref_lock_file_name(ref);
220 retval = write_ref_file(filename, lock_filename, fd, sha1);
226 int check_ref_format(const char *ref)
229 if (ref[0] == '.' || ref[0] == '/')
231 middle = strchr(ref, '/');
232 if (!middle || !middle[1])
234 if (strchr(middle + 1, '/'))
239 int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
245 if (check_ref_format(ref))
247 filename = ref_file_name(ref);
248 lock_filename = ref_lock_file_name(ref);
249 fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
251 error("Writing %s", lock_filename);
254 retval = write_ref_file(filename, lock_filename, fd, sha1);