Switch to using ref transactions in walker_fetch(). As part of the refactoring
to use ref transactions we also fix a potential memory leak where in the
original code if write_ref_sha1() would fail we would end up returning from
the function without free()ing the msg string.

Signed-off-by: Ronnie Sahlberg <sahlb...@google.com>
---
 walker.c | 45 ++++++++++++++++++++-------------------------
 1 file changed, 20 insertions(+), 25 deletions(-)

diff --git a/walker.c b/walker.c
index 1dd86b8..fa02d09 100644
--- a/walker.c
+++ b/walker.c
@@ -251,24 +251,16 @@ void walker_targets_free(int targets, char **target, 
const char **write_ref)
 int walker_fetch(struct walker *walker, int targets, char **target,
                 const char **write_ref, const char *write_ref_log_details)
 {
-       struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
+       struct ref_transaction *transaction;
        unsigned char *sha1 = xmalloc(targets * 20);
-       char *msg;
-       int ret;
+       char *msg = NULL;
        int i;
 
        save_commit_buffer = 0;
 
-       for (i = 0; i < targets; i++) {
-               if (!write_ref || !write_ref[i])
-                       continue;
-
-               lock[i] = lock_ref_sha1(write_ref[i], NULL);
-               if (!lock[i]) {
-                       error("Can't lock ref %s", write_ref[i]);
-                       goto unlock_and_fail;
-               }
-       }
+       transaction = ref_transaction_begin();
+       if (!transaction)
+               return -1;
 
        if (!walker->get_recover)
                for_each_ref(mark_complete, NULL);
@@ -276,14 +268,14 @@ int walker_fetch(struct walker *walker, int targets, char 
**target,
        for (i = 0; i < targets; i++) {
                if (interpret_target(walker, target[i], &sha1[20 * i])) {
                        error("Could not interpret response from server '%s' as 
something to pull", target[i]);
-                       goto unlock_and_fail;
+                       goto rollback_and_fail;
                }
                if (process(walker, lookup_unknown_object(&sha1[20 * i])))
-                       goto unlock_and_fail;
+                       goto rollback_and_fail;
        }
 
        if (loop(walker))
-               goto unlock_and_fail;
+               goto rollback_and_fail;
 
        if (write_ref_log_details) {
                msg = xmalloc(strlen(write_ref_log_details) + 12);
@@ -294,19 +286,22 @@ int walker_fetch(struct walker *walker, int targets, char 
**target,
        for (i = 0; i < targets; i++) {
                if (!write_ref || !write_ref[i])
                        continue;
-               ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch 
(unknown)");
-               lock[i] = NULL;
-               if (ret)
-                       goto unlock_and_fail;
+               if (ref_transaction_update(transaction, write_ref[i],
+                                          &sha1[20 * i], NULL,
+                                          0, 0))
+                       goto rollback_and_fail;
        }
-       free(msg);
 
+       if (ref_transaction_commit(transaction, msg ? msg : "fetch (unknown)",
+                                  UPDATE_REFS_QUIET_ON_ERR))
+               goto rollback_and_fail;
+
+       free(msg);
        return 0;
 
-unlock_and_fail:
-       for (i = 0; i < targets; i++)
-               if (lock[i])
-                       unlock_ref(lock[i]);
+rollback_and_fail:
+       free(msg);
+       ref_transaction_rollback(transaction);
 
        return -1;
 }
-- 
1.9.1.513.gd486896

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to