Thread (4 messages) 4 messages, 4 authors, 2019-10-31

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

From: Johannes Schindelin <hidden>
Date: 2019-10-29 07:30:03
Subsystem: the rest · Maintainer: Linus Torvalds

Possibly related (same subject, not in this thread)

Hi Junio,

On Mon, 28 Oct 2019, Junio C Hamano wrote:
dev@kipras.org writes:
quoted
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:
	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 ;-)
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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help