@@ -83,7 +83,7 @@ static const char *template_file;
8383static const char * author_message , * author_message_buffer ;
8484static char * edit_message , * use_message ;
8585static char * fixup_message , * squash_message ;
86- static int all , edit_flag , also , interactive , only , amend , signoff ;
86+ static int all , edit_flag , also , interactive , patch_interactive , only , amend , signoff ;
8787static int quiet , verbose , no_verify , allow_empty , dry_run , renew_authorship ;
8888static int no_post_rewrite , allow_empty_message ;
8989static char * untracked_files_arg , * force_date , * ignore_submodule_arg ;
@@ -152,6 +152,7 @@ static struct option builtin_commit_options[] = {
152152 OPT_BOOLEAN ('a' , "all" , & all , "commit all changed files" ),
153153 OPT_BOOLEAN ('i' , "include" , & also , "add specified files to index for commit" ),
154154 OPT_BOOLEAN (0 , "interactive" , & interactive , "interactively add files" ),
155+ OPT_BOOLEAN ('p' , "patch" , & patch_interactive , "interactively add changes" ),
155156 OPT_BOOLEAN ('o' , "only" , & only , "commit only specified files" ),
156157 OPT_BOOLEAN ('n' , "no-verify" , & no_verify , "bypass pre-commit hook" ),
157158 OPT_BOOLEAN (0 , "dry-run" , & dry_run , "show what would be committed" ),
@@ -336,25 +337,45 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
336337 int fd ;
337338 struct string_list partial ;
338339 const char * * pathspec = NULL ;
340+ char * old_index_env = NULL ;
339341 int refresh_flags = REFRESH_QUIET ;
340342
341343 if (is_status )
342344 refresh_flags |= REFRESH_UNMERGED ;
343- if (interactive ) {
344- if (interactive_add (argc , argv , prefix ) != 0 )
345- die (_ ("interactive add failed" ));
346- if (read_cache_preload (NULL ) < 0 )
347- die (_ ("index file corrupt" ));
348- commit_style = COMMIT_AS_IS ;
349- return get_index_file ();
350- }
351345
352346 if (* argv )
353347 pathspec = get_pathspec (prefix , argv );
354348
355349 if (read_cache_preload (pathspec ) < 0 )
356350 die (_ ("index file corrupt" ));
357351
352+ if (interactive ) {
353+ fd = hold_locked_index (& index_lock , 1 );
354+
355+ refresh_cache_or_die (refresh_flags );
356+
357+ if (write_cache (fd , active_cache , active_nr ) ||
358+ close_lock_file (& index_lock ))
359+ die (_ ("unable to create temporary index" ));
360+
361+ old_index_env = getenv (INDEX_ENVIRONMENT );
362+ setenv (INDEX_ENVIRONMENT , index_lock .filename , 1 );
363+
364+ if (interactive_add (argc , argv , prefix , patch_interactive ) != 0 )
365+ die (_ ("interactive add failed" ));
366+
367+ if (old_index_env && * old_index_env )
368+ setenv (INDEX_ENVIRONMENT , old_index_env , 1 );
369+ else
370+ unsetenv (INDEX_ENVIRONMENT );
371+
372+ discard_cache ();
373+ read_cache_from (index_lock .filename );
374+
375+ commit_style = COMMIT_NORMAL ;
376+ return index_lock .filename ;
377+ }
378+
358379 /*
359380 * Non partial, non as-is commit.
360381 *
@@ -1043,8 +1064,11 @@ static int parse_and_validate_options(int argc, const char *argv[],
10431064 author_message_buffer = read_commit_message (author_message );
10441065 }
10451066
1067+ if (patch_interactive )
1068+ interactive = 1 ;
1069+
10461070 if (!!also + !!only + !!all + !!interactive > 1 )
1047- die (_ ("Only one of --include/--only/--all/--interactive can be used." ));
1071+ die (_ ("Only one of --include/--only/--all/--interactive/--patch can be used." ));
10481072 if (argc == 0 && (also || (only && !amend )))
10491073 die (_ ("No paths with --include/--only does not make sense." ));
10501074 if (argc == 0 && only && amend )
@@ -1066,8 +1090,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
10661090
10671091 if (all && argc > 0 )
10681092 die (_ ("Paths with -a does not make sense." ));
1069- else if (interactive && argc > 0 )
1070- die (_ ("Paths with --interactive does not make sense." ));
10711093
10721094 if (null_termination && status_format == STATUS_FORMAT_LONG )
10731095 status_format = STATUS_FORMAT_PORCELAIN ;
0 commit comments