6 static int read_ref(const char *path
, unsigned char *sha1
)
9 int fd
= open(path
, 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(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 (lstat(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 const char *headpath
= git_path("HEAD");
75 if (!read_ref(headpath
, sha1
))
76 return fn(headpath
, sha1
);
80 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
82 return do_for_each_ref(get_refs_directory(), fn
);
85 static char *ref_file_name(const char *ref
)
87 char *base
= get_refs_directory();
88 int baselen
= strlen(base
);
89 int reflen
= strlen(ref
);
90 char *ret
= xmalloc(baselen
+ 2 + reflen
);
91 sprintf(ret
, "%s/%s", base
, ref
);
95 static char *ref_lock_file_name(const char *ref
)
97 char *base
= get_refs_directory();
98 int baselen
= strlen(base
);
99 int reflen
= strlen(ref
);
100 char *ret
= xmalloc(baselen
+ 7 + reflen
);
101 sprintf(ret
, "%s/%s.lock", base
, ref
);
105 static int read_ref_file(const char *filename
, unsigned char *sha1
) {
106 int fd
= open(filename
, O_RDONLY
);
109 return error("Couldn't open %s\n", filename
);
111 if ((read(fd
, hex
, 41) < 41) ||
113 get_sha1_hex(hex
, sha1
)) {
114 error("Couldn't read a hash from %s\n", filename
);
122 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
126 if (check_ref_format(ref
))
128 filename
= ref_file_name(ref
);
129 retval
= read_ref_file(filename
, sha1
);
134 static int lock_ref_file(const char *filename
, const char *lock_filename
,
135 const unsigned char *old_sha1
)
137 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
138 unsigned char current_sha1
[20];
141 return error("Couldn't open lock file for %s: %s",
142 filename
, strerror(errno
));
144 retval
= read_ref_file(filename
, current_sha1
);
148 unlink(lock_filename
);
149 return error("Could not read the current value of %s",
152 if (memcmp(current_sha1
, old_sha1
, 20)) {
154 unlink(lock_filename
);
155 error("The current value of %s is %s",
156 filename
, sha1_to_hex(current_sha1
));
157 return error("Expected %s",
158 sha1_to_hex(old_sha1
));
163 unlink(lock_filename
);
164 return error("Unexpectedly found a value of %s for %s",
165 sha1_to_hex(current_sha1
), filename
);
171 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
176 if (check_ref_format(ref
))
178 filename
= ref_file_name(ref
);
179 lock_filename
= ref_lock_file_name(ref
);
180 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
186 static int write_ref_file(const char *filename
,
187 const char *lock_filename
, int fd
,
188 const unsigned char *sha1
)
190 char *hex
= sha1_to_hex(sha1
);
192 if (write(fd
, hex
, 40) < 40 ||
193 write(fd
, &term
, 1) < 1) {
194 error("Couldn't write %s\n", filename
);
199 rename(lock_filename
, filename
);
203 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
210 if (check_ref_format(ref
))
212 filename
= ref_file_name(ref
);
213 lock_filename
= ref_lock_file_name(ref
);
214 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
220 int check_ref_format(const char *ref
)
223 if (ref
[0] == '.' || ref
[0] == '/')
225 middle
= strchr(ref
, '/');
226 if (!middle
|| !middle
[1])
228 if (strchr(middle
+ 1, '/'))
233 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
239 if (check_ref_format(ref
))
241 filename
= ref_file_name(ref
);
242 lock_filename
= ref_lock_file_name(ref
);
243 fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
245 error("Writing %s", lock_filename
);
248 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);