apply: move lockfile into `apply_state`
[gitweb.git] / builtin / am.c
index c369dd1dce2d705206414d0bbf5a4415d9487cab..40968428dd5f4f966c07c2bed374015e0b399550 100644 (file)
@@ -671,9 +671,7 @@ static int detect_patch_format(const char **paths)
                goto done;
        }
 
-       strbuf_reset(&l2);
        strbuf_getline(&l2, fp);
-       strbuf_reset(&l3);
        strbuf_getline(&l3, fp);
 
        /*
@@ -696,6 +694,8 @@ static int detect_patch_format(const char **paths)
 done:
        fclose(fp);
        strbuf_release(&l1);
+       strbuf_release(&l2);
+       strbuf_release(&l3);
        return ret;
 }
 
@@ -881,6 +881,7 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
 static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
 {
        struct strbuf sb = STRBUF_INIT;
+       int rc = 0;
 
        while (!strbuf_getline_lf(&sb, in)) {
                const char *str;
@@ -894,19 +895,27 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
 
                        errno = 0;
                        timestamp = parse_timestamp(str, &end, 10);
-                       if (errno)
-                               return error(_("invalid timestamp"));
+                       if (errno) {
+                               rc = error(_("invalid timestamp"));
+                               goto exit;
+                       }
 
-                       if (!skip_prefix(end, " ", &str))
-                               return error(_("invalid Date line"));
+                       if (!skip_prefix(end, " ", &str)) {
+                               rc = error(_("invalid Date line"));
+                               goto exit;
+                       }
 
                        errno = 0;
                        tz = strtol(str, &end, 10);
-                       if (errno)
-                               return error(_("invalid timezone offset"));
+                       if (errno) {
+                               rc = error(_("invalid timezone offset"));
+                               goto exit;
+                       }
 
-                       if (*end)
-                               return error(_("invalid Date line"));
+                       if (*end) {
+                               rc = error(_("invalid Date line"));
+                               goto exit;
+                       }
 
                        /*
                         * mercurial's timezone is in seconds west of UTC,
@@ -931,9 +940,9 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
                fwrite(sb.buf, 1, sb.len, out);
                strbuf_reset(&sb);
        }
-
+exit:
        strbuf_release(&sb);
-       return 0;
+       return rc;
 }
 
 /**
@@ -1125,11 +1134,11 @@ static const char *msgnum(const struct am_state *state)
  */
 static void refresh_and_write_cache(void)
 {
-       struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
+       struct lock_file lock_file = LOCK_INIT;
 
-       hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
+       hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
        refresh_cache(REFRESH_QUIET);
-       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
+       if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                die(_("unable to write index file"));
 }
 
@@ -1479,11 +1488,10 @@ static int run_apply(const struct am_state *state, const char *index_file)
        struct argv_array apply_opts = ARGV_ARRAY_INIT;
        struct apply_state apply_state;
        int res, opts_left;
-       static struct lock_file lock_file;
        int force_apply = 0;
        int options = 0;
 
-       if (init_apply_state(&apply_state, NULL, &lock_file))
+       if (init_apply_state(&apply_state, NULL))
                die("BUG: init_apply_state() failed");
 
        argv_array_push(&apply_opts, "apply");
@@ -1937,15 +1945,14 @@ static void am_resolve(struct am_state *state)
  */
 static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
 {
-       struct lock_file *lock_file;
+       struct lock_file lock_file = LOCK_INIT;
        struct unpack_trees_options opts;
        struct tree_desc t[2];
 
        if (parse_tree(head) || parse_tree(remote))
                return -1;
 
-       lock_file = xcalloc(1, sizeof(struct lock_file));
-       hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
+       hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
 
        refresh_cache(REFRESH_QUIET);
 
@@ -1961,11 +1968,11 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
        init_tree_desc(&t[1], remote->buffer, remote->size);
 
        if (unpack_trees(2, t, &opts)) {
-               rollback_lock_file(lock_file);
+               rollback_lock_file(&lock_file);
                return -1;
        }
 
-       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
+       if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                die(_("unable to write new index file"));
 
        return 0;
@@ -1977,15 +1984,14 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
  */
 static int merge_tree(struct tree *tree)
 {
-       struct lock_file *lock_file;
+       struct lock_file lock_file = LOCK_INIT;
        struct unpack_trees_options opts;
        struct tree_desc t[1];
 
        if (parse_tree(tree))
                return -1;
 
-       lock_file = xcalloc(1, sizeof(struct lock_file));
-       hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
+       hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
 
        memset(&opts, 0, sizeof(opts));
        opts.head_idx = 1;
@@ -1996,11 +2002,11 @@ static int merge_tree(struct tree *tree)
        init_tree_desc(&t[0], tree->buffer, tree->size);
 
        if (unpack_trees(1, t, &opts)) {
-               rollback_lock_file(lock_file);
+               rollback_lock_file(&lock_file);
                return -1;
        }
 
-       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
+       if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                die(_("unable to write new index file"));
 
        return 0;
@@ -2096,6 +2102,7 @@ static int safe_to_abort(const struct am_state *state)
                        die(_("could not parse %s"), am_path(state, "abort-safety"));
        } else
                oidclr(&abort_safety);
+       strbuf_release(&sb);
 
        if (get_oid("HEAD", &head))
                oidclr(&head);