git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [FR] Allow `git stash create` to include untracked changes

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Oct 29, 2019, 07:29 UTC
Message-ID
<nycvar.QRO.7.76.6.1910290806310.46@tvgsbejvaqbjf.bet>
In-Reply-To
<xmqqa79lbpte.fsf@gitster-ct.c.googlers.com>
Hi Junio,
On Mon, 28 Oct 2019, Junio C Hamano wrote:
Show 11 quoted lines
> dev@kipras.org writes:
>
> > Would it be possible for `git stash create` to include untracked
> > changes (probably the same way `git stash push --include-untracked`
> > does)?
>
> Doesn't the subcommand have -u/--include-untracked option?
>
> ... goes and looks git-stash.sh ...
>
> create_stash () {

Careful, this is a false friend. It _is_ work horse of `git stash create`, but not _quite_ in the way you think:

-- snap --
create)
	shift
	create_stash -m "$*" && echo "$w_commit"
	;;
-- snap --
So what is the reason for the existence of this command-line parsing:
Show 18 quoted lines
>
> 	prepare_fallback_ident
>
> 	stash_msg=
> 	untracked=
> 	while test $# != 0
> 	do
> 		case "$1" in
> 		-m|--message)
> 			...
> 			;;
> 		-u|--include-untracked)
> 			shift
> 			untracked=${1?"BUG: create_stash () -u requires an argument"}
> 			;;
> 		...
> 	done
> 	...
Why, `push_stash` calls `create_stash` ;-)
-- snip --
push_stash () {
	[...]
        untracked=
	[...]
                -u|--include-untracked)
			untracked=untracked
			;;
	[...]
        create_stash -m "$stash_msg" -u "$untracked" -- "$@"
	[...]
-- snap --

So the reason that `git stash create -u ...` does not work is that it never was designed to work ;-)

Show 19 quoted lines
> It is entirely possible that with recent push to rewrite scripted
> Porcelain commands to builtin/, we may have lost a feature or two
> by accident.
>
>   ... goes and checks ...
>
> And it does appear that builtin/stash.c::create_stash() lacks
> support for command line arguments since d4788af8 ("stash: convert
> create to builtin", 2019-02-25).
>
> Would doing
>
> 	export GIT_TEST_STASH_USE_BUILTIN=no
>
> or
>
> 	git config --global stash.usebuiltin no
>
> help in the meantime???

Well, given this code in `builtin/stash.c`, I would imagine that `git stash push -u ...` works both in the legacy and the built-in version of `git stash`:

-- snip --
static int push_stash(int argc, const char **argv, const char *prefix)
{
	int keep_index = -1;
	int patch_mode = 0;
	int include_untracked = 0;
	int quiet = 0;
	const char *stash_msg = NULL;
	struct pathspec ps;
	struct option options[] = {
		OPT_BOOL('k', "keep-index", &keep_index,
			 N_("keep index")),
		OPT_BOOL('p', "patch", &patch_mode,
			 N_("stash in patch mode")),
		OPT__QUIET(&quiet, N_("quiet mode")),
		OPT_BOOL('u', "include-untracked", &include_untracked,
			 N_("include untracked files in stash")),
		OPT_SET_INT('a', "all", &include_untracked,
			    N_("include ignore files"), 2),
		OPT_STRING('m', "message", &stash_msg, N_("message"),
			   N_("stash message")),
		OPT_END()
	};
	if (argc)
		argc = parse_options(argc, argv, prefix, options,
				     git_stash_push_usage,
				     0);
	parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
		       prefix, argv);
	return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
			     include_untracked);
}
-- snap --

So why does `git stash create -u ...` not work? Because the original design of `git stash create` never intended to take any command-line arguments other than the stash message. And that design can obviously not be fixed without breaking backwards compatibility.

What we _could_ do is to add a new command-line option to `git stash push` that would make it behave like the `create` subsubcommand: _not_ update the `refs/stash` ref but instead print the commit hash.

I am not quite sure how to call this option (`--ephemeral` came to my mind, as the created commit is not reachable from anywhere and is hence subject to garbage collection).

A completely different approach would be to allow overriding the ref to store the stash in, with an empty value triggering the mode where the ref is not updated but the commit hash would be printed instead. I am thinking of something like `git stash -r '' push ...`, starting with this patch:

-- snip --
diff --git a/builtin/stash.c b/builtin/stash.c
index bb4f6d8d762..43b0a155b1d 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1553,6 +1553,8 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
 	struct argv_array args = ARGV_ARRAY_INIT;

 	struct option options[] = {
+		OPT_STRING('r', "ref", &ref_stash, N_("ref"),
+			   N_("override `refs/stash`")),
 		OPT_END()
 	};

-- snap --

The biggest trick will be to make all the code paths safe that assume
that `ref_stash` refers to a valid ref ;-)

Ciao,
Dscho
Previous: Kipras MelnikovasNext: Thomas Gummerer
Message 4 of 5 in “[FR] Allow `git stash create` to include untracked changes”
  1. dev@kipras.orgOct 26, 2019
  2. Junio C HamanoOct 28, 2019
  3. Kipras MelnikovasOct 28, 2019
  4. Johannes SchindelinOct 29, 2019
  5. Thomas GummererOct 31, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.