From 1020d08786c1413d488845f9175d01c0ea073a6c Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 6 May 2011 22:59:59 -0700 Subject: Use a temporary index for git commit --interactive Change the behaviour of git commit --interactive so that when you abort the commit (by leaving the commit message empty) the index remains unchanged. Hitherto an aborted commit --interactive has added the selected hunks to the index regardless of whether the commit succeeded or not. Signed-off-by: Conrad Irwin Signed-off-by: Junio C Hamano --- builtin/commit.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'builtin/commit.c') diff --git a/builtin/commit.c b/builtin/commit.c index 67757e999..636aea6d6 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -336,18 +336,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int int fd; struct string_list partial; const char **pathspec = NULL; + char *old_index_env = NULL; int refresh_flags = REFRESH_QUIET; if (is_status) refresh_flags |= REFRESH_UNMERGED; - if (interactive) { - if (interactive_add(argc, argv, prefix) != 0) - die(_("interactive add failed")); - if (read_cache_preload(NULL) < 0) - die(_("index file corrupt")); - commit_style = COMMIT_AS_IS; - return get_index_file(); - } if (*argv) pathspec = get_pathspec(prefix, argv); @@ -355,6 +348,33 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int if (read_cache_preload(pathspec) < 0) die(_("index file corrupt")); + if (interactive) { + fd = hold_locked_index(&index_lock, 1); + + refresh_cache_or_die(refresh_flags); + + if (write_cache(fd, active_cache, active_nr) || + close_lock_file(&index_lock)) + die(_("unable to create temporary index")); + + old_index_env = getenv(INDEX_ENVIRONMENT); + setenv(INDEX_ENVIRONMENT, index_lock.filename, 1); + + if (interactive_add(argc, argv, prefix) != 0) + die(_("interactive add failed")); + + if (old_index_env && *old_index_env) + setenv(INDEX_ENVIRONMENT, old_index_env, 1); + else + unsetenv(INDEX_ENVIRONMENT); + + discard_cache(); + read_cache_from(index_lock.filename); + + commit_style = COMMIT_NORMAL; + return index_lock.filename; + } + /* * Non partial, non as-is commit. * -- cgit v1.2.1 From e41fcfe955cb8080bf8f0e16352d3131d1f01ac8 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 6 May 2011 23:00:00 -0700 Subject: Allow git commit --interactive with paths Make git commit --interactive feel more like git add --interactive by allowing the user to restrict the list of files they have to deal with. A test in t7501 used to ensure that this is not allowed; no need for that anymore. Signed-off-by: Conrad Irwin Signed-off-by: Junio C Hamano --- builtin/commit.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'builtin/commit.c') diff --git a/builtin/commit.c b/builtin/commit.c index 636aea6d6..7707af884 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1084,8 +1084,6 @@ static int parse_and_validate_options(int argc, const char *argv[], if (all && argc > 0) die(_("Paths with -a does not make sense.")); - else if (interactive && argc > 0) - die(_("Paths with --interactive does not make sense.")); if (null_termination && status_format == STATUS_FORMAT_LONG) status_format = STATUS_FORMAT_PORCELAIN; -- cgit v1.2.1 From b4bd466820ee272fccfefff1d23d64c7826d1d07 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sat, 7 May 2011 10:58:07 -0700 Subject: Add support for -p/--patch to git-commit The --interactive flag is already shared by git add and git commit, share the -p and --patch flags too. Signed-off-by: Conrad Irwin Signed-off-by: Junio C Hamano --- builtin/commit.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'builtin/commit.c') diff --git a/builtin/commit.c b/builtin/commit.c index 7707af884..008c1ec83 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -83,7 +83,7 @@ static const char *template_file; static const char *author_message, *author_message_buffer; static char *edit_message, *use_message; static char *fixup_message, *squash_message; -static int all, edit_flag, also, interactive, only, amend, signoff; +static int all, edit_flag, also, interactive, patch_interactive, only, amend, signoff; static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; static int no_post_rewrite, allow_empty_message; static char *untracked_files_arg, *force_date, *ignore_submodule_arg; @@ -152,6 +152,7 @@ static struct option builtin_commit_options[] = { OPT_BOOLEAN('a', "all", &all, "commit all changed files"), OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"), OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"), + OPT_BOOLEAN('p', "patch", &patch_interactive, "interactively add changes"), OPT_BOOLEAN('o', "only", &only, "commit only specified files"), OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"), OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"), @@ -360,7 +361,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int old_index_env = getenv(INDEX_ENVIRONMENT); setenv(INDEX_ENVIRONMENT, index_lock.filename, 1); - if (interactive_add(argc, argv, prefix) != 0) + if (interactive_add(argc, argv, prefix, patch_interactive) != 0) die(_("interactive add failed")); if (old_index_env && *old_index_env) @@ -1061,8 +1062,11 @@ static int parse_and_validate_options(int argc, const char *argv[], author_message_buffer = read_commit_message(author_message); } + if (patch_interactive) + interactive = 1; + if (!!also + !!only + !!all + !!interactive > 1) - die(_("Only one of --include/--only/--all/--interactive can be used.")); + die(_("Only one of --include/--only/--all/--interactive/--patch can be used.")); if (argc == 0 && (also || (only && !amend))) die(_("No paths with --include/--only does not make sense.")); if (argc == 0 && only && amend) -- cgit v1.2.1