@@ -952,9 +952,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
952952{
953953 int ret = 0 ;
954954 struct strbuf untracked_msg = STRBUF_INIT ;
955- struct strbuf out = STRBUF_INIT ;
956955 struct child_process cp_upd_index = CHILD_PROCESS_INIT ;
957- struct child_process cp_write_tree = CHILD_PROCESS_INIT ;
956+ struct index_state istate = { NULL } ;
958957
959958 cp_upd_index .git_cmd = 1 ;
960959 argv_array_pushl (& cp_upd_index .args , "update-index" , "-z" , "--add" ,
@@ -969,15 +968,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
969968 goto done ;
970969 }
971970
972- cp_write_tree .git_cmd = 1 ;
973- argv_array_push (& cp_write_tree .args , "write-tree" );
974- argv_array_pushf (& cp_write_tree .env_array , "GIT_INDEX_FILE=%s" ,
975- stash_index_path .buf );
976- if (pipe_command (& cp_write_tree , NULL , 0 , & out , 0 ,NULL , 0 )) {
971+ if (write_index_as_tree (& info -> u_tree , & istate , stash_index_path .buf , 0 ,
972+ NULL )) {
977973 ret = -1 ;
978974 goto done ;
979975 }
980- get_oid_hex (out .buf , & info -> u_tree );
981976
982977 if (commit_tree (untracked_msg .buf , untracked_msg .len ,
983978 & info -> u_tree , NULL , & info -> u_commit , NULL , NULL )) {
@@ -986,8 +981,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
986981 }
987982
988983done :
984+ discard_index (& istate );
989985 strbuf_release (& untracked_msg );
990- strbuf_release (& out );
991986 remove_path (stash_index_path .buf );
992987 return ret ;
993988}
@@ -996,11 +991,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
996991 struct strbuf * out_patch , int quiet )
997992{
998993 int ret = 0 ;
999- struct strbuf out = STRBUF_INIT ;
1000994 struct child_process cp_read_tree = CHILD_PROCESS_INIT ;
1001995 struct child_process cp_add_i = CHILD_PROCESS_INIT ;
1002- struct child_process cp_write_tree = CHILD_PROCESS_INIT ;
1003996 struct child_process cp_diff_tree = CHILD_PROCESS_INIT ;
997+ struct index_state istate = { NULL };
1004998
1005999 remove_path (stash_index_path .buf );
10061000
@@ -1026,17 +1020,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10261020 }
10271021
10281022 /* State of the working tree. */
1029- cp_write_tree .git_cmd = 1 ;
1030- argv_array_push (& cp_write_tree .args , "write-tree" );
1031- argv_array_pushf (& cp_write_tree .env_array , "GIT_INDEX_FILE=%s" ,
1032- stash_index_path .buf );
1033- if (pipe_command (& cp_write_tree , NULL , 0 , & out , 0 ,NULL , 0 )) {
1023+ if (write_index_as_tree (& info -> w_tree , & istate , stash_index_path .buf , 0 ,
1024+ NULL )) {
10341025 ret = -1 ;
10351026 goto done ;
10361027 }
10371028
1038- get_oid_hex (out .buf , & info -> w_tree );
1039-
10401029 cp_diff_tree .git_cmd = 1 ;
10411030 argv_array_pushl (& cp_diff_tree .args , "diff-tree" , "-p" , "HEAD" ,
10421031 oid_to_hex (& info -> w_tree ), "--" , NULL );
@@ -1052,7 +1041,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10521041 }
10531042
10541043done :
1055- strbuf_release ( & out );
1044+ discard_index ( & istate );
10561045 remove_path (stash_index_path .buf );
10571046 return ret ;
10581047}
@@ -1062,9 +1051,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10621051 int ret = 0 ;
10631052 struct rev_info rev ;
10641053 struct child_process cp_upd_index = CHILD_PROCESS_INIT ;
1065- struct child_process cp_write_tree = CHILD_PROCESS_INIT ;
1066- struct strbuf out = STRBUF_INIT ;
10671054 struct strbuf diff_output = STRBUF_INIT ;
1055+ struct index_state istate = { NULL };
10681056
10691057 set_alternate_index_output (stash_index_path .buf );
10701058 if (reset_tree (& info -> i_tree , 0 , 0 )) {
@@ -1103,20 +1091,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
11031091 goto done ;
11041092 }
11051093
1106- cp_write_tree .git_cmd = 1 ;
1107- argv_array_push (& cp_write_tree .args , "write-tree" );
1108- argv_array_pushf (& cp_write_tree .env_array , "GIT_INDEX_FILE=%s" ,
1109- stash_index_path .buf );
1110- if (pipe_command (& cp_write_tree , NULL , 0 , & out , 0 ,NULL , 0 )) {
1094+ if (write_index_as_tree (& info -> w_tree , & istate , stash_index_path .buf , 0 ,
1095+ NULL )) {
11111096 ret = -1 ;
11121097 goto done ;
11131098 }
11141099
1115- get_oid_hex (out .buf , & info -> w_tree );
1116-
11171100done :
1101+ discard_index (& istate );
11181102 UNLEAK (rev );
1119- strbuf_release (& out );
11201103 object_array_clear (& rev .pending );
11211104 strbuf_release (& diff_output );
11221105 remove_path (stash_index_path .buf );
0 commit comments