@@ -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
981976done :
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
10471036done :
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 init_revisions (& rev , NULL );
10631051
@@ -1097,20 +1085,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10971085 goto done ;
10981086 }
10991087
1100- cp_write_tree .git_cmd = 1 ;
1101- argv_array_push (& cp_write_tree .args , "write-tree" );
1102- argv_array_pushf (& cp_write_tree .env_array , "GIT_INDEX_FILE=%s" ,
1103- stash_index_path .buf );
1104- if (pipe_command (& cp_write_tree , NULL , 0 , & out , 0 ,NULL , 0 )) {
1088+ if (write_index_as_tree (& info -> w_tree , & istate , stash_index_path .buf , 0 ,
1089+ NULL )) {
11051090 ret = -1 ;
11061091 goto done ;
11071092 }
11081093
1109- get_oid_hex (out .buf , & info -> w_tree );
1110-
11111094done :
1095+ discard_index (& istate );
11121096 UNLEAK (rev );
1113- strbuf_release (& out );
11141097 object_array_clear (& rev .pending );
11151098 strbuf_release (& diff_output );
11161099 remove_path (stash_index_path .buf );
0 commit comments