Skip to content

Commit 6fd9d0f

Browse files
ungpsdscho
authored andcommitted
stash: replace all write-tree child processes with API calls
This commit replaces spawning `git write-tree` with API calls. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
1 parent 43a4888 commit 6fd9d0f

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

builtin/stash.c

+12-29
Original file line numberDiff line numberDiff line change
@@ -945,9 +945,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
945945
{
946946
int ret = 0;
947947
struct strbuf untracked_msg = STRBUF_INIT;
948-
struct strbuf out = STRBUF_INIT;
949948
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
950-
struct child_process cp_write_tree = CHILD_PROCESS_INIT;
949+
struct index_state istate = { NULL };
951950

952951
cp_upd_index.git_cmd = 1;
953952
argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
@@ -962,15 +961,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
962961
goto done;
963962
}
964963

965-
cp_write_tree.git_cmd = 1;
966-
argv_array_push(&cp_write_tree.args, "write-tree");
967-
argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
968-
stash_index_path.buf);
969-
if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
964+
if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
965+
NULL)) {
970966
ret = -1;
971967
goto done;
972968
}
973-
get_oid_hex(out.buf, &info->u_tree);
974969

975970
if (commit_tree(untracked_msg.buf, untracked_msg.len,
976971
&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
@@ -979,8 +974,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
979974
}
980975

981976
done:
977+
discard_index(&istate);
982978
strbuf_release(&untracked_msg);
983-
strbuf_release(&out);
984979
remove_path(stash_index_path.buf);
985980
return ret;
986981
}
@@ -989,11 +984,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
989984
struct strbuf *out_patch, int quiet)
990985
{
991986
int ret = 0;
992-
struct strbuf out = STRBUF_INIT;
993987
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
994988
struct child_process cp_add_i = CHILD_PROCESS_INIT;
995-
struct child_process cp_write_tree = CHILD_PROCESS_INIT;
996989
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
990+
struct index_state istate = { NULL };
997991

998992
remove_path(stash_index_path.buf);
999993

@@ -1019,17 +1013,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10191013
}
10201014

10211015
/* State of the working tree. */
1022-
cp_write_tree.git_cmd = 1;
1023-
argv_array_push(&cp_write_tree.args, "write-tree");
1024-
argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
1025-
stash_index_path.buf);
1026-
if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
1016+
if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1017+
NULL)) {
10271018
ret = -1;
10281019
goto done;
10291020
}
10301021

1031-
get_oid_hex(out.buf, &info->w_tree);
1032-
10331022
cp_diff_tree.git_cmd = 1;
10341023
argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
10351024
oid_to_hex(&info->w_tree), "--", NULL);
@@ -1045,7 +1034,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10451034
}
10461035

10471036
done:
1048-
strbuf_release(&out);
1037+
discard_index(&istate);
10491038
remove_path(stash_index_path.buf);
10501039
return ret;
10511040
}
@@ -1055,9 +1044,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10551044
int ret = 0;
10561045
struct rev_info rev;
10571046
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1058-
struct child_process cp_write_tree = CHILD_PROCESS_INIT;
1059-
struct strbuf out = STRBUF_INIT;
10601047
struct strbuf diff_output = STRBUF_INIT;
1048+
struct index_state istate = { NULL };
10611049

10621050
set_alternate_index_output(stash_index_path.buf);
10631051
if (reset_tree(&info->i_tree, 0, 0)) {
@@ -1096,20 +1084,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10961084
goto done;
10971085
}
10981086

1099-
cp_write_tree.git_cmd = 1;
1100-
argv_array_push(&cp_write_tree.args, "write-tree");
1101-
argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
1102-
stash_index_path.buf);
1103-
if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
1087+
if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1088+
NULL)) {
11041089
ret = -1;
11051090
goto done;
11061091
}
11071092

1108-
get_oid_hex(out.buf, &info->w_tree);
1109-
11101093
done:
1094+
discard_index(&istate);
11111095
UNLEAK(rev);
1112-
strbuf_release(&out);
11131096
object_array_clear(&rev.pending);
11141097
strbuf_release(&diff_output);
11151098
remove_path(stash_index_path.buf);

0 commit comments

Comments
 (0)