Add "--patch" option to git-add--interactive
When the "--patch" option is supplied, the patch_update_cmd() function is called bypassing the main_loop() and exits. Seeing as builtin-add is the only caller of git-add--interactive we can impose a strict requirement on the format of the arguments to avoid possible ambiguity: an "--" argument must be used whenever any pathspecs are passed, both with the "--patch" option and without it. Signed-off-by: Wincent Colaiuta <win@wincent.com>
Wincent Colaiuta committed
Nov 25, 2007 at 14:15 UTC
b63e99500137c913bd801a2f22b6cf88c63b95c5
3 files changed
+65
-19
Documentation/git-add.txt
+8
-1
@@ -61,7 +61,14 @@ OPTIONS
61
62
-i, \--interactive::
63
Add modified contents in the working tree interactively to
64
- the index.
64
+ the index. Optional path arguments may be supplied to limit
65
+ operation to a subset of the working tree. See ``Interactive
66
+ mode'' for details.
67
+
68
+-p, \--patch:
69
+ Similar to Interactive mode but the initial command loop is
70
+ bypassed and the 'patch' subcommand is invoked using each of
71
+ the specified filepatterns before exiting.
72
73
-u::
74
Update only files that git already knows about. This is similar
builtin-add.c
+16
-8
@@ -19,7 +19,7 @@ static const char * const builtin_add_usage[] = {
19
"git-add [options] [--] <filepattern>...",
20
NULL
21
};
22
-
22
+static int patch_interactive = 0, add_interactive = 0;
23
static int take_worktree_changes;
24
25
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
@@ -144,7 +144,7 @@ static const char **validate_pathspec(int argc, const char **argv, const char *p
144
145
int interactive_add(int argc, const char **argv, const char *prefix)
146
{
147
- int status;
147
+ int status, ac;
148
const char **args;
149
const char **pathspec = NULL;
150
@@ -154,11 +154,17 @@ int interactive_add(int argc, const char **argv, const char *prefix)
154
return -1;
155
}
156
157
- args = xcalloc(sizeof(const char *), (argc + 2));
158
- args[0] = "add--interactive";
159
- if (argc)
160
- memcpy(&(args[1]), pathspec, sizeof(const char *) * argc);
161
- args[argc + 1] = NULL;
157
+ args = xcalloc(sizeof(const char *), (argc + 4));
158
+ ac = 0;
159
+ args[ac++] = "add--interactive";
160
+ if (patch_interactive)
161
+ args[ac++] = "--patch";
162
+ args[ac++] = "--";
163
+ if (argc) {
164
+ memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
165
+ ac += argc;
166
+ }
167
+ args[ac] = NULL;
168
169
status = run_command_v_opt(args, RUN_GIT_CMD);
170
free(args);
@@ -171,13 +177,13 @@ static const char ignore_error[] =
177
"The following paths are ignored by one of your .gitignore files:\n";
178
179
static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
174
-static int add_interactive = 0;
180
181
static struct option builtin_add_options[] = {
182
OPT__DRY_RUN(&show_only),
183
OPT__VERBOSE(&verbose),
184
OPT_GROUP(""),
185
OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
186
+ OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
187
OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
188
OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
189
OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
@@ -192,6 +198,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
198
199
argc = parse_options(argc, argv, builtin_add_options,
200
builtin_add_usage, 0);
201
+ if (patch_interactive)
202
+ add_interactive = 1;
203
if (add_interactive)
204
exit(interactive_add(argc, argv, prefix));
205
git-add--interactive.perl
+41
-10
@@ -2,6 +2,9 @@
2
3
use strict;
4
5
+# command line options
6
+my $patch_mode;
7
+
8
sub run_cmd_pipe {
9
if ($^O eq 'MSWin32') {
10
my @invalid = grep {m/[":*]/} @_;
@@ -552,8 +555,8 @@ sub help_patch_cmd {
555
print <<\EOF ;
556
y - stage this hunk
557
n - do not stage this hunk
555
-a - stage this and all the remaining hunks
556
-d - do not stage this hunk nor any of the remaining hunks
558
+a - stage this and all the remaining hunks in the file
559
+d - do not stage this hunk nor any of the remaining hunks in the file
560
j - leave this hunk undecided, see next undecided hunk
561
J - leave this hunk undecided, see next hunk
562
k - leave this hunk undecided, see previous undecided hunk
@@ -563,13 +566,21 @@ EOF
566
}
567
568
sub patch_update_cmd {
566
- my @mods = list_modified('file-only');
567
- @mods = grep { !($_->{BINARY}) } @mods;
568
- return if (!@mods);
569
+ my @mods = grep { !($_->{BINARY}) } list_modified('file-only');
570
+ my @them;
571
570
- my (@them) = list_and_choose({ PROMPT => 'Patch update',
571
- HEADER => $status_head, },
572
- @mods);
572
+ if (!@mods) {
573
+ print STDERR "No changes.\n";
574
+ return 0;
575
+ }
576
+ if ($patch_mode) {
577
+ @them = @mods;
578
+ }
579
+ else {
580
+ @them = list_and_choose({ PROMPT => 'Patch update',
581
+ HEADER => $status_head, },
582
+ @mods);
583
+ }
584
for (@them) {
585
patch_update_file($_->{VALUE});
586
}
@@ -783,6 +794,20 @@ add untracked - add contents of untracked files to the staged set of changes
794
EOF
795
}
796
797
+sub process_args {
798
+ return unless @ARGV;
799
+ my $arg = shift @ARGV;
800
+ if ($arg eq "--patch") {
801
+ $patch_mode = 1;
802
+ $arg = shift @ARGV or die "missing --";
803
+ die "invalid argument $arg, expecting --"
804
+ unless $arg eq "--";
805
+ }
806
+ elsif ($arg ne "--") {
807
+ die "invalid argument $arg, expecting --";
808
+ }
809
+}
810
+
811
sub main_loop {
812
my @cmd = ([ 'status', \&status_cmd, ],
813
[ 'update', \&update_cmd, ],
@@ -811,6 +836,12 @@ sub main_loop {
836
}
837
}
838
839
+process_args();
840
refresh();
815
-status_cmd();
816
-main_loop();
841
+if ($patch_mode) {
842
+ patch_update_cmd();
843
+}
844
+else {
845
+ status_cmd();
846
+ main_loop();
847
+}