This commit replaces spawning `git write-tree` with API calls.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebast...@gmail.com>
---
 builtin/stash.c | 41 ++++++++++++-----------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)

diff --git a/builtin/stash.c b/builtin/stash.c
index d2365ada2e..651d05c820 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -947,9 +947,8 @@ static int save_untracked_files(struct stash_info *info, 
struct strbuf *msg,
 {
        int ret = 0;
        struct strbuf untracked_msg = STRBUF_INIT;
-       struct strbuf out = STRBUF_INIT;
        struct child_process cp_upd_index = CHILD_PROCESS_INIT;
-       struct child_process cp_write_tree = CHILD_PROCESS_INIT;
+       struct index_state istate = { NULL };
 
        cp_upd_index.git_cmd = 1;
        argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
@@ -964,15 +963,11 @@ static int save_untracked_files(struct stash_info *info, 
struct strbuf *msg,
                goto done;
        }
 
-       cp_write_tree.git_cmd = 1;
-       argv_array_push(&cp_write_tree.args, "write-tree");
-       argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
-                        stash_index_path.buf);
-       if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
+       if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
+                               NULL)) {
                ret = -1;
                goto done;
        }
-       get_oid_hex(out.buf, &info->u_tree);
 
        if (commit_tree(untracked_msg.buf, untracked_msg.len,
                        &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
@@ -981,8 +976,8 @@ static int save_untracked_files(struct stash_info *info, 
struct strbuf *msg,
        }
 
 done:
+       discard_index(&istate);
        strbuf_release(&untracked_msg);
-       strbuf_release(&out);
        remove_path(stash_index_path.buf);
        return ret;
 }
@@ -991,11 +986,10 @@ static int stash_patch(struct stash_info *info, struct 
pathspec ps,
                       struct strbuf *out_patch, int quiet)
 {
        int ret = 0;
-       struct strbuf out = STRBUF_INIT;
        struct child_process cp_read_tree = CHILD_PROCESS_INIT;
        struct child_process cp_add_i = CHILD_PROCESS_INIT;
-       struct child_process cp_write_tree = CHILD_PROCESS_INIT;
        struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
+       struct index_state istate = { NULL };
 
        remove_path(stash_index_path.buf);
 
@@ -1021,17 +1015,12 @@ static int stash_patch(struct stash_info *info, struct 
pathspec ps,
        }
 
        /* State of the working tree. */
-       cp_write_tree.git_cmd = 1;
-       argv_array_push(&cp_write_tree.args, "write-tree");
-       argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
-                        stash_index_path.buf);
-       if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
+       if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
+                               NULL)) {
                ret = -1;
                goto done;
        }
 
-       get_oid_hex(out.buf, &info->w_tree);
-
        cp_diff_tree.git_cmd = 1;
        argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
                         oid_to_hex(&info->w_tree), "--", NULL);
@@ -1047,7 +1036,7 @@ static int stash_patch(struct stash_info *info, struct 
pathspec ps,
        }
 
 done:
-       strbuf_release(&out);
+       discard_index(&istate);
        remove_path(stash_index_path.buf);
        return ret;
 }
@@ -1057,9 +1046,8 @@ static int stash_working_tree(struct stash_info *info, 
struct pathspec ps)
        int ret = 0;
        struct rev_info rev;
        struct child_process cp_upd_index = CHILD_PROCESS_INIT;
-       struct child_process cp_write_tree = CHILD_PROCESS_INIT;
-       struct strbuf out = STRBUF_INIT;
        struct strbuf diff_output = STRBUF_INIT;
+       struct index_state istate = { NULL };
 
        set_alternate_index_output(stash_index_path.buf);
        if (reset_tree(&info->i_tree, 0, 0)) {
@@ -1099,20 +1087,15 @@ static int stash_working_tree(struct stash_info *info, 
struct pathspec ps)
                goto done;
        }
 
-       cp_write_tree.git_cmd = 1;
-       argv_array_push(&cp_write_tree.args, "write-tree");
-       argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
-                        stash_index_path.buf);
-       if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
+       if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
+                               NULL)) {
                ret = -1;
                goto done;
        }
 
-       get_oid_hex(out.buf, &info->w_tree);
-
 done:
+       discard_index(&istate);
        UNLEAK(rev);
-       strbuf_release(&out);
        object_array_clear(&rev.pending);
        strbuf_release(&diff_output);
        remove_path(stash_index_path.buf);
-- 
2.19.0.rc0.23.g10a62394e7

Reply via email to