Skip to content

Commit 5928e0b

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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15fcc3e commit 5928e0b

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

builtin/stash--helper.c

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
950950
{
951951
int ret = 0;
952952
struct strbuf untracked_msg = STRBUF_INIT;
953-
struct strbuf out = STRBUF_INIT;
954953
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
955-
struct child_process cp_write_tree = CHILD_PROCESS_INIT;
954+
struct index_state istate = { NULL };
956955

957956
cp_upd_index.git_cmd = 1;
958957
argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
@@ -967,15 +966,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
967966
goto done;
968967
}
969968

970-
cp_write_tree.git_cmd = 1;
971-
argv_array_push(&cp_write_tree.args, "write-tree");
972-
argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
973-
stash_index_path.buf);
974-
if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
969+
if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
970+
NULL)) {
975971
ret = -1;
976972
goto done;
977973
}
978-
get_oid_hex(out.buf, &info->u_tree);
979974

980975
if (commit_tree(untracked_msg.buf, untracked_msg.len,
981976
&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
@@ -984,8 +979,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
984979
}
985980

986981
done:
982+
discard_index(&istate);
987983
strbuf_release(&untracked_msg);
988-
strbuf_release(&out);
989984
remove_path(stash_index_path.buf);
990985
return ret;
991986
}
@@ -994,11 +989,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
994989
struct strbuf *out_patch, int quiet)
995990
{
996991
int ret = 0;
997-
struct strbuf out = STRBUF_INIT;
998992
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
999993
struct child_process cp_add_i = CHILD_PROCESS_INIT;
1000-
struct child_process cp_write_tree = CHILD_PROCESS_INIT;
1001994
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
995+
struct index_state istate = { NULL };
1002996

1003997
remove_path(stash_index_path.buf);
1004998

@@ -1024,17 +1018,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10241018
}
10251019

10261020
/* State of the working tree. */
1027-
cp_write_tree.git_cmd = 1;
1028-
argv_array_push(&cp_write_tree.args, "write-tree");
1029-
argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
1030-
stash_index_path.buf);
1031-
if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
1021+
if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1022+
NULL)) {
10321023
ret = -1;
10331024
goto done;
10341025
}
10351026

1036-
get_oid_hex(out.buf, &info->w_tree);
1037-
10381027
cp_diff_tree.git_cmd = 1;
10391028
argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
10401029
oid_to_hex(&info->w_tree), "--", NULL);
@@ -1050,7 +1039,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10501039
}
10511040

10521041
done:
1053-
strbuf_release(&out);
1042+
discard_index(&istate);
10541043
remove_path(stash_index_path.buf);
10551044
return ret;
10561045
}
@@ -1060,9 +1049,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10601049
int ret = 0;
10611050
struct rev_info rev;
10621051
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1063-
struct child_process cp_write_tree = CHILD_PROCESS_INIT;
1064-
struct strbuf out = STRBUF_INIT;
10651052
struct strbuf diff_output = STRBUF_INIT;
1053+
struct index_state istate = { NULL };
10661054

10671055
set_alternate_index_output(stash_index_path.buf);
10681056
if (reset_tree(&info->i_tree, 0, 0)) {
@@ -1101,20 +1089,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
11011089
goto done;
11021090
}
11031091

1104-
cp_write_tree.git_cmd = 1;
1105-
argv_array_push(&cp_write_tree.args, "write-tree");
1106-
argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
1107-
stash_index_path.buf);
1108-
if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
1092+
if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1093+
NULL)) {
11091094
ret = -1;
11101095
goto done;
11111096
}
11121097

1113-
get_oid_hex(out.buf, &info->w_tree);
1114-
11151098
done:
1099+
discard_index(&istate);
11161100
UNLEAK(rev);
1117-
strbuf_release(&out);
11181101
object_array_clear(&rev.pending);
11191102
strbuf_release(&diff_output);
11201103
remove_path(stash_index_path.buf);

0 commit comments

Comments
 (0)