Skip to contentSkip to conversation

Improve abstraction of ref lock/write.

RFC In review Shawn Pearce 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

RFC 2/5 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.

---

 fetch.c |   32 +++++----
 refs.c  |  236 ++++++++++++++++++++++++++++++---------------------------------
 refs.h  |   24 ++++--
 3 files changed, 144 insertions(+), 148 deletions(-)

d38945e0c759f8739da4c24485578a8193d54c59

fetch.c +17 -15
index 73bde07..8bdaacb 100644
@@ -204,14 +204,14 @@ static int mark_complete(const char *pat
 
 int pull(char *target)
 {
+	struct ref_lock *lock;
 	unsigned char sha1[20];
-	int fd = -1;
 
 	save_commit_buffer = 0;
 	track_object_refs = 0;
-	if (write_ref && current_ref) {
-		fd = lock_ref_sha1(write_ref, current_ref);
-		if (fd < 0)
+	if (write_ref) {
+		lock = lock_ref_sha1(write_ref, current_ref, 1);
+		if (!lock)
 			return -1;
 	}
 
@@ -219,20 +219,22 @@ int pull(char *target)
 		for_each_ref(mark_complete);
 	}
 
-	if (interpret_target(target, sha1))
-		return error("Could not interpret %s as something to pull",
-			     target);
-	if (process(lookup_unknown_object(sha1)))
+	if (interpret_target(target, sha1)) {
+		error("Could not interpret %s as something to pull", target);
+		unlock_ref(lock);
+		return -1;
+	}
+	if (process(lookup_unknown_object(sha1))) {
+		unlock_ref(lock);
 		return -1;
-	if (loop())
+	}
+	if (loop()) {
+		unlock_ref(lock);
 		return -1;
-	
+	}
+
 	if (write_ref) {
-		if (current_ref) {
-			write_ref_sha1(write_ref, fd, sha1);
-		} else {
-			write_ref_sha1_unlocked(write_ref, sha1);
-		}
+		return write_ref_sha1(lock, sha1, "git fetch");
 	}
 	return 0;
 }
refs.c +111 -125

256 lines not on this page · raw message

refs.h +16 -8
index fa816c1..b7e9df2 100644
@@ -1,6 +1,13 @@
 #ifndef REFS_H
 #define REFS_H
 
+struct ref_lock {
+	char *ref_file;
+	char *lock_file;
+	unsigned char old_sha1[20];
+	int lock_fd;
+};
+
 /*
  * Calls the specified function for each ref file until it returns nonzero,
  * and returns the value
@@ -14,16 +21,17 @@ extern int for_each_remote_ref(int (*fn)
 /** Reads the refs file specified into sha1 **/
 extern int get_ref_sha1(const char *ref, unsigned char *sha1);
 
-/** Locks ref and returns the fd to give to write_ref_sha1() if the ref
- * has the given value currently; otherwise, returns -1.
- **/
-extern int lock_ref_sha1(const char *ref, const unsigned char *old_sha1);
+/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/
+extern struct ref_lock* lock_ref_sha1(const char *ref, const unsigned char *old_sha1, int mustexist);
+
+/** Locks any ref (for 'HEAD' type refs). */
+extern struct ref_lock* lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int mustexist);
 
-/** Writes sha1 into the refs file specified, locked with the given fd. **/
-extern int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1);
+/** Release any lock taken but not written. **/
+extern void unlock_ref (struct ref_lock *lock);
 
-/** Writes sha1 into the refs file specified. **/
-extern int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1);
+/** Writes sha1 into the ref specified by the lock. **/
+extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
 
 /** Returns 0 if target has the right format for a ref. **/
 extern int check_ref_format(const char *target);
Signature · 1 line
1.3.2.g7278
#1 lore raw
Quoted 7 lines · Shawn Pearce

Created 'struct ref_lock' to contain the data necessary to perform a ref update...

 fetch.c |   32 +++++----
 refs.c  |  236 ++++++++++++++++++++++++++++++---------------------------------
 refs.h  |   24 ++++--
 3 files changed, 144 insertions(+), 148 deletions(-)

Looks a bit intrusive probably necessary so not necessarily bad but I'll postpone looking at this for now; maybe tomorrow or more likely over the weekend.

Eyeballs from people touched the affected files in the past are greatly appreciated.