From: Thomas Rast <trast@student.ethz.ch>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Pierre Habouzit <madcoder@debian.org>
Subject: [RFC PATCH v2 3/3] Implement unstage --patch and discard --patch
Date: Fri, 24 Jul 2009 18:24:36 +0200 [thread overview]
Message-ID: <9cad6176fe941bd56bad7fed49cc55a96a7040ae.1248452530.git.trast@student.ethz.ch> (raw)
In-Reply-To: <2d756f643fc7d1b50cf590c95467fa029b49ff46.1248426652.git.trast@student.ethz.ch>
This implements a new optional argument --patch=<mode> in the
git-add--interactive helper. The modes are:
'stage' (default, as before): offer hunks from 'git diff' and stage
the ones selected by the user.
'unstage': offer hunks from 'git diff --cached', and unstage the ones
selected.
'discard': offer hunks from 'git diff', and discard (i.e., undo the
edit in the working tree) the ones selected.
With this in hand, we can then easily add a --patch option to the
git-unstage and git-discard scripts that run git-add--interactive in
the corresponding modes.
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
Documentation/git-discard.txt | 7 ++++
Documentation/git-unstage.txt | 7 ++++
git-add--interactive.perl | 69 +++++++++++++++++++++++++++++++---------
git-discard.sh | 9 ++++-
git-unstage.sh | 9 ++++-
5 files changed, 81 insertions(+), 20 deletions(-)
diff --git a/Documentation/git-discard.txt b/Documentation/git-discard.txt
index 4db14f0..2c063d1 100644
--- a/Documentation/git-discard.txt
+++ b/Documentation/git-discard.txt
@@ -24,6 +24,13 @@ lost.
(This is the same as `git checkout \-- <paths>`.)
+OPTIONS
+-------
+-p::
+--patch::
+ Interactively select hunks to discard.
+
+
SEE ALSO
--------
linkgit:git-checkout[1]
diff --git a/Documentation/git-unstage.txt b/Documentation/git-unstage.txt
index 49d09fb..d3da3e1 100644
--- a/Documentation/git-unstage.txt
+++ b/Documentation/git-unstage.txt
@@ -21,6 +21,13 @@ is not affected. (This is the same as `git reset \-- <paths>` unless
you are on an unborn branch.)
+OPTIONS
+-------
+-p::
+--patch::
+ Interactively select hunks to unstage.
+
+
SEE ALSO
--------
linkgit:git-reset[1]
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index df9f231..502a0e4 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -73,6 +73,30 @@
# command line options
my $patch_mode;
+my %patch_modes = (
+ 'stage' => {
+ DIFF => 'diff-files -p',
+ APPLY => 'apply --cached',
+ VERB => 'Stage',
+ PARTICIPLE => 'Staging',
+ FILTER => 'file-only',
+ },
+ 'unstage' => {
+ DIFF => 'diff-index -p --cached HEAD',
+ APPLY => 'apply -R --cached',
+ VERB => 'UNstage',
+ PARTICIPLE => 'UNstaging',
+ FILTER => 'index-only',
+ },
+ 'discard' => {
+ DIFF => 'diff-files -p',
+ APPLY => 'apply -R',
+ VERB => 'RESET',
+ PARTICIPLE => 'RESETTING',
+ FILTER => 'file-only',
+ },
+);
+
sub run_cmd_pipe {
if ($^O eq 'MSWin32' || $^O eq 'msys') {
my @invalid = grep {m/[":*]/} @_;
@@ -615,10 +639,11 @@
sub parse_diff {
my ($path) = @_;
- my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
+ my @diff_cmd = split(" ", $patch_modes{$patch_mode}{DIFF});
+ my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
my @colored = ();
if ($diff_use_color) {
- @colored = run_cmd_pipe(qw(git diff-files -p --color --), $path);
+ @colored = run_cmd_pipe("git", @diff_cmd, qw(--color --), $path);
}
my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
@@ -877,6 +902,7 @@
or die "failed to open hunk edit file for writing: " . $!;
print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
print $fh @$oldtext;
+ my $participle = $patch_modes{$patch_mode}{PARTICIPLE};
print $fh <<EOF;
# ---
# To remove '-' lines, make them ' ' lines (context).
@@ -884,7 +910,7 @@
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
-# marked for staging. If it does not apply cleanly, you will be given
+# marked for $participle. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.
EOF
@@ -918,7 +944,7 @@
sub diff_applies {
my $fh;
- open $fh, '| git apply --recount --cached --check';
+ open $fh, '| git '.$patch_modes{$patch_mode}{APPLY}.' --recount --check';
for my $h (@_) {
print $fh @{$h->{TEXT}};
}
@@ -988,12 +1014,13 @@
}
sub help_patch_cmd {
- print colored $help_color, <<\EOF ;
-y - stage this hunk
-n - do not stage this hunk
-q - quit, do not stage this hunk nor any of the remaining ones
-a - stage this and all the remaining hunks in the file
-d - do not stage this hunk nor any of the remaining hunks in the file
+ my $verb = lc $patch_modes{$patch_mode}{VERB};
+ print colored $help_color, <<EOF ;
+y - $verb this hunk
+n - do not $verb this hunk
+q - quit, do not $verb this hunk nor any of the remaining ones
+a - $verb this and all the remaining hunks in the file
+d - do not $verb this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
@@ -1007,7 +1034,11 @@
}
sub patch_update_cmd {
- my @all_mods = list_modified('file-only');
+ my $filter = 'file-only';
+ if ($patch_mode) {
+ $filter = $patch_modes{$patch_mode}{FILTER};
+ }
+ my @all_mods = list_modified($filter);
my @mods = grep { !($_->{BINARY}) } @all_mods;
my @them;
@@ -1138,8 +1169,8 @@
for (@{$hunk[$ix]{DISPLAY}}) {
print;
}
- print colored $prompt_color, 'Stage ',
- ($hunk[$ix]{TYPE} eq 'mode' ? 'mode change' : 'this hunk'),
+ print colored $prompt_color, $patch_modes{$patch_mode}{VERB},
+ ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' : ' this hunk'),
" [y,n,q,a,d,/$other,?]? ";
my $line = prompt_single_character;
if ($line) {
@@ -1314,7 +1345,7 @@
if (@result) {
my $fh;
- open $fh, '| git apply --cached --recount';
+ open $fh, '| git '.$patch_modes{$patch_mode}{APPLY}.' --recount';
for (@{$head->{TEXT}}, @result) {
print $fh $_;
}
@@ -1363,8 +1394,14 @@
sub process_args {
return unless @ARGV;
my $arg = shift @ARGV;
- if ($arg eq "--patch") {
- $patch_mode = 1;
+ if ($arg =~ /--patch(?:=(.*))?/) {
+ if (defined $1 && defined $patch_modes{$1}) {
+ $patch_mode = $1;
+ } elsif (defined $1) {
+ die "unknown --patch mode: $1";
+ } else {
+ $patch_mode = 'stage';
+ }
$arg = shift @ARGV or die "missing --";
die "invalid argument $arg, expecting --"
unless $arg eq "--";
diff --git a/git-discard.sh b/git-discard.sh
index 595df98..628346d 100644
--- a/git-discard.sh
+++ b/git-discard.sh
@@ -3,13 +3,18 @@
SUBDIRECTORY_OK=Yes
OPTIONS_KEEPDASHDASH=
OPTIONS_SPEC="\
-git discard file ...
---"
+git discard [-p] file ...
+--
+p,patch interactively select hunks to discard"
. git-sh-setup
case "$1" in
+ -p|--patch)
+ shift
+ exec git add--interactive --patch=discard "$@"
+ ;;
--)
if [ $# -eq 1 ]; then
die "You must specify at least one file to discard changes from"
diff --git a/git-unstage.sh b/git-unstage.sh
index 7f99adf..921e12b 100644
--- a/git-unstage.sh
+++ b/git-unstage.sh
@@ -3,13 +3,18 @@
SUBDIRECTORY_OK=Yes
OPTIONS_KEEPDASHDASH=
OPTIONS_SPEC="\
-git unstage file ...
---"
+git unstage [-p] file ...
+--
+p,patch interactively select hunks to unstage"
. git-sh-setup
case "$1" in
+ -p|--patch)
+ shift
+ exec git add--interactive --patch=unstage "$@"
+ ;;
--)
if [ $# -eq 1 ]; then
die "You must specify at least one file to unstage"
--
1.6.4.rc2.217.g74c0b.dirty
next prev parent reply other threads:[~2009-07-24 16:25 UTC|newest]
Thread overview: 76+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-23 7:41 [PATCH] git-add -p: be able to undo a given hunk Pierre Habouzit
2009-07-23 8:41 ` Thomas Rast
2009-07-23 8:50 ` Pierre Habouzit
2009-07-24 9:15 ` [RFC PATCH] Implement unstage and reset modes for git-add--interactive Thomas Rast
2009-07-24 16:24 ` [RFC PATCH v2 1/3] Introduce git-unstage Thomas Rast
2009-07-24 17:59 ` Bert Wesarg
2009-07-24 18:02 ` Bert Wesarg
2009-07-24 18:23 ` Elijah Newren
2009-07-24 16:24 ` [RFC PATCH v2 2/3] Introduce git-discard Thomas Rast
2009-07-24 18:02 ` Elijah Newren
2009-07-24 18:12 ` Bert Wesarg
2009-07-24 18:24 ` Elijah Newren
2009-07-25 14:58 ` Pierre Habouzit
2009-07-24 16:24 ` Thomas Rast [this message]
2009-07-24 16:40 ` [RFC PATCH v2 3/3] Implement unstage --patch and discard --patch Matthias Kestenholz
2009-07-24 18:08 ` Bert Wesarg
2009-07-24 16:39 ` [RFC PATCH] Implement unstage and reset modes for git-add--interactive Junio C Hamano
2009-07-24 21:58 ` Nanako Shiraishi
2009-07-24 23:17 ` Thomas Rast
2009-07-24 23:25 ` Junio C Hamano
2009-07-25 21:29 ` [RFC PATCH v3 0/5] {checkout,reset,stash} --patch Thomas Rast
2009-07-25 21:29 ` [RFC PATCH v3 1/5] git-apply--interactive: Refactor patch mode code Thomas Rast
2009-07-25 21:29 ` [RFC PATCH v3 2/5] builtin-add: refactor the meat of interactive_add() Thomas Rast
2009-07-25 21:29 ` [RFC PATCH v3 3/5] Implement 'git reset --patch' Thomas Rast
2009-07-25 21:29 ` [RFC PATCH v3 4/5] Implement 'git checkout --patch' Thomas Rast
2009-07-25 21:29 ` [RFC PATCH v3 5/5] Implement 'git stash save --patch' Thomas Rast
2009-07-26 6:03 ` Sverre Rabbelier
2009-07-26 8:45 ` Thomas Rast
2009-07-27 10:10 ` [RFC PATCH v3 0/5] {checkout,reset,stash} --patch Thomas Rast
2009-07-28 21:20 ` [PATCH v4 " Thomas Rast
2009-07-28 21:20 ` [PATCH v4 1/5] git-apply--interactive: Refactor patch mode code Thomas Rast
2009-07-28 21:20 ` [PATCH v4 2/5] builtin-add: refactor the meat of interactive_add() Thomas Rast
2009-07-28 21:20 ` [PATCH v4 3/5] Implement 'git reset --patch' Thomas Rast
2009-07-28 21:20 ` [PATCH v4 4/5] Implement 'git checkout --patch' Thomas Rast
2009-07-28 21:20 ` [PATCH v4 5/5] Implement 'git stash save --patch' Thomas Rast
2009-07-28 21:20 ` [PATCH v4 6/5] DWIM 'git stash save -p' for 'git stash -p' Thomas Rast
2009-08-09 6:52 ` [PATCH v4 0/5] {checkout,reset,stash} --patch Jeff King
2009-08-09 9:17 ` Thomas Rast
2009-08-09 16:32 ` [PATCH v4 0/5] " Nicolas Sebrecht
2009-08-09 16:44 ` Thomas Rast
2009-08-09 21:28 ` Nicolas Sebrecht
2009-08-09 21:42 ` Thomas Rast
2009-08-09 22:26 ` Nicolas Sebrecht
2009-08-10 9:36 ` Thomas Rast
2009-08-13 12:29 ` [PATCH v5 0/6] " Thomas Rast
2009-08-13 12:29 ` [PATCH v5 1/6] git-apply--interactive: Refactor patch mode code Thomas Rast
2009-08-13 12:29 ` [PATCH v5 2/6] Add a small patch-mode testing library Thomas Rast
2009-08-13 12:29 ` [PATCH v5 3/6] builtin-add: refactor the meat of interactive_add() Thomas Rast
2009-08-13 12:29 ` [PATCH v5 4/6] Implement 'git reset --patch' Thomas Rast
2009-08-15 11:48 ` [PATCH v5.1 " Thomas Rast
2009-08-13 12:29 ` [PATCH v5 5/6] Implement 'git checkout --patch' Thomas Rast
2009-08-15 11:48 ` [PATCH v5.1 " Thomas Rast
2009-08-13 12:29 ` [PATCH v5 6/6] Implement 'git stash save --patch' Thomas Rast
2009-08-13 12:29 ` [PATCH v5 7/6] DWIM 'git stash save -p' for 'git stash -p' Thomas Rast
2009-08-14 20:57 ` [PATCH v5 0/6] Re: {checkout,reset,stash} --patch Nicolas Sebrecht
2009-08-15 6:51 ` [PATCH v5 0/6] " Jeff King
2009-08-15 7:57 ` Junio C Hamano
2009-08-15 10:14 ` Thomas Rast
2009-08-15 10:04 ` Thomas Rast
2009-08-18 16:48 ` Jeff King
2009-08-19 9:40 ` Thomas Rast
2009-08-19 10:11 ` Jeff King
2009-07-23 19:58 ` [PATCH] git-add -p: be able to undo a given hunk Junio C Hamano
2009-07-24 10:32 ` Nanako Shiraishi
2009-07-24 16:06 ` Junio C Hamano
2009-07-24 17:06 ` Jeff King
2009-07-25 0:54 ` Junio C Hamano
2009-07-25 9:35 ` Thomas Rast
2009-07-25 14:48 ` Pierre Habouzit
2009-07-25 14:52 ` Pierre Habouzit
2009-07-26 15:39 ` Jeff King
2009-07-27 8:26 ` Pierre Habouzit
2009-07-27 10:30 ` Jeff King
2009-07-27 10:06 ` Thomas Rast
2009-07-27 10:36 ` Jeff King
2009-07-24 14:58 ` Pierre Habouzit
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9cad6176fe941bd56bad7fed49cc55a96a7040ae.1248452530.git.trast@student.ethz.ch \
--to=trast@student.ethz.ch \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=madcoder@debian.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).