]> git.ipfire.org Git - thirdparty/git.git/commitdiff
files-backend: extract out `create_symref_lock()`
authorKarthik Nayak <karthik.188@gmail.com>
Tue, 7 May 2024 12:58:53 +0000 (14:58 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 May 2024 15:51:49 +0000 (08:51 -0700)
The function `create_symref_locked()` creates a symref by creating a
'<symref>.lock' file and then committing the symref lock, which creates
the final symref.

Extract the early half of `create_symref_locked()` into a new helper
function `create_symref_lock()`. Because the name of the new function is
too similar to the original, rename the original to
`create_and_commit_symref()` to avoid confusion.

The new function `create_symref_locked()` can be used to create the
symref lock in a separate step from that of committing it. This allows
to add transactional support for symrefs, where the lock would be
created in the preparation step and the lock would be committed in the
finish step.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/files-backend.c

index e4d0aa3d41fd77eeacf7efad08f8510fc1333631..74a713090ca5506ee4bcf524e25df939351f5235 100644 (file)
@@ -1920,27 +1920,49 @@ static void update_symref_reflog(struct files_ref_store *refs,
        }
 }
 
-static int create_symref_locked(struct files_ref_store *refs,
-                               struct ref_lock *lock, const char *refname,
-                               const char *target, const char *logmsg)
+static int create_symref_lock(struct files_ref_store *refs,
+                             struct ref_lock *lock, const char *refname,
+                             const char *target, struct strbuf *err)
 {
+       if (!fdopen_lock_file(&lock->lk, "w")) {
+               strbuf_addf(err, "unable to fdopen %s: %s",
+                            get_lock_file_path(&lock->lk), strerror(errno));
+               return -1;
+       }
+
+       if (fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target) < 0) {
+               strbuf_addf(err, "unable to write to %s: %s",
+                            get_lock_file_path(&lock->lk), strerror(errno));
+               return -1;
+       }
+
+       return 0;
+}
+
+static int create_and_commit_symref(struct files_ref_store *refs,
+                                   struct ref_lock *lock, const char *refname,
+                                   const char *target, const char *logmsg)
+{
+       struct strbuf err = STRBUF_INIT;
+       int ret;
+
        if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
                update_symref_reflog(refs, lock, refname, target, logmsg);
                return 0;
        }
 
-       if (!fdopen_lock_file(&lock->lk, "w"))
-               return error("unable to fdopen %s: %s",
-                            get_lock_file_path(&lock->lk), strerror(errno));
+       ret = create_symref_lock(refs, lock, refname, target, &err);
+       if (!ret) {
+               update_symref_reflog(refs, lock, refname, target, logmsg);
 
-       update_symref_reflog(refs, lock, refname, target, logmsg);
+               if (commit_ref(lock) < 0)
+                       return error("unable to write symref for %s: %s", refname,
+                                    strerror(errno));
+       } else {
+               return error("%s", err.buf);
+       }
 
-       /* no error check; commit_ref will check ferror */
-       fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target);
-       if (commit_ref(lock) < 0)
-               return error("unable to write symref for %s: %s", refname,
-                            strerror(errno));
-       return 0;
+       return ret;
 }
 
 static int files_create_symref(struct ref_store *ref_store,
@@ -1960,7 +1982,8 @@ static int files_create_symref(struct ref_store *ref_store,
                return -1;
        }
 
-       ret = create_symref_locked(refs, lock, refname, target, logmsg);
+       ret = create_and_commit_symref(refs, lock, refname, target, logmsg);
+
        unlock_ref(lock);
        return ret;
 }