@@ -95,26 +95,34 @@ static void print_new_head_line(struct commit *commit)
9595 printf ("\n" );
9696}
9797
98- static int update_index_refresh (void )
98+ static int update_index_refresh (int fd , struct lock_file * index_lock )
9999{
100- const char * argv_update_index [] = {"update-index" , "--refresh" , NULL };
101- return run_command_v_opt (argv_update_index , RUN_GIT_CMD );
102- }
100+ int result ;
101+
102+ if (!index_lock ) {
103+ index_lock = xcalloc (1 , sizeof (struct lock_file ));
104+ fd = hold_locked_index (index_lock , 1 );
105+ }
103106
104- struct update_cb_data {
105- int index_fd ;
106- struct lock_file * lock ;
107- int exit_code ;
108- };
107+ if (read_cache () < 0 )
108+ return error ("Could not read index" );
109+ result = refresh_cache (0 ) ? 1 : 0 ;
110+ if (write_cache (fd , active_cache , active_nr ) ||
111+ close (fd ) ||
112+ commit_locked_index (index_lock ))
113+ return error ("Could not refresh index" );
114+ return result ;
115+ }
109116
110117static void update_index_from_diff (struct diff_queue_struct * q ,
111118 struct diff_options * opt , void * data )
112119{
113120 int i ;
114- struct update_cb_data * cb = data ;
121+ int * discard_flag = data ;
115122
116123 /* do_diff_cache() mangled the index */
117124 discard_cache ();
125+ * discard_flag = 1 ;
118126 read_cache ();
119127
120128 for (i = 0 ; i < q -> nr ; i ++ ) {
@@ -128,34 +136,33 @@ static void update_index_from_diff(struct diff_queue_struct *q,
128136 } else
129137 remove_file_from_cache (one -> path );
130138 }
131-
132- cb -> exit_code = write_cache (cb -> index_fd , active_cache , active_nr ) ||
133- close (cb -> index_fd ) ||
134- commit_locked_index (cb -> lock );
135139}
136140
137141static int read_from_tree (const char * prefix , const char * * argv ,
138142 unsigned char * tree_sha1 )
139143{
144+ struct lock_file * lock = xcalloc (1 , sizeof (struct lock_file ));
145+ int index_fd , index_was_discarded = 0 ;
140146 struct diff_options opt ;
141- struct update_cb_data cb ;
142147
143148 memset (& opt , 0 , sizeof (opt ));
144149 diff_tree_setup_paths (get_pathspec (prefix , (const char * * )argv ), & opt );
145150 opt .output_format = DIFF_FORMAT_CALLBACK ;
146151 opt .format_callback = update_index_from_diff ;
147- opt .format_callback_data = & cb ;
152+ opt .format_callback_data = & index_was_discarded ;
148153
149- cb .lock = xcalloc (1 , sizeof (struct lock_file ));
150- cb .index_fd = hold_locked_index (cb .lock , 1 );
151- cb .exit_code = 0 ;
154+ index_fd = hold_locked_index (lock , 1 );
155+ index_was_discarded = 0 ;
152156 read_cache ();
153157 if (do_diff_cache (tree_sha1 , & opt ))
154158 return 1 ;
155159 diffcore_std (& opt );
156160 diff_flush (& opt );
157161
158- return cb .exit_code ;
162+ if (!index_was_discarded )
163+ /* The index is still clobbered from do_diff_cache() */
164+ discard_cache ();
165+ return update_index_refresh (index_fd , lock );
159166}
160167
161168static void prepend_reflog_action (const char * action , char * buf , size_t size )
@@ -225,9 +232,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
225232 else if (reset_type != NONE )
226233 die ("Cannot do %s reset with paths." ,
227234 reset_type_names [reset_type ]);
228- if (read_from_tree (prefix , argv + i , sha1 ))
229- return 1 ;
230- return update_index_refresh () ? 1 : 0 ;
235+ return read_from_tree (prefix , argv + i , sha1 );
231236 }
232237 if (reset_type == NONE )
233238 reset_type = MIXED ; /* by default */
@@ -264,7 +269,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
264269 case SOFT : /* Nothing else to do. */
265270 break ;
266271 case MIXED : /* Report what has not been updated. */
267- update_index_refresh ();
272+ update_index_refresh (0 , NULL );
268273 break ;
269274 }
270275
0 commit comments