@@ -116,32 +116,42 @@ static void update_index_from_diff(struct diff_queue_struct *q,
116116 struct diff_options * opt , void * data )
117117{
118118 int i ;
119+ int intent_to_add = * (int * )data ;
119120
120121 for (i = 0 ; i < q -> nr ; i ++ ) {
121122 struct diff_filespec * one = q -> queue [i ]-> one ;
122- if (one -> mode && !is_null_sha1 (one -> sha1 )) {
123- struct cache_entry * ce ;
124- ce = make_cache_entry (one -> mode , one -> sha1 , one -> path ,
125- 0 , 0 );
126- if (!ce )
127- die (_ ("make_cache_entry failed for path '%s'" ),
128- one -> path );
129- add_cache_entry (ce , ADD_CACHE_OK_TO_ADD |
130- ADD_CACHE_OK_TO_REPLACE );
131- } else
123+ int is_missing = !(one -> mode && !is_null_sha1 (one -> sha1 ));
124+ struct cache_entry * ce ;
125+
126+ if (is_missing && !intent_to_add ) {
132127 remove_file_from_cache (one -> path );
128+ continue ;
129+ }
130+
131+ ce = make_cache_entry (one -> mode , one -> sha1 , one -> path ,
132+ 0 , 0 );
133+ if (!ce )
134+ die (_ ("make_cache_entry failed for path '%s'" ),
135+ one -> path );
136+ if (is_missing ) {
137+ ce -> ce_flags |= CE_INTENT_TO_ADD ;
138+ set_object_name_for_intent_to_add_entry (ce );
139+ }
140+ add_cache_entry (ce , ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE );
133141 }
134142}
135143
136144static int read_from_tree (const struct pathspec * pathspec ,
137- unsigned char * tree_sha1 )
145+ unsigned char * tree_sha1 ,
146+ int intent_to_add )
138147{
139148 struct diff_options opt ;
140149
141150 memset (& opt , 0 , sizeof (opt ));
142151 copy_pathspec (& opt .pathspec , pathspec );
143152 opt .output_format = DIFF_FORMAT_CALLBACK ;
144153 opt .format_callback = update_index_from_diff ;
154+ opt .format_callback_data = & intent_to_add ;
145155
146156 if (do_diff_cache (tree_sha1 , & opt ))
147157 return 1 ;
@@ -258,6 +268,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
258268 const char * rev ;
259269 unsigned char sha1 [20 ];
260270 struct pathspec pathspec ;
271+ int intent_to_add = 0 ;
261272 const struct option options [] = {
262273 OPT__QUIET (& quiet , N_ ("be quiet, only report errors" )),
263274 OPT_SET_INT (0 , "mixed" , & reset_type ,
@@ -270,6 +281,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
270281 OPT_SET_INT (0 , "keep" , & reset_type ,
271282 N_ ("reset HEAD but keep local changes" ), KEEP ),
272283 OPT_BOOL ('p' , "patch" , & patch_mode , N_ ("select hunks interactively" )),
284+ OPT_BOOL ('N' , "intent-to-add" , & intent_to_add ,
285+ N_ ("record only the fact that removed paths will be added later" )),
273286 OPT_END ()
274287 };
275288
@@ -327,6 +340,9 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
327340 die (_ ("%s reset is not allowed in a bare repository" ),
328341 _ (reset_type_names [reset_type ]));
329342
343+ if (intent_to_add && reset_type != MIXED )
344+ die (_ ("-N can only be used with --mixed" ));
345+
330346 /* Soft reset does not touch the index file nor the working tree
331347 * at all, but requires them in a good order. Other resets reset
332348 * the index file to the tree object we are switching to. */
@@ -338,7 +354,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
338354 int newfd = hold_locked_index (lock , 1 );
339355 if (reset_type == MIXED ) {
340356 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN ;
341- if (read_from_tree (& pathspec , sha1 ))
357+ if (read_from_tree (& pathspec , sha1 , intent_to_add ))
342358 return 1 ;
343359 refresh_index (& the_index , flags , NULL , NULL ,
344360 _ ("Unstaged changes after reset:" ));
0 commit comments