Skip to content

Commit b4bd466

Browse files
ConradIrwingitster
authored andcommitted
Add support for -p/--patch to git-commit
The --interactive flag is already shared by git add and git commit, share the -p and --patch flags too. Signed-off-by: Conrad Irwin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e41fcfe commit b4bd466

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

Documentation/git-commit.txt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ git-commit - Record changes to the repository
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git commit' [-a | --interactive] [-s] [-v] [-u<mode>] [--amend] [--dry-run]
12-
[(-c | -C | --fixup | --squash) <commit>] [-F <file> | -m <msg>]
13-
[--reset-author] [--allow-empty] [--allow-empty-message] [--no-verify]
14-
[-e] [--author=<author>] [--date=<date>] [--cleanup=<mode>]
15-
[--status | --no-status] [-i | -o] [--] [<file>...]
11+
'git commit' [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
12+
[--dry-run] [(-c | -C | --fixup | --squash) <commit>]
13+
[-F <file> | -m <msg>] [--reset-author] [--allow-empty]
14+
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
15+
[--date=<date>] [--cleanup=<mode>] [--status | --no-status]
16+
[-i | -o] [--] [<file>...]
1617

1718
DESCRIPTION
1819
-----------
@@ -39,10 +40,10 @@ The content to be added can be specified in several ways:
3940
that have been removed from the working tree, and then perform the
4041
actual commit;
4142

42-
5. by using the --interactive switch with the 'commit' command to decide one
43-
by one which files should be part of the commit, before finalizing the
44-
operation. Currently, this is done by invoking 'git add --interactive'
45-
on a temporary index.
43+
5. by using the --interactive or --patch switches with the 'commit' command
44+
to decide one by one which files or hunks should be part of the commit,
45+
before finalizing the operation. See the ``Interactive Mode`` section of
46+
linkgit:git-add[1] to learn how to operate these modes.
4647

4748
The `--dry-run` option can be used to obtain a
4849
summary of what is included by any of the above for the next
@@ -60,6 +61,12 @@ OPTIONS
6061
been modified and deleted, but new files you have not
6162
told git about are not affected.
6263

64+
-p::
65+
--patch::
66+
Use the interactive patch selection interface to chose
67+
which changes to commit. See linkgit:git-add[1] for
68+
details.
69+
6370
-C <commit>::
6471
--reuse-message=<commit>::
6572
Take an existing commit object, and reuse the log message

builtin/add.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ int run_add_interactive(const char *revision, const char *patch_mode,
241241
return status;
242242
}
243243

244-
int interactive_add(int argc, const char **argv, const char *prefix)
244+
int interactive_add(int argc, const char **argv, const char *prefix, int patch)
245245
{
246246
const char **pathspec = NULL;
247247

@@ -252,7 +252,7 @@ int interactive_add(int argc, const char **argv, const char *prefix)
252252
}
253253

254254
return run_add_interactive(NULL,
255-
patch_interactive ? "--patch" : NULL,
255+
patch ? "--patch" : NULL,
256256
pathspec);
257257
}
258258

@@ -377,7 +377,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
377377
if (patch_interactive)
378378
add_interactive = 1;
379379
if (add_interactive)
380-
exit(interactive_add(argc - 1, argv + 1, prefix));
380+
exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
381381

382382
if (edit_interactive)
383383
return(edit_patch(argc, argv, prefix));

builtin/commit.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static const char *template_file;
8383
static const char *author_message, *author_message_buffer;
8484
static char *edit_message, *use_message;
8585
static 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;
8787
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
8888
static int no_post_rewrite, allow_empty_message;
8989
static 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"),
@@ -360,7 +361,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
360361
old_index_env = getenv(INDEX_ENVIRONMENT);
361362
setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
362363

363-
if (interactive_add(argc, argv, prefix) != 0)
364+
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
364365
die(_("interactive add failed"));
365366

366367
if (old_index_env && *old_index_env)
@@ -1061,8 +1062,11 @@ static int parse_and_validate_options(int argc, const char *argv[],
10611062
author_message_buffer = read_commit_message(author_message);
10621063
}
10631064

1065+
if (patch_interactive)
1066+
interactive = 1;
1067+
10641068
if (!!also + !!only + !!all + !!interactive > 1)
1065-
die(_("Only one of --include/--only/--all/--interactive can be used."));
1069+
die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
10661070
if (argc == 0 && (also || (only && !amend)))
10671071
die(_("No paths with --include/--only does not make sense."));
10681072
if (argc == 0 && only && amend)

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ extern struct commit_list *get_shallow_commits(struct object_array *heads,
159159
int is_descendant_of(struct commit *, struct commit_list *);
160160
int in_merge_bases(struct commit *, struct commit **, int);
161161

162-
extern int interactive_add(int argc, const char **argv, const char *prefix);
162+
extern int interactive_add(int argc, const char **argv, const char *prefix, int patch);
163163
extern int run_add_interactive(const char *revision, const char *patch_mode,
164164
const char **pathspec);
165165

0 commit comments

Comments
 (0)