[PATCH] Add an "-i" option to git-reset, to confirm a reset.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxx>
---
 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

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux