stash: replace all `write-tree` child processes with API calls

Avoid spawning write-tree child processes by replacing the calls with in-core API calls. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul-Sebastian Ungureanu committed Feb 25, 2019 at 23:16 UTC 48ee24ab72b4ffd40789a99281066d79c2c5a720
1 file changed +12 -29
builtin/stash--helper.c
+12 -29
@@ -943,9 +943,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
943 {
944 int ret = 0;
945 struct strbuf untracked_msg = STRBUF_INIT;
946 - struct strbuf out = STRBUF_INIT;
946 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
948 - struct child_process cp_write_tree = CHILD_PROCESS_INIT;
947 + struct index_state istate = { NULL };
948
949 cp_upd_index.git_cmd = 1;
950 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
@@ -960,15 +959,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
959 goto done;
960 }
961
963 - cp_write_tree.git_cmd = 1;
964 - argv_array_push(&cp_write_tree.args, "write-tree");
965 - argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
966 - stash_index_path.buf);
967 - if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
962 + if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
963 + NULL)) {
964 ret = -1;
965 goto done;
966 }
971 - get_oid_hex(out.buf, &info->u_tree);
967
968 if (commit_tree(untracked_msg.buf, untracked_msg.len,
969 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
@@ -977,8 +972,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
972 }
973
974 done:
975 + discard_index(&istate);
976 strbuf_release(&untracked_msg);
981 - strbuf_release(&out);
977 remove_path(stash_index_path.buf);
978 return ret;
979 }
@@ -987,11 +982,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
982 struct strbuf *out_patch, int quiet)
983 {
984 int ret = 0;
990 - struct strbuf out = STRBUF_INIT;
985 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
986 struct child_process cp_add_i = CHILD_PROCESS_INIT;
993 - struct child_process cp_write_tree = CHILD_PROCESS_INIT;
987 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
988 + struct index_state istate = { NULL };
989
990 remove_path(stash_index_path.buf);
991
@@ -1017,17 +1011,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
1011 }
1012
1013 /* State of the working tree. */
1020 - cp_write_tree.git_cmd = 1;
1021 - argv_array_push(&cp_write_tree.args, "write-tree");
1022 - argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
1023 - stash_index_path.buf);
1024 - if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
1014 + if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1015 + NULL)) {
1016 ret = -1;
1017 goto done;
1018 }
1019
1029 - get_oid_hex(out.buf, &info->w_tree);
1030 -
1020 cp_diff_tree.git_cmd = 1;
1021 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1022 oid_to_hex(&info->w_tree), "--", NULL);
@@ -1043,7 +1032,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
1032 }
1033
1034 done:
1046 - strbuf_release(&out);
1035 + discard_index(&istate);
1036 remove_path(stash_index_path.buf);
1037 return ret;
1038 }
@@ -1053,9 +1042,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1042 int ret = 0;
1043 struct rev_info rev;
1044 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1056 - struct child_process cp_write_tree = CHILD_PROCESS_INIT;
1057 - struct strbuf out = STRBUF_INIT;
1045 struct strbuf diff_output = STRBUF_INIT;
1046 + struct index_state istate = { NULL };
1047
1048 init_revisions(&rev, NULL);
1049
@@ -1095,20 +1083,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1083 goto done;
1084 }
1085
1098 - cp_write_tree.git_cmd = 1;
1099 - argv_array_push(&cp_write_tree.args, "write-tree");
1100 - argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
1101 - stash_index_path.buf);
1102 - if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
1086 + if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1087 + NULL)) {
1088 ret = -1;
1089 goto done;
1090 }
1091
1107 - get_oid_hex(out.buf, &info->w_tree);
1108 -
1092 done:
1093 + discard_index(&istate);
1094 UNLEAK(rev);
1111 - strbuf_release(&out);
1095 object_array_clear(&rev.pending);
1096 strbuf_release(&diff_output);
1097 remove_path(stash_index_path.buf);