Skip to content

Commit

Permalink
Merge branch 'js/add-edit'
Browse files Browse the repository at this point in the history
* js/add-edit:
  t3702: fix reliance on SHELL_PATH being '/bin/sh'
  git-add: introduce --edit (to edit the diff vs. the index)
  • Loading branch information
Junio C Hamano committed May 18, 2009
2 parents c16cea7 + 7c8224b commit 362724a
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Documentation/git-add.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SYNOPSIS
--------
[verse]
'git add' [-n] [-v] [--force | -f] [--interactive | -i] [--patch | -p]
[--all | [--update | -u]] [--intent-to-add | -N]
[--edit | -e] [--all | [--update | -u]] [--intent-to-add | -N]
[--refresh] [--ignore-errors] [--] <filepattern>...

DESCRIPTION
Expand Down Expand Up @@ -76,6 +76,15 @@ 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.
+
*NOTE*: Obviously, if you change anything else than the first character
on lines beginning with a space or a minus, the patch will no longer
apply.

-u::
--update::
Update only files that git already knows about, staging modified
Expand Down
59 changes: 56 additions & 3 deletions builtin-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
#include "cache-tree.h"
#include "run-command.h"
#include "parse-options.h"
#include "diff.h"
#include "revision.h"

static const char * const builtin_add_usage[] = {
"git add [options] [--] <filepattern>...",
NULL
};
static int patch_interactive, add_interactive;
static int patch_interactive, add_interactive, edit_interactive;
static int take_worktree_changes;

static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
Expand Down Expand Up @@ -187,6 +189,51 @@ int interactive_add(int argc, const char **argv, const char *prefix)
return status;
}

int edit_patch(int argc, const char **argv, const char *prefix)
{
char *file = xstrdup(git_path("ADD_EDIT.patch"));
const char *apply_argv[] = { "apply", "--recount", "--cached",
file, NULL };
struct child_process child;
struct rev_info rev;
int out;
struct stat st;

git_config(git_diff_basic_config, NULL); /* no "diff" UI options */

if (read_cache() < 0)
die ("Could not read the index");

init_revisions(&rev, prefix);
rev.diffopt.context = 7;

argc = setup_revisions(argc, argv, &rev, NULL);
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
out = open(file, O_CREAT | O_WRONLY, 0644);
if (out < 0)
die ("Could not open '%s' for writing.", file);
rev.diffopt.file = fdopen(out, "w");
rev.diffopt.close_file = 1;
if (run_diff_files(&rev, 0))
die ("Could not write patch");

launch_editor(file, NULL, NULL);

if (stat(file, &st))
die("Could not stat '%s'", file);
if (!st.st_size)
die("Empty patch. Aborted.");

memset(&child, 0, sizeof(child));
child.git_cmd = 1;
child.argv = apply_argv;
if (run_command(&child))
die ("Could not apply '%s'", file);

unlink(file);
return 0;
}

static struct lock_file lock_file;

static const char ignore_error[] =
Expand All @@ -201,6 +248,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, "edit current diff and apply"),
OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"),
Expand Down Expand Up @@ -251,14 +299,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
int require_pathspec;

argc = parse_options(argc, argv, builtin_add_options,
builtin_add_usage, 0);
builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
if (patch_interactive)
add_interactive = 1;
if (add_interactive)
exit(interactive_add(argc, argv, prefix));
exit(interactive_add(argc - 1, argv + 1, prefix));

git_config(add_config, NULL);

if (edit_interactive)
return(edit_patch(argc, argv, prefix));
argc--;
argv++;

if (addremove && take_worktree_changes)
die("-A and -u are mutually incompatible");
if ((addremove || take_worktree_changes) && !argc) {
Expand Down
121 changes: 121 additions & 0 deletions t/t3702-add-edit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/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

cat > second-part << EOF
To him an heir was afterward born,
a son in his halls, whom heaven sent
to favor the folk, feeling their woe
that erst they had lacked an earl for leader
so long a while; the Lord endowed him,
the Wielder of Wonder, with world's renown.
EOF

test_expect_success 'setup' '
git add file &&
test_tick &&
git commit -m initial file
'

cat > expected-patch << EOF
diff --git a/file b/file
index b9834b5..9020acb 100644
--- a/file
+++ b/file
@@ -1,11 +1,6 @@
-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!
+To him an heir was afterward born,
+a son in his halls, whom heaven sent
+to favor the folk, feeling their woe
+that erst they had lacked an earl for leader
+so long a while; the Lord endowed him,
+the Wielder of Wonder, with world's renown.
EOF

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 -f "$1" orig-patch &&
mv -f patch "$1"
EOF

test_set_editor "$(pwd)/fake-editor.sh"
chmod a+x fake-editor.sh

test_expect_success 'add -e' '
cp second-part file &&
git add -e &&
test_cmp second-part file &&
test_cmp orig-patch expected-patch &&
git diff --cached > out &&
test_cmp out expected
'

test_done

0 comments on commit 362724a

Please sign in to comment.