Subject: [PATCH v3] Introduced force flag to the git stash clear subcommand.

From: "Nadav Goldstein via GitGitGadget" <[email-masked]>
Date: Tue, 20 Jun 2023 00:03:34 +0000

From: Nadav Goldstein <[email-masked]>

stash clean subcommand now support the force flag, along
with the configuration var stash.requireforce, that if
set to true, will make git stash clear fail unless supplied
with force flag.

Signed-off-by: Nadav Goldstein <[email-masked]>
---
    stash clear: added safety flag for stash clear subcommand
    
    This patch started to solve the issue of easy trigger of git stash
    clear. I first went with using an interactive (-i) flag to stash clear,
    but following the conversations I had here I understood that it was a
    misleading flag and not a good direction for implementation.
    
    So in this version of the patch (v3), I went with Junio proposal to
    introduce force flag to the clean subcommand.
    
    Thanks!

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1232%2Fnadav96%2Fclear-stash-prompt-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1232/nadav96/clear-stash-prompt-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1232

Range-diff vs v2:

 1:  13bc75a2b05 < -:  ----------- add-menu: added add-menu to lib objects
 2:  7271a285d18 < -:  ----------- clean: refector to the interactive part of clean
 -:  ----------- > 1:  6150ec27b5a Introduced force flag to the git stash clear subcommand.

 Documentation/git-stash.txt | 12 ++++++++++--
 builtin/stash.c             | 12 +++++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index f4bb6114d91..e95410d507e 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -20,7 +20,7 @@ SYNOPSIS
 	     [--] [<pathspec>...]]
 'git stash' save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
 	     [-u | --include-untracked] [-a | --all] [<message>]
-'git stash' clear
+'git stash' clear [-f | --force]
 'git stash' create [<message>]
 'git stash' store [(-m | --message) <message>] [-q | --quiet] <commit>
 
@@ -130,7 +130,7 @@ the stash entry is applied on top of the commit that was HEAD at the
 time `git stash` was run, it restores the originally stashed state
 with no conflicts.
 
-clear::
+clear [-f|--force]::
 	Remove all the stash entries. Note that those entries will then
 	be subject to pruning, and may be impossible to recover (see
 	'Examples' below for a possible strategy).
@@ -208,6 +208,14 @@ to learn how to operate the `--patch` mode.
 The `--patch` option implies `--keep-index`.  You can use
 `--no-keep-index` to override this.
 
+-f::
+--force::
+	This option is only valid for `clear` command
++
+If the Git configuration variable stash.requireForce is set
+to true, 'git stash clear' will refuse to remove all the stash 
+entries unless given -f.
+
 -S::
 --staged::
 	This option is only valid for `push` and `save` commands.
diff --git a/builtin/stash.c b/builtin/stash.c
index a7e17ffe384..d037bc4f69c 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -53,7 +53,7 @@
 #define BUILTIN_STASH_CREATE_USAGE \
 	N_("git stash create [<message>]")
 #define BUILTIN_STASH_CLEAR_USAGE \
-	"git stash clear"
+	"git stash clear [-f | --force]"
 
 static const char * const git_stash_usage[] = {
 	BUILTIN_STASH_LIST_USAGE,
@@ -122,6 +122,7 @@ static const char * const git_stash_save_usage[] = {
 
 static const char ref_stash[] = "refs/stash";
 static struct strbuf stash_index_path = STRBUF_INIT;
+static int clear_require_force = 0;
 
 /*
  * w_commit is set to the commit containing the working tree
@@ -246,7 +247,9 @@ static int do_clear_stash(void)
 
 static int clear_stash(int argc, const char **argv, const char *prefix)
 {
+	int force = 0;
 	struct option options[] = {
+		OPT__FORCE(&force, N_("force"), PARSE_OPT_NOCOMPLETE),
 		OPT_END()
 	};
 
@@ -258,6 +261,9 @@ static int clear_stash(int argc, const char **argv, const char *prefix)
 		return error(_("git stash clear with arguments is "
 			       "unimplemented"));
 
+	if (!force && clear_require_force)
+		return error(_("fatal: stash.requireForce set to true and -f was not given; refusing to clear stash"));
+
 	return do_clear_stash();
 }
 
@@ -851,6 +857,10 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 		show_include_untracked = git_config_bool(var, value);
 		return 0;
 	}
+	if (!strcmp(var, "stash.requireforce")) {
+		clear_require_force = git_config_bool(var, value);
+		return 0;
+	}
 	return git_diff_basic_config(var, value, cb);
 }

base-commit: d7d8841f67f29e6ecbad85a11805c907d0f00d5d
-- 
gitgitgadget