From: Thomas Rast <trast@student.ethz.ch>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Pierre Habouzit <madcoder@debian.org>,
Nanako Shiraishi <nanako3@lavabit.com>
Subject: [RFC PATCH v3 4/5] Implement 'git checkout --patch'
Date: Sat, 25 Jul 2009 23:29:33 +0200 [thread overview]
Message-ID: <93da49abd5fc514906bac73553d6802eb7fb69fd.1248557241.git.trast@student.ethz.ch> (raw)
In-Reply-To: <cover.1248557241.git.trast@student.ethz.ch>
This introduces a --patch mode for git-checkout. In the index usage
git checkout --patch -- [files...]
it lets the user discard edits from the <files> at the granularity of
hunks (by selecting hunks from 'git diff' and then reverse applying
them to the worktree).
We also accept a revision argument
git checkout --patch <revision> -- [files...]
which offers hunks from the difference between the <revision> and the
worktree. The chosen hunks are then reverse applied to both index and
worktree, discarding them completely. This application is done
"atomically" in the sense that we first check if the patch applies to
the index (it should always apply to the worktree). If it does not,
we give the user a choice to either abort or apply to the worktree
anyway.
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
Documentation/git-checkout.txt | 13 ++++++++++-
builtin-checkout.c | 19 +++++++++++++++
git-add--interactive.perl | 48 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 79 insertions(+), 1 deletions(-)
diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt
index ad4b31e..26a5447 100644
--- a/Documentation/git-checkout.txt
+++ b/Documentation/git-checkout.txt
@@ -11,6 +11,7 @@ SYNOPSIS
'git checkout' [-q] [-f] [-m] [<branch>]
'git checkout' [-q] [-f] [-m] [-b <new_branch>] [<start_point>]
'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
+'git checkout' --patch [<tree-ish>] [--] [<paths>...]
DESCRIPTION
-----------
@@ -25,7 +26,7 @@ use the --track or --no-track options, which will be passed to `git
branch`. As a convenience, --track without `-b` implies branch
creation; see the description of --track below.
-When <paths> are given, this command does *not* switch
+When <paths> or --patch are given, this command does *not* switch
branches. It updates the named paths in the working tree from
the index file, or from a named <tree-ish> (most often a commit). In
this case, the `-b` and `--track` options are meaningless and giving
@@ -113,6 +114,16 @@ the conflicted merge in the specified paths.
"merge" (default) and "diff3" (in addition to what is shown by
"merge" style, shows the original contents).
+-p::
+--patch::
+ Interactively select hunks in the difference between the
+ <tree-ish> (or the index, if unspecified) and the working
+ tree. The chosen hunks are then applied in reverse to the
+ working tree (and if a <tree-ish> was specified, the index).
++
+This means that you can use `git checkout -p` to selectively discard
+edits from your current working tree.
+
<branch>::
Branch to checkout; if it refers to a branch (i.e., a name that,
when prepended with "refs/heads/", is a valid ref), then that
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 446cac7..7d57741 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -572,6 +572,13 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
return git_xmerge_config(var, value, cb);
}
+static int interactive_checkout(const char *revision, const char **pathspec,
+ struct checkout_opts *opts)
+{
+ return run_add_interactive(revision, "--patch=checkout", pathspec);
+}
+
+
int cmd_checkout(int argc, const char **argv, const char *prefix)
{
struct checkout_opts opts;
@@ -580,6 +587,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
struct branch_info new;
struct tree *source_tree = NULL;
char *conflict_style = NULL;
+ int patch_mode = 0;
struct option options[] = {
OPT__QUIET(&opts.quiet),
OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
@@ -594,6 +602,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
OPT_BOOLEAN('m', "merge", &opts.merge, "merge"),
OPT_STRING(0, "conflict", &conflict_style, "style",
"conflict style (merge or diff3)"),
+ OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"),
OPT_END(),
};
int has_dash_dash;
@@ -608,6 +617,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options, checkout_usage,
PARSE_OPT_KEEP_DASHDASH);
+ if (patch_mode && (opts.track > 0 || opts.new_branch
+ || opts.new_branch_log || opts.merge || opts.force))
+ die ("--patch is incompatible with all other options");
+
/* --track without -b should DWIM */
if (0 < opts.track && !opts.new_branch) {
const char *argv0 = argv[0];
@@ -714,6 +727,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
if (!pathspec)
die("invalid path specification");
+ if (patch_mode)
+ return interactive_checkout(NULL, pathspec, &opts);
+
/* Checkout paths */
if (opts.new_branch) {
if (argc == 1) {
@@ -729,6 +745,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
return checkout_paths(source_tree, pathspec, &opts);
}
+ if (patch_mode)
+ return interactive_checkout(new.name, NULL, &opts);
+
if (opts.new_branch) {
struct strbuf buf = STRBUF_INIT;
if (strbuf_check_branch_ref(&buf, opts.new_branch))
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index b1aa846..5005a8d 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -75,6 +75,7 @@
my $patch_mode_revision;
sub apply_patch;
+sub apply_patch_for_checkout_commit;
my %patch_modes = (
'stage' => {
@@ -93,6 +94,22 @@
PARTICIPLE => 'resetting',
FILTER => 'index-only',
},
+ 'checkout_index' => {
+ DIFF => 'diff-files -p',
+ APPLY => sub { apply_patch 'apply -R', @_; },
+ APPLY_CHECK => 'apply -R',
+ VERB => 'Check out',
+ PARTICIPLE => 'checking out',
+ FILTER => 'file-only',
+ },
+ 'checkout_commit' => {
+ DIFF => 'diff-files -p',
+ APPLY => \&apply_patch_for_checkout_commit,
+ APPLY_CHECK => 'apply -R',
+ VERB => 'Check out',
+ PARTICIPLE => 'checking out',
+ FILTER => undef,
+ },
);
my %patch_mode_flavour = %{$patch_modes{stage}};
@@ -1050,6 +1067,28 @@
return $ret;
}
+sub apply_patch_for_checkout_commit {
+ my $applies_index = run_git_apply 'apply -R --cached --recount --check', @_;
+ my $applies_worktree = run_git_apply 'apply -R --recount --check', @_;
+
+ if ($applies_worktree && $applies_index) {
+ run_git_apply 'apply -R --cached --recount', @_;
+ run_git_apply 'apply -R --recount', @_;
+ return 1;
+ } elsif (!$applies_index) {
+ print colored $error_color, "The selected hunks do not apply to the index tree!\n";
+ if (prompt_yesno "Apply them to the worktree anyway? ") {
+ return run_git_apply 'apply -R --recount', @_;
+ } else {
+ print colored $error_color, "Nothing was applied.\n";
+ return 0;
+ }
+ } else {
+ print STDERR @_;
+ return 0;
+ }
+}
+
sub patch_update_cmd {
my @all_mods = list_modified($patch_mode_flavour{FILTER});
my @mods = grep { !($_->{BINARY}) } @all_mods;
@@ -1410,6 +1449,15 @@
$patch_mode_revision = $arg;
$arg = shift @ARGV or die "missing --";
}
+ } elsif ($1 eq 'checkout') {
+ $arg = shift @ARGV or die "missing --";
+ if ($arg eq '--') {
+ $patch_mode = 'checkout_index';
+ } else {
+ $patch_mode = 'checkout_commit';
+ $patch_mode_revision = $arg;
+ $arg = shift @ARGV or die "missing --";
+ }
} elsif ($1 eq 'stage') {
$patch_mode = 'stage';
$arg = shift @ARGV or die "missing --";
--
1.6.4.rc2.227.gf5e17
next prev parent reply other threads:[~2009-07-25 21:30 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 ` [RFC PATCH v2 3/3] Implement unstage --patch and discard --patch Thomas Rast
2009-07-24 16:40 ` 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 ` Thomas Rast [this message]
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=93da49abd5fc514906bac73553d6802eb7fb69fd.1248557241.git.trast@student.ethz.ch \
--to=trast@student.ethz.ch \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=madcoder@debian.org \
--cc=nanako3@lavabit.com \
/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).