From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH/RFC] commit: new option to abort -a something is already staged
Date: Mon, 20 Aug 2018 17:41:20 +0200 [thread overview]
Message-ID: <20180820154120.19297-1-pclouds@gmail.com> (raw)
So many times I have carefully cherry picked changes to the index with
`git add -p` then accidentally did `git commit -am ....` (usually by
retrieving a command from history and pressing Enter too quickly)
which destroyed beautiful index.
One way to deal with this is some form of `git undo` that allows me to
retrieve the old index. That's not a lot of work by itself. The problem
is designing that `git undo` interface because there are more undo
options that this.
Instead, let's handle just this problem for now. This new option
commit.preciousDirtyIndex, if set to false, will not allow `commit -a`
to continue if the final index is different from the existing one. I
don't think this can be achieved with hooks because the hooks don't
know about "-a" or different commit modes.
Or is there a better way to handle this?
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Documentation/config.txt | 5 +++++
builtin/commit.c | 19 +++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 95ad715a44..3937681ee9 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1417,6 +1417,11 @@ commit.gpgSign::
convenient to use an agent to avoid typing your GPG passphrase
several times.
+commit.preciousDirtyIndex::
+ If some changes are partially staged in the index (i.e.
+ "git commit -a" and "git commit" commit different changes), reject
+ "git commit -a".
+
commit.status::
A boolean to enable/disable inclusion of status information in the
commit message template when using an editor to prepare the commit
diff --git a/builtin/commit.c b/builtin/commit.c
index 213fca2d8e..489e4b9f50 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -98,6 +98,7 @@ static const char *author_message, *author_message_buffer;
static char *edit_message, *use_message;
static char *fixup_message, *squash_message;
static int all, also, interactive, patch_interactive, only, amend, signoff;
+static int allow_dirty_index = 1;
static int edit_flag = -1; /* unspecified */
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
static int config_commit_verbose = -1; /* unspecified */
@@ -385,10 +386,24 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
* (B) on failure, rollback the real index.
*/
if (all || (also && pathspec.nr)) {
+ int compare_oid = all && !allow_dirty_index;
+ struct object_id previous_oid;
+
+ if (compare_oid) {
+ if (update_main_cache_tree(0) || !the_index.cache_tree)
+ die(_("error building trees"));
+ if (the_index.cache_tree->entry_count >= 0)
+ oidcpy(&previous_oid, &the_index.cache_tree->oid);
+ else
+ oidclr(&previous_oid);
+ }
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
refresh_cache_or_die(refresh_flags);
update_main_cache_tree(WRITE_TREE_SILENT);
+ if (compare_oid && the_index.cache_tree &&
+ oidcmp(&previous_oid, &the_index.cache_tree->oid))
+ die(_("staged content is different, aborting"));
if (write_locked_index(&the_index, &index_lock, 0))
die(_("unable to write new_index file"));
commit_style = COMMIT_NORMAL;
@@ -1413,6 +1428,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
return 0;
}
+ if (!strcmp(k, "commit.preciousdirtyindex")) {
+ allow_dirty_index = !git_config_bool(k, v);
+ return 0;
+ }
status = git_gpg_config(k, v, NULL);
if (status)
--
2.18.0.1003.g5e2e2c8169
next reply other threads:[~2018-08-20 15:41 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-20 15:41 Nguyễn Thái Ngọc Duy [this message]
2018-08-20 15:55 ` [PATCH/RFC] commit: new option to abort -a something is already staged Junio C Hamano
2018-08-20 17:48 ` Eric Sunshine
2018-08-20 19:30 ` Jonathan Nieder
2018-08-21 14:43 ` Duy Nguyen
2018-08-23 2:11 ` Jonathan Nieder
2018-08-23 2:15 ` Jonathan Nieder
2018-08-23 14:49 ` Duy Nguyen
2018-08-23 15:28 ` Junio C Hamano
2018-08-24 3:02 ` Jacob Keller
2018-08-24 14:42 ` Duy Nguyen
2018-08-24 23:23 ` Jacob Keller
2018-08-24 2:59 ` Jacob Keller
2018-09-16 6:31 ` [PATCH v2 0/1] Make 'git commit' not accidentally lose staged content Nguyễn Thái Ngọc Duy
2018-09-16 6:31 ` [PATCH v2 1/1] commit: do not clobber the index Nguyễn Thái Ngọc Duy
2018-09-17 17:09 ` [PATCH v2 0/1] Make 'git commit' not accidentally lose staged content Junio C Hamano
2018-09-17 17:29 ` Duy Nguyen
2018-09-17 18:15 ` Jeff King
2018-09-17 18:41 ` Duy Nguyen
2018-09-18 17:35 ` Jeff King
2018-09-18 19:36 ` Jacob Keller
2018-09-18 23:19 ` Jeff King
2018-09-19 16:12 ` Duy Nguyen
2018-09-19 16:16 ` Jeff King
2018-09-17 19:26 ` Junio C Hamano
2018-09-18 19:41 ` Jacob Keller
2018-09-18 21:11 ` Eckhard Maaß
2018-09-18 19:33 ` Jacob Keller
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=20180820154120.19297-1-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.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.
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).