6 static int read_ref(const char *refname, unsigned char *sha1)
9 int fd = open(git_path("%s", refname), O_RDONLY);
13 if (read(fd, buffer, sizeof(buffer)) >= 40)
14 ret = get_sha1_hex(buffer, sha1);
20 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
23 DIR *dir = opendir(git_path("%s", base));
27 int baselen = strlen(base);
28 char *path = xmalloc(baselen + 257);
30 if (!strncmp(base, "./", 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];
43 if (de->d_name[0] == '.')
45 namelen = strlen(de->d_name);
48 memcpy(path + baselen, de->d_name, namelen+1);
49 if (stat(git_path("%s", path), &st) < 0)
51 if (S_ISDIR(st.st_mode)) {
52 retval = do_for_each_ref(path, fn);
57 if (read_ref(path, sha1) < 0)
59 if (!has_sha1_file(sha1))
61 retval = fn(path, sha1);
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);
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);
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);
104 static int read_ref_file(const char *filename, unsigned char *sha1) {
105 int fd = open(filename, O_RDONLY);
108 return error("Couldn't open %s\n", filename);
110 if ((read(fd, hex, 41) < 41) ||
112 get_sha1_hex(hex, sha1)) {
113 error("Couldn't read a hash from %s\n", filename);
121 int get_ref_sha1(const char *ref, unsigned char *sha1)
125 if (check_ref_format(ref))
127 filename = ref_file_name(ref);
128 retval = read_ref_file(filename, sha1);
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];
140 return error("Couldn't open lock file for %s: %s",
141 filename, strerror(errno));
143 retval = read_ref_file(filename, current_sha1);
147 unlink(lock_filename);
148 return error("Could not read the current value of %s",
151 if (memcmp(current_sha1, old_sha1, 20)) {
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));
162 unlink(lock_filename);
163 return error("Unexpectedly found a value of %s for %s",
164 sha1_to_hex(current_sha1), filename);
170 int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
175 if (check_ref_format(ref))
177 filename = ref_file_name(ref);
178 lock_filename = ref_lock_file_name(ref);
179 retval = lock_ref_file(filename, lock_filename, old_sha1);
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);
191 if (write(fd, hex, 40) < 40 ||
192 write(fd, &term, 1) < 1) {
193 error("Couldn't write %s\n", filename);
198 rename(lock_filename, filename);
202 int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
209 if (check_ref_format(ref))
211 filename = ref_file_name(ref);
212 lock_filename = ref_lock_file_name(ref);
213 retval = write_ref_file(filename, lock_filename, fd, sha1);
219 int check_ref_format(const char *ref)
222 if (ref[0] == '.' || ref[0] == '/')
224 middle = strchr(ref, '/');
225 if (!middle || !middle[1])
227 if (strchr(middle + 1, '/'))
232 int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
238 if (check_ref_format(ref))
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);
244 error("Writing %s", lock_filename);
247 retval = write_ref_file(filename, lock_filename, fd, sha1);