@@ -943,9 +943,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
943943{
944944 int ret = 0 ;
945945 struct strbuf untracked_msg = STRBUF_INIT ;
946- struct strbuf out = STRBUF_INIT ;
947946 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 } ;
949948
950949 cp_upd_index .git_cmd = 1 ;
951950 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,
960959 goto done ;
961960 }
962961
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 )) {
968964 ret = -1 ;
969965 goto done ;
970966 }
971- get_oid_hex (out .buf , & info -> u_tree );
972967
973968 if (commit_tree (untracked_msg .buf , untracked_msg .len ,
974969 & 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,
977972 }
978973
979974done :
975+ discard_index (& istate );
980976 strbuf_release (& untracked_msg );
981- strbuf_release (& out );
982977 remove_path (stash_index_path .buf );
983978 return ret ;
984979}
@@ -987,11 +982,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
987982 struct strbuf * out_patch , int quiet )
988983{
989984 int ret = 0 ;
990- struct strbuf out = STRBUF_INIT ;
991985 struct child_process cp_read_tree = CHILD_PROCESS_INIT ;
992986 struct child_process cp_add_i = CHILD_PROCESS_INIT ;
993- struct child_process cp_write_tree = CHILD_PROCESS_INIT ;
994987 struct child_process cp_diff_tree = CHILD_PROCESS_INIT ;
988+ struct index_state istate = { NULL };
995989
996990 remove_path (stash_index_path .buf );
997991
@@ -1017,17 +1011,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10171011 }
10181012
10191013 /* 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 )) {
10251016 ret = -1 ;
10261017 goto done ;
10271018 }
10281019
1029- get_oid_hex (out .buf , & info -> w_tree );
1030-
10311020 cp_diff_tree .git_cmd = 1 ;
10321021 argv_array_pushl (& cp_diff_tree .args , "diff-tree" , "-p" , "HEAD" ,
10331022 oid_to_hex (& info -> w_tree ), "--" , NULL );
@@ -1043,7 +1032,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10431032 }
10441033
10451034done :
1046- strbuf_release ( & out );
1035+ discard_index ( & istate );
10471036 remove_path (stash_index_path .buf );
10481037 return ret ;
10491038}
@@ -1053,9 +1042,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10531042 int ret = 0 ;
10541043 struct rev_info rev ;
10551044 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 ;
10581045 struct strbuf diff_output = STRBUF_INIT ;
1046+ struct index_state istate = { NULL };
10591047
10601048 init_revisions (& rev , NULL );
10611049
@@ -1095,20 +1083,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10951083 goto done ;
10961084 }
10971085
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 )) {
11031088 ret = -1 ;
11041089 goto done ;
11051090 }
11061091
1107- get_oid_hex (out .buf , & info -> w_tree );
1108-
11091092done :
1093+ discard_index (& istate );
11101094 UNLEAK (rev );
1111- strbuf_release (& out );
11121095 object_array_clear (& rev .pending );
11131096 strbuf_release (& diff_output );
11141097 remove_path (stash_index_path .buf );
0 commit comments