@matheusbsbs / matheus-bagliano / commits / 4bd18c43d9
Fork 0

Improve abstraction of ref lock/write.

Created 'struct ref_lock' to contain the data necessary to perform a ref update. This change improves writing a ref as the file names are generated only once (rather than twice) and supports following symrefs (up to the maximum depth). Further the ref_lock structure provides room to extend the update API with ref logging. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn Pearce committed May 17, 2006 at 05:55 UTC 4bd18c43d9a8a8fc1aa598926100f6999953ba48
3 files changed +144 -148
fetch.c
+17 -15
@@ -204,14 +204,14 @@ static int mark_complete(const char *path, const unsigned char *sha1)
204
205 int pull(char *target)
206 {
207 + struct ref_lock *lock;
208 unsigned char sha1[20];
208 - int fd = -1;
209
210 save_commit_buffer = 0;
211 track_object_refs = 0;
212 - if (write_ref && current_ref) {
213 - fd = lock_ref_sha1(write_ref, current_ref);
214 - if (fd < 0)
212 + if (write_ref) {
213 + lock = lock_ref_sha1(write_ref, current_ref, 1);
214 + if (!lock)
215 return -1;
216 }
217
@@ -219,20 +219,22 @@ int pull(char *target)
219 for_each_ref(mark_complete);
220 }
221
222 - if (interpret_target(target, sha1))
223 - return error("Could not interpret %s as something to pull",
224 - target);
225 - if (process(lookup_unknown_object(sha1)))
222 + if (interpret_target(target, sha1)) {
223 + error("Could not interpret %s as something to pull", target);
224 + unlock_ref(lock);
225 + return -1;
226 + }
227 + if (process(lookup_unknown_object(sha1))) {
228 + unlock_ref(lock);
229 return -1;
227 - if (loop())
230 + }
231 + if (loop()) {
232 + unlock_ref(lock);
233 return -1;
229 -
234 + }
235 +
236 if (write_ref) {
231 - if (current_ref) {
232 - write_ref_sha1(write_ref, fd, sha1);
233 - } else {
234 - write_ref_sha1_unlocked(write_ref, sha1);
235 - }
237 + return write_ref_sha1(lock, sha1, "git fetch");
238 }
239 return 0;
240 }
refs.c
+111 -125
@@ -198,26 +198,6 @@ int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
198 return do_for_each_ref("refs/remotes", fn, 13);
199 }
200
201 -static char *ref_file_name(const char *ref)
202 -{
203 - char *base = get_refs_directory();
204 - int baselen = strlen(base);
205 - int reflen = strlen(ref);
206 - char *ret = xmalloc(baselen + 2 + reflen);
207 - sprintf(ret, "%s/%s", base, ref);
208 - return ret;
209 -}
210 -
211 -static char *ref_lock_file_name(const char *ref)
212 -{
213 - char *base = get_refs_directory();
214 - int baselen = strlen(base);
215 - int reflen = strlen(ref);
216 - char *ret = xmalloc(baselen + 7 + reflen);
217 - sprintf(ret, "%s/%s.lock", base, ref);
218 - return ret;
219 -}
220 -
201 int get_ref_sha1(const char *ref, unsigned char *sha1)
202 {
203 if (check_ref_format(ref))
@@ -225,94 +205,6 @@ int get_ref_sha1(const char *ref, unsigned char *sha1)
205 return read_ref(git_path("refs/%s", ref), sha1);
206 }
207
228 -static int lock_ref_file(const char *filename, const char *lock_filename,
229 - const unsigned char *old_sha1)
230 -{
231 - int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
232 - unsigned char current_sha1[20];
233 - int retval;
234 - if (fd < 0) {
235 - return error("Couldn't open lock file for %s: %s",
236 - filename, strerror(errno));
237 - }
238 - retval = read_ref(filename, current_sha1);
239 - if (old_sha1) {
240 - if (retval) {
241 - close(fd);
242 - unlink(lock_filename);
243 - return error("Could not read the current value of %s",
244 - filename);
245 - }
246 - if (memcmp(current_sha1, old_sha1, 20)) {
247 - close(fd);
248 - unlink(lock_filename);
249 - error("The current value of %s is %s",
250 - filename, sha1_to_hex(current_sha1));
251 - return error("Expected %s",
252 - sha1_to_hex(old_sha1));
253 - }
254 - } else {
255 - if (!retval) {
256 - close(fd);
257 - unlink(lock_filename);
258 - return error("Unexpectedly found a value of %s for %s",
259 - sha1_to_hex(current_sha1), filename);
260 - }
261 - }
262 - return fd;
263 -}
264 -
265 -int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
266 -{
267 - char *filename;
268 - char *lock_filename;
269 - int retval;
270 - if (check_ref_format(ref))
271 - return -1;
272 - filename = ref_file_name(ref);
273 - lock_filename = ref_lock_file_name(ref);
274 - retval = lock_ref_file(filename, lock_filename, old_sha1);
275 - free(filename);
276 - free(lock_filename);
277 - return retval;
278 -}
279 -
280 -static int write_ref_file(const char *filename,
281 - const char *lock_filename, int fd,
282 - const unsigned char *sha1)
283 -{
284 - char *hex = sha1_to_hex(sha1);
285 - char term = '\n';
286 - if (write(fd, hex, 40) < 40 ||
287 - write(fd, &term, 1) < 1) {
288 - error("Couldn't write %s", filename);
289 - close(fd);
290 - return -1;
291 - }
292 - close(fd);
293 - rename(lock_filename, filename);
294 - return 0;
295 -}
296 -
297 -int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
298 -{
299 - char *filename;
300 - char *lock_filename;
301 - int retval;
302 - if (fd < 0)
303 - return -1;
304 - if (check_ref_format(ref))
305 - return -1;
306 - filename = ref_file_name(ref);
307 - lock_filename = ref_lock_file_name(ref);
308 - if (safe_create_leading_directories(filename))
309 - die("unable to create leading directory for %s", filename);
310 - retval = write_ref_file(filename, lock_filename, fd, sha1);
311 - free(filename);
312 - free(lock_filename);
313 - return retval;
314 -}
315 -
208 /*
209 * Make sure "ref" is something reasonable to have under ".git/refs/";
210 * We do not like it if:
@@ -365,25 +257,119 @@ int check_ref_format(const char *ref)
257 }
258 }
259
368 -int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
260 +static struct ref_lock* verify_lock(struct ref_lock *lock,
261 + const unsigned char *old_sha1, int mustexist)
262 +{
263 + char buf[40];
264 + int nr, fd = open(lock->ref_file, O_RDONLY);
265 + if (fd < 0 && (mustexist || errno != ENOENT)) {
266 + error("Can't verify ref %s", lock->ref_file);
267 + unlock_ref(lock);
268 + return NULL;
269 + }
270 + nr = read(fd, buf, 40);
271 + close(fd);
272 + if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) {
273 + error("Can't verify ref %s", lock->ref_file);
274 + unlock_ref(lock);
275 + return NULL;
276 + }
277 + if (memcmp(lock->old_sha1, old_sha1, 20)) {
278 + error("Ref %s is at %s but expected %s", lock->ref_file,
279 + sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
280 + unlock_ref(lock);
281 + return NULL;
282 + }
283 + return lock;
284 +}
285 +
286 +static struct ref_lock* lock_ref_sha1_basic(const char *path,
287 + int plen,
288 + const unsigned char *old_sha1, int mustexist)
289 +{
290 + struct ref_lock *lock;
291 +
292 + lock = xcalloc(1, sizeof(struct ref_lock));
293 + lock->lock_fd = -1;
294 +
295 + plen = strlen(path) - plen;
296 + path = resolve_ref(path, lock->old_sha1, mustexist);
297 + if (!path) {
298 + error("Can't read ref %s", path);
299 + unlock_ref(lock);
300 + return NULL;
301 + }
302 +
303 + lock->ref_file = strdup(path);
304 + lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file));
305 +
306 + if (safe_create_leading_directories(lock->lock_file))
307 + die("unable to create directory for %s", lock->lock_file);
308 + lock->lock_fd = open(lock->lock_file,
309 + O_WRONLY | O_CREAT | O_EXCL, 0666);
310 + if (lock->lock_fd < 0) {
311 + error("Couldn't open lock file %s: %s",
312 + lock->lock_file, strerror(errno));
313 + unlock_ref(lock);
314 + return NULL;
315 + }
316 +
317 + return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
318 +}
319 +
320 +struct ref_lock* lock_ref_sha1(const char *ref,
321 + const unsigned char *old_sha1, int mustexist)
322 {
370 - char *filename;
371 - char *lock_filename;
372 - int fd;
373 - int retval;
323 if (check_ref_format(ref))
324 + return NULL;
325 + return lock_ref_sha1_basic(git_path("refs/%s", ref),
326 + strlen(ref), old_sha1, mustexist);
327 +}
328 +
329 +struct ref_lock* lock_any_ref_for_update(const char *ref,
330 + const unsigned char *old_sha1, int mustexist)
331 +{
332 + return lock_ref_sha1_basic(git_path("%s", ref),
333 + strlen(ref), old_sha1, mustexist);
334 +}
335 +
336 +void unlock_ref (struct ref_lock *lock)
337 +{
338 + if (lock->lock_fd >= 0) {
339 + close(lock->lock_fd);
340 + unlink(lock->lock_file);
341 + }
342 + if (lock->ref_file)
343 + free(lock->ref_file);
344 + if (lock->lock_file)
345 + free(lock->lock_file);
346 + free(lock);
347 +}
348 +
349 +int write_ref_sha1(struct ref_lock *lock,
350 + const unsigned char *sha1, const char *logmsg)
351 +{
352 + static char term = '\n';
353 +
354 + if (!lock)
355 return -1;
376 - filename = ref_file_name(ref);
377 - lock_filename = ref_lock_file_name(ref);
378 - if (safe_create_leading_directories(filename))
379 - die("unable to create leading directory for %s", filename);
380 - fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
381 - if (fd < 0) {
382 - error("Writing %s", lock_filename);
383 - perror("Open");
356 + if (!memcmp(lock->old_sha1, sha1, 20)) {
357 + unlock_ref(lock);
358 + return 0;
359 }
385 - retval = write_ref_file(filename, lock_filename, fd, sha1);
386 - free(filename);
387 - free(lock_filename);
388 - return retval;
360 + if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
361 + write(lock->lock_fd, &term, 1) != 1
362 + || close(lock->lock_fd) < 0) {
363 + error("Couldn't write %s", lock->lock_file);
364 + unlock_ref(lock);
365 + return -1;
366 + }
367 + if (rename(lock->lock_file, lock->ref_file) < 0) {
368 + error("Couldn't set %s", lock->ref_file);
369 + unlock_ref(lock);
370 + return -1;
371 + }
372 + lock->lock_fd = -1;
373 + unlock_ref(lock);
374 + return 0;
375 }
refs.h
+16 -8
@@ -1,6 +1,13 @@
1 #ifndef REFS_H
2 #define REFS_H
3
4 +struct ref_lock {
5 + char *ref_file;
6 + char *lock_file;
7 + unsigned char old_sha1[20];
8 + int lock_fd;
9 +};
10 +
11 /*
12 * Calls the specified function for each ref file until it returns nonzero,
13 * and returns the value
@@ -14,16 +21,17 @@ extern int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *
21 /** Reads the refs file specified into sha1 **/
22 extern int get_ref_sha1(const char *ref, unsigned char *sha1);
23
17 -/** Locks ref and returns the fd to give to write_ref_sha1() if the ref
18 - * has the given value currently; otherwise, returns -1.
19 - **/
20 -extern int lock_ref_sha1(const char *ref, const unsigned char *old_sha1);
24 +/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/
25 +extern struct ref_lock* lock_ref_sha1(const char *ref, const unsigned char *old_sha1, int mustexist);
26 +
27 +/** Locks any ref (for 'HEAD' type refs). */
28 +extern struct ref_lock* lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int mustexist);
29
22 -/** Writes sha1 into the refs file specified, locked with the given fd. **/
23 -extern int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1);
30 +/** Release any lock taken but not written. **/
31 +extern void unlock_ref (struct ref_lock *lock);
32
25 -/** Writes sha1 into the refs file specified. **/
26 -extern int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1);
33 +/** Writes sha1 into the ref specified by the lock. **/
34 +extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
35
36 /** Returns 0 if target has the right format for a ref. **/
37 extern int check_ref_format(const char *target);