From: Kelvie Wong <kelvie@ieee.org>
To: git@vger.kernel.org
Cc: Kelvie Wong <kelvie@ieee.org>
Subject: [PATCH] Add an "-i" option to git-reset, to confirm a reset.
Date: Sat, 15 Dec 2007 19:26:36 -0800 [thread overview]
Message-ID: <1197775596-14329-1-git-send-email-kelvie@ieee.org> (raw)
It shows a diffstat, and asks the user if they would like to continue, or
show a full diff of the things getting reset.
I know that many times, I do a reset --hard thinking I had commited a file
already, but it turns out that I hadn't; and so this makes sure I don't
lose any work when the caffeine wears off.
Maybe it should also be made that only hard resets take this option, as
I cannot see this being useful in other places.
Signed-off-by: Kelvie Wong <kelvie@ieee.org>
---
Documentation/git-reset.txt | 4 +++
builtin-reset.c | 46 ++++++++++++++++++++++++++++++++++++++----
2 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-reset.txt b/Documentation/git-reset.txt
index 050e4ea..0323d9d 100644
--- a/Documentation/git-reset.txt
+++ b/Documentation/git-reset.txt
@@ -48,6 +48,10 @@ OPTIONS
-q::
Be quiet, only report errors.
+-i::
+ Show what is about to be reset, and ask for confirmation before doing
+ so.
+
<commit>::
Commit to make the current HEAD.
diff --git a/builtin-reset.c b/builtin-reset.c
index 713c2d5..1086817 100644
--- a/builtin-reset.c
+++ b/builtin-reset.c
@@ -18,7 +18,8 @@
#include "tree.h"
static const char builtin_reset_usage[] =
-"git-reset [--mixed | --soft | --hard] [-q] [<commit-ish>] [ [--] <paths>...]";
+"git-reset [--mixed | --soft | --hard] [-q] [-i] [<commit-ish>] [ [--] "
+"<paths>...]";
static char *args_to_str(const char **argv)
{
@@ -56,17 +57,47 @@ static int unmerged_files(void)
return 0;
}
-static int reset_index_file(const unsigned char *sha1, int is_hard_reset)
+static int reset_index_file(const unsigned char *sha1, int is_hard_reset,
+ int confirm_reset)
{
int i = 0;
const char *args[6];
+ struct strbuf buf;
+ char result = 0;
+ const char *ref = sha1_to_hex(sha1);
+ const char *diffstat_args[] = { "diff", "--stat", ref, NULL };
+ const char *diff_args[] = { "diff", ref, NULL };
args[i++] = "read-tree";
args[i++] = "-v";
args[i++] = "--reset";
+
+ /* Show the user what is about to be reset, and in more detail, if they
+ * like. */
+ if(confirm_reset) {
+ printf("The following files will be reset:\n");
+ run_command_v_opt(diffstat_args, RUN_GIT_CMD);
+ strbuf_init(&buf, 0);
+ while(result != 'y') {
+ printf("Continue? ((y)es/(n)o/view (d)iff)\n");
+ strbuf_getline(&buf, stdin, '\n');
+ result = tolower(buf.buf[0]);
+ switch(result) {
+ case 'd':
+ run_command_v_opt(diff_args, RUN_GIT_CMD);
+ break;
+ case 'n':
+ return 1;
+ break;
+ };
+ }
+ strbuf_release(&buf);
+ }
+
+
if (is_hard_reset)
args[i++] = "-u";
- args[i++] = sha1_to_hex(sha1);
+ args[i++] = ref;
args[i] = NULL;
return run_command_v_opt(args, RUN_GIT_CMD);
@@ -181,7 +212,8 @@ static const char *reset_type_names[] = { "mixed", "soft", "hard", NULL };
int cmd_reset(int argc, const char **argv, const char *prefix)
{
- int i = 1, reset_type = NONE, update_ref_status = 0, quiet = 0;
+ int i = 1, reset_type = NONE, update_ref_status = 0, quiet = 0,
+ confirm = 0;
const char *rev = "HEAD";
unsigned char sha1[20], *orig = NULL, sha1_orig[20],
*old_orig = NULL, sha1_old_orig[20];
@@ -210,6 +242,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
quiet = 1;
i++;
}
+ else if (!strcmp(argv[i], "-i")) {
+ confirm = 1;
+ i++;
+ }
else
break;
}
@@ -251,7 +287,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
if (is_merge() || unmerged_files())
die("Cannot do a soft reset in the middle of a merge.");
}
- else if (reset_index_file(sha1, (reset_type == HARD)))
+ else if (reset_index_file(sha1, (reset_type == HARD), confirm))
die("Could not reset index file to revision '%s'.", rev);
/* Any resets update HEAD to the head being switched to,
--
1.5.4-rc0.GIT
next reply other threads:[~2007-12-16 3:26 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-16 3:26 Kelvie Wong [this message]
2007-12-16 3:35 ` [PATCH] Add an "-i" option to git-reset, to confirm a reset Johannes Schindelin
2007-12-16 3:46 ` Kelvie Wong
2007-12-16 20:09 ` Junio C Hamano
2007-12-16 21:26 ` Kelvie Wong
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=1197775596-14329-1-git-send-email-kelvie@ieee.org \
--to=kelvie@ieee.org \
--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.
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).