From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Johannes Sixt <j.sixt@viscovery.net>
Cc: git@vger.kernel.org, gitster@pobox.com
Subject: [PATCH v2 2/2] git-add: introduce --edit (to edit the diff vs. the index)
Date: Thu, 5 Jun 2008 17:20:46 +0100 (BST) [thread overview]
Message-ID: <alpine.DEB.1.00.0806051720300.21190@racer> (raw)
In-Reply-To: <alpine.DEB.1.00.0806051719170.21190@racer>
With "git add -e [<files>]", Git will fire up an editor with the current
diff relative to the index (i.e. what you would get with "git diff
[<files>]").
Now you can edit the patch as much as you like, including adding/removing
lines, editing the text, whatever. Make sure, though, that the first
character of the hunk lines is still a space, a plus or a minus.
After you closed the editor, Git will adjust the line counts of the
hunks if necessary, thanks to the --fixup-line-counts option of apply,
and commit the patch. Except if you deleted everything, in which case
nothing happens (for obvious reasons).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
Documentation/git-add.txt | 9 ++++-
builtin-add.c | 47 ++++++++++++++++++++++++-
t/t3702-add-edit.sh | 86 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 139 insertions(+), 3 deletions(-)
create mode 100755 t/t3702-add-edit.sh
diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt
index 1afd0c6..dd744f1 100644
--- a/Documentation/git-add.txt
+++ b/Documentation/git-add.txt
@@ -8,8 +8,8 @@ git-add - Add file contents to the index
SYNOPSIS
--------
[verse]
-'git-add' [-n] [-v] [-f] [--interactive | -i] [--patch | -p] [-u] [--refresh]
- [--ignore-errors] [--] <filepattern>...
+'git-add' [-n] [-v] [-f] [--interactive | -i] [--patch | -p] [--edit | -e]
+ [-u] [--refresh] [--ignore-errors] [--] <filepattern>...
DESCRIPTION
-----------
@@ -70,6 +70,11 @@ OPTIONS
bypassed and the 'patch' subcommand is invoked using each of
the specified filepatterns before exiting.
+-e, \--edit::
+ Open the diff vs. the index in an editor and let the user
+ edit it. After the editor was closed, adjust the hunk headers
+ and apply the patch to the index.
+
-u::
Update only files that git already knows about, staging modified
content for commit and marking deleted files for removal. This
diff --git a/builtin-add.c b/builtin-add.c
index 1da22ee..216f331 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -19,7 +19,7 @@ static const char * const builtin_add_usage[] = {
"git-add [options] [--] <filepattern>...",
NULL
};
-static int patch_interactive = 0, add_interactive = 0;
+static int patch_interactive = 0, add_interactive = 0, edit_interactive = 0;
static int take_worktree_changes;
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
@@ -186,6 +186,48 @@ int interactive_add(int argc, const char **argv, const char *prefix)
return status;
}
+int edit_patch(int argc, const char **argv, const char *prefix)
+{
+ static struct lock_file lock;
+ struct child_process child;
+ int ac;
+ struct stat st;
+
+ memset(&child, 0, sizeof(child));
+ child.argv = xcalloc(sizeof(const char *), (argc + 5));
+ ac = 0;
+ child.git_cmd = 1;
+ child.argv[ac++] = "diff-files";
+ child.argv[ac++] = "--no-color";
+ child.argv[ac++] = "-p";
+ child.argv[ac++] = "--";
+ if (argc) {
+ const char **pathspec = validate_pathspec(argc, argv, prefix);
+ if (!pathspec)
+ return -1;
+ memcpy(&(child.argv[ac]), pathspec, sizeof(*argv) * argc);
+ ac += argc;
+ }
+ child.argv[ac] = NULL;
+ child.out = hold_lock_file_for_update(&lock, git_path("EDIT_PATCH"), 1);
+
+ if (run_command(&child))
+ return 1;
+ free(child.argv);
+
+ launch_editor(lock.filename, NULL, NULL);
+
+ if (stat(lock.filename, &st))
+ return 1;
+ if (!st.st_size)
+ die ("Empty patch. Aborted.");
+
+ execl_git_cmd("apply", "--ignore-hunk-headers", "--cached",
+ lock.filename, NULL);
+
+ return 1;
+}
+
static struct lock_file lock_file;
static const char ignore_error[] =
@@ -200,6 +242,7 @@ static struct option builtin_add_options[] = {
OPT_GROUP(""),
OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
+ OPT_BOOLEAN('e', "edit", &edit_interactive, "super-interactive patching"),
OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
@@ -226,6 +269,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, builtin_add_options,
builtin_add_usage, 0);
+ if (edit_interactive)
+ return(edit_patch(argc, argv, prefix));
if (patch_interactive)
add_interactive = 1;
if (add_interactive)
diff --git a/t/t3702-add-edit.sh b/t/t3702-add-edit.sh
new file mode 100755
index 0000000..be2f4da
--- /dev/null
+++ b/t/t3702-add-edit.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='add -e basic tests'
+. ./test-lib.sh
+
+
+cat > file << EOF
+LO, praise of the prowess of people-kings
+of spear-armed Danes, in days long sped,
+we have heard, and what honor the athelings won!
+Oft Scyld the Scefing from squadroned foes,
+from many a tribe, the mead-bench tore,
+awing the earls. Since erst he lay
+friendless, a foundling, fate repaid him:
+for he waxed under welkin, in wealth he throve,
+till before him the folk, both far and near,
+who house by the whale-path, heard his mandate,
+gave him gifts: a good king he!
+EOF
+
+test_expect_success 'setup' '
+
+ git add file &&
+ test_tick &&
+ git commit -m initial file
+
+'
+
+cat > patch << EOF
+diff --git a/file b/file
+index b9834b5..ef6e94c 100644
+--- a/file
++++ b/file
+@@ -3,1 +3,333 @@ of spear-armed Danes, in days long sped,
+ we have heard, and what honor the athelings won!
++
+ Oft Scyld the Scefing from squadroned foes,
+@@ -2,7 +1,5 @@ awing the earls. Since erst he lay
+ friendless, a foundling, fate repaid him:
++
+ for he waxed under welkin, in wealth he throve,
+EOF
+
+cat > expected << EOF
+diff --git a/file b/file
+index b9834b5..ef6e94c 100644
+--- a/file
++++ b/file
+@@ -1,10 +1,12 @@
+ LO, praise of the prowess of people-kings
+ of spear-armed Danes, in days long sped,
+ we have heard, and what honor the athelings won!
++
+ Oft Scyld the Scefing from squadroned foes,
+ from many a tribe, the mead-bench tore,
+ awing the earls. Since erst he lay
+ friendless, a foundling, fate repaid him:
++
+ for he waxed under welkin, in wealth he throve,
+ till before him the folk, both far and near,
+ who house by the whale-path, heard his mandate,
+EOF
+
+echo "#!$SHELL_PATH" >fake-editor.sh
+cat >> fake-editor.sh <<\EOF
+mv "$1" orig-patch &&
+mv patch "$1"
+EOF
+
+test_set_editor "$(pwd)/fake-editor.sh"
+chmod a+x fake-editor.sh
+
+test_expect_success 'add -e' '
+
+ cp fake-editor.sh file &&
+ git add -e &&
+ test_cmp fake-editor.sh file &&
+ git diff --cached > out &&
+ test_cmp out expected
+
+'
+
+test_done
--
1.5.6.rc1.181.gb439d
next prev parent reply other threads:[~2008-06-05 16:23 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-05 10:16 [PATCH 1/2] Allow git-apply to fix up the line counts Johannes Schindelin
2008-06-05 10:17 ` [PATCH 2/2] git-add: introduce --edit (to edit the diff vs. the index) Johannes Schindelin
2008-06-05 11:24 ` [PATCH 1/2] Allow git-apply to fix up the line counts Johannes Sixt
2008-06-05 13:04 ` Johannes Schindelin
2008-06-05 13:36 ` Johannes Sixt
2008-06-05 13:47 ` Johannes Schindelin
2008-06-05 14:13 ` Johannes Sixt
2008-06-05 14:54 ` Johannes Schindelin
2008-06-05 15:07 ` Johannes Sixt
2008-06-05 16:19 ` [PATCH v2 0/2] git add --edit Johannes Schindelin
2008-06-05 16:20 ` [PATCH v2 1/2] Allow git-apply to ignore the hunk headers Johannes Schindelin
2008-06-05 21:16 ` Junio C Hamano
2008-06-05 22:39 ` Johannes Schindelin
2008-06-05 23:06 ` [PATCH v3 0/2] git add --edit Johannes Schindelin
2008-06-05 23:06 ` [PATCH v3 1/2] Allow git-apply to ignore the hunk headers (AKA recountdiff) Johannes Schindelin
2008-06-06 4:55 ` Junio C Hamano
2008-06-06 13:58 ` Johannes Schindelin
2008-06-06 15:34 ` Junio C Hamano
2008-06-06 16:13 ` Junio C Hamano
2008-06-06 16:37 ` Johannes Schindelin
2008-06-06 16:46 ` Junio C Hamano
2008-06-06 17:35 ` Johannes Schindelin
2008-06-06 5:18 ` Govind Salinas
2008-06-06 14:00 ` Johannes Schindelin
2008-06-05 23:07 ` [PATCH v3 2/2] git-add: introduce --edit (to edit the diff vs. the index) Johannes Schindelin
2008-06-06 10:02 ` Olivier Marin
2008-06-06 14:21 ` Johannes Schindelin
2008-06-06 4:55 ` [PATCH v3 0/2] git add --edit Junio C Hamano
2008-06-06 13:59 ` Johannes Schindelin
2008-06-05 23:22 ` [PATCH v2 1/2] Allow git-apply to ignore the hunk headers Junio C Hamano
2008-06-05 23:36 ` Johannes Schindelin
2008-06-06 7:02 ` Paolo Bonzini
2008-06-06 14:04 ` Johannes Schindelin
2008-06-06 10:33 ` Sergei Organov
2008-06-06 14:27 ` Johannes Schindelin
2008-06-06 15:14 ` Junio C Hamano
2008-06-05 16:20 ` Johannes Schindelin [this message]
2008-06-05 18:12 ` [PATCH v2 2/2] git-add: introduce --edit (to edit the diff vs. the index) Pieter de Bie
2008-06-05 18:39 ` [PATCH 1/2] Allow git-apply to fix up the line counts Junio C Hamano
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.DEB.1.00.0806051720300.21190@racer \
--to=johannes.schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=j.sixt@viscovery.net \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).