git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v5 0/5] Convert some stash functionality to a builtin
@ 2018-04-05  2:28 Joel Teichroeb
  2018-04-05  2:28 ` [PATCH v5 1/5] stash: improve option parsing test coverage Joel Teichroeb
                   ` (6 more replies)
  0 siblings, 7 replies; 181+ messages in thread
From: Joel Teichroeb @ 2018-04-05  2:28 UTC (permalink / raw)
  To: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, Johannes Schindelin, gitster
  Cc: Joel Teichroeb

I've been working on converting all of git stash to be a
builtin, however it's hard to get it all working at once with
limited time, so I've moved around half of it to a new
stash--helper builtin and called these functions from the shell
script. Once this is stabalized, it should be easier to convert
the rest of the commands one at a time without breaking
anything.

I've sent most of this code before, but that was targetting a
full replacement of stash. The code is overall the same, but
with some code review changes and updates for internal api
changes.

Since there seems to be interest from GSOC students who want to
work on converting builtins, I figured I should finish what I
have that works now so they could build on top of it.

The code is based on next as write_cache_as_tree was changed to
take an object ID. It can easily be rebase on master by changing
the two calls to write_cache_as_tree to use tha hash.

Previous threads:
v1: https://public-inbox.org/git/20180325173916.GE10909@hank/T/
v2: https://public-inbox.org/git/20180326011426.19159-1-joel@teichroeb.net/
v3: https://public-inbox.org/git/20180327054432.26419-1-joel@teichroeb.net/
v4: https://public-inbox.org/git/20180328222129.22192-1-joel@teichroeb.net/

Changes from v4:
 - Fixed a typo (Thanks Eric)
 - Redid my test again with input from Junio
 - Cleaned up usages of get_oid (Thanks Junio)
 - Slightly reduced calls to builtin functions (rerere, rev-parse)
 - Added comments clarifying why when forking/cmd_* is used

Joel Teichroeb (5):
  stash: improve option parsing test coverage
  stash: convert apply to builtin
  stash: convert drop and clear to builtin
  stash: convert branch to builtin
  stash: convert pop to builtin

 .gitignore              |   1 +
 Makefile                |   1 +
 builtin.h               |   1 +
 builtin/stash--helper.c | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 136 +----------
 git.c                   |   1 +
 t/t3903-stash.sh        |  35 +++
 7 files changed, 677 insertions(+), 128 deletions(-)
 create mode 100644 builtin/stash--helper.c

-- 
2.16.3


^ permalink raw reply	[flat|nested] 181+ messages in thread

* [PATCH v5 1/5] stash: improve option parsing test coverage
  2018-04-05  2:28 [PATCH v5 0/5] Convert some stash functionality to a builtin Joel Teichroeb
@ 2018-04-05  2:28 ` Joel Teichroeb
  2018-04-06 13:06   ` Johannes Schindelin
  2018-04-05  2:28 ` [PATCH v5 2/5] stash: convert apply to builtin Joel Teichroeb
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 181+ messages in thread
From: Joel Teichroeb @ 2018-04-05  2:28 UTC (permalink / raw)
  To: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, Johannes Schindelin, gitster
  Cc: Joel Teichroeb

In preparation for converting the stash command incrementally to
a builtin command, this patch improves test coverage of the option
parsing. Both for having too many parameters, or too few.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index aefde7b172..4eaa4cae9a 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '
 	test foo = "$(cat file/file)"
 '
 
+test_expect_success 'giving too many ref agruments does not modify files' '
+	git stash clear &&
+	test_when_finished "git reset --hard HEAD" &&
+	echo foo >file2 &&
+	git stash &&
+	echo bar >file2 &&
+	git stash &&
+	test-chmtime =123456789 file2 &&
+	for type in apply pop "branch stash-branch"
+	do
+		test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
+		test_i18ngrep "Too many" err &&
+		test 123456789 = $(test-chmtime -v +0 file2 | sed 's/[^0-9].*$//') || return 1
+	done
+'
+
+test_expect_success 'giving too many ref agruments to drop does not drop anything' '
+	git stash list > stashlist1 &&
+	test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
+	test_i18ngrep "Too many" err &&
+	git stash list > stashlist2 &&
+	test_cmp stashlist1 stashlist2
+'
+
+test_expect_success 'giving too many ref agruments to show does not show anything' '
+	test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out && # show must not show anything
+	test_i18ngrep "Too many" err &&
+	test_must_be_empty out
+'
+
 test_expect_success 'stash create - no changes' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
@@ -479,6 +509,11 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
+test_expect_success 'stash branch complains with no arguments' '
+	test_must_fail git stash branch 2>err &&
+	test_i18ngrep "No branch name specified" err
+'
+
 test_expect_success 'stash show format defaults to --stat' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
-- 
2.16.3


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v5 2/5] stash: convert apply to builtin
  2018-04-05  2:28 [PATCH v5 0/5] Convert some stash functionality to a builtin Joel Teichroeb
  2018-04-05  2:28 ` [PATCH v5 1/5] stash: improve option parsing test coverage Joel Teichroeb
@ 2018-04-05  2:28 ` Joel Teichroeb
  2018-04-05  7:50   ` Christian Couder
                     ` (2 more replies)
  2018-04-05  2:28 ` [PATCH v5 3/5] stash: convert drop and clear " Joel Teichroeb
                   ` (4 subsequent siblings)
  6 siblings, 3 replies; 181+ messages in thread
From: Joel Teichroeb @ 2018-04-05  2:28 UTC (permalink / raw)
  To: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, Johannes Schindelin, gitster
  Cc: Joel Teichroeb

Add a bulitin helper for performing stash commands. Converting
all at once proved hard to review, so starting with just apply
let conversion get started without the other command being
finished.

The helper is being implemented as a drop in replacement for
stash so that when it is complete it can simply be renamed and
the shell script deleted.

Delete the contents of the apply_stash shell function and replace
it with a call to stash--helper apply until pop is also
converted.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
---
 .gitignore              |   1 +
 Makefile                |   1 +
 builtin.h               |   1 +
 builtin/stash--helper.c | 431 ++++++++++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |  75 +--------
 git.c                   |   1 +
 6 files changed, 440 insertions(+), 70 deletions(-)
 create mode 100644 builtin/stash--helper.c

diff --git a/.gitignore b/.gitignore
index 833ef3b0b7..296d5f376d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -152,6 +152,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
diff --git a/Makefile b/Makefile
index 96f6138f63..6cfdbe9a32 100644
--- a/Makefile
+++ b/Makefile
@@ -1028,6 +1028,7 @@ BUILTIN_OBJS += builtin/send-pack.o
 BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-ref.o
+BUILTIN_OBJS += builtin/stash--helper.o
 BUILTIN_OBJS += builtin/stripspace.o
 BUILTIN_OBJS += builtin/submodule--helper.o
 BUILTIN_OBJS += builtin/symbolic-ref.o
diff --git a/builtin.h b/builtin.h
index 42378f3aa4..a14fd85b0e 100644
--- a/builtin.h
+++ b/builtin.h
@@ -219,6 +219,7 @@ extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
 extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
 extern int cmd_status(int argc, const char **argv, const char *prefix);
+extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
new file mode 100644
index 0000000000..9d00a9547d
--- /dev/null
+++ b/builtin/stash--helper.c
@@ -0,0 +1,431 @@
+#include "builtin.h"
+#include "config.h"
+#include "parse-options.h"
+#include "refs.h"
+#include "lockfile.h"
+#include "cache-tree.h"
+#include "unpack-trees.h"
+#include "merge-recursive.h"
+#include "argv-array.h"
+#include "run-command.h"
+#include "dir.h"
+#include "rerere.h"
+
+static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char * const git_stash_helper_apply_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char *ref_stash = "refs/stash";
+static int quiet;
+static struct strbuf stash_index_path = STRBUF_INIT;
+
+struct stash_info {
+	struct object_id w_commit;
+	struct object_id b_commit;
+	struct object_id i_commit;
+	struct object_id u_commit;
+	struct object_id w_tree;
+	struct object_id b_tree;
+	struct object_id i_tree;
+	struct object_id u_tree;
+	struct strbuf revision;
+	int is_stash_ref;
+	int has_u;
+};
+
+static int grab_oid(struct object_id *oid, const char *fmt, const char *rev)
+{
+	struct strbuf buf = STRBUF_INIT;
+	int ret;
+
+	strbuf_addf(&buf, fmt, rev);
+	ret = get_oid(buf.buf, oid);
+	strbuf_release(&buf);
+	return ret;
+}
+
+static void free_stash_info(struct stash_info *info)
+{
+	strbuf_release(&info->revision);
+}
+
+static int get_stash_info(struct stash_info *info, int argc, const char **argv)
+{
+	struct strbuf symbolic = STRBUF_INIT;
+	int ret;
+	const char *revision;
+	const char *commit = NULL;
+	char *end_of_rev;
+	char *expanded_ref;
+	struct object_id discard;
+
+	if (argc > 1) {
+		int i;
+		struct strbuf refs_msg = STRBUF_INIT;
+		for (i = 0; i < argc; ++i)
+			strbuf_addf(&refs_msg, " '%s'", argv[i]);
+
+		fprintf_ln(stderr, _("Too many revisions specified:%s"), refs_msg.buf);
+		strbuf_release(&refs_msg);
+
+		return -1;
+	}
+
+	if (argc == 1)
+		commit = argv[0];
+
+	strbuf_init(&info->revision, 0);
+	if (commit == NULL) {
+		if (!ref_exists(ref_stash)) {
+			free_stash_info(info);
+			return error(_("No stash entries found."));
+		}
+
+		strbuf_addf(&info->revision, "%s@{0}", ref_stash);
+	} else if (strspn(commit, "0123456789") == strlen(commit)) {
+		strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
+	} else {
+		strbuf_addstr(&info->revision, commit);
+	}
+
+	revision = info->revision.buf;
+
+	if (get_oid(revision, &info->w_commit)) {
+		error(_("%s is not a valid reference"), revision);
+		free_stash_info(info);
+		return -1;
+	}
+
+	if (grab_oid(&info->b_commit, "%s^1", revision) ||
+		grab_oid(&info->w_tree, "%s:", revision) ||
+		grab_oid(&info->b_tree, "%s^1:", revision) ||
+		grab_oid(&info->i_tree, "%s^2:", revision)) {
+
+		error(_("'%s' is not a stash-like commit"), revision);
+		free_stash_info(info);
+		return -1;
+	}
+
+	info->has_u = !grab_oid(&info->u_tree, "%s^3:", revision);
+
+	end_of_rev = strchrnul(revision, '@');
+	strbuf_add(&symbolic, revision, end_of_rev - revision);
+
+	ret = dwim_ref(symbolic.buf, symbolic.len, &discard, &expanded_ref);
+	strbuf_release(&symbolic);
+	switch (ret) {
+	case 0: /* Not found, but valid ref */
+		info->is_stash_ref = 0;
+		break;
+	case 1:
+		info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
+		break;
+	default: /* Invalid or ambiguous */
+		free_stash_info(info);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int reset_tree(struct object_id *i_tree, int update, int reset)
+{
+	struct unpack_trees_options opts;
+	int nr_trees = 1;
+	struct tree_desc t[MAX_UNPACK_TREES];
+	struct tree *tree;
+	struct lock_file lock_file = LOCK_INIT;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+
+	memset(&opts, 0, sizeof(opts));
+
+	tree = parse_tree_indirect(i_tree);
+	if (parse_tree(tree))
+		return -1;
+
+	init_tree_desc(t, tree->buffer, tree->size);
+
+	opts.head_idx = 1;
+	opts.src_index = &the_index;
+	opts.dst_index = &the_index;
+	opts.merge = 1;
+	opts.reset = reset;
+	opts.update = update;
+	opts.fn = oneway_merge;
+
+	if (unpack_trees(nr_trees, t, &opts))
+		return -1;
+
+	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		return error(_("unable to write new index file"));
+
+	return 0;
+}
+
+static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *w_commit_hex = oid_to_hex(w_commit);
+
+	/* Diff-tree would not be very hard to replace with a native function,
+	 * however it should be done together with apply_cached.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
+	argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
+
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int apply_cached(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/* Apply currently only reads either from stdin or a file, thus
+	 * apply_all_patches would have to be updated to optionally take a buffer
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int reset_head(const char *prefix)
+{
+	struct argv_array args = ARGV_ARRAY_INIT;
+
+	/* Reset is overall quite simple, however there is no current public API
+	 * for resetting.
+	 */
+	argv_array_push(&args, "reset");
+	return cmd_reset(args.argc, args.argv, prefix);
+}
+
+static int diff_cached_index(struct strbuf *out, struct object_id *c_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *c_tree_hex = oid_to_hex(c_tree);
+
+	/* diff-index is very similar to diff-tree above, and should be converted
+	 * together with update_index
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only", "--diff-filter=A", NULL);
+	argv_array_push(&cp.args, c_tree_hex);
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int update_index(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/* Update-index is very complicated and may need to have a public function
+	 * exposed in order to remove this forking
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int restore_untracked(struct object_id *u_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int res;
+
+	/*
+	 * We need to run restore files from a given index, but without affecting
+	 * the current index, so we use GIT_INDEX_FILE with run_command to fork
+	 * processes that will not interfere.
+	 */
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "read-tree");
+	argv_array_push(&cp.args, oid_to_hex(u_tree));
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
+	if (run_command(&cp)) {
+		remove_path(stash_index_path.buf);
+		return -1;
+	}
+
+	child_process_init(&cp);
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
+
+	res = run_command(&cp);
+	remove_path(stash_index_path.buf);
+	return res;
+}
+
+static int do_apply_stash(const char *prefix, struct stash_info *info, int index)
+{
+	struct merge_options o;
+	struct object_id c_tree;
+	struct object_id index_tree;
+	const struct object_id *bases[1];
+	int bases_count = 1;
+	struct commit *result;
+	int ret;
+	int has_index = index;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))
+		return error(_("Cannot apply a stash in the middle of a merge"));
+
+	if (index) {
+		if (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree, &info->i_tree)) {
+			has_index = 0;
+		} else {
+			struct strbuf out = STRBUF_INIT;
+
+			if (diff_tree_binary(&out, &info->w_commit)) {
+				strbuf_release(&out);
+				return -1;
+			}
+
+			ret = apply_cached(&out);
+			strbuf_release(&out);
+			if (ret)
+				return -1;
+
+			discard_cache();
+			read_cache();
+			if (write_cache_as_tree(&index_tree, 0, NULL))
+				return -1;
+
+			reset_head(prefix);
+		}
+	}
+
+	if (info->has_u) {
+		if (restore_untracked(&info->u_tree))
+			return error(_("Could not restore untracked files from stash"));
+	}
+
+	init_merge_options(&o);
+
+	o.branch1 = "Updated upstream";
+	o.branch2 = "Stashed changes";
+
+	if (!oidcmp(&info->b_tree, &c_tree))
+		o.branch1 = "Version stash was based on";
+
+	if (quiet)
+		o.verbosity = 0;
+
+	if (o.verbosity >= 3)
+		printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
+
+	bases[0] = &info->b_tree;
+
+	ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, bases_count, bases, &result);
+	if (ret != 0) {
+		rerere(0);
+
+		if (index)
+			fprintf_ln(stderr, _("Index was not unstashed."));
+
+		return ret;
+	}
+
+	if (has_index) {
+		if (reset_tree(&index_tree, 0, 0))
+			return -1;
+	} else {
+		struct strbuf out = STRBUF_INIT;
+
+		if (diff_cached_index(&out, &c_tree)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		if (reset_tree(&c_tree, 0, 1)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		ret = update_index(&out);
+		strbuf_release(&out);
+		if (ret)
+			return -1;
+
+		discard_cache();
+	}
+
+	if (!quiet) {
+		struct argv_array args = ARGV_ARRAY_INIT;
+		/* Status is quite simple and could be replaced with calls to wt_status
+		 * in the future, but it adds complexities which may require more tests
+		 */
+		argv_array_push(&args, "status");
+		cmd_status(args.argc, args.argv, prefix);
+	}
+
+	return 0;
+}
+
+static int apply_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0;
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			git_stash_helper_apply_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	ret = do_apply_stash(prefix, &info, index);
+	free_stash_info(&info);
+	return ret;
+}
+
+int cmd_stash__helper(int argc, const char **argv, const char *prefix)
+{
+	int result = 0;
+	pid_t pid = getpid();
+	const char *index_file;
+
+	struct option options[] = {
+		OPT_END()
+	};
+
+	git_config(git_default_config, NULL);
+
+	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
+		PARSE_OPT_KEEP_UNKNOWN|PARSE_OPT_KEEP_DASHDASH);
+
+	index_file = get_index_file();
+	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, (uintmax_t)pid);
+
+	if (argc < 1)
+		usage_with_options(git_stash_helper_usage, options);
+	else if (!strcmp(argv[0], "apply"))
+		result = apply_stash(argc, argv, prefix);
+	else {
+		error(_("unknown subcommand: %s"), argv[0]);
+		usage_with_options(git_stash_helper_usage, options);
+		result = 1;
+	}
+
+	return result;
+}
diff --git a/git-stash.sh b/git-stash.sh
index 94793c1a91..0b5d1f3743 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -566,76 +566,11 @@ assert_stash_ref() {
 }
 
 apply_stash () {
-
-	assert_stash_like "$@"
-
-	git update-index -q --refresh || die "$(gettext "unable to refresh index")"
-
-	# current index state
-	c_tree=$(git write-tree) ||
-		die "$(gettext "Cannot apply a stash in the middle of a merge")"
-
-	unstashed_index_tree=
-	if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
-			test "$c_tree" != "$i_tree"
-	then
-		git diff-tree --binary $s^2^..$s^2 | git apply --cached
-		test $? -ne 0 &&
-			die "$(gettext "Conflicts in index. Try without --index.")"
-		unstashed_index_tree=$(git write-tree) ||
-			die "$(gettext "Could not save index tree")"
-		git reset
-	fi
-
-	if test -n "$u_tree"
-	then
-		GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
-		GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
-		rm -f "$TMPindex" ||
-		die "$(gettext "Could not restore untracked files from stash entry")"
-	fi
-
-	eval "
-		GITHEAD_$w_tree='Stashed changes' &&
-		GITHEAD_$c_tree='Updated upstream' &&
-		GITHEAD_$b_tree='Version stash was based on' &&
-		export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
-	"
-
-	if test -n "$GIT_QUIET"
-	then
-		GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
-	fi
-	if git merge-recursive $b_tree -- $c_tree $w_tree
-	then
-		# No conflict
-		if test -n "$unstashed_index_tree"
-		then
-			git read-tree "$unstashed_index_tree"
-		else
-			a="$TMP-added" &&
-			git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
-			git read-tree --reset $c_tree &&
-			git update-index --add --stdin <"$a" ||
-				die "$(gettext "Cannot unstage modified files")"
-			rm -f "$a"
-		fi
-		squelch=
-		if test -n "$GIT_QUIET"
-		then
-			squelch='>/dev/null 2>&1'
-		fi
-		(cd "$START_DIR" && eval "git status $squelch") || :
-	else
-		# Merge conflict; keep the exit status from merge-recursive
-		status=$?
-		git rerere
-		if test -n "$INDEX_OPTION"
-		then
-			gettextln "Index was not unstashed." >&2
-		fi
-		exit $status
-	fi
+	cd "$START_DIR"
+	git stash--helper apply "$@"
+	res=$?
+	cd_to_toplevel
+	return $res
 }
 
 pop_stash() {
diff --git a/git.c b/git.c
index ceaa58ef40..6ffe6364ac 100644
--- a/git.c
+++ b/git.c
@@ -466,6 +466,7 @@ static struct cmd_struct commands[] = {
 	{ "show-branch", cmd_show_branch, RUN_SETUP },
 	{ "show-ref", cmd_show_ref, RUN_SETUP },
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
 	{ "stripspace", cmd_stripspace },
 	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX},
-- 
2.16.3


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v5 3/5] stash: convert drop and clear to builtin
  2018-04-05  2:28 [PATCH v5 0/5] Convert some stash functionality to a builtin Joel Teichroeb
  2018-04-05  2:28 ` [PATCH v5 1/5] stash: improve option parsing test coverage Joel Teichroeb
  2018-04-05  2:28 ` [PATCH v5 2/5] stash: convert apply to builtin Joel Teichroeb
@ 2018-04-05  2:28 ` Joel Teichroeb
  2018-04-06 15:39   ` Johannes Schindelin
  2018-04-05  2:28 ` [PATCH v5 4/5] stash: convert branch " Joel Teichroeb
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 181+ messages in thread
From: Joel Teichroeb @ 2018-04-05  2:28 UTC (permalink / raw)
  To: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, Johannes Schindelin, gitster
  Cc: Joel Teichroeb

Add the drop and clear commands to the builtin helper. These two
are each simple, but are being added together as they are quite
related.

We have to unfortunately keep the drop and clear functions in the
shell script as functions are called with parameters internally
that are not valid when the commands are called externally. Once
pop is converted they can both be removed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
---
 builtin/stash--helper.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   4 +-
 2 files changed, 109 insertions(+), 2 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 9d00a9547d..520cd746c4 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,7 +12,14 @@
 #include "rerere.h"
 
 static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper clear"),
+	NULL
+};
+
+static const char * const git_stash_helper_drop_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
 };
 
@@ -21,6 +28,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_clear_usage[] = {
+	N_("git stash--helper clear"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -134,6 +146,29 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
 	return 0;
 }
 
+static int do_clear_stash(void)
+{
+	struct object_id obj;
+	if (get_oid(ref_stash, &obj))
+		return 0;
+
+	return delete_ref(NULL, ref_stash, &obj, 0);
+}
+
+static int clear_stash(int argc, const char **argv, const char *prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options, git_stash_helper_clear_usage, PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (argc != 0)
+		return error(_("git stash--helper clear with parameters is unimplemented"));
+
+	return do_clear_stash();
+}
+
 static int reset_tree(struct object_id *i_tree, int update, int reset)
 {
 	struct unpack_trees_options opts;
@@ -399,6 +434,74 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int do_drop_stash(const char *prefix, struct stash_info *info)
+{
+	struct argv_array args = ARGV_ARRAY_INIT;
+	int ret;
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/* reflog does not provide a simple function for deleting refs. One will
+	 * need to be added to avoid implementing too much reflog code here
+	 */
+	argv_array_pushl(&args, "reflog", "delete", "--updateref", "--rewrite", NULL);
+	argv_array_push(&args, info->revision.buf);
+	ret = cmd_reflog(args.argc, args.argv, prefix);
+	if (!ret) {
+		if (!quiet)
+			printf(_("Dropped %s (%s)\n"), info->revision.buf, oid_to_hex(&info->w_commit));
+	} else {
+		return error(_("%s: Could not drop stash entry"), info->revision.buf);
+	}
+
+	/* This could easily be replaced by get_oid, but currently it will throw a
+	 * fatal error when a reflog is empty, which we can not recover from
+	 */
+	cp.git_cmd = 1;
+	/* Even though --quiet is specified, rev-parse still outputs the hash */
+	cp.no_stdout = 1;
+	argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
+	argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
+	ret = run_command(&cp);
+
+	if (ret)
+		do_clear_stash();
+
+	return 0;
+}
+
+static int assert_stash_ref(struct stash_info *info)
+{
+	if (!info->is_stash_ref)
+		return error(_("'%s' is not a stash reference"), info->revision.buf);
+
+	return 0;
+}
+
+static int drop_stash(int argc, const char **argv, const char *prefix)
+{
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			git_stash_helper_drop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	if (assert_stash_ref(&info)) {
+		free_stash_info(&info);
+		return -1;
+	}
+
+	ret = do_drop_stash(prefix, &info);
+	free_stash_info(&info);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	int result = 0;
@@ -421,6 +524,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_stash_helper_usage, options);
 	else if (!strcmp(argv[0], "apply"))
 		result = apply_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "clear"))
+		result = clear_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "drop"))
+		result = drop_stash(argc, argv, prefix);
 	else {
 		error(_("unknown subcommand: %s"), argv[0]);
 		usage_with_options(git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 0b5d1f3743..0b8f07b38a 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -652,7 +652,7 @@ apply)
 	;;
 clear)
 	shift
-	clear_stash "$@"
+	git stash--helper clear "$@"
 	;;
 create)
 	shift
@@ -664,7 +664,7 @@ store)
 	;;
 drop)
 	shift
-	drop_stash "$@"
+	git stash--helper drop "$@"
 	;;
 pop)
 	shift
-- 
2.16.3


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v5 4/5] stash: convert branch to builtin
  2018-04-05  2:28 [PATCH v5 0/5] Convert some stash functionality to a builtin Joel Teichroeb
                   ` (2 preceding siblings ...)
  2018-04-05  2:28 ` [PATCH v5 3/5] stash: convert drop and clear " Joel Teichroeb
@ 2018-04-05  2:28 ` Joel Teichroeb
  2018-04-06 15:50   ` Johannes Schindelin
  2018-04-05  2:28 ` [PATCH v5 5/5] stash: convert pop " Joel Teichroeb
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 181+ messages in thread
From: Joel Teichroeb @ 2018-04-05  2:28 UTC (permalink / raw)
  To: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, Johannes Schindelin, gitster
  Cc: Joel Teichroeb

Add stash branch to the helper and delete the apply_to_branch
function from the shell script.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
---
 builtin/stash--helper.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 17 ++---------------
 2 files changed, 53 insertions(+), 15 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 520cd746c4..486796bb6a 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -14,6 +14,7 @@
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	NULL
 };
@@ -28,6 +29,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_branch_usage[] = {
+	N_("git stash--helper branch <branchname> [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_clear_usage[] = {
 	N_("git stash--helper clear"),
 	NULL
@@ -502,6 +508,49 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int branch_stash(int argc, const char **argv, const char *prefix)
+{
+	const char *branch = NULL;
+	int ret;
+	struct argv_array args = ARGV_ARRAY_INIT;
+	struct stash_info info;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			git_stash_helper_branch_usage, 0);
+
+	if (argc == 0)
+		return error(_("No branch name specified"));
+
+	branch = argv[0];
+
+	if (get_stash_info(&info, argc - 1, argv + 1))
+		return -1;
+
+	/* Checkout does not currently provide a function for checking out a branch
+	 * as cmd_checkout does a large amount of sanity checks first that we
+	 * require here.
+	 */
+	argv_array_pushl(&args, "checkout", "-b", NULL);
+	argv_array_push(&args, branch);
+	argv_array_push(&args, oid_to_hex(&info.b_commit));
+	ret = cmd_checkout(args.argc, args.argv, prefix);
+	if (ret) {
+		free_stash_info(&info);
+		return -1;
+	}
+
+	ret = do_apply_stash(prefix, &info, 1);
+	if (!ret && info.is_stash_ref)
+		ret = do_drop_stash(prefix, &info);
+
+	free_stash_info(&info);
+
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	int result = 0;
@@ -528,6 +577,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		result = clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		result = drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "branch"))
+		result = branch_stash(argc, argv, prefix);
 	else {
 		error(_("unknown subcommand: %s"), argv[0]);
 		usage_with_options(git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 0b8f07b38a..c5fd4c6c44 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -598,20 +598,6 @@ drop_stash () {
 	clear_stash
 }
 
-apply_to_branch () {
-	test -n "$1" || die "$(gettext "No branch name specified")"
-	branch=$1
-	shift 1
-
-	set -- --index "$@"
-	assert_stash_like "$@"
-
-	git checkout -b $branch $REV^ &&
-	apply_stash "$@" && {
-		test -z "$IS_STASH_REF" || drop_stash "$@"
-	}
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -672,7 +658,8 @@ pop)
 	;;
 branch)
 	shift
-	apply_to_branch "$@"
+	cd "$START_DIR"
+	git stash--helper branch "$@"
 	;;
 *)
 	case $# in
-- 
2.16.3


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v5 5/5] stash: convert pop to builtin
  2018-04-05  2:28 [PATCH v5 0/5] Convert some stash functionality to a builtin Joel Teichroeb
                   ` (3 preceding siblings ...)
  2018-04-05  2:28 ` [PATCH v5 4/5] stash: convert branch " Joel Teichroeb
@ 2018-04-05  2:28 ` Joel Teichroeb
  2018-04-06 16:12   ` Johannes Schindelin
  2018-04-06 16:15 ` [PATCH v5 0/5] Convert some stash functionality to a builtin Johannes Schindelin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
  6 siblings, 1 reply; 181+ messages in thread
From: Joel Teichroeb @ 2018-04-05  2:28 UTC (permalink / raw)
  To: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, Johannes Schindelin, gitster
  Cc: Joel Teichroeb

Add stash pop to the helper and delete the pop_stash, drop_stash,
assert_stash_ref and pop_stash functions from the shell script
now that they are no longer needed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
---
 builtin/stash--helper.c | 41 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 50 ++++---------------------------------------------
 2 files changed, 45 insertions(+), 46 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 486796bb6a..7c8950bc90 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,6 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
+	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
@@ -24,6 +25,11 @@ static const char * const git_stash_helper_drop_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_pop_usage[] = {
+	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_apply_usage[] = {
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
 	NULL
@@ -508,6 +514,39 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int pop_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0, ret;
+	struct stash_info info;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			git_stash_helper_pop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	if (assert_stash_ref(&info)) {
+		free_stash_info(&info);
+		return -1;
+	}
+
+	if (do_apply_stash(prefix, &info, index)) {
+		printf_ln(_("The stash entry is kept in case you need it again."));
+		free_stash_info(&info);
+		return -1;
+	}
+
+	ret = do_drop_stash(prefix, &info);
+	free_stash_info(&info);
+	return ret;
+}
+
 static int branch_stash(int argc, const char **argv, const char *prefix)
 {
 	const char *branch = NULL;
@@ -577,6 +616,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		result = clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		result = drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "pop"))
+		result = pop_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "branch"))
 		result = branch_stash(argc, argv, prefix);
 	else {
diff --git a/git-stash.sh b/git-stash.sh
index c5fd4c6c44..8f2640fe90 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -554,50 +554,6 @@ assert_stash_like() {
 	}
 }
 
-is_stash_ref() {
-	is_stash_like "$@" && test -n "$IS_STASH_REF"
-}
-
-assert_stash_ref() {
-	is_stash_ref "$@" || {
-		args="$*"
-		die "$(eval_gettext "'\$args' is not a stash reference")"
-	}
-}
-
-apply_stash () {
-	cd "$START_DIR"
-	git stash--helper apply "$@"
-	res=$?
-	cd_to_toplevel
-	return $res
-}
-
-pop_stash() {
-	assert_stash_ref "$@"
-
-	if apply_stash "$@"
-	then
-		drop_stash "$@"
-	else
-		status=$?
-		say "$(gettext "The stash entry is kept in case you need it again.")"
-		exit $status
-	fi
-}
-
-drop_stash () {
-	assert_stash_ref "$@"
-
-	git reflog delete --updateref --rewrite "${REV}" &&
-		say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
-		die "$(eval_gettext "\${REV}: Could not drop stash entry")"
-
-	# clear_stash if we just dropped the last stash entry
-	git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
-	clear_stash
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -634,7 +590,8 @@ push)
 	;;
 apply)
 	shift
-	apply_stash "$@"
+	cd "$START_DIR"
+	git stash--helper apply "$@"
 	;;
 clear)
 	shift
@@ -654,7 +611,8 @@ drop)
 	;;
 pop)
 	shift
-	pop_stash "$@"
+	cd "$START_DIR"
+	git stash--helper pop "$@"
 	;;
 branch)
 	shift
-- 
2.16.3


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 2/5] stash: convert apply to builtin
  2018-04-05  2:28 ` [PATCH v5 2/5] stash: convert apply to builtin Joel Teichroeb
@ 2018-04-05  7:50   ` Christian Couder
  2018-04-05  7:59     ` Christian Couder
  2018-04-05 13:34     ` Johannes Schindelin
  2018-04-06 15:10   ` Johannes Schindelin
  2018-04-06 15:17   ` Johannes Schindelin
  2 siblings, 2 replies; 181+ messages in thread
From: Christian Couder @ 2018-04-05  7:50 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Eric Sunshine,
	Johannes Schindelin, Junio C Hamano

On Thu, Apr 5, 2018 at 4:28 AM, Joel Teichroeb <joel@teichroeb.net> wrote:
> Add a bulitin helper for performing stash commands. Converting
> all at once proved hard to review, so starting with just apply
> let conversion get started without the other command being
> finished.
>
> The helper is being implemented as a drop in replacement for
> stash so that when it is complete it can simply be renamed and
> the shell script deleted.
>
> Delete the contents of the apply_stash shell function and replace
> it with a call to stash--helper apply until pop is also
> converted.
>
> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
> ---
>  .gitignore              |   1 +
>  Makefile                |   1 +
>  builtin.h               |   1 +
>  builtin/stash--helper.c | 431 ++++++++++++++++++++++++++++++++++++++++++++++++
>  git-stash.sh            |  75 +--------
>  git.c                   |   1 +
>  6 files changed, 440 insertions(+), 70 deletions(-)
>  create mode 100644 builtin/stash--helper.c
>
> diff --git a/.gitignore b/.gitignore
> index 833ef3b0b7..296d5f376d 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -152,6 +152,7 @@
>  /git-show-ref
>  /git-stage
>  /git-stash
> +/git-stash--helper
>  /git-status
>  /git-stripspace
>  /git-submodule
> diff --git a/Makefile b/Makefile
> index 96f6138f63..6cfdbe9a32 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1028,6 +1028,7 @@ BUILTIN_OBJS += builtin/send-pack.o
>  BUILTIN_OBJS += builtin/shortlog.o
>  BUILTIN_OBJS += builtin/show-branch.o
>  BUILTIN_OBJS += builtin/show-ref.o
> +BUILTIN_OBJS += builtin/stash--helper.o
>  BUILTIN_OBJS += builtin/stripspace.o
>  BUILTIN_OBJS += builtin/submodule--helper.o
>  BUILTIN_OBJS += builtin/symbolic-ref.o
> diff --git a/builtin.h b/builtin.h
> index 42378f3aa4..a14fd85b0e 100644
> --- a/builtin.h
> +++ b/builtin.h
> @@ -219,6 +219,7 @@ extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
>  extern int cmd_show(int argc, const char **argv, const char *prefix);
>  extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
>  extern int cmd_status(int argc, const char **argv, const char *prefix);
> +extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
>  extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
>  extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
>  extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> new file mode 100644
> index 0000000000..9d00a9547d
> --- /dev/null
> +++ b/builtin/stash--helper.c
> @@ -0,0 +1,431 @@
> +#include "builtin.h"
> +#include "config.h"
> +#include "parse-options.h"
> +#include "refs.h"
> +#include "lockfile.h"
> +#include "cache-tree.h"
> +#include "unpack-trees.h"
> +#include "merge-recursive.h"
> +#include "argv-array.h"
> +#include "run-command.h"
> +#include "dir.h"
> +#include "rerere.h"
> +
> +static const char * const git_stash_helper_usage[] = {
> +       N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
> +       NULL
> +};
> +
> +static const char * const git_stash_helper_apply_usage[] = {
> +       N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
> +       NULL
> +};
> +
> +static const char *ref_stash = "refs/stash";
> +static int quiet;
> +static struct strbuf stash_index_path = STRBUF_INIT;
> +
> +struct stash_info {
> +       struct object_id w_commit;
> +       struct object_id b_commit;
> +       struct object_id i_commit;
> +       struct object_id u_commit;
> +       struct object_id w_tree;
> +       struct object_id b_tree;
> +       struct object_id i_tree;
> +       struct object_id u_tree;
> +       struct strbuf revision;
> +       int is_stash_ref;
> +       int has_u;
> +};
> +
> +static int grab_oid(struct object_id *oid, const char *fmt, const char *rev)
> +{
> +       struct strbuf buf = STRBUF_INIT;
> +       int ret;
> +
> +       strbuf_addf(&buf, fmt, rev);
> +       ret = get_oid(buf.buf, oid);
> +       strbuf_release(&buf);
> +       return ret;
> +}
> +
> +static void free_stash_info(struct stash_info *info)
> +{
> +       strbuf_release(&info->revision);
> +}
> +
> +static int get_stash_info(struct stash_info *info, int argc, const char **argv)
> +{
> +       struct strbuf symbolic = STRBUF_INIT;
> +       int ret;
> +       const char *revision;
> +       const char *commit = NULL;
> +       char *end_of_rev;
> +       char *expanded_ref;
> +       struct object_id discard;
> +
> +       if (argc > 1) {
> +               int i;
> +               struct strbuf refs_msg = STRBUF_INIT;
> +               for (i = 0; i < argc; ++i)
> +                       strbuf_addf(&refs_msg, " '%s'", argv[i]);
> +
> +               fprintf_ln(stderr, _("Too many revisions specified:%s"), refs_msg.buf);
> +               strbuf_release(&refs_msg);
> +
> +               return -1;
> +       }
> +
> +       if (argc == 1)
> +               commit = argv[0];
> +
> +       strbuf_init(&info->revision, 0);
> +       if (commit == NULL) {
> +               if (!ref_exists(ref_stash)) {
> +                       free_stash_info(info);
> +                       return error(_("No stash entries found."));

Here `info` is freed first and then `return error(...)` is used.

> +               }
> +
> +               strbuf_addf(&info->revision, "%s@{0}", ref_stash);
> +       } else if (strspn(commit, "0123456789") == strlen(commit)) {
> +               strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
> +       } else {
> +               strbuf_addstr(&info->revision, commit);
> +       }
> +
> +       revision = info->revision.buf;
> +
> +       if (get_oid(revision, &info->w_commit)) {
> +               error(_("%s is not a valid reference"), revision);
> +               free_stash_info(info);
> +               return -1;

Maybe:

               free_stash_info(info);
               return error(_("%s is not a valid reference"), revision);

to save one line and be more consistent with above.

> +       }
> +
> +       if (grab_oid(&info->b_commit, "%s^1", revision) ||
> +               grab_oid(&info->w_tree, "%s:", revision) ||
> +               grab_oid(&info->b_tree, "%s^1:", revision) ||
> +               grab_oid(&info->i_tree, "%s^2:", revision)) {
> +
> +               error(_("'%s' is not a stash-like commit"), revision);
> +               free_stash_info(info);
> +               return -1;

Here also.

> +       }
> +
> +       info->has_u = !grab_oid(&info->u_tree, "%s^3:", revision);
> +
> +       end_of_rev = strchrnul(revision, '@');
> +       strbuf_add(&symbolic, revision, end_of_rev - revision);
> +
> +       ret = dwim_ref(symbolic.buf, symbolic.len, &discard, &expanded_ref);
> +       strbuf_release(&symbolic);
> +       switch (ret) {
> +       case 0: /* Not found, but valid ref */
> +               info->is_stash_ref = 0;
> +               break;
> +       case 1:
> +               info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
> +               break;
> +       default: /* Invalid or ambiguous */
> +               free_stash_info(info);
> +               return -1;
> +       }
> +
> +       return 0;
> +}
> +
> +static int reset_tree(struct object_id *i_tree, int update, int reset)
> +{
> +       struct unpack_trees_options opts;
> +       int nr_trees = 1;
> +       struct tree_desc t[MAX_UNPACK_TREES];
> +       struct tree *tree;
> +       struct lock_file lock_file = LOCK_INIT;
> +
> +       read_cache_preload(NULL);
> +       if (refresh_cache(REFRESH_QUIET))
> +               return -1;
> +
> +       hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
> +
> +       memset(&opts, 0, sizeof(opts));
> +
> +       tree = parse_tree_indirect(i_tree);
> +       if (parse_tree(tree))
> +               return -1;
> +
> +       init_tree_desc(t, tree->buffer, tree->size);
> +
> +       opts.head_idx = 1;
> +       opts.src_index = &the_index;
> +       opts.dst_index = &the_index;
> +       opts.merge = 1;
> +       opts.reset = reset;
> +       opts.update = update;
> +       opts.fn = oneway_merge;
> +
> +       if (unpack_trees(nr_trees, t, &opts))
> +               return -1;
> +
> +       if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
> +               return error(_("unable to write new index file"));
> +
> +       return 0;
> +}
> +
> +static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
> +{
> +       struct child_process cp = CHILD_PROCESS_INIT;
> +       const char *w_commit_hex = oid_to_hex(w_commit);
> +
> +       /* Diff-tree would not be very hard to replace with a native function,
> +        * however it should be done together with apply_cached.
> +        */

Our multi line comments are more like:

       /*
        * Diff-tree would not be very hard to replace with a native function,
        * however it should be done together with apply_cached.
        */

> +       cp.git_cmd = 1;
> +       argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
> +       argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
> +
> +       return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
> +}
> +
> +static int apply_cached(struct strbuf *out)
> +{
> +       struct child_process cp = CHILD_PROCESS_INIT;
> +
> +       /* Apply currently only reads either from stdin or a file, thus
> +        * apply_all_patches would have to be updated to optionally take a buffer
> +        */

Here also and below the multi line comment style is not the recommended one.

> +       cp.git_cmd = 1;
> +       argv_array_pushl(&cp.args, "apply", "--cached", NULL);
> +       return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
> +}

[...]

> +static int restore_untracked(struct object_id *u_tree)
> +{
> +       struct child_process cp = CHILD_PROCESS_INIT;
> +       int res;
> +
> +       /*
> +        * We need to run restore files from a given index, but without affecting
> +        * the current index, so we use GIT_INDEX_FILE with run_command to fork
> +        * processes that will not interfere.
> +        */

Here it is the recommended style.

> +       cp.git_cmd = 1;
> +       argv_array_push(&cp.args, "read-tree");
> +       argv_array_push(&cp.args, oid_to_hex(u_tree));
> +       argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
> +       if (run_command(&cp)) {
> +               remove_path(stash_index_path.buf);
> +               return -1;
> +       }
> +
> +       child_process_init(&cp);
> +       cp.git_cmd = 1;
> +       argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
> +       argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
> +
> +       res = run_command(&cp);
> +       remove_path(stash_index_path.buf);
> +       return res;
> +}
> +
> +static int do_apply_stash(const char *prefix, struct stash_info *info, int index)
> +{
> +       struct merge_options o;
> +       struct object_id c_tree;
> +       struct object_id index_tree;
> +       const struct object_id *bases[1];
> +       int bases_count = 1;
> +       struct commit *result;
> +       int ret;
> +       int has_index = index;
> +
> +       read_cache_preload(NULL);
> +       if (refresh_cache(REFRESH_QUIET))
> +               return -1;
> +
> +       if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))
> +               return error(_("Cannot apply a stash in the middle of a merge"));
> +
> +       if (index) {
> +               if (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree, &info->i_tree)) {
> +                       has_index = 0;
> +               } else {
> +                       struct strbuf out = STRBUF_INIT;
> +
> +                       if (diff_tree_binary(&out, &info->w_commit)) {
> +                               strbuf_release(&out);
> +                               return -1;
> +                       }
> +
> +                       ret = apply_cached(&out);
> +                       strbuf_release(&out);
> +                       if (ret)
> +                               return -1;
> +
> +                       discard_cache();
> +                       read_cache();
> +                       if (write_cache_as_tree(&index_tree, 0, NULL))
> +                               return -1;
> +
> +                       reset_head(prefix);
> +               }
> +       }
> +
> +       if (info->has_u) {
> +               if (restore_untracked(&info->u_tree))
> +                       return error(_("Could not restore untracked files from stash"));
> +       }

Maybe:

       if (info->has_u && restore_untracked(&info->u_tree))
               return error(_("Could not restore untracked files from stash"));

> +       init_merge_options(&o);
> +
> +       o.branch1 = "Updated upstream";
> +       o.branch2 = "Stashed changes";
> +
> +       if (!oidcmp(&info->b_tree, &c_tree))
> +               o.branch1 = "Version stash was based on";
> +
> +       if (quiet)
> +               o.verbosity = 0;
> +
> +       if (o.verbosity >= 3)
> +               printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
> +
> +       bases[0] = &info->b_tree;
> +
> +       ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, bases_count, bases, &result);
> +       if (ret != 0) {
> +               rerere(0);
> +
> +               if (index)
> +                       fprintf_ln(stderr, _("Index was not unstashed."));
> +
> +               return ret;
> +       }
> +
> +       if (has_index) {
> +               if (reset_tree(&index_tree, 0, 0))
> +                       return -1;
> +       } else {
> +               struct strbuf out = STRBUF_INIT;
> +
> +               if (diff_cached_index(&out, &c_tree)) {
> +                       strbuf_release(&out);
> +                       return -1;
> +               }
> +
> +               if (reset_tree(&c_tree, 0, 1)) {
> +                       strbuf_release(&out);
> +                       return -1;
> +               }
> +
> +               ret = update_index(&out);
> +               strbuf_release(&out);
> +               if (ret)
> +                       return -1;
> +
> +               discard_cache();
> +       }
> +
> +       if (!quiet) {
> +               struct argv_array args = ARGV_ARRAY_INIT;
> +               /* Status is quite simple and could be replaced with calls to wt_status
> +                * in the future, but it adds complexities which may require more tests
> +                */

Multi line comment style.

> +               argv_array_push(&args, "status");
> +               cmd_status(args.argc, args.argv, prefix);
> +       }
> +
> +       return 0;
> +}

[...]

> +int cmd_stash__helper(int argc, const char **argv, const char *prefix)
> +{
> +       int result = 0;
> +       pid_t pid = getpid();
> +       const char *index_file;
> +
> +       struct option options[] = {
> +               OPT_END()
> +       };
> +
> +       git_config(git_default_config, NULL);
> +
> +       argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
> +               PARSE_OPT_KEEP_UNKNOWN|PARSE_OPT_KEEP_DASHDASH);
> +
> +       index_file = get_index_file();
> +       strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, (uintmax_t)pid);
> +
> +       if (argc < 1)
> +               usage_with_options(git_stash_helper_usage, options);

usage_with_options() does an `exit(129)` ...

> +       else if (!strcmp(argv[0], "apply"))

... so we can get rid of the `else` above...

> +               result = apply_stash(argc, argv, prefix);
> +       else {
> +               error(_("unknown subcommand: %s"), argv[0]);
> +               usage_with_options(git_stash_helper_usage, options);
> +               result = 1;

... and here we don't need to set `result`.

> +       }
> +
> +       return result;
> +}

So maybe we can get rid of `result` and have something like:

       if (argc < 1) {
               error(_("at least one argument is required"));
               usage_with_options(git_stash_helper_usage, options);
       }

       if (!strcmp(argv[0], "apply"))
               return apply_stash(argc, argv, prefix);

       error(_("unknown subcommand: %s"), argv[0]);
       usage_with_options(git_stash_helper_usage, options);
}

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 2/5] stash: convert apply to builtin
  2018-04-05  7:50   ` Christian Couder
@ 2018-04-05  7:59     ` Christian Couder
  2018-04-05  8:13       ` Christian Couder
  2018-04-05 13:34     ` Johannes Schindelin
  1 sibling, 1 reply; 181+ messages in thread
From: Christian Couder @ 2018-04-05  7:59 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Eric Sunshine,
	Johannes Schindelin, Junio C Hamano

On Thu, Apr 5, 2018 at 9:50 AM, Christian Couder
<christian.couder@gmail.com> wrote:
>
> So maybe we can get rid of `result` and have something like:
>
>        if (argc < 1) {
>                error(_("at least one argument is required"));
>                usage_with_options(git_stash_helper_usage, options);

Maybe we could also simplify these 2 lines by using usage_msg_opt().

>        }
>
>        if (!strcmp(argv[0], "apply"))
>                return apply_stash(argc, argv, prefix);
>
>        error(_("unknown subcommand: %s"), argv[0]);
>        usage_with_options(git_stash_helper_usage, options);
> }

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 2/5] stash: convert apply to builtin
  2018-04-05  7:59     ` Christian Couder
@ 2018-04-05  8:13       ` Christian Couder
  0 siblings, 0 replies; 181+ messages in thread
From: Christian Couder @ 2018-04-05  8:13 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Eric Sunshine,
	Johannes Schindelin, Junio C Hamano

On Thu, Apr 5, 2018 at 9:59 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> On Thu, Apr 5, 2018 at 9:50 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>>
>> So maybe we can get rid of `result` and have something like:
>>
>>        if (argc < 1) {
>>                error(_("at least one argument is required"));
>>                usage_with_options(git_stash_helper_usage, options);
>
> Maybe we could also simplify these 2 lines by using usage_msg_opt().
>
>>        }
>>
>>        if (!strcmp(argv[0], "apply"))
>>                return apply_stash(argc, argv, prefix);
>>
>>        error(_("unknown subcommand: %s"), argv[0]);
>>        usage_with_options(git_stash_helper_usage, options);

And here actually we could improve the above 2 lines using something like:

usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
                              git_stash_helper_usage, options);

It's better than using `error()` because the printed message will
start with "fatal" instead of "error".

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 2/5] stash: convert apply to builtin
  2018-04-05  7:50   ` Christian Couder
  2018-04-05  7:59     ` Christian Couder
@ 2018-04-05 13:34     ` Johannes Schindelin
  1 sibling, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-05 13:34 UTC (permalink / raw)
  To: Christian Couder
  Cc: Joel Teichroeb, Git Mailing List, Thomas Gummerer, Eric Sunshine,
	Junio C Hamano

Hi Christian,

[please cull a *lot* more of the quoted mail when you do not reference any
of it... Thanks]

On Thu, 5 Apr 2018, Christian Couder wrote:

> On Thu, Apr 5, 2018 at 4:28 AM, Joel Teichroeb <joel@teichroeb.net> wrote:
> >
> > [...]
> > +
> > +       revision = info->revision.buf;
> > +
> > +       if (get_oid(revision, &info->w_commit)) {
> > +               error(_("%s is not a valid reference"), revision);
> > +               free_stash_info(info);
> > +               return -1;
> 
> Maybe:
> 
>                free_stash_info(info);
>                return error(_("%s is not a valid reference"), revision);
> 
> to save one line and be more consistent with above.

No. The parameter `revision` of the `error()` call is assigned just above
the `if()` block and clearly points into the `info` structure. So you must
not release that `info` before printing the error. The order of statements
is correct.

> > +       if (grab_oid(&info->b_commit, "%s^1", revision) ||
> > +               grab_oid(&info->w_tree, "%s:", revision) ||
> > +               grab_oid(&info->b_tree, "%s^1:", revision) ||
> > +               grab_oid(&info->i_tree, "%s^2:", revision)) {
> > +
> > +               error(_("'%s' is not a stash-like commit"), revision);
> > +               free_stash_info(info);
> > +               return -1;
> 
> Here also.

Yes, here, too, `revision` points at a field inside `info`, so we must not
release it before using it.

> > +       if (info->has_u) {
> > +               if (restore_untracked(&info->u_tree))
> > +                       return error(_("Could not restore untracked files from stash"));
> > +       }
> 
> Maybe:
> 
>        if (info->has_u && restore_untracked(&info->u_tree))
>                return error(_("Could not restore untracked files from stash"));

I agree with this, as it avoids an unncessary indentation level.

> So maybe we can get rid of `result` and have something like:
> 
>        if (argc < 1) {
>                error(_("at least one argument is required"));
>                usage_with_options(git_stash_helper_usage, options);
>        }
> 
>        if (!strcmp(argv[0], "apply"))
>                return apply_stash(argc, argv, prefix);

... except we have to use !!apply_stash() here: apply_stash() probably
returns -1 in case of error (at least that would be consistent with our
coding conventions), and the return value from cmd_*() is handed to exit()
as exit status. The `!!` trick turns any non-zero value into a 1, also
consistent with our coding conventions where we set exit code 1 upon error
in the "business logic".

> 
>        error(_("unknown subcommand: %s"), argv[0]);
>        usage_with_options(git_stash_helper_usage, options);
> }

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 1/5] stash: improve option parsing test coverage
  2018-04-05  2:28 ` [PATCH v5 1/5] stash: improve option parsing test coverage Joel Teichroeb
@ 2018-04-06 13:06   ` Johannes Schindelin
  2018-04-06 22:48     ` Paul-Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-06 13:06 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hi Joel,

On Wed, 4 Apr 2018, Joel Teichroeb wrote:

> In preparation for converting the stash command incrementally to
> a builtin command, this patch improves test coverage of the option
> parsing. Both for having too many parameters, or too few.

Very good.

> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
> ---
>  t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index aefde7b172..4eaa4cae9a 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '
>  	test foo = "$(cat file/file)"
>  '
>  
> +test_expect_success 'giving too many ref agruments does not modify files' '

Quick, before Eric beats me to it! A typo! s/agruments/arguments/

> +	git stash clear &&
> +	test_when_finished "git reset --hard HEAD" &&
> +	echo foo >file2 &&
> +	git stash &&
> +	echo bar >file2 &&
> +	git stash &&
> +	test-chmtime =123456789 file2 &&
> +	for type in apply pop "branch stash-branch"
> +	do
> +		test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
> +		test_i18ngrep "Too many" err &&
> +		test 123456789 = $(test-chmtime -v +0 file2 | sed 's/[^0-9].*$//') || return 1

Not your problem, but if there is future work on this (read: if I get to
mentor a GSoC student, and if I get them to work on it: this idiom
`test-chmtime -v +0 ... | sed ...` is too common, there really *should* be
a `test-chmtime --get ...`).

Any prospective GSoC student: you know what I have in stock for you ;-)

> +	done
> +'
> +
> +test_expect_success 'giving too many ref agruments to drop does not drop anything' '

s/agruments/arguments/

Also, we try to keep the lines to <= 80 columns. So maybe

+test_expect_success 'drop: too many arguments errors out (does nothing)' '

> +	git stash list > stashlist1 &&

Again, before Eric can beat me to it: we prefer the syntax `>file` (i.e.
without a space between the `>` and the file name).

BTW is there a public branch with your patches? I am not so much of a fan
of reviewing and having all the work being punted back to the contributor,
especially when we failed you for so long in reviewing these patches. I
would love, for example, to open a PR on GitHub (even if that would only
apply to code changes, not so much to commit message changes, not until we
have rebase support reword! and drop! keywords).

> +	test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
> +	test_i18ngrep "Too many" err &&
> +	git stash list > stashlist2 &&
> +	test_cmp stashlist1 stashlist2
> +'

It might make sense to rename stashlist1 to `expect` an `stashlist2` to
`actual`, to clarify the roles.

> +
> +test_expect_success 'giving too many ref agruments to show does not show anything' '

Maybe

test_expect_success 'show: error on too may arguments (show nothing)'

to keep within the 80 columns/line limit?

The rest looks obviously correct to me.

Thanks!
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 2/5] stash: convert apply to builtin
  2018-04-05  2:28 ` [PATCH v5 2/5] stash: convert apply to builtin Joel Teichroeb
  2018-04-05  7:50   ` Christian Couder
@ 2018-04-06 15:10   ` Johannes Schindelin
  2018-04-06 15:17   ` Johannes Schindelin
  2 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-06 15:10 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hi Joel,

On Wed, 4 Apr 2018, Joel Teichroeb wrote:

> Add a bulitin helper for performing stash commands. Converting
> all at once proved hard to review, so starting with just apply
> let conversion get started without the other command being

s/let/lets the/ s/command/commands/

> finished.
> 
> The helper is being implemented as a drop in replacement for
> stash so that when it is complete it can simply be renamed and
> the shell script deleted.
> 
> Delete the contents of the apply_stash shell function and replace
> it with a call to stash--helper apply until pop is also
> converted.
> 
> [...]
>
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> new file mode 100644
> index 0000000000..9d00a9547d
> --- /dev/null
> +++ b/builtin/stash--helper.c
> @@ -0,0 +1,431 @@
> +#include "builtin.h"
> +#include "config.h"
> +#include "parse-options.h"
> +#include "refs.h"
> +#include "lockfile.h"
> +#include "cache-tree.h"
> +#include "unpack-trees.h"
> +#include "merge-recursive.h"
> +#include "argv-array.h"
> +#include "run-command.h"
> +#include "dir.h"
> +#include "rerere.h"
> +
> +static const char * const git_stash_helper_usage[] = {

Note to other reviewers: while it would appear that our convention is
usually to cuddle the `*` with the identifier afterwards (i.e. `char
*const` instead of `char * const`), it would *also* appear that we somehow
prefer spaces on both sides when it comes to the _usage:

$ git grep 'usage\[' builtin/*.c | grep -c ' \* '
88

$ git grep 'usage\[' builtin/*.c | grep -c ' \*[^ ]'
18

So much to clean up and make more consistent... (not your problem, Joel)

> +	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
> +	NULL
> +};
> +
> +static const char * const git_stash_helper_apply_usage[] = {
> +	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
> +	NULL
> +};
> +
> +static const char *ref_stash = "refs/stash";
> +static int quiet;
> +static struct strbuf stash_index_path = STRBUF_INIT;
> +
> +struct stash_info {
> +	struct object_id w_commit;
> +	struct object_id b_commit;
> +	struct object_id i_commit;
> +	struct object_id u_commit;
> +	struct object_id w_tree;
> +	struct object_id b_tree;
> +	struct object_id i_tree;
> +	struct object_id u_tree;
> +	struct strbuf revision;
> +	int is_stash_ref;
> +	int has_u;
> +};
> +
> +static int grab_oid(struct object_id *oid, const char *fmt, const char *rev)
> +{
> +	struct strbuf buf = STRBUF_INIT;
> +	int ret;
> +
> +	strbuf_addf(&buf, fmt, rev);
> +	ret = get_oid(buf.buf, oid);
> +	strbuf_release(&buf);
> +	return ret;
> +}

It could be even more reusable, by using

int get_oidf(struct object_id *oid, const char *fmt, ...)
{
	va_list ap;
	char *p;
	int ret;

	va_start(ap, fmt);
	p = xstrvfmt(fmt, ap);
	va_end(ap);
	ret = get_oid(p, oid);
	free(p);

	return ret;
}

Feel free to do this as a preparatory patch (it should probably live next
to get_oid()), otherwise there is always an opportunity for a
micro-project (or not so micro- one).

> +static void free_stash_info(struct stash_info *info)
> +{
> +	strbuf_release(&info->revision);
> +}
> +
> +static int get_stash_info(struct stash_info *info, int argc, const char **argv)
> +{
> +	struct strbuf symbolic = STRBUF_INIT;
> +	int ret;
> +	const char *revision;
> +	const char *commit = NULL;
> +	char *end_of_rev;
> +	char *expanded_ref;
> +	struct object_id discard;

I think elsewhere we prefer the name "dummy" for variables that are
assigned without really needing the values.

> +
> +	if (argc > 1) {
> +		int i;
> +		struct strbuf refs_msg = STRBUF_INIT;
> +		for (i = 0; i < argc; ++i)
> +			strbuf_addf(&refs_msg, " '%s'", argv[i]);
> +
> +		fprintf_ln(stderr, _("Too many revisions specified:%s"), refs_msg.buf);

Note to interested parties: while it may have been okay for a mere Unix
shell script to output such a message directly to stderr, in C, we need to
use `error()` and start the error message with lower-case. This should
*not* be done as part of the conversion to C, which is kind of bug-for-bug.

> +		strbuf_release(&refs_msg);
> +
> +		return -1;
> +	}
> +
> +	if (argc == 1)
> +		commit = argv[0];

It is probably a bit too magical to assign `commit = argv[0]` right
away... but it would work, as `argv` is guaranteed to be NULL-terminated.

> +
> +	strbuf_init(&info->revision, 0);
> +	if (commit == NULL) {

We usually use `!commit` instead of `commit == NULL`. It's just the style
we use, not a judgement.

> +		if (!ref_exists(ref_stash)) {
> +			free_stash_info(info);
> +			return error(_("No stash entries found."));

Technically, the shell version used `die`. We could use the same here,
too, but `error()` seems to be fine, too.

However, if you use `error()` here, you should use it also instead of the
`fprintf(stderr, ...);` above, for "Too many revisions specified".

Also: our convention is to start error messages lower-case because they
are prefixed with `error: `.

But in the interest of a faithful conversion to C, maybe we should start
by using `fprintf(stderr, ...)` first, and only convert to `error()` later
(because that will require changes to the test suite, too).

> +		}
> +
> +		strbuf_addf(&info->revision, "%s@{0}", ref_stash);
> +	} else if (strspn(commit, "0123456789") == strlen(commit)) {
> +		strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
> +	} else {
> +		strbuf_addstr(&info->revision, commit);
> +	}
> +
> +	revision = info->revision.buf;
> +
> +	if (get_oid(revision, &info->w_commit)) {
> +		error(_("%s is not a valid reference"), revision);
> +		free_stash_info(info);
> +		return -1;
> +	}
> +
> +	if (grab_oid(&info->b_commit, "%s^1", revision) ||
> +		grab_oid(&info->w_tree, "%s:", revision) ||
> +		grab_oid(&info->b_tree, "%s^1:", revision) ||
> +		grab_oid(&info->i_tree, "%s^2:", revision)) {
> +
> +		error(_("'%s' is not a stash-like commit"), revision);
> +		free_stash_info(info);
> +		return -1;
> +	}

Fans of keeping Git commands in the form of Unix shell scripts, please
note that this is a deviation from the shell script, but in a good way:
before, if any of those revisions could not be parsed by `git rev-parse`,
the error would not have resulted in a fatal error. Instead, IS_STASH_LIKE
and IS_STASH_REF would have been unchanged, as would be w_commit,
b_commit, w_tree, b_tree and i_tree.

The error checking in C is much better for end users.

> +	info->has_u = !grab_oid(&info->u_tree, "%s^3:", revision);
> +
> +	end_of_rev = strchrnul(revision, '@');
> +	strbuf_add(&symbolic, revision, end_of_rev - revision);

This is a literal translation of the shell script's `"${REV%@*}"`, but I
wonder whether we can do better in C. After all,
`refs/stash@my-laptop@{1}` is possibly a valid stash, when I called `git
fetch laptop refs/stash:refs/stash@my-laptop` regularly.

Let's see.

Nope, we really don't have anything in our "API" to peel off reflog
suffixes... Another micro-project for the future. Nothing that needs to
hold up this patch series. (I am saying this mainly for the benefit of
other reviewers...)

> +
> +	ret = dwim_ref(symbolic.buf, symbolic.len, &discard, &expanded_ref);
> +	strbuf_release(&symbolic);
> +	switch (ret) {
> +	case 0: /* Not found, but valid ref */
> +		info->is_stash_ref = 0;
> +		break;
> +	case 1:
> +		info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
> +		break;
> +	default: /* Invalid or ambiguous */
> +		free_stash_info(info);
> +		return -1;
> +	}
> +
> +	return 0;
> +}
> +
> +static int reset_tree(struct object_id *i_tree, int update, int reset)
> +{
> +	struct unpack_trees_options opts;
> +	int nr_trees = 1;
> +	struct tree_desc t[MAX_UNPACK_TREES];
> +	struct tree *tree;
> +	struct lock_file lock_file = LOCK_INIT;
> +
> +	read_cache_preload(NULL);
> +	if (refresh_cache(REFRESH_QUIET))
> +		return -1;
> +
> +	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
> +
> +	memset(&opts, 0, sizeof(opts));
> +
> +	tree = parse_tree_indirect(i_tree);
> +	if (parse_tree(tree))
> +		return -1;
> +
> +	init_tree_desc(t, tree->buffer, tree->size);
> +
> +	opts.head_idx = 1;
> +	opts.src_index = &the_index;
> +	opts.dst_index = &the_index;
> +	opts.merge = 1;
> +	opts.reset = reset;
> +	opts.update = update;
> +	opts.fn = oneway_merge;
> +
> +	if (unpack_trees(nr_trees, t, &opts))
> +		return -1;
> +
> +	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
> +		return error(_("unable to write new index file"));
> +
> +	return 0;
> +}

Oh how I wish we had that as API:

$ git grep -W unpack_trees \*.c | grep init_tree_desc
archive.c-              init_tree_desc(&t, args->tree->buffer,
args->tree->size);
builtin/am.c-   init_tree_desc(&t[0], head->buffer, head->size);
builtin/am.c-   init_tree_desc(&t[1], remote->buffer, remote->size);
builtin/am.c-   init_tree_desc(&t[0], tree->buffer, tree->size);
builtin/checkout.c-     init_tree_desc(&tree_desc, tree->buffer, tree->size);
builtin/checkout.c-             init_tree_desc(&trees[0], tree->buffer, tree->size);
builtin/checkout.c-             init_tree_desc(&trees[1], tree->buffer, tree->size);
builtin/clone.c-        init_tree_desc(&t, tree->buffer, tree->size);
builtin/commit.c-       init_tree_desc(&t, tree->buffer, tree->size);
builtin/merge.c-                init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
builtin/read-tree.c-            init_tree_desc(t+i, tree->buffer, tree->size);
diff-lib.c-     init_tree_desc(&t, tree->buffer, tree->size);
merge-recursive.c-      init_tree_desc_from_tree(t+0, common);
merge-recursive.c-      init_tree_desc_from_tree(t+1, head);
merge-recursive.c-      init_tree_desc_from_tree(t+2, merge);
merge.c-                init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);

Looks like a lot of duplication to me. (Again, this is just a lament, and
a suggestion for a future project, not a micro-project, though).

> +
> +static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
> +{
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +	const char *w_commit_hex = oid_to_hex(w_commit);
> +
> +	/* Diff-tree would not be very hard to replace with a native function,
> +	 * however it should be done together with apply_cached.
> +	 */
> +	cp.git_cmd = 1;
> +	argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
> +	argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
> +
> +	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
> +}

As I stated before: I think this is good enough for the moment. We can
always spend time later to reduce these spawned processes and call
in-process functions instead. One teeny tiny step at a time.

> +static int apply_cached(struct strbuf *out)
> +{
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +
> +	/* Apply currently only reads either from stdin or a file, thus
> +	 * apply_all_patches would have to be updated to optionally take a buffer
> +	 */
> +	cp.git_cmd = 1;
> +	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
> +	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
> +}
> +
> +static int reset_head(const char *prefix)
> +{
> +	struct argv_array args = ARGV_ARRAY_INIT;
> +
> +	/* Reset is overall quite simple, however there is no current public API
> +	 * for resetting.
> +	 */
> +	argv_array_push(&args, "reset");
> +	return cmd_reset(args.argc, args.argv, prefix);
> +}
> +
> +static int diff_cached_index(struct strbuf *out, struct object_id *c_tree)
> +{
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +	const char *c_tree_hex = oid_to_hex(c_tree);
> +
> +	/* diff-index is very similar to diff-tree above, and should be converted
> +	 * together with update_index
> +	 */
> +	cp.git_cmd = 1;
> +	argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only", "--diff-filter=A", NULL);

We should probably note in the name of this function that we return a list
only of the added files.

> +	argv_array_push(&cp.args, c_tree_hex);
> +	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
> +}
> +
> +static int update_index(struct strbuf *out)
> +{
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +
> +	/* Update-index is very complicated and may need to have a public function
> +	 * exposed in order to remove this forking
> +	 */
> +	cp.git_cmd = 1;
> +	argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
> +	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
> +}
> +
> +static int restore_untracked(struct object_id *u_tree)
> +{
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +	int res;
> +
> +	/*
> +	 * We need to run restore files from a given index, but without affecting
> +	 * the current index, so we use GIT_INDEX_FILE with run_command to fork
> +	 * processes that will not interfere.
> +	 */
> +	cp.git_cmd = 1;
> +	argv_array_push(&cp.args, "read-tree");
> +	argv_array_push(&cp.args, oid_to_hex(u_tree));
> +	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
> +	if (run_command(&cp)) {
> +		remove_path(stash_index_path.buf);
> +		return -1;
> +	}
> +
> +	child_process_init(&cp);
> +	cp.git_cmd = 1;
> +	argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
> +	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
> +
> +	res = run_command(&cp);
> +	remove_path(stash_index_path.buf);
> +	return res;
> +}
> +
> +static int do_apply_stash(const char *prefix, struct stash_info *info, int index)
> +{
> +	struct merge_options o;
> +	struct object_id c_tree;
> +	struct object_id index_tree;
> +	const struct object_id *bases[1];
> +	int bases_count = 1;

It is always 1, maybe not waste a variable on it? ;-)

> +	struct commit *result;
> +	int ret;
> +	int has_index = index;
> +
> +	read_cache_preload(NULL);
> +	if (refresh_cache(REFRESH_QUIET))
> +		return -1;
> +
> +	if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))
> +		return error(_("Cannot apply a stash in the middle of a merge"));
> +
> +	if (index) {
> +		if (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree, &info->i_tree)) {
> +			has_index = 0;
> +		} else {
> +			struct strbuf out = STRBUF_INIT;
> +
> +			if (diff_tree_binary(&out, &info->w_commit)) {
> +				strbuf_release(&out);
> +				return -1;
> +			}
> +
> +			ret = apply_cached(&out);
> +			strbuf_release(&out);
> +			if (ret)
> +				return -1;

A more faithful conversion would pipe the output of `git diff-tree
--binary` into `git apply --cached`, but I do not think it is worth
complicating things here.

> +
> +			discard_cache();
> +			read_cache();

$ git grep -A1 discard_cache | grep -c read_cache
10

I guess that's another idiom we use a lot. (Again, micro-project here.)

> +			if (write_cache_as_tree(&index_tree, 0, NULL))
> +				return -1;
> +
> +			reset_head(prefix);
> +		}
> +	}
> +
> +	if (info->has_u) {
> +		if (restore_untracked(&info->u_tree))
> +			return error(_("Could not restore untracked files from stash"));
> +	}
> +
> +	init_merge_options(&o);
> +
> +	o.branch1 = "Updated upstream";
> +	o.branch2 = "Stashed changes";
> +
> +	if (!oidcmp(&info->b_tree, &c_tree))
> +		o.branch1 = "Version stash was based on";
> +
> +	if (quiet)
> +		o.verbosity = 0;
> +
> +	if (o.verbosity >= 3)
> +		printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
> +
> +	bases[0] = &info->b_tree;
> +
> +	ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, bases_count, bases, &result);
> +	if (ret != 0) {
> +		rerere(0);
> +
> +		if (index)
> +			fprintf_ln(stderr, _("Index was not unstashed."));
> +
> +		return ret;
> +	}
> +
> +	if (has_index) {
> +		if (reset_tree(&index_tree, 0, 0))
> +			return -1;
> +	} else {
> +		struct strbuf out = STRBUF_INIT;
> +
> +		if (diff_cached_index(&out, &c_tree)) {
> +			strbuf_release(&out);
> +			return -1;
> +		}
> +
> +		if (reset_tree(&c_tree, 0, 1)) {
> +			strbuf_release(&out);
> +			return -1;
> +		}
> +
> +		ret = update_index(&out);
> +		strbuf_release(&out);
> +		if (ret)
> +			return -1;
> +
> +		discard_cache();
> +	}
> +
> +	if (!quiet) {
> +		struct argv_array args = ARGV_ARRAY_INIT;
> +		/* Status is quite simple and could be replaced with calls to wt_status
> +		 * in the future, but it adds complexities which may require more tests
> +		 */
> +		argv_array_push(&args, "status");
> +		cmd_status(args.argc, args.argv, prefix);
> +	}

This is a change in behavior. The shell script calls `git status` always,
and only *suppresses* the output if `--quiet` was passed. I guess the
difference in behavior really boils down only to refreshing the index.
Maybe we can do that here, too? Something like

	if (quiet) {
		if (refresh_cache(REFRESH_QUIET))
			warning("could not refresh index");
	} else {
		...

> +
> +	return 0;
> +}
> +
> +static int apply_stash(int argc, const char **argv, const char *prefix)
> +{
> +	int index = 0;
> +	struct stash_info info;
> +	int ret;
> +	struct option options[] = {
> +		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
> +		OPT_BOOL(0, "index", &index,
> +			N_("attempt to recreate the index")),
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			git_stash_helper_apply_usage, 0);
> +
> +	if (get_stash_info(&info, argc, argv))
> +		return -1;
> +
> +	ret = do_apply_stash(prefix, &info, index);

This is the only caller of that function. But I guess that `pop` also
needs to call it, so I guess it makes sense to keep `do_apply_stash()`
separate.

> +	free_stash_info(&info);
> +	return ret;
> +}
> +
> +int cmd_stash__helper(int argc, const char **argv, const char *prefix)
> +{
> +	int result = 0;
> +	pid_t pid = getpid();
> +	const char *index_file;
> +
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	git_config(git_default_config, NULL);
> +
> +	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
> +		PARSE_OPT_KEEP_UNKNOWN|PARSE_OPT_KEEP_DASHDASH);

Please keep spaces around the pipe symbol.

> +	index_file = get_index_file();
> +	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, (uintmax_t)pid);
> +
> +	if (argc < 1)
> +		usage_with_options(git_stash_helper_usage, options);
> +	else if (!strcmp(argv[0], "apply"))
> +		result = apply_stash(argc, argv, prefix);
> +	else {
> +		error(_("unknown subcommand: %s"), argv[0]);
> +		usage_with_options(git_stash_helper_usage, options);
> +		result = 1;
> +	}
> +
> +	return result;
> +}
> diff --git a/git-stash.sh b/git-stash.sh
> index 94793c1a91..0b5d1f3743 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -566,76 +566,11 @@ assert_stash_ref() {
>  }
>  
>  apply_stash () {
> -
> -	assert_stash_like "$@"
> -
> -	git update-index -q --refresh || die "$(gettext "unable to refresh index")"
> -
> -	# current index state
> -	c_tree=$(git write-tree) ||
> -		die "$(gettext "Cannot apply a stash in the middle of a merge")"
> -
> -	unstashed_index_tree=
> -	if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
> -			test "$c_tree" != "$i_tree"
> -	then
> -		git diff-tree --binary $s^2^..$s^2 | git apply --cached
> -		test $? -ne 0 &&
> -			die "$(gettext "Conflicts in index. Try without --index.")"
> -		unstashed_index_tree=$(git write-tree) ||
> -			die "$(gettext "Could not save index tree")"
> -		git reset
> -	fi
> -
> -	if test -n "$u_tree"
> -	then
> -		GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
> -		GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
> -		rm -f "$TMPindex" ||
> -		die "$(gettext "Could not restore untracked files from stash entry")"
> -	fi
> -
> -	eval "
> -		GITHEAD_$w_tree='Stashed changes' &&
> -		GITHEAD_$c_tree='Updated upstream' &&
> -		GITHEAD_$b_tree='Version stash was based on' &&
> -		export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
> -	"
> -
> -	if test -n "$GIT_QUIET"
> -	then
> -		GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
> -	fi
> -	if git merge-recursive $b_tree -- $c_tree $w_tree
> -	then
> -		# No conflict
> -		if test -n "$unstashed_index_tree"
> -		then
> -			git read-tree "$unstashed_index_tree"
> -		else
> -			a="$TMP-added" &&
> -			git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
> -			git read-tree --reset $c_tree &&
> -			git update-index --add --stdin <"$a" ||
> -				die "$(gettext "Cannot unstage modified files")"
> -			rm -f "$a"
> -		fi
> -		squelch=
> -		if test -n "$GIT_QUIET"
> -		then
> -			squelch='>/dev/null 2>&1'
> -		fi
> -		(cd "$START_DIR" && eval "git status $squelch") || :
> -	else
> -		# Merge conflict; keep the exit status from merge-recursive
> -		status=$?
> -		git rerere
> -		if test -n "$INDEX_OPTION"
> -		then
> -			gettextln "Index was not unstashed." >&2
> -		fi
> -		exit $status
> -	fi
> +	cd "$START_DIR"
> +	git stash--helper apply "$@"
> +	res=$?
> +	cd_to_toplevel

Is it important to cd to top-level when we are exiting anyway? I guess it
is, because we are not exiting right away, necessarily: both pop_stash()
and apply_to_branch() call this function. But then, both callers only call
drop_stash() afterwards (which does not perform worktree operations)...

> +	return $res
>  }
>  
>  pop_stash() {
> diff --git a/git.c b/git.c
> index ceaa58ef40..6ffe6364ac 100644
> --- a/git.c
> +++ b/git.c
> @@ -466,6 +466,7 @@ static struct cmd_struct commands[] = {
>  	{ "show-branch", cmd_show_branch, RUN_SETUP },
>  	{ "show-ref", cmd_show_ref, RUN_SETUP },
>  	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> +	{ "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
>  	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
>  	{ "stripspace", cmd_stripspace },
>  	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX},
> -- 
> 2.16.3

This patch (apart from the minor suggestions I had) looks very good to me
already. Thank you so much!

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 2/5] stash: convert apply to builtin
  2018-04-05  2:28 ` [PATCH v5 2/5] stash: convert apply to builtin Joel Teichroeb
  2018-04-05  7:50   ` Christian Couder
  2018-04-06 15:10   ` Johannes Schindelin
@ 2018-04-06 15:17   ` Johannes Schindelin
  2 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-06 15:17 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hi Joel,

completely forgot to reiterate this (my reply to Christian was probably
buried in the thread):

On Wed, 4 Apr 2018, Joel Teichroeb wrote:

> +int cmd_stash__helper(int argc, const char **argv, const char *prefix)
> +{
> +	int result = 0;
> +	pid_t pid = getpid();
> +	const char *index_file;
> +
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	git_config(git_default_config, NULL);
> +
> +	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
> +		PARSE_OPT_KEEP_UNKNOWN|PARSE_OPT_KEEP_DASHDASH);
> +
> +	index_file = get_index_file();
> +	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, (uintmax_t)pid);
> +
> +	if (argc < 1)
> +		usage_with_options(git_stash_helper_usage, options);
> +	else if (!strcmp(argv[0], "apply"))
> +		result = apply_stash(argc, argv, prefix);
> +	else {
> +		error(_("unknown subcommand: %s"), argv[0]);
> +		usage_with_options(git_stash_helper_usage, options);
> +		result = 1;
> +	}
> +
> +	return result;
> +}

The `result` variable can contain negative values, while the exit status
really should be either 0 or 1 (and the return value of
`cmd_stash__helper()` is what `run_builtin()` hands to `exit()` as exit
status). So please use

	return !!result;

here (which returns 0 if result is 0, and 1 otherwise).

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 3/5] stash: convert drop and clear to builtin
  2018-04-05  2:28 ` [PATCH v5 3/5] stash: convert drop and clear " Joel Teichroeb
@ 2018-04-06 15:39   ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-06 15:39 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hi Joel,

On Wed, 4 Apr 2018, Joel Teichroeb wrote:

> Add the drop and clear commands to the builtin helper. These two
> are each simple, but are being added together as they are quite
> related.

Makes sense.

> We have to unfortunately keep the drop and clear functions in the
> shell script as functions are called with parameters internally
> that are not valid when the commands are called externally. Once
> pop is converted they can both be removed.

Excellent explanation.

> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 9d00a9547d..520cd746c4 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -134,6 +146,29 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
>  	return 0;
>  }
>  
> +static int do_clear_stash(void)
> +{
> +	struct object_id obj;
> +	if (get_oid(ref_stash, &obj))
> +		return 0;
> +
> +	return delete_ref(NULL, ref_stash, &obj, 0);
> +}
> +
> +static int clear_stash(int argc, const char **argv, const char *prefix)
> +{
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options, git_stash_helper_clear_usage, PARSE_OPT_STOP_AT_NON_OPTION);
> +
> +	if (argc != 0)
> +		return error(_("git stash--helper clear with parameters is unimplemented"));
> +
> +	return do_clear_stash();
> +}
> +
>  static int reset_tree(struct object_id *i_tree, int update, int reset)
>  {
>  	struct unpack_trees_options opts;
> @@ -399,6 +434,74 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> +static int do_drop_stash(const char *prefix, struct stash_info *info)
> +{
> +	struct argv_array args = ARGV_ARRAY_INIT;
> +	int ret;
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +
> +	/* reflog does not provide a simple function for deleting refs. One will
> +	 * need to be added to avoid implementing too much reflog code here
> +	 */
> +	argv_array_pushl(&args, "reflog", "delete", "--updateref", "--rewrite", NULL);
> +	argv_array_push(&args, info->revision.buf);
> +	ret = cmd_reflog(args.argc, args.argv, prefix);
> +	if (!ret) {
> +		if (!quiet)
> +			printf(_("Dropped %s (%s)\n"), info->revision.buf, oid_to_hex(&info->w_commit));

This is a correct conversion. In the future, we will want to print this to
stderr, though.

> +	} else {
> +		return error(_("%s: Could not drop stash entry"), info->revision.buf);
> +	}
> +
> +	/* This could easily be replaced by get_oid, but currently it will throw a
> +	 * fatal error when a reflog is empty, which we can not recover from
> +	 */
> +	cp.git_cmd = 1;
> +	/* Even though --quiet is specified, rev-parse still outputs the hash */
> +	cp.no_stdout = 1;
> +	argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
> +	argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
> +	ret = run_command(&cp);

Since we already have `grab_oid()` (or `get_oidf()` if you heed my
suggestion), we could use that here right away.

> +
> +	if (ret)
> +		do_clear_stash();

I was a bit puzzled why we clear the entire stash when we only wanted to
drop one? Going back to the shell script, there is this helpful comment:

	# clear_stash if we just dropped the last stash entry

Can you please add that comment here, too? It really helps readers, such
as myself...

> +
> +	return 0;
> +}
> +
> +static int assert_stash_ref(struct stash_info *info)
> +{
> +	if (!info->is_stash_ref)
> +		return error(_("'%s' is not a stash reference"), info->revision.buf);
> +
> +	return 0;
> +}

I am a bit curious why that did not appear in the previous patch, as
`apply_stash()` in the shell script already called `assert_stash_ref()`...

The rest of the patch looks pretty good to me! Thank you.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 4/5] stash: convert branch to builtin
  2018-04-05  2:28 ` [PATCH v5 4/5] stash: convert branch " Joel Teichroeb
@ 2018-04-06 15:50   ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-06 15:50 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hi Joel,

On Wed, 4 Apr 2018, Joel Teichroeb wrote:

> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 520cd746c4..486796bb6a 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -502,6 +508,49 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> +static int branch_stash(int argc, const char **argv, const char *prefix)
> +{
> +	const char *branch = NULL;
> +	int ret;
> +	struct argv_array args = ARGV_ARRAY_INIT;
> +	struct stash_info info;
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			git_stash_helper_branch_usage, 0);
> +
> +	if (argc == 0)
> +		return error(_("No branch name specified"));

We should probably also test for argc > 2 and error out in that case...

> +
> +	branch = argv[0];
> +
> +	if (get_stash_info(&info, argc - 1, argv + 1))
> +		return -1;
> +
> +	/* Checkout does not currently provide a function for checking out a branch
> +	 * as cmd_checkout does a large amount of sanity checks first that we
> +	 * require here.
> +	 */

While this is true, this code comment is *prone* to become stale. Maybe
move this remark into the commit message?

> +	argv_array_pushl(&args, "checkout", "-b", NULL);
> +	argv_array_push(&args, branch);
> +	argv_array_push(&args, oid_to_hex(&info.b_commit));
> +	ret = cmd_checkout(args.argc, args.argv, prefix);
> +	if (ret) {
> +		free_stash_info(&info);
> +		return -1;
> +	}
> +
> +	ret = do_apply_stash(prefix, &info, 1);
> +	if (!ret && info.is_stash_ref)
> +		ret = do_drop_stash(prefix, &info);

An alternative way to translate that &&-chain would be to do this:

	ret = cmd_checkout(args.argc, args.argv, prefix);
	if (!ret)
		ret = do_apply_stash(prefix, &info, 1);
	if (!ret && info.is_stash_ref)
		ret = do_drop_stash(prefix, &info);

... which might be a bit easier to read and to maintain?

> +
> +	free_stash_info(&info);
> +
> +	return ret;
> +}
> +
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  {
>  	int result = 0;
> @@ -528,6 +577,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		result = clear_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "drop"))
>  		result = drop_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "branch"))
> +		result = branch_stash(argc, argv, prefix);
>  	else {
>  		error(_("unknown subcommand: %s"), argv[0]);
>  		usage_with_options(git_stash_helper_usage, options);
> diff --git a/git-stash.sh b/git-stash.sh
> index 0b8f07b38a..c5fd4c6c44 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -598,20 +598,6 @@ drop_stash () {
>  	clear_stash
>  }
>  
> -apply_to_branch () {
> -	test -n "$1" || die "$(gettext "No branch name specified")"
> -	branch=$1
> -	shift 1
> -
> -	set -- --index "$@"
> -	assert_stash_like "$@"
> -
> -	git checkout -b $branch $REV^ &&
> -	apply_stash "$@" && {
> -		test -z "$IS_STASH_REF" || drop_stash "$@"
> -	}
> -}
> -
>  test "$1" = "-p" && set "push" "$@"
>  
>  PARSE_CACHE='--not-parsed'
> @@ -672,7 +658,8 @@ pop)
>  	;;
>  branch)
>  	shift
> -	apply_to_branch "$@"
> +	cd "$START_DIR"
> +	git stash--helper branch "$@"
>  	;;
>  *)
>  	case $# in

The rest looks obviously good to me (I am not all that sure about the `cd
"$START_DIR"` but it definitely does not hurt).

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 5/5] stash: convert pop to builtin
  2018-04-05  2:28 ` [PATCH v5 5/5] stash: convert pop " Joel Teichroeb
@ 2018-04-06 16:12   ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-06 16:12 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hi Joel,

On Wed, 4 Apr 2018, Joel Teichroeb wrote:

> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 486796bb6a..7c8950bc90 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -13,6 +13,7 @@
>  
>  static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
> +	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),

In the shell version, it says `( pop | apply )`. I think we should do that
here, too.

> @@ -508,6 +514,39 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> +static int pop_stash(int argc, const char **argv, const char *prefix)
> +{
> +	int index = 0, ret;
> +	struct stash_info info;
> +	struct option options[] = {
> +		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
> +		OPT_BOOL(0, "index", &index,
> +			N_("attempt to recreate the index")),
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			git_stash_helper_pop_usage, 0);

I think we should test argc after this, too.

> +
> +	if (get_stash_info(&info, argc, argv))
> +		return -1;
> +
> +	if (assert_stash_ref(&info)) {
> +		free_stash_info(&info);
> +		return -1;
> +	}
> +
> +	if (do_apply_stash(prefix, &info, index)) {
> +		printf_ln(_("The stash entry is kept in case you need it again."));
> +		free_stash_info(&info);
> +		return -1;
> +	}

The same `if (!ret)` trick to convert &&-chains as I mentioned earlier
could be used here, too.

> +
> +	ret = do_drop_stash(prefix, &info);
> +	free_stash_info(&info);
> +	return ret;
> +}
> +
>  static int branch_stash(int argc, const char **argv, const char *prefix)
>  {
>  	const char *branch = NULL;
> @@ -577,6 +616,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		result = clear_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "drop"))
>  		result = drop_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "pop"))
> +		result = pop_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "branch"))
>  		result = branch_stash(argc, argv, prefix);
>  	else {
> diff --git a/git-stash.sh b/git-stash.sh
> index c5fd4c6c44..8f2640fe90 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -554,50 +554,6 @@ assert_stash_like() {
>  	}
>  }
>  
> -is_stash_ref() {
> -	is_stash_like "$@" && test -n "$IS_STASH_REF"
> -}
> -
> -assert_stash_ref() {
> -	is_stash_ref "$@" || {
> -		args="$*"
> -		die "$(eval_gettext "'\$args' is not a stash reference")"
> -	}
> -}
> -
> -apply_stash () {
> -	cd "$START_DIR"
> -	git stash--helper apply "$@"
> -	res=$?
> -	cd_to_toplevel
> -	return $res
> -}
> -
> -pop_stash() {
> -	assert_stash_ref "$@"
> -
> -	if apply_stash "$@"
> -	then
> -		drop_stash "$@"
> -	else
> -		status=$?
> -		say "$(gettext "The stash entry is kept in case you need it again.")"
> -		exit $status
> -	fi
> -}
> -
> -drop_stash () {
> -	assert_stash_ref "$@"
> -
> -	git reflog delete --updateref --rewrite "${REV}" &&
> -		say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
> -		die "$(eval_gettext "\${REV}: Could not drop stash entry")"
> -
> -	# clear_stash if we just dropped the last stash entry
> -	git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
> -	clear_stash
> -}
> -
>  test "$1" = "-p" && set "push" "$@"
>  
>  PARSE_CACHE='--not-parsed'
> @@ -634,7 +590,8 @@ push)
>  	;;
>  apply)
>  	shift
> -	apply_stash "$@"
> +	cd "$START_DIR"
> +	git stash--helper apply "$@"
>  	;;
>  clear)
>  	shift
> @@ -654,7 +611,8 @@ drop)
>  	;;
>  pop)
>  	shift
> -	pop_stash "$@"
> +	cd "$START_DIR"
> +	git stash--helper pop "$@"
>  	;;
>  branch)
>  	shift

Nice!

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 0/5] Convert some stash functionality to a builtin
  2018-04-05  2:28 [PATCH v5 0/5] Convert some stash functionality to a builtin Joel Teichroeb
                   ` (4 preceding siblings ...)
  2018-04-05  2:28 ` [PATCH v5 5/5] stash: convert pop " Joel Teichroeb
@ 2018-04-06 16:15 ` Johannes Schindelin
  2018-04-28 22:06   ` Paul-Sebastian Ungureanu
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
  6 siblings, 1 reply; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-06 16:15 UTC (permalink / raw)
  To: Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hi Joel,


On Wed, 4 Apr 2018, Joel Teichroeb wrote:

> I've been working on converting all of git stash to be a
> builtin, however it's hard to get it all working at once with
> limited time, so I've moved around half of it to a new
> stash--helper builtin and called these functions from the shell
> script. Once this is stabalized, it should be easier to convert
> the rest of the commands one at a time without breaking
> anything.
> 
> I've sent most of this code before, but that was targetting a
> full replacement of stash. The code is overall the same, but
> with some code review changes and updates for internal api
> changes.
> 
> Since there seems to be interest from GSOC students who want to
> work on converting builtins, I figured I should finish what I
> have that works now so they could build on top of it.
> 
> The code is based on next as write_cache_as_tree was changed to
> take an object ID. It can easily be rebase on master by changing
> the two calls to write_cache_as_tree to use tha hash.

Very nice, thank you!

I reviewed all five patches, and offered a couple of suggestion.

As is tradition on this list, I also offered way more comments that do not
require any action from you, please use discretion when reading those: I
definitely do not expect you to do anything about those "for the future"
comments.

This patch series is in a very good shape, methinks, and should not need a
whole lot more work to enter `next`. From that point on, a GSoC student
working on the `stash` project (if we get any) could take over.

Thank you so much!
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 1/5] stash: improve option parsing test coverage
  2018-04-06 13:06   ` Johannes Schindelin
@ 2018-04-06 22:48     ` Paul-Sebastian Ungureanu
  2018-04-09  8:32       ` Johannes Schindelin
  0 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-04-06 22:48 UTC (permalink / raw)
  To: Johannes Schindelin, Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster


On 06.04.2018 16:06, Johannes Schindelin wrote:
>> +	git stash clear &&
>> +	test_when_finished "git reset --hard HEAD" &&
>> +	echo foo >file2 &&
>> +	git stash &&
>> +	echo bar >file2 &&
>> +	git stash &&
>> +	test-chmtime =123456789 file2 &&
>> +	for type in apply pop "branch stash-branch"
>> +	do
>> +		test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
>> +		test_i18ngrep "Too many" err &&
>> +		test 123456789 = $(test-chmtime -v +0 file2 | sed 's/[^0-9].*$//') || return 1
> 
> Not your problem, but if there is future work on this (read: if I get to
> mentor a GSoC student, and if I get them to work on it: this idiom
> `test-chmtime -v +0 ... | sed ...` is too common, there really *should* be
> a `test-chmtime --get ...`).

Hi,

I submitted a patch for this [1].

[1]
https://public-inbox.org/git/20180406221947.28402-1-ungureanupaulsebastian@gmail.com/

Best regards,
Paul Ungureanu

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 1/5] stash: improve option parsing test coverage
  2018-04-06 22:48     ` Paul-Sebastian Ungureanu
@ 2018-04-09  8:32       ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-09  8:32 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu
  Cc: Joel Teichroeb, Git Mailing List, Thomas Gummerer,
	Christian Couder, Eric Sunshine, gitster

Hi Paul,

On Sat, 7 Apr 2018, Paul-Sebastian Ungureanu wrote:

> On 06.04.2018 16:06, Johannes Schindelin wrote:
> > > +	git stash clear &&
> > > +	test_when_finished "git reset --hard HEAD" &&
> > > +	echo foo >file2 &&
> > > +	git stash &&
> > > +	echo bar >file2 &&
> > > +	git stash &&
> > > +	test-chmtime =123456789 file2 &&
> > > +	for type in apply pop "branch stash-branch"
> > > +	do
> > > +		test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
> > > +		test_i18ngrep "Too many" err &&
> > > +		test 123456789 = $(test-chmtime -v +0 file2 | sed
> > > 's/[^0-9].*$//') || return 1
> > 
> > Not your problem, but if there is future work on this (read: if I get to
> > mentor a GSoC student, and if I get them to work on it: this idiom
> > `test-chmtime -v +0 ... | sed ...` is too common, there really *should* be
> > a `test-chmtime --get ...`).
> 
> Hi,
> 
> I submitted a patch for this [1].
> 
> [1]
> https://public-inbox.org/git/20180406221947.28402-1-ungureanupaulsebastian@gmail.com/

Excellent, thank you so much!
Johannes

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 0/5] Convert some stash functionality to a builtin
  2018-04-06 16:15 ` [PATCH v5 0/5] Convert some stash functionality to a builtin Johannes Schindelin
@ 2018-04-28 22:06   ` Paul-Sebastian Ungureanu
  2018-04-29 13:04     ` Johannes Schindelin
       [not found]     ` <CA+CzEk8c1Pt+n9Jy5vL9_K60Q=6VKnLTdBY1JFRnb-POuRFv0Q@mail.gmail.com>
  0 siblings, 2 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-04-28 22:06 UTC (permalink / raw)
  To: Johannes Schindelin, Joel Teichroeb
  Cc: Git Mailing List, Thomas Gummerer, Christian Couder,
	Eric Sunshine, gitster

Hello Joel,

>> Since there seems to be interest from GSOC students who want to
>> work on converting builtins, I figured I should finish what I
>> have that works now so they could build on top of it.

First of all, I must thank you for submitting this series of patches. It 
is a great starting point to convert 'git stash'.

I would like to continue your work on "git stash" if that is fine with 
you. I will continue to build on top of it, starting with applying some 
patches in order to implement what was already suggested in this thread. 
  During the summer, I am planning to finish this process and, 
hopefully, have a 100% C, built-in 'git stash'.

Best regards,
Paul

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 0/5] Convert some stash functionality to a builtin
  2018-04-28 22:06   ` Paul-Sebastian Ungureanu
@ 2018-04-29 13:04     ` Johannes Schindelin
       [not found]     ` <CA+CzEk8c1Pt+n9Jy5vL9_K60Q=6VKnLTdBY1JFRnb-POuRFv0Q@mail.gmail.com>
  1 sibling, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-04-29 13:04 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu
  Cc: Joel Teichroeb, Git Mailing List, Thomas Gummerer,
	Christian Couder, Eric Sunshine, gitster

Hi,

On Sun, 29 Apr 2018, Paul-Sebastian Ungureanu wrote:

> > > Since there seems to be interest from GSOC students who want to
> > > work on converting builtins, I figured I should finish what I
> > > have that works now so they could build on top of it.
> 
> First of all, I must thank you for submitting this series of patches. It is a
> great starting point to convert 'git stash'.
> 
> I would like to continue your work on "git stash" if that is fine with you.

So you mean you would like to take custody of Joel's patches and address
the reviews so far, then re-submit?

Ciao,
Dscho

P.S.: Yes, I think I know that you want to do that (as we discussed on
IRC), but I just wanted to clarify on the mailing list, to avoid
misunderstandings ;-).

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v5 0/5] Convert some stash functionality to a builtin
       [not found]     ` <CA+CzEk8c1Pt+n9Jy5vL9_K60Q=6VKnLTdBY1JFRnb-POuRFv0Q@mail.gmail.com>
@ 2018-04-30 15:43       ` Joel Teichroeb
  0 siblings, 0 replies; 181+ messages in thread
From: Joel Teichroeb @ 2018-04-30 15:43 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu
  Cc: Johannes Schindelin, Git Mailing List, Thomas Gummerer,
	Christian Couder, Eric Sunshine, Junio C Hamano

Re-sending, but this time in plain text (why doesn't gmail for android
support that...)

On Mon, Apr 30, 2018 at 7:35 AM, Joel Teichroeb <joel@teichroeb.net> wrote:
> Hi Paul,
>
> That sounds great to me!
>
> Thanks,
> Joel
>
>
> On Sat, Apr 28, 2018, 3:06 PM Paul-Sebastian Ungureanu
> <ungureanupaulsebastian@gmail.com> wrote:
>>
>> Hello Joel,
>>
>> >> Since there seems to be interest from GSOC students who want to
>> >> work on converting builtins, I figured I should finish what I
>> >> have that works now so they could build on top of it.
>>
>> First of all, I must thank you for submitting this series of patches. It
>> is a great starting point to convert 'git stash'.
>>
>> I would like to continue your work on "git stash" if that is fine with
>> you. I will continue to build on top of it, starting with applying some
>> patches in order to implement what was already suggested in this thread.
>>   During the summer, I am planning to finish this process and,
>> hopefully, have a 100% C, built-in 'git stash'.
>>
>> Best regards,
>> Paul

^ permalink raw reply	[flat|nested] 181+ messages in thread

* [PATCH v6 0/4] stash: add new tests and introduce a new helper function
  2018-04-05  2:28 [PATCH v5 0/5] Convert some stash functionality to a builtin Joel Teichroeb
                   ` (5 preceding siblings ...)
  2018-04-06 16:15 ` [PATCH v5 0/5] Convert some stash functionality to a builtin Johannes Schindelin
@ 2018-06-25 16:42 ` Paul-Sebastian Ungureanu
  2018-06-25 16:42   ` [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid' Paul-Sebastian Ungureanu
                     ` (17 more replies)
  6 siblings, 18 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:42 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Hello,

This first series of patches does bring some changes and improvements to the
test suite. One of the patches also introduces a new function `get_oidf()` which
will be hepful for the incoming patches related to `git stash`.

Thanks,
Paul


Joel Teichroeb (1):
  stash: improve option parsing test coverage

Paul-Sebastian Ungureanu (3):
  sha1-name.c: added 'get_oidf', which acts like 'get_oid'
  stash: update test cases conform to coding guidelines
  stash: renamed test cases to be more descriptive

 cache.h          |   1 +
 sha1-name.c      |  19 ++++++
 t/t3903-stash.sh | 169 +++++++++++++++++++++++++++++------------------
 3 files changed, 123 insertions(+), 66 deletions(-)

-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply	[flat|nested] 181+ messages in thread

* [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid'
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
@ 2018-06-25 16:42   ` Paul-Sebastian Ungureanu
  2018-06-26 22:02     ` Johannes Schindelin
  2018-06-25 16:42   ` [PATCH v6 2/4] stash: improve option parsing test coverage Paul-Sebastian Ungureanu
                     ` (16 subsequent siblings)
  17 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:42 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Compared to 'get_oid', 'get_oidf' has as parameters a
printf format string and the additional arguments. This
will help simplify the code in subsequent commits.

Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 cache.h     |  1 +
 sha1-name.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/cache.h b/cache.h
index 89a107a7f..9252a4546 100644
--- a/cache.h
+++ b/cache.h
@@ -1321,6 +1321,7 @@ struct object_context {
 	GET_OID_BLOB)
 
 extern int get_oid(const char *str, struct object_id *oid);
+extern int get_oidf(struct object_id *oid, const char *fmt, ...);
 extern int get_oid_commit(const char *str, struct object_id *oid);
 extern int get_oid_committish(const char *str, struct object_id *oid);
 extern int get_oid_tree(const char *str, struct object_id *oid);
diff --git a/sha1-name.c b/sha1-name.c
index 60d9ef3c7..80ee8f742 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1466,6 +1466,25 @@ int get_oid(const char *name, struct object_id *oid)
 	return get_oid_with_context(name, 0, oid, &unused);
 }
 
+/*
+ * This returns a non-zero value if the string (built using printf
+ * format and the given arguments) is not a valid object.
+ */
+int get_oidf(struct object_id *oid, const char *fmt, ...)
+{
+	va_list ap;
+	int ret;
+	struct strbuf sb = STRBUF_INIT;
+
+	va_start(ap, fmt);
+	strbuf_vaddf(&sb, fmt, ap);
+	va_end(ap);
+
+	ret = get_oid(sb.buf, oid);
+	strbuf_release(&sb);
+
+	return ret;
+}
 
 /*
  * Many callers know that the user meant to name a commit-ish by
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 2/4] stash: improve option parsing test coverage
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
  2018-06-25 16:42   ` [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid' Paul-Sebastian Ungureanu
@ 2018-06-25 16:42   ` Paul-Sebastian Ungureanu
  2018-06-25 16:42   ` [PATCH v6 3/4] stash: update test cases conform to coding guidelines Paul-Sebastian Ungureanu
                     ` (15 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:42 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

From: Joel Teichroeb <joel@teichroeb.net>

In preparation for converting the stash command incrementally to
a builtin command, this patch improves test coverage of the option
parsing. Both for having too many parameters, or too few.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 1f871d3cc..af7586d43 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '
 	test foo = "$(cat file/file)"
 '
 
+test_expect_success 'giving too many ref arguments does not modify files' '
+	git stash clear &&
+	test_when_finished "git reset --hard HEAD" &&
+	echo foo >file2 &&
+	git stash &&
+	echo bar >file2 &&
+	git stash &&
+	test-tool chmtime =123456789 file2 &&
+	for type in apply pop "branch stash-branch"
+	do
+		test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
+		test_i18ngrep "Too many revisions" err &&
+		test 123456789 = $(test-tool chmtime -g file2) || return 1
+	done
+'
+
+test_expect_success 'drop: too many arguments errors out (does nothing)' '
+	git stash list >expect &&
+	test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
+	test_i18ngrep "Too many revisions" err &&
+	git stash list >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'show: too many arguments errors out (does nothing)' '
+	test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
+	test_i18ngrep "Too many revisions" err &&
+	test_must_be_empty out
+'
+
 test_expect_success 'stash create - no changes' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
@@ -479,6 +509,11 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
+test_expect_success 'stash branch complains with no arguments' '
+	test_must_fail git stash branch 2>err &&
+	test_i18ngrep "No branch name specified" err
+'
+
 test_expect_success 'stash show format defaults to --stat' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 3/4] stash: update test cases conform to coding guidelines
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
  2018-06-25 16:42   ` [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid' Paul-Sebastian Ungureanu
  2018-06-25 16:42   ` [PATCH v6 2/4] stash: improve option parsing test coverage Paul-Sebastian Ungureanu
@ 2018-06-25 16:42   ` Paul-Sebastian Ungureanu
  2018-06-26 22:08     ` Johannes Schindelin
  2018-06-25 16:42   ` [PATCH v6 4/4] stash: renamed test cases to be more descriptive Paul-Sebastian Ungureanu
                     ` (14 subsequent siblings)
  17 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:42 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Removed whitespaces after redirection operators.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 59 deletions(-)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index af7586d43..de6cab1fe 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -8,22 +8,22 @@ test_description='Test git stash'
 . ./test-lib.sh
 
 test_expect_success 'stash some dirty working directory' '
-	echo 1 > file &&
+	echo 1 >file &&
 	git add file &&
 	echo unrelated >other-file &&
 	git add other-file &&
 	test_tick &&
 	git commit -m initial &&
-	echo 2 > file &&
+	echo 2 >file &&
 	git add file &&
-	echo 3 > file &&
+	echo 3 >file &&
 	test_tick &&
 	git stash &&
 	git diff-files --quiet &&
 	git diff-index --cached --quiet HEAD
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/file b/file
 index 0cfbf08..00750ed 100644
 --- a/file
@@ -35,7 +35,7 @@ EOF
 
 test_expect_success 'parents of stash' '
 	test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
-	git diff stash^2..stash > output &&
+	git diff stash^2..stash >output &&
 	test_cmp output expect
 '
 
@@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '
 
 test_expect_success 'apply stashed changes (including index)' '
 	git reset --hard HEAD^ &&
-	echo 6 > other-file &&
+	echo 6 >other-file &&
 	git add other-file &&
 	test_tick &&
 	git commit -m other-file &&
@@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '
 
 test_expect_success 'drop top stash' '
 	git reset --hard &&
-	git stash list > stashlist1 &&
-	echo 7 > file &&
+	git stash list >expected &&
+	echo 7 >file &&
 	git stash &&
 	git stash drop &&
-	git stash list > stashlist2 &&
-	test_cmp stashlist1 stashlist2 &&
+	git stash list >actual &&
+	test_cmp expected actual &&
 	git stash apply &&
 	test 3 = $(cat file) &&
 	test 1 = $(git show :file) &&
@@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '
 
 test_expect_success 'drop middle stash' '
 	git reset --hard &&
-	echo 8 > file &&
+	echo 8 >file &&
 	git stash &&
-	echo 9 > file &&
+	echo 9 >file &&
 	git stash &&
 	git stash drop stash@{1} &&
 	test 2 = $(git stash list | wc -l) &&
@@ -160,7 +160,7 @@ test_expect_success 'stash pop' '
 	test 0 = $(git stash list | wc -l)
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/file2 b/file2
 new file mode 100644
 index 0000000..1fe912c
@@ -170,7 +170,7 @@ index 0000000..1fe912c
 +bar2
 EOF
 
-cat > expect1 << EOF
+cat >expect1 <<EOF
 diff --git a/file b/file
 index 257cc56..5716ca5 100644
 --- a/file
@@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644
 +bar
 EOF
 
-cat > expect2 << EOF
+cat >expect2 <<EOF
 diff --git a/file b/file
 index 7601807..5716ca5 100644
 --- a/file
@@ -198,79 +198,79 @@ index 0000000..1fe912c
 EOF
 
 test_expect_success 'stash branch' '
-	echo foo > file &&
+	echo foo >file &&
 	git commit file -m first &&
-	echo bar > file &&
-	echo bar2 > file2 &&
+	echo bar >file &&
+	echo bar2 >file2 &&
 	git add file2 &&
 	git stash &&
-	echo baz > file &&
+	echo baz >file &&
 	git commit file -m second &&
 	git stash branch stashbranch &&
 	test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
 	test $(git rev-parse HEAD) = $(git rev-parse master^) &&
-	git diff --cached > output &&
+	git diff --cached >output &&
 	test_cmp output expect &&
-	git diff > output &&
+	git diff >output &&
 	test_cmp output expect1 &&
 	git add file &&
 	git commit -m alternate\ second &&
-	git diff master..stashbranch > output &&
+	git diff master..stashbranch >output &&
 	test_cmp output expect2 &&
 	test 0 = $(git stash list | wc -l)
 '
 
 test_expect_success 'apply -q is quiet' '
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	git stash apply -q > output.out 2>&1 &&
+	git stash apply -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'save -q is quiet' '
-	git stash save --quiet > output.out 2>&1 &&
+	git stash save --quiet >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'pop -q is quiet' '
-	git stash pop -q > output.out 2>&1 &&
+	git stash pop -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'pop -q --index works and is quiet' '
-	echo foo > file &&
+	echo foo >file &&
 	git add file &&
 	git stash save --quiet &&
-	git stash pop -q --index > output.out 2>&1 &&
+	git stash pop -q --index >output.out 2>&1 &&
 	test foo = "$(git show :file)" &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'drop -q is quiet' '
 	git stash &&
-	git stash drop -q > output.out 2>&1 &&
+	git stash drop -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'stash -k' '
-	echo bar3 > file &&
-	echo bar4 > file2 &&
+	echo bar3 >file &&
+	echo bar4 >file2 &&
 	git add file2 &&
 	git stash -k &&
 	test bar,bar4 = $(cat file),$(cat file2)
 '
 
 test_expect_success 'stash --no-keep-index' '
-	echo bar33 > file &&
-	echo bar44 > file2 &&
+	echo bar33 >file &&
+	echo bar44 >file2 &&
 	git add file2 &&
 	git stash --no-keep-index &&
 	test bar,bar2 = $(cat file),$(cat file2)
 '
 
 test_expect_success 'stash --invalid-option' '
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	test_must_fail git stash --invalid-option &&
 	test_must_fail git stash save --invalid-option &&
@@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	git stash branch stash-branch ${STASH_ID} &&
-	test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
+	test_when_finished "git reset --hard HEAD && git checkout master &&
+	git branch -D stash-branch" &&
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
@@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	git stash branch stash-branch ${STASH_ID} &&
-	test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
+	test_when_finished "git reset --hard HEAD && git checkout master &&
+	git branch -D stash-branch" &&
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
@@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	echo "1	0	file" >expected &&
@@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	echo "1	0	file" >expected &&
@@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	echo bar > file &&
+	echo bar >file &&
 	git stash &&
 	test_must_fail git stash drop $(git rev-parse stash@{0}) &&
 	git stash pop &&
@@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	echo bar > file &&
+	echo bar >file &&
 	git stash &&
 	test_must_fail git stash pop $(git rev-parse stash@{0}) &&
 	git stash pop &&
@@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re
 
 test_expect_success 'ref with non-existent reflog' '
 	git stash clear &&
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	git stash &&
 	test_must_fail git rev-parse --quiet --verify does-not-exist &&
@@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '
 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash clear &&
 	test_must_fail git stash drop stash@{0} &&
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	git stash &&
 	test_must_fail git stash drop stash@{1} &&
@@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu
 	test_i18ncmp expect actual
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/HEAD b/HEAD
 new file mode 100644
 index 0000000..fe0cbee
@@ -737,14 +739,14 @@ EOF
 test_expect_success 'stash where working directory contains "HEAD" file' '
 	git stash clear &&
 	git reset --hard &&
-	echo file-not-a-ref > HEAD &&
+	echo file-not-a-ref >HEAD &&
 	git add HEAD &&
 	test_tick &&
 	git stash &&
 	git diff-files --quiet &&
 	git diff-index --cached --quiet HEAD &&
 	test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
-	git diff stash^..stash > output &&
+	git diff stash^..stash >output &&
 	test_cmp output expect
 '
 
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 4/4] stash: renamed test cases to be more descriptive
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (2 preceding siblings ...)
  2018-06-25 16:42   ` [PATCH v6 3/4] stash: update test cases conform to coding guidelines Paul-Sebastian Ungureanu
@ 2018-06-25 16:42   ` Paul-Sebastian Ungureanu
  2018-06-26 22:09     ` Johannes Schindelin
  2018-06-25 16:43   ` [PATCH v6 1/4] stash: convert apply to builtin Paul-Sebastian Ungureanu
                     ` (13 subsequent siblings)
  17 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:42 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Renamed some test cases' labels to be more descriptive and under 80
characters per line.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index de6cab1fe..8d002a7f2 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	test_cmp expected actual
 '
 
-test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
+test_expect_success 'drop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
@@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r
 	git reset --hard HEAD
 '
 
-test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
+test_expect_success 'pop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
@@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form "n", n >= N' '
 	git stash drop
 '
 
-test_expect_success 'stash branch should not drop the stash if the branch exists' '
+test_expect_success 'branch: should not drop the stash if the branch exists' '
 	git stash clear &&
 	echo foo >file &&
 	git add file &&
@@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists
 	git rev-parse stash@{0} --
 '
 
-test_expect_success 'stash branch should not drop the stash if the apply fails' '
+test_expect_success 'branch: should not drop the stash if the apply fails' '
 	git stash clear &&
 	git reset HEAD~1 --hard &&
 	echo foo >file &&
@@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'
 	git rev-parse stash@{0} --
 '
 
-test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
+test_expect_success 'apply: shows same status as git status (relative to ./)' '
 	git stash clear &&
 	echo 1 >subdir/subfile1 &&
 	echo 2 >subdir/subfile2 &&
@@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '
 	test_i18ncmp expect actual
 '
 
-test_expect_success 'stash push with pathspec shows no changes when there are none' '
+test_expect_success 'push: <pathspec> shows no changes when there are none' '
 	>foo &&
 	git add foo &&
 	git commit -m "tmp" &&
@@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no
 	test_i18ncmp expect actual
 '
 
-test_expect_success 'stash push with pathspec not in the repository errors out' '
+test_expect_success 'push: <pathspec> not in the repository errors out' '
 	>untracked &&
 	test_must_fail git stash push untracked &&
 	test_path_is_file untracked
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 1/4] stash: convert apply to builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (3 preceding siblings ...)
  2018-06-25 16:42   ` [PATCH v6 4/4] stash: renamed test cases to be more descriptive Paul-Sebastian Ungureanu
@ 2018-06-25 16:43   ` Paul-Sebastian Ungureanu
  2018-06-25 16:43   ` [PATCH v6 2/4] stash: convert drop and clear " Paul-Sebastian Ungureanu
                     ` (12 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:43 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

From: Joel Teichroeb <joel@teichroeb.net>

Add a builtin helper for performing stash commands. Converting
all at once proved hard to review, so starting with just apply
lets conversion get started without the other commands being
finished.

The helper is being implemented as a drop in replacement for
stash so that when it is complete it can simply be renamed and
the shell script deleted.

Delete the contents of the apply_stash shell function and replace
it with a call to stash--helper apply until pop is also
converted.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 .gitignore              |   1 +
 Makefile                |   1 +
 builtin.h               |   1 +
 builtin/stash--helper.c | 442 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |  78 +------
 git.c                   |   1 +
 6 files changed, 453 insertions(+), 71 deletions(-)
 create mode 100644 builtin/stash--helper.c

diff --git a/.gitignore b/.gitignore
index 388cc4bee..56e63dd24 100644
--- a/.gitignore
+++ b/.gitignore
@@ -155,6 +155,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
diff --git a/Makefile b/Makefile
index 1d27f3636..ca6072053 100644
--- a/Makefile
+++ b/Makefile
@@ -1078,6 +1078,7 @@ BUILTIN_OBJS += builtin/serve.o
 BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-ref.o
+BUILTIN_OBJS += builtin/stash--helper.o
 BUILTIN_OBJS += builtin/stripspace.o
 BUILTIN_OBJS += builtin/submodule--helper.o
 BUILTIN_OBJS += builtin/symbolic-ref.o
diff --git a/builtin.h b/builtin.h
index 4e0f64723..ba63ce0f6 100644
--- a/builtin.h
+++ b/builtin.h
@@ -221,6 +221,7 @@ extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
 extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
 extern int cmd_status(int argc, const char **argv, const char *prefix);
+extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
new file mode 100644
index 000000000..1c4387b10
--- /dev/null
+++ b/builtin/stash--helper.c
@@ -0,0 +1,442 @@
+#include "builtin.h"
+#include "config.h"
+#include "parse-options.h"
+#include "refs.h"
+#include "lockfile.h"
+#include "cache-tree.h"
+#include "unpack-trees.h"
+#include "merge-recursive.h"
+#include "argv-array.h"
+#include "run-command.h"
+#include "dir.h"
+#include "rerere.h"
+
+static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char * const git_stash_helper_apply_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char *ref_stash = "refs/stash";
+static int quiet;
+static struct strbuf stash_index_path = STRBUF_INIT;
+
+/*
+ * w_commit is set to the commit containing the working tree
+ * b_commit is set to the base commit
+ * i_commit is set to the commit containing the index tree
+ * u_commit is set to the commit containing the untracked files tree
+ * w_tree is set to the working tree
+ * b_tree is set to the base tree
+ * i_tree is set to the index tree
+ * u_tree is set to the untracked files tree
+ */
+
+struct stash_info {
+	struct object_id w_commit;
+	struct object_id b_commit;
+	struct object_id i_commit;
+	struct object_id u_commit;
+	struct object_id w_tree;
+	struct object_id b_tree;
+	struct object_id i_tree;
+	struct object_id u_tree;
+	struct strbuf revision;
+	int is_stash_ref;
+	int has_u;
+};
+
+static void free_stash_info(struct stash_info *info)
+{
+	strbuf_release(&info->revision);
+}
+
+static void assert_stash_like(struct stash_info *info, const char * revision)
+{
+	if (get_oidf(&info->b_commit, "%s^1", revision) ||
+	    get_oidf(&info->w_tree, "%s:", revision) ||
+	    get_oidf(&info->b_tree, "%s^1:", revision) ||
+	    get_oidf(&info->i_tree, "%s^2:", revision)) {
+		free_stash_info(info);
+		error(_("'%s' is not a stash-like commit"), revision);
+		exit(128);
+	}
+}
+
+static int get_stash_info(struct stash_info *info, int argc, const char **argv)
+{
+	struct strbuf symbolic = STRBUF_INIT;
+	int ret;
+	const char *revision;
+	const char *commit = NULL;
+	char *end_of_rev;
+	char *expanded_ref;
+	struct object_id dummy;
+
+	if (argc > 1) {
+		int i;
+		struct strbuf refs_msg = STRBUF_INIT;
+		for (i = 0; i < argc; ++i)
+			strbuf_addf(&refs_msg, " '%s'", argv[i]);
+
+		fprintf_ln(stderr, _("Too many revisions specified:%s"), refs_msg.buf);
+		strbuf_release(&refs_msg);
+
+		return -1;
+	}
+
+	if (argc == 1)
+		commit = argv[0];
+
+	strbuf_init(&info->revision, 0);
+	if (!commit) {
+		if (!ref_exists(ref_stash)) {
+			free_stash_info(info);
+			fprintf_ln(stderr, "No stash entries found.");
+			return -1;
+		}
+
+		strbuf_addf(&info->revision, "%s@{0}", ref_stash);
+	} else if (strspn(commit, "0123456789") == strlen(commit)) {
+		strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
+	} else {
+		strbuf_addstr(&info->revision, commit);
+	}
+
+	revision = info->revision.buf;
+
+	if (get_oid(revision, &info->w_commit)) {
+		error(_("%s is not a valid reference"), revision);
+		free_stash_info(info);
+		return -1;
+	}
+
+	assert_stash_like(info, revision);
+
+	info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
+
+	end_of_rev = strchrnul(revision, '@');
+	strbuf_add(&symbolic, revision, end_of_rev - revision);
+
+	ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
+	strbuf_release(&symbolic);
+	switch (ret) {
+	case 0: /* Not found, but valid ref */
+		info->is_stash_ref = 0;
+		break;
+	case 1:
+		info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
+		break;
+	default: /* Invalid or ambiguous */
+		free_stash_info(info);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int reset_tree(struct object_id *i_tree, int update, int reset)
+{
+	struct unpack_trees_options opts;
+	int nr_trees = 1;
+	struct tree_desc t[MAX_UNPACK_TREES];
+	struct tree *tree;
+	struct lock_file lock_file = LOCK_INIT;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+
+	memset(&opts, 0, sizeof(opts));
+
+	tree = parse_tree_indirect(i_tree);
+	if (parse_tree(tree))
+		return -1;
+
+	init_tree_desc(t, tree->buffer, tree->size);
+
+	opts.head_idx = 1;
+	opts.src_index = &the_index;
+	opts.dst_index = &the_index;
+	opts.merge = 1;
+	opts.reset = reset;
+	opts.update = update;
+	opts.fn = oneway_merge;
+
+	if (unpack_trees(nr_trees, t, &opts))
+		return -1;
+
+	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		return error(_("unable to write new index file"));
+
+	return 0;
+}
+
+static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *w_commit_hex = oid_to_hex(w_commit);
+
+	/*
+	 * Diff-tree would not be very hard to replace with a native function,
+	 * however it should be done together with apply_cached.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
+	argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
+
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int apply_cached(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Apply currently only reads either from stdin or a file, thus
+	 * apply_all_patches would have to be updated to optionally take a buffer
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int reset_head(const char *prefix)
+{
+	struct argv_array args = ARGV_ARRAY_INIT;
+
+	/*
+	 * Reset is overall quite simple, however there is no current public API
+	 * for resetting.
+	 */
+	argv_array_push(&args, "reset");
+	return cmd_reset(args.argc, args.argv, prefix);
+}
+
+static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *c_tree_hex = oid_to_hex(c_tree);
+
+	/*
+	 * diff-index is very similar to diff-tree above, and should be converted
+	 * together with update_index
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only",
+			 "--diff-filter=A", NULL);
+	argv_array_push(&cp.args, c_tree_hex);
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int update_index(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Update-index is very complicated and may need to have a public function
+	 * exposed in order to remove this forking
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int restore_untracked(struct object_id *u_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int res;
+
+	/*
+	 * We need to run restore files from a given index, but without affecting
+	 * the current index, so we use GIT_INDEX_FILE with run_command to fork
+	 * processes that will not interfere.
+	 */
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "read-tree");
+	argv_array_push(&cp.args, oid_to_hex(u_tree));
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
+	if (run_command(&cp)) {
+		remove_path(stash_index_path.buf);
+		return -1;
+	}
+
+	child_process_init(&cp);
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", stash_index_path.buf);
+
+	res = run_command(&cp);
+	remove_path(stash_index_path.buf);
+	return res;
+}
+
+static int do_apply_stash(const char *prefix, struct stash_info *info,
+	int index)
+{
+	struct merge_options o;
+	struct object_id c_tree;
+	struct object_id index_tree;
+	const struct object_id *bases[1];
+	struct commit *result;
+	int ret;
+	int has_index = index;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))
+		return error(_("Cannot apply a stash in the middle of a merge"));
+
+	if (index) {
+		if (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree,
+			&info->i_tree)) {
+			has_index = 0;
+		} else {
+			struct strbuf out = STRBUF_INIT;
+
+			if (diff_tree_binary(&out, &info->w_commit)) {
+				strbuf_release(&out);
+				return -1;
+			}
+
+			ret = apply_cached(&out);
+			strbuf_release(&out);
+			if (ret)
+				return -1;
+
+			discard_cache();
+			read_cache();
+			if (write_cache_as_tree(&index_tree, 0, NULL))
+				return -1;
+
+			reset_head(prefix);
+		}
+	}
+
+	if (info->has_u && restore_untracked(&info->u_tree))
+		return error(_("Could not restore untracked files from stash"));
+
+	init_merge_options(&o);
+
+	o.branch1 = "Updated upstream";
+	o.branch2 = "Stashed changes";
+
+	if (!oidcmp(&info->b_tree, &c_tree))
+		o.branch1 = "Version stash was based on";
+
+	if (quiet)
+		o.verbosity = 0;
+
+	if (o.verbosity >= 3)
+		printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
+
+	bases[0] = &info->b_tree;
+
+	ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
+				      &result);
+	if (ret != 0) {
+		rerere(0);
+
+		if (index)
+			fprintf_ln(stderr, _("Index was not unstashed."));
+
+		return ret;
+	}
+
+	if (has_index) {
+		if (reset_tree(&index_tree, 0, 0))
+			return -1;
+	} else {
+		struct strbuf out = STRBUF_INIT;
+
+		if (get_newly_staged(&out, &c_tree)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		if (reset_tree(&c_tree, 0, 1)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		ret = update_index(&out);
+		strbuf_release(&out);
+		if (ret)
+			return -1;
+
+		discard_cache();
+	}
+
+	if (quiet) {
+		if (refresh_cache(REFRESH_QUIET))
+			warning("could not refresh index");
+	} else {
+		struct argv_array args = ARGV_ARRAY_INIT;
+		/*
+		 * Status is quite simple and could be replaced with calls to wt_status
+		 * in the future, but it adds complexities which may require more tests
+		 */
+		argv_array_push(&args, "status");
+		cmd_status(args.argc, args.argv, prefix);
+	}
+
+	return 0;
+}
+
+static int apply_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0;
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_apply_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	ret = do_apply_stash(prefix, &info, index);
+	free_stash_info(&info);
+	return ret;
+}
+
+int cmd_stash__helper(int argc, const char **argv, const char *prefix)
+{
+	pid_t pid = getpid();
+	const char *index_file;
+
+	struct option options[] = {
+		OPT_END()
+	};
+
+	git_config(git_default_config, NULL);
+
+	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
+			     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
+
+	index_file = get_index_file();
+	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
+		    (uintmax_t)pid);
+
+	if (argc < 1)
+		usage_with_options(git_stash_helper_usage, options);
+	if (!strcmp(argv[0], "apply"))
+		return !!apply_stash(argc, argv, prefix);
+
+	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
+		      git_stash_helper_usage, options);
+}
diff --git a/git-stash.sh b/git-stash.sh
index 94793c1a9..809b1c2d1 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -566,76 +566,11 @@ assert_stash_ref() {
 }
 
 apply_stash () {
-
-	assert_stash_like "$@"
-
-	git update-index -q --refresh || die "$(gettext "unable to refresh index")"
-
-	# current index state
-	c_tree=$(git write-tree) ||
-		die "$(gettext "Cannot apply a stash in the middle of a merge")"
-
-	unstashed_index_tree=
-	if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
-			test "$c_tree" != "$i_tree"
-	then
-		git diff-tree --binary $s^2^..$s^2 | git apply --cached
-		test $? -ne 0 &&
-			die "$(gettext "Conflicts in index. Try without --index.")"
-		unstashed_index_tree=$(git write-tree) ||
-			die "$(gettext "Could not save index tree")"
-		git reset
-	fi
-
-	if test -n "$u_tree"
-	then
-		GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
-		GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
-		rm -f "$TMPindex" ||
-		die "$(gettext "Could not restore untracked files from stash entry")"
-	fi
-
-	eval "
-		GITHEAD_$w_tree='Stashed changes' &&
-		GITHEAD_$c_tree='Updated upstream' &&
-		GITHEAD_$b_tree='Version stash was based on' &&
-		export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
-	"
-
-	if test -n "$GIT_QUIET"
-	then
-		GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
-	fi
-	if git merge-recursive $b_tree -- $c_tree $w_tree
-	then
-		# No conflict
-		if test -n "$unstashed_index_tree"
-		then
-			git read-tree "$unstashed_index_tree"
-		else
-			a="$TMP-added" &&
-			git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
-			git read-tree --reset $c_tree &&
-			git update-index --add --stdin <"$a" ||
-				die "$(gettext "Cannot unstage modified files")"
-			rm -f "$a"
-		fi
-		squelch=
-		if test -n "$GIT_QUIET"
-		then
-			squelch='>/dev/null 2>&1'
-		fi
-		(cd "$START_DIR" && eval "git status $squelch") || :
-	else
-		# Merge conflict; keep the exit status from merge-recursive
-		status=$?
-		git rerere
-		if test -n "$INDEX_OPTION"
-		then
-			gettextln "Index was not unstashed." >&2
-		fi
-		exit $status
-	fi
+	cd "$START_DIR"
+	git stash--helper apply "$@"
+	res=$?
+	cd_to_toplevel
+	return $res
 }
 
 pop_stash() {
@@ -713,7 +648,8 @@ push)
 	;;
 apply)
 	shift
-	apply_stash "$@"
+	cd "$START_DIR"
+	git stash--helper apply "$@"
 	;;
 clear)
 	shift
diff --git a/git.c b/git.c
index c2f48d53d..d43c42e31 100644
--- a/git.c
+++ b/git.c
@@ -539,6 +539,7 @@ static struct cmd_struct commands[] = {
 	{ "show-branch", cmd_show_branch, RUN_SETUP },
 	{ "show-ref", cmd_show_ref, RUN_SETUP },
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
 	{ "stripspace", cmd_stripspace },
 	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 2/4] stash: convert drop and clear to builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (4 preceding siblings ...)
  2018-06-25 16:43   ` [PATCH v6 1/4] stash: convert apply to builtin Paul-Sebastian Ungureanu
@ 2018-06-25 16:43   ` Paul-Sebastian Ungureanu
  2018-06-26 22:17     ` Johannes Schindelin
  2018-06-25 16:43   ` [PATCH v6 3/4] stash: convert branch " Paul-Sebastian Ungureanu
                     ` (11 subsequent siblings)
  17 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:43 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

From: Joel Teichroeb <joel@teichroeb.net>

Add the drop and clear commands to the builtin helper. These two
are each simple, but are being added together as they are quite
related.

We have to unfortunately keep the drop and clear functions in the
shell script as functions are called with parameters internally
that are not valid when the commands are called externally. Once
pop is converted they can both be removed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 112 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   4 +-
 2 files changed, 114 insertions(+), 2 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 1c4387b10..84a537f39 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,7 +12,14 @@
 #include "rerere.h"
 
 static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper clear"),
+	NULL
+};
+
+static const char * const git_stash_helper_drop_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
 };
 
@@ -21,6 +28,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_clear_usage[] = {
+	N_("git stash--helper clear"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -139,6 +151,31 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
 	return 0;
 }
 
+static int do_clear_stash(void)
+{
+	struct object_id obj;
+	if (get_oid(ref_stash, &obj))
+		return 0;
+
+	return delete_ref(NULL, ref_stash, &obj, 0);
+}
+
+static int clear_stash(int argc, const char **argv, const char *prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_clear_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (argc != 0)
+		return error(_("git stash--helper clear with parameters is unimplemented"));
+
+	return do_clear_stash();
+}
+
 static int reset_tree(struct object_id *i_tree, int update, int reset)
 {
 	struct unpack_trees_options opts;
@@ -414,6 +451,77 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int do_drop_stash(const char *prefix, struct stash_info *info)
+{
+	struct argv_array args = ARGV_ARRAY_INIT;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int ret;
+
+	/*
+	 * reflog does not provide a simple function for deleting refs. One will
+	 * need to be added to avoid implementing too much reflog code here
+	 */
+	argv_array_pushl(&args, "reflog", "delete", "--updateref", "--rewrite",
+			 NULL);
+	argv_array_push(&args, info->revision.buf);
+	ret = cmd_reflog(args.argc, args.argv, prefix);
+	if (!ret) {
+		if (!quiet)
+			printf(_("Dropped %s (%s)\n"), info->revision.buf,
+			       oid_to_hex(&info->w_commit));
+	} else {
+		return error(_("%s: Could not drop stash entry"), info->revision.buf);
+	}
+
+	/*
+	 * This could easily be replaced by get_oid, but currently it will throw a
+	 * fatal error when a reflog is empty, which we can not recover from
+	 */
+	cp.git_cmd = 1;
+	/* Even though --quiet is specified, rev-parse still outputs the hash */
+	cp.no_stdout = 1;
+	argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
+	argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
+	ret = run_command(&cp);
+
+	/* do_clear_stash if we just dropped the last stash entry */
+	if (ret)
+		do_clear_stash();
+
+	return 0;
+}
+
+static void assert_stash_ref(struct stash_info *info)
+{
+	if (!info->is_stash_ref) {
+		free_stash_info(info);
+		error(_("'%s' is not a stash reference"), info->revision.buf);
+		exit(128);
+	}
+}
+
+static int drop_stash(int argc, const char **argv, const char *prefix)
+{
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_drop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	assert_stash_ref(&info);
+
+	ret = do_drop_stash(prefix, &info);
+	free_stash_info(&info);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -436,6 +544,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_stash_helper_usage, options);
 	if (!strcmp(argv[0], "apply"))
 		return !!apply_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "clear"))
+		return !!clear_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "drop"))
+		return !!drop_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 809b1c2d1..a99d5dc9e 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -653,7 +653,7 @@ apply)
 	;;
 clear)
 	shift
-	clear_stash "$@"
+	git stash--helper clear "$@"
 	;;
 create)
 	shift
@@ -665,7 +665,7 @@ store)
 	;;
 drop)
 	shift
-	drop_stash "$@"
+	git stash--helper drop "$@"
 	;;
 pop)
 	shift
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 3/4] stash: convert branch to builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (5 preceding siblings ...)
  2018-06-25 16:43   ` [PATCH v6 2/4] stash: convert drop and clear " Paul-Sebastian Ungureanu
@ 2018-06-25 16:43   ` Paul-Sebastian Ungureanu
  2018-06-26 22:23     ` Johannes Schindelin
  2018-06-25 16:43   ` [PATCH v6 4/4] stash: convert pop " Paul-Sebastian Ungureanu
                     ` (10 subsequent siblings)
  17 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:43 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

From: Joel Teichroeb <joel@teichroeb.net>

Add stash branch to the helper and delete the apply_to_branch
function from the shell script.

Checkout does not currently provide a function for checking out
a branch as cmd_checkout does a large amount of sanity checks
first that we require here.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 43 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 17 ++--------------
 2 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 84a537f39..fbf78249c 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -14,6 +14,7 @@
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	NULL
 };
@@ -28,6 +29,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_branch_usage[] = {
+	N_("git stash--helper branch <branchname> [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_clear_usage[] = {
 	N_("git stash--helper clear"),
 	NULL
@@ -522,6 +528,41 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int branch_stash(int argc, const char **argv, const char *prefix)
+{
+	const char *branch = NULL;
+	int ret;
+	struct argv_array args = ARGV_ARRAY_INIT;
+	struct stash_info info;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_branch_usage, 0);
+
+	if (argc == 0)
+		return error(_("No branch name specified"));
+
+	branch = argv[0];
+
+	if (get_stash_info(&info, argc - 1, argv + 1))
+		return -1;
+
+	argv_array_pushl(&args, "checkout", "-b", NULL);
+	argv_array_push(&args, branch);
+	argv_array_push(&args, oid_to_hex(&info.b_commit));
+	ret = cmd_checkout(args.argc, args.argv, prefix);
+	if (!ret)
+		ret = do_apply_stash(prefix, &info, 1);
+	if (!ret && info.is_stash_ref)
+		ret = do_drop_stash(prefix, &info);
+
+	free_stash_info(&info);
+
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -548,6 +589,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		return !!drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "branch"))
+		return !!branch_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index a99d5dc9e..29d9f4425 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -598,20 +598,6 @@ drop_stash () {
 	clear_stash
 }
 
-apply_to_branch () {
-	test -n "$1" || die "$(gettext "No branch name specified")"
-	branch=$1
-	shift 1
-
-	set -- --index "$@"
-	assert_stash_like "$@"
-
-	git checkout -b $branch $REV^ &&
-	apply_stash "$@" && {
-		test -z "$IS_STASH_REF" || drop_stash "$@"
-	}
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -673,7 +659,8 @@ pop)
 	;;
 branch)
 	shift
-	apply_to_branch "$@"
+	cd "$START_DIR"
+	git stash--helper branch "$@"
 	;;
 *)
 	case $# in
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 4/4] stash: convert pop to builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (6 preceding siblings ...)
  2018-06-25 16:43   ` [PATCH v6 3/4] stash: convert branch " Paul-Sebastian Ungureanu
@ 2018-06-25 16:43   ` Paul-Sebastian Ungureanu
  2018-06-26 22:31     ` Johannes Schindelin
  2018-06-25 16:46   ` [PATCH v6 1/6] stash: implement the "list" command in the builtin Paul-Sebastian Ungureanu
                     ` (9 subsequent siblings)
  17 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:43 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

From: Joel Teichroeb <joel@teichroeb.net>

Add stash pop to the helper and delete the pop_stash, drop_stash,
assert_stash_ref functions from the shell script now that they
are no longer needed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-
 git-stash.sh            | 47 ++---------------------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index fbf78249c..a38d6ae8a 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,7 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
-	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	NULL
@@ -24,6 +24,11 @@ static const char * const git_stash_helper_drop_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_pop_usage[] = {
+	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_apply_usage[] = {
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
 	NULL
@@ -528,6 +533,33 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int pop_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0, ret;
+	struct stash_info info;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_pop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	assert_stash_ref(&info);
+	if ((ret = do_apply_stash(prefix, &info, index)))
+		printf_ln(_("The stash entry is kept in case you need it again."));
+	else
+		ret = do_drop_stash(prefix, &info);
+
+	free_stash_info(&info);
+	return ret;
+}
+
 static int branch_stash(int argc, const char **argv, const char *prefix)
 {
 	const char *branch = NULL;
@@ -589,6 +621,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		return !!drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "pop"))
+		return !!pop_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "branch"))
 		return !!branch_stash(argc, argv, prefix);
 
diff --git a/git-stash.sh b/git-stash.sh
index 29d9f4425..8f2640fe9 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -554,50 +554,6 @@ assert_stash_like() {
 	}
 }
 
-is_stash_ref() {
-	is_stash_like "$@" && test -n "$IS_STASH_REF"
-}
-
-assert_stash_ref() {
-	is_stash_ref "$@" || {
-		args="$*"
-		die "$(eval_gettext "'\$args' is not a stash reference")"
-	}
-}
-
-apply_stash () {
-	cd "$START_DIR"
-	git stash--helper apply "$@"
-	res=$?
-	cd_to_toplevel
-	return $res
-}
-
-pop_stash() {
-	assert_stash_ref "$@"
-
-	if apply_stash "$@"
-	then
-		drop_stash "$@"
-	else
-		status=$?
-		say "$(gettext "The stash entry is kept in case you need it again.")"
-		exit $status
-	fi
-}
-
-drop_stash () {
-	assert_stash_ref "$@"
-
-	git reflog delete --updateref --rewrite "${REV}" &&
-		say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
-		die "$(eval_gettext "\${REV}: Could not drop stash entry")"
-
-	# clear_stash if we just dropped the last stash entry
-	git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
-	clear_stash
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -655,7 +611,8 @@ drop)
 	;;
 pop)
 	shift
-	pop_stash "$@"
+	cd "$START_DIR"
+	git stash--helper pop "$@"
 	;;
 branch)
 	shift
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 1/6] stash: implement the "list" command in the builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (7 preceding siblings ...)
  2018-06-25 16:43   ` [PATCH v6 4/4] stash: convert pop " Paul-Sebastian Ungureanu
@ 2018-06-25 16:46   ` Paul-Sebastian Ungureanu
  2018-06-25 16:46   ` [PATCH v6 2/6] stash: convert show to builtin Paul-Sebastian Ungureanu
                     ` (8 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:46 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Add stash list to the helper and delete the list_stash function
from the shell script.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 33 +++++++++++++++++++++++++++++++++
 git-stash.sh            |  7 +------
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index a38d6ae8a..2ed21f5d1 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,6 +12,7 @@
 #include "rerere.h"
 
 static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper list [<options>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_list_usage[] = {
+	N_("git stash--helper list [<options>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_drop_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
@@ -595,6 +601,31 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int list_stash(int argc, const char **argv, const char *prefix)
+{
+	int ret;
+	struct argv_array args = ARGV_ARRAY_INIT;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_list_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	if (!ref_exists(ref_stash))
+		return 0;
+
+	argv_array_pushl(&args, "log", "--format=%gd: %gs", "-g",
+			 "--first-parent", "-m", NULL);
+	argv_array_pushv(&args, argv);
+	argv_array_push(&args, ref_stash);
+	argv_array_push(&args, "--");
+	ret = cmd_log(args.argc, args.argv, prefix);
+	argv_array_clear(&args);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -625,6 +656,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!pop_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "branch"))
 		return !!branch_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "list"))
+		return !!list_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 8f2640fe9..6052441aa 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -382,11 +382,6 @@ have_stash () {
 	git rev-parse --verify --quiet $ref_stash >/dev/null
 }
 
-list_stash () {
-	have_stash || return 0
-	git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
-}
-
 show_stash () {
 	ALLOW_UNKNOWN_FLAGS=t
 	assert_stash_like "$@"
@@ -574,7 +569,7 @@ test -n "$seen_non_option" || set "push" "$@"
 case "$1" in
 list)
 	shift
-	list_stash "$@"
+	git stash--helper list "$@"
 	;;
 show)
 	shift
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 2/6] stash: convert show to builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (8 preceding siblings ...)
  2018-06-25 16:46   ` [PATCH v6 1/6] stash: implement the "list" command in the builtin Paul-Sebastian Ungureanu
@ 2018-06-25 16:46   ` Paul-Sebastian Ungureanu
  2018-06-25 16:46   ` [PATCH v6 3/6] stash: change `git stash show` usage text and documentation Paul-Sebastian Ungureanu
                     ` (7 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:46 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Add stash show to the helper and delete the show_stash, have_stash,
assert_stash_like, is_stash_like and parse_flags_and_rev functions
from the shell script now that they are no longer needed.

Before this commit, `git stash show` would ignore `--index` and
`--quiet` options. Now, `git stash show` errors out on `--index`
and does not display any message on `--quiet`, but errors out
if the stash is not empty.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c |  77 +++++++++++++++++++++++
 git-stash.sh            | 132 +---------------------------------------
 2 files changed, 78 insertions(+), 131 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 2ed21f5d1..a7eba3fba 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,6 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
+	N_("git stash--helper show [<stash>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -25,6 +26,11 @@ static const char * const git_stash_helper_list_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_show_usage[] = {
+	N_("git stash--helper show [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_drop_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
@@ -626,6 +632,75 @@ static int list_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int show_stat = 1;
+static int show_patch;
+
+static int git_stash_config(const char *var, const char *value, void *cb)
+{
+	if (!strcmp(var, "stash.showStat")) {
+		show_stat = git_config_bool(var, value);
+		return 0;
+	}
+	if (!strcmp(var, "stash.showPatch")) {
+		show_patch = git_config_bool(var, value);
+		return 0;
+	}
+	return git_default_config(var, value, cb);
+}
+
+static int show_stash(int argc, const char **argv, const char *prefix)
+{
+	int i, ret = 0;
+	struct argv_array args = ARGV_ARRAY_INIT;
+	struct argv_array args_refs = ARGV_ARRAY_INIT;
+	struct stash_info info;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_show_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	argv_array_push(&args, "diff");
+
+	/* Push args which are not options into args_refs. */
+	for (i = 0; i < argc; ++i) {
+		if (argv[i][0] == '-')
+			argv_array_push(&args, argv[i]);
+		else
+			argv_array_push(&args_refs, argv[i]);
+	}
+
+	if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {
+		argv_array_clear(&args);
+		argv_array_clear(&args_refs);
+		return -1;
+	}
+
+	/*
+	 * The config settings are applied only if there are not passed
+	 * any flags.
+	 */
+	if (args.argc == 1) {
+		git_config(git_stash_config, NULL);
+		if (show_stat)
+			argv_array_push(&args, "--stat");
+
+		if (show_patch)
+			argv_array_push(&args, "-p");
+	}
+
+	argv_array_pushl(&args, oid_to_hex(&info.b_commit),
+			 oid_to_hex(&info.w_commit), NULL);
+
+	ret = cmd_diff(args.argc, args.argv, prefix);
+
+	free_stash_info(&info);
+	argv_array_clear(&args);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -658,6 +733,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!branch_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "list"))
 		return !!list_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "show"))
+		return !!show_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 6052441aa..0d05cbc1e 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -378,35 +378,6 @@ save_stash () {
 	fi
 }
 
-have_stash () {
-	git rev-parse --verify --quiet $ref_stash >/dev/null
-}
-
-show_stash () {
-	ALLOW_UNKNOWN_FLAGS=t
-	assert_stash_like "$@"
-
-	if test -z "$FLAGS"
-	then
-		if test "$(git config --bool stash.showStat || echo true)" = "true"
-		then
-			FLAGS=--stat
-		fi
-
-		if test "$(git config --bool stash.showPatch || echo false)" = "true"
-		then
-			FLAGS=${FLAGS}${FLAGS:+ }-p
-		fi
-
-		if test -z "$FLAGS"
-		then
-			return 0
-		fi
-	fi
-
-	git diff ${FLAGS} $b_commit $w_commit
-}
-
 show_help () {
 	exec git help stash
 	exit 1
@@ -448,107 +419,6 @@ show_help () {
 #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
 #
 
-parse_flags_and_rev()
-{
-	test "$PARSE_CACHE" = "$*" && return 0 # optimisation
-	PARSE_CACHE="$*"
-
-	IS_STASH_LIKE=
-	IS_STASH_REF=
-	INDEX_OPTION=
-	s=
-	w_commit=
-	b_commit=
-	i_commit=
-	u_commit=
-	w_tree=
-	b_tree=
-	i_tree=
-	u_tree=
-
-	FLAGS=
-	REV=
-	for opt
-	do
-		case "$opt" in
-			-q|--quiet)
-				GIT_QUIET=-t
-			;;
-			--index)
-				INDEX_OPTION=--index
-			;;
-			--help)
-				show_help
-			;;
-			-*)
-				test "$ALLOW_UNKNOWN_FLAGS" = t ||
-					die "$(eval_gettext "unknown option: \$opt")"
-				FLAGS="${FLAGS}${FLAGS:+ }$opt"
-			;;
-			*)
-				REV="${REV}${REV:+ }'$opt'"
-			;;
-		esac
-	done
-
-	eval set -- $REV
-
-	case $# in
-		0)
-			have_stash || die "$(gettext "No stash entries found.")"
-			set -- ${ref_stash}@{0}
-		;;
-		1)
-			:
-		;;
-		*)
-			die "$(eval_gettext "Too many revisions specified: \$REV")"
-		;;
-	esac
-
-	case "$1" in
-		*[!0-9]*)
-			:
-		;;
-		*)
-			set -- "${ref_stash}@{$1}"
-		;;
-	esac
-
-	REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
-		reference="$1"
-		die "$(eval_gettext "\$reference is not a valid reference")"
-	}
-
-	i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
-	set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
-	s=$1 &&
-	w_commit=$1 &&
-	b_commit=$2 &&
-	w_tree=$3 &&
-	b_tree=$4 &&
-	i_tree=$5 &&
-	IS_STASH_LIKE=t &&
-	test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
-	IS_STASH_REF=t
-
-	u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
-	u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
-}
-
-is_stash_like()
-{
-	parse_flags_and_rev "$@"
-	test -n "$IS_STASH_LIKE"
-}
-
-assert_stash_like() {
-	is_stash_like "$@" || {
-		args="$*"
-		die "$(eval_gettext "'\$args' is not a stash-like commit")"
-	}
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -573,7 +443,7 @@ list)
 	;;
 show)
 	shift
-	show_stash "$@"
+	git stash--helper show "$@"
 	;;
 save)
 	shift
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 3/6] stash: change `git stash show` usage text and documentation
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (9 preceding siblings ...)
  2018-06-25 16:46   ` [PATCH v6 2/6] stash: convert show to builtin Paul-Sebastian Ungureanu
@ 2018-06-25 16:46   ` Paul-Sebastian Ungureanu
  2018-06-25 16:46   ` [PATCH v6 4/6] stash: refactor `show_stash()` to use the diff API Paul-Sebastian Ungureanu
                     ` (6 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:46 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

It is already stated in documentation that it will accept any
option known to `git diff`, but not in the usage text and some
parts of the documentation.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 Documentation/git-stash.txt | 4 ++--
 builtin/stash--helper.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 7ef8c4791..e31ea7d30 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git stash' list [<options>]
-'git stash' show [<stash>]
+'git stash' show [<options>] [<stash>]
 'git stash' drop [-q|--quiet] [<stash>]
 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
 'git stash' branch <branchname> [<stash>]
@@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash
 The command takes options applicable to the 'git log'
 command to control what is shown and how. See linkgit:git-log[1].
 
-show [<stash>]::
+show [<options>] [<stash>]::
 
 	Show the changes recorded in the stash entry as a diff between the
 	stashed contents and the commit back when the stash entry was first
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index a7eba3fba..4f49fd04b 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,7 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
-	N_("git stash--helper show [<stash>]"),
+	N_("git stash--helper show [<options>] [<stash>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -27,7 +27,7 @@ static const char * const git_stash_helper_list_usage[] = {
 };
 
 static const char * const git_stash_helper_show_usage[] = {
-	N_("git stash--helper show [<stash>]"),
+	N_("git stash--helper show [<options>] [<stash>]"),
 	NULL
 };
 
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 4/6] stash: refactor `show_stash()` to use the diff API
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (10 preceding siblings ...)
  2018-06-25 16:46   ` [PATCH v6 3/6] stash: change `git stash show` usage text and documentation Paul-Sebastian Ungureanu
@ 2018-06-25 16:46   ` Paul-Sebastian Ungureanu
  2018-06-25 16:46   ` [PATCH v6 5/6] stash: update `git stash show` documentation Paul-Sebastian Ungureanu
                     ` (5 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:46 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Currently, `show_stash()` uses `cmd_diff()` to generate
the output. After this commit, the output will be generated
using the internal API.

Before this commit, `git stash show --quiet` would act like
`git diff` and error out if the stash is not empty. Now, the
`--quiet` option does not error out given an empty stash.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 72 +++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 27 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 4f49fd04b..4589e12d6 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -10,6 +10,8 @@
 #include "run-command.h"
 #include "dir.h"
 #include "rerere.h"
+#include "revision.h"
+#include "log-tree.h"
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
@@ -650,55 +652,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 
 static int show_stash(int argc, const char **argv, const char *prefix)
 {
-	int i, ret = 0;
-	struct argv_array args = ARGV_ARRAY_INIT;
-	struct argv_array args_refs = ARGV_ARRAY_INIT;
+	int i;
+	int flags = 0;
 	struct stash_info info;
+	struct rev_info rev;
+	struct argv_array stash_args = ARGV_ARRAY_INIT;
 	struct option options[] = {
 		OPT_END()
 	};
 
-	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_show_usage,
-			     PARSE_OPT_KEEP_UNKNOWN);
+	init_diff_ui_defaults();
+	git_config(git_diff_ui_config, NULL);
 
-	argv_array_push(&args, "diff");
+	init_revisions(&rev, prefix);
 
-	/* Push args which are not options into args_refs. */
-	for (i = 0; i < argc; ++i) {
-		if (argv[i][0] == '-')
-			argv_array_push(&args, argv[i]);
+	/* Push args which are not options into stash_args. */
+	for (i = 1; i < argc; ++i) {
+		if (argv[i][0] != '-')
+			argv_array_push(&stash_args, argv[i]);
 		else
-			argv_array_push(&args_refs, argv[i]);
-	}
-
-	if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {
-		argv_array_clear(&args);
-		argv_array_clear(&args_refs);
-		return -1;
+			flags++;
 	}
 
 	/*
 	 * The config settings are applied only if there are not passed
 	 * any flags.
 	 */
-	if (args.argc == 1) {
+	if (!flags) {
 		git_config(git_stash_config, NULL);
 		if (show_stat)
-			argv_array_push(&args, "--stat");
+			rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;
+		if (show_patch) {
+			rev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;
+			rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
+		}
+	}
 
-		if (show_patch)
-			argv_array_push(&args, "-p");
+	if (get_stash_info(&info, stash_args.argc, stash_args.argv)) {
+		argv_array_clear(&stash_args);
+		return -1;
 	}
 
-	argv_array_pushl(&args, oid_to_hex(&info.b_commit),
-			 oid_to_hex(&info.w_commit), NULL);
+	argc = setup_revisions(argc, argv, &rev, NULL);
+	if (!rev.diffopt.output_format)
+		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+	diff_setup_done(&rev.diffopt);
+	rev.diffopt.flags.recursive = 1;
+	setup_diff_pager(&rev.diffopt);
 
-	ret = cmd_diff(args.argc, args.argv, prefix);
+	/*
+	 * We can return early if there was any option not recognised by
+	 * `diff_opt_parse()`, besides the word `stash`.
+	 */
+	if (argc > 1) {
+		free_stash_info(&info);
+		argv_array_clear(&stash_args);
+		usage_with_options(git_stash_helper_show_usage, options);
+	}
+
+	/* Do the diff thing. */
+	diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
+	log_tree_diff_flush(&rev);
 
 	free_stash_info(&info);
-	argv_array_clear(&args);
-	return ret;
+	argv_array_clear(&stash_args);
+	return 0;
 }
 
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 5/6] stash: update `git stash show` documentation
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (11 preceding siblings ...)
  2018-06-25 16:46   ` [PATCH v6 4/6] stash: refactor `show_stash()` to use the diff API Paul-Sebastian Ungureanu
@ 2018-06-25 16:46   ` Paul-Sebastian Ungureanu
  2018-06-25 16:46   ` [PATCH v6 6/6] stash: convert store to builtin Paul-Sebastian Ungureanu
                     ` (4 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:46 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Add in documentation about the change of behavior regarding
the `--quiet` option, which was introduced in the last commit.
(the `--quiet` option does not exit anymore with erorr if it
is given an empty stash as argument)

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 Documentation/git-stash.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index e31ea7d30..d60ebdb96 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -117,6 +117,9 @@ show [<options>] [<stash>]::
 	You can use stash.showStat and/or stash.showPatch config variables
 	to change the default behavior.
 
+	It accepts any option known to `git diff`, but acts different on
+	`--quiet` option and exit with zero regardless of differences.
+
 pop [--index] [-q|--quiet] [<stash>]::
 
 	Remove a single stashed state from the stash list and apply it
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [PATCH v6 6/6] stash: convert store to builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (12 preceding siblings ...)
  2018-06-25 16:46   ` [PATCH v6 5/6] stash: update `git stash show` documentation Paul-Sebastian Ungureanu
@ 2018-06-25 16:46   ` Paul-Sebastian Ungureanu
  2018-06-26 21:59   ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Johannes Schindelin
                     ` (3 subsequent siblings)
  17 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-25 16:46 UTC (permalink / raw)
  To: git; +Cc: Johannes.Schindelin, joel, gitster

Add stash store to the helper and delete the store_stash function
from the shell script.

Add the usage string which was forgotten in the shell script.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 48 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 43 ++----------------------------------
 2 files changed, 50 insertions(+), 41 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 4589e12d6..556d91b20 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -20,6 +20,7 @@ static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
+	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
 	NULL
 };
 
@@ -58,6 +59,11 @@ static const char * const git_stash_helper_clear_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_store_usage[] = {
+	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -719,6 +725,46 @@ static int show_stash(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
+static int store_stash(int argc, const char **argv, const char *prefix)
+{
+	struct object_id obj;
+	int ret = 0;
+	const char *stash_msg = NULL;
+	const char *w_commit = NULL;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_STRING('m', "message", &stash_msg, "message", N_("stash message")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_store_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	if (argc != 1) {
+		fprintf(stderr, _("\"git stash--helper store\" requires one <commit> argument\n"));
+		return -1;
+	}
+
+	w_commit = argv[0];
+
+	if (!stash_msg)
+		stash_msg  = xstrdup("Created via \"git stash--helper store\".");
+
+	ret = get_oid(w_commit, &obj);
+	if (!ret) {
+		ret = update_ref(stash_msg, ref_stash, &obj, NULL,
+				 REF_FORCE_CREATE_REFLOG,
+				 quiet ? UPDATE_REFS_QUIET_ON_ERR :
+				 UPDATE_REFS_MSG_ON_ERR);
+	}
+
+	if (ret && !quiet)
+		fprintf_ln(stderr, _("Cannot update %s with %s"), ref_stash, w_commit);
+
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -753,6 +799,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!list_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "show"))
 		return !!show_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "store"))
+		return !!store_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 0d05cbc1e..5739c5152 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -191,45 +191,6 @@ create_stash () {
 	die "$(gettext "Cannot record working tree state")"
 }
 
-store_stash () {
-	while test $# != 0
-	do
-		case "$1" in
-		-m|--message)
-			shift
-			stash_msg="$1"
-			;;
-		-m*)
-			stash_msg=${1#-m}
-			;;
-		--message=*)
-			stash_msg=${1#--message=}
-			;;
-		-q|--quiet)
-			quiet=t
-			;;
-		*)
-			break
-			;;
-		esac
-		shift
-	done
-	test $# = 1 ||
-	die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
-
-	w_commit="$1"
-	if test -z "$stash_msg"
-	then
-		stash_msg="Created via \"git stash store\"."
-	fi
-
-	git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
-	ret=$?
-	test $ret != 0 && test -z "$quiet" &&
-	die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
-	return $ret
-}
-
 push_stash () {
 	keep_index=
 	patch_mode=
@@ -308,7 +269,7 @@ push_stash () {
 		clear_stash || die "$(gettext "Cannot initialize stash")"
 
 	create_stash -m "$stash_msg" -u "$untracked" -- "$@"
-	store_stash -m "$stash_msg" -q $w_commit ||
+	git stash--helper store -m "$stash_msg" -q $w_commit ||
 	die "$(gettext "Cannot save the current status")"
 	say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
 
@@ -468,7 +429,7 @@ create)
 	;;
 store)
 	shift
-	store_stash "$@"
+	git stash--helper store "$@"
 	;;
 drop)
 	shift
-- 
2.18.0.rc2.13.g506fc12fb


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (13 preceding siblings ...)
  2018-06-25 16:46   ` [PATCH v6 6/6] stash: convert store to builtin Paul-Sebastian Ungureanu
@ 2018-06-26 21:59   ` Johannes Schindelin
  2018-06-27 18:47     ` Junio C Hamano
  2018-06-26 22:12   ` [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin Johannes Schindelin
                     ` (2 subsequent siblings)
  17 siblings, 1 reply; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 21:59 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git, joel, gitster

Hi,

On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> This first series of patches does bring some changes and improvements to
> the test suite. One of the patches also introduces a new function
> `get_oidf()` which will be hepful for the incoming patches related to
> `git stash`.

For reviewers: it is *my* fault that this patch submission is a bit funny
with two 1/4 and one 1/6 patches... *I* suggested to not send a 14-strong
patch series but split it into three, and then I failed to explain the
correct invocation to do that from the command-line.

My sincere apologies,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid'
  2018-06-25 16:42   ` [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid' Paul-Sebastian Ungureanu
@ 2018-06-26 22:02     ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 22:02 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git, joel, gitster

Hi Paul,

as a general rule, we try to keep the commit subjects in the imperative,
i.e.

	sha1-name.c: add 'get_oidf', which acts like 'get_oid'

On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> Compared to 'get_oid', 'get_oidf' has as parameters a
> printf format string and the additional arguments. This
> will help simplify the code in subsequent commits.

Good. The patch is obviously correct, from my perspective.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 3/4] stash: update test cases conform to coding guidelines
  2018-06-25 16:42   ` [PATCH v6 3/4] stash: update test cases conform to coding guidelines Paul-Sebastian Ungureanu
@ 2018-06-26 22:08     ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 22:08 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git, joel, gitster

Hi Paul,

On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> Removed whitespaces after redirection operators.

That is accurate a description of what the patch does. Let's add the why
(and change the tense to present tense, as is the custom in our commit
messages):

	This adjusts t3903-stash.sh to conform with our coding conventions
	better, by removing whitespace between redirection operators and
	their file names.

The patch does two more things, though: it breaks at least one long line,
and it renames at least two non-descriptive files to the much better name
`expected`. Maybe you want to mention that in the commit message, to?

Thanks,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 4/4] stash: renamed test cases to be more descriptive
  2018-06-25 16:42   ` [PATCH v6 4/4] stash: renamed test cases to be more descriptive Paul-Sebastian Ungureanu
@ 2018-06-26 22:09     ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 22:09 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git, joel, gitster

Hi Paul,

On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> Renamed some test cases' labels to be more descriptive and under 80
> characters per line.
> 
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>

As I suggested this kind of change, I am obviously happy with this patch.

Apart from minor suggestions, I think this patch series is good to go.

Thank you!
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (14 preceding siblings ...)
  2018-06-26 21:59   ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Johannes Schindelin
@ 2018-06-26 22:12   ` Johannes Schindelin
  2018-06-28 23:14     ` Paul-Sebastian Ungureanu
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
  17 siblings, 1 reply; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 22:12 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git, joel, gitster

Hi Paul,

On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> This second series of patches contains commits to convert `apply`, `drop`,
> `clear`, `branch`, `pop` stash subcommands to builtins.
> 
> 
> Joel Teichroeb (4):
>   stash: convert apply to builtin
>   stash: convert drop and clear to builtin
>   stash: convert branch to builtin
>   stash: convert pop to builtin

Were there any changes since v5 in these patches?

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 2/4] stash: convert drop and clear to builtin
  2018-06-25 16:43   ` [PATCH v6 2/4] stash: convert drop and clear " Paul-Sebastian Ungureanu
@ 2018-06-26 22:17     ` Johannes Schindelin
  2018-06-28 22:51       ` Paul-Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 22:17 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git, joel, gitster

Hi Paul,

On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 1c4387b10..84a537f39 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -414,6 +451,77 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> +static int do_drop_stash(const char *prefix, struct stash_info *info)
> +{
> +	struct argv_array args = ARGV_ARRAY_INIT;
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +	int ret;
> +
> +	/*
> +	 * reflog does not provide a simple function for deleting refs. One will
> +	 * need to be added to avoid implementing too much reflog code here
> +	 */
> +	argv_array_pushl(&args, "reflog", "delete", "--updateref", "--rewrite",
> +			 NULL);
> +	argv_array_push(&args, info->revision.buf);
> +	ret = cmd_reflog(args.argc, args.argv, prefix);
> +	if (!ret) {
> +		if (!quiet)
> +			printf(_("Dropped %s (%s)\n"), info->revision.buf,
> +			       oid_to_hex(&info->w_commit));
> +	} else {
> +		return error(_("%s: Could not drop stash entry"), info->revision.buf);
> +	}
> +
> +	/*
> +	 * This could easily be replaced by get_oid, but currently it will throw a
> +	 * fatal error when a reflog is empty, which we can not recover from
> +	 */
> +	cp.git_cmd = 1;
> +	/* Even though --quiet is specified, rev-parse still outputs the hash */
> +	cp.no_stdout = 1;
> +	argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
> +	argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
> +	ret = run_command(&cp);

I thought you had introduced `get_oidf()` specifically so you could avoid
the `rev-parse` call... `get_oidf(&dummy_oid, "%s@{0}", ref_stash)` should
do this, right?

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 3/4] stash: convert branch to builtin
  2018-06-25 16:43   ` [PATCH v6 3/4] stash: convert branch " Paul-Sebastian Ungureanu
@ 2018-06-26 22:23     ` Johannes Schindelin
  2018-06-27 18:39       ` Junio C Hamano
  0 siblings, 1 reply; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 22:23 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git, joel, gitster

Hi Paul,

On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 84a537f39..fbf78249c 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -522,6 +528,41 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> +static int branch_stash(int argc, const char **argv, const char *prefix)
> +{
> +	const char *branch = NULL;
> +	int ret;
> +	struct argv_array args = ARGV_ARRAY_INIT;
> +	struct stash_info info;
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_branch_usage, 0);
> +
> +	if (argc == 0)
> +		return error(_("No branch name specified"));
> +
> +	branch = argv[0];
> +
> +	if (get_stash_info(&info, argc - 1, argv + 1))
> +		return -1;
> +
> +	argv_array_pushl(&args, "checkout", "-b", NULL);
> +	argv_array_push(&args, branch);
> +	argv_array_push(&args, oid_to_hex(&info.b_commit));

Why not combine these? _pushl() takes a NULL-terminated list:

	argv_array_pushl(&args, "checkout", "-b", branch,
			 oid_to_hex(&info.b_commit), NULL);

> +	ret = cmd_checkout(args.argc, args.argv, prefix);

I guess it is okay to run that, but the cmd_() functions are not *really*
meant to be called this way... Personally, I would be more comfortable
with a `run_command()` here, i.e. with a spawned process, until the time
when checkout.c/checkout.h have learned enough API for a direct call.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 4/4] stash: convert pop to builtin
  2018-06-25 16:43   ` [PATCH v6 4/4] stash: convert pop " Paul-Sebastian Ungureanu
@ 2018-06-26 22:31     ` Johannes Schindelin
  0 siblings, 0 replies; 181+ messages in thread
From: Johannes Schindelin @ 2018-06-26 22:31 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

Hi Paul,

I think I had revewied these 4 patches before, and I'd wager a bet that
you addressed all of my suggestions, if any.

I had a look over patches 2-4, and want to take a little bit more time
tomorrow to pour over patch 1 (which is a little larger, as it lays a lot
of ground work), to make sure that I cannot find anything else to improve.

Well done so far!
Dscho


On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:

> From: Joel Teichroeb <joel@teichroeb.net>
> 
> Add stash pop to the helper and delete the pop_stash, drop_stash,
> assert_stash_ref functions from the shell script now that they
> are no longer needed.
> 
> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-
>  git-stash.sh            | 47 ++---------------------------------------
>  2 files changed, 37 insertions(+), 46 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index fbf78249c..a38d6ae8a 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -13,7 +13,7 @@
>  
>  static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
> -	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
> +	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper branch <branchname> [<stash>]"),
>  	N_("git stash--helper clear"),
>  	NULL
> @@ -24,6 +24,11 @@ static const char * const git_stash_helper_drop_usage[] = {
>  	NULL
>  };
>  
> +static const char * const git_stash_helper_pop_usage[] = {
> +	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
> +	NULL
> +};
> +
>  static const char * const git_stash_helper_apply_usage[] = {
>  	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
>  	NULL
> @@ -528,6 +533,33 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> +static int pop_stash(int argc, const char **argv, const char *prefix)
> +{
> +	int index = 0, ret;
> +	struct stash_info info;
> +	struct option options[] = {
> +		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
> +		OPT_BOOL(0, "index", &index,
> +			N_("attempt to recreate the index")),
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_pop_usage, 0);
> +
> +	if (get_stash_info(&info, argc, argv))
> +		return -1;
> +
> +	assert_stash_ref(&info);
> +	if ((ret = do_apply_stash(prefix, &info, index)))
> +		printf_ln(_("The stash entry is kept in case you need it again."));
> +	else
> +		ret = do_drop_stash(prefix, &info);
> +
> +	free_stash_info(&info);
> +	return ret;
> +}
> +
>  static int branch_stash(int argc, const char **argv, const char *prefix)
>  {
>  	const char *branch = NULL;
> @@ -589,6 +621,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!clear_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "drop"))
>  		return !!drop_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "pop"))
> +		return !!pop_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "branch"))
>  		return !!branch_stash(argc, argv, prefix);
>  
> diff --git a/git-stash.sh b/git-stash.sh
> index 29d9f4425..8f2640fe9 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -554,50 +554,6 @@ assert_stash_like() {
>  	}
>  }
>  
> -is_stash_ref() {
> -	is_stash_like "$@" && test -n "$IS_STASH_REF"
> -}
> -
> -assert_stash_ref() {
> -	is_stash_ref "$@" || {
> -		args="$*"
> -		die "$(eval_gettext "'\$args' is not a stash reference")"
> -	}
> -}
> -
> -apply_stash () {
> -	cd "$START_DIR"
> -	git stash--helper apply "$@"
> -	res=$?
> -	cd_to_toplevel
> -	return $res
> -}
> -
> -pop_stash() {
> -	assert_stash_ref "$@"
> -
> -	if apply_stash "$@"
> -	then
> -		drop_stash "$@"
> -	else
> -		status=$?
> -		say "$(gettext "The stash entry is kept in case you need it again.")"
> -		exit $status
> -	fi
> -}
> -
> -drop_stash () {
> -	assert_stash_ref "$@"
> -
> -	git reflog delete --updateref --rewrite "${REV}" &&
> -		say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
> -		die "$(eval_gettext "\${REV}: Could not drop stash entry")"
> -
> -	# clear_stash if we just dropped the last stash entry
> -	git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
> -	clear_stash
> -}
> -
>  test "$1" = "-p" && set "push" "$@"
>  
>  PARSE_CACHE='--not-parsed'
> @@ -655,7 +611,8 @@ drop)
>  	;;
>  pop)
>  	shift
> -	pop_stash "$@"
> +	cd "$START_DIR"
> +	git stash--helper pop "$@"
>  	;;
>  branch)
>  	shift
> -- 
> 2.18.0.rc2.13.g506fc12fb
> 
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 3/4] stash: convert branch to builtin
  2018-06-26 22:23     ` Johannes Schindelin
@ 2018-06-27 18:39       ` Junio C Hamano
  2018-06-28 16:17         ` Paul-Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Junio C Hamano @ 2018-06-27 18:39 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Paul-Sebastian Ungureanu, git, joel

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>> +	ret = cmd_checkout(args.argc, args.argv, prefix);
>
> I guess it is okay to run that, but the cmd_() functions are not *really*
> meant to be called this way... Personally, I would be more comfortable
> with a `run_command()` here, i.e. with a spawned process, until the time
> when checkout.c/checkout.h have learned enough API for a direct call.

Thanks for pointing it out.  I'll add a bit more for those who are
reading from sideline (iow, if you are Dscho, you do not have to
read the remainder, as I know Dscho knows all of this).

It is not OK to use cmd_$foo() as subroutine; depending on the value
of $foo, where the call is made and the number of times the call is
made, it may happen to work OK in the current code, but that is a
ticking timebomb waiting to go off.

This is primarily because cmd_$foo() is designed to be replacement
of "main()" in usual programs---it is allowed to assume the global
variables it uses have their initial values and nobody cares the
state it leaves behind when it returns.  Argument parser may flip
bits in file-scope static variables to record which options are
seen, assuming that these variables are initialized to all-zero, and
that assumption would not hold for the second call to cmd_$foo(),
etc.  cmd_$foo() also is allowed to call die() when there is an
unrecoverable error condition in the context of carrying out $foo; a
scripted Porcelain that used $foo as a building block to implement a
larger operation (e.g. "stash" that used "checkout" to switch to a
different branch) may want to react to the failure and do something
extra (i.e. "git checkout || do something more").  

Using run_command() interface would not cause any of these problems;
the subcommand will run in a clean environment, and the caller can
react to its failure.

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function
  2018-06-26 21:59   ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Johannes Schindelin
@ 2018-06-27 18:47     ` Junio C Hamano
  2018-06-28 22:32       ` Paul-Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Junio C Hamano @ 2018-06-27 18:47 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Paul-Sebastian Ungureanu, git, joel

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> Hi,
>
> On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:
>
>> This first series of patches does bring some changes and improvements to
>> the test suite. One of the patches also introduces a new function
>> `get_oidf()` which will be hepful for the incoming patches related to
>> `git stash`.
>
> For reviewers: it is *my* fault that this patch submission is a bit funny
> with two 1/4 and one 1/6 patches... *I* suggested to not send a 14-strong
> patch series but split it into three, and then I failed to explain the
> correct invocation to do that from the command-line.
>
> My sincere apologies,
> Dscho

Heh, what is more useful than apology is to tell us which order
these three (apparent) series build on top of each other ;-)

The answer, IIUC, is that 

 * oidf+tests come first, then
 * apply/drop/branch/pop (as these rely on oidf) on top, and finally
 * list (as it wants to add to stash--helper that is a new file in the middle)

When there is clear dependency like that, I agree that it would help
readers to emphasize that these cannot be applied in an arbitrary
order.  It is especially true as the second part of the above _will_
apply more-or-less cleanly without the first one, and then fail to
compile due to lack of oidf.

Thanks.

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 3/4] stash: convert branch to builtin
  2018-06-27 18:39       ` Junio C Hamano
@ 2018-06-28 16:17         ` Paul-Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-28 16:17 UTC (permalink / raw)
  To: Junio C Hamano, Johannes Schindelin; +Cc: git, joel

Hello,

On 27.06.2018 21:39, Junio C Hamano wrote:
> This is primarily because cmd_$foo() is designed to be replacement
> of "main()" in usual programs---it is allowed to assume the global
> variables it uses have their initial values and nobody cares the
> state it leaves behind when it returns.  Argument parser may flip
> bits in file-scope static variables to record which options are
> seen, assuming that these variables are initialized to all-zero, and
> that assumption would not hold for the second call to cmd_$foo(),
> etc.  cmd_$foo() also is allowed to call die() when there is an
> unrecoverable error condition in the context of carrying out $foo; a
> scripted Porcelain that used $foo as a building block to implement a
> larger operation (e.g. "stash" that used "checkout" to switch to a
> different branch) may want to react to the failure and do something
> extra (i.e. "git checkout || do something more").
> 
> Using run_command() interface would not cause any of these problems;
> the subcommand will run in a clean environment, and the caller can
> react to its failure.
> 

Thank you a lot for this great explanation. When I will submit a new 
iteration, I will make sure to replace calls of `cmd_$foo()` with 
`run_command()` (or with API, if possible).

Best regards,
Paul Ungureanu

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function
  2018-06-27 18:47     ` Junio C Hamano
@ 2018-06-28 22:32       ` Paul-Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-28 22:32 UTC (permalink / raw)
  To: Junio C Hamano, Johannes Schindelin; +Cc: git, joel

Hello,

> Heh, what is more useful than apology is to tell us which order
> these three (apparent) series build on top of each other ;-)
> 
> The answer, IIUC, is that
> 
>   * oidf+tests come first, then
>   * apply/drop/branch/pop (as these rely on oidf) on top, and finally
>   * list (as it wants to add to stash--helper that is a new file in the middle)
> 
> When there is clear dependency like that, I agree that it would help
> readers to emphasize that these cannot be applied in an arbitrary
> order.  It is especially true as the second part of the above _will_
> apply more-or-less cleanly without the first one, and then fail to
> compile due to lack of oidf.
> 
> Thanks.
> 

I would actually say that it was 100% my fault (I could have tested 
before sending patches or ask if I was not completely sure) and I would 
like to apologize for this.

The order of the commits is actually good. The only thing that is not 
right are the cover letters, which are missing. Every 1/N marks the 
beginning of a series of patches. Just to be more clear, this is the 
right order:

First patch:
sha1-name.c: added 'get_oidf', which acts like 'get_oid'
stash: improve option parsing test coverage
stash: update test cases conform to coding guidelines
stash: renamed test cases to be more descriptive

Second patch:
stash: convert apply to builtin
stash: convert drop and clear to builtin
stash: convert branch to builtin
stash: convert pop to builtin

Third patch:
stash: implement the "list" command in the builtin
stash: convert show to builtin
stash: change `git stash show` usage text and documentation
stash: refactor `show_stash()` to use the diff API
stash: update `git stash show` documentation
stash: convert store to builtin

The only thing which was in the cover letters and might be worth 
mentioning here is related to `git stash show`. Although it might not be 
efficient, a 1:1 conversion is more easily to follow and review. Because 
of that, the first commit related to `git stash show` approaches a 1:1 
conversion to C. There is a subsequent patch (`refactor ....`) which 
makes the `show` subcommand use the existent API. Any change of behavior 
is noted in the commit message which introduces that change.

Best,
Paul

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 2/4] stash: convert drop and clear to builtin
  2018-06-26 22:17     ` Johannes Schindelin
@ 2018-06-28 22:51       ` Paul-Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-28 22:51 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, gitster

Hi,

On 27.06.2018 01:17, Johannes Schindelin wrote:
> I thought you had introduced `get_oidf()` specifically so you could avoid
> the `rev-parse` call... `get_oidf(&dummy_oid, "%s@{0}", ref_stash)` should
> do this, right?

We discussed this over the IRC channel, but since not everybody might 
follow the chat, I would like to put it also here.

The main reason why `get_oidf()` was introduced is to make 
`assert_stash_like()` less tedious. I am sure that this is not the only 
place where this function could be useful.

Over the last weeks, I have been working in parallel on adding a new 
flag (`GET_OID_GENTLY`) for `get_oid_with_context()`. By using this 
flag, the `rev-parse` call could be replaced with an internal API call. 
I hope that I will be able to send this patch soon enough.

Best regards,
Paul

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin
  2018-06-26 22:12   ` [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin Johannes Schindelin
@ 2018-06-28 23:14     ` Paul-Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-06-28 23:14 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, gitster

Hello,

On 27.06.2018 01:12, Johannes Schindelin wrote:

>> Joel Teichroeb (4):
>>    stash: convert apply to builtin
>>    stash: convert drop and clear to builtin
>>    stash: convert branch to builtin
>>    stash: convert pop to builtin
> 
> Were there any changes since v5 in these patches?
> 

There are some changes since v5 based on the reviews.

There are also some changes which were not suggested in the reviews, but 
which I considered to be good (I hope they really are). For example, in 
the previous version of the patch that introduces the `stash--helper`, 
there was no `assert_stash_like()` function (it was part of 
`get_stash_info()`). In v6, it was implemented as a separate function in 
order to keep it more alike to `git-stash.sh` script. Another difference 
was to change `assert_stash_ref()` function type from `int` to `void` 
and call `exit()` inside. This was introduced in order to make it more 
similar with `assert()` from `assert.h`. I do not think of these changes 
to be something of great importance, but I hope they might make the code 
easier to read.

Best,
Paul

^ permalink raw reply	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (15 preceding siblings ...)
  2018-06-26 22:12   ` [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin Johannes Schindelin
@ 2018-08-08 18:58   ` Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 01/26] sha1-name.c: added 'get_oidf', which acts like 'get_oid' Paul-Sebastian Ungureanu
                       ` (26 more replies)
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
  17 siblings, 27 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Hello,

Here is the whole `git stash` C version. Some of the previous
patches were already reviewed (up to and including "stash: convert
store to builtin"), but there are some which were not
(starting with "stash: convert create to builtin").

In order to see the difference between the shell version and
the C version, I ran `time` on:

* git test suite (t3903-stash.sh, t3904-stash-patch.sh,
t3905-stash-include-untracked.sh and t3906-stash-submodule.sh)

        t3903-stash.sh:
        ** SHELL: 12,69s user 9,95s system 109% cpu 20,730 total
        **     C:  2,67s user 2,84s system 105% cpu  5,206 total

        t3904-stash-patch.sh:
        ** SHELL: 1,43s user 0,94s system 106% cpu 2,242 total
        **     C: 1,01s user 0,58s system 104% cpu 1,530 total

        t3905-stash-include-untracked.sh
        ** SHELL: 2,22s user 1,73s system 110% cpu 3,569 total
        **     C: 0,59s user 0,57s system 106% cpu 1,085 total

        t3906-stash-submodule.sh
        ** SHELL: 2,89s user 2,99s system 106% cpu 5,527 total
        **     C: 2,21s user 2,61s system 105% cpu 4,568 total

        TOTAL:
        ** SHELL: 19.23s user 15.61s system
        **     C:  6.48s user  6.60s system

* a git repository with 4000 files: 1000 not changed,
1000 staged files, 1000 unstaged files, 1000 untracked.
In this case I ran some of the most used commands:

        git stash push:

        ** SHELL: 0,12s user 0,21s system 101% cpu 0,329 total
        **     C: 0,06s user 0,13s system 105% cpu 0,185 total

        git stash push -u:

        ** SHELL: 0,18s user 0,27s system  108% cpu 0,401 total
        **     C: 0,09s user 0,19s system  103% cpu 0,267 total

        git stash pop:

        ** SHELL: 0,16s user 0,26s system 103% cpu 0,399 total
        **     C: 0,13s user 0,19s system 102% cpu 0,308 total

Best regards,
Paul Ungureanu


Joel Teichroeb (5):
  stash: improve option parsing test coverage
  stash: convert apply to builtin
  stash: convert drop and clear to builtin
  stash: convert branch to builtin
  stash: convert pop to builtin

Paul-Sebastian Ungureanu (21):
  sha1-name.c: added 'get_oidf', which acts like 'get_oid'
  stash: update test cases conform to coding guidelines
  stash: renamed test cases to be more descriptive
  stash: implement the "list" command in the builtin
  stash: convert show to builtin
  stash: change `git stash show` usage text and documentation
  stash: refactor `show_stash()` to use the diff API
  stash: update `git stash show` documentation
  stash: convert store to builtin
  stash: convert create to builtin
  stash: replace spawning a "read-tree" process
  stash: avoid spawning a "diff-index" process
  stash: convert push to builtin
  stash: make push to be quiet
  stash: add tests for `git stash push -q`
  stash: replace spawning `git ls-files` child process
  stash: convert save to builtin
  stash: convert `stash--helper.c` into `stash.c`
  stash: optimize `get_untracked_files()` and `check_changes()`
  stash: replace all `write-tree` child processes with API calls
  stash: replace all "git apply" child processes with API calls

 Documentation/git-stash.txt |    7 +-
 Makefile                    |    2 +-
 builtin.h                   |    1 +
 builtin/stash.c             | 1562 +++++++++++++++++++++++++++++++++++
 cache.h                     |    1 +
 git-stash.sh                |  752 -----------------
 git.c                       |    1 +
 sha1-name.c                 |   19 +
 t/t3903-stash.sh            |  190 +++--
 9 files changed, 1714 insertions(+), 821 deletions(-)
 create mode 100644 builtin/stash.c
 delete mode 100755 git-stash.sh

-- 
2.18.0.573.g56500d98f


^ permalink raw reply	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 01/26] sha1-name.c: added 'get_oidf', which acts like 'get_oid'
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 02/26] stash: improve option parsing test coverage Paul-Sebastian Ungureanu
                       ` (25 subsequent siblings)
  26 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Compared to 'get_oid', 'get_oidf' has as parameters a
printf format string and the additional arguments. This
will help simplify the code in subsequent commits.

Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 cache.h     |  1 +
 sha1-name.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/cache.h b/cache.h
index 8dc7134f0..4ec09b336 100644
--- a/cache.h
+++ b/cache.h
@@ -1321,6 +1321,7 @@ struct object_context {
 	GET_OID_BLOB)
 
 extern int get_oid(const char *str, struct object_id *oid);
+extern int get_oidf(struct object_id *oid, const char *fmt, ...);
 extern int get_oid_commit(const char *str, struct object_id *oid);
 extern int get_oid_committish(const char *str, struct object_id *oid);
 extern int get_oid_tree(const char *str, struct object_id *oid);
diff --git a/sha1-name.c b/sha1-name.c
index c9cc1318b..261b960bb 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1471,6 +1471,25 @@ int get_oid(const char *name, struct object_id *oid)
 	return get_oid_with_context(name, 0, oid, &unused);
 }
 
+/*
+ * This returns a non-zero value if the string (built using printf
+ * format and the given arguments) is not a valid object.
+ */
+int get_oidf(struct object_id *oid, const char *fmt, ...)
+{
+	va_list ap;
+	int ret;
+	struct strbuf sb = STRBUF_INIT;
+
+	va_start(ap, fmt);
+	strbuf_vaddf(&sb, fmt, ap);
+	va_end(ap);
+
+	ret = get_oid(sb.buf, oid);
+	strbuf_release(&sb);
+
+	return ret;
+}
 
 /*
  * Many callers know that the user meant to name a commit-ish by
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 02/26] stash: improve option parsing test coverage
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 01/26] sha1-name.c: added 'get_oidf', which acts like 'get_oid' Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 03/26] stash: update test cases conform to coding guidelines Paul-Sebastian Ungureanu
                       ` (24 subsequent siblings)
  26 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

In preparation for converting the stash command incrementally to
a builtin command, this patch improves test coverage of the option
parsing. Both for having too many parameters, or too few.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 1f871d3cc..af7586d43 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '
 	test foo = "$(cat file/file)"
 '
 
+test_expect_success 'giving too many ref arguments does not modify files' '
+	git stash clear &&
+	test_when_finished "git reset --hard HEAD" &&
+	echo foo >file2 &&
+	git stash &&
+	echo bar >file2 &&
+	git stash &&
+	test-tool chmtime =123456789 file2 &&
+	for type in apply pop "branch stash-branch"
+	do
+		test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
+		test_i18ngrep "Too many revisions" err &&
+		test 123456789 = $(test-tool chmtime -g file2) || return 1
+	done
+'
+
+test_expect_success 'drop: too many arguments errors out (does nothing)' '
+	git stash list >expect &&
+	test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
+	test_i18ngrep "Too many revisions" err &&
+	git stash list >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'show: too many arguments errors out (does nothing)' '
+	test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
+	test_i18ngrep "Too many revisions" err &&
+	test_must_be_empty out
+'
+
 test_expect_success 'stash create - no changes' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
@@ -479,6 +509,11 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
+test_expect_success 'stash branch complains with no arguments' '
+	test_must_fail git stash branch 2>err &&
+	test_i18ngrep "No branch name specified" err
+'
+
 test_expect_success 'stash show format defaults to --stat' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 03/26] stash: update test cases conform to coding guidelines
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 01/26] sha1-name.c: added 'get_oidf', which acts like 'get_oid' Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 02/26] stash: improve option parsing test coverage Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive Paul-Sebastian Ungureanu
                       ` (23 subsequent siblings)
  26 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Removed whitespaces after redirection operators.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 59 deletions(-)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index af7586d43..de6cab1fe 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -8,22 +8,22 @@ test_description='Test git stash'
 . ./test-lib.sh
 
 test_expect_success 'stash some dirty working directory' '
-	echo 1 > file &&
+	echo 1 >file &&
 	git add file &&
 	echo unrelated >other-file &&
 	git add other-file &&
 	test_tick &&
 	git commit -m initial &&
-	echo 2 > file &&
+	echo 2 >file &&
 	git add file &&
-	echo 3 > file &&
+	echo 3 >file &&
 	test_tick &&
 	git stash &&
 	git diff-files --quiet &&
 	git diff-index --cached --quiet HEAD
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/file b/file
 index 0cfbf08..00750ed 100644
 --- a/file
@@ -35,7 +35,7 @@ EOF
 
 test_expect_success 'parents of stash' '
 	test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
-	git diff stash^2..stash > output &&
+	git diff stash^2..stash >output &&
 	test_cmp output expect
 '
 
@@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '
 
 test_expect_success 'apply stashed changes (including index)' '
 	git reset --hard HEAD^ &&
-	echo 6 > other-file &&
+	echo 6 >other-file &&
 	git add other-file &&
 	test_tick &&
 	git commit -m other-file &&
@@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '
 
 test_expect_success 'drop top stash' '
 	git reset --hard &&
-	git stash list > stashlist1 &&
-	echo 7 > file &&
+	git stash list >expected &&
+	echo 7 >file &&
 	git stash &&
 	git stash drop &&
-	git stash list > stashlist2 &&
-	test_cmp stashlist1 stashlist2 &&
+	git stash list >actual &&
+	test_cmp expected actual &&
 	git stash apply &&
 	test 3 = $(cat file) &&
 	test 1 = $(git show :file) &&
@@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '
 
 test_expect_success 'drop middle stash' '
 	git reset --hard &&
-	echo 8 > file &&
+	echo 8 >file &&
 	git stash &&
-	echo 9 > file &&
+	echo 9 >file &&
 	git stash &&
 	git stash drop stash@{1} &&
 	test 2 = $(git stash list | wc -l) &&
@@ -160,7 +160,7 @@ test_expect_success 'stash pop' '
 	test 0 = $(git stash list | wc -l)
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/file2 b/file2
 new file mode 100644
 index 0000000..1fe912c
@@ -170,7 +170,7 @@ index 0000000..1fe912c
 +bar2
 EOF
 
-cat > expect1 << EOF
+cat >expect1 <<EOF
 diff --git a/file b/file
 index 257cc56..5716ca5 100644
 --- a/file
@@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644
 +bar
 EOF
 
-cat > expect2 << EOF
+cat >expect2 <<EOF
 diff --git a/file b/file
 index 7601807..5716ca5 100644
 --- a/file
@@ -198,79 +198,79 @@ index 0000000..1fe912c
 EOF
 
 test_expect_success 'stash branch' '
-	echo foo > file &&
+	echo foo >file &&
 	git commit file -m first &&
-	echo bar > file &&
-	echo bar2 > file2 &&
+	echo bar >file &&
+	echo bar2 >file2 &&
 	git add file2 &&
 	git stash &&
-	echo baz > file &&
+	echo baz >file &&
 	git commit file -m second &&
 	git stash branch stashbranch &&
 	test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
 	test $(git rev-parse HEAD) = $(git rev-parse master^) &&
-	git diff --cached > output &&
+	git diff --cached >output &&
 	test_cmp output expect &&
-	git diff > output &&
+	git diff >output &&
 	test_cmp output expect1 &&
 	git add file &&
 	git commit -m alternate\ second &&
-	git diff master..stashbranch > output &&
+	git diff master..stashbranch >output &&
 	test_cmp output expect2 &&
 	test 0 = $(git stash list | wc -l)
 '
 
 test_expect_success 'apply -q is quiet' '
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	git stash apply -q > output.out 2>&1 &&
+	git stash apply -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'save -q is quiet' '
-	git stash save --quiet > output.out 2>&1 &&
+	git stash save --quiet >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'pop -q is quiet' '
-	git stash pop -q > output.out 2>&1 &&
+	git stash pop -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'pop -q --index works and is quiet' '
-	echo foo > file &&
+	echo foo >file &&
 	git add file &&
 	git stash save --quiet &&
-	git stash pop -q --index > output.out 2>&1 &&
+	git stash pop -q --index >output.out 2>&1 &&
 	test foo = "$(git show :file)" &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'drop -q is quiet' '
 	git stash &&
-	git stash drop -q > output.out 2>&1 &&
+	git stash drop -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'stash -k' '
-	echo bar3 > file &&
-	echo bar4 > file2 &&
+	echo bar3 >file &&
+	echo bar4 >file2 &&
 	git add file2 &&
 	git stash -k &&
 	test bar,bar4 = $(cat file),$(cat file2)
 '
 
 test_expect_success 'stash --no-keep-index' '
-	echo bar33 > file &&
-	echo bar44 > file2 &&
+	echo bar33 >file &&
+	echo bar44 >file2 &&
 	git add file2 &&
 	git stash --no-keep-index &&
 	test bar,bar2 = $(cat file),$(cat file2)
 '
 
 test_expect_success 'stash --invalid-option' '
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	test_must_fail git stash --invalid-option &&
 	test_must_fail git stash save --invalid-option &&
@@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	git stash branch stash-branch ${STASH_ID} &&
-	test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
+	test_when_finished "git reset --hard HEAD && git checkout master &&
+	git branch -D stash-branch" &&
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
@@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	git stash branch stash-branch ${STASH_ID} &&
-	test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
+	test_when_finished "git reset --hard HEAD && git checkout master &&
+	git branch -D stash-branch" &&
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
@@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	echo "1	0	file" >expected &&
@@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	echo "1	0	file" >expected &&
@@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	echo bar > file &&
+	echo bar >file &&
 	git stash &&
 	test_must_fail git stash drop $(git rev-parse stash@{0}) &&
 	git stash pop &&
@@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	echo bar > file &&
+	echo bar >file &&
 	git stash &&
 	test_must_fail git stash pop $(git rev-parse stash@{0}) &&
 	git stash pop &&
@@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re
 
 test_expect_success 'ref with non-existent reflog' '
 	git stash clear &&
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	git stash &&
 	test_must_fail git rev-parse --quiet --verify does-not-exist &&
@@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '
 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash clear &&
 	test_must_fail git stash drop stash@{0} &&
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	git stash &&
 	test_must_fail git stash drop stash@{1} &&
@@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu
 	test_i18ncmp expect actual
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/HEAD b/HEAD
 new file mode 100644
 index 0000000..fe0cbee
@@ -737,14 +739,14 @@ EOF
 test_expect_success 'stash where working directory contains "HEAD" file' '
 	git stash clear &&
 	git reset --hard &&
-	echo file-not-a-ref > HEAD &&
+	echo file-not-a-ref >HEAD &&
 	git add HEAD &&
 	test_tick &&
 	git stash &&
 	git diff-files --quiet &&
 	git diff-index --cached --quiet HEAD &&
 	test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
-	git diff stash^..stash > output &&
+	git diff stash^..stash >output &&
 	test_cmp output expect
 '
 
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (2 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 03/26] stash: update test cases conform to coding guidelines Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 19:31       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 05/26] stash: convert apply to builtin Paul-Sebastian Ungureanu
                       ` (22 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Renamed some test cases' labels to be more descriptive and under 80
characters per line.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index de6cab1fe..8d002a7f2 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	test_cmp expected actual
 '
 
-test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
+test_expect_success 'drop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
@@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r
 	git reset --hard HEAD
 '
 
-test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
+test_expect_success 'pop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
@@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form "n", n >= N' '
 	git stash drop
 '
 
-test_expect_success 'stash branch should not drop the stash if the branch exists' '
+test_expect_success 'branch: should not drop the stash if the branch exists' '
 	git stash clear &&
 	echo foo >file &&
 	git add file &&
@@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists
 	git rev-parse stash@{0} --
 '
 
-test_expect_success 'stash branch should not drop the stash if the apply fails' '
+test_expect_success 'branch: should not drop the stash if the apply fails' '
 	git stash clear &&
 	git reset HEAD~1 --hard &&
 	echo foo >file &&
@@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'
 	git rev-parse stash@{0} --
 '
 
-test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
+test_expect_success 'apply: shows same status as git status (relative to ./)' '
 	git stash clear &&
 	echo 1 >subdir/subfile1 &&
 	echo 2 >subdir/subfile2 &&
@@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '
 	test_i18ncmp expect actual
 '
 
-test_expect_success 'stash push with pathspec shows no changes when there are none' '
+test_expect_success 'push: <pathspec> shows no changes when there are none' '
 	>foo &&
 	git add foo &&
 	git commit -m "tmp" &&
@@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no
 	test_i18ncmp expect actual
 '
 
-test_expect_success 'stash push with pathspec not in the repository errors out' '
+test_expect_success 'push: <pathspec> not in the repository errors out' '
 	>untracked &&
 	test_must_fail git stash push untracked &&
 	test_path_is_file untracked
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 05/26] stash: convert apply to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (3 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-08 20:18       ` Junio C Hamano
  2018-08-18 16:09       ` Duy Nguyen
  2018-08-08 18:58     ` [GSoC][PATCH v7 06/26] stash: convert drop and clear " Paul-Sebastian Ungureanu
                       ` (21 subsequent siblings)
  26 siblings, 2 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add a builtin helper for performing stash commands. Converting
all at once proved hard to review, so starting with just apply
lets conversion get started without the other commands being
finished.

The helper is being implemented as a drop in replacement for
stash so that when it is complete it can simply be renamed and
the shell script deleted.

Delete the contents of the apply_stash shell function and replace
it with a call to stash--helper apply until pop is also
converted.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 .gitignore              |   1 +
 Makefile                |   1 +
 builtin.h               |   1 +
 builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |  78 +------
 git.c                   |   1 +
 6 files changed, 463 insertions(+), 71 deletions(-)
 create mode 100644 builtin/stash--helper.c

diff --git a/.gitignore b/.gitignore
index 3284a1e9b..3abbba81a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -156,6 +156,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
diff --git a/Makefile b/Makefile
index bc4fc8eea..af82bc7ee 100644
--- a/Makefile
+++ b/Makefile
@@ -1083,6 +1083,7 @@ BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-index.o
 BUILTIN_OBJS += builtin/show-ref.o
+BUILTIN_OBJS += builtin/stash--helper.o
 BUILTIN_OBJS += builtin/stripspace.o
 BUILTIN_OBJS += builtin/submodule--helper.o
 BUILTIN_OBJS += builtin/symbolic-ref.o
diff --git a/builtin.h b/builtin.h
index 0362f1ce2..55ceb1574 100644
--- a/builtin.h
+++ b/builtin.h
@@ -222,6 +222,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
 extern int cmd_show_index(int argc, const char **argv, const char *prefix);
 extern int cmd_status(int argc, const char **argv, const char *prefix);
+extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
new file mode 100644
index 000000000..ef6a9d30d
--- /dev/null
+++ b/builtin/stash--helper.c
@@ -0,0 +1,452 @@
+#include "builtin.h"
+#include "config.h"
+#include "parse-options.h"
+#include "refs.h"
+#include "lockfile.h"
+#include "cache-tree.h"
+#include "unpack-trees.h"
+#include "merge-recursive.h"
+#include "argv-array.h"
+#include "run-command.h"
+#include "dir.h"
+#include "rerere.h"
+
+static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char * const git_stash_helper_apply_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char *ref_stash = "refs/stash";
+static int quiet;
+static struct strbuf stash_index_path = STRBUF_INIT;
+
+/*
+ * w_commit is set to the commit containing the working tree
+ * b_commit is set to the base commit
+ * i_commit is set to the commit containing the index tree
+ * u_commit is set to the commit containing the untracked files tree
+ * w_tree is set to the working tree
+ * b_tree is set to the base tree
+ * i_tree is set to the index tree
+ * u_tree is set to the untracked files tree
+ */
+
+struct stash_info {
+	struct object_id w_commit;
+	struct object_id b_commit;
+	struct object_id i_commit;
+	struct object_id u_commit;
+	struct object_id w_tree;
+	struct object_id b_tree;
+	struct object_id i_tree;
+	struct object_id u_tree;
+	struct strbuf revision;
+	int is_stash_ref;
+	int has_u;
+};
+
+static void free_stash_info(struct stash_info *info)
+{
+	strbuf_release(&info->revision);
+}
+
+static void assert_stash_like(struct stash_info *info, const char * revision)
+{
+	if (get_oidf(&info->b_commit, "%s^1", revision) ||
+	    get_oidf(&info->w_tree, "%s:", revision) ||
+	    get_oidf(&info->b_tree, "%s^1:", revision) ||
+	    get_oidf(&info->i_tree, "%s^2:", revision)) {
+		free_stash_info(info);
+		error(_("'%s' is not a stash-like commit"), revision);
+		exit(128);
+	}
+}
+
+static int get_stash_info(struct stash_info *info, int argc, const char **argv)
+{
+	struct strbuf symbolic = STRBUF_INIT;
+	int ret;
+	const char *revision;
+	const char *commit = NULL;
+	char *end_of_rev;
+	char *expanded_ref;
+	struct object_id dummy;
+
+	if (argc > 1) {
+		int i;
+		struct strbuf refs_msg = STRBUF_INIT;
+		for (i = 0; i < argc; ++i)
+			strbuf_addf(&refs_msg, " '%s'", argv[i]);
+
+		fprintf_ln(stderr, _("Too many revisions specified:%s"),
+			   refs_msg.buf);
+		strbuf_release(&refs_msg);
+
+		return -1;
+	}
+
+	if (argc == 1)
+		commit = argv[0];
+
+	strbuf_init(&info->revision, 0);
+	if (!commit) {
+		if (!ref_exists(ref_stash)) {
+			free_stash_info(info);
+			fprintf_ln(stderr, "No stash entries found.");
+			return -1;
+		}
+
+		strbuf_addf(&info->revision, "%s@{0}", ref_stash);
+	} else if (strspn(commit, "0123456789") == strlen(commit)) {
+		strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
+	} else {
+		strbuf_addstr(&info->revision, commit);
+	}
+
+	revision = info->revision.buf;
+
+	if (get_oid(revision, &info->w_commit)) {
+		error(_("%s is not a valid reference"), revision);
+		free_stash_info(info);
+		return -1;
+	}
+
+	assert_stash_like(info, revision);
+
+	info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
+
+	end_of_rev = strchrnul(revision, '@');
+	strbuf_add(&symbolic, revision, end_of_rev - revision);
+
+	ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
+	strbuf_release(&symbolic);
+	switch (ret) {
+	case 0: /* Not found, but valid ref */
+		info->is_stash_ref = 0;
+		break;
+	case 1:
+		info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
+		break;
+	default: /* Invalid or ambiguous */
+		free_stash_info(info);
+	}
+
+	free(expanded_ref);
+	return !(ret == 0 || ret == 1);
+}
+
+static int reset_tree(struct object_id *i_tree, int update, int reset)
+{
+	struct unpack_trees_options opts;
+	int nr_trees = 1;
+	struct tree_desc t[MAX_UNPACK_TREES];
+	struct tree *tree;
+	struct lock_file lock_file = LOCK_INIT;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+
+	memset(&opts, 0, sizeof(opts));
+
+	tree = parse_tree_indirect(i_tree);
+	if (parse_tree(tree))
+		return -1;
+
+	init_tree_desc(t, tree->buffer, tree->size);
+
+	opts.head_idx = 1;
+	opts.src_index = &the_index;
+	opts.dst_index = &the_index;
+	opts.merge = 1;
+	opts.reset = reset;
+	opts.update = update;
+	opts.fn = oneway_merge;
+
+	if (unpack_trees(nr_trees, t, &opts))
+		return -1;
+
+	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		return error(_("unable to write new index file"));
+
+	return 0;
+}
+
+static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *w_commit_hex = oid_to_hex(w_commit);
+
+	/*
+	 * Diff-tree would not be very hard to replace with a native function,
+	 * however it should be done together with apply_cached.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
+	argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
+
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int apply_cached(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Apply currently only reads either from stdin or a file, thus
+	 * apply_all_patches would have to be updated to optionally take a
+	 * buffer.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int reset_head(const char *prefix)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Reset is overall quite simple, however there is no current public
+	 * API for resetting.
+	 */
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "reset");
+
+	return run_command(&cp);
+}
+
+static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *c_tree_hex = oid_to_hex(c_tree);
+
+	/*
+	 * diff-index is very similar to diff-tree above, and should be
+	 * converted together with update_index.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only",
+			 "--diff-filter=A", NULL);
+	argv_array_push(&cp.args, c_tree_hex);
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int update_index(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Update-index is very complicated and may need to have a public
+	 * function exposed in order to remove this forking.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int restore_untracked(struct object_id *u_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int res;
+
+	/*
+	 * We need to run restore files from a given index, but without
+	 * affecting the current index, so we use GIT_INDEX_FILE with
+	 * run_command to fork processes that will not interfere.
+	 */
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "read-tree");
+	argv_array_push(&cp.args, oid_to_hex(u_tree));
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (run_command(&cp)) {
+		remove_path(stash_index_path.buf);
+		return -1;
+	}
+
+	child_process_init(&cp);
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+
+	res = run_command(&cp);
+	remove_path(stash_index_path.buf);
+	return res;
+}
+
+static int do_apply_stash(const char *prefix, struct stash_info *info,
+	int index)
+{
+	struct merge_options o;
+	struct object_id c_tree;
+	struct object_id index_tree;
+	const struct object_id *bases[1];
+	struct commit *result;
+	int ret;
+	int has_index = index;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))
+		return error(_("Cannot apply a stash in the middle of a merge"));
+
+	if (index) {
+		if (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree,
+			&info->i_tree)) {
+			has_index = 0;
+		} else {
+			struct strbuf out = STRBUF_INIT;
+
+			if (diff_tree_binary(&out, &info->w_commit)) {
+				strbuf_release(&out);
+				return -1;
+			}
+
+			ret = apply_cached(&out);
+			strbuf_release(&out);
+			if (ret)
+				return -1;
+
+			discard_cache();
+			read_cache();
+			if (write_cache_as_tree(&index_tree, 0, NULL))
+				return -1;
+
+			reset_head(prefix);
+		}
+	}
+
+	if (info->has_u && restore_untracked(&info->u_tree))
+		return error(_("Could not restore untracked files from stash"));
+
+	init_merge_options(&o);
+
+	o.branch1 = "Updated upstream";
+	o.branch2 = "Stashed changes";
+
+	if (!oidcmp(&info->b_tree, &c_tree))
+		o.branch1 = "Version stash was based on";
+
+	if (quiet)
+		o.verbosity = 0;
+
+	if (o.verbosity >= 3)
+		printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
+
+	bases[0] = &info->b_tree;
+
+	ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
+				      &result);
+	if (ret != 0) {
+		rerere(0);
+
+		if (index)
+			fprintf_ln(stderr, _("Index was not unstashed."));
+
+		return ret;
+	}
+
+	if (has_index) {
+		if (reset_tree(&index_tree, 0, 0))
+			return -1;
+	} else {
+		struct strbuf out = STRBUF_INIT;
+
+		if (get_newly_staged(&out, &c_tree)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		if (reset_tree(&c_tree, 0, 1)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		ret = update_index(&out);
+		strbuf_release(&out);
+		if (ret)
+			return -1;
+
+		discard_cache();
+	}
+
+	if (quiet) {
+		if (refresh_cache(REFRESH_QUIET))
+			warning("could not refresh index");
+	} else {
+		struct child_process cp = CHILD_PROCESS_INIT;
+
+		/*
+		 * Status is quite simple and could be replaced with calls to
+		 * wt_status in the future, but it adds complexities which may
+		 * require more tests.
+		 */
+		cp.git_cmd = 1;
+		cp.dir = prefix;
+		argv_array_push(&cp.args, "status");
+		run_command(&cp);
+	}
+
+	return 0;
+}
+
+static int apply_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0;
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_apply_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	ret = do_apply_stash(prefix, &info, index);
+	free_stash_info(&info);
+	return ret;
+}
+
+int cmd_stash__helper(int argc, const char **argv, const char *prefix)
+{
+	pid_t pid = getpid();
+	const char *index_file;
+
+	struct option options[] = {
+		OPT_END()
+	};
+
+	git_config(git_default_config, NULL);
+
+	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
+			     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
+
+	index_file = get_index_file();
+	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
+		    (uintmax_t)pid);
+
+	if (argc < 1)
+		usage_with_options(git_stash_helper_usage, options);
+	if (!strcmp(argv[0], "apply"))
+		return !!apply_stash(argc, argv, prefix);
+
+	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
+		      git_stash_helper_usage, options);
+}
diff --git a/git-stash.sh b/git-stash.sh
index 94793c1a9..809b1c2d1 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -566,76 +566,11 @@ assert_stash_ref() {
 }
 
 apply_stash () {
-
-	assert_stash_like "$@"
-
-	git update-index -q --refresh || die "$(gettext "unable to refresh index")"
-
-	# current index state
-	c_tree=$(git write-tree) ||
-		die "$(gettext "Cannot apply a stash in the middle of a merge")"
-
-	unstashed_index_tree=
-	if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
-			test "$c_tree" != "$i_tree"
-	then
-		git diff-tree --binary $s^2^..$s^2 | git apply --cached
-		test $? -ne 0 &&
-			die "$(gettext "Conflicts in index. Try without --index.")"
-		unstashed_index_tree=$(git write-tree) ||
-			die "$(gettext "Could not save index tree")"
-		git reset
-	fi
-
-	if test -n "$u_tree"
-	then
-		GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
-		GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
-		rm -f "$TMPindex" ||
-		die "$(gettext "Could not restore untracked files from stash entry")"
-	fi
-
-	eval "
-		GITHEAD_$w_tree='Stashed changes' &&
-		GITHEAD_$c_tree='Updated upstream' &&
-		GITHEAD_$b_tree='Version stash was based on' &&
-		export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
-	"
-
-	if test -n "$GIT_QUIET"
-	then
-		GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
-	fi
-	if git merge-recursive $b_tree -- $c_tree $w_tree
-	then
-		# No conflict
-		if test -n "$unstashed_index_tree"
-		then
-			git read-tree "$unstashed_index_tree"
-		else
-			a="$TMP-added" &&
-			git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
-			git read-tree --reset $c_tree &&
-			git update-index --add --stdin <"$a" ||
-				die "$(gettext "Cannot unstage modified files")"
-			rm -f "$a"
-		fi
-		squelch=
-		if test -n "$GIT_QUIET"
-		then
-			squelch='>/dev/null 2>&1'
-		fi
-		(cd "$START_DIR" && eval "git status $squelch") || :
-	else
-		# Merge conflict; keep the exit status from merge-recursive
-		status=$?
-		git rerere
-		if test -n "$INDEX_OPTION"
-		then
-			gettextln "Index was not unstashed." >&2
-		fi
-		exit $status
-	fi
+	cd "$START_DIR"
+	git stash--helper apply "$@"
+	res=$?
+	cd_to_toplevel
+	return $res
 }
 
 pop_stash() {
@@ -713,7 +648,8 @@ push)
 	;;
 apply)
 	shift
-	apply_stash "$@"
+	cd "$START_DIR"
+	git stash--helper apply "$@"
 	;;
 clear)
 	shift
diff --git a/git.c b/git.c
index fc7d15d54..a3c83f57e 100644
--- a/git.c
+++ b/git.c
@@ -543,6 +543,7 @@ static struct cmd_struct commands[] = {
 	{ "show-index", cmd_show_index },
 	{ "show-ref", cmd_show_ref, RUN_SETUP },
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
 	{ "stripspace", cmd_stripspace },
 	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 06/26] stash: convert drop and clear to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (4 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 05/26] stash: convert apply to builtin Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 07/26] stash: convert branch " Paul-Sebastian Ungureanu
                       ` (20 subsequent siblings)
  26 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add the drop and clear commands to the builtin helper. These two
are each simple, but are being added together as they are quite
related.

We have to unfortunately keep the drop and clear functions in the
shell script as functions are called with parameters internally
that are not valid when the commands are called externally. Once
pop is converted they can both be removed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   4 +-
 2 files changed, 117 insertions(+), 2 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index ef6a9d30d..ae719b7fc 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,7 +12,14 @@
 #include "rerere.h"
 
 static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper clear"),
+	NULL
+};
+
+static const char * const git_stash_helper_drop_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
 };
 
@@ -21,6 +28,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_clear_usage[] = {
+	N_("git stash--helper clear"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -140,6 +152,31 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
 	return !(ret == 0 || ret == 1);
 }
 
+static int do_clear_stash(void)
+{
+	struct object_id obj;
+	if (get_oid(ref_stash, &obj))
+		return 0;
+
+	return delete_ref(NULL, ref_stash, &obj, 0);
+}
+
+static int clear_stash(int argc, const char **argv, const char *prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_clear_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (argc != 0)
+		return error(_("git stash--helper clear with parameters is unimplemented"));
+
+	return do_clear_stash();
+}
+
 static int reset_tree(struct object_id *i_tree, int update, int reset)
 {
 	struct unpack_trees_options opts;
@@ -424,6 +461,80 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int do_drop_stash(const char *prefix, struct stash_info *info)
+{
+	struct child_process cp_reflog = CHILD_PROCESS_INIT;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int ret;
+
+	/*
+	 * reflog does not provide a simple function for deleting refs. One will
+	 * need to be added to avoid implementing too much reflog code here
+	 */
+
+	cp_reflog.git_cmd = 1;
+	argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
+			 "--rewrite", NULL);
+	argv_array_push(&cp_reflog.args, info->revision.buf);
+	ret = run_command(&cp_reflog);
+	if (!ret) {
+		if (!quiet)
+			printf(_("Dropped %s (%s)\n"), info->revision.buf,
+			       oid_to_hex(&info->w_commit));
+	} else {
+		return error(_("%s: Could not drop stash entry"),
+			     info->revision.buf);
+	}
+
+	/*
+	 * This could easily be replaced by get_oid, but currently it will throw
+	 * a fatal error when a reflog is empty, which we can not recover from.
+	 */
+	cp.git_cmd = 1;
+	/* Even though --quiet is specified, rev-parse still outputs the hash */
+	cp.no_stdout = 1;
+	argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
+	argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
+	ret = run_command(&cp);
+
+	/* do_clear_stash if we just dropped the last stash entry */
+	if (ret)
+		do_clear_stash();
+
+	return 0;
+}
+
+static void assert_stash_ref(struct stash_info *info)
+{
+	if (!info->is_stash_ref) {
+		free_stash_info(info);
+		error(_("'%s' is not a stash reference"), info->revision.buf);
+		exit(128);
+	}
+}
+
+static int drop_stash(int argc, const char **argv, const char *prefix)
+{
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_drop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	assert_stash_ref(&info);
+
+	ret = do_drop_stash(prefix, &info);
+	free_stash_info(&info);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -446,6 +557,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_stash_helper_usage, options);
 	if (!strcmp(argv[0], "apply"))
 		return !!apply_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "clear"))
+		return !!clear_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "drop"))
+		return !!drop_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 809b1c2d1..a99d5dc9e 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -653,7 +653,7 @@ apply)
 	;;
 clear)
 	shift
-	clear_stash "$@"
+	git stash--helper clear "$@"
 	;;
 create)
 	shift
@@ -665,7 +665,7 @@ store)
 	;;
 drop)
 	shift
-	drop_stash "$@"
+	git stash--helper drop "$@"
 	;;
 pop)
 	shift
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 07/26] stash: convert branch to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (5 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 06/26] stash: convert drop and clear " Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 08/26] stash: convert pop " Paul-Sebastian Ungureanu
                       ` (19 subsequent siblings)
  26 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add stash branch to the helper and delete the apply_to_branch
function from the shell script.

Checkout does not currently provide a function for checking out
a branch as cmd_checkout does a large amount of sanity checks
first that we require here.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 44 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 17 ++--------------
 2 files changed, 46 insertions(+), 15 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index ae719b7fc..1e4d07295 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -14,6 +14,7 @@
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	NULL
 };
@@ -28,6 +29,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_branch_usage[] = {
+	N_("git stash--helper branch <branchname> [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_clear_usage[] = {
 	N_("git stash--helper clear"),
 	NULL
@@ -535,6 +541,42 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int branch_stash(int argc, const char **argv, const char *prefix)
+{
+	const char *branch = NULL;
+	int ret;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct stash_info info;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_branch_usage, 0);
+
+	if (argc == 0)
+		return error(_("No branch name specified"));
+
+	branch = argv[0];
+
+	if (get_stash_info(&info, argc - 1, argv + 1))
+		return -1;
+
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "checkout", "-b", NULL);
+	argv_array_push(&cp.args, branch);
+	argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
+	ret = run_command(&cp);
+	if (!ret)
+		ret = do_apply_stash(prefix, &info, 1);
+	if (!ret && info.is_stash_ref)
+		ret = do_drop_stash(prefix, &info);
+
+	free_stash_info(&info);
+
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -561,6 +603,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		return !!drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "branch"))
+		return !!branch_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index a99d5dc9e..29d9f4425 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -598,20 +598,6 @@ drop_stash () {
 	clear_stash
 }
 
-apply_to_branch () {
-	test -n "$1" || die "$(gettext "No branch name specified")"
-	branch=$1
-	shift 1
-
-	set -- --index "$@"
-	assert_stash_like "$@"
-
-	git checkout -b $branch $REV^ &&
-	apply_stash "$@" && {
-		test -z "$IS_STASH_REF" || drop_stash "$@"
-	}
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -673,7 +659,8 @@ pop)
 	;;
 branch)
 	shift
-	apply_to_branch "$@"
+	cd "$START_DIR"
+	git stash--helper branch "$@"
 	;;
 *)
 	case $# in
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 08/26] stash: convert pop to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (6 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 07/26] stash: convert branch " Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-08 18:58     ` [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin Paul-Sebastian Ungureanu
                       ` (18 subsequent siblings)
  26 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add stash pop to the helper and delete the pop_stash, drop_stash,
assert_stash_ref functions from the shell script now that they
are no longer needed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-
 git-stash.sh            | 47 ++---------------------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 1e4d07295..d6bd468e0 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,7 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
-	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	NULL
@@ -24,6 +24,11 @@ static const char * const git_stash_helper_drop_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_pop_usage[] = {
+	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_apply_usage[] = {
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
 	NULL
@@ -541,6 +546,33 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int pop_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0, ret;
+	struct stash_info info;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_pop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	assert_stash_ref(&info);
+	if ((ret = do_apply_stash(prefix, &info, index)))
+		printf_ln(_("The stash entry is kept in case you need it again."));
+	else
+		ret = do_drop_stash(prefix, &info);
+
+	free_stash_info(&info);
+	return ret;
+}
+
 static int branch_stash(int argc, const char **argv, const char *prefix)
 {
 	const char *branch = NULL;
@@ -603,6 +635,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		return !!drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "pop"))
+		return !!pop_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "branch"))
 		return !!branch_stash(argc, argv, prefix);
 
diff --git a/git-stash.sh b/git-stash.sh
index 29d9f4425..8f2640fe9 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -554,50 +554,6 @@ assert_stash_like() {
 	}
 }
 
-is_stash_ref() {
-	is_stash_like "$@" && test -n "$IS_STASH_REF"
-}
-
-assert_stash_ref() {
-	is_stash_ref "$@" || {
-		args="$*"
-		die "$(eval_gettext "'\$args' is not a stash reference")"
-	}
-}
-
-apply_stash () {
-	cd "$START_DIR"
-	git stash--helper apply "$@"
-	res=$?
-	cd_to_toplevel
-	return $res
-}
-
-pop_stash() {
-	assert_stash_ref "$@"
-
-	if apply_stash "$@"
-	then
-		drop_stash "$@"
-	else
-		status=$?
-		say "$(gettext "The stash entry is kept in case you need it again.")"
-		exit $status
-	fi
-}
-
-drop_stash () {
-	assert_stash_ref "$@"
-
-	git reflog delete --updateref --rewrite "${REV}" &&
-		say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
-		die "$(eval_gettext "\${REV}: Could not drop stash entry")"
-
-	# clear_stash if we just dropped the last stash entry
-	git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
-	clear_stash
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -655,7 +611,8 @@ drop)
 	;;
 pop)
 	shift
-	pop_stash "$@"
+	cd "$START_DIR"
+	git stash--helper pop "$@"
 	;;
 branch)
 	shift
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (7 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 08/26] stash: convert pop " Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 19:41       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 10/26] stash: convert show to builtin Paul-Sebastian Ungureanu
                       ` (17 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Add stash list to the helper and delete the list_stash function
from the shell script.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++
 git-stash.sh            |  7 +------
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index d6bd468e0..daa4d0034 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,6 +12,7 @@
 #include "rerere.h"
 
 static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper list [<options>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_list_usage[] = {
+	N_("git stash--helper list [<options>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_drop_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
@@ -609,6 +615,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int list_stash(int argc, const char **argv, const char *prefix)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_list_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	if (!ref_exists(ref_stash))
+		return 0;
+
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
+			 "--first-parent", "-m", NULL);
+	argv_array_pushv(&cp.args, argv);
+	argv_array_push(&cp.args, ref_stash);
+	argv_array_push(&cp.args, "--");
+	return run_command(&cp);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -639,6 +668,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!pop_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "branch"))
 		return !!branch_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "list"))
+		return !!list_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 8f2640fe9..6052441aa 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -382,11 +382,6 @@ have_stash () {
 	git rev-parse --verify --quiet $ref_stash >/dev/null
 }
 
-list_stash () {
-	have_stash || return 0
-	git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
-}
-
 show_stash () {
 	ALLOW_UNKNOWN_FLAGS=t
 	assert_stash_like "$@"
@@ -574,7 +569,7 @@ test -n "$seen_non_option" || set "push" "$@"
 case "$1" in
 list)
 	shift
-	list_stash "$@"
+	git stash--helper list "$@"
 	;;
 show)
 	shift
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 10/26] stash: convert show to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (8 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 20:20       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation Paul-Sebastian Ungureanu
                       ` (16 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Add stash show to the helper and delete the show_stash, have_stash,
assert_stash_like, is_stash_like and parse_flags_and_rev functions
from the shell script now that they are no longer needed.

Before this commit, `git stash show` would ignore `--index` and
`--quiet` options. Now, `git stash show` errors out on `--index`
and does not display any message on `--quiet`, but errors out
if the stash is not empty.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c |  78 ++++++++++++++++++++++++
 git-stash.sh            | 132 +---------------------------------------
 2 files changed, 79 insertions(+), 131 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index daa4d0034..e764cd33e 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,6 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
+	N_("git stash--helper show [<stash>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -25,6 +26,11 @@ static const char * const git_stash_helper_list_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_show_usage[] = {
+	N_("git stash--helper show [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_drop_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
@@ -638,6 +644,76 @@ static int list_stash(int argc, const char **argv, const char *prefix)
 	return run_command(&cp);
 }
 
+static int show_stat = 1;
+static int show_patch;
+
+static int git_stash_config(const char *var, const char *value, void *cb)
+{
+	if (!strcmp(var, "stash.showStat")) {
+		show_stat = git_config_bool(var, value);
+		return 0;
+	}
+	if (!strcmp(var, "stash.showPatch")) {
+		show_patch = git_config_bool(var, value);
+		return 0;
+	}
+	return git_default_config(var, value, cb);
+}
+
+static int show_stash(int argc, const char **argv, const char *prefix)
+{
+	int i, ret = 0;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct argv_array args_refs = ARGV_ARRAY_INIT;
+	struct stash_info info;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_show_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "diff");
+
+	/* Push arguments which are not options into args_refs. */
+	for (i = 0; i < argc; ++i) {
+		if (argv[i][0] == '-')
+			argv_array_push(&cp.args, argv[i]);
+		else
+			argv_array_push(&args_refs, argv[i]);
+	}
+
+	if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {
+		child_process_clear(&cp);
+		argv_array_clear(&args_refs);
+		return -1;
+	}
+
+	/*
+	 * The config settings are applied only if there are not passed
+	 * any flags.
+	 */
+	if (cp.args.argc == 1) {
+		git_config(git_stash_config, NULL);
+		if (show_stat)
+			argv_array_push(&cp.args, "--stat");
+
+		if (show_patch)
+			argv_array_push(&cp.args, "-p");
+	}
+
+	argv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),
+			 oid_to_hex(&info.w_commit), NULL);
+
+	ret = run_command(&cp);
+
+	free_stash_info(&info);
+	argv_array_clear(&args_refs);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -670,6 +746,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!branch_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "list"))
 		return !!list_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "show"))
+		return !!show_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 6052441aa..0d05cbc1e 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -378,35 +378,6 @@ save_stash () {
 	fi
 }
 
-have_stash () {
-	git rev-parse --verify --quiet $ref_stash >/dev/null
-}
-
-show_stash () {
-	ALLOW_UNKNOWN_FLAGS=t
-	assert_stash_like "$@"
-
-	if test -z "$FLAGS"
-	then
-		if test "$(git config --bool stash.showStat || echo true)" = "true"
-		then
-			FLAGS=--stat
-		fi
-
-		if test "$(git config --bool stash.showPatch || echo false)" = "true"
-		then
-			FLAGS=${FLAGS}${FLAGS:+ }-p
-		fi
-
-		if test -z "$FLAGS"
-		then
-			return 0
-		fi
-	fi
-
-	git diff ${FLAGS} $b_commit $w_commit
-}
-
 show_help () {
 	exec git help stash
 	exit 1
@@ -448,107 +419,6 @@ show_help () {
 #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
 #
 
-parse_flags_and_rev()
-{
-	test "$PARSE_CACHE" = "$*" && return 0 # optimisation
-	PARSE_CACHE="$*"
-
-	IS_STASH_LIKE=
-	IS_STASH_REF=
-	INDEX_OPTION=
-	s=
-	w_commit=
-	b_commit=
-	i_commit=
-	u_commit=
-	w_tree=
-	b_tree=
-	i_tree=
-	u_tree=
-
-	FLAGS=
-	REV=
-	for opt
-	do
-		case "$opt" in
-			-q|--quiet)
-				GIT_QUIET=-t
-			;;
-			--index)
-				INDEX_OPTION=--index
-			;;
-			--help)
-				show_help
-			;;
-			-*)
-				test "$ALLOW_UNKNOWN_FLAGS" = t ||
-					die "$(eval_gettext "unknown option: \$opt")"
-				FLAGS="${FLAGS}${FLAGS:+ }$opt"
-			;;
-			*)
-				REV="${REV}${REV:+ }'$opt'"
-			;;
-		esac
-	done
-
-	eval set -- $REV
-
-	case $# in
-		0)
-			have_stash || die "$(gettext "No stash entries found.")"
-			set -- ${ref_stash}@{0}
-		;;
-		1)
-			:
-		;;
-		*)
-			die "$(eval_gettext "Too many revisions specified: \$REV")"
-		;;
-	esac
-
-	case "$1" in
-		*[!0-9]*)
-			:
-		;;
-		*)
-			set -- "${ref_stash}@{$1}"
-		;;
-	esac
-
-	REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
-		reference="$1"
-		die "$(eval_gettext "\$reference is not a valid reference")"
-	}
-
-	i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
-	set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
-	s=$1 &&
-	w_commit=$1 &&
-	b_commit=$2 &&
-	w_tree=$3 &&
-	b_tree=$4 &&
-	i_tree=$5 &&
-	IS_STASH_LIKE=t &&
-	test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
-	IS_STASH_REF=t
-
-	u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
-	u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
-}
-
-is_stash_like()
-{
-	parse_flags_and_rev "$@"
-	test -n "$IS_STASH_LIKE"
-}
-
-assert_stash_like() {
-	is_stash_like "$@" || {
-		args="$*"
-		die "$(eval_gettext "'\$args' is not a stash-like commit")"
-	}
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -573,7 +443,7 @@ list)
 	;;
 show)
 	shift
-	show_stash "$@"
+	git stash--helper show "$@"
 	;;
 save)
 	shift
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (9 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 10/26] stash: convert show to builtin Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 20:26       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API Paul-Sebastian Ungureanu
                       ` (15 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

It is already stated in documentation that it will accept any
option known to `git diff`, but not in the usage text and some
parts of the documentation.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 Documentation/git-stash.txt | 4 ++--
 builtin/stash--helper.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 7ef8c4791..e31ea7d30 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git stash' list [<options>]
-'git stash' show [<stash>]
+'git stash' show [<options>] [<stash>]
 'git stash' drop [-q|--quiet] [<stash>]
 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
 'git stash' branch <branchname> [<stash>]
@@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash
 The command takes options applicable to the 'git log'
 command to control what is shown and how. See linkgit:git-log[1].
 
-show [<stash>]::
+show [<options>] [<stash>]::
 
 	Show the changes recorded in the stash entry as a diff between the
 	stashed contents and the commit back when the stash entry was first
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index e764cd33e..0c1efca6b 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,7 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
-	N_("git stash--helper show [<stash>]"),
+	N_("git stash--helper show [<options>] [<stash>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -27,7 +27,7 @@ static const char * const git_stash_helper_list_usage[] = {
 };
 
 static const char * const git_stash_helper_show_usage[] = {
-	N_("git stash--helper show [<stash>]"),
+	N_("git stash--helper show [<options>] [<stash>]"),
 	NULL
 };
 
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (10 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 21:01       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 13/26] stash: update `git stash show` documentation Paul-Sebastian Ungureanu
                       ` (14 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Currently, `show_stash()` uses `cmd_diff()` to generate
the output. After this commit, the output will be generated
using the internal API.

Before this commit, `git stash show --quiet` would act like
`git diff` and error out if the stash is not empty. Now, the
`--quiet` option does not error out given an empty stash.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 28 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 0c1efca6b..ec8c38c6f 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -10,6 +10,8 @@
 #include "run-command.h"
 #include "dir.h"
 #include "rerere.h"
+#include "revision.h"
+#include "log-tree.h"
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
@@ -662,56 +664,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 
 static int show_stash(int argc, const char **argv, const char *prefix)
 {
-	int i, ret = 0;
-	struct child_process cp = CHILD_PROCESS_INIT;
-	struct argv_array args_refs = ARGV_ARRAY_INIT;
+	int i;
+	int flags = 0;
 	struct stash_info info;
+	struct rev_info rev;
+	struct argv_array stash_args = ARGV_ARRAY_INIT;
 	struct option options[] = {
 		OPT_END()
 	};
 
-	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_show_usage,
-			     PARSE_OPT_KEEP_UNKNOWN);
+	init_diff_ui_defaults();
+	git_config(git_diff_ui_config, NULL);
 
-	cp.git_cmd = 1;
-	argv_array_push(&cp.args, "diff");
+	init_revisions(&rev, prefix);
 
-	/* Push arguments which are not options into args_refs. */
-	for (i = 0; i < argc; ++i) {
-		if (argv[i][0] == '-')
-			argv_array_push(&cp.args, argv[i]);
+	/* Push arguments which are not options into stash_args. */
+	for (i = 1; i < argc; ++i) {
+		if (argv[i][0] != '-')
+			argv_array_push(&stash_args, argv[i]);
 		else
-			argv_array_push(&args_refs, argv[i]);
-	}
-
-	if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {
-		child_process_clear(&cp);
-		argv_array_clear(&args_refs);
-		return -1;
+			flags++;
 	}
 
 	/*
 	 * The config settings are applied only if there are not passed
 	 * any flags.
 	 */
-	if (cp.args.argc == 1) {
+	if (!flags) {
 		git_config(git_stash_config, NULL);
 		if (show_stat)
-			argv_array_push(&cp.args, "--stat");
+			rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;
+		if (show_patch) {
+			rev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;
+			rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
+		}
+	}
 
-		if (show_patch)
-			argv_array_push(&cp.args, "-p");
+	if (get_stash_info(&info, stash_args.argc, stash_args.argv)) {
+		argv_array_clear(&stash_args);
+		return -1;
 	}
 
-	argv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),
-			 oid_to_hex(&info.w_commit), NULL);
+	argc = setup_revisions(argc, argv, &rev, NULL);
+	if (!rev.diffopt.output_format)
+		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+	diff_setup_done(&rev.diffopt);
+	rev.diffopt.flags.recursive = 1;
+	setup_diff_pager(&rev.diffopt);
 
-	ret = run_command(&cp);
+	/*
+	 * We can return early if there was any option not recognised by
+	 * `diff_opt_parse()`, besides the word `stash`.
+	 */
+	if (argc > 1) {
+		free_stash_info(&info);
+		argv_array_clear(&stash_args);
+		usage_with_options(git_stash_helper_show_usage, options);
+	}
+
+	/* Do the diff thing. */
+	diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
+	log_tree_diff_flush(&rev);
 
 	free_stash_info(&info);
-	argv_array_clear(&args_refs);
-	return ret;
+	argv_array_clear(&stash_args);
+	return 0;
 }
 
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 13/26] stash: update `git stash show` documentation
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (11 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 21:08       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 14/26] stash: convert store to builtin Paul-Sebastian Ungureanu
                       ` (13 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Add in documentation about the change of behavior regarding
the `--quiet` option, which was introduced in the last commit.
(the `--quiet` option does not exit anymore with erorr if it
is given an empty stash as argument)

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 Documentation/git-stash.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index e31ea7d30..d60ebdb96 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -117,6 +117,9 @@ show [<options>] [<stash>]::
 	You can use stash.showStat and/or stash.showPatch config variables
 	to change the default behavior.
 
+	It accepts any option known to `git diff`, but acts different on
+	`--quiet` option and exit with zero regardless of differences.
+
 pop [--index] [-q|--quiet] [<stash>]::
 
 	Remove a single stashed state from the stash list and apply it
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 14/26] stash: convert store to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (12 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 13/26] stash: update `git stash show` documentation Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 21:26       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 15/26] stash: convert create " Paul-Sebastian Ungureanu
                       ` (12 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Add stash store to the helper and delete the store_stash function
from the shell script.

Add the usage string which was forgotten in the shell script.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 43 ++--------------------------------
 2 files changed, 54 insertions(+), 41 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index ec8c38c6f..5ff810f8c 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -20,6 +20,7 @@ static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
+	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
 	NULL
 };
 
@@ -58,6 +59,11 @@ static const char * const git_stash_helper_clear_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_store_usage[] = {
+	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -731,6 +737,50 @@ static int show_stash(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
+static int do_store_stash(const char *w_commit, const char *stash_msg,
+			  int quiet)
+{
+	int ret = 0;
+	struct object_id obj;
+
+	if (!stash_msg)
+		stash_msg  = xstrdup("Created via \"git stash--helper store\".");
+
+	ret = get_oid(w_commit, &obj);
+	if (!ret) {
+		ret = update_ref(stash_msg, ref_stash, &obj, NULL,
+				 REF_FORCE_CREATE_REFLOG,
+				 quiet ? UPDATE_REFS_QUIET_ON_ERR :
+				 UPDATE_REFS_MSG_ON_ERR);
+	}
+	if (ret && !quiet)
+		fprintf_ln(stderr, _("Cannot update %s with %s"),
+			   ref_stash, w_commit);
+
+	return ret;
+}
+
+static int store_stash(int argc, const char **argv, const char *prefix)
+{
+	const char *stash_msg = NULL;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_STRING('m', "message", &stash_msg, "message", N_("stash message")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_store_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	if (argc != 1) {
+		fprintf(stderr, _("\"git stash--helper store\" requires one <commit> argument\n"));
+		return -1;
+	}
+
+	return do_store_stash(argv[0], stash_msg, quiet);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -765,6 +815,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!list_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "show"))
 		return !!show_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "store"))
+		return !!store_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 0d05cbc1e..5739c5152 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -191,45 +191,6 @@ create_stash () {
 	die "$(gettext "Cannot record working tree state")"
 }
 
-store_stash () {
-	while test $# != 0
-	do
-		case "$1" in
-		-m|--message)
-			shift
-			stash_msg="$1"
-			;;
-		-m*)
-			stash_msg=${1#-m}
-			;;
-		--message=*)
-			stash_msg=${1#--message=}
-			;;
-		-q|--quiet)
-			quiet=t
-			;;
-		*)
-			break
-			;;
-		esac
-		shift
-	done
-	test $# = 1 ||
-	die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
-
-	w_commit="$1"
-	if test -z "$stash_msg"
-	then
-		stash_msg="Created via \"git stash store\"."
-	fi
-
-	git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
-	ret=$?
-	test $ret != 0 && test -z "$quiet" &&
-	die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
-	return $ret
-}
-
 push_stash () {
 	keep_index=
 	patch_mode=
@@ -308,7 +269,7 @@ push_stash () {
 		clear_stash || die "$(gettext "Cannot initialize stash")"
 
 	create_stash -m "$stash_msg" -u "$untracked" -- "$@"
-	store_stash -m "$stash_msg" -q $w_commit ||
+	git stash--helper store -m "$stash_msg" -q $w_commit ||
 	die "$(gettext "Cannot save the current status")"
 	say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
 
@@ -468,7 +429,7 @@ create)
 	;;
 store)
 	shift
-	store_stash "$@"
+	git stash--helper store "$@"
 	;;
 drop)
 	shift
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 15/26] stash: convert create to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (13 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 14/26] stash: convert store to builtin Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-15 22:13       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 16/26] stash: replace spawning a "read-tree" process Paul-Sebastian Ungureanu
                       ` (11 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Add stash create to the helper.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   2 +-
 2 files changed, 407 insertions(+), 1 deletion(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 5ff810f8c..a4e57899b 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -21,6 +21,7 @@ static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
+	N_("git stash--helper create [<message>]"),
 	NULL
 };
 
@@ -64,6 +65,11 @@ static const char * const git_stash_helper_store_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_create_usage[] = {
+	N_("git stash--helper create [<message>]"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -781,6 +787,404 @@ static int store_stash(int argc, const char **argv, const char *prefix)
 	return do_store_stash(argv[0], stash_msg, quiet);
 }
 
+/*
+ * `out` will be filled with the names of untracked files. The return value is:
+ *
+ * < 0 if there was a bug (any arg given outside the repo will be detected
+ *     by `setup_revision()`)
+ * = 0 if there are not any untracked files
+ * > 0 if there are untracked files
+ */
+static int get_untracked_files(const char **argv, int line_term,
+			       int include_untracked, struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "ls-files", "-o", NULL);
+	if (line_term)
+		argv_array_push(&cp.args, "-z");
+	if (include_untracked != 2)
+		argv_array_push(&cp.args, "--exclude-standard");
+	argv_array_push(&cp.args, "--");
+	if (argv)
+		argv_array_pushv(&cp.args, argv);
+
+	if (pipe_command(&cp, NULL, 0, out, 0, NULL, 0))
+		return -1;
+	return out->len;
+}
+
+/*
+ * The return value of `check_changes()` can be:
+ *
+ * < 0 if there was an error
+ * = 0 if there are no changes.
+ * > 0 if there are changes.
+ */
+static int check_changes(const char **argv, int include_untracked,
+			 const char *prefix)
+{
+	int result;
+	int ret = 0;
+	struct rev_info rev;
+	struct object_id dummy;
+	struct strbuf out = STRBUF_INIT;
+	struct argv_array args = ARGV_ARRAY_INIT;
+
+	init_revisions(&rev, prefix);
+	parse_pathspec(&rev.prune_data, 0, PATHSPEC_PREFER_FULL,
+		       prefix, argv);
+
+	rev.diffopt.flags.quick = 1;
+	rev.diffopt.flags.ignore_submodules = 1;
+	rev.abbrev = 0;
+
+	/* No initial commit. */
+	if (get_oid("HEAD", &dummy)) {
+		ret = -1;
+		goto done;
+	}
+
+	add_head_to_pending(&rev);
+	diff_setup_done(&rev.diffopt);
+
+	if (read_cache() < 0) {
+		ret = -1;
+		goto done;
+	}
+	result = run_diff_index(&rev, 1);
+	if (diff_result_code(&rev.diffopt, result)) {
+		ret = 1;
+		goto done;
+	}
+
+	object_array_clear(&rev.pending);
+	result = run_diff_files(&rev, 0);
+	if (diff_result_code(&rev.diffopt, result)) {
+		ret = 1;
+		goto done;
+	}
+
+	if (include_untracked && get_untracked_files(argv, 0,
+						     include_untracked, &out))
+		ret = 1;
+
+done:
+	strbuf_release(&out);
+	argv_array_clear(&args);
+	return ret;
+}
+
+static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
+				struct strbuf *out)
+{
+	int ret = 0;
+	struct strbuf untracked_msg = STRBUF_INIT;
+	struct strbuf out2 = STRBUF_INIT;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct child_process cp2 = CHILD_PROCESS_INIT;
+
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "update-index", "-z", "--add",
+			 "--remove", "--stdin", NULL);
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+
+	strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
+	if (pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp2.git_cmd = 1;
+	argv_array_push(&cp2.args, "write-tree");
+	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+	get_oid_hex(out2.buf, &info->u_tree);
+
+	if (commit_tree(untracked_msg.buf, untracked_msg.len,
+			&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
+		ret = -1;
+		goto done;
+	}
+
+done:
+	strbuf_release(&untracked_msg);
+	strbuf_release(&out2);
+	remove_path(stash_index_path.buf);
+	return ret;
+}
+
+static struct strbuf patch = STRBUF_INIT;
+
+static int stash_patch(struct stash_info *info, const char **argv)
+{
+	int ret = 0;
+	struct strbuf out2 = STRBUF_INIT;
+	struct child_process cp0 = CHILD_PROCESS_INIT;
+	struct child_process cp1 = CHILD_PROCESS_INIT;
+	struct child_process cp2 = CHILD_PROCESS_INIT;
+	struct child_process cp3 = CHILD_PROCESS_INIT;
+
+	remove_path(stash_index_path.buf);
+
+	cp0.git_cmd = 1;
+	argv_array_pushl(&cp0.args, "read-tree", "HEAD", NULL);
+	argv_array_pushf(&cp0.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (run_command(&cp0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp1.git_cmd = 1;
+	argv_array_pushl(&cp1.args, "add--interactive", "--patch=stash",
+			"--", NULL);
+	if (argv)
+		argv_array_pushv(&cp1.args, argv);
+	argv_array_pushf(&cp1.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (run_command(&cp1)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp2.git_cmd = 1;
+	argv_array_push(&cp2.args, "write-tree");
+	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	get_oid_hex(out2.buf, &info->w_tree);
+
+	cp3.git_cmd = 1;
+	argv_array_pushl(&cp3.args, "diff-tree", "-p", "HEAD",
+			 oid_to_hex(&info->w_tree), "--", NULL);
+	if (pipe_command(&cp3, NULL, 0, &patch, 0, NULL, 0))
+		ret = -1;
+
+	if (!patch.len) {
+		fprintf_ln(stdout, "No changes selected");
+		ret = 1;
+	}
+
+done:
+	strbuf_release(&out2);
+	remove_path(stash_index_path.buf);
+	return ret;
+}
+
+static int stash_working_tree(struct stash_info *info, const char **argv)
+{
+	int ret = 0;
+	struct child_process cp0 = CHILD_PROCESS_INIT;
+	struct child_process cp1 = CHILD_PROCESS_INIT;
+	struct child_process cp2 = CHILD_PROCESS_INIT;
+	struct child_process cp3 = CHILD_PROCESS_INIT;
+	struct strbuf out1 = STRBUF_INIT;
+	struct strbuf out3 = STRBUF_INIT;
+
+	cp0.git_cmd = 1;
+	argv_array_push(&cp0.args, "read-tree");
+	argv_array_pushf(&cp0.args, "--index-output=%s", stash_index_path.buf);
+	argv_array_pushl(&cp0.args, "-m", oid_to_hex(&info->i_tree), NULL);
+	if (run_command(&cp0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp1.git_cmd = 1;
+	argv_array_pushl(&cp1.args, "diff-index", "--name-only", "-z",
+			"HEAD", "--", NULL);
+	if (argv)
+		argv_array_pushv(&cp1.args, argv);
+	argv_array_pushf(&cp1.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+
+	if (pipe_command(&cp1, NULL, 0, &out1, 0, NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp2.git_cmd = 1;
+	argv_array_pushl(&cp2.args, "update-index", "-z", "--add",
+			 "--remove", "--stdin", NULL);
+	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+
+	if (pipe_command(&cp2, out1.buf, out1.len, NULL, 0, NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp3.git_cmd = 1;
+	argv_array_push(&cp3.args, "write-tree");
+	argv_array_pushf(&cp3.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (pipe_command(&cp3, NULL, 0, &out3, 0,NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	get_oid_hex(out3.buf, &info->w_tree);
+
+done:
+	strbuf_release(&out1);
+	strbuf_release(&out3);
+	remove_path(stash_index_path.buf);
+	return ret;
+}
+
+static int do_create_stash(int argc, const char **argv, const char *prefix,
+			   const char **stash_msg, int include_untracked,
+			   int patch_mode, struct stash_info *info)
+{
+	int untracked_commit_option = 0;
+	int ret = 0;
+	int subject_len;
+	int flags;
+	const char *head_short_sha1 = NULL;
+	const char *branch_ref = NULL;
+	const char *head_subject = NULL;
+	const char *branch_name = "(no branch)";
+	struct commit *head_commit = NULL;
+	struct commit_list *parents = NULL;
+	struct strbuf msg = STRBUF_INIT;
+	struct strbuf commit_tree_label = STRBUF_INIT;
+	struct strbuf out = STRBUF_INIT;
+	struct strbuf final_stash_msg = STRBUF_INIT;
+
+	read_cache_preload(NULL);
+	refresh_cache(REFRESH_QUIET);
+
+	if (!check_changes(argv, include_untracked, prefix)) {
+		ret = 1;
+		goto done;
+	}
+
+	if (get_oid("HEAD", &info->b_commit)) {
+		fprintf_ln(stderr, "You do not have the initial commit yet");
+		ret = -1;
+		goto done;
+	} else {
+		head_commit = lookup_commit(the_repository, &info->b_commit);
+	}
+
+	branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
+	if (flags & REF_ISSYMREF)
+		branch_name = strrchr(branch_ref, '/') + 1;
+	head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
+					     DEFAULT_ABBREV);
+	subject_len = find_commit_subject(get_commit_buffer(head_commit, NULL),
+					  &head_subject);
+	strbuf_addf(&msg, "%s: %s %.*s\n", branch_name, head_short_sha1,
+		    subject_len, head_subject);
+
+	strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
+	commit_list_insert(head_commit, &parents);
+	if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
+	    commit_tree(commit_tree_label.buf, commit_tree_label.len,
+			&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
+		fprintf_ln(stderr, "Cannot save the current index state");
+		ret = -1;
+		goto done;
+	}
+
+	if (include_untracked && get_untracked_files(argv, 1,
+						     include_untracked, &out)) {
+		if (save_untracked_files(info, &msg, &out)) {
+			printf_ln("Cannot save the untracked files");
+			ret = -1;
+			goto done;
+		}
+		untracked_commit_option = 1;
+	}
+	if (patch_mode) {
+		ret = stash_patch(info, argv);
+		if (ret < 0) {
+			printf_ln("Cannot save the current worktree state");
+			goto done;
+		} else if (ret > 0) {
+			goto done;
+		}
+	} else {
+		if (stash_working_tree(info, argv)) {
+			printf_ln("Cannot save the current worktree state");
+			ret = -1;
+			goto done;
+		}
+	}
+
+	if (!*stash_msg || !strlen(*stash_msg))
+		strbuf_addf(&final_stash_msg, "WIP on %s", msg.buf);
+	else
+		strbuf_addf(&final_stash_msg, "On %s: %s\n", branch_name,
+			    *stash_msg);
+	*stash_msg = strbuf_detach(&final_stash_msg, NULL);
+
+	/*
+	 * `parents` will be empty after calling `commit_tree()`, so there is
+	 * no need to call `free_commit_list()`
+	 */
+	parents = NULL;
+	if (untracked_commit_option)
+		commit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);
+	commit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);
+	commit_list_insert(head_commit, &parents);
+
+	if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
+			parents, &info->w_commit, NULL, NULL)) {
+		printf_ln("Cannot record working tree state");
+		ret = -1;
+		goto done;
+	}
+
+done:
+	strbuf_release(&commit_tree_label);
+	strbuf_release(&msg);
+	strbuf_release(&out);
+	strbuf_release(&final_stash_msg);
+	return ret;
+}
+
+static int create_stash(int argc, const char **argv, const char *prefix)
+{
+	int include_untracked = 0;
+	int ret = 0;
+	const char *stash_msg = NULL;
+	struct stash_info info;
+	struct option options[] = {
+		OPT_BOOL('u', "include-untracked", &include_untracked,
+			 N_("include untracked files in stash")),
+		OPT_STRING('m', "message", &stash_msg, N_("message"),
+			 N_("stash message")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_create_usage,
+			     0);
+
+	ret = do_create_stash(argc, argv, prefix, &stash_msg,
+			      include_untracked, 0, &info);
+
+	if (!ret)
+		printf_ln("%s", oid_to_hex(&info.w_commit));
+
+	/*
+	 * ret can be 1 if there were no changes. In this case, we should
+	 * not error out.
+	 */
+	return ret < 0;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -817,6 +1221,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!show_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "store"))
 		return !!store_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "create"))
+		return !!create_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 5739c5152..ab06e4ffb 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -425,7 +425,7 @@ clear)
 	;;
 create)
 	shift
-	create_stash -m "$*" && echo "$w_commit"
+	git stash--helper create --message "$*"
 	;;
 store)
 	shift
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 16/26] stash: replace spawning a "read-tree" process
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (14 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 15/26] stash: convert create " Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-18 21:07       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 17/26] stash: avoid spawning a "diff-index" process Paul-Sebastian Ungureanu
                       ` (10 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Instead of spawning a child process, make use of `reset_tree()`
function already implemented in `stash-helper.c`.
---
 builtin/stash--helper.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index a4e57899b..887b78d05 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -984,21 +984,18 @@ static int stash_patch(struct stash_info *info, const char **argv)
 static int stash_working_tree(struct stash_info *info, const char **argv)
 {
 	int ret = 0;
-	struct child_process cp0 = CHILD_PROCESS_INIT;
 	struct child_process cp1 = CHILD_PROCESS_INIT;
 	struct child_process cp2 = CHILD_PROCESS_INIT;
 	struct child_process cp3 = CHILD_PROCESS_INIT;
 	struct strbuf out1 = STRBUF_INIT;
 	struct strbuf out3 = STRBUF_INIT;
 
-	cp0.git_cmd = 1;
-	argv_array_push(&cp0.args, "read-tree");
-	argv_array_pushf(&cp0.args, "--index-output=%s", stash_index_path.buf);
-	argv_array_pushl(&cp0.args, "-m", oid_to_hex(&info->i_tree), NULL);
-	if (run_command(&cp0)) {
+	set_alternate_index_output(stash_index_path.buf);
+	if (reset_tree(&info->i_tree, 0, 0)) {
 		ret = -1;
 		goto done;
 	}
+	set_alternate_index_output(".git/index");
 
 	cp1.git_cmd = 1;
 	argv_array_pushl(&cp1.args, "diff-index", "--name-only", "-z",
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 17/26] stash: avoid spawning a "diff-index" process
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (15 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 16/26] stash: replace spawning a "read-tree" process Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-18 22:06       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 18/26] stash: convert push to builtin Paul-Sebastian Ungureanu
                       ` (9 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

This commits replaces spawning `diff-index` child process by using
the already existing `diff` API
---
 builtin/stash--helper.c | 56 ++++++++++++++++++++++++++++++-----------
 1 file changed, 42 insertions(+), 14 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 887b78d05..f905d3908 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,6 +12,7 @@
 #include "rerere.h"
 #include "revision.h"
 #include "log-tree.h"
+#include "diffcore.h"
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
@@ -297,6 +298,18 @@ static int reset_head(const char *prefix)
 	return run_command(&cp);
 }
 
+static void add_diff_to_buf(struct diff_queue_struct *q,
+			    struct diff_options *options,
+			    void *data)
+{
+	int i;
+	for (i = 0; i < q->nr; i++) {
+		struct diff_filepair *p = q->queue[i];
+		strbuf_addstr(data, p->one->path);
+		strbuf_addch(data, '\n');
+	}
+}
+
 static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
@@ -981,14 +994,16 @@ static int stash_patch(struct stash_info *info, const char **argv)
 	return ret;
 }
 
-static int stash_working_tree(struct stash_info *info, const char **argv)
+static int stash_working_tree(struct stash_info *info,
+			      const char **argv, const char *prefix)
 {
 	int ret = 0;
-	struct child_process cp1 = CHILD_PROCESS_INIT;
 	struct child_process cp2 = CHILD_PROCESS_INIT;
 	struct child_process cp3 = CHILD_PROCESS_INIT;
-	struct strbuf out1 = STRBUF_INIT;
 	struct strbuf out3 = STRBUF_INIT;
+	struct argv_array args = ARGV_ARRAY_INIT;
+	struct strbuf diff_output = STRBUF_INIT;
+	struct rev_info rev;
 
 	set_alternate_index_output(stash_index_path.buf);
 	if (reset_tree(&info->i_tree, 0, 0)) {
@@ -997,26 +1012,36 @@ static int stash_working_tree(struct stash_info *info, const char **argv)
 	}
 	set_alternate_index_output(".git/index");
 
-	cp1.git_cmd = 1;
-	argv_array_pushl(&cp1.args, "diff-index", "--name-only", "-z",
-			"HEAD", "--", NULL);
+	argv_array_push(&args, "dummy");
 	if (argv)
-		argv_array_pushv(&cp1.args, argv);
-	argv_array_pushf(&cp1.env_array, "GIT_INDEX_FILE=%s",
-			 stash_index_path.buf);
+		argv_array_pushv(&args, argv);
+	git_config(git_diff_basic_config, NULL);
+	init_revisions(&rev, prefix);
+	args.argc = setup_revisions(args.argc, args.argv, &rev, NULL);
+
+	rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
+	rev.diffopt.format_callback = add_diff_to_buf;
+	rev.diffopt.format_callback_data = &diff_output;
+
+	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
+		ret = -1;
+		goto done;
+	}
 
-	if (pipe_command(&cp1, NULL, 0, &out1, 0, NULL, 0)) {
+	add_pending_object(&rev, parse_object(the_repository, &info->b_commit), "");
+	if (run_diff_index(&rev, 0)) {
 		ret = -1;
 		goto done;
 	}
 
 	cp2.git_cmd = 1;
-	argv_array_pushl(&cp2.args, "update-index", "-z", "--add",
+	argv_array_pushl(&cp2.args, "update-index", "--add",
 			 "--remove", "--stdin", NULL);
 	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
 			 stash_index_path.buf);
 
-	if (pipe_command(&cp2, out1.buf, out1.len, NULL, 0, NULL, 0)) {
+	if (pipe_command(&cp2, diff_output.buf, diff_output.len,
+			 NULL, 0, NULL, 0)) {
 		ret = -1;
 		goto done;
 	}
@@ -1033,8 +1058,11 @@ static int stash_working_tree(struct stash_info *info, const char **argv)
 	get_oid_hex(out3.buf, &info->w_tree);
 
 done:
-	strbuf_release(&out1);
+	UNLEAK(rev);
 	strbuf_release(&out3);
+	argv_array_clear(&args);
+	object_array_clear(&rev.pending);
+	strbuf_release(&diff_output);
 	remove_path(stash_index_path.buf);
 	return ret;
 }
@@ -1112,7 +1140,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 			goto done;
 		}
 	} else {
-		if (stash_working_tree(info, argv)) {
+		if (stash_working_tree(info, argv, prefix)) {
 			printf_ln("Cannot save the current worktree state");
 			ret = -1;
 			goto done;
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 18/26] stash: convert push to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (16 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 17/26] stash: avoid spawning a "diff-index" process Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-18 15:36       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 19/26] stash: make push to be quiet Paul-Sebastian Ungureanu
                       ` (8 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

Add stash push to the helper.
---
 builtin/stash--helper.c | 209 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   6 +-
 2 files changed, 213 insertions(+), 2 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index f905d3908..c26cad3d5 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper clear"),
 	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
 	N_("git stash--helper create [<message>]"),
+	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
+	   "          [--] [<pathspec>...]]"),
 	NULL
 };
 
@@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_push_usage[] = {
+	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
+	   "          [--] [<pathspec>...]]"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -1210,6 +1220,203 @@ static int create_stash(int argc, const char **argv, const char *prefix)
 	return ret < 0;
 }
 
+static int do_push_stash(int argc, const char **argv, const char *prefix,
+			 int keep_index, int patch_mode, int include_untracked,
+			 int quiet, const char *stash_msg)
+{
+	int ret = 0;
+	struct pathspec ps;
+	struct stash_info info;
+	if (patch_mode && keep_index == -1)
+		keep_index = 1;
+
+	if (patch_mode && include_untracked) {
+		fprintf_ln(stderr, "Can't use --patch and --include-untracked or --all at the same time");
+		return -1;
+	}
+
+	parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
+
+	if (read_cache() < 0)
+		die(_("index file corrupt"));
+
+	if (!include_untracked && ps.nr) {
+		int i;
+		char *ps_matched = xcalloc(ps.nr, 1);
+
+		for (i = 0; i < active_nr; ++i) {
+			const struct cache_entry *ce = active_cache[i];
+			if (!ce_path_match(ce, &ps, ps_matched))
+				continue;
+		}
+
+		if (report_path_error(ps_matched, &ps, prefix)) {
+			fprintf_ln(stderr, "Did you forget to 'git add'?");
+			return -1;
+		}
+	}
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	if (!check_changes(argv, include_untracked, prefix)) {
+		fprintf_ln(stdout, "No local changes to save");
+		return 0;
+	}
+
+	if (!reflog_exists(ref_stash) && do_clear_stash()) {
+		fprintf_ln(stderr, "Cannot initialize stash");
+		return -1;
+	}
+
+	if ((ret = do_create_stash(argc, argv, prefix, &stash_msg,
+				   include_untracked, patch_mode, &info)))
+		return ret;
+
+	if (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {
+		fprintf(stderr, "Cannot save the current status");
+		return -1;
+	}
+
+	fprintf(stdout, "Saved working directory and index state %s", stash_msg);
+
+	if (!patch_mode) {
+		if (include_untracked && ps.nr == 0) {
+			struct child_process cp = CHILD_PROCESS_INIT;
+
+			cp.git_cmd = 1;
+			argv_array_pushl(&cp.args, "clean", "--force",
+					 "--quiet", "-d", NULL);
+			if (include_untracked == 2)
+				argv_array_push(&cp.args, "-x");
+			if (run_command(&cp))
+				return -1;
+		}
+		if (argc != 0) {
+			int i;
+			struct child_process cp1 = CHILD_PROCESS_INIT;
+			struct child_process cp2 = CHILD_PROCESS_INIT;
+			struct child_process cp3 = CHILD_PROCESS_INIT;
+			struct strbuf out = STRBUF_INIT;
+
+			cp1.git_cmd = 1;
+			argv_array_push(&cp1.args, "add");
+			if (!include_untracked)
+				argv_array_push(&cp1.args, "-u");
+			if (include_untracked == 2)
+				argv_array_push(&cp1.args, "--force");
+			argv_array_push(&cp1.args, "--");
+			for (i = 0; i < ps.nr; ++i)
+				argv_array_push(&cp1.args, ps.items[i].match);
+			if (run_command(&cp1))
+				return -1;
+
+			cp2.git_cmd = 1;
+			argv_array_pushl(&cp2.args, "diff-index", "-p",
+					 "--cached", "--binary", "HEAD", "--",
+					 NULL);
+			for (i = 0; i < ps.nr; ++i)
+				argv_array_push(&cp2.args, ps.items[i].match);
+			if (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))
+				return -1;
+
+			cp3.git_cmd = 1;
+			argv_array_pushl(&cp3.args, "apply", "--index", "-R",
+					 NULL);
+			if (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,
+					 0))
+				return -1;
+		} else {
+			struct child_process cp = CHILD_PROCESS_INIT;
+			cp.git_cmd = 1;
+			argv_array_pushl(&cp.args, "reset", "--hard", "-q",
+					 NULL);
+			if (run_command(&cp))
+				return -1;
+		}
+
+		if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
+			int i;
+			struct child_process cp1 = CHILD_PROCESS_INIT;
+			struct child_process cp2 = CHILD_PROCESS_INIT;
+			struct strbuf out = STRBUF_INIT;
+
+			if (reset_tree(&info.i_tree, 0, 1))
+				return -1;
+
+			cp1.git_cmd = 1;
+			argv_array_pushl(&cp1.args, "ls-files", "-z",
+					 "--modified", "--", NULL);
+			for (i = 0; i < ps.nr; ++i)
+				argv_array_push(&cp1.args, ps.items[i].match);
+			if (pipe_command(&cp1, NULL, 0, &out, 0, NULL, 0))
+				return -1;
+
+			cp2.git_cmd = 1;
+			argv_array_pushl(&cp2.args, "checkout-index", "-z",
+					 "--force", "--stdin", NULL);
+			if (pipe_command(&cp2, out.buf, out.len, NULL, 0, NULL,
+					 0))
+				return -1;
+		}
+	} else {
+		struct child_process cp = CHILD_PROCESS_INIT;
+
+		cp.git_cmd = 1;
+		argv_array_pushl(&cp.args, "apply", "-R", NULL);
+
+		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
+			fprintf_ln(stderr, "Cannot remove worktree changes");
+			return -1;
+		}
+
+		if (keep_index < 1) {
+			int i;
+			struct child_process cp = CHILD_PROCESS_INIT;
+
+			cp.git_cmd = 1;
+			argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
+			for (i = 0; i < ps.nr; ++i)
+				argv_array_push(&cp.args, ps.items[i].match);
+			if (run_command(&cp))
+				return -1;
+		}
+	}
+	return 0;
+}
+
+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 option options[] = {
+		OPT_SET_INT('k', "keep-index", &keep_index,
+			N_("keep index"), 1),
+		OPT_BOOL('p', "patch", &patch_mode,
+			N_("stash in patch mode")),
+		OPT_BOOL('q', "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()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_push_usage,
+			     0);
+
+	return do_push_stash(argc, argv, prefix, keep_index, patch_mode,
+			     include_untracked, quiet, stash_msg);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -1248,6 +1455,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!store_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "create"))
 		return !!create_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "push"))
+		return !!push_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index ab06e4ffb..c3146f62a 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -412,7 +412,8 @@ save)
 	;;
 push)
 	shift
-	push_stash "$@"
+	cd "$START_DIR"
+	git stash--helper push "$@"
 	;;
 apply)
 	shift
@@ -448,7 +449,8 @@ branch)
 *)
 	case $# in
 	0)
-		push_stash &&
+		cd "$START_DIR"
+		git stash--helper push &&
 		say "$(gettext "(To restore them type \"git stash apply\")")"
 		;;
 	*)
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 19/26] stash: make push to be quiet
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (17 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 18/26] stash: convert push to builtin Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-18 15:46       ` Thomas Gummerer
  2018-08-08 18:58     ` [GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q` Paul-Sebastian Ungureanu
                       ` (7 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

There is a change in behaviour with this commit. When there was
no initial commit, the shell version of stash would still display
a message. This commit makes `push` to not display any message if
`--quiet` or `-q` is specified.
---
 builtin/stash--helper.c | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index c26cad3d5..4fd79532c 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -1079,7 +1079,7 @@ static int stash_working_tree(struct stash_info *info,
 
 static int do_create_stash(int argc, const char **argv, const char *prefix,
 			   const char **stash_msg, int include_untracked,
-			   int patch_mode, struct stash_info *info)
+			   int patch_mode, struct stash_info *info, int quiet)
 {
 	int untracked_commit_option = 0;
 	int ret = 0;
@@ -1105,7 +1105,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 	}
 
 	if (get_oid("HEAD", &info->b_commit)) {
-		fprintf_ln(stderr, "You do not have the initial commit yet");
+		if (!quiet)
+			fprintf_ln(stderr, "You do not have the initial commit yet");
 		ret = -1;
 		goto done;
 	} else {
@@ -1127,7 +1128,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 	if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
 	    commit_tree(commit_tree_label.buf, commit_tree_label.len,
 			&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
-		fprintf_ln(stderr, "Cannot save the current index state");
+		if (!quiet)
+			fprintf_ln(stderr, "Cannot save the current index state");
 		ret = -1;
 		goto done;
 	}
@@ -1135,7 +1137,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 	if (include_untracked && get_untracked_files(argv, 1,
 						     include_untracked, &out)) {
 		if (save_untracked_files(info, &msg, &out)) {
-			printf_ln("Cannot save the untracked files");
+			if (!quiet)
+				printf_ln("Cannot save the untracked files");
 			ret = -1;
 			goto done;
 		}
@@ -1144,14 +1147,16 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 	if (patch_mode) {
 		ret = stash_patch(info, argv);
 		if (ret < 0) {
-			printf_ln("Cannot save the current worktree state");
+			if (!quiet)
+				printf_ln("Cannot save the current worktree state");
 			goto done;
 		} else if (ret > 0) {
 			goto done;
 		}
 	} else {
 		if (stash_working_tree(info, argv, prefix)) {
-			printf_ln("Cannot save the current worktree state");
+			if (!quiet)
+				printf_ln("Cannot save the current worktree state");
 			ret = -1;
 			goto done;
 		}
@@ -1176,7 +1181,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 
 	if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
 			parents, &info->w_commit, NULL, NULL)) {
-		printf_ln("Cannot record working tree state");
+		if (!quiet)
+			printf_ln("Cannot record working tree state");
 		ret = -1;
 		goto done;
 	}
@@ -1208,7 +1214,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
 			     0);
 
 	ret = do_create_stash(argc, argv, prefix, &stash_msg,
-			      include_untracked, 0, &info);
+			      include_untracked, 0, &info, 0);
 
 	if (!ret)
 		printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1261,25 +1267,31 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
 		return -1;
 
 	if (!check_changes(argv, include_untracked, prefix)) {
-		fprintf_ln(stdout, "No local changes to save");
+		if (!quiet)
+			fprintf_ln(stdout, "No local changes to save");
 		return 0;
 	}
 
 	if (!reflog_exists(ref_stash) && do_clear_stash()) {
-		fprintf_ln(stderr, "Cannot initialize stash");
+		if (!quiet)
+			fprintf_ln(stderr, "Cannot initialize stash");
 		return -1;
 	}
 
 	if ((ret = do_create_stash(argc, argv, prefix, &stash_msg,
-				   include_untracked, patch_mode, &info)))
+				   include_untracked, patch_mode, &info,
+				   quiet)))
 		return ret;
 
 	if (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {
-		fprintf(stderr, "Cannot save the current status");
+		if (!quiet)
+			fprintf_ln(stderr, "Cannot save the current status");
 		return -1;
 	}
 
-	fprintf(stdout, "Saved working directory and index state %s", stash_msg);
+	if (!quiet)
+		fprintf(stdout, "Saved working directory and index state %s",
+			stash_msg);
 
 	if (!patch_mode) {
 		if (include_untracked && ps.nr == 0) {
@@ -1367,7 +1379,8 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
 		argv_array_pushl(&cp.args, "apply", "-R", NULL);
 
 		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
-			fprintf_ln(stderr, "Cannot remove worktree changes");
+			if (!quiet)
+				fprintf_ln(stderr, "Cannot remove worktree changes");
 			return -1;
 		}
 
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q`
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (18 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 19/26] stash: make push to be quiet Paul-Sebastian Ungureanu
@ 2018-08-08 18:58     ` Paul-Sebastian Ungureanu
  2018-08-18 16:12       ` Thomas Gummerer
  2018-08-08 18:59     ` [GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process Paul-Sebastian Ungureanu
                       ` (6 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:58 UTC (permalink / raw)
  To: git

This commit introduces more tests for the quiet option of
`git stash push`.
---
 t/t3903-stash.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 8d002a7f2..b78db74ae 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1064,6 +1064,27 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '
 	test_path_is_file untracked
 '
 
+test_expect_success 'push: -q is quiet with changes' '
+	>foo &&
+	git stash push -q >output 2>&1 &&
+	test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet with no changes' '
+	git stash push -q >output 2>&1 &&
+	test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet even if there is no initial commit' '
+	git init foo_dir &&
+	cd foo_dir &&
+	touch bar &&
+	test_must_fail git stash push -q >output 2>&1 &&
+	test_must_be_empty output &&
+	cd .. &&
+	rm -rf foo_dir
+'
+
 test_expect_success 'untracked files are left in place when -u is not given' '
 	>file &&
 	git add file &&
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (19 preceding siblings ...)
  2018-08-08 18:58     ` [GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q` Paul-Sebastian Ungureanu
@ 2018-08-08 18:59     ` Paul-Sebastian Ungureanu
  2018-08-18 22:17       ` Thomas Gummerer
  2018-08-08 18:59     ` [GSoC][PATCH v7 22/26] stash: convert save to builtin Paul-Sebastian Ungureanu
                       ` (5 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:59 UTC (permalink / raw)
  To: git

This commit replaces spawning `git ls-files` child process with
API calls to get the untracked files.
---
 builtin/stash--helper.c | 49 +++++++++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 17 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 4fd79532c..5c27f5dcf 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -813,27 +813,42 @@ static int store_stash(int argc, const char **argv, const char *prefix)
 /*
  * `out` will be filled with the names of untracked files. The return value is:
  *
- * < 0 if there was a bug (any arg given outside the repo will be detected
- *     by `setup_revision()`)
  * = 0 if there are not any untracked files
  * > 0 if there are untracked files
  */
-static int get_untracked_files(const char **argv, int line_term,
+static int get_untracked_files(const char **argv, const char *prefix,
 			       int include_untracked, struct strbuf *out)
 {
-	struct child_process cp = CHILD_PROCESS_INIT;
-	cp.git_cmd = 1;
-	argv_array_pushl(&cp.args, "ls-files", "-o", NULL);
-	if (line_term)
-		argv_array_push(&cp.args, "-z");
+	int max_len;
+	int i;
+	char *seen;
+	struct dir_struct dir;
+	struct pathspec pathspec;
+
+	memset(&dir, 0, sizeof(dir));
 	if (include_untracked != 2)
-		argv_array_push(&cp.args, "--exclude-standard");
-	argv_array_push(&cp.args, "--");
-	if (argv)
-		argv_array_pushv(&cp.args, argv);
+		setup_standard_excludes(&dir);
 
-	if (pipe_command(&cp, NULL, 0, out, 0, NULL, 0))
-		return -1;
+	parse_pathspec(&pathspec, 0,
+		       PATHSPEC_PREFER_FULL,
+		       prefix, argv);
+	seen = xcalloc(pathspec.nr, 1);
+
+	max_len = fill_directory(&dir, the_repository->index, &pathspec);
+	for (i = 0; i < dir.nr; i++) {
+		struct dir_entry *ent = dir.entries[i];
+		if (!dir_path_match(ent, &pathspec, max_len, seen)) {
+			free(ent);
+			continue;
+		}
+		strbuf_addf(out, "%s\n", ent->name);
+		free(ent);
+	}
+
+	free(dir.entries);
+	free(dir.ignored);
+	clear_directory(&dir);
+	free(seen);
 	return out->len;
 }
 
@@ -888,7 +903,7 @@ static int check_changes(const char **argv, int include_untracked,
 		goto done;
 	}
 
-	if (include_untracked && get_untracked_files(argv, 0,
+	if (include_untracked && get_untracked_files(argv, prefix,
 						     include_untracked, &out))
 		ret = 1;
 
@@ -908,7 +923,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
 	struct child_process cp2 = CHILD_PROCESS_INIT;
 
 	cp.git_cmd = 1;
-	argv_array_pushl(&cp.args, "update-index", "-z", "--add",
+	argv_array_pushl(&cp.args, "update-index", "--add",
 			 "--remove", "--stdin", NULL);
 	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
 			 stash_index_path.buf);
@@ -1134,7 +1149,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 		goto done;
 	}
 
-	if (include_untracked && get_untracked_files(argv, 1,
+	if (include_untracked && get_untracked_files(argv, prefix,
 						     include_untracked, &out)) {
 		if (save_untracked_files(info, &msg, &out)) {
 			if (!quiet)
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 22/26] stash: convert save to builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (20 preceding siblings ...)
  2018-08-08 18:59     ` [GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process Paul-Sebastian Ungureanu
@ 2018-08-08 18:59     ` Paul-Sebastian Ungureanu
  2018-08-18 16:33       ` Thomas Gummerer
  2018-08-08 18:59     ` [GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c` Paul-Sebastian Ungureanu
                       ` (4 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:59 UTC (permalink / raw)
  To: git

Add stash save to the helper and delete functions which are no
longer needed (`show_help()`, `save_stash()`, `push_stash()`,
`create_stash()`, `clear_stash()`, `untracked_files()` and
`no_changes()`).
---
 builtin/stash--helper.c |  48 +++++++
 git-stash.sh            | 311 +---------------------------------------
 2 files changed, 50 insertions(+), 309 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 5c27f5dcf..f54a476e3 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -26,6 +26,8 @@ static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
 	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
 	   "          [--] [<pathspec>...]]"),
+	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
 	NULL
 };
 
@@ -81,6 +83,12 @@ static const char * const git_stash_helper_push_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_save_usage[] = {
+	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -1445,6 +1453,44 @@ static int push_stash(int argc, const char **argv, const char *prefix)
 			     include_untracked, quiet, stash_msg);
 }
 
+static int save_stash(int argc, const char **argv, const char *prefix)
+{
+	int i;
+	int keep_index = -1;
+	int patch_mode = 0;
+	int include_untracked = 0;
+	int quiet = 0;
+	char *stash_msg = NULL;
+	struct strbuf alt_stash_msg = STRBUF_INIT;
+	struct option options[] = {
+		OPT_SET_INT('k', "keep-index", &keep_index,
+			N_("keep index"), 1),
+		OPT_BOOL('p', "patch", &patch_mode,
+			N_("stash in patch mode")),
+		OPT_BOOL('q', "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()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_save_usage,
+			     0);
+
+	for (i = 0; i < argc; ++i)
+		strbuf_addf(&alt_stash_msg, "%s ", argv[i]);
+
+	stash_msg = strbuf_detach(&alt_stash_msg, NULL);
+
+	return do_push_stash(0, NULL, prefix, keep_index, patch_mode,
+			     include_untracked, quiet, stash_msg);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -1485,6 +1531,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!create_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "push"))
 		return !!push_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "save"))
+		return !!save_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index c3146f62a..695f1feba 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -36,314 +36,6 @@ else
        reset_color=
 fi
 
-no_changes () {
-	git diff-index --quiet --cached HEAD --ignore-submodules -- "$@" &&
-	git diff-files --quiet --ignore-submodules -- "$@" &&
-	(test -z "$untracked" || test -z "$(untracked_files "$@")")
-}
-
-untracked_files () {
-	if test "$1" = "-z"
-	then
-		shift
-		z=-z
-	else
-		z=
-	fi
-	excl_opt=--exclude-standard
-	test "$untracked" = "all" && excl_opt=
-	git ls-files -o $z $excl_opt -- "$@"
-}
-
-clear_stash () {
-	if test $# != 0
-	then
-		die "$(gettext "git stash clear with parameters is unimplemented")"
-	fi
-	if current=$(git rev-parse --verify --quiet $ref_stash)
-	then
-		git update-ref -d $ref_stash $current
-	fi
-}
-
-create_stash () {
-	stash_msg=
-	untracked=
-	while test $# != 0
-	do
-		case "$1" in
-		-m|--message)
-			shift
-			stash_msg=${1?"BUG: create_stash () -m requires an argument"}
-			;;
-		-m*)
-			stash_msg=${1#-m}
-			;;
-		--message=*)
-			stash_msg=${1#--message=}
-			;;
-		-u|--include-untracked)
-			shift
-			untracked=${1?"BUG: create_stash () -u requires an argument"}
-			;;
-		--)
-			shift
-			break
-			;;
-		esac
-		shift
-	done
-
-	git update-index -q --refresh
-	if no_changes "$@"
-	then
-		exit 0
-	fi
-
-	# state of the base commit
-	if b_commit=$(git rev-parse --verify HEAD)
-	then
-		head=$(git rev-list --oneline -n 1 HEAD --)
-	else
-		die "$(gettext "You do not have the initial commit yet")"
-	fi
-
-	if branch=$(git symbolic-ref -q HEAD)
-	then
-		branch=${branch#refs/heads/}
-	else
-		branch='(no branch)'
-	fi
-	msg=$(printf '%s: %s' "$branch" "$head")
-
-	# state of the index
-	i_tree=$(git write-tree) &&
-	i_commit=$(printf 'index on %s\n' "$msg" |
-		git commit-tree $i_tree -p $b_commit) ||
-		die "$(gettext "Cannot save the current index state")"
-
-	if test -n "$untracked"
-	then
-		# Untracked files are stored by themselves in a parentless commit, for
-		# ease of unpacking later.
-		u_commit=$(
-			untracked_files -z "$@" | (
-				GIT_INDEX_FILE="$TMPindex" &&
-				export GIT_INDEX_FILE &&
-				rm -f "$TMPindex" &&
-				git update-index -z --add --remove --stdin &&
-				u_tree=$(git write-tree) &&
-				printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree  &&
-				rm -f "$TMPindex"
-		) ) || die "$(gettext "Cannot save the untracked files")"
-
-		untracked_commit_option="-p $u_commit";
-	else
-		untracked_commit_option=
-	fi
-
-	if test -z "$patch_mode"
-	then
-
-		# state of the working tree
-		w_tree=$( (
-			git read-tree --index-output="$TMPindex" -m $i_tree &&
-			GIT_INDEX_FILE="$TMPindex" &&
-			export GIT_INDEX_FILE &&
-			git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" &&
-			git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
-			git write-tree &&
-			rm -f "$TMPindex"
-		) ) ||
-			die "$(gettext "Cannot save the current worktree state")"
-
-	else
-
-		rm -f "$TMP-index" &&
-		GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
-
-		# find out what the user wants
-		GIT_INDEX_FILE="$TMP-index" \
-			git add--interactive --patch=stash -- "$@" &&
-
-		# state of the working tree
-		w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
-		die "$(gettext "Cannot save the current worktree state")"
-
-		git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
-		test -s "$TMP-patch" ||
-		die "$(gettext "No changes selected")"
-
-		rm -f "$TMP-index" ||
-		die "$(gettext "Cannot remove temporary index (can't happen)")"
-
-	fi
-
-	# create the stash
-	if test -z "$stash_msg"
-	then
-		stash_msg=$(printf 'WIP on %s' "$msg")
-	else
-		stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
-	fi
-	w_commit=$(printf '%s\n' "$stash_msg" |
-	git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
-	die "$(gettext "Cannot record working tree state")"
-}
-
-push_stash () {
-	keep_index=
-	patch_mode=
-	untracked=
-	stash_msg=
-	while test $# != 0
-	do
-		case "$1" in
-		-k|--keep-index)
-			keep_index=t
-			;;
-		--no-keep-index)
-			keep_index=n
-			;;
-		-p|--patch)
-			patch_mode=t
-			# only default to keep if we don't already have an override
-			test -z "$keep_index" && keep_index=t
-			;;
-		-q|--quiet)
-			GIT_QUIET=t
-			;;
-		-u|--include-untracked)
-			untracked=untracked
-			;;
-		-a|--all)
-			untracked=all
-			;;
-		-m|--message)
-			shift
-			test -z ${1+x} && usage
-			stash_msg=$1
-			;;
-		-m*)
-			stash_msg=${1#-m}
-			;;
-		--message=*)
-			stash_msg=${1#--message=}
-			;;
-		--help)
-			show_help
-			;;
-		--)
-			shift
-			break
-			;;
-		-*)
-			option="$1"
-			eval_gettextln "error: unknown option for 'stash push': \$option"
-			usage
-			;;
-		*)
-			break
-			;;
-		esac
-		shift
-	done
-
-	eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
-
-	if test -n "$patch_mode" && test -n "$untracked"
-	then
-		die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"
-	fi
-
-	test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1
-
-	git update-index -q --refresh
-	if no_changes "$@"
-	then
-		say "$(gettext "No local changes to save")"
-		exit 0
-	fi
-
-	git reflog exists $ref_stash ||
-		clear_stash || die "$(gettext "Cannot initialize stash")"
-
-	create_stash -m "$stash_msg" -u "$untracked" -- "$@"
-	git stash--helper store -m "$stash_msg" -q $w_commit ||
-	die "$(gettext "Cannot save the current status")"
-	say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
-
-	if test -z "$patch_mode"
-	then
-		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
-		if test -n "$untracked" && test $# = 0
-		then
-			git clean --force --quiet -d $CLEAN_X_OPTION
-		fi
-
-		if test $# != 0
-		then
-			test -z "$untracked" && UPDATE_OPTION="-u" || UPDATE_OPTION=
-			test "$untracked" = "all" && FORCE_OPTION="--force" || FORCE_OPTION=
-			git add $UPDATE_OPTION $FORCE_OPTION -- "$@"
-			git diff-index -p --cached --binary HEAD -- "$@" |
-			git apply --index -R
-		else
-			git reset --hard -q
-		fi
-
-		if test "$keep_index" = "t" && test -n "$i_tree"
-		then
-			git read-tree --reset $i_tree
-			git ls-files -z --modified -- "$@" |
-			git checkout-index -z --force --stdin
-		fi
-	else
-		git apply -R < "$TMP-patch" ||
-		die "$(gettext "Cannot remove worktree changes")"
-
-		if test "$keep_index" != "t"
-		then
-			git reset -q -- "$@"
-		fi
-	fi
-}
-
-save_stash () {
-	push_options=
-	while test $# != 0
-	do
-		case "$1" in
-		--)
-			shift
-			break
-			;;
-		-*)
-			# pass all options through to push_stash
-			push_options="$push_options $1"
-			;;
-		*)
-			break
-			;;
-		esac
-		shift
-	done
-
-	stash_msg="$*"
-
-	if test -z "$stash_msg"
-	then
-		push_stash $push_options
-	else
-		push_stash $push_options -m "$stash_msg"
-	fi
-}
-
-show_help () {
-	exec git help stash
-	exit 1
-}
-
 #
 # Parses the remaining options looking for flags and
 # at most one revision defaulting to ${ref_stash}@{0}
@@ -408,7 +100,8 @@ show)
 	;;
 save)
 	shift
-	save_stash "$@"
+	cd "$START_DIR"
+	git stash--helper save "$@"
 	;;
 push)
 	shift
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c`
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (21 preceding siblings ...)
  2018-08-08 18:59     ` [GSoC][PATCH v7 22/26] stash: convert save to builtin Paul-Sebastian Ungureanu
@ 2018-08-08 18:59     ` Paul-Sebastian Ungureanu
  2018-08-18 16:51       ` Thomas Gummerer
  2018-08-08 18:59     ` [GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()` Paul-Sebastian Ungureanu
                       ` (3 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:59 UTC (permalink / raw)
  To: git

The old shell script `git-stash.sh`  was removed and replaced
entirely by `builtin/stash.c`. In order to do that, `create` and
`push` were adapted to work without `stash.sh`. For example, before
this commit, `git stash create` called `git stash--helper create
--message "$*"`. If it called `git stash--helper create "$@"`, then
some of these changes wouldn't have been necessary.

This commit also removes the word `helper` since now stash is
called directly and not by a shell script.
---
 .gitignore                           |   1 -
 Makefile                             |   3 +-
 builtin.h                            |   2 +-
 builtin/{stash--helper.c => stash.c} | 132 ++++++++++++-----------
 git-stash.sh                         | 153 ---------------------------
 git.c                                |   2 +-
 6 files changed, 74 insertions(+), 219 deletions(-)
 rename builtin/{stash--helper.c => stash.c} (91%)
 delete mode 100755 git-stash.sh

diff --git a/.gitignore b/.gitignore
index 3abbba81a..3284a1e9b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -156,7 +156,6 @@
 /git-show-ref
 /git-stage
 /git-stash
-/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
diff --git a/Makefile b/Makefile
index af82bc7ee..c41e31ad8 100644
--- a/Makefile
+++ b/Makefile
@@ -612,7 +612,6 @@ SCRIPT_SH += git-quiltimport.sh
 SCRIPT_SH += git-rebase.sh
 SCRIPT_SH += git-remote-testgit.sh
 SCRIPT_SH += git-request-pull.sh
-SCRIPT_SH += git-stash.sh
 SCRIPT_SH += git-submodule.sh
 SCRIPT_SH += git-web--browse.sh
 
@@ -1083,7 +1082,7 @@ BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-index.o
 BUILTIN_OBJS += builtin/show-ref.o
-BUILTIN_OBJS += builtin/stash--helper.o
+BUILTIN_OBJS += builtin/stash.o
 BUILTIN_OBJS += builtin/stripspace.o
 BUILTIN_OBJS += builtin/submodule--helper.o
 BUILTIN_OBJS += builtin/symbolic-ref.o
diff --git a/builtin.h b/builtin.h
index 55ceb1574..e8102d825 100644
--- a/builtin.h
+++ b/builtin.h
@@ -222,7 +222,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
 extern int cmd_show_index(int argc, const char **argv, const char *prefix);
 extern int cmd_status(int argc, const char **argv, const char *prefix);
-extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
+extern int cmd_stash(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
diff --git a/builtin/stash--helper.c b/builtin/stash.c
similarity index 91%
rename from builtin/stash--helper.c
rename to builtin/stash.c
index f54a476e3..0ef88408a 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash.c
@@ -14,77 +14,77 @@
 #include "log-tree.h"
 #include "diffcore.h"
 
-static const char * const git_stash_helper_usage[] = {
-	N_("git stash--helper list [<options>]"),
-	N_("git stash--helper show [<options>] [<stash>]"),
-	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
-	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
-	N_("git stash--helper branch <branchname> [<stash>]"),
-	N_("git stash--helper clear"),
-	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
-	N_("git stash--helper create [<message>]"),
-	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+static const char * const git_stash_usage[] = {
+	N_("git stash list [<options>]"),
+	N_("git stash show [<options>] [<stash>]"),
+	N_("git stash drop [-q|--quiet] [<stash>]"),
+	N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash branch <branchname> [<stash>]"),
+	N_("git stash clear"),
+	N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
+	N_("git stash create [<message>]"),
+	N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
 	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
 	   "          [--] [<pathspec>...]]"),
-	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
 	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_list_usage[] = {
-	N_("git stash--helper list [<options>]"),
+static const char * const git_stash_list_usage[] = {
+	N_("git stash list [<options>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_show_usage[] = {
-	N_("git stash--helper show [<options>] [<stash>]"),
+static const char * const git_stash_show_usage[] = {
+	N_("git stash show [<options>] [<stash>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_drop_usage[] = {
-	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
+static const char * const git_stash_drop_usage[] = {
+	N_("git stash drop [-q|--quiet] [<stash>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_pop_usage[] = {
-	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
+static const char * const git_stash_pop_usage[] = {
+	N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_apply_usage[] = {
-	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+static const char * const git_stash_apply_usage[] = {
+	N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_branch_usage[] = {
-	N_("git stash--helper branch <branchname> [<stash>]"),
+static const char * const git_stash_branch_usage[] = {
+	N_("git stash branch <branchname> [<stash>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_clear_usage[] = {
-	N_("git stash--helper clear"),
+static const char * const git_stash_clear_usage[] = {
+	N_("git stash clear"),
 	NULL
 };
 
-static const char * const git_stash_helper_store_usage[] = {
-	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
+static const char * const git_stash_store_usage[] = {
+	N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
 	NULL
 };
 
-static const char * const git_stash_helper_create_usage[] = {
-	N_("git stash--helper create [<message>]"),
+static const char * const git_stash_create_usage[] = {
+	N_("git stash create [<message>]"),
 	NULL
 };
 
-static const char * const git_stash_helper_push_usage[] = {
-	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+static const char * const git_stash_push_usage[] = {
+	N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
 	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
 	   "          [--] [<pathspec>...]]"),
 	NULL
 };
 
-static const char * const git_stash_helper_save_usage[] = {
-	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+static const char * const git_stash_save_usage[] = {
+	N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
 	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
 	NULL
 };
@@ -224,11 +224,11 @@ static int clear_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_clear_usage,
+			     git_stash_clear_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 
 	if (argc != 0)
-		return error(_("git stash--helper clear with parameters is unimplemented"));
+		return error(_("git stash clear with parameters is unimplemented"));
 
 	return do_clear_stash();
 }
@@ -519,7 +519,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_apply_usage, 0);
+			     git_stash_apply_usage, 0);
 
 	if (get_stash_info(&info, argc, argv))
 		return -1;
@@ -591,7 +591,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_drop_usage, 0);
+			     git_stash_drop_usage, 0);
 
 	if (get_stash_info(&info, argc, argv))
 		return -1;
@@ -615,7 +615,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_pop_usage, 0);
+			     git_stash_pop_usage, 0);
 
 	if (get_stash_info(&info, argc, argv))
 		return -1;
@@ -641,7 +641,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_branch_usage, 0);
+			     git_stash_branch_usage, 0);
 
 	if (argc == 0)
 		return error(_("No branch name specified"));
@@ -674,7 +674,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_list_usage,
+			     git_stash_list_usage,
 			     PARSE_OPT_KEEP_UNKNOWN);
 
 	if (!ref_exists(ref_stash))
@@ -762,7 +762,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
 	if (argc > 1) {
 		free_stash_info(&info);
 		argv_array_clear(&stash_args);
-		usage_with_options(git_stash_helper_show_usage, options);
+		usage_with_options(git_stash_show_usage, options);
 	}
 
 	/* Do the diff thing. */
@@ -781,7 +781,7 @@ static int do_store_stash(const char *w_commit, const char *stash_msg,
 	struct object_id obj;
 
 	if (!stash_msg)
-		stash_msg  = xstrdup("Created via \"git stash--helper store\".");
+		stash_msg  = xstrdup("Created via \"git stash store\".");
 
 	ret = get_oid(w_commit, &obj);
 	if (!ret) {
@@ -807,11 +807,11 @@ static int store_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_store_usage,
+			     git_stash_store_usage,
 			     PARSE_OPT_KEEP_UNKNOWN);
 
 	if (argc != 1) {
-		fprintf(stderr, _("\"git stash--helper store\" requires one <commit> argument\n"));
+		fprintf(stderr, _("\"git stash store\" requires one <commit> argument\n"));
 		return -1;
 	}
 
@@ -1220,23 +1220,26 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 
 static int create_stash(int argc, const char **argv, const char *prefix)
 {
+	int i;
 	int include_untracked = 0;
 	int ret = 0;
-	const char *stash_msg = NULL;
 	struct stash_info info;
+	struct strbuf stash_msg_buf = STRBUF_INIT;
+	const char *stash_msg;
 	struct option options[] = {
-		OPT_BOOL('u', "include-untracked", &include_untracked,
-			 N_("include untracked files in stash")),
-		OPT_STRING('m', "message", &stash_msg, N_("message"),
-			 N_("stash message")),
 		OPT_END()
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_create_usage,
+			     git_stash_create_usage,
 			     0);
 
-	ret = do_create_stash(argc, argv, prefix, &stash_msg,
+	for (i = 0; i < argc; ++i)
+		strbuf_addf(&stash_msg_buf, "%s ", argv[i]);
+
+	stash_msg = strbuf_detach(&stash_msg_buf, NULL);
+
+	ret = do_create_stash(0, NULL, prefix, &stash_msg,
 			      include_untracked, 0, &info, 0);
 
 	if (!ret)
@@ -1445,9 +1448,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_push_usage,
-			     0);
+	if (argc)
+		argc = parse_options(argc, argv, prefix, options,
+				     git_stash_push_usage,
+				     0);
 
 	return do_push_stash(argc, argv, prefix, keep_index, patch_mode,
 			     include_untracked, quiet, stash_msg);
@@ -1479,7 +1483,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_stash_helper_save_usage,
+			     git_stash_save_usage,
 			     0);
 
 	for (i = 0; i < argc; ++i)
@@ -1491,7 +1495,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
 			     include_untracked, quiet, stash_msg);
 }
 
-int cmd_stash__helper(int argc, const char **argv, const char *prefix)
+int cmd_stash(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
 	const char *index_file;
@@ -1502,16 +1506,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 
 	git_config(git_default_config, NULL);
 
-	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
+	argc = parse_options(argc, argv, prefix, options, git_stash_usage,
 			     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
 
 	index_file = get_index_file();
 	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
 		    (uintmax_t)pid);
 
-	if (argc < 1)
-		usage_with_options(git_stash_helper_usage, options);
-	if (!strcmp(argv[0], "apply"))
+	if (argc == 0)
+		return !!push_stash(0, NULL, prefix);
+	else if (!strcmp(argv[0], "apply"))
 		return !!apply_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "clear"))
 		return !!clear_stash(argc, argv, prefix);
@@ -1533,7 +1537,13 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!push_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "save"))
 		return !!save_stash(argc, argv, prefix);
+	if (*argv[0] == '-') {
+		struct argv_array args = ARGV_ARRAY_INIT;
+		argv_array_push(&args, "push");
+		argv_array_pushv(&args, argv);
+		return !!push_stash(args.argc, args.argv, prefix);
+	}
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
-		      git_stash_helper_usage, options);
+		      git_stash_usage, options);
 }
diff --git a/git-stash.sh b/git-stash.sh
deleted file mode 100755
index 695f1feba..000000000
--- a/git-stash.sh
+++ /dev/null
@@ -1,153 +0,0 @@
-#!/bin/sh
-# Copyright (c) 2007, Nanako Shiraishi
-
-dashless=$(basename "$0" | sed -e 's/-/ /')
-USAGE="list [<options>]
-   or: $dashless show [<stash>]
-   or: $dashless drop [-q|--quiet] [<stash>]
-   or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
-   or: $dashless branch <branchname> [<stash>]
-   or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
-		      [-u|--include-untracked] [-a|--all] [<message>]
-   or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]
-		       [-u|--include-untracked] [-a|--all] [-m <message>]
-		       [-- <pathspec>...]]
-   or: $dashless clear"
-
-SUBDIRECTORY_OK=Yes
-OPTIONS_SPEC=
-START_DIR=$(pwd)
-. git-sh-setup
-require_work_tree
-prefix=$(git rev-parse --show-prefix) || exit 1
-cd_to_toplevel
-
-TMP="$GIT_DIR/.git-stash.$$"
-TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$
-trap 'rm -f "$TMP-"* "$TMPindex"' 0
-
-ref_stash=refs/stash
-
-if git config --get-colorbool color.interactive; then
-       help_color="$(git config --get-color color.interactive.help 'red bold')"
-       reset_color="$(git config --get-color '' reset)"
-else
-       help_color=
-       reset_color=
-fi
-
-#
-# Parses the remaining options looking for flags and
-# at most one revision defaulting to ${ref_stash}@{0}
-# if none found.
-#
-# Derives related tree and commit objects from the
-# revision, if one is found.
-#
-# stash records the work tree, and is a merge between the
-# base commit (first parent) and the index tree (second parent).
-#
-#   REV is set to the symbolic version of the specified stash-like commit
-#   IS_STASH_LIKE is non-blank if ${REV} looks like a stash
-#   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
-#   s is set to the SHA1 of the stash commit
-#   w_commit is set to the commit containing the working tree
-#   b_commit is set to the base commit
-#   i_commit is set to the commit containing the index tree
-#   u_commit is set to the commit containing the untracked files tree
-#   w_tree is set to the working tree
-#   b_tree is set to the base tree
-#   i_tree is set to the index tree
-#   u_tree is set to the untracked files tree
-#
-#   GIT_QUIET is set to t if -q is specified
-#   INDEX_OPTION is set to --index if --index is specified.
-#   FLAGS is set to the remaining flags (if allowed)
-#
-# dies if:
-#   * too many revisions specified
-#   * no revision is specified and there is no stash stack
-#   * a revision is specified which cannot be resolve to a SHA1
-#   * a non-existent stash reference is specified
-#   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
-#
-
-test "$1" = "-p" && set "push" "$@"
-
-PARSE_CACHE='--not-parsed'
-# The default command is "push" if nothing but options are given
-seen_non_option=
-for opt
-do
-	case "$opt" in
-	--) break ;;
-	-*) ;;
-	*) seen_non_option=t; break ;;
-	esac
-done
-
-test -n "$seen_non_option" || set "push" "$@"
-
-# Main command set
-case "$1" in
-list)
-	shift
-	git stash--helper list "$@"
-	;;
-show)
-	shift
-	git stash--helper show "$@"
-	;;
-save)
-	shift
-	cd "$START_DIR"
-	git stash--helper save "$@"
-	;;
-push)
-	shift
-	cd "$START_DIR"
-	git stash--helper push "$@"
-	;;
-apply)
-	shift
-	cd "$START_DIR"
-	git stash--helper apply "$@"
-	;;
-clear)
-	shift
-	git stash--helper clear "$@"
-	;;
-create)
-	shift
-	git stash--helper create --message "$*"
-	;;
-store)
-	shift
-	git stash--helper store "$@"
-	;;
-drop)
-	shift
-	git stash--helper drop "$@"
-	;;
-pop)
-	shift
-	cd "$START_DIR"
-	git stash--helper pop "$@"
-	;;
-branch)
-	shift
-	cd "$START_DIR"
-	git stash--helper branch "$@"
-	;;
-*)
-	case $# in
-	0)
-		cd "$START_DIR"
-		git stash--helper push &&
-		say "$(gettext "(To restore them type \"git stash apply\")")"
-		;;
-	*)
-		usage
-	esac
-	;;
-esac
diff --git a/git.c b/git.c
index a3c83f57e..add13c823 100644
--- a/git.c
+++ b/git.c
@@ -543,7 +543,7 @@ static struct cmd_struct commands[] = {
 	{ "show-index", cmd_show_index },
 	{ "show-ref", cmd_show_ref, RUN_SETUP },
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
-	{ "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
+	{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
 	{ "stripspace", cmd_stripspace },
 	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()`
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (22 preceding siblings ...)
  2018-08-08 18:59     ` [GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c` Paul-Sebastian Ungureanu
@ 2018-08-08 18:59     ` Paul-Sebastian Ungureanu
  2018-08-18 22:33       ` Thomas Gummerer
  2018-08-08 18:59     ` [GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls Paul-Sebastian Ungureanu
                       ` (2 subsequent siblings)
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:59 UTC (permalink / raw)
  To: git

This commits introduces a optimization by avoiding calling the
same functions again. For example, `git stash push -u`
would call at some points the following functions:

 * `check_changes()`
 * `do_create_stash()`, which calls: `check_changes()` and
`get_untracked_files()`

Note that `check_changes()` also calls `get_untracked_files()`.
So, `check_changes()` is called 2 times and `get_untracked_files()`
3 times. By checking at the beginning of the function if we already
performed a check, we can avoid making useless calls.
---
 builtin/stash.c | 50 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 14 deletions(-)

diff --git a/builtin/stash.c b/builtin/stash.c
index 0ef88408a..4d5c0d16e 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -819,13 +819,23 @@ static int store_stash(int argc, const char **argv, const char *prefix)
 }
 
 /*
- * `out` will be filled with the names of untracked files. The return value is:
+ * `has_untracked_files` is:
+ * -2 if `get_untracked_files()` hasn't been called
+ * -1 if there were errors
+ *  0 if there are no untracked files
+ *  1 if there are untracked files
+ *
+ * `untracked_files` will be filled with the names of untracked files.
+ * The return value is:
  *
  * = 0 if there are not any untracked files
  * > 0 if there are untracked files
  */
+static struct strbuf untracked_files = STRBUF_INIT;
+static int has_untracked_files = -2;
+
 static int get_untracked_files(const char **argv, const char *prefix,
-			       int include_untracked, struct strbuf *out)
+			       int include_untracked)
 {
 	int max_len;
 	int i;
@@ -833,6 +843,9 @@ static int get_untracked_files(const char **argv, const char *prefix,
 	struct dir_struct dir;
 	struct pathspec pathspec;
 
+	if (has_untracked_files != -2)
+		return has_untracked_files;
+
 	memset(&dir, 0, sizeof(dir));
 	if (include_untracked != 2)
 		setup_standard_excludes(&dir);
@@ -849,7 +862,7 @@ static int get_untracked_files(const char **argv, const char *prefix,
 			free(ent);
 			continue;
 		}
-		strbuf_addf(out, "%s\n", ent->name);
+		strbuf_addf(&untracked_files, "%s\n", ent->name);
 		free(ent);
 	}
 
@@ -857,16 +870,25 @@ static int get_untracked_files(const char **argv, const char *prefix,
 	free(dir.ignored);
 	clear_directory(&dir);
 	free(seen);
-	return out->len;
+	has_untracked_files = untracked_files.len;
+	return untracked_files.len;
 }
 
 /*
+ * `changes` is:
+ * -2 if `check_changes()` hasn't been called
+ * -1 if there were any errors
+ *  0 if there are no changes
+ *  1 if there are changes
+ *
  * The return value of `check_changes()` can be:
  *
  * < 0 if there was an error
  * = 0 if there are no changes.
  * > 0 if there are changes.
  */
+static int changes = -2;
+
 static int check_changes(const char **argv, int include_untracked,
 			 const char *prefix)
 {
@@ -874,9 +896,11 @@ static int check_changes(const char **argv, int include_untracked,
 	int ret = 0;
 	struct rev_info rev;
 	struct object_id dummy;
-	struct strbuf out = STRBUF_INIT;
 	struct argv_array args = ARGV_ARRAY_INIT;
 
+	if (changes != -2)
+		return changes;
+
 	init_revisions(&rev, prefix);
 	parse_pathspec(&rev.prune_data, 0, PATHSPEC_PREFER_FULL,
 		       prefix, argv);
@@ -912,17 +936,16 @@ static int check_changes(const char **argv, int include_untracked,
 	}
 
 	if (include_untracked && get_untracked_files(argv, prefix,
-						     include_untracked, &out))
+						     include_untracked))
 		ret = 1;
 
 done:
-	strbuf_release(&out);
+	changes = ret;
 	argv_array_clear(&args);
 	return ret;
 }
 
-static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
-				struct strbuf *out)
+static int save_untracked_files(struct stash_info *info, struct strbuf *msg)
 {
 	int ret = 0;
 	struct strbuf untracked_msg = STRBUF_INIT;
@@ -937,7 +960,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
 			 stash_index_path.buf);
 
 	strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
-	if (pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0)) {
+	if (pipe_command(&cp, untracked_files.buf, untracked_files.len,
+			 NULL, 0, NULL, 0)) {
 		ret = -1;
 		goto done;
 	}
@@ -1116,7 +1140,6 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 	struct commit_list *parents = NULL;
 	struct strbuf msg = STRBUF_INIT;
 	struct strbuf commit_tree_label = STRBUF_INIT;
-	struct strbuf out = STRBUF_INIT;
 	struct strbuf final_stash_msg = STRBUF_INIT;
 
 	read_cache_preload(NULL);
@@ -1158,8 +1181,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 	}
 
 	if (include_untracked && get_untracked_files(argv, prefix,
-						     include_untracked, &out)) {
-		if (save_untracked_files(info, &msg, &out)) {
+						     include_untracked)) {
+		if (save_untracked_files(info, &msg)) {
 			if (!quiet)
 				printf_ln("Cannot save the untracked files");
 			ret = -1;
@@ -1213,7 +1236,6 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
 done:
 	strbuf_release(&commit_tree_label);
 	strbuf_release(&msg);
-	strbuf_release(&out);
 	strbuf_release(&final_stash_msg);
 	return ret;
 }
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (23 preceding siblings ...)
  2018-08-08 18:59     ` [GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()` Paul-Sebastian Ungureanu
@ 2018-08-08 18:59     ` Paul-Sebastian Ungureanu
  2018-08-19  8:17       ` Thomas Gummerer
  2018-08-08 18:59     ` [GSoC][PATCH v7 26/26] stash: replace all "git apply" " Paul-Sebastian Ungureanu
  2018-08-15 22:25     ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Thomas Gummerer
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:59 UTC (permalink / raw)
  To: git

This commit replaces spawning `git write-tree` with API calls.
---
 builtin/stash.c | 40 ++++++++++++----------------------------
 1 file changed, 12 insertions(+), 28 deletions(-)

diff --git a/builtin/stash.c b/builtin/stash.c
index 4d5c0d16e..46e76a34e 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -949,9 +949,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)
 {
 	int ret = 0;
 	struct strbuf untracked_msg = STRBUF_INIT;
-	struct strbuf out2 = STRBUF_INIT;
 	struct child_process cp = CHILD_PROCESS_INIT;
-	struct child_process cp2 = CHILD_PROCESS_INIT;
+	struct index_state state = { NULL };
 
 	cp.git_cmd = 1;
 	argv_array_pushl(&cp.args, "update-index", "--add",
@@ -966,15 +965,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)
 		goto done;
 	}
 
-	cp2.git_cmd = 1;
-	argv_array_push(&cp2.args, "write-tree");
-	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
-			 stash_index_path.buf);
-	if (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {
+	if (write_index_as_tree(&info->u_tree, &state, stash_index_path.buf, 0,
+				NULL)) {
 		ret = -1;
 		goto done;
 	}
-	get_oid_hex(out2.buf, &info->u_tree);
 
 	if (commit_tree(untracked_msg.buf, untracked_msg.len,
 			&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
@@ -984,7 +979,6 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)
 
 done:
 	strbuf_release(&untracked_msg);
-	strbuf_release(&out2);
 	remove_path(stash_index_path.buf);
 	return ret;
 }
@@ -994,11 +988,10 @@ static struct strbuf patch = STRBUF_INIT;
 static int stash_patch(struct stash_info *info, const char **argv)
 {
 	int ret = 0;
-	struct strbuf out2 = STRBUF_INIT;
 	struct child_process cp0 = CHILD_PROCESS_INIT;
 	struct child_process cp1 = CHILD_PROCESS_INIT;
-	struct child_process cp2 = CHILD_PROCESS_INIT;
 	struct child_process cp3 = CHILD_PROCESS_INIT;
+	struct index_state state = { NULL };
 
 	remove_path(stash_index_path.buf);
 
@@ -1023,17 +1016,12 @@ static int stash_patch(struct stash_info *info, const char **argv)
 		goto done;
 	}
 
-	cp2.git_cmd = 1;
-	argv_array_push(&cp2.args, "write-tree");
-	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
-			 stash_index_path.buf);
-	if (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {
+	if (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,
+				NULL)) {
 		ret = -1;
 		goto done;
 	}
 
-	get_oid_hex(out2.buf, &info->w_tree);
-
 	cp3.git_cmd = 1;
 	argv_array_pushl(&cp3.args, "diff-tree", "-p", "HEAD",
 			 oid_to_hex(&info->w_tree), "--", NULL);
@@ -1046,7 +1034,6 @@ static int stash_patch(struct stash_info *info, const char **argv)
 	}
 
 done:
-	strbuf_release(&out2);
 	remove_path(stash_index_path.buf);
 	return ret;
 }
@@ -1056,11 +1043,10 @@ static int stash_working_tree(struct stash_info *info,
 {
 	int ret = 0;
 	struct child_process cp2 = CHILD_PROCESS_INIT;
-	struct child_process cp3 = CHILD_PROCESS_INIT;
-	struct strbuf out3 = STRBUF_INIT;
 	struct argv_array args = ARGV_ARRAY_INIT;
 	struct strbuf diff_output = STRBUF_INIT;
 	struct rev_info rev;
+	struct index_state state = { NULL };
 
 	set_alternate_index_output(stash_index_path.buf);
 	if (reset_tree(&info->i_tree, 0, 0)) {
@@ -1103,20 +1089,18 @@ static int stash_working_tree(struct stash_info *info,
 		goto done;
 	}
 
-	cp3.git_cmd = 1;
-	argv_array_push(&cp3.args, "write-tree");
-	argv_array_pushf(&cp3.env_array, "GIT_INDEX_FILE=%s",
-			 stash_index_path.buf);
-	if (pipe_command(&cp3, NULL, 0, &out3, 0,NULL, 0)) {
+	if (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,
+				NULL)) {
+
 		ret = -1;
 		goto done;
 	}
 
-	get_oid_hex(out3.buf, &info->w_tree);
+	discard_cache();
+	read_cache();
 
 done:
 	UNLEAK(rev);
-	strbuf_release(&out3);
 	argv_array_clear(&args);
 	object_array_clear(&rev.pending);
 	strbuf_release(&diff_output);
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v7 26/26] stash: replace all "git apply" child processes with API calls
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (24 preceding siblings ...)
  2018-08-08 18:59     ` [GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls Paul-Sebastian Ungureanu
@ 2018-08-08 18:59     ` Paul-Sebastian Ungureanu
  2018-08-19  8:40       ` Thomas Gummerer
  2018-08-15 22:25     ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Thomas Gummerer
  26 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-08 18:59 UTC (permalink / raw)
  To: git

`apply_all_patches()` does not provide a method to apply patches
from strbuf. Because of this, this commit introduces a new
function `apply_patch_from_buf()` which applies a patch from buf.
It works by saving the strbuf as a file. This way we can call
`apply_all_patches()`. Before returning, the created file is
removed.
---
 builtin/stash.c | 61 +++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 27 deletions(-)

diff --git a/builtin/stash.c b/builtin/stash.c
index 46e76a34e..74eda822c 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -13,6 +13,7 @@
 #include "revision.h"
 #include "log-tree.h"
 #include "diffcore.h"
+#include "apply.h"
 
 static const char * const git_stash_usage[] = {
 	N_("git stash list [<options>]"),
@@ -277,10 +278,6 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
 	struct child_process cp = CHILD_PROCESS_INIT;
 	const char *w_commit_hex = oid_to_hex(w_commit);
 
-	/*
-	 * Diff-tree would not be very hard to replace with a native function,
-	 * however it should be done together with apply_cached.
-	 */
 	cp.git_cmd = 1;
 	argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
 	argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
@@ -288,18 +285,36 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
 	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
 }
 
-static int apply_cached(struct strbuf *out)
+static int apply_patch_from_buf(struct strbuf *patch, int cached, int reverse,
+				int check_index)
 {
-	struct child_process cp = CHILD_PROCESS_INIT;
+	int ret = 0;
+	struct apply_state state;
+	struct argv_array args = ARGV_ARRAY_INIT;
+	const char *patch_path = ".git/stash_patch.patch";
+	FILE *patch_file;
 
-	/*
-	 * Apply currently only reads either from stdin or a file, thus
-	 * apply_all_patches would have to be updated to optionally take a
-	 * buffer.
-	 */
-	cp.git_cmd = 1;
-	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
-	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+	if (init_apply_state(&state, NULL))
+		return -1;
+
+	state.cached = cached;
+	state.apply_in_reverse = reverse;
+	state.check_index = check_index;
+	if (state.cached)
+		state.check_index = 1;
+	if (state.check_index)
+		state.unsafe_paths = 0;
+
+	patch_file = fopen(patch_path, "w");
+	strbuf_write(patch, patch_file);
+	fclose(patch_file);
+
+	argv_array_push(&args, patch_path);
+	ret = apply_all_patches(&state, args.argc, args.argv, 0);
+
+	remove_path(patch_path);
+	clear_apply_state(&state);
+	return ret;
 }
 
 static int reset_head(const char *prefix)
@@ -418,7 +433,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
 				return -1;
 			}
 
-			ret = apply_cached(&out);
+			ret = apply_patch_from_buf(&out, 1, 0, 0);
 			strbuf_release(&out);
 			if (ret)
 				return -1;
@@ -1341,7 +1356,6 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
 			int i;
 			struct child_process cp1 = CHILD_PROCESS_INIT;
 			struct child_process cp2 = CHILD_PROCESS_INIT;
-			struct child_process cp3 = CHILD_PROCESS_INIT;
 			struct strbuf out = STRBUF_INIT;
 
 			cp1.git_cmd = 1;
@@ -1365,11 +1379,9 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
 			if (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))
 				return -1;
 
-			cp3.git_cmd = 1;
-			argv_array_pushl(&cp3.args, "apply", "--index", "-R",
-					 NULL);
-			if (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,
-					 0))
+			discard_cache();
+			read_cache();
+			if (apply_patch_from_buf(&out, 0, 1, 1))
 				return -1;
 		} else {
 			struct child_process cp = CHILD_PROCESS_INIT;
@@ -1405,12 +1417,7 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
 				return -1;
 		}
 	} else {
-		struct child_process cp = CHILD_PROCESS_INIT;
-
-		cp.git_cmd = 1;
-		argv_array_pushl(&cp.args, "apply", "-R", NULL);
-
-		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
+		if (apply_patch_from_buf(&patch, 0, 1, 0)) {
 			if (!quiet)
 				fprintf_ln(stderr, "Cannot remove worktree changes");
 			return -1;
-- 
2.18.0.573.g56500d98f


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin
  2018-08-08 18:58     ` [GSoC][PATCH v7 05/26] stash: convert apply to builtin Paul-Sebastian Ungureanu
@ 2018-08-08 20:18       ` Junio C Hamano
  2018-08-09 20:01         ` Paul-Sebastian Ungureanu
  2018-08-18 16:09       ` Duy Nguyen
  1 sibling, 1 reply; 181+ messages in thread
From: Junio C Hamano @ 2018-08-08 20:18 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:

> From: Joel Teichroeb <joel@teichroeb.net>
>
> Add a builtin helper for performing stash commands. Converting
> all at once proved hard to review, so starting with just apply
> lets conversion get started without the other commands being
> finished.
>
> The helper is being implemented as a drop in replacement for
> stash so that when it is complete it can simply be renamed and
> the shell script deleted.
>
> Delete the contents of the apply_stash shell function and replace
> it with a call to stash--helper apply until pop is also
> converted.
>
> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>

Good to see that the right way to forward a patch from another
person is used, but is this a GSoC project?

> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> new file mode 100644
> index 000000000..ef6a9d30d
> --- /dev/null
> +++ b/builtin/stash--helper.c
> @@ -0,0 +1,452 @@
> +#include "builtin.h"
> +#include "config.h"
> +#include "parse-options.h"
> +#include "refs.h"
> +#include "lockfile.h"
> +#include "cache-tree.h"
> +#include "unpack-trees.h"
> +#include "merge-recursive.h"
> +#include "argv-array.h"
> +#include "run-command.h"
> +#include "dir.h"
> +#include "rerere.h"

Wow, "apply" is a biggie, as you'd pretty much have to do
everything, like merging and updating the index and asking rerere to
auto-resolve.  Quite a many include files.

> +static const char * const git_stash_helper_usage[] = {
> +	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
> +	NULL
> +};
> +
> +static const char * const git_stash_helper_apply_usage[] = {
> +	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
> +	NULL
> +};
> + ...
> +static void assert_stash_like(struct stash_info *info, const char * revision)

This inherits an unfortunate name from the scripted version (it does
more than asserting), but it is OK to keep the original name,
especially in this early step in the series.

Lose the SP in "* revision"; the asterisk sticks to the variable/parameter name.

> +{
> +	if (get_oidf(&info->b_commit, "%s^1", revision) ||
> +	    get_oidf(&info->w_tree, "%s:", revision) ||
> +	    get_oidf(&info->b_tree, "%s^1:", revision) ||
> +	    get_oidf(&info->i_tree, "%s^2:", revision)) {
> +		free_stash_info(info);
> +		error(_("'%s' is not a stash-like commit"), revision);
> +		exit(128);
> +	}
> +}

> +static int reset_tree(struct object_id *i_tree, int update, int reset)
> +{
> ...
> +}

Kind-a surprising that there is no helper function to do this
already.  The implementation looked OK, though.

> +static int apply_cached(struct strbuf *out)
> +{
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +
> +	/*
> +	 * Apply currently only reads either from stdin or a file, thus
> +	 * apply_all_patches would have to be updated to optionally take a
> +	 * buffer.
> +	 */
> +	cp.git_cmd = 1;
> +	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
> +	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
> +}

Applying and updating the index is more resource intensive than
spawning a process, and not having to worry about the process dying
is a benefit, so overall, making this into an internal call would be
a lot lower priority, I would guess.

> +static int reset_head(const char *prefix)
> +{

This is resetting the index to the HEAD, right?  reset_head sounds
as if it takes a commit-ish and moves HEAD there.


^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin
  2018-08-08 20:18       ` Junio C Hamano
@ 2018-08-09 20:01         ` Paul-Sebastian Ungureanu
  2018-08-09 21:00           ` Junio C Hamano
  0 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-09 20:01 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Hello,

On 08.08.2018 23:18, Junio C Hamano wrote:
> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:
> 
>> From: Joel Teichroeb <joel@teichroeb.net>
>>
>> Add a builtin helper for performing stash commands. Converting
>> all at once proved hard to review, so starting with just apply
>> lets conversion get started without the other commands being
>> finished.
>>
>> The helper is being implemented as a drop in replacement for
>> stash so that when it is complete it can simply be renamed and
>> the shell script deleted.
>>
>> Delete the contents of the apply_stash shell function and replace
>> it with a call to stash--helper apply until pop is also
>> converted.
>>
>> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
>> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> 
> Good to see that the right way to forward a patch from another
> person is used, but is this a GSoC project?

Yes, it is. I forgot to add the [GSoC] tag in the last series of patches.

>> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
>> new file mode 100644
>> index 000000000..ef6a9d30d
>> --- /dev/null
>> +++ b/builtin/stash--helper.c
>> @@ -0,0 +1,452 @@
>> +#include "builtin.h"
>> +#include "config.h"
>> +#include "parse-options.h"
>> +#include "refs.h"
>> +#include "lockfile.h"
>> +#include "cache-tree.h"
>> +#include "unpack-trees.h"
>> +#include "merge-recursive.h"
>> +#include "argv-array.h"
>> +#include "run-command.h"
>> +#include "dir.h"
>> +#include "rerere.h"
> 
> Wow, "apply" is a biggie, as you'd pretty much have to do
> everything, like merging and updating the index and asking rerere to
> auto-resolve.  Quite a many include files.
> 
>> +static const char * const git_stash_helper_usage[] = {
>> +	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
>> +	NULL
>> +};
>> +
>> +static const char * const git_stash_helper_apply_usage[] = {
>> +	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
>> +	NULL
>> +};
>> + ...
>> +static void assert_stash_like(struct stash_info *info, const char * revision)
> 
> This inherits an unfortunate name from the scripted version (it does
> more than asserting), but it is OK to keep the original name,
> especially in this early step in the series.
> 
> Lose the SP in "* revision"; the asterisk sticks to the variable/parameter name.

Will do so.

>> +{
>> +	if (get_oidf(&info->b_commit, "%s^1", revision) ||
>> +	    get_oidf(&info->w_tree, "%s:", revision) ||
>> +	    get_oidf(&info->b_tree, "%s^1:", revision) ||
>> +	    get_oidf(&info->i_tree, "%s^2:", revision)) {
>> +		free_stash_info(info);
>> +		error(_("'%s' is not a stash-like commit"), revision);
>> +		exit(128);
>> +	}
>> +}
> 
>> +static int reset_tree(struct object_id *i_tree, int update, int reset)
>> +{
>> ...
>> +}
> 
> Kind-a surprising that there is no helper function to do this
> already.  The implementation looked OK, though.
> 
>> +static int apply_cached(struct strbuf *out)
>> +{
>> +	struct child_process cp = CHILD_PROCESS_INIT;
>> +
>> +	/*
>> +	 * Apply currently only reads either from stdin or a file, thus
>> +	 * apply_all_patches would have to be updated to optionally take a
>> +	 * buffer.
>> +	 */
>> +	cp.git_cmd = 1;
>> +	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
>> +	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
>> +}
> 
> Applying and updating the index is more resource intensive than
> spawning a process, and not having to worry about the process dying
> is a benefit, so overall, making this into an internal call would be
> a lot lower priority, I would guess.

Indeed. In the last but one patch [1], I tried to convert all of the 
"apply" processes. At the moment, `apply_all_patches()` cannot take a 
buffer. A solution for this was to write the buffer into a file and pass 
the name of that file to the function. Of course, this might not be a 
bright idea and for this reason I am not sure if that patch is worth.

[1]
https://public-inbox.org/git/56500d98f9d5daaa5f21a43767885baede86e3a0.1533753605.git.ungureanupaulsebastian@gmail.com/

>> +static int reset_head(const char *prefix)
>> +{
> 
> This is resetting the index to the HEAD, right?  reset_head sounds
> as if it takes a commit-ish and moves HEAD there.
> 

Yes, it resets the index to the HEAD.

Best,
Paul

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin
  2018-08-09 20:01         ` Paul-Sebastian Ungureanu
@ 2018-08-09 21:00           ` Junio C Hamano
  2018-08-10 15:35             ` Paul-Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Junio C Hamano @ 2018-08-09 21:00 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:

>> Good to see that the right way to forward a patch from another
>> person is used, but is this a GSoC project?
>
> Yes, it is. I forgot to add the [GSoC] tag in the last series of patches.

The reason I asked was because IIRC GSoC was not supposed to be team
effort but "summer job" by individual students.

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin
  2018-08-09 21:00           ` Junio C Hamano
@ 2018-08-10 15:35             ` Paul-Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-10 15:35 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Hellom

On 10.08.2018 00:00, Junio C Hamano wrote:
> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:
> 
>>> Good to see that the right way to forward a patch from another
>>> person is used, but is this a GSoC project?
>>
>> Yes, it is. I forgot to add the [GSoC] tag in the last series of patches.
> 
> The reason I asked was because IIRC GSoC was not supposed to be team
> effort but "summer job" by individual students.
> 

Before starting working on this, I discussed with Joel and ask
him whether I could take custody of this patches. He was open
to the idea, so I continued to build on top of them. Since then,
I have made some changes to his code and also added a lot more of
my own.

What I did was to completely convert git stash and apply some
more optimizations on top of that, like reducing the number of
spawned processes. Due to this kind of improvements I was able
to significantly reduce the execution time and this has visible
effects, running about 3 times faster.

Please do not get me wrong, I do not want to bash Joel or his work.
I just wanted to make it clear that I did not copy his work or made
it look like it was mine. After Joel agreed to hand the patches off
to me [1], every line of code that I wrote was written by myself, 
without his (or anyone else) assistance. I really did my best to finish 
this project and I believe that my mentor, dscho, can also confirm this. 
I am really sorry for the confusion.

[1]
https://public-inbox.org/git/CA+CzEk_qwHs5qUstyFeepzwvCBR=9SvH90+__f-gfxFySETZzQ@mail.gmail.com/

Best regards,
Paul Ungureanu

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive
  2018-08-08 18:58     ` [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive Paul-Sebastian Ungureanu
@ 2018-08-15 19:31       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 19:31 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

> Subject: Re: [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive

Please use the imperative mood in the title and the commit messages
themselves.  From Documentation/SubmittingPatches:

    Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
    instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
    to do frotz", as if you are giving orders to the codebase to change
    its behavior.

From a quick skim over the rest of the series, this also applies to
some of the subsequent patches in the series. 

On 08/08, Paul-Sebastian Ungureanu wrote:
> Renamed some test cases' labels to be more descriptive and under 80
> characters per line.
> 
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  t/t3903-stash.sh | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index de6cab1fe..8d002a7f2 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
>  	test_cmp expected actual
>  '
>  
> -test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
> +test_expect_success 'drop: fail early if specified stash is not a stash ref' '
>  	git stash clear &&
>  	test_when_finished "git reset --hard HEAD && git stash clear" &&
>  	git reset --hard &&
> @@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r
>  	git reset --hard HEAD
>  '
>  
> -test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
> +test_expect_success 'pop: fail early if specified stash is not a stash ref' '
>  	git stash clear &&
>  	test_when_finished "git reset --hard HEAD && git stash clear" &&
>  	git reset --hard &&
> @@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form "n", n >= N' '
>  	git stash drop
>  '
>  
> -test_expect_success 'stash branch should not drop the stash if the branch exists' '
> +test_expect_success 'branch: should not drop the stash if the branch exists' '

Since we're adjusting the titles of the tests here I'll allow myself
to nitpick a little :)

Maybe "branch: do not drop the stash if the branch exists", which
sounds more like an assertion, as the "pop" and "drop" titles above.

>  	git stash clear &&
>  	echo foo >file &&
>  	git add file &&
> @@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists
>  	git rev-parse stash@{0} --
>  '
>  
> -test_expect_success 'stash branch should not drop the stash if the apply fails' '
> +test_expect_success 'branch: should not drop the stash if the apply fails' '
>  	git stash clear &&
>  	git reset HEAD~1 --hard &&
>  	echo foo >file &&
> @@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'
>  	git rev-parse stash@{0} --
>  '
>  
> -test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
> +test_expect_success 'apply: shows same status as git status (relative to ./)' '

s/shows/show/ above maybe?  This used to be a full sentence
previously, where 'shows' was appropriate, but I think "show" sounds
better after the colon.

>  	git stash clear &&
>  	echo 1 >subdir/subfile1 &&
>  	echo 2 >subdir/subfile2 &&
> @@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '
>  	test_i18ncmp expect actual
>  '
>  
> -test_expect_success 'stash push with pathspec shows no changes when there are none' '
> +test_expect_success 'push: <pathspec> shows no changes when there are none' '

Maybe "push <pathspec>: show no changes when there are none"?  "push
<pathspec>" would be the rest of the 'git stash' command, having the
colon in between them seems a bit odd.

>  	>foo &&
>  	git add foo &&
>  	git commit -m "tmp" &&
> @@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no
>  	test_i18ncmp expect actual
>  '
>  
> -test_expect_success 'stash push with pathspec not in the repository errors out' '
> +test_expect_success 'push: <pathspec> not in the repository errors out' '

This one makes sense to me.

>  	>untracked &&
>  	test_must_fail git stash push untracked &&
>  	test_path_is_file untracked
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin
  2018-08-08 18:58     ` [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin Paul-Sebastian Ungureanu
@ 2018-08-15 19:41       ` Thomas Gummerer
  2018-08-18 11:44         ` Paul Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 19:41 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

> Subject: stash: implement the "list" command in the builtin

Nit: The previous commit messages all have the format "stash: convert
<command> to builtin", maybe follow the same pattern here?

The rest of the patch looks good to me.

On 08/08, Paul-Sebastian Ungureanu wrote:
> Add stash list to the helper and delete the list_stash function
> from the shell script.
> 
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++
>  git-stash.sh            |  7 +------
>  2 files changed, 32 insertions(+), 6 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index d6bd468e0..daa4d0034 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -12,6 +12,7 @@
>  #include "rerere.h"
>  
>  static const char * const git_stash_helper_usage[] = {
> +	N_("git stash--helper list [<options>]"),
>  	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper branch <branchname> [<stash>]"),
> @@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {
>  	NULL
>  };
>  
> +static const char * const git_stash_helper_list_usage[] = {
> +	N_("git stash--helper list [<options>]"),
> +	NULL
> +};
> +
>  static const char * const git_stash_helper_drop_usage[] = {
>  	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
>  	NULL
> @@ -609,6 +615,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> +static int list_stash(int argc, const char **argv, const char *prefix)
> +{
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_list_usage,
> +			     PARSE_OPT_KEEP_UNKNOWN);
> +
> +	if (!ref_exists(ref_stash))
> +		return 0;
> +
> +	cp.git_cmd = 1;
> +	argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
> +			 "--first-parent", "-m", NULL);
> +	argv_array_pushv(&cp.args, argv);
> +	argv_array_push(&cp.args, ref_stash);
> +	argv_array_push(&cp.args, "--");
> +	return run_command(&cp);
> +}
> +
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  {
>  	pid_t pid = getpid();
> @@ -639,6 +668,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!pop_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "branch"))
>  		return !!branch_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "list"))
> +		return !!list_stash(argc, argv, prefix);
>  
>  	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
>  		      git_stash_helper_usage, options);
> diff --git a/git-stash.sh b/git-stash.sh
> index 8f2640fe9..6052441aa 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -382,11 +382,6 @@ have_stash () {
>  	git rev-parse --verify --quiet $ref_stash >/dev/null
>  }
>  
> -list_stash () {
> -	have_stash || return 0
> -	git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
> -}
> -
>  show_stash () {
>  	ALLOW_UNKNOWN_FLAGS=t
>  	assert_stash_like "$@"
> @@ -574,7 +569,7 @@ test -n "$seen_non_option" || set "push" "$@"
>  case "$1" in
>  list)
>  	shift
> -	list_stash "$@"
> +	git stash--helper list "$@"
>  	;;
>  show)
>  	shift
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 10/26] stash: convert show to builtin
  2018-08-08 18:58     ` [GSoC][PATCH v7 10/26] stash: convert show to builtin Paul-Sebastian Ungureanu
@ 2018-08-15 20:20       ` Thomas Gummerer
  2018-08-18 12:11         ` Paul Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 20:20 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Add stash show to the helper and delete the show_stash, have_stash,
> assert_stash_like, is_stash_like and parse_flags_and_rev functions
> from the shell script now that they are no longer needed.
> 
> Before this commit, `git stash show` would ignore `--index` and
> `--quiet` options. Now, `git stash show` errors out on `--index`
> and does not display any message on `--quiet`, but errors out
> if the stash is not empty.

I think "errors out" is slightly misleading here.  Maybe "but exits
with an exit code similar to 'git diff'" instead?

Looking at why we ignored them before, it's because we filtered them
out in 'parse_flags_and_rev', which looks more accidental than
intentional, and I think we could consider a bug, so this change in
behaviour here is okay.

'--quiet' doesn't make too much sense to use with 'git stash show', so
I'm not sure whether or not it makes sense to support it at all.  But
we do promise to pass all options through to in our documentation, so
the new behaviour is what we are documenting.

> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  builtin/stash--helper.c |  78 ++++++++++++++++++++++++
>  git-stash.sh            | 132 +---------------------------------------
>  2 files changed, 79 insertions(+), 131 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index daa4d0034..e764cd33e 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -13,6 +13,7 @@
>  
>  static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper list [<options>]"),
> +	N_("git stash--helper show [<stash>]"),
>  	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper branch <branchname> [<stash>]"),
> @@ -25,6 +26,11 @@ static const char * const git_stash_helper_list_usage[] = {
>  	NULL
>  };
>  
> +static const char * const git_stash_helper_show_usage[] = {
> +	N_("git stash--helper show [<stash>]"),
> +	NULL
> +};
> +
>  static const char * const git_stash_helper_drop_usage[] = {
>  	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
>  	NULL
> @@ -638,6 +644,76 @@ static int list_stash(int argc, const char **argv, const char *prefix)
>  	return run_command(&cp);
>  }
>  
> +static int show_stat = 1;
> +static int show_patch;
> +
> +static int git_stash_config(const char *var, const char *value, void *cb)
> +{
> +	if (!strcmp(var, "stash.showStat")) {
> +		show_stat = git_config_bool(var, value);
> +		return 0;
> +	}
> +	if (!strcmp(var, "stash.showPatch")) {
> +		show_patch = git_config_bool(var, value);
> +		return 0;
> +	}
> +	return git_default_config(var, value, cb);
> +}
> +
> +static int show_stash(int argc, const char **argv, const char *prefix)
> +{
> +	int i, ret = 0;
> +	struct child_process cp = CHILD_PROCESS_INIT;
> +	struct argv_array args_refs = ARGV_ARRAY_INIT;
> +	struct stash_info info;
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_show_usage,
> +			     PARSE_OPT_KEEP_UNKNOWN);
> +
> +	cp.git_cmd = 1;
> +	argv_array_push(&cp.args, "diff");
> +
> +	/* Push arguments which are not options into args_refs. */
> +	for (i = 0; i < argc; ++i) {
> +		if (argv[i][0] == '-')
> +			argv_array_push(&cp.args, argv[i]);
> +		else
> +			argv_array_push(&args_refs, argv[i]);
> +	}
> +
> +	if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {
> +		child_process_clear(&cp);
> +		argv_array_clear(&args_refs);
> +		return -1;
> +	}
> +
> +	/*
> +	 * The config settings are applied only if there are not passed
> +	 * any flags.
> +	 */
> +	if (cp.args.argc == 1) {
> +		git_config(git_stash_config, NULL);
> +		if (show_stat)
> +			argv_array_push(&cp.args, "--stat");
> +
> +		if (show_patch)
> +			argv_array_push(&cp.args, "-p");
> +	}
> +
> +	argv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),
> +			 oid_to_hex(&info.w_commit), NULL);
> +
> +	ret = run_command(&cp);
> +
> +	free_stash_info(&info);
> +	argv_array_clear(&args_refs);
> +	return ret;
> +}
> +
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  {
>  	pid_t pid = getpid();
> @@ -670,6 +746,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!branch_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "list"))
>  		return !!list_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "show"))
> +		return !!show_stash(argc, argv, prefix);
>  
>  	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
>  		      git_stash_helper_usage, options);
> diff --git a/git-stash.sh b/git-stash.sh
> index 6052441aa..0d05cbc1e 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -378,35 +378,6 @@ save_stash () {
>  	fi
>  }
>  
> -have_stash () {
> -	git rev-parse --verify --quiet $ref_stash >/dev/null
> -}
> -
> -show_stash () {
> -	ALLOW_UNKNOWN_FLAGS=t
> -	assert_stash_like "$@"
> -
> -	if test -z "$FLAGS"
> -	then
> -		if test "$(git config --bool stash.showStat || echo true)" = "true"
> -		then
> -			FLAGS=--stat
> -		fi
> -
> -		if test "$(git config --bool stash.showPatch || echo false)" = "true"
> -		then
> -			FLAGS=${FLAGS}${FLAGS:+ }-p
> -		fi
> -
> -		if test -z "$FLAGS"
> -		then
> -			return 0
> -		fi
> -	fi
> -
> -	git diff ${FLAGS} $b_commit $w_commit
> -}
> -
>  show_help () {
>  	exec git help stash
>  	exit 1
> @@ -448,107 +419,6 @@ show_help () {
>  #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
>  #
>  
> -parse_flags_and_rev()
> -{
> -	test "$PARSE_CACHE" = "$*" && return 0 # optimisation
> -	PARSE_CACHE="$*"
> -
> -	IS_STASH_LIKE=
> -	IS_STASH_REF=
> -	INDEX_OPTION=
> -	s=
> -	w_commit=
> -	b_commit=
> -	i_commit=
> -	u_commit=
> -	w_tree=
> -	b_tree=
> -	i_tree=
> -	u_tree=
> -
> -	FLAGS=
> -	REV=
> -	for opt
> -	do
> -		case "$opt" in
> -			-q|--quiet)
> -				GIT_QUIET=-t
> -			;;
> -			--index)
> -				INDEX_OPTION=--index
> -			;;
> -			--help)
> -				show_help
> -			;;
> -			-*)
> -				test "$ALLOW_UNKNOWN_FLAGS" = t ||
> -					die "$(eval_gettext "unknown option: \$opt")"
> -				FLAGS="${FLAGS}${FLAGS:+ }$opt"
> -			;;
> -			*)
> -				REV="${REV}${REV:+ }'$opt'"
> -			;;
> -		esac
> -	done
> -
> -	eval set -- $REV
> -
> -	case $# in
> -		0)
> -			have_stash || die "$(gettext "No stash entries found.")"
> -			set -- ${ref_stash}@{0}
> -		;;
> -		1)
> -			:
> -		;;
> -		*)
> -			die "$(eval_gettext "Too many revisions specified: \$REV")"
> -		;;
> -	esac
> -
> -	case "$1" in
> -		*[!0-9]*)
> -			:
> -		;;
> -		*)
> -			set -- "${ref_stash}@{$1}"
> -		;;
> -	esac
> -
> -	REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
> -		reference="$1"
> -		die "$(eval_gettext "\$reference is not a valid reference")"
> -	}
> -
> -	i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
> -	set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
> -	s=$1 &&
> -	w_commit=$1 &&
> -	b_commit=$2 &&
> -	w_tree=$3 &&
> -	b_tree=$4 &&
> -	i_tree=$5 &&
> -	IS_STASH_LIKE=t &&
> -	test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
> -	IS_STASH_REF=t
> -
> -	u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
> -	u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
> -}
> -
> -is_stash_like()
> -{
> -	parse_flags_and_rev "$@"
> -	test -n "$IS_STASH_LIKE"
> -}
> -
> -assert_stash_like() {
> -	is_stash_like "$@" || {
> -		args="$*"
> -		die "$(eval_gettext "'\$args' is not a stash-like commit")"
> -	}
> -}
> -
>  test "$1" = "-p" && set "push" "$@"
>  
>  PARSE_CACHE='--not-parsed'
> @@ -573,7 +443,7 @@ list)
>  	;;
>  show)
>  	shift
> -	show_stash "$@"
> +	git stash--helper show "$@"
>  	;;
>  save)
>  	shift
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation
  2018-08-08 18:58     ` [GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation Paul-Sebastian Ungureanu
@ 2018-08-15 20:26       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 20:26 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

> Subject: stash: change `git stash show` usage text and documentation

Another nitpick about commit messages.  "change ... usage text and
documentation" doesn't say much about what the actual change is.
How about something like "stash: mention options in "show" synopsis"
instead?

The change itself looks good to me, thanks!

On 08/08, Paul-Sebastian Ungureanu wrote:
> It is already stated in documentation that it will accept any
> option known to `git diff`, but not in the usage text and some
> parts of the documentation.
> 
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  Documentation/git-stash.txt | 4 ++--
>  builtin/stash--helper.c     | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
> index 7ef8c4791..e31ea7d30 100644
> --- a/Documentation/git-stash.txt
> +++ b/Documentation/git-stash.txt
> @@ -9,7 +9,7 @@ SYNOPSIS
>  --------
>  [verse]
>  'git stash' list [<options>]
> -'git stash' show [<stash>]
> +'git stash' show [<options>] [<stash>]
>  'git stash' drop [-q|--quiet] [<stash>]
>  'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
>  'git stash' branch <branchname> [<stash>]
> @@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash
>  The command takes options applicable to the 'git log'
>  command to control what is shown and how. See linkgit:git-log[1].
>  
> -show [<stash>]::
> +show [<options>] [<stash>]::
>  
>  	Show the changes recorded in the stash entry as a diff between the
>  	stashed contents and the commit back when the stash entry was first
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index e764cd33e..0c1efca6b 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -13,7 +13,7 @@
>  
>  static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper list [<options>]"),
> -	N_("git stash--helper show [<stash>]"),
> +	N_("git stash--helper show [<options>] [<stash>]"),
>  	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper branch <branchname> [<stash>]"),
> @@ -27,7 +27,7 @@ static const char * const git_stash_helper_list_usage[] = {
>  };
>  
>  static const char * const git_stash_helper_show_usage[] = {
> -	N_("git stash--helper show [<stash>]"),
> +	N_("git stash--helper show [<options>] [<stash>]"),
>  	NULL
>  };
>  
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API
  2018-08-08 18:58     ` [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API Paul-Sebastian Ungureanu
@ 2018-08-15 21:01       ` Thomas Gummerer
  2018-08-18 15:11         ` Paul Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 21:01 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Currently, `show_stash()` uses `cmd_diff()` to generate
> the output. After this commit, the output will be generated
> using the internal API.
> 
> Before this commit, `git stash show --quiet` would act like
> `git diff` and error out if the stash is not empty. Now, the
> `--quiet` option does not error out given an empty stash.

I think this needs a bit more justification.  As I mentioned in my
comment to a previous patch, I'm not sure '--quiet' makes much sense
with 'git stash show' (it will show nothing, and will always exit with
an error code, as the stash will always contain something), but if we
are supporting the same flags as 'git diff', and essentially just
forwarding them, shouldn't they keep the same behaviour as well?

> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------
>  1 file changed, 45 insertions(+), 28 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 0c1efca6b..ec8c38c6f 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -10,6 +10,8 @@
>  #include "run-command.h"
>  #include "dir.h"
>  #include "rerere.h"
> +#include "revision.h"
> +#include "log-tree.h"
>  
>  static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper list [<options>]"),
> @@ -662,56 +664,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)
>  
>  static int show_stash(int argc, const char **argv, const char *prefix)
>  {
> -	int i, ret = 0;
> -	struct child_process cp = CHILD_PROCESS_INIT;
> -	struct argv_array args_refs = ARGV_ARRAY_INIT;
> +	int i;
> +	int flags = 0;
>  	struct stash_info info;
> +	struct rev_info rev;
> +	struct argv_array stash_args = ARGV_ARRAY_INIT;
>  	struct option options[] = {
>  		OPT_END()
>  	};
>  
> -	argc = parse_options(argc, argv, prefix, options,
> -			     git_stash_helper_show_usage,
> -			     PARSE_OPT_KEEP_UNKNOWN);
> +	init_diff_ui_defaults();
> +	git_config(git_diff_ui_config, NULL);
>  
> -	cp.git_cmd = 1;
> -	argv_array_push(&cp.args, "diff");
> +	init_revisions(&rev, prefix);
>  
> -	/* Push arguments which are not options into args_refs. */
> -	for (i = 0; i < argc; ++i) {
> -		if (argv[i][0] == '-')
> -			argv_array_push(&cp.args, argv[i]);
> +	/* Push arguments which are not options into stash_args. */
> +	for (i = 1; i < argc; ++i) {
> +		if (argv[i][0] != '-')
> +			argv_array_push(&stash_args, argv[i]);
>  		else
> -			argv_array_push(&args_refs, argv[i]);
> -	}
> -
> -	if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {
> -		child_process_clear(&cp);
> -		argv_array_clear(&args_refs);
> -		return -1;
> +			flags++;
>  	}
>  
>  	/*
>  	 * The config settings are applied only if there are not passed
>  	 * any flags.
>  	 */
> -	if (cp.args.argc == 1) {
> +	if (!flags) {
>  		git_config(git_stash_config, NULL);
>  		if (show_stat)
> -			argv_array_push(&cp.args, "--stat");
> +			rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;
> +		if (show_patch) {
> +			rev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;
> +			rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
> +		}

I failed to notice this in the previous patch (the problem existed
there as well), but this changes the behaviour of 'git -c
stash.showStat=false stash show <stash>'.  Previously doing this would
not show anything, which is the correct behaviour, while now still
shows the diffstat.

I think the show_stat variable is interpreted the wrong way around in
the previous patch.

Something else I noticed now that I was playing around more with the
config options is that the parsing of the config options is not
correctly done in the previous patch.  It does a 'strcmp(var,
"stash.showStat"))', but the config API makes all variables lowercase
(config options are case insensitive, and making everything lowercase
is the way to ensure that), so it should be 'strcmp(var, "stash.showstat"))', 
and similar for the 'stash.showpatch' config option.

This all sounds like it would be nice to have some tests for these
config options, to make sure we get it right, and won't break them in
the future.

> +	}
>  
> -		if (show_patch)
> -			argv_array_push(&cp.args, "-p");
> +	if (get_stash_info(&info, stash_args.argc, stash_args.argv)) {
> +		argv_array_clear(&stash_args);
> +		return -1;
>  	}
>  
> -	argv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),
> -			 oid_to_hex(&info.w_commit), NULL);
> +	argc = setup_revisions(argc, argv, &rev, NULL);
> +	if (!rev.diffopt.output_format)
> +		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
> +	diff_setup_done(&rev.diffopt);
> +	rev.diffopt.flags.recursive = 1;
> +	setup_diff_pager(&rev.diffopt);
>  
> -	ret = run_command(&cp);
> +	/*
> +	 * We can return early if there was any option not recognised by
> +	 * `diff_opt_parse()`, besides the word `stash`.
> +	 */

I'm not sure I follow what you mean with this comment.  I don't see
any 'diff_opt_parse' in the code above?  

> +	if (argc > 1) {
> +		free_stash_info(&info);
> +		argv_array_clear(&stash_args);
> +		usage_with_options(git_stash_helper_show_usage, options);
> +	}
> +
> +	/* Do the diff thing. */

This comment should be dropped.  It's obvious that we are doing the
diff thing below from the code, so the comment is redundant at best,
makes people read more lines, and could possibly get outdated.

I'd also drop the other comments above, as they do not provide a lot
of extra value imho.

> +	diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
> +	log_tree_diff_flush(&rev);
>  
>  	free_stash_info(&info);
> -	argv_array_clear(&args_refs);
> -	return ret;
> +	argv_array_clear(&stash_args);
> +	return 0;
>  }
>  
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 13/26] stash: update `git stash show` documentation
  2018-08-08 18:58     ` [GSoC][PATCH v7 13/26] stash: update `git stash show` documentation Paul-Sebastian Ungureanu
@ 2018-08-15 21:08       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 21:08 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Add in documentation about the change of behavior regarding
> the `--quiet` option, which was introduced in the last commit.
> (the `--quiet` option does not exit anymore with erorr if it

s/erorr/error/

> is given an empty stash as argument)

If we want to keep the change in behaviour here (which I'm not sure
about as mentioned in my comment on the previous patch), I think this
should be folded into the previous patch.  I don't think there's much
value in having this as a separate commit, and folding it into the
previous commit has the advantage that we can easily see that the new
behaviour is documented.

> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  Documentation/git-stash.txt | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
> index e31ea7d30..d60ebdb96 100644
> --- a/Documentation/git-stash.txt
> +++ b/Documentation/git-stash.txt
> @@ -117,6 +117,9 @@ show [<options>] [<stash>]::
>  	You can use stash.showStat and/or stash.showPatch config variables
>  	to change the default behavior.
>  
> +	It accepts any option known to `git diff`, but acts different on

I notice that we are using single quotes for git commands in some
places and backticks in other places in this man page.  We may want to
clean that up at some point.  I wouldn't want to do it in this series
though, as this is already long enough, and we've had this
inconsistency for a while already.

> +	`--quiet` option and exit with zero regardless of differences.
> +
>  pop [--index] [-q|--quiet] [<stash>]::
>  
>  	Remove a single stashed state from the stash list and apply it
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 14/26] stash: convert store to builtin
  2018-08-08 18:58     ` [GSoC][PATCH v7 14/26] stash: convert store to builtin Paul-Sebastian Ungureanu
@ 2018-08-15 21:26       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 21:26 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Add stash store to the helper and delete the store_stash function
> from the shell script.
> 
> Add the usage string which was forgotten in the shell script.

I think similarly to 'git stash create', which also doesn't appear in
the usage, this was intentionally omitted in the shell script.  The
reason for the omission is that this is only intended to be useful in
scripts, and not in interactive usage.  As such it doesn't add much
value in showing it in 'git stash -h'.  Meanwhile it is in the
synopsis in the man page.

If we want to add it to the help output, I think it would be best to
do so in a separate commit, and for 'git stash create' as well.  But
I'm not sure that's a good change.

> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  builtin/stash--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++
>  git-stash.sh            | 43 ++--------------------------------
>  2 files changed, 54 insertions(+), 41 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index ec8c38c6f..5ff810f8c 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -20,6 +20,7 @@ static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
>  	N_("git stash--helper branch <branchname> [<stash>]"),
>  	N_("git stash--helper clear"),
> +	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
>  	NULL
>  };
>  
> @@ -58,6 +59,11 @@ static const char * const git_stash_helper_clear_usage[] = {
>  	NULL
>  };
>  
> +static const char * const git_stash_helper_store_usage[] = {
> +	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
> +	NULL
> +};
> +
>  static const char *ref_stash = "refs/stash";
>  static int quiet;
>  static struct strbuf stash_index_path = STRBUF_INIT;
> @@ -731,6 +737,50 @@ static int show_stash(int argc, const char **argv, const char *prefix)
>  	return 0;
>  }
>  
> +static int do_store_stash(const char *w_commit, const char *stash_msg,
> +			  int quiet)
> +{
> +	int ret = 0;
> +	struct object_id obj;
> +
> +	if (!stash_msg)
> +		stash_msg  = xstrdup("Created via \"git stash--helper store\".");

I assume we're going to s/--helper// in a later commit?  Not sure
adding the '--helper' here is necessary, as a user would never invoke
'git stash--helper' directly, so they would expect the stash to be
created by 'git stash store'.  Anyway that's fairly minor, as I assume
this is going to change by the end of the patch series.

> +
> +	ret = get_oid(w_commit, &obj);
> +	if (!ret) {
> +		ret = update_ref(stash_msg, ref_stash, &obj, NULL,
> +				 REF_FORCE_CREATE_REFLOG,
> +				 quiet ? UPDATE_REFS_QUIET_ON_ERR :
> +				 UPDATE_REFS_MSG_ON_ERR);
> +	}
> +	if (ret && !quiet)
> +		fprintf_ln(stderr, _("Cannot update %s with %s"),
> +			   ref_stash, w_commit);
> +
> +	return ret;
> +}
> +
> +static int store_stash(int argc, const char **argv, const char *prefix)
> +{
> +	const char *stash_msg = NULL;
> +	struct option options[] = {
> +		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
> +		OPT_STRING('m', "message", &stash_msg, "message", N_("stash message")),
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_store_usage,
> +			     PARSE_OPT_KEEP_UNKNOWN);
> +
> +	if (argc != 1) {
> +		fprintf(stderr, _("\"git stash--helper store\" requires one <commit> argument\n"));
> +		return -1;
> +	}
> +
> +	return do_store_stash(argv[0], stash_msg, quiet);
> +}
> +
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  {
>  	pid_t pid = getpid();
> @@ -765,6 +815,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!list_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "show"))
>  		return !!show_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "store"))
> +		return !!store_stash(argc, argv, prefix);
>  
>  	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
>  		      git_stash_helper_usage, options);
> diff --git a/git-stash.sh b/git-stash.sh
> index 0d05cbc1e..5739c5152 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -191,45 +191,6 @@ create_stash () {
>  	die "$(gettext "Cannot record working tree state")"
>  }
>  
> -store_stash () {
> -	while test $# != 0
> -	do
> -		case "$1" in
> -		-m|--message)
> -			shift
> -			stash_msg="$1"
> -			;;
> -		-m*)
> -			stash_msg=${1#-m}
> -			;;
> -		--message=*)
> -			stash_msg=${1#--message=}
> -			;;
> -		-q|--quiet)
> -			quiet=t
> -			;;
> -		*)
> -			break
> -			;;
> -		esac
> -		shift
> -	done
> -	test $# = 1 ||
> -	die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
> -
> -	w_commit="$1"
> -	if test -z "$stash_msg"
> -	then
> -		stash_msg="Created via \"git stash store\"."
> -	fi
> -
> -	git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
> -	ret=$?
> -	test $ret != 0 && test -z "$quiet" &&
> -	die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
> -	return $ret
> -}
> -
>  push_stash () {
>  	keep_index=
>  	patch_mode=
> @@ -308,7 +269,7 @@ push_stash () {
>  		clear_stash || die "$(gettext "Cannot initialize stash")"
>  
>  	create_stash -m "$stash_msg" -u "$untracked" -- "$@"
> -	store_stash -m "$stash_msg" -q $w_commit ||
> +	git stash--helper store -m "$stash_msg" -q $w_commit ||
>  	die "$(gettext "Cannot save the current status")"
>  	say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
>  
> @@ -468,7 +429,7 @@ create)
>  	;;
>  store)
>  	shift
> -	store_stash "$@"
> +	git stash--helper store "$@"
>  	;;
>  drop)
>  	shift
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin
  2018-08-08 18:58     ` [GSoC][PATCH v7 15/26] stash: convert create " Paul-Sebastian Ungureanu
@ 2018-08-15 22:13       ` Thomas Gummerer
  2018-08-18 15:39         ` Paul Sebastian Ungureanu
  0 siblings, 1 reply; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 22:13 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Add stash create to the helper.
> 
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
> ---
>  builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++
>  git-stash.sh            |   2 +-
>  2 files changed, 407 insertions(+), 1 deletion(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 5ff810f8c..a4e57899b 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -21,6 +21,7 @@ static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper branch <branchname> [<stash>]"),
>  	N_("git stash--helper clear"),
>  	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
> +	N_("git stash--helper create [<message>]"),
>  	NULL
>  };
>  
> @@ -64,6 +65,11 @@ static const char * const git_stash_helper_store_usage[] = {
>  	NULL
>  };
>  
> +static const char * const git_stash_helper_create_usage[] = {
> +	N_("git stash--helper create [<message>]"),
> +	NULL
> +};
> +
>  static const char *ref_stash = "refs/stash";
>  static int quiet;
>  static struct strbuf stash_index_path = STRBUF_INIT;
> @@ -781,6 +787,404 @@ static int store_stash(int argc, const char **argv, const char *prefix)
>  	return do_store_stash(argv[0], stash_msg, quiet);
>  }
>
> [...]
> 
> +
> +static int do_create_stash(int argc, const char **argv, const char *prefix,
> +			   const char **stash_msg, int include_untracked,
> +			   int patch_mode, struct stash_info *info)
> +{
> +	int untracked_commit_option = 0;
> +	int ret = 0;
> +	int subject_len;
> +	int flags;
> +	const char *head_short_sha1 = NULL;
> +	const char *branch_ref = NULL;
> +	const char *head_subject = NULL;
> +	const char *branch_name = "(no branch)";
> +	struct commit *head_commit = NULL;
> +	struct commit_list *parents = NULL;
> +	struct strbuf msg = STRBUF_INIT;
> +	struct strbuf commit_tree_label = STRBUF_INIT;
> +	struct strbuf out = STRBUF_INIT;
> +	struct strbuf final_stash_msg = STRBUF_INIT;
> +
> +	read_cache_preload(NULL);
> +	refresh_cache(REFRESH_QUIET);
> +
> +	if (!check_changes(argv, include_untracked, prefix)) {
> +		ret = 1;
> +		goto done;

I wonder if we can just 'exit(0)' here, instead of returning.  This
whole command is a builtin, and I *think* outside of 'libgit.a' exiting
early is fine.  It does mean that we're not free'ing the memory
though, which means a leak checker would probably complain.  So
dunno.  It would simplify the code a little, but not sure it's worth it.

> +	}
> +
> +	if (get_oid("HEAD", &info->b_commit)) {
> +		fprintf_ln(stderr, "You do not have the initial commit yet");
> +		ret = -1;
> +		goto done;
> +	} else {
> +		head_commit = lookup_commit(the_repository, &info->b_commit);
> +	}
> +
> +	branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
> +	if (flags & REF_ISSYMREF)
> +		branch_name = strrchr(branch_ref, '/') + 1;
> +	head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
> +					     DEFAULT_ABBREV);
> +	subject_len = find_commit_subject(get_commit_buffer(head_commit, NULL),
> +					  &head_subject);
> +	strbuf_addf(&msg, "%s: %s %.*s\n", branch_name, head_short_sha1,
> +		    subject_len, head_subject);

I think this can be written in a slightly simpler way:

	head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
					     DEFAULT_ABBREV);
	strbuf_addf(&msg, "%s: %s", branch_name, head_short_sha1);
	pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
	strbuf_addch(&msg, '\n');

The other advantage this brings is that it is consistent with other
places where we print/use the subject of a commit (e.g. in 'git reset
--hard').

> +
> +	strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
> +	commit_list_insert(head_commit, &parents);
> +	if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
> +	    commit_tree(commit_tree_label.buf, commit_tree_label.len,
> +			&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
> +		fprintf_ln(stderr, "Cannot save the current index state");

Looks like this message is translated in the current 'git stash'
implementation, so it should be here as well.  Same for the messages
below.

> +		ret = -1;
> +		goto done;
> +	}
> +
> +	if (include_untracked && get_untracked_files(argv, 1,
> +						     include_untracked, &out)) {
> +		if (save_untracked_files(info, &msg, &out)) {
> +			printf_ln("Cannot save the untracked files");

Why does this go to stdout, whereas "Cannot save the current index
state" above goes to stderr?  In the shell version of git stash these
all go to stderr fwiw.  There are a few similar cases, it would
probably be worth going through all the print statements here and see
if they need to be translated, and to which output stream they should
go.

> +			ret = -1;
> +			goto done;
> +		}
> +		untracked_commit_option = 1;
> +	}
> +	if (patch_mode) {
> +		ret = stash_patch(info, argv);
> +		if (ret < 0) {
> +			printf_ln("Cannot save the current worktree state");
> +			goto done;
> +		} else if (ret > 0) {
> +			goto done;
> +		}
> +	} else {
> +		if (stash_working_tree(info, argv)) {
> +			printf_ln("Cannot save the current worktree state");
> +			ret = -1;
> +			goto done;
> +		}
> +	}
> +
> +	if (!*stash_msg || !strlen(*stash_msg))
> +		strbuf_addf(&final_stash_msg, "WIP on %s", msg.buf);
> +	else
> +		strbuf_addf(&final_stash_msg, "On %s: %s\n", branch_name,
> +			    *stash_msg);
> +	*stash_msg = strbuf_detach(&final_stash_msg, NULL);

strbuf_detach means we're taking ownership of the memory, so we'll
have to free it afterwards. Looking at this we may not even want to
re-use the 'stash_msg' variable here, but instead introduce another
variable for it, so it doesn't have a dual meaning in this function.

> +
> +	/*
> +	 * `parents` will be empty after calling `commit_tree()`, so there is
> +	 * no need to call `free_commit_list()`
> +	 */
> +	parents = NULL;
> +	if (untracked_commit_option)
> +		commit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);
> +	commit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);
> +	commit_list_insert(head_commit, &parents);
> +
> +	if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
> +			parents, &info->w_commit, NULL, NULL)) {
> +		printf_ln("Cannot record working tree state");
> +		ret = -1;
> +		goto done;
> +	}
> +
> +done:
> +	strbuf_release(&commit_tree_label);
> +	strbuf_release(&msg);
> +	strbuf_release(&out);
> +	strbuf_release(&final_stash_msg);
> +	return ret;
> +}
> +
> +static int create_stash(int argc, const char **argv, const char *prefix)
> +{
> +	int include_untracked = 0;
> +	int ret = 0;
> +	const char *stash_msg = NULL;
> +	struct stash_info info;
> +	struct option options[] = {
> +		OPT_BOOL('u', "include-untracked", &include_untracked,
> +			 N_("include untracked files in stash")),
> +		OPT_STRING('m', "message", &stash_msg, N_("message"),
> +			 N_("stash message")),
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_create_usage,
> +			     0);
> +
> +	ret = do_create_stash(argc, argv, prefix, &stash_msg,
> +			      include_untracked, 0, &info);

stash_msg doesn't have to be passed as a pointer to a pointer here, as
we never need the modified value after this function returns.  I think
just passing 'stash_msg' here instead of '&stash_msg' will make
'do_create_stash' slightly easier to read.

> +
> +	if (!ret)
> +		printf_ln("%s", oid_to_hex(&info.w_commit));
> +
> +	/*
> +	 * ret can be 1 if there were no changes. In this case, we should
> +	 * not error out.
> +	 */
> +	return ret < 0;
> +}
> +
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  {
>  	pid_t pid = getpid();
> @@ -817,6 +1221,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!show_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "store"))
>  		return !!store_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "create"))
> +		return !!create_stash(argc, argv, prefix);
>  
>  	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
>  		      git_stash_helper_usage, options);
> diff --git a/git-stash.sh b/git-stash.sh
> index 5739c5152..ab06e4ffb 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -425,7 +425,7 @@ clear)
>  	;;
>  create)
>  	shift
> -	create_stash -m "$*" && echo "$w_commit"
> +	git stash--helper create --message "$*"
>  	;;
>  store)
>  	shift
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
                       ` (25 preceding siblings ...)
  2018-08-08 18:59     ` [GSoC][PATCH v7 26/26] stash: replace all "git apply" " Paul-Sebastian Ungureanu
@ 2018-08-15 22:25     ` Thomas Gummerer
  2018-08-16 21:25       ` Paul Sebastian Ungureanu
  26 siblings, 1 reply; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-15 22:25 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Hello,
> 
> Here is the whole `git stash` C version. Some of the previous
> patches were already reviewed (up to and including "stash: convert
> store to builtin"), but there are some which were not
> (starting with "stash: convert create to builtin").

Thanks for this new iteration, and sorry I took a while to find some
time to review this.  I had another read through the patches up until
patch 15, and left some comments, before running out of time again.  I
hope to find some time in the next few days to go through the rest of
the series as well.

One more comment in terms of the structure of the series.  The
patches doing the actual conversion from shell to C seem to be
interleaved with cleanup patches and patches that make the C version
use more internal APIs.  I'd suggest putting all the cleanup patches
(e.g. "stash: change `git stash show` usage text and documentation")
to the front of the series, as that's more likely to be
uncontroversial, and could maybe even be merged by itself.

Then I'd put all the conversion from shell to C patches, and only once
everything is converted I'd put the patches to use more of the
internal APIs rather than using run_command everywhere.  A possible
alternative would be to squash the patches to replace the run_command
calls with patches that use the internal API directly, to save the
reviewers some time by reading through less churn.  Though I'm kind of
on the fence with that, as a faithful conversion using 'run_command'
may be easier to review as a first step.

Hope this helps!

> In order to see the difference between the shell version and
> the C version, I ran `time` on:
> 
> * git test suite (t3903-stash.sh, t3904-stash-patch.sh,
> t3905-stash-include-untracked.sh and t3906-stash-submodule.sh)
> 
>         t3903-stash.sh:
>         ** SHELL: 12,69s user 9,95s system 109% cpu 20,730 total
>         **     C:  2,67s user 2,84s system 105% cpu  5,206 total
> 
>         t3904-stash-patch.sh:
>         ** SHELL: 1,43s user 0,94s system 106% cpu 2,242 total
>         **     C: 1,01s user 0,58s system 104% cpu 1,530 total
> 
>         t3905-stash-include-untracked.sh
>         ** SHELL: 2,22s user 1,73s system 110% cpu 3,569 total
>         **     C: 0,59s user 0,57s system 106% cpu 1,085 total
> 
>         t3906-stash-submodule.sh
>         ** SHELL: 2,89s user 2,99s system 106% cpu 5,527 total
>         **     C: 2,21s user 2,61s system 105% cpu 4,568 total
> 
>         TOTAL:
>         ** SHELL: 19.23s user 15.61s system
>         **     C:  6.48s user  6.60s system

Awesome!

> * a git repository with 4000 files: 1000 not changed,
> 1000 staged files, 1000 unstaged files, 1000 untracked.
> In this case I ran some of the most used commands:
> 
>         git stash push:
> 
>         ** SHELL: 0,12s user 0,21s system 101% cpu 0,329 total
>         **     C: 0,06s user 0,13s system 105% cpu 0,185 total
> 
>         git stash push -u:
> 
>         ** SHELL: 0,18s user 0,27s system  108% cpu 0,401 total
>         **     C: 0,09s user 0,19s system  103% cpu 0,267 total
> 
>         git stash pop:
> 
>         ** SHELL: 0,16s user 0,26s system 103% cpu 0,399 total
>         **     C: 0,13s user 0,19s system 102% cpu 0,308 total
> 
> Best regards,
> Paul Ungureanu
> 
> 
> Joel Teichroeb (5):
>   stash: improve option parsing test coverage
>   stash: convert apply to builtin
>   stash: convert drop and clear to builtin
>   stash: convert branch to builtin
>   stash: convert pop to builtin
> 
> Paul-Sebastian Ungureanu (21):
>   sha1-name.c: added 'get_oidf', which acts like 'get_oid'
>   stash: update test cases conform to coding guidelines
>   stash: renamed test cases to be more descriptive
>   stash: implement the "list" command in the builtin
>   stash: convert show to builtin
>   stash: change `git stash show` usage text and documentation
>   stash: refactor `show_stash()` to use the diff API
>   stash: update `git stash show` documentation
>   stash: convert store to builtin
>   stash: convert create to builtin
>   stash: replace spawning a "read-tree" process
>   stash: avoid spawning a "diff-index" process
>   stash: convert push to builtin
>   stash: make push to be quiet
>   stash: add tests for `git stash push -q`
>   stash: replace spawning `git ls-files` child process
>   stash: convert save to builtin
>   stash: convert `stash--helper.c` into `stash.c`
>   stash: optimize `get_untracked_files()` and `check_changes()`
>   stash: replace all `write-tree` child processes with API calls
>   stash: replace all "git apply" child processes with API calls
> 
>  Documentation/git-stash.txt |    7 +-
>  Makefile                    |    2 +-
>  builtin.h                   |    1 +
>  builtin/stash.c             | 1562 +++++++++++++++++++++++++++++++++++
>  cache.h                     |    1 +
>  git-stash.sh                |  752 -----------------
>  git.c                       |    1 +
>  sha1-name.c                 |   19 +
>  t/t3903-stash.sh            |  190 +++--
>  9 files changed, 1714 insertions(+), 821 deletions(-)
>  create mode 100644 builtin/stash.c
>  delete mode 100755 git-stash.sh
> 
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin
  2018-08-15 22:25     ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Thomas Gummerer
@ 2018-08-16 21:25       ` Paul Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul Sebastian Ungureanu @ 2018-08-16 21:25 UTC (permalink / raw)
  To: Thomas Gummerer; +Cc: Git Mailing List

Hi,

On Thu, Aug 16, 2018 at 1:25 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
> On 08/08, Paul-Sebastian Ungureanu wrote:
>> Hello,
>>
>> Here is the whole `git stash` C version. Some of the previous
>> patches were already reviewed (up to and including "stash: convert
>> store to builtin"), but there are some which were not
>> (starting with "stash: convert create to builtin").
>
> Thanks for this new iteration, and sorry I took a while to find some
> time to review this.  I had another read through the patches up until
> patch 15, and left some comments, before running out of time again.  I
> hope to find some time in the next few days to go through the rest of
> the series as well.

Thank you a lot for taking time to review my patches. It really means a lot.

> One more comment in terms of the structure of the series.  The
> patches doing the actual conversion from shell to C seem to be
> interleaved with cleanup patches and patches that make the C version
> use more internal APIs.  I'd suggest putting all the cleanup patches
> (e.g. "stash: change `git stash show` usage text and documentation")
> to the front of the series, as that's more likely to be
> uncontroversial, and could maybe even be merged by itself.

Good point.

> Then I'd put all the conversion from shell to C patches, and only once
> everything is converted I'd put the patches to use more of the
> internal APIs rather than using run_command everywhere.  A possible
> alternative would be to squash the patches to replace the run_command
> calls with patches that use the internal API directly, to save the
> reviewers some time by reading through less churn.  Though I'm kind of
> on the fence with that, as a faithful conversion using 'run_command'
> may be easier to review as a first step.

Makes sense. Actually, as you said, the patches that replace `run_command()`
or `pipe_command()` were not squashed because I thought it might be more
easier for reviewers. The `stash: replace all "git apply" child
processes with API
calls` patch is a exception to the rule because I was highly in doubts
if the patch
would actually be good.

> Hope this helps!

It helps a lot. Thank you!

>> In order to see the difference between the shell version and
>> the C version, I ran `time` on:
>>
>> * git test suite (t3903-stash.sh, t3904-stash-patch.sh,
>> t3905-stash-include-untracked.sh and t3906-stash-submodule.sh)
>>
>>         t3903-stash.sh:
>>         ** SHELL: 12,69s user 9,95s system 109% cpu 20,730 total
>>         **     C:  2,67s user 2,84s system 105% cpu  5,206 total
>>
>>         t3904-stash-patch.sh:
>>         ** SHELL: 1,43s user 0,94s system 106% cpu 2,242 total
>>         **     C: 1,01s user 0,58s system 104% cpu 1,530 total
>>
>>         t3905-stash-include-untracked.sh
>>         ** SHELL: 2,22s user 1,73s system 110% cpu 3,569 total
>>         **     C: 0,59s user 0,57s system 106% cpu 1,085 total
>>
>>         t3906-stash-submodule.sh
>>         ** SHELL: 2,89s user 2,99s system 106% cpu 5,527 total
>>         **     C: 2,21s user 2,61s system 105% cpu 4,568 total
>>
>>         TOTAL:
>>         ** SHELL: 19.23s user 15.61s system
>>         **     C:  6.48s user  6.60s system
>
> Awesome!

I hope that it will get even better.

Best regards,
Paul

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin
  2018-08-15 19:41       ` Thomas Gummerer
@ 2018-08-18 11:44         ` Paul Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul Sebastian Ungureanu @ 2018-08-18 11:44 UTC (permalink / raw)
  To: Thomas Gummerer; +Cc: Git Mailing List

On Wed, Aug 15, 2018 at 10:41 PM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
>> Subject: stash: implement the "list" command in the builtin
>
> Nit: The previous commit messages all have the format "stash: convert
> <command> to builtin", maybe follow the same pattern here?

At first, the subject of the commit matched the pattern. I think I
changed it in order
to avoid any confusion with "list" as data structure. Now, I guess
that "stash:" at the
beginning of the commit message removes any doubt.

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 10/26] stash: convert show to builtin
  2018-08-15 20:20       ` Thomas Gummerer
@ 2018-08-18 12:11         ` Paul Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul Sebastian Ungureanu @ 2018-08-18 12:11 UTC (permalink / raw)
  To: Thomas Gummerer; +Cc: Git Mailing List

On Wed, Aug 15, 2018 at 11:20 PM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
> '--quiet' doesn't make too much sense to use with 'git stash show', so
> I'm not sure whether or not it makes sense to support it at all.  But
> we do promise to pass all options through to in our documentation, so
> the new behaviour is what we are documenting.

Indeed, I guess it doesn't make sense at all. I cannot come up
with a situation where `--quiet` would be useful. I will think more
about it and come back with a reply if I find any good example
where it would be useful.

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API
  2018-08-15 21:01       ` Thomas Gummerer
@ 2018-08-18 15:11         ` Paul Sebastian Ungureanu
  0 siblings, 0 replies; 181+ messages in thread
From: Paul Sebastian Ungureanu @ 2018-08-18 15:11 UTC (permalink / raw)
  To: Thomas Gummerer; +Cc: Git Mailing List

On Thu, Aug 16, 2018 at 12:01 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
> On 08/08, Paul-Sebastian Ungureanu wrote:
>> Currently, `show_stash()` uses `cmd_diff()` to generate
>> the output. After this commit, the output will be generated
>> using the internal API.
>>
>> Before this commit, `git stash show --quiet` would act like
>> `git diff` and error out if the stash is not empty. Now, the
>> `--quiet` option does not error out given an empty stash.
>
> I think this needs a bit more justification.  As I mentioned in my
> comment to a previous patch, I'm not sure '--quiet' makes much sense
> with 'git stash show' (it will show nothing, and will always exit with
> an error code, as the stash will always contain something), but if we
> are supporting the same flags as 'git diff', and essentially just
> forwarding them, shouldn't they keep the same behaviour as well?

If we are going to support them, I guess there wouldn't be a problem
if any change in behaviour is noted in documentation (but as you said,
the next commit should be squashed into this). Indeed, the big question
is if we should support them. I would say no considering there is no
benefit.

>> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
>> ---
>>  builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------
>>  1 file changed, 45 insertions(+), 28 deletions(-)
>>
>> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
>> index 0c1efca6b..ec8c38c6f 100644
>> --- a/builtin/stash--helper.c
>> +++ b/builtin/stash--helper.c
>> @@ -10,6 +10,8 @@
>>  #include "run-command.h"
>>  #include "dir.h"
>>  #include "rerere.h"
>> +#include "revision.h"
>> +#include "log-tree.h"
>>
>>  static const char * const git_stash_helper_usage[] = {
>>       N_("git stash--helper list [<options>]"),
>> @@ -662,56 +664,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)
>>
>>  static int show_stash(int argc, const char **argv, const char *prefix)
>>  {
>> -     int i, ret = 0;
>> -     struct child_process cp = CHILD_PROCESS_INIT;
>> -     struct argv_array args_refs = ARGV_ARRAY_INIT;
>> +     int i;
>> +     int flags = 0;
>>       struct stash_info info;
>> +     struct rev_info rev;
>> +     struct argv_array stash_args = ARGV_ARRAY_INIT;
>>       struct option options[] = {
>>               OPT_END()
>>       };
>>
>> -     argc = parse_options(argc, argv, prefix, options,
>> -                          git_stash_helper_show_usage,
>> -                          PARSE_OPT_KEEP_UNKNOWN);
>> +     init_diff_ui_defaults();
>> +     git_config(git_diff_ui_config, NULL);
>>
>> -     cp.git_cmd = 1;
>> -     argv_array_push(&cp.args, "diff");
>> +     init_revisions(&rev, prefix);
>>
>> -     /* Push arguments which are not options into args_refs. */
>> -     for (i = 0; i < argc; ++i) {
>> -             if (argv[i][0] == '-')
>> -                     argv_array_push(&cp.args, argv[i]);
>> +     /* Push arguments which are not options into stash_args. */
>> +     for (i = 1; i < argc; ++i) {
>> +             if (argv[i][0] != '-')
>> +                     argv_array_push(&stash_args, argv[i]);
>>               else
>> -                     argv_array_push(&args_refs, argv[i]);
>> -     }
>> -
>> -     if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {
>> -             child_process_clear(&cp);
>> -             argv_array_clear(&args_refs);
>> -             return -1;
>> +                     flags++;
>>       }
>>
>>       /*
>>        * The config settings are applied only if there are not passed
>>        * any flags.
>>        */
>> -     if (cp.args.argc == 1) {
>> +     if (!flags) {
>>               git_config(git_stash_config, NULL);
>>               if (show_stat)
>> -                     argv_array_push(&cp.args, "--stat");
>> +                     rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;
>> +             if (show_patch) {
>> +                     rev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;
>> +                     rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
>> +             }
>
> I failed to notice this in the previous patch (the problem existed
> there as well), but this changes the behaviour of 'git -c
> stash.showStat=false stash show <stash>'.  Previously doing this would
> not show anything, which is the correct behaviour, while now still
> shows the diffstat.
>
> I think the show_stat variable is interpreted the wrong way around in
> the previous patch.
>
> Something else I noticed now that I was playing around more with the
> config options is that the parsing of the config options is not
> correctly done in the previous patch.  It does a 'strcmp(var,
> "stash.showStat"))', but the config API makes all variables lowercase
> (config options are case insensitive, and making everything lowercase
> is the way to ensure that), so it should be 'strcmp(var, "stash.showstat"))',
> and similar for the 'stash.showpatch' config option.
>
> This all sounds like it would be nice to have some tests for these
> config options, to make sure we get it right, and won't break them in
> the future.

Thanks! Great suggestion! I will add some tests for this.

>> +     }
>>
>> -             if (show_patch)
>> -                     argv_array_push(&cp.args, "-p");
>> +     if (get_stash_info(&info, stash_args.argc, stash_args.argv)) {
>> +             argv_array_clear(&stash_args);
>> +             return -1;
>>       }
>>
>> -     argv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),
>> -                      oid_to_hex(&info.w_commit), NULL);
>> +     argc = setup_revisions(argc, argv, &rev, NULL);
>> +     if (!rev.diffopt.output_format)
>> +             rev.diffopt.output_format = DIFF_FORMAT_PATCH;
>> +     diff_setup_done(&rev.diffopt);
>> +     rev.diffopt.flags.recursive = 1;
>> +     setup_diff_pager(&rev.diffopt);
>>
>> -     ret = run_command(&cp);
>> +     /*
>> +      * We can return early if there was any option not recognised by
>> +      * `diff_opt_parse()`, besides the word `stash`.
>> +      */
>
> I'm not sure I follow what you mean with this comment.  I don't see
> any 'diff_opt_parse' in the code above?

Oh, right. `diff_opt_parse()` is called by `setup_revision()` at some point,
but as you said a little bit below, these two comments (the previous one
and the next one) are redundant.

>> +     if (argc > 1) {
>> +             free_stash_info(&info);
>> +             argv_array_clear(&stash_args);
>> +             usage_with_options(git_stash_helper_show_usage, options);
>> +     }
>> +
>> +     /* Do the diff thing. */
>
> This comment should be dropped.  It's obvious that we are doing the
> diff thing below from the code, so the comment is redundant at best,
> makes people read more lines, and could possibly get outdated.
>
> I'd also drop the other comments above, as they do not provide a lot
> of extra value imho.
>
>> +     diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
>> +     log_tree_diff_flush(&rev);
>>
>>       free_stash_info(&info);
>> -     argv_array_clear(&args_refs);
>> -     return ret;
>> +     argv_array_clear(&stash_args);
>> +     return 0;
>>  }
>>
>>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>> --
>> 2.18.0.573.g56500d98f
>>

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 18/26] stash: convert push to builtin
  2018-08-08 18:58     ` [GSoC][PATCH v7 18/26] stash: convert push to builtin Paul-Sebastian Ungureanu
@ 2018-08-18 15:36       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 15:36 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Add stash push to the helper.
> ---

This (and the previous two and I think most subsequent patches) are
missing your sign-off.

>  builtin/stash--helper.c | 209 ++++++++++++++++++++++++++++++++++++++++
>  git-stash.sh            |   6 +-
>  2 files changed, 213 insertions(+), 2 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index f905d3908..c26cad3d5 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper clear"),
>  	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
>  	N_("git stash--helper create [<message>]"),
> +	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
> +	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
> +	   "          [--] [<pathspec>...]]"),
>  	NULL
>  };
>  
> @@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {
>  	NULL
>  };
>  
> +static const char * const git_stash_helper_push_usage[] = {
> +	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
> +	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
> +	   "          [--] [<pathspec>...]]"),
> +	NULL
> +};
> +
>  static const char *ref_stash = "refs/stash";
>  static int quiet;
>  static struct strbuf stash_index_path = STRBUF_INIT;
> @@ -1210,6 +1220,203 @@ static int create_stash(int argc, const char **argv, const char *prefix)
>  	return ret < 0;
>  }
>  
> +static int do_push_stash(int argc, const char **argv, const char *prefix,
> +			 int keep_index, int patch_mode, int include_untracked,
> +			 int quiet, const char *stash_msg)
> +{
> +	int ret = 0;
> +	struct pathspec ps;
> +	struct stash_info info;
> +	if (patch_mode && keep_index == -1)
> +		keep_index = 1;
> +
> +	if (patch_mode && include_untracked) {
> +		fprintf_ln(stderr, "Can't use --patch and --include-untracked or --all at the same time");

This should be marked for translation.  Similar for the messages
below.  I noticed this in a previous patch as well, so it may be worth
reviewing all the output, and checking that it's going to the right
stream and is marked for translation.

> +		return -1;
> +	}
> +
> +	parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
> +
> +	if (read_cache() < 0)
> +		die(_("index file corrupt"));
> +
> +	if (!include_untracked && ps.nr) {
> +		int i;
> +		char *ps_matched = xcalloc(ps.nr, 1);
> +
> +		for (i = 0; i < active_nr; ++i) {
> +			const struct cache_entry *ce = active_cache[i];
> +			if (!ce_path_match(ce, &ps, ps_matched))
> +				continue;
> +		}
> +
> +		if (report_path_error(ps_matched, &ps, prefix)) {
> +			fprintf_ln(stderr, "Did you forget to 'git add'?");
> +			return -1;
> +		}
> +	}
> +
> +	read_cache_preload(NULL);

Instead of doing a 'read_cache' before and 'read_cache_preload(NULL)'
here, we could just use 'read_cache_preload(NULL)' above.
'read_cache' does return early if the index has already been read, so
there's no big harm in doing this twice, but just having one call is
still neater I think.

It would make the command slightly slower in the error case above, but
I doubt that's worth worrying about.

> +	if (refresh_cache(REFRESH_QUIET))
> +		return -1;
> +
> +	if (!check_changes(argv, include_untracked, prefix)) {
> +		fprintf_ln(stdout, "No local changes to save");
> +		return 0;
> +	}
> +
> +	if (!reflog_exists(ref_stash) && do_clear_stash()) {
> +		fprintf_ln(stderr, "Cannot initialize stash");
> +		return -1;
> +	}
> +
> +	if ((ret = do_create_stash(argc, argv, prefix, &stash_msg,
> +				   include_untracked, patch_mode, &info)))
> +		return ret;

Should this be 'return ret < 0'?  'ret == 1' means there are no
changes, for which we currently get a 0 exit code.  Though on second
thought that can't happen, because we already have 'check_changes'
above.  Why do we want the 'ret' variable here?

Something I notice here is that we are passing 'argc' and 'argv'
around a lot.  We passed that through parse-options already, and it
seems to me that we're mostly left with pathspecs here, rather than
'argv'.  It looks to me like we could just parse the pathspecs in the
callers (which we do in some places, but maybe not in all of them) and
then pass 'struct pathspec' around instead of the leftover argv, which
is easier to understand, and gives all these functions a neater/easier
to understand interface.

Also looking at 'do_create_stash', the 'argc' argument seems to be
unused?  I failed to notice that earlier obviously, but we should get
rid of that unused argument.

> +
> +	if (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {
> +		fprintf(stderr, "Cannot save the current status");
> +		return -1;
> +	}
> +
> +	fprintf(stdout, "Saved working directory and index state %s", stash_msg);
> +
> +	if (!patch_mode) {
> +		if (include_untracked && ps.nr == 0) {
> +			struct child_process cp = CHILD_PROCESS_INIT;
> +
> +			cp.git_cmd = 1;
> +			argv_array_pushl(&cp.args, "clean", "--force",
> +					 "--quiet", "-d", NULL);
> +			if (include_untracked == 2)
> +				argv_array_push(&cp.args, "-x");
> +			if (run_command(&cp))
> +				return -1;
> +		}
> +		if (argc != 0) {

We use 'ps.nr' in the check above, and 'argc' here.  I think we should
consistently use 'ps.nr'.

> +			int i;
> +			struct child_process cp1 = CHILD_PROCESS_INIT;
> +			struct child_process cp2 = CHILD_PROCESS_INIT;
> +			struct child_process cp3 = CHILD_PROCESS_INIT;
> +			struct strbuf out = STRBUF_INIT;
> +
> +			cp1.git_cmd = 1;
> +			argv_array_push(&cp1.args, "add");
> +			if (!include_untracked)
> +				argv_array_push(&cp1.args, "-u");
> +			if (include_untracked == 2)
> +				argv_array_push(&cp1.args, "--force");
> +			argv_array_push(&cp1.args, "--");
> +			for (i = 0; i < ps.nr; ++i)
> +				argv_array_push(&cp1.args, ps.items[i].match);

Since we're doing this 'argv_array_push' with pathspec elements a few
times already (and may do it even more often if the code is being
refactored to pass 'struct pathspec' around), would it be worth
factoring this out as a tiny helper function?

> +			if (run_command(&cp1))
> +				return -1;
> +
> +			cp2.git_cmd = 1;
> +			argv_array_pushl(&cp2.args, "diff-index", "-p",
> +					 "--cached", "--binary", "HEAD", "--",
> +					 NULL);
> +			for (i = 0; i < ps.nr; ++i)
> +				argv_array_push(&cp2.args, ps.items[i].match);
> +			if (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))
> +				return -1;
> +
> +			cp3.git_cmd = 1;
> +			argv_array_pushl(&cp3.args, "apply", "--index", "-R",
> +					 NULL);
> +			if (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,
> +					 0))
> +				return -1;
> +		} else {
> +			struct child_process cp = CHILD_PROCESS_INIT;
> +			cp.git_cmd = 1;
> +			argv_array_pushl(&cp.args, "reset", "--hard", "-q",
> +					 NULL);
> +			if (run_command(&cp))
> +				return -1;
> +		}
> +
> +		if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
> +			int i;
> +			struct child_process cp1 = CHILD_PROCESS_INIT;
> +			struct child_process cp2 = CHILD_PROCESS_INIT;
> +			struct strbuf out = STRBUF_INIT;
> +
> +			if (reset_tree(&info.i_tree, 0, 1))
> +				return -1;
> +
> +			cp1.git_cmd = 1;
> +			argv_array_pushl(&cp1.args, "ls-files", "-z",
> +					 "--modified", "--", NULL);
> +			for (i = 0; i < ps.nr; ++i)
> +				argv_array_push(&cp1.args, ps.items[i].match);
> +			if (pipe_command(&cp1, NULL, 0, &out, 0, NULL, 0))
> +				return -1;
> +
> +			cp2.git_cmd = 1;
> +			argv_array_pushl(&cp2.args, "checkout-index", "-z",
> +					 "--force", "--stdin", NULL);
> +			if (pipe_command(&cp2, out.buf, out.len, NULL, 0, NULL,
> +					 0))
> +				return -1;
> +		}
> +	} else {
> +		struct child_process cp = CHILD_PROCESS_INIT;
> +
> +		cp.git_cmd = 1;
> +		argv_array_pushl(&cp.args, "apply", "-R", NULL);
> +
> +		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
> +			fprintf_ln(stderr, "Cannot remove worktree changes");
> +			return -1;
> +		}
> +
> +		if (keep_index < 1) {
> +			int i;
> +			struct child_process cp = CHILD_PROCESS_INIT;
> +
> +			cp.git_cmd = 1;
> +			argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
> +			for (i = 0; i < ps.nr; ++i)
> +				argv_array_push(&cp.args, ps.items[i].match);
> +			if (run_command(&cp))
> +				return -1;
> +		}
> +	}
> +	return 0;
> +}
> +
> +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 option options[] = {
> +		OPT_SET_INT('k', "keep-index", &keep_index,
> +			N_("keep index"), 1),
> +		OPT_BOOL('p', "patch", &patch_mode,
> +			N_("stash in patch mode")),
> +		OPT_BOOL('q', "quiet", &quiet,
> +			N_("quiet mode")),

We should be able to use OPT__QUIET here.

> +		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()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_push_usage,
> +			     0);
> +
> +	return do_push_stash(argc, argv, prefix, keep_index, patch_mode,
> +			     include_untracked, quiet, stash_msg);
> +}
> +
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  {
>  	pid_t pid = getpid();
> @@ -1248,6 +1455,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!store_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "create"))
>  		return !!create_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "push"))
> +		return !!push_stash(argc, argv, prefix);
>  
>  	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
>  		      git_stash_helper_usage, options);
> diff --git a/git-stash.sh b/git-stash.sh
> index ab06e4ffb..c3146f62a 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -412,7 +412,8 @@ save)
>  	;;
>  push)
>  	shift
> -	push_stash "$@"
> +	cd "$START_DIR"
> +	git stash--helper push "$@"
>  	;;
>  apply)
>  	shift
> @@ -448,7 +449,8 @@ branch)
>  *)
>  	case $# in
>  	0)
> -		push_stash &&
> +		cd "$START_DIR"
> +		git stash--helper push &&
>  		say "$(gettext "(To restore them type \"git stash apply\")")"
>  		;;
>  	*)
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin
  2018-08-15 22:13       ` Thomas Gummerer
@ 2018-08-18 15:39         ` Paul Sebastian Ungureanu
  2018-08-18 20:23           ` Thomas Gummerer
  0 siblings, 1 reply; 181+ messages in thread
From: Paul Sebastian Ungureanu @ 2018-08-18 15:39 UTC (permalink / raw)
  To: Thomas Gummerer; +Cc: Git Mailing List

On Thu, Aug 16, 2018 at 1:13 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
> On 08/08, Paul-Sebastian Ungureanu wrote:
>> Add stash create to the helper.
>>
>> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
>> ---
>>  builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++
>>  git-stash.sh            |   2 +-
>>  2 files changed, 407 insertions(+), 1 deletion(-)
>>
>> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
>> index 5ff810f8c..a4e57899b 100644
>> --- a/builtin/stash--helper.c
>> +++ b/builtin/stash--helper.c
>> @@ -21,6 +21,7 @@ static const char * const git_stash_helper_usage[] = {
>>       N_("git stash--helper branch <branchname> [<stash>]"),
>>       N_("git stash--helper clear"),
>>       N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
>> +     N_("git stash--helper create [<message>]"),
>>       NULL
>>  };
>>
>> @@ -64,6 +65,11 @@ static const char * const git_stash_helper_store_usage[] = {
>>       NULL
>>  };
>>
>> +static const char * const git_stash_helper_create_usage[] = {
>> +     N_("git stash--helper create [<message>]"),
>> +     NULL
>> +};
>> +
>>  static const char *ref_stash = "refs/stash";
>>  static int quiet;
>>  static struct strbuf stash_index_path = STRBUF_INIT;
>> @@ -781,6 +787,404 @@ static int store_stash(int argc, const char **argv, const char *prefix)
>>       return do_store_stash(argv[0], stash_msg, quiet);
>>  }
>>
>> [...]
>>
>> +
>> +static int do_create_stash(int argc, const char **argv, const char *prefix,
>> +                        const char **stash_msg, int include_untracked,
>> +                        int patch_mode, struct stash_info *info)
>> +{
>> +     int untracked_commit_option = 0;
>> +     int ret = 0;
>> +     int subject_len;
>> +     int flags;
>> +     const char *head_short_sha1 = NULL;
>> +     const char *branch_ref = NULL;
>> +     const char *head_subject = NULL;
>> +     const char *branch_name = "(no branch)";
>> +     struct commit *head_commit = NULL;
>> +     struct commit_list *parents = NULL;
>> +     struct strbuf msg = STRBUF_INIT;
>> +     struct strbuf commit_tree_label = STRBUF_INIT;
>> +     struct strbuf out = STRBUF_INIT;
>> +     struct strbuf final_stash_msg = STRBUF_INIT;
>> +
>> +     read_cache_preload(NULL);
>> +     refresh_cache(REFRESH_QUIET);
>> +
>> +     if (!check_changes(argv, include_untracked, prefix)) {
>> +             ret = 1;
>> +             goto done;
>
> I wonder if we can just 'exit(0)' here, instead of returning.  This
> whole command is a builtin, and I *think* outside of 'libgit.a' exiting
> early is fine.  It does mean that we're not free'ing the memory
> though, which means a leak checker would probably complain.  So
> dunno.  It would simplify the code a little, but not sure it's worth it.

Indeed, there shouldn't be any problem by calling exit(0).

>> +     }
>> +
>> +     if (get_oid("HEAD", &info->b_commit)) {
>> +             fprintf_ln(stderr, "You do not have the initial commit yet");
>> +             ret = -1;
>> +             goto done;
>> +     } else {
>> +             head_commit = lookup_commit(the_repository, &info->b_commit);
>> +     }
>> +
>> +     branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
>> +     if (flags & REF_ISSYMREF)
>> +             branch_name = strrchr(branch_ref, '/') + 1;
>> +     head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
>> +                                          DEFAULT_ABBREV);
>> +     subject_len = find_commit_subject(get_commit_buffer(head_commit, NULL),
>> +                                       &head_subject);
>> +     strbuf_addf(&msg, "%s: %s %.*s\n", branch_name, head_short_sha1,
>> +                 subject_len, head_subject);
>
> I think this can be written in a slightly simpler way:
>
>         head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
>                                              DEFAULT_ABBREV);
>         strbuf_addf(&msg, "%s: %s", branch_name, head_short_sha1);
>         pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
>         strbuf_addch(&msg, '\n');
>
> The other advantage this brings is that it is consistent with other
> places where we print/use the subject of a commit (e.g. in 'git reset
> --hard').

Thanks for this suggestion.

>> +
>> +     strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
>> +     commit_list_insert(head_commit, &parents);
>> +     if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
>> +         commit_tree(commit_tree_label.buf, commit_tree_label.len,
>> +                     &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
>> +             fprintf_ln(stderr, "Cannot save the current index state");
>
> Looks like this message is translated in the current 'git stash'
> implementation, so it should be here as well.  Same for the messages
> below.
>
>> +             ret = -1;
>> +             goto done;
>> +     }
>> +
>> +     if (include_untracked && get_untracked_files(argv, 1,
>> +                                                  include_untracked, &out)) {
>> +             if (save_untracked_files(info, &msg, &out)) {
>> +                     printf_ln("Cannot save the untracked files");
>
> Why does this go to stdout, whereas "Cannot save the current index
> state" above goes to stderr?  In the shell version of git stash these
> all go to stderr fwiw.  There are a few similar cases, it would
> probably be worth going through all the print statements here and see
> if they need to be translated, and to which output stream they should
> go.

This is what I am going to do.

>> +                     ret = -1;
>> +                     goto done;
>> +             }
>> +             untracked_commit_option = 1;
>> +     }
>> +     if (patch_mode) {
>> +             ret = stash_patch(info, argv);
>> +             if (ret < 0) {
>> +                     printf_ln("Cannot save the current worktree state");
>> +                     goto done;
>> +             } else if (ret > 0) {
>> +                     goto done;
>> +             }
>> +     } else {
>> +             if (stash_working_tree(info, argv)) {
>> +                     printf_ln("Cannot save the current worktree state");
>> +                     ret = -1;
>> +                     goto done;
>> +             }
>> +     }
>> +
>> +     if (!*stash_msg || !strlen(*stash_msg))
>> +             strbuf_addf(&final_stash_msg, "WIP on %s", msg.buf);
>> +     else
>> +             strbuf_addf(&final_stash_msg, "On %s: %s\n", branch_name,
>> +                         *stash_msg);
>> +     *stash_msg = strbuf_detach(&final_stash_msg, NULL);
>
> strbuf_detach means we're taking ownership of the memory, so we'll
> have to free it afterwards. Looking at this we may not even want to
> re-use the 'stash_msg' variable here, but instead introduce another
> variable for it, so it doesn't have a dual meaning in this function.
>
>> +
>> +     /*
>> +      * `parents` will be empty after calling `commit_tree()`, so there is
>> +      * no need to call `free_commit_list()`
>> +      */
>> +     parents = NULL;
>> +     if (untracked_commit_option)
>> +             commit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);
>> +     commit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);
>> +     commit_list_insert(head_commit, &parents);
>> +
>> +     if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
>> +                     parents, &info->w_commit, NULL, NULL)) {
>> +             printf_ln("Cannot record working tree state");
>> +             ret = -1;
>> +             goto done;
>> +     }
>> +
>> +done:
>> +     strbuf_release(&commit_tree_label);
>> +     strbuf_release(&msg);
>> +     strbuf_release(&out);
>> +     strbuf_release(&final_stash_msg);
>> +     return ret;
>> +}
>> +
>> +static int create_stash(int argc, const char **argv, const char *prefix)
>> +{
>> +     int include_untracked = 0;
>> +     int ret = 0;
>> +     const char *stash_msg = NULL;
>> +     struct stash_info info;
>> +     struct option options[] = {
>> +             OPT_BOOL('u', "include-untracked", &include_untracked,
>> +                      N_("include untracked files in stash")),
>> +             OPT_STRING('m', "message", &stash_msg, N_("message"),
>> +                      N_("stash message")),
>> +             OPT_END()
>> +     };
>> +
>> +     argc = parse_options(argc, argv, prefix, options,
>> +                          git_stash_helper_create_usage,
>> +                          0);
>> +
>> +     ret = do_create_stash(argc, argv, prefix, &stash_msg,
>> +                           include_untracked, 0, &info);
>
> stash_msg doesn't have to be passed as a pointer to a pointer here, as
> we never need the modified value after this function returns.  I think
> just passing 'stash_msg' here instead of '&stash_msg' will make
> 'do_create_stash' slightly easier to read.

That's right, but `do_create_stash()` is also called by
`do_push_stash()`, which will need the modified value.

>> +
>> +     if (!ret)
>> +             printf_ln("%s", oid_to_hex(&info.w_commit));
>> +
>> +     /*
>> +      * ret can be 1 if there were no changes. In this case, we should
>> +      * not error out.
>> +      */
>> +     return ret < 0;
>> +}
>> +
>>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>>  {
>>       pid_t pid = getpid();
>> @@ -817,6 +1221,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>>               return !!show_stash(argc, argv, prefix);
>>       else if (!strcmp(argv[0], "store"))
>>               return !!store_stash(argc, argv, prefix);
>> +     else if (!strcmp(argv[0], "create"))
>> +             return !!create_stash(argc, argv, prefix);
>>
>>       usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
>>                     git_stash_helper_usage, options);
>> diff --git a/git-stash.sh b/git-stash.sh
>> index 5739c5152..ab06e4ffb 100755
>> --- a/git-stash.sh
>> +++ b/git-stash.sh
>> @@ -425,7 +425,7 @@ clear)
>>       ;;
>>  create)
>>       shift
>> -     create_stash -m "$*" && echo "$w_commit"
>> +     git stash--helper create --message "$*"
>>       ;;
>>  store)
>>       shift
>> --
>> 2.18.0.573.g56500d98f
>>
Thanks!

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 19/26] stash: make push to be quiet
  2018-08-08 18:58     ` [GSoC][PATCH v7 19/26] stash: make push to be quiet Paul-Sebastian Ungureanu
@ 2018-08-18 15:46       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 15:46 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

> Subject: stash: make push to be quiet

Nit: maybe "stash: make push -q quiet"?  I think the subject should at
least mention the -q option.

On 08/08, Paul-Sebastian Ungureanu wrote:
> There is a change in behaviour with this commit. When there was
> no initial commit, the shell version of stash would still display
> a message. This commit makes `push` to not display any message if
> `--quiet` or `-q` is specified.

Yeah, not being quiet here cna be considered a bug, so this change in
behaviour makes sense.

Should the "No changes selected" message in 'stash_patch' also be made
quiet?

> ---
>  builtin/stash--helper.c | 41 +++++++++++++++++++++++++++--------------
>  1 file changed, 27 insertions(+), 14 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index c26cad3d5..4fd79532c 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -1079,7 +1079,7 @@ static int stash_working_tree(struct stash_info *info,
>  
>  static int do_create_stash(int argc, const char **argv, const char *prefix,
>  			   const char **stash_msg, int include_untracked,
> -			   int patch_mode, struct stash_info *info)
> +			   int patch_mode, struct stash_info *info, int quiet)
>  {
>  	int untracked_commit_option = 0;
>  	int ret = 0;
> @@ -1105,7 +1105,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
>  	}
>  
>  	if (get_oid("HEAD", &info->b_commit)) {
> -		fprintf_ln(stderr, "You do not have the initial commit yet");
> +		if (!quiet)
> +			fprintf_ln(stderr, "You do not have the initial commit yet");
>  		ret = -1;
>  		goto done;
>  	} else {
> @@ -1127,7 +1128,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
>  	if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
>  	    commit_tree(commit_tree_label.buf, commit_tree_label.len,
>  			&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
> -		fprintf_ln(stderr, "Cannot save the current index state");
> +		if (!quiet)
> +			fprintf_ln(stderr, "Cannot save the current index state");
>  		ret = -1;
>  		goto done;
>  	}
> @@ -1135,7 +1137,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
>  	if (include_untracked && get_untracked_files(argv, 1,
>  						     include_untracked, &out)) {
>  		if (save_untracked_files(info, &msg, &out)) {
> -			printf_ln("Cannot save the untracked files");
> +			if (!quiet)
> +				printf_ln("Cannot save the untracked files");
>  			ret = -1;
>  			goto done;
>  		}
> @@ -1144,14 +1147,16 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
>  	if (patch_mode) {
>  		ret = stash_patch(info, argv);
>  		if (ret < 0) {
> -			printf_ln("Cannot save the current worktree state");
> +			if (!quiet)
> +				printf_ln("Cannot save the current worktree state");
>  			goto done;
>  		} else if (ret > 0) {
>  			goto done;
>  		}
>  	} else {
>  		if (stash_working_tree(info, argv, prefix)) {
> -			printf_ln("Cannot save the current worktree state");
> +			if (!quiet)
> +				printf_ln("Cannot save the current worktree state");
>  			ret = -1;
>  			goto done;
>  		}
> @@ -1176,7 +1181,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
>  
>  	if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
>  			parents, &info->w_commit, NULL, NULL)) {
> -		printf_ln("Cannot record working tree state");
> +		if (!quiet)
> +			printf_ln("Cannot record working tree state");
>  		ret = -1;
>  		goto done;
>  	}
> @@ -1208,7 +1214,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
>  			     0);
>  
>  	ret = do_create_stash(argc, argv, prefix, &stash_msg,
> -			      include_untracked, 0, &info);
> +			      include_untracked, 0, &info, 0);
>  
>  	if (!ret)
>  		printf_ln("%s", oid_to_hex(&info.w_commit));
> @@ -1261,25 +1267,31 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
>  		return -1;
>  
>  	if (!check_changes(argv, include_untracked, prefix)) {
> -		fprintf_ln(stdout, "No local changes to save");
> +		if (!quiet)
> +			fprintf_ln(stdout, "No local changes to save");
>  		return 0;
>  	}
>  
>  	if (!reflog_exists(ref_stash) && do_clear_stash()) {
> -		fprintf_ln(stderr, "Cannot initialize stash");
> +		if (!quiet)
> +			fprintf_ln(stderr, "Cannot initialize stash");
>  		return -1;
>  	}
>  
>  	if ((ret = do_create_stash(argc, argv, prefix, &stash_msg,
> -				   include_untracked, patch_mode, &info)))
> +				   include_untracked, patch_mode, &info,
> +				   quiet)))
>  		return ret;
>  
>  	if (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {
> -		fprintf(stderr, "Cannot save the current status");
> +		if (!quiet)
> +			fprintf_ln(stderr, "Cannot save the current status");
>  		return -1;
>  	}
>  
> -	fprintf(stdout, "Saved working directory and index state %s", stash_msg);
> +	if (!quiet)
> +		fprintf(stdout, "Saved working directory and index state %s",
> +			stash_msg);
>  
>  	if (!patch_mode) {
>  		if (include_untracked && ps.nr == 0) {
> @@ -1367,7 +1379,8 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
>  		argv_array_pushl(&cp.args, "apply", "-R", NULL);
>  
>  		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
> -			fprintf_ln(stderr, "Cannot remove worktree changes");
> +			if (!quiet)
> +				fprintf_ln(stderr, "Cannot remove worktree changes");
>  			return -1;
>  		}
>  
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin
  2018-08-08 18:58     ` [GSoC][PATCH v7 05/26] stash: convert apply to builtin Paul-Sebastian Ungureanu
  2018-08-08 20:18       ` Junio C Hamano
@ 2018-08-18 16:09       ` Duy Nguyen
  1 sibling, 0 replies; 181+ messages in thread
From: Duy Nguyen @ 2018-08-18 16:09 UTC (permalink / raw)
  To: Paul Sebastian Ungureanu; +Cc: Git Mailing List

On Wed, Aug 8, 2018 at 9:00 PM Paul-Sebastian Ungureanu
<ungureanupaulsebastian@gmail.com> wrote:
> +       strbuf_init(&info->revision, 0);
> +       if (!commit) {
> +               if (!ref_exists(ref_stash)) {
> +                       free_stash_info(info);
> +                       fprintf_ln(stderr, "No stash entries found.");

Maybe _() ?
-- 
Duy

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q`
  2018-08-08 18:58     ` [GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q` Paul-Sebastian Ungureanu
@ 2018-08-18 16:12       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 16:12 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> This commit introduces more tests for the quiet option of
> `git stash push`.

I think this commit should be squashed into the previous one, so we
have implementation and tests in one commit.  That way it's easier to
see during review that there are tests for the change.  For more
discussion on that also see [1].

[1]: https://public-inbox.org/git/20180806144726.GB97564@aiede.svl.corp.google.com/

> ---
>  t/t3903-stash.sh | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index 8d002a7f2..b78db74ae 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -1064,6 +1064,27 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '
>  	test_path_is_file untracked
>  '
>  
> +test_expect_success 'push: -q is quiet with changes' '
> +	>foo &&
> +	git stash push -q >output 2>&1 &&

We create an untracked file here and then call 'git stash push', which
will not create a new stash, as we don't use the --include-untracked
option.  In fact, right now this test is doing the same thing as the
test below.  There should be a 'git add foo' above the 'git stash
push' call to test what we're claiming to test here.

> +	test_must_be_empty output
> +'
> +
> +test_expect_success 'push: -q is quiet with no changes' '
> +	git stash push -q >output 2>&1 &&
> +	test_must_be_empty output
> +'
> +
> +test_expect_success 'push: -q is quiet even if there is no initial commit' '
> +	git init foo_dir &&
> +	cd foo_dir &&
> +	touch bar &&

The typical style in the test suite for creating a new file is to use
'>bar', unless you care about the 'mtime' the file has.  We don't seem
to care about that in this test, so avoiding 'touch' would be better.

> +	test_must_fail git stash push -q >output 2>&1 &&
> +	test_must_be_empty output &&
> +	cd .. &&

The above should be in a subshell, i.e.

    (
        cd foo_dir &&
	touch bar &&
        test_must_fail git stash push -q >output 2>&1 &&
        test_must_be_empty output &&
    )

then you don't have to do the 'cd ..' in the end.  With the 'cd ..' in
the end, if one of the commands between the 'cd foo_dir' and 'cd ..'
fails, all subsequent tests will be run inside of 'foo_dir', which
puts them in a different environment than they expect.  That can cause
all kinds of weirdness.

If inside a subshell, the current working directory of the parent
shell is unaffected, so we don't have to worry about cd'ing back, and
subsequent tests will get the correct cwd even if things go wrong in
this test.
	

> +	rm -rf foo_dir

We'll want to run this cleanup to run even if the test fails.  To do
so, the 'test_when_finished' helper can be used.  Using that, this
would go at the top of the test, as 'test_when_finished rm -rf
foo_dir'.  Otherwise if any of the commands above fail, 'foo_dir' will
not be removed, and may interfere with subsequent tests.

> +'
> +
>  test_expect_success 'untracked files are left in place when -u is not given' '
>  	>file &&
>  	git add file &&
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 22/26] stash: convert save to builtin
  2018-08-08 18:59     ` [GSoC][PATCH v7 22/26] stash: convert save to builtin Paul-Sebastian Ungureanu
@ 2018-08-18 16:33       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 16:33 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Add stash save to the helper and delete functions which are no
> longer needed (`show_help()`, `save_stash()`, `push_stash()`,
> `create_stash()`, `clear_stash()`, `untracked_files()` and
> `no_changes()`).
> ---
>  builtin/stash--helper.c |  48 +++++++
>  git-stash.sh            | 311 +---------------------------------------
>  2 files changed, 50 insertions(+), 309 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 5c27f5dcf..f54a476e3 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -26,6 +26,8 @@ static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
>  	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
>  	   "          [--] [<pathspec>...]]"),
> +	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
> +	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
>  	NULL
>  };
>  
> @@ -81,6 +83,12 @@ static const char * const git_stash_helper_push_usage[] = {
>  	NULL
>  };
>  
> +static const char * const git_stash_helper_save_usage[] = {
> +	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
> +	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
> +	NULL
> +};
> +
>  static const char *ref_stash = "refs/stash";
>  static int quiet;
>  static struct strbuf stash_index_path = STRBUF_INIT;
> @@ -1445,6 +1453,44 @@ static int push_stash(int argc, const char **argv, const char *prefix)
>  			     include_untracked, quiet, stash_msg);
>  }
>  
> +static int save_stash(int argc, const char **argv, const char *prefix)
> +{
> +	int i;
> +	int keep_index = -1;
> +	int patch_mode = 0;
> +	int include_untracked = 0;
> +	int quiet = 0;
> +	char *stash_msg = NULL;
> +	struct strbuf alt_stash_msg = STRBUF_INIT;
> +	struct option options[] = {
> +		OPT_SET_INT('k', "keep-index", &keep_index,
> +			N_("keep index"), 1),
> +		OPT_BOOL('p', "patch", &patch_mode,
> +			N_("stash in patch mode")),
> +		OPT_BOOL('q', "quiet", &quiet,
> +			N_("quiet mode")),

This could be OPT__QUIET again as mentioned in the previous patch as
well.

> +		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()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options,
> +			     git_stash_helper_save_usage,
> +			     0);
> +
> +	for (i = 0; i < argc; ++i)
> +		strbuf_addf(&alt_stash_msg, "%s ", argv[i]);
> +
> +	stash_msg = strbuf_detach(&alt_stash_msg, NULL);

We unconditionally overwrite 'stash_msg' here, even if a '-m'
parameter was given earlier, which I don't think is what we intended
here.  I think we started "supporting" (not erroring out on rather)
the '-m' flag accidentally (my bad, sorry) in 'git stash save' rather
than intentionally, and indeed it has the same behaviour as the code
above.

However I think we should just not add support the '-m' flag here, as
it doesn't make a lot of sense to have two ways of passing a message.

We also never free the memory we get back here from 'strbuf_detach'.
As this is not code in 'libgit.a' that's probably fine, and we can
just add an 'UNLEAK(stash_msg)' here I think.

It may generally be interesting to consider using a leak checker, and
see how far we can get in making this leak free.  It may not be
possible to make 'git stash' completely leak free, as the underlying
APIs may not be, but it may be interesting to see how far we can get
for the new code only.

> +
> +	return do_push_stash(0, NULL, prefix, keep_index, patch_mode,
> +			     include_untracked, quiet, stash_msg);
> +}
> +
>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  {
>  	pid_t pid = getpid();
> @@ -1485,6 +1531,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!create_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "push"))
>  		return !!push_stash(argc, argv, prefix);
> +	else if (!strcmp(argv[0], "save"))
> +		return !!save_stash(argc, argv, prefix);
>  
>  	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
>  		      git_stash_helper_usage, options);
>
> [...]

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c`
  2018-08-08 18:59     ` [GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c` Paul-Sebastian Ungureanu
@ 2018-08-18 16:51       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 16:51 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> The old shell script `git-stash.sh`  was removed and replaced
> entirely by `builtin/stash.c`. In order to do that, `create` and
> `push` were adapted to work without `stash.sh`. For example, before
> this commit, `git stash create` called `git stash--helper create
> --message "$*"`. If it called `git stash--helper create "$@"`, then
> some of these changes wouldn't have been necessary.
> 
> This commit also removes the word `helper` since now stash is
> called directly and not by a shell script.
> ---
>  .gitignore                           |   1 -
>  Makefile                             |   3 +-
>  builtin.h                            |   2 +-
>  builtin/{stash--helper.c => stash.c} | 132 ++++++++++++-----------
>  git-stash.sh                         | 153 ---------------------------
>  git.c                                |   2 +-
>  6 files changed, 74 insertions(+), 219 deletions(-)
>  rename builtin/{stash--helper.c => stash.c} (91%)
>  delete mode 100755 git-stash.sh
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash.c
> similarity index 91%
> rename from builtin/stash--helper.c
> rename to builtin/stash.c
> index f54a476e3..0ef88408a 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash.c
>
> [...]
>
> @@ -1445,9 +1448,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)
>  		OPT_END()
>  	};
>  
> -	argc = parse_options(argc, argv, prefix, options,
> -			     git_stash_helper_push_usage,
> -			     0);
> +	if (argc)
> +		argc = parse_options(argc, argv, prefix, options,
> +				     git_stash_push_usage,
> +				     0);

This change is a bit surprising here.  Why is this necessary?  I
thought parse_options would handle no arguments just fine?

>  	return do_push_stash(argc, argv, prefix, keep_index, patch_mode,
>  			     include_untracked, quiet, stash_msg);
> @@ -1479,7 +1483,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
>  	};
>  
>  	argc = parse_options(argc, argv, prefix, options,
> -			     git_stash_helper_save_usage,
> +			     git_stash_save_usage,
>  			     0);
>  
>  	for (i = 0; i < argc; ++i)
> @@ -1491,7 +1495,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
>  			     include_untracked, quiet, stash_msg);
>  }
>  
> -int cmd_stash__helper(int argc, const char **argv, const char *prefix)
> +int cmd_stash(int argc, const char **argv, const char *prefix)
>  {
>  	pid_t pid = getpid();
>  	const char *index_file;
> @@ -1502,16 +1506,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  
>  	git_config(git_default_config, NULL);
>  
> -	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
> +	argc = parse_options(argc, argv, prefix, options, git_stash_usage,
>  			     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
>  
>  	index_file = get_index_file();
>  	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
>  		    (uintmax_t)pid);
>  
> -	if (argc < 1)
> -		usage_with_options(git_stash_helper_usage, options);
> -	if (!strcmp(argv[0], "apply"))
> +	if (argc == 0)
> +		return !!push_stash(0, NULL, prefix);
> +	else if (!strcmp(argv[0], "apply"))
>  		return !!apply_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "clear"))
>  		return !!clear_stash(argc, argv, prefix);
> @@ -1533,7 +1537,13 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>  		return !!push_stash(argc, argv, prefix);
>  	else if (!strcmp(argv[0], "save"))
>  		return !!save_stash(argc, argv, prefix);
> +	if (*argv[0] == '-') {
> +		struct argv_array args = ARGV_ARRAY_INIT;
> +		argv_array_push(&args, "push");
> +		argv_array_pushv(&args, argv);
> +		return !!push_stash(args.argc, args.argv, prefix);
> +	}

This is a bit different than what the current code does.  Currently
the rules for when a plain 'git stash' becomes 'git stash push' are
the following:

- If there are no arguments.
- If all arguments are option arguments.
- If the first argument of 'git stash' is '-p'.
- If the first argument of 'git stash' is '--'.

This is to avoid someone typing 'git stash -q drop' for example, and
then being surprised that a new stash was created instead of an old
one being dropped, which what we have above would do.

For more reasoning about these aliasing rules see also the thread at [1].

[1]: https://public-inbox.org/git/20170213200950.m3bcyp52wd25p737@sigill.intra.peff.net/

>  
>  	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
> -		      git_stash_helper_usage, options);
> +		      git_stash_usage, options);
>  }
> diff --git a/git-stash.sh b/git-stash.sh
> deleted file mode 100755
> index 695f1feba..000000000
> --- a/git-stash.sh
> +++ /dev/null
> @@ -1,153 +0,0 @@
>
> [...]

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin
  2018-08-18 15:39         ` Paul Sebastian Ungureanu
@ 2018-08-18 20:23           ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 20:23 UTC (permalink / raw)
  To: Paul Sebastian Ungureanu; +Cc: Git Mailing List

On 08/18, Paul Sebastian Ungureanu wrote:
> On Thu, Aug 16, 2018 at 1:13 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
> > On 08/08, Paul-Sebastian Ungureanu wrote:
> >>
> >>  [...]
> >>
> >> +                     ret = -1;
> >> +                     goto done;
> >> +             }
> >> +             untracked_commit_option = 1;
> >> +     }
> >> +     if (patch_mode) {
> >> +             ret = stash_patch(info, argv);
> >> +             if (ret < 0) {
> >> +                     printf_ln("Cannot save the current worktree state");
> >> +                     goto done;
> >> +             } else if (ret > 0) {
> >> +                     goto done;
> >> +             }
> >> +     } else {
> >> +             if (stash_working_tree(info, argv)) {
> >> +                     printf_ln("Cannot save the current worktree state");
> >> +                     ret = -1;
> >> +                     goto done;
> >> +             }
> >> +     }
> >> +
> >> +     if (!*stash_msg || !strlen(*stash_msg))
> >> +             strbuf_addf(&final_stash_msg, "WIP on %s", msg.buf);
> >> +     else
> >> +             strbuf_addf(&final_stash_msg, "On %s: %s\n", branch_name,
> >> +                         *stash_msg);
> >> +     *stash_msg = strbuf_detach(&final_stash_msg, NULL);
> >
> > strbuf_detach means we're taking ownership of the memory, so we'll
> > have to free it afterwards. Looking at this we may not even want to
> > re-use the 'stash_msg' variable here, but instead introduce another
> > variable for it, so it doesn't have a dual meaning in this function.
> >
> >> +
> >> +     /*
> >> +      * `parents` will be empty after calling `commit_tree()`, so there is
> >> +      * no need to call `free_commit_list()`
> >> +      */
> >> +     parents = NULL;
> >> +     if (untracked_commit_option)
> >> +             commit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);
> >> +     commit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);
> >> +     commit_list_insert(head_commit, &parents);
> >> +
> >> +     if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
> >> +                     parents, &info->w_commit, NULL, NULL)) {
> >> +             printf_ln("Cannot record working tree state");
> >> +             ret = -1;
> >> +             goto done;
> >> +     }
> >> +
> >> +done:
> >> +     strbuf_release(&commit_tree_label);
> >> +     strbuf_release(&msg);
> >> +     strbuf_release(&out);
> >> +     strbuf_release(&final_stash_msg);
> >> +     return ret;
> >> +}
> >> +
> >> +static int create_stash(int argc, const char **argv, const char *prefix)
> >> +{
> >> +     int include_untracked = 0;
> >> +     int ret = 0;
> >> +     const char *stash_msg = NULL;
> >> +     struct stash_info info;
> >> +     struct option options[] = {
> >> +             OPT_BOOL('u', "include-untracked", &include_untracked,
> >> +                      N_("include untracked files in stash")),
> >> +             OPT_STRING('m', "message", &stash_msg, N_("message"),
> >> +                      N_("stash message")),
> >> +             OPT_END()
> >> +     };
> >> +
> >> +     argc = parse_options(argc, argv, prefix, options,
> >> +                          git_stash_helper_create_usage,
> >> +                          0);
> >> +
> >> +     ret = do_create_stash(argc, argv, prefix, &stash_msg,
> >> +                           include_untracked, 0, &info);
> >
> > stash_msg doesn't have to be passed as a pointer to a pointer here, as
> > we never need the modified value after this function returns.  I think
> > just passing 'stash_msg' here instead of '&stash_msg' will make
> > 'do_create_stash' slightly easier to read.
> 
> That's right, but `do_create_stash()` is also called by
> `do_push_stash()`, which will need the modified value.

Ah right, I didn't read that far yet when leaving this comment :)

Reading the original push_stash again though, do we actually need the
modified value in 'do_push_stash()'?  The original lines are:

	create_stash -m "$stash_msg" -u "$untracked" -- "$@"
	store_stash -m "$stash_msg" -q $w_commit ||
	die "$(gettext "Cannot save the current status")"

'$stash_msg' gets passed in to 'create_stash()', but is the
'stash_msg' variable inside of 'create_stash()' is local and only the
local copy is modified, so 'store_stash()' would still get the
original.  Or am I missing something?

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 16/26] stash: replace spawning a "read-tree" process
  2018-08-08 18:58     ` [GSoC][PATCH v7 16/26] stash: replace spawning a "read-tree" process Paul-Sebastian Ungureanu
@ 2018-08-18 21:07       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 21:07 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> Instead of spawning a child process, make use of `reset_tree()`
> function already implemented in `stash-helper.c`.
> ---
>  builtin/stash--helper.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index a4e57899b..887b78d05 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -984,21 +984,18 @@ static int stash_patch(struct stash_info *info, const char **argv)
>  static int stash_working_tree(struct stash_info *info, const char **argv)
>  {
>  	int ret = 0;
> -	struct child_process cp0 = CHILD_PROCESS_INIT;
>  	struct child_process cp1 = CHILD_PROCESS_INIT;
>  	struct child_process cp2 = CHILD_PROCESS_INIT;
>  	struct child_process cp3 = CHILD_PROCESS_INIT;
>  	struct strbuf out1 = STRBUF_INIT;
>  	struct strbuf out3 = STRBUF_INIT;
>  
> -	cp0.git_cmd = 1;
> -	argv_array_push(&cp0.args, "read-tree");
> -	argv_array_pushf(&cp0.args, "--index-output=%s", stash_index_path.buf);
> -	argv_array_pushl(&cp0.args, "-m", oid_to_hex(&info->i_tree), NULL);
> -	if (run_command(&cp0)) {
> +	set_alternate_index_output(stash_index_path.buf);
> +	if (reset_tree(&info->i_tree, 0, 0)) {
>  		ret = -1;
>  		goto done;
>  	}
> +	set_alternate_index_output(".git/index");

I think this second 'set_alternate_index_output()' should be
'set_alternate_index_output(NULL)', which has slightly different
semantics than setting it to '.git/index'.  Having it set means that
the index is written unconditionally even if it is not set.

Also the index file could be something other than ".git/index", if
the GIT_INDEX_FILE environment variable is set, so it should be
replaced with 'get_index_file()' if we were to keep this.

I was also wondering if we could avoid writing a temporary index to
disk, in 'stash_working_tree', but I don't see an easy way for doing
that.

>  
>  	cp1.git_cmd = 1;
>  	argv_array_pushl(&cp1.args, "diff-index", "--name-only", "-z",
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 17/26] stash: avoid spawning a "diff-index" process
  2018-08-08 18:58     ` [GSoC][PATCH v7 17/26] stash: avoid spawning a "diff-index" process Paul-Sebastian Ungureanu
@ 2018-08-18 22:06       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 22:06 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> This commits replaces spawning `diff-index` child process by using
> the already existing `diff` API

I think this should be squashed into the previous commit.  It's easier
to review a commit that replaces all the 'run_command'/'pipe_command'
calls in one function, rather than doing it call by call, especially
if they interact with eachother.

E.g. I was going to suggest replacing the 'write_tree' call as well,
but reading ahead in the series I see that that's already being done :)  

While replacing all the calls of one type with the internal API call
is probably easiest for writing the patches, at least I would find it
easier to review replacing the run-command API calls in one codepath
at a time.

> ---
>  builtin/stash--helper.c | 56 ++++++++++++++++++++++++++++++-----------
>  1 file changed, 42 insertions(+), 14 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 887b78d05..f905d3908 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -12,6 +12,7 @@
>  #include "rerere.h"
>  #include "revision.h"
>  #include "log-tree.h"
> +#include "diffcore.h"
>  
>  static const char * const git_stash_helper_usage[] = {
>  	N_("git stash--helper list [<options>]"),
> @@ -297,6 +298,18 @@ static int reset_head(const char *prefix)
>  	return run_command(&cp);
>  }
>  
> +static void add_diff_to_buf(struct diff_queue_struct *q,
> +			    struct diff_options *options,
> +			    void *data)
> +{
> +	int i;
> +	for (i = 0; i < q->nr; i++) {
> +		struct diff_filepair *p = q->queue[i];
> +		strbuf_addstr(data, p->one->path);
> +		strbuf_addch(data, '\n');

What about filenames that include a '\n'?  I think this in combination
with removing the '-z' flag from the 'update-index' call will break
with filenames that have a LF in them.  This should be a '\0' instead
of a '\n', and we should still be using the '-z' flag in
'update-index'.

> +	}
> +}
> +
>  static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
>  {
>  	struct child_process cp = CHILD_PROCESS_INIT;
> @@ -981,14 +994,16 @@ static int stash_patch(struct stash_info *info, const char **argv)
>  	return ret;
>  }
>  
> -static int stash_working_tree(struct stash_info *info, const char **argv)
> +static int stash_working_tree(struct stash_info *info,
> +			      const char **argv, const char *prefix)
>  {
>  	int ret = 0;
> -	struct child_process cp1 = CHILD_PROCESS_INIT;
>  	struct child_process cp2 = CHILD_PROCESS_INIT;
>  	struct child_process cp3 = CHILD_PROCESS_INIT;
> -	struct strbuf out1 = STRBUF_INIT;
>  	struct strbuf out3 = STRBUF_INIT;

We're left with cp{2,3} and out3 here, which is a bit weird.  Then
again renaming them in this patch adds more churn, making it harder to
review.  Maybe instead of numbering them it would be better to name
them after the child process they are calling?  e.g. 'cp1' could
become 'cp_di', and so on?

> +	struct argv_array args = ARGV_ARRAY_INIT;
> +	struct strbuf diff_output = STRBUF_INIT;
> +	struct rev_info rev;
>  
>  	set_alternate_index_output(stash_index_path.buf);
>  	if (reset_tree(&info->i_tree, 0, 0)) {
> @@ -997,26 +1012,36 @@ static int stash_working_tree(struct stash_info *info, const char **argv)
>  	}
>  	set_alternate_index_output(".git/index");
>  
> -	cp1.git_cmd = 1;
> -	argv_array_pushl(&cp1.args, "diff-index", "--name-only", "-z",
> -			"HEAD", "--", NULL);
> +	argv_array_push(&args, "dummy");

Not being familiar with the setup_revisions code, I had to dig a bit
to figure out why this makes sense.  This is a dummy replacement for
argv[0] in normal operation.  In retrospect it's kind of obvious, but
maybe call "dummy" "fake_argv0" instead, to help nudge future readers
in the right direction?

>  	if (argv)
> -		argv_array_pushv(&cp1.args, argv);
> -	argv_array_pushf(&cp1.env_array, "GIT_INDEX_FILE=%s",
> -			 stash_index_path.buf);
> +		argv_array_pushv(&args, argv);
> +	git_config(git_diff_basic_config, NULL);
> +	init_revisions(&rev, prefix);
> +	args.argc = setup_revisions(args.argc, args.argv, &rev, NULL);
> +
> +	rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
> +	rev.diffopt.format_callback = add_diff_to_buf;
> +	rev.diffopt.format_callback_data = &diff_output;
> +
> +	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
> +		ret = -1;
> +		goto done;
> +	}
>  
> -	if (pipe_command(&cp1, NULL, 0, &out1, 0, NULL, 0)) {
> +	add_pending_object(&rev, parse_object(the_repository, &info->b_commit), "");
> +	if (run_diff_index(&rev, 0)) {
>  		ret = -1;
>  		goto done;
>  	}
>  
>  	cp2.git_cmd = 1;
> -	argv_array_pushl(&cp2.args, "update-index", "-z", "--add",
> +	argv_array_pushl(&cp2.args, "update-index", "--add",
>  			 "--remove", "--stdin", NULL);
>  	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
>  			 stash_index_path.buf);
>  
> -	if (pipe_command(&cp2, out1.buf, out1.len, NULL, 0, NULL, 0)) {
> +	if (pipe_command(&cp2, diff_output.buf, diff_output.len,
> +			 NULL, 0, NULL, 0)) {
>  		ret = -1;
>  		goto done;
>  	}
> @@ -1033,8 +1058,11 @@ static int stash_working_tree(struct stash_info *info, const char **argv)
>  	get_oid_hex(out3.buf, &info->w_tree);
>  
>  done:
> -	strbuf_release(&out1);
> +	UNLEAK(rev);
>  	strbuf_release(&out3);
> +	argv_array_clear(&args);
> +	object_array_clear(&rev.pending);
> +	strbuf_release(&diff_output);
>  	remove_path(stash_index_path.buf);
>  	return ret;
>  }
> @@ -1112,7 +1140,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
>  			goto done;
>  		}
>  	} else {
> -		if (stash_working_tree(info, argv)) {
> +		if (stash_working_tree(info, argv, prefix)) {
>  			printf_ln("Cannot save the current worktree state");
>  			ret = -1;
>  			goto done;
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process
  2018-08-08 18:59     ` [GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process Paul-Sebastian Ungureanu
@ 2018-08-18 22:17       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 22:17 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> This commit replaces spawning `git ls-files` child process with
> API calls to get the untracked files.
> ---
>  builtin/stash--helper.c | 49 +++++++++++++++++++++++++++--------------
>  1 file changed, 32 insertions(+), 17 deletions(-)
> 
> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
> index 4fd79532c..5c27f5dcf 100644
> --- a/builtin/stash--helper.c
> +++ b/builtin/stash--helper.c
> @@ -813,27 +813,42 @@ static int store_stash(int argc, const char **argv, const char *prefix)
>  /*
>   * `out` will be filled with the names of untracked files. The return value is:
>   *
> - * < 0 if there was a bug (any arg given outside the repo will be detected
> - *     by `setup_revision()`)
>   * = 0 if there are not any untracked files
>   * > 0 if there are untracked files
>   */
> -static int get_untracked_files(const char **argv, int line_term,
> +static int get_untracked_files(const char **argv, const char *prefix,
>  			       int include_untracked, struct strbuf *out)
>  {
> -	struct child_process cp = CHILD_PROCESS_INIT;
> -	cp.git_cmd = 1;
> -	argv_array_pushl(&cp.args, "ls-files", "-o", NULL);
> -	if (line_term)
> -		argv_array_push(&cp.args, "-z");
> +	int max_len;
> +	int i;
> +	char *seen;
> +	struct dir_struct dir;
> +	struct pathspec pathspec;
> +
> +	memset(&dir, 0, sizeof(dir));
>  	if (include_untracked != 2)
> -		argv_array_push(&cp.args, "--exclude-standard");
> -	argv_array_push(&cp.args, "--");
> -	if (argv)
> -		argv_array_pushv(&cp.args, argv);
> +		setup_standard_excludes(&dir);
>  
> -	if (pipe_command(&cp, NULL, 0, out, 0, NULL, 0))
> -		return -1;
> +	parse_pathspec(&pathspec, 0,
> +		       PATHSPEC_PREFER_FULL,
> +		       prefix, argv);
> +	seen = xcalloc(pathspec.nr, 1);
> +
> +	max_len = fill_directory(&dir, the_repository->index, &pathspec);
> +	for (i = 0; i < dir.nr; i++) {
> +		struct dir_entry *ent = dir.entries[i];
> +		if (!dir_path_match(ent, &pathspec, max_len, seen)) {
> +			free(ent);
> +			continue;
> +		}
> +		strbuf_addf(out, "%s\n", ent->name);

As mentioned in my comments about the 'diff-index' replacement, this
'\n' should probably be '\0', and we should keep the '-z' flag in 'git
update-index', in case somebody has a '\n' in their filenames.

While creating such a file is probably not a good idea anyway, we
should still be able to handle it (and have been before this, so we
shouldn't break it).

> +		free(ent);
> +	}
> +
> +	free(dir.entries);
> +	free(dir.ignored);
> +	clear_directory(&dir);
> +	free(seen);
>  	return out->len;
>  }
>  
> @@ -888,7 +903,7 @@ static int check_changes(const char **argv, int include_untracked,
>  		goto done;
>  	}
>  
> -	if (include_untracked && get_untracked_files(argv, 0,
> +	if (include_untracked && get_untracked_files(argv, prefix,
>  						     include_untracked, &out))
>  		ret = 1;
>  
> @@ -908,7 +923,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
>  	struct child_process cp2 = CHILD_PROCESS_INIT;
>  
>  	cp.git_cmd = 1;
> -	argv_array_pushl(&cp.args, "update-index", "-z", "--add",
> +	argv_array_pushl(&cp.args, "update-index", "--add",
>  			 "--remove", "--stdin", NULL);
>  	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
>  			 stash_index_path.buf);
> @@ -1134,7 +1149,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,
>  		goto done;
>  	}
>  
> -	if (include_untracked && get_untracked_files(argv, 1,
> +	if (include_untracked && get_untracked_files(argv, prefix,
>  						     include_untracked, &out)) {
>  		if (save_untracked_files(info, &msg, &out)) {
>  			if (!quiet)
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()`
  2018-08-08 18:59     ` [GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()` Paul-Sebastian Ungureanu
@ 2018-08-18 22:33       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-18 22:33 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> This commits introduces a optimization by avoiding calling the
> same functions again. For example, `git stash push -u`
> would call at some points the following functions:
> 
>  * `check_changes()`
>  * `do_create_stash()`, which calls: `check_changes()` and
> `get_untracked_files()`
> 
> Note that `check_changes()` also calls `get_untracked_files()`.
> So, `check_changes()` is called 2 times and `get_untracked_files()`
> 3 times. By checking at the beginning of the function if we already
> performed a check, we can avoid making useless calls.

While I can see that this may give us some performance gains, what's
being described above sounds like we should look into why we are
making these duplicate calls in the first place, rather than trying to
return early from them.  I feel like the duplicate calls are mostly a
remnant from the way the shell script was written, but not inherent to
the design of 'git stash'. 

For example 'check_changes' could be called from 'create_stash'
directly, so we don't have to call it in 'do_create_stash', in the
process removing the duplicate call from the 'git stash push'
codepath.  That would provide the same improvements, and keep the code
cleaner, rather than introducing more special cases for these
functions.

I haven't looked into the 'get_untracked_files()' call chain yet, but
I imagine we can do something similar for that.

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls
  2018-08-08 18:59     ` [GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls Paul-Sebastian Ungureanu
@ 2018-08-19  8:17       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-19  8:17 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> This commit replaces spawning `git write-tree` with API calls.
> ---
>  builtin/stash.c | 40 ++++++++++++----------------------------
>  1 file changed, 12 insertions(+), 28 deletions(-)

Nice reduction in lines here!

> 
> diff --git a/builtin/stash.c b/builtin/stash.c
> index 4d5c0d16e..46e76a34e 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -949,9 +949,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)
>  {
>  	int ret = 0;
>  	struct strbuf untracked_msg = STRBUF_INIT;
> -	struct strbuf out2 = STRBUF_INIT;
>  	struct child_process cp = CHILD_PROCESS_INIT;
> -	struct child_process cp2 = CHILD_PROCESS_INIT;
> +	struct index_state state = { NULL };

We often call this 'istate' throughout the codebase.  Would be nice to
call it that here as well, to reduce the cognitive load for people
already familiar with the codebase.

>  
>  	cp.git_cmd = 1;
>  	argv_array_pushl(&cp.args, "update-index", "--add",
> @@ -966,15 +965,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)
>  		goto done;
>  	}
>  
> -	cp2.git_cmd = 1;
> -	argv_array_push(&cp2.args, "write-tree");
> -	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
> -			 stash_index_path.buf);
> -	if (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {
> +	if (write_index_as_tree(&info->u_tree, &state, stash_index_path.buf, 0,
> +				NULL)) {
>  		ret = -1;
>  		goto done;
>  	}
> -	get_oid_hex(out2.buf, &info->u_tree);
>  
>  	if (commit_tree(untracked_msg.buf, untracked_msg.len,
>  			&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
> @@ -984,7 +979,6 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)
>  
>  done:
>  	strbuf_release(&untracked_msg);
> -	strbuf_release(&out2);
>  	remove_path(stash_index_path.buf);
>  	return ret;
>  }
> @@ -994,11 +988,10 @@ static struct strbuf patch = STRBUF_INIT;
>  static int stash_patch(struct stash_info *info, const char **argv)
>  {
>  	int ret = 0;
> -	struct strbuf out2 = STRBUF_INIT;
>  	struct child_process cp0 = CHILD_PROCESS_INIT;
>  	struct child_process cp1 = CHILD_PROCESS_INIT;
> -	struct child_process cp2 = CHILD_PROCESS_INIT;
>  	struct child_process cp3 = CHILD_PROCESS_INIT;
> +	struct index_state state = { NULL };
>  
>  	remove_path(stash_index_path.buf);
>  
> @@ -1023,17 +1016,12 @@ static int stash_patch(struct stash_info *info, const char **argv)
>  		goto done;
>  	}
>  
> -	cp2.git_cmd = 1;
> -	argv_array_push(&cp2.args, "write-tree");
> -	argv_array_pushf(&cp2.env_array, "GIT_INDEX_FILE=%s",
> -			 stash_index_path.buf);
> -	if (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {
> +	if (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,
> +				NULL)) {
>  		ret = -1;
>  		goto done;
>  	}
>  
> -	get_oid_hex(out2.buf, &info->w_tree);
> -
>  	cp3.git_cmd = 1;
>  	argv_array_pushl(&cp3.args, "diff-tree", "-p", "HEAD",
>  			 oid_to_hex(&info->w_tree), "--", NULL);
> @@ -1046,7 +1034,6 @@ static int stash_patch(struct stash_info *info, const char **argv)
>  	}
>  
>  done:
> -	strbuf_release(&out2);
>  	remove_path(stash_index_path.buf);
>  	return ret;
>  }
> @@ -1056,11 +1043,10 @@ static int stash_working_tree(struct stash_info *info,
>  {
>  	int ret = 0;
>  	struct child_process cp2 = CHILD_PROCESS_INIT;
> -	struct child_process cp3 = CHILD_PROCESS_INIT;
> -	struct strbuf out3 = STRBUF_INIT;
>  	struct argv_array args = ARGV_ARRAY_INIT;
>  	struct strbuf diff_output = STRBUF_INIT;
>  	struct rev_info rev;
> +	struct index_state state = { NULL };
>  
>  	set_alternate_index_output(stash_index_path.buf);
>  	if (reset_tree(&info->i_tree, 0, 0)) {
> @@ -1103,20 +1089,18 @@ static int stash_working_tree(struct stash_info *info,
>  		goto done;
>  	}
>  
> -	cp3.git_cmd = 1;
> -	argv_array_push(&cp3.args, "write-tree");
> -	argv_array_pushf(&cp3.env_array, "GIT_INDEX_FILE=%s",
> -			 stash_index_path.buf);
> -	if (pipe_command(&cp3, NULL, 0, &out3, 0,NULL, 0)) {
> +	if (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,
> +				NULL)) {
> +
>  		ret = -1;
>  		goto done;
>  	}
>  
> -	get_oid_hex(out3.buf, &info->w_tree);
> +	discard_cache();
> +	read_cache();

This 'discard_cache()'/'read_cache()' pair surprises me a bit, and I
can't figure out why it's necessary now. 'write_index_as_tree()' reads
and writes from the index file at 'stash_index_path', while
'{discard,read}_cache()' operate on 'the_index', which should always be
distinct from the temporary index we are using here.  So this
shouldn't be needed, at least not because of the changes we are making
in this patch.

>  
>  done:
>  	UNLEAK(rev);
> -	strbuf_release(&out3);
>  	argv_array_clear(&args);
>  	object_array_clear(&rev.pending);
>  	strbuf_release(&diff_output);
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* Re: [GSoC][PATCH v7 26/26] stash: replace all "git apply" child processes with API calls
  2018-08-08 18:59     ` [GSoC][PATCH v7 26/26] stash: replace all "git apply" " Paul-Sebastian Ungureanu
@ 2018-08-19  8:40       ` Thomas Gummerer
  0 siblings, 0 replies; 181+ messages in thread
From: Thomas Gummerer @ 2018-08-19  8:40 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

On 08/08, Paul-Sebastian Ungureanu wrote:
> `apply_all_patches()` does not provide a method to apply patches
> from strbuf. Because of this, this commit introduces a new
> function `apply_patch_from_buf()` which applies a patch from buf.
> It works by saving the strbuf as a file. This way we can call
> `apply_all_patches()`. Before returning, the created file is
> removed.

I'm not a fan of this approach.  We're going from doing the operation
in memory to using a temporary file that we write to disk and have to
re-read afterwards, which I suspect might be slower than using the
'run-command' API.

From a quick look the 'apply_patch' function almost does what we want.
It reads the patch file, and then does everything else in memory.  It
seems to me that by factoring out reading the patch file from that
function, we should be able to use the rest to do the operation
in-memory here, which would be much nicer.

> ---
>  builtin/stash.c | 61 +++++++++++++++++++++++++++----------------------
>  1 file changed, 34 insertions(+), 27 deletions(-)
> 
> diff --git a/builtin/stash.c b/builtin/stash.c
> index 46e76a34e..74eda822c 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -13,6 +13,7 @@
>  #include "revision.h"
>  #include "log-tree.h"
>  #include "diffcore.h"
> +#include "apply.h"
>  
>  static const char * const git_stash_usage[] = {
>  	N_("git stash list [<options>]"),
> @@ -277,10 +278,6 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
>  	struct child_process cp = CHILD_PROCESS_INIT;
>  	const char *w_commit_hex = oid_to_hex(w_commit);
>  
> -	/*
> -	 * Diff-tree would not be very hard to replace with a native function,
> -	 * however it should be done together with apply_cached.
> -	 */

Hmm there was probably a good reason why we wrote this comment in the
first place.  I can't recall what that reason was, but we should
probably explore that.  If there was no reason for it, then we should
remove the comment where it was added in the series (since this is all
new code the comment comes from somewhere else in this series).

>  	cp.git_cmd = 1;
>  	argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
>  	argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
> @@ -288,18 +285,36 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
>  	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
>  }
>  
> -static int apply_cached(struct strbuf *out)
> +static int apply_patch_from_buf(struct strbuf *patch, int cached, int reverse,
> +				int check_index)
>  {
> -	struct child_process cp = CHILD_PROCESS_INIT;
> +	int ret = 0;
> +	struct apply_state state;
> +	struct argv_array args = ARGV_ARRAY_INIT;
> +	const char *patch_path = ".git/stash_patch.patch";

We should not rely on '.git/' here.  This will not work if 'GIT_DIR'
is set, or even in a worktree, where '.git' is just a file, not a
directory.

> +	FILE *patch_file;
>  
> -	/*
> -	 * Apply currently only reads either from stdin or a file, thus
> -	 * apply_all_patches would have to be updated to optionally take a
> -	 * buffer.
> -	 */

Ah and indeed the comment here is suggesting a very similar thing to
what I suggested above :)

> -	cp.git_cmd = 1;
> -	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
> -	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
> +	if (init_apply_state(&state, NULL))
> +		return -1;
> +
> +	state.cached = cached;
> +	state.apply_in_reverse = reverse;
> +	state.check_index = check_index;
> +	if (state.cached)
> +		state.check_index = 1;
> +	if (state.check_index)
> +		state.unsafe_paths = 0;
> +
> +	patch_file = fopen(patch_path, "w");
> +	strbuf_write(patch, patch_file);
> +	fclose(patch_file);
> +
> +	argv_array_push(&args, patch_path);
> +	ret = apply_all_patches(&state, args.argc, args.argv, 0);
> +
> +	remove_path(patch_path);
> +	clear_apply_state(&state);
> +	return ret;
>  }
>  
>  static int reset_head(const char *prefix)
> @@ -418,7 +433,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
>  				return -1;
>  			}
>  
> -			ret = apply_cached(&out);
> +			ret = apply_patch_from_buf(&out, 1, 0, 0);
>  			strbuf_release(&out);
>  			if (ret)
>  				return -1;
> @@ -1341,7 +1356,6 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
>  			int i;
>  			struct child_process cp1 = CHILD_PROCESS_INIT;
>  			struct child_process cp2 = CHILD_PROCESS_INIT;
> -			struct child_process cp3 = CHILD_PROCESS_INIT;
>  			struct strbuf out = STRBUF_INIT;
>  
>  			cp1.git_cmd = 1;
> @@ -1365,11 +1379,9 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
>  			if (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))
>  				return -1;
>  
> -			cp3.git_cmd = 1;
> -			argv_array_pushl(&cp3.args, "apply", "--index", "-R",
> -					 NULL);
> -			if (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,
> -					 0))
> +			discard_cache();
> +			read_cache();
> +			if (apply_patch_from_buf(&out, 0, 1, 1))
>  				return -1;
>  		} else {
>  			struct child_process cp = CHILD_PROCESS_INIT;
> @@ -1405,12 +1417,7 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,
>  				return -1;
>  		}
>  	} else {
> -		struct child_process cp = CHILD_PROCESS_INIT;
> -
> -		cp.git_cmd = 1;
> -		argv_array_pushl(&cp.args, "apply", "-R", NULL);
> -
> -		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
> +		if (apply_patch_from_buf(&patch, 0, 1, 0)) {
>  			if (!quiet)
>  				fprintf_ln(stderr, "Cannot remove worktree changes");
>  			return -1;
> -- 
> 2.18.0.573.g56500d98f
> 

^ permalink raw reply	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 00/20] Convert "git stash" to C builtin
  2018-06-25 16:42 ` [PATCH v6 0/4] stash: add new tests and introduce a new helper function Paul-Sebastian Ungureanu
                     ` (16 preceding siblings ...)
  2018-08-08 18:58   ` [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
@ 2018-08-30 21:40   ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 01/20] sha1-name.c: add `get_oidf()` which acts like `get_oid()` Paul-Sebastian Ungureanu
                       ` (23 more replies)
  17 siblings, 24 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Hello,

This a new iteration of `stash.c`. What is new?

 * Some commits got squashed. The commit related to replacing
 `git apply` child process was dropped since it wasn't the best
 idea.

 * In v7, there was a bug [1] related to config `git stash show`
 The bug was fixed and a test file was added for this.

 * Fixed `git stash [push]` [2]. In v7, `git stash -q drop` would
 act like `git stash push -q drop`.

 * Fixed coding-style nits. Verified that messages are marked
 for translation and are going to the correct output stream.

 * Fixed one memory leak (related to `strbuf_detach`).

 * Simplified the code a little bit.

[1]:
https://public-inbox.org/git/20180815210142.GN2734@hank.intra.tgummerer.com/

[2]:
https://public-inbox.org/git/20180818165102.GF11326@hank.intra.tgummerer.com/

Best regards,
Paul

Joel Teichroeb (5):
  stash: improve option parsing test coverage
  stash: convert apply to builtin
  stash: convert drop and clear to builtin
  stash: convert branch to builtin
  stash: convert pop to builtin

Paul-Sebastian Ungureanu (15):
  sha1-name.c: add `get_oidf()` which acts like `get_oid()`
  stash: update test cases conform to coding guidelines
  stash: rename test cases to be more descriptive
  stash: add tests for `git stash show` config
  stash: convert list to builtin
  stash: convert show to builtin
  stash: mention options in `show` synopsis.
  stash: convert store to builtin
  stash: convert create to builtin
  stash: convert push to builtin
  stash: make push -q quiet
  stash: convert save to builtin
  stash: convert `stash--helper.c` into `stash.c`
  stash: optimize `get_untracked_files()` and `check_changes()`
  stash: replace all `write-tree` child processes with API calls

 Documentation/git-stash.txt  |    4 +-
 Makefile                     |    2 +-
 builtin.h                    |    1 +
 builtin/stash.c              | 1563 ++++++++++++++++++++++++++++++++++
 cache.h                      |    1 +
 git-stash.sh                 |  752 ----------------
 git.c                        |    1 +
 sha1-name.c                  |   19 +
 t/t3903-stash.sh             |  192 +++--
 t/t3907-stash-show-config.sh |   81 ++
 10 files changed, 1795 insertions(+), 821 deletions(-)
 create mode 100644 builtin/stash.c
 delete mode 100755 git-stash.sh
 create mode 100755 t/t3907-stash-show-config.sh

-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 01/20] sha1-name.c: add `get_oidf()` which acts like `get_oid()`
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 02/20] stash: improve option parsing test coverage Paul-Sebastian Ungureanu
                       ` (22 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Compared to `get_oid()`, `get_oidf()` has as parameters
a pointer to `object_id`, a printf format string and
additional arguments. This will help simplify the code
in subsequent commits.

Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 cache.h     |  1 +
 sha1-name.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/cache.h b/cache.h
index b1fd3d58ab..d93b2e25a5 100644
--- a/cache.h
+++ b/cache.h
@@ -1309,6 +1309,7 @@ struct object_context {
 	GET_OID_BLOB)
 
 extern int get_oid(const char *str, struct object_id *oid);
+extern int get_oidf(struct object_id *oid, const char *fmt, ...);
 extern int get_oid_commit(const char *str, struct object_id *oid);
 extern int get_oid_committish(const char *str, struct object_id *oid);
 extern int get_oid_tree(const char *str, struct object_id *oid);
diff --git a/sha1-name.c b/sha1-name.c
index c9cc1318b7..261b960bbd 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1471,6 +1471,25 @@ int get_oid(const char *name, struct object_id *oid)
 	return get_oid_with_context(name, 0, oid, &unused);
 }
 
+/*
+ * This returns a non-zero value if the string (built using printf
+ * format and the given arguments) is not a valid object.
+ */
+int get_oidf(struct object_id *oid, const char *fmt, ...)
+{
+	va_list ap;
+	int ret;
+	struct strbuf sb = STRBUF_INIT;
+
+	va_start(ap, fmt);
+	strbuf_vaddf(&sb, fmt, ap);
+	va_end(ap);
+
+	ret = get_oid(sb.buf, oid);
+	strbuf_release(&sb);
+
+	return ret;
+}
 
 /*
  * Many callers know that the user meant to name a commit-ish by
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 02/20] stash: improve option parsing test coverage
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 01/20] sha1-name.c: add `get_oidf()` which acts like `get_oid()` Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 03/20] stash: update test cases conform to coding guidelines Paul-Sebastian Ungureanu
                       ` (21 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

In preparation for converting the stash command incrementally to
a builtin command, this patch improves test coverage of the option
parsing. Both for having too many parameters, or too few.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 1f871d3cca..af7586d43d 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '
 	test foo = "$(cat file/file)"
 '
 
+test_expect_success 'giving too many ref arguments does not modify files' '
+	git stash clear &&
+	test_when_finished "git reset --hard HEAD" &&
+	echo foo >file2 &&
+	git stash &&
+	echo bar >file2 &&
+	git stash &&
+	test-tool chmtime =123456789 file2 &&
+	for type in apply pop "branch stash-branch"
+	do
+		test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
+		test_i18ngrep "Too many revisions" err &&
+		test 123456789 = $(test-tool chmtime -g file2) || return 1
+	done
+'
+
+test_expect_success 'drop: too many arguments errors out (does nothing)' '
+	git stash list >expect &&
+	test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
+	test_i18ngrep "Too many revisions" err &&
+	git stash list >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'show: too many arguments errors out (does nothing)' '
+	test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
+	test_i18ngrep "Too many revisions" err &&
+	test_must_be_empty out
+'
+
 test_expect_success 'stash create - no changes' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
@@ -479,6 +509,11 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
+test_expect_success 'stash branch complains with no arguments' '
+	test_must_fail git stash branch 2>err &&
+	test_i18ngrep "No branch name specified" err
+'
+
 test_expect_success 'stash show format defaults to --stat' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 03/20] stash: update test cases conform to coding guidelines
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 01/20] sha1-name.c: add `get_oidf()` which acts like `get_oid()` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 02/20] stash: improve option parsing test coverage Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 22:11       ` Junio C Hamano
  2018-08-30 21:40     ` [GSoC][PATCH v8 04/20] stash: rename test cases to be more descriptive Paul-Sebastian Ungureanu
                       ` (20 subsequent siblings)
  23 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Removed whitespaces after redirection operators.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 59 deletions(-)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index af7586d43d..de6cab1fe7 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -8,22 +8,22 @@ test_description='Test git stash'
 . ./test-lib.sh
 
 test_expect_success 'stash some dirty working directory' '
-	echo 1 > file &&
+	echo 1 >file &&
 	git add file &&
 	echo unrelated >other-file &&
 	git add other-file &&
 	test_tick &&
 	git commit -m initial &&
-	echo 2 > file &&
+	echo 2 >file &&
 	git add file &&
-	echo 3 > file &&
+	echo 3 >file &&
 	test_tick &&
 	git stash &&
 	git diff-files --quiet &&
 	git diff-index --cached --quiet HEAD
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/file b/file
 index 0cfbf08..00750ed 100644
 --- a/file
@@ -35,7 +35,7 @@ EOF
 
 test_expect_success 'parents of stash' '
 	test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
-	git diff stash^2..stash > output &&
+	git diff stash^2..stash >output &&
 	test_cmp output expect
 '
 
@@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '
 
 test_expect_success 'apply stashed changes (including index)' '
 	git reset --hard HEAD^ &&
-	echo 6 > other-file &&
+	echo 6 >other-file &&
 	git add other-file &&
 	test_tick &&
 	git commit -m other-file &&
@@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '
 
 test_expect_success 'drop top stash' '
 	git reset --hard &&
-	git stash list > stashlist1 &&
-	echo 7 > file &&
+	git stash list >expected &&
+	echo 7 >file &&
 	git stash &&
 	git stash drop &&
-	git stash list > stashlist2 &&
-	test_cmp stashlist1 stashlist2 &&
+	git stash list >actual &&
+	test_cmp expected actual &&
 	git stash apply &&
 	test 3 = $(cat file) &&
 	test 1 = $(git show :file) &&
@@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '
 
 test_expect_success 'drop middle stash' '
 	git reset --hard &&
-	echo 8 > file &&
+	echo 8 >file &&
 	git stash &&
-	echo 9 > file &&
+	echo 9 >file &&
 	git stash &&
 	git stash drop stash@{1} &&
 	test 2 = $(git stash list | wc -l) &&
@@ -160,7 +160,7 @@ test_expect_success 'stash pop' '
 	test 0 = $(git stash list | wc -l)
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/file2 b/file2
 new file mode 100644
 index 0000000..1fe912c
@@ -170,7 +170,7 @@ index 0000000..1fe912c
 +bar2
 EOF
 
-cat > expect1 << EOF
+cat >expect1 <<EOF
 diff --git a/file b/file
 index 257cc56..5716ca5 100644
 --- a/file
@@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644
 +bar
 EOF
 
-cat > expect2 << EOF
+cat >expect2 <<EOF
 diff --git a/file b/file
 index 7601807..5716ca5 100644
 --- a/file
@@ -198,79 +198,79 @@ index 0000000..1fe912c
 EOF
 
 test_expect_success 'stash branch' '
-	echo foo > file &&
+	echo foo >file &&
 	git commit file -m first &&
-	echo bar > file &&
-	echo bar2 > file2 &&
+	echo bar >file &&
+	echo bar2 >file2 &&
 	git add file2 &&
 	git stash &&
-	echo baz > file &&
+	echo baz >file &&
 	git commit file -m second &&
 	git stash branch stashbranch &&
 	test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
 	test $(git rev-parse HEAD) = $(git rev-parse master^) &&
-	git diff --cached > output &&
+	git diff --cached >output &&
 	test_cmp output expect &&
-	git diff > output &&
+	git diff >output &&
 	test_cmp output expect1 &&
 	git add file &&
 	git commit -m alternate\ second &&
-	git diff master..stashbranch > output &&
+	git diff master..stashbranch >output &&
 	test_cmp output expect2 &&
 	test 0 = $(git stash list | wc -l)
 '
 
 test_expect_success 'apply -q is quiet' '
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	git stash apply -q > output.out 2>&1 &&
+	git stash apply -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'save -q is quiet' '
-	git stash save --quiet > output.out 2>&1 &&
+	git stash save --quiet >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'pop -q is quiet' '
-	git stash pop -q > output.out 2>&1 &&
+	git stash pop -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'pop -q --index works and is quiet' '
-	echo foo > file &&
+	echo foo >file &&
 	git add file &&
 	git stash save --quiet &&
-	git stash pop -q --index > output.out 2>&1 &&
+	git stash pop -q --index >output.out 2>&1 &&
 	test foo = "$(git show :file)" &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'drop -q is quiet' '
 	git stash &&
-	git stash drop -q > output.out 2>&1 &&
+	git stash drop -q >output.out 2>&1 &&
 	test_must_be_empty output.out
 '
 
 test_expect_success 'stash -k' '
-	echo bar3 > file &&
-	echo bar4 > file2 &&
+	echo bar3 >file &&
+	echo bar4 >file2 &&
 	git add file2 &&
 	git stash -k &&
 	test bar,bar4 = $(cat file),$(cat file2)
 '
 
 test_expect_success 'stash --no-keep-index' '
-	echo bar33 > file &&
-	echo bar44 > file2 &&
+	echo bar33 >file &&
+	echo bar44 >file2 &&
 	git add file2 &&
 	git stash --no-keep-index &&
 	test bar,bar2 = $(cat file),$(cat file2)
 '
 
 test_expect_success 'stash --invalid-option' '
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	test_must_fail git stash --invalid-option &&
 	test_must_fail git stash save --invalid-option &&
@@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	git stash branch stash-branch ${STASH_ID} &&
-	test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
+	test_when_finished "git reset --hard HEAD && git checkout master &&
+	git branch -D stash-branch" &&
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
@@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	git stash branch stash-branch ${STASH_ID} &&
-	test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
+	test_when_finished "git reset --hard HEAD && git checkout master &&
+	git branch -D stash-branch" &&
 	test $(git ls-files --modified | wc -l) -eq 1
 '
 
@@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	echo "1	0	file" >expected &&
@@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-	echo bar >> file &&
+	echo bar >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	echo "1	0	file" >expected &&
@@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD" &&
 	git reset --hard &&
-	echo foo >> file &&
+	echo foo >>file &&
 	STASH_ID=$(git stash create) &&
 	git reset --hard &&
 	cat >expected <<-EOF &&
@@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	echo bar > file &&
+	echo bar >file &&
 	git stash &&
 	test_must_fail git stash drop $(git rev-parse stash@{0}) &&
 	git stash pop &&
@@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
-	echo foo > file &&
+	echo foo >file &&
 	git stash &&
-	echo bar > file &&
+	echo bar >file &&
 	git stash &&
 	test_must_fail git stash pop $(git rev-parse stash@{0}) &&
 	git stash pop &&
@@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re
 
 test_expect_success 'ref with non-existent reflog' '
 	git stash clear &&
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	git stash &&
 	test_must_fail git rev-parse --quiet --verify does-not-exist &&
@@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '
 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash clear &&
 	test_must_fail git stash drop stash@{0} &&
-	echo bar5 > file &&
-	echo bar6 > file2 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
 	git add file2 &&
 	git stash &&
 	test_must_fail git stash drop stash@{1} &&
@@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu
 	test_i18ncmp expect actual
 '
 
-cat > expect << EOF
+cat >expect <<EOF
 diff --git a/HEAD b/HEAD
 new file mode 100644
 index 0000000..fe0cbee
@@ -737,14 +739,14 @@ EOF
 test_expect_success 'stash where working directory contains "HEAD" file' '
 	git stash clear &&
 	git reset --hard &&
-	echo file-not-a-ref > HEAD &&
+	echo file-not-a-ref >HEAD &&
 	git add HEAD &&
 	test_tick &&
 	git stash &&
 	git diff-files --quiet &&
 	git diff-index --cached --quiet HEAD &&
 	test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
-	git diff stash^..stash > output &&
+	git diff stash^..stash >output &&
 	test_cmp output expect
 '
 
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 04/20] stash: rename test cases to be more descriptive
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (2 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 03/20] stash: update test cases conform to coding guidelines Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config Paul-Sebastian Ungureanu
                       ` (19 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Rename some test cases' labels to be more descriptive and under 80
characters per line.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3903-stash.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index de6cab1fe7..3114c7bc4c 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	test_cmp expected actual
 '
 
-test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
+test_expect_success 'drop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
@@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r
 	git reset --hard HEAD
 '
 
-test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
+test_expect_success 'pop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
 	git reset --hard &&
@@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form "n", n >= N' '
 	git stash drop
 '
 
-test_expect_success 'stash branch should not drop the stash if the branch exists' '
+test_expect_success 'branch: do not drop the stash if the branch exists' '
 	git stash clear &&
 	echo foo >file &&
 	git add file &&
@@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists
 	git rev-parse stash@{0} --
 '
 
-test_expect_success 'stash branch should not drop the stash if the apply fails' '
+test_expect_success 'branch: should not drop the stash if the apply fails' '
 	git stash clear &&
 	git reset HEAD~1 --hard &&
 	echo foo >file &&
@@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'
 	git rev-parse stash@{0} --
 '
 
-test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
+test_expect_success 'apply: show same status as git status (relative to ./)' '
 	git stash clear &&
 	echo 1 >subdir/subfile1 &&
 	echo 2 >subdir/subfile2 &&
@@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '
 	test_i18ncmp expect actual
 '
 
-test_expect_success 'stash push with pathspec shows no changes when there are none' '
+test_expect_success 'push <pathspec>: show no changes when there are none' '
 	>foo &&
 	git add foo &&
 	git commit -m "tmp" &&
@@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no
 	test_i18ncmp expect actual
 '
 
-test_expect_success 'stash push with pathspec not in the repository errors out' '
+test_expect_success 'push: <pathspec> not in the repository errors out' '
 	>untracked &&
 	test_must_fail git stash push untracked &&
 	test_path_is_file untracked
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (3 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 04/20] stash: rename test cases to be more descriptive Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 22:07       ` Junio C Hamano
  2018-08-30 21:40     ` [GSoC][PATCH v8 06/20] stash: convert apply to builtin Paul-Sebastian Ungureanu
                       ` (18 subsequent siblings)
  23 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

This commit introduces tests for `git stash show`
config. It tests all the cases where `stash.showStat`
and `stash.showPatch` are unset or set to true / false.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 t/t3907-stash-show-config.sh | 81 ++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100755 t/t3907-stash-show-config.sh

diff --git a/t/t3907-stash-show-config.sh b/t/t3907-stash-show-config.sh
new file mode 100755
index 0000000000..8fe369c1a1
--- /dev/null
+++ b/t/t3907-stash-show-config.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+test_description='Test git stash show configuration.'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	test_commit file
+'
+
+# takes three parameters:
+# 1. the stash.showStat value (or "<unset>")
+# 2. the stash.showPatch value (or "<unset>")
+# 3. the diff options of the expected output (or nothing for no output)
+test_stat_and_patch () {
+	if test "<unset>" = "$1"
+	then
+		test_might_fail git config --unset stash.showStat
+	else
+		test_config stash.showStat "$1"
+	fi &&
+
+	if test "<unset>" = "$2"
+	then
+		test_might_fail git config --unset stash.showPatch
+	else
+		test_config stash.showPatch "$2"
+	fi &&
+
+	shift &&
+	shift &&
+	echo 2 >file.t &&
+	git diff "$@" >expect &&
+	git stash &&
+	git stash show >actual &&
+
+	if test -z "$1"
+	then
+		test_must_be_empty actual
+	else
+		test_cmp expect actual
+	fi
+}
+
+test_expect_success 'showStat unset showPatch unset' '
+	test_stat_and_patch "<unset>" "<unset>" --stat
+'
+
+test_expect_success 'showStat unset showPatch false' '
+	test_stat_and_patch "<unset>" false --stat
+'
+
+test_expect_success 'showStat unset showPatch true' '
+	test_stat_and_patch "<unset>" true --stat -p
+'
+
+test_expect_success 'showStat false showPatch unset' '
+	test_stat_and_patch false "<unset>"
+'
+
+test_expect_success 'showStat false showPatch false' '
+	test_stat_and_patch false false
+'
+
+test_expect_success 'showStat false showPatch true' '
+	test_stat_and_patch false true -p
+'
+
+test_expect_success 'showStat true showPatch unset' '
+	test_stat_and_patch true "<unset>" --stat
+'
+
+test_expect_success 'showStat true showPatch false' '
+	test_stat_and_patch true false --stat
+'
+
+test_expect_success 'showStat true showPatch true' '
+	test_stat_and_patch true true --stat -p
+'
+
+test_done
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 06/20] stash: convert apply to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (4 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 07/20] stash: convert drop and clear " Paul-Sebastian Ungureanu
                       ` (17 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add a builtin helper for performing stash commands. Converting
all at once proved hard to review, so starting with just apply
lets conversion get started without the other commands being
finished.

The helper is being implemented as a drop in replacement for
stash so that when it is complete it can simply be renamed and
the shell script deleted.

Delete the contents of the apply_stash shell function and replace
it with a call to stash--helper apply until pop is also
converted.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 .gitignore              |   1 +
 Makefile                |   1 +
 builtin.h               |   1 +
 builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |  78 +------
 git.c                   |   1 +
 6 files changed, 463 insertions(+), 71 deletions(-)
 create mode 100644 builtin/stash--helper.c

diff --git a/.gitignore b/.gitignore
index ffceea7d59..b59661cb88 100644
--- a/.gitignore
+++ b/.gitignore
@@ -157,6 +157,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
diff --git a/Makefile b/Makefile
index d03df31c2a..f900c68e69 100644
--- a/Makefile
+++ b/Makefile
@@ -1093,6 +1093,7 @@ BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-index.o
 BUILTIN_OBJS += builtin/show-ref.o
+BUILTIN_OBJS += builtin/stash--helper.o
 BUILTIN_OBJS += builtin/stripspace.o
 BUILTIN_OBJS += builtin/submodule--helper.o
 BUILTIN_OBJS += builtin/symbolic-ref.o
diff --git a/builtin.h b/builtin.h
index 99206df4bd..317bc338f7 100644
--- a/builtin.h
+++ b/builtin.h
@@ -223,6 +223,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
 extern int cmd_show_index(int argc, const char **argv, const char *prefix);
 extern int cmd_status(int argc, const char **argv, const char *prefix);
+extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
new file mode 100644
index 0000000000..fde795a764
--- /dev/null
+++ b/builtin/stash--helper.c
@@ -0,0 +1,452 @@
+#include "builtin.h"
+#include "config.h"
+#include "parse-options.h"
+#include "refs.h"
+#include "lockfile.h"
+#include "cache-tree.h"
+#include "unpack-trees.h"
+#include "merge-recursive.h"
+#include "argv-array.h"
+#include "run-command.h"
+#include "dir.h"
+#include "rerere.h"
+
+static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char * const git_stash_helper_apply_usage[] = {
+	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
+static const char *ref_stash = "refs/stash";
+static int quiet;
+static struct strbuf stash_index_path = STRBUF_INIT;
+
+/*
+ * w_commit is set to the commit containing the working tree
+ * b_commit is set to the base commit
+ * i_commit is set to the commit containing the index tree
+ * u_commit is set to the commit containing the untracked files tree
+ * w_tree is set to the working tree
+ * b_tree is set to the base tree
+ * i_tree is set to the index tree
+ * u_tree is set to the untracked files tree
+ */
+
+struct stash_info {
+	struct object_id w_commit;
+	struct object_id b_commit;
+	struct object_id i_commit;
+	struct object_id u_commit;
+	struct object_id w_tree;
+	struct object_id b_tree;
+	struct object_id i_tree;
+	struct object_id u_tree;
+	struct strbuf revision;
+	int is_stash_ref;
+	int has_u;
+};
+
+static void free_stash_info(struct stash_info *info)
+{
+	strbuf_release(&info->revision);
+}
+
+static void assert_stash_like(struct stash_info *info, const char *revision)
+{
+	if (get_oidf(&info->b_commit, "%s^1", revision) ||
+	    get_oidf(&info->w_tree, "%s:", revision) ||
+	    get_oidf(&info->b_tree, "%s^1:", revision) ||
+	    get_oidf(&info->i_tree, "%s^2:", revision)) {
+		free_stash_info(info);
+		error(_("'%s' is not a stash-like commit"), revision);
+		exit(128);
+	}
+}
+
+static int get_stash_info(struct stash_info *info, int argc, const char **argv)
+{
+	struct strbuf symbolic = STRBUF_INIT;
+	int ret;
+	const char *revision;
+	const char *commit = NULL;
+	char *end_of_rev;
+	char *expanded_ref;
+	struct object_id dummy;
+
+	if (argc > 1) {
+		int i;
+		struct strbuf refs_msg = STRBUF_INIT;
+		for (i = 0; i < argc; ++i)
+			strbuf_addf(&refs_msg, " '%s'", argv[i]);
+
+		fprintf_ln(stderr, _("Too many revisions specified:%s"),
+			   refs_msg.buf);
+		strbuf_release(&refs_msg);
+
+		return -1;
+	}
+
+	if (argc == 1)
+		commit = argv[0];
+
+	strbuf_init(&info->revision, 0);
+	if (!commit) {
+		if (!ref_exists(ref_stash)) {
+			free_stash_info(info);
+			fprintf_ln(stderr, _("No stash entries found."));
+			return -1;
+		}
+
+		strbuf_addf(&info->revision, "%s@{0}", ref_stash);
+	} else if (strspn(commit, "0123456789") == strlen(commit)) {
+		strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
+	} else {
+		strbuf_addstr(&info->revision, commit);
+	}
+
+	revision = info->revision.buf;
+
+	if (get_oid(revision, &info->w_commit)) {
+		error(_("%s is not a valid reference"), revision);
+		free_stash_info(info);
+		return -1;
+	}
+
+	assert_stash_like(info, revision);
+
+	info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
+
+	end_of_rev = strchrnul(revision, '@');
+	strbuf_add(&symbolic, revision, end_of_rev - revision);
+
+	ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
+	strbuf_release(&symbolic);
+	switch (ret) {
+	case 0: /* Not found, but valid ref */
+		info->is_stash_ref = 0;
+		break;
+	case 1:
+		info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
+		break;
+	default: /* Invalid or ambiguous */
+		free_stash_info(info);
+	}
+
+	free(expanded_ref);
+	return !(ret == 0 || ret == 1);
+}
+
+static int reset_tree(struct object_id *i_tree, int update, int reset)
+{
+	struct unpack_trees_options opts;
+	int nr_trees = 1;
+	struct tree_desc t[MAX_UNPACK_TREES];
+	struct tree *tree;
+	struct lock_file lock_file = LOCK_INIT;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+
+	memset(&opts, 0, sizeof(opts));
+
+	tree = parse_tree_indirect(i_tree);
+	if (parse_tree(tree))
+		return -1;
+
+	init_tree_desc(t, tree->buffer, tree->size);
+
+	opts.head_idx = 1;
+	opts.src_index = &the_index;
+	opts.dst_index = &the_index;
+	opts.merge = 1;
+	opts.reset = reset;
+	opts.update = update;
+	opts.fn = oneway_merge;
+
+	if (unpack_trees(nr_trees, t, &opts))
+		return -1;
+
+	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		return error(_("unable to write new index file"));
+
+	return 0;
+}
+
+static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *w_commit_hex = oid_to_hex(w_commit);
+
+	/*
+	 * Diff-tree would not be very hard to replace with a native function,
+	 * however it should be done together with apply_cached.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
+	argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
+
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int apply_cached(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Apply currently only reads either from stdin or a file, thus
+	 * apply_all_patches would have to be updated to optionally take a
+	 * buffer.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "apply", "--cached", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int reset_head(void)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Reset is overall quite simple, however there is no current public
+	 * API for resetting.
+	 */
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "reset");
+
+	return run_command(&cp);
+}
+
+static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const char *c_tree_hex = oid_to_hex(c_tree);
+
+	/*
+	 * diff-index is very similar to diff-tree above, and should be
+	 * converted together with update_index.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only",
+			 "--diff-filter=A", NULL);
+	argv_array_push(&cp.args, c_tree_hex);
+	return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
+}
+
+static int update_index(struct strbuf *out)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	/*
+	 * Update-index is very complicated and may need to have a public
+	 * function exposed in order to remove this forking.
+	 */
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
+	return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}
+
+static int restore_untracked(struct object_id *u_tree)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int res;
+
+	/*
+	 * We need to run restore files from a given index, but without
+	 * affecting the current index, so we use GIT_INDEX_FILE with
+	 * run_command to fork processes that will not interfere.
+	 */
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "read-tree");
+	argv_array_push(&cp.args, oid_to_hex(u_tree));
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (run_command(&cp)) {
+		remove_path(stash_index_path.buf);
+		return -1;
+	}
+
+	child_process_init(&cp);
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
+	argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+
+	res = run_command(&cp);
+	remove_path(stash_index_path.buf);
+	return res;
+}
+
+static int do_apply_stash(const char *prefix, struct stash_info *info,
+	int index)
+{
+	struct merge_options o;
+	struct object_id c_tree;
+	struct object_id index_tree;
+	const struct object_id *bases[1];
+	struct commit *result;
+	int ret;
+	int has_index = index;
+
+	read_cache_preload(NULL);
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))
+		return error(_("Cannot apply a stash in the middle of a merge"));
+
+	if (index) {
+		if (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree,
+			&info->i_tree)) {
+			has_index = 0;
+		} else {
+			struct strbuf out = STRBUF_INIT;
+
+			if (diff_tree_binary(&out, &info->w_commit)) {
+				strbuf_release(&out);
+				return -1;
+			}
+
+			ret = apply_cached(&out);
+			strbuf_release(&out);
+			if (ret)
+				return -1;
+
+			discard_cache();
+			read_cache();
+			if (write_cache_as_tree(&index_tree, 0, NULL))
+				return -1;
+
+			reset_head();
+		}
+	}
+
+	if (info->has_u && restore_untracked(&info->u_tree))
+		return error(_("Could not restore untracked files from stash"));
+
+	init_merge_options(&o);
+
+	o.branch1 = "Updated upstream";
+	o.branch2 = "Stashed changes";
+
+	if (!oidcmp(&info->b_tree, &c_tree))
+		o.branch1 = "Version stash was based on";
+
+	if (quiet)
+		o.verbosity = 0;
+
+	if (o.verbosity >= 3)
+		printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
+
+	bases[0] = &info->b_tree;
+
+	ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
+				      &result);
+	if (ret) {
+		rerere(0);
+
+		if (index)
+			fprintf_ln(stderr, _("Index was not unstashed."));
+
+		return ret;
+	}
+
+	if (has_index) {
+		if (reset_tree(&index_tree, 0, 0))
+			return -1;
+	} else {
+		struct strbuf out = STRBUF_INIT;
+
+		if (get_newly_staged(&out, &c_tree)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		if (reset_tree(&c_tree, 0, 1)) {
+			strbuf_release(&out);
+			return -1;
+		}
+
+		ret = update_index(&out);
+		strbuf_release(&out);
+		if (ret)
+			return -1;
+
+		discard_cache();
+	}
+
+	if (quiet) {
+		if (refresh_cache(REFRESH_QUIET))
+			warning("could not refresh index");
+	} else {
+		struct child_process cp = CHILD_PROCESS_INIT;
+
+		/*
+		 * Status is quite simple and could be replaced with calls to
+		 * wt_status in the future, but it adds complexities which may
+		 * require more tests.
+		 */
+		cp.git_cmd = 1;
+		cp.dir = prefix;
+		argv_array_push(&cp.args, "status");
+		run_command(&cp);
+	}
+
+	return 0;
+}
+
+static int apply_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0;
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_apply_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	ret = do_apply_stash(prefix, &info, index);
+	free_stash_info(&info);
+	return ret;
+}
+
+int cmd_stash__helper(int argc, const char **argv, const char *prefix)
+{
+	pid_t pid = getpid();
+	const char *index_file;
+
+	struct option options[] = {
+		OPT_END()
+	};
+
+	git_config(git_default_config, NULL);
+
+	argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
+			     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
+
+	index_file = get_index_file();
+	strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
+		    (uintmax_t)pid);
+
+	if (argc < 1)
+		usage_with_options(git_stash_helper_usage, options);
+	if (!strcmp(argv[0], "apply"))
+		return !!apply_stash(argc, argv, prefix);
+
+	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
+		      git_stash_helper_usage, options);
+}
diff --git a/git-stash.sh b/git-stash.sh
index 94793c1a91..809b1c2d1d 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -566,76 +566,11 @@ assert_stash_ref() {
 }
 
 apply_stash () {
-
-	assert_stash_like "$@"
-
-	git update-index -q --refresh || die "$(gettext "unable to refresh index")"
-
-	# current index state
-	c_tree=$(git write-tree) ||
-		die "$(gettext "Cannot apply a stash in the middle of a merge")"
-
-	unstashed_index_tree=
-	if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
-			test "$c_tree" != "$i_tree"
-	then
-		git diff-tree --binary $s^2^..$s^2 | git apply --cached
-		test $? -ne 0 &&
-			die "$(gettext "Conflicts in index. Try without --index.")"
-		unstashed_index_tree=$(git write-tree) ||
-			die "$(gettext "Could not save index tree")"
-		git reset
-	fi
-
-	if test -n "$u_tree"
-	then
-		GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
-		GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
-		rm -f "$TMPindex" ||
-		die "$(gettext "Could not restore untracked files from stash entry")"
-	fi
-
-	eval "
-		GITHEAD_$w_tree='Stashed changes' &&
-		GITHEAD_$c_tree='Updated upstream' &&
-		GITHEAD_$b_tree='Version stash was based on' &&
-		export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
-	"
-
-	if test -n "$GIT_QUIET"
-	then
-		GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
-	fi
-	if git merge-recursive $b_tree -- $c_tree $w_tree
-	then
-		# No conflict
-		if test -n "$unstashed_index_tree"
-		then
-			git read-tree "$unstashed_index_tree"
-		else
-			a="$TMP-added" &&
-			git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
-			git read-tree --reset $c_tree &&
-			git update-index --add --stdin <"$a" ||
-				die "$(gettext "Cannot unstage modified files")"
-			rm -f "$a"
-		fi
-		squelch=
-		if test -n "$GIT_QUIET"
-		then
-			squelch='>/dev/null 2>&1'
-		fi
-		(cd "$START_DIR" && eval "git status $squelch") || :
-	else
-		# Merge conflict; keep the exit status from merge-recursive
-		status=$?
-		git rerere
-		if test -n "$INDEX_OPTION"
-		then
-			gettextln "Index was not unstashed." >&2
-		fi
-		exit $status
-	fi
+	cd "$START_DIR"
+	git stash--helper apply "$@"
+	res=$?
+	cd_to_toplevel
+	return $res
 }
 
 pop_stash() {
@@ -713,7 +648,8 @@ push)
 	;;
 apply)
 	shift
-	apply_stash "$@"
+	cd "$START_DIR"
+	git stash--helper apply "$@"
 	;;
 clear)
 	shift
diff --git a/git.c b/git.c
index c27c38738b..3c0e762d7d 100644
--- a/git.c
+++ b/git.c
@@ -544,6 +544,7 @@ static struct cmd_struct commands[] = {
 	{ "show-index", cmd_show_index },
 	{ "show-ref", cmd_show_ref, RUN_SETUP },
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
 	{ "stripspace", cmd_stripspace },
 	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 07/20] stash: convert drop and clear to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (5 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 06/20] stash: convert apply to builtin Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 08/20] stash: convert branch " Paul-Sebastian Ungureanu
                       ` (16 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add the drop and clear commands to the builtin helper. These two
are each simple, but are being added together as they are quite
related.

We have to unfortunately keep the drop and clear functions in the
shell script as functions are called with parameters internally
that are not valid when the commands are called externally. Once
pop is converted they can both be removed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   4 +-
 2 files changed, 117 insertions(+), 2 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index fde795a764..cbe23fef11 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,7 +12,14 @@
 #include "rerere.h"
 
 static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper clear"),
+	NULL
+};
+
+static const char * const git_stash_helper_drop_usage[] = {
+	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
 };
 
@@ -21,6 +28,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_clear_usage[] = {
+	N_("git stash--helper clear"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -140,6 +152,31 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
 	return !(ret == 0 || ret == 1);
 }
 
+static int do_clear_stash(void)
+{
+	struct object_id obj;
+	if (get_oid(ref_stash, &obj))
+		return 0;
+
+	return delete_ref(NULL, ref_stash, &obj, 0);
+}
+
+static int clear_stash(int argc, const char **argv, const char *prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_clear_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (argc)
+		return error(_("git stash clear with parameters is unimplemented"));
+
+	return do_clear_stash();
+}
+
 static int reset_tree(struct object_id *i_tree, int update, int reset)
 {
 	struct unpack_trees_options opts;
@@ -424,6 +461,80 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int do_drop_stash(const char *prefix, struct stash_info *info)
+{
+	struct child_process cp_reflog = CHILD_PROCESS_INIT;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int ret;
+
+	/*
+	 * reflog does not provide a simple function for deleting refs. One will
+	 * need to be added to avoid implementing too much reflog code here
+	 */
+
+	cp_reflog.git_cmd = 1;
+	argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
+			 "--rewrite", NULL);
+	argv_array_push(&cp_reflog.args, info->revision.buf);
+	ret = run_command(&cp_reflog);
+	if (!ret) {
+		if (!quiet)
+			printf_ln(_("Dropped %s (%s)"), info->revision.buf,
+				  oid_to_hex(&info->w_commit));
+	} else {
+		return error(_("%s: Could not drop stash entry"),
+			     info->revision.buf);
+	}
+
+	/*
+	 * This could easily be replaced by get_oid, but currently it will throw
+	 * a fatal error when a reflog is empty, which we can not recover from.
+	 */
+	cp.git_cmd = 1;
+	/* Even though --quiet is specified, rev-parse still outputs the hash */
+	cp.no_stdout = 1;
+	argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
+	argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
+	ret = run_command(&cp);
+
+	/* do_clear_stash if we just dropped the last stash entry */
+	if (ret)
+		do_clear_stash();
+
+	return 0;
+}
+
+static void assert_stash_ref(struct stash_info *info)
+{
+	if (!info->is_stash_ref) {
+		free_stash_info(info);
+		error(_("'%s' is not a stash reference"), info->revision.buf);
+		exit(128);
+	}
+}
+
+static int drop_stash(int argc, const char **argv, const char *prefix)
+{
+	struct stash_info info;
+	int ret;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_drop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	assert_stash_ref(&info);
+
+	ret = do_drop_stash(prefix, &info);
+	free_stash_info(&info);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -446,6 +557,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_stash_helper_usage, options);
 	if (!strcmp(argv[0], "apply"))
 		return !!apply_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "clear"))
+		return !!clear_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "drop"))
+		return !!drop_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 809b1c2d1d..a99d5dc9e5 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -653,7 +653,7 @@ apply)
 	;;
 clear)
 	shift
-	clear_stash "$@"
+	git stash--helper clear "$@"
 	;;
 create)
 	shift
@@ -665,7 +665,7 @@ store)
 	;;
 drop)
 	shift
-	drop_stash "$@"
+	git stash--helper drop "$@"
 	;;
 pop)
 	shift
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 08/20] stash: convert branch to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (6 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 07/20] stash: convert drop and clear " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-09-03 13:29       ` Johannes Schindelin
  2018-08-30 21:40     ` [GSoC][PATCH v8 09/20] stash: convert pop " Paul-Sebastian Ungureanu
                       ` (15 subsequent siblings)
  23 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add stash branch to the helper and delete the apply_to_branch
function from the shell script.

Checkout does not currently provide a function for checking out
a branch as cmd_checkout does a large amount of sanity checks
first that we require here.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 44 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 17 ++--------------
 2 files changed, 46 insertions(+), 15 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index cbe23fef11..dadc028649 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -14,6 +14,7 @@
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	NULL
 };
@@ -28,6 +29,11 @@ static const char * const git_stash_helper_apply_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_branch_usage[] = {
+	N_("git stash--helper branch <branchname> [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_clear_usage[] = {
 	N_("git stash--helper clear"),
 	NULL
@@ -535,6 +541,42 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int branch_stash(int argc, const char **argv, const char *prefix)
+{
+	const char *branch = NULL;
+	int ret;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct stash_info info;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_branch_usage, 0);
+
+	if (!argc)
+		return error(_("No branch name specified"));
+
+	branch = argv[0];
+
+	if (get_stash_info(&info, argc - 1, argv + 1))
+		return -1;
+
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "checkout", "-b", NULL);
+	argv_array_push(&cp.args, branch);
+	argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
+	ret = run_command(&cp);
+	if (!ret)
+		ret = do_apply_stash(prefix, &info, 1);
+	if (!ret && info.is_stash_ref)
+		ret = do_drop_stash(prefix, &info);
+
+	free_stash_info(&info);
+
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -561,6 +603,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		return !!drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "branch"))
+		return !!branch_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index a99d5dc9e5..29d9f44255 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -598,20 +598,6 @@ drop_stash () {
 	clear_stash
 }
 
-apply_to_branch () {
-	test -n "$1" || die "$(gettext "No branch name specified")"
-	branch=$1
-	shift 1
-
-	set -- --index "$@"
-	assert_stash_like "$@"
-
-	git checkout -b $branch $REV^ &&
-	apply_stash "$@" && {
-		test -z "$IS_STASH_REF" || drop_stash "$@"
-	}
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -673,7 +659,8 @@ pop)
 	;;
 branch)
 	shift
-	apply_to_branch "$@"
+	cd "$START_DIR"
+	git stash--helper branch "$@"
 	;;
 *)
 	case $# in
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 09/20] stash: convert pop to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (7 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 08/20] stash: convert branch " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 10/20] stash: convert list " Paul-Sebastian Ungureanu
                       ` (14 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

From: Joel Teichroeb <joel@teichroeb.net>

Add stash pop to the helper and delete the pop_stash, drop_stash,
assert_stash_ref functions from the shell script now that they
are no longer needed.

Signed-off-by: Joel Teichroeb <joel@teichroeb.net>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-
 git-stash.sh            | 47 ++---------------------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index dadc028649..9fb1003dbb 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -13,7 +13,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
-	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
+	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
 	NULL
@@ -24,6 +24,11 @@ static const char * const git_stash_helper_drop_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_pop_usage[] = {
+	N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_apply_usage[] = {
 	N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
 	NULL
@@ -541,6 +546,33 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int pop_stash(int argc, const char **argv, const char *prefix)
+{
+	int index = 0, ret;
+	struct stash_info info;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_BOOL(0, "index", &index,
+			N_("attempt to recreate the index")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_pop_usage, 0);
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+
+	assert_stash_ref(&info);
+	if ((ret = do_apply_stash(prefix, &info, index)))
+		printf_ln(_("The stash entry is kept in case you need it again."));
+	else
+		ret = do_drop_stash(prefix, &info);
+
+	free_stash_info(&info);
+	return ret;
+}
+
 static int branch_stash(int argc, const char **argv, const char *prefix)
 {
 	const char *branch = NULL;
@@ -603,6 +635,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!clear_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "drop"))
 		return !!drop_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "pop"))
+		return !!pop_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "branch"))
 		return !!branch_stash(argc, argv, prefix);
 
diff --git a/git-stash.sh b/git-stash.sh
index 29d9f44255..8f2640fe90 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -554,50 +554,6 @@ assert_stash_like() {
 	}
 }
 
-is_stash_ref() {
-	is_stash_like "$@" && test -n "$IS_STASH_REF"
-}
-
-assert_stash_ref() {
-	is_stash_ref "$@" || {
-		args="$*"
-		die "$(eval_gettext "'\$args' is not a stash reference")"
-	}
-}
-
-apply_stash () {
-	cd "$START_DIR"
-	git stash--helper apply "$@"
-	res=$?
-	cd_to_toplevel
-	return $res
-}
-
-pop_stash() {
-	assert_stash_ref "$@"
-
-	if apply_stash "$@"
-	then
-		drop_stash "$@"
-	else
-		status=$?
-		say "$(gettext "The stash entry is kept in case you need it again.")"
-		exit $status
-	fi
-}
-
-drop_stash () {
-	assert_stash_ref "$@"
-
-	git reflog delete --updateref --rewrite "${REV}" &&
-		say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
-		die "$(eval_gettext "\${REV}: Could not drop stash entry")"
-
-	# clear_stash if we just dropped the last stash entry
-	git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
-	clear_stash
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -655,7 +611,8 @@ drop)
 	;;
 pop)
 	shift
-	pop_stash "$@"
+	cd "$START_DIR"
+	git stash--helper pop "$@"
 	;;
 branch)
 	shift
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 10/20] stash: convert list to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (8 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 09/20] stash: convert pop " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 11/20] stash: convert show " Paul-Sebastian Ungureanu
                       ` (13 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Add stash list to the helper and delete the list_stash function
from the shell script.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++
 git-stash.sh            |  7 +------
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 9fb1003dbb..c42a297078 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,6 +12,7 @@
 #include "rerere.h"
 
 static const char * const git_stash_helper_usage[] = {
+	N_("git stash--helper list [<options>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_list_usage[] = {
+	N_("git stash--helper list [<options>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_drop_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
@@ -609,6 +615,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
+static int list_stash(int argc, const char **argv, const char *prefix)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_list_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	if (!ref_exists(ref_stash))
+		return 0;
+
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
+			 "--first-parent", "-m", NULL);
+	argv_array_pushv(&cp.args, argv);
+	argv_array_push(&cp.args, ref_stash);
+	argv_array_push(&cp.args, "--");
+	return run_command(&cp);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -639,6 +668,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!pop_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "branch"))
 		return !!branch_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "list"))
+		return !!list_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 8f2640fe90..6052441aa2 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -382,11 +382,6 @@ have_stash () {
 	git rev-parse --verify --quiet $ref_stash >/dev/null
 }
 
-list_stash () {
-	have_stash || return 0
-	git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
-}
-
 show_stash () {
 	ALLOW_UNKNOWN_FLAGS=t
 	assert_stash_like "$@"
@@ -574,7 +569,7 @@ test -n "$seen_non_option" || set "push" "$@"
 case "$1" in
 list)
 	shift
-	list_stash "$@"
+	git stash--helper list "$@"
 	;;
 show)
 	shift
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 11/20] stash: convert show to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (9 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 10/20] stash: convert list " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 12/20] stash: mention options in `show` synopsis Paul-Sebastian Ungureanu
                       ` (12 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Add stash show to the helper and delete the show_stash, have_stash,
assert_stash_like, is_stash_like and parse_flags_and_rev functions
from the shell script now that they are no longer needed.

In shell version, although `git stash show` accepts `--index` and
`--quiet` options, it ignores them. In C, both options are passed
further to `git diff`.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c |  87 ++++++++++++++++++++++++++
 git-stash.sh            | 132 +---------------------------------------
 2 files changed, 88 insertions(+), 131 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index c42a297078..1dba7e6853 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -10,9 +10,12 @@
 #include "run-command.h"
 #include "dir.h"
 #include "rerere.h"
+#include "revision.h"
+#include "log-tree.h"
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
+	N_("git stash--helper show [<stash>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -25,6 +28,11 @@ static const char * const git_stash_helper_list_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_show_usage[] = {
+	N_("git stash--helper show [<stash>]"),
+	NULL
+};
+
 static const char * const git_stash_helper_drop_usage[] = {
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	NULL
@@ -638,6 +646,83 @@ static int list_stash(int argc, const char **argv, const char *prefix)
 	return run_command(&cp);
 }
 
+static int show_stat = 1;
+static int show_patch;
+
+static int git_stash_config(const char *var, const char *value, void *cb)
+{
+	if (!strcmp(var, "stash.showstat")) {
+		show_stat = git_config_bool(var, value);
+		return 0;
+	}
+	if (!strcmp(var, "stash.showpatch")) {
+		show_patch = git_config_bool(var, value);
+		return 0;
+	}
+	return git_default_config(var, value, cb);
+}
+
+static int show_stash(int argc, const char **argv, const char *prefix)
+{
+	int i;
+	int opts = 0;
+	int ret = 0;
+	struct stash_info info;
+	struct rev_info rev;
+	struct argv_array stash_args = ARGV_ARRAY_INIT;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	init_diff_ui_defaults();
+	git_config(git_diff_ui_config, NULL);
+	init_revisions(&rev, prefix);
+
+	for (i = 1; i < argc; ++i) {
+		if (argv[i][0] != '-')
+			argv_array_push(&stash_args, argv[i]);
+		else
+			opts++;
+	}
+
+	ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
+	argv_array_clear(&stash_args);
+	if (ret)
+		return -1;
+
+	/*
+	 * The config settings are applied only if there are not passed
+	 * any options.
+	 */
+	if (!opts) {
+		git_config(git_stash_config, NULL);
+		if (show_stat)
+			rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
+
+		if (show_patch)
+			rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
+
+		if (!show_stat && !show_patch) {
+			free_stash_info(&info);
+			return 0;
+		}
+	}
+
+	argc = setup_revisions(argc, argv, &rev, NULL);
+	if (argc > 1) {
+		free_stash_info(&info);
+		usage_with_options(git_stash_helper_show_usage, options);
+	}
+
+	rev.diffopt.flags.recursive = 1;
+	setup_diff_pager(&rev.diffopt);
+	diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
+	log_tree_diff_flush(&rev);
+
+	free_stash_info(&info);
+	return diff_result_code(&rev.diffopt, 0);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -670,6 +755,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!branch_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "list"))
 		return !!list_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "show"))
+		return !!show_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 6052441aa2..0d05cbc1e5 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -378,35 +378,6 @@ save_stash () {
 	fi
 }
 
-have_stash () {
-	git rev-parse --verify --quiet $ref_stash >/dev/null
-}
-
-show_stash () {
-	ALLOW_UNKNOWN_FLAGS=t
-	assert_stash_like "$@"
-
-	if test -z "$FLAGS"
-	then
-		if test "$(git config --bool stash.showStat || echo true)" = "true"
-		then
-			FLAGS=--stat
-		fi
-
-		if test "$(git config --bool stash.showPatch || echo false)" = "true"
-		then
-			FLAGS=${FLAGS}${FLAGS:+ }-p
-		fi
-
-		if test -z "$FLAGS"
-		then
-			return 0
-		fi
-	fi
-
-	git diff ${FLAGS} $b_commit $w_commit
-}
-
 show_help () {
 	exec git help stash
 	exit 1
@@ -448,107 +419,6 @@ show_help () {
 #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
 #
 
-parse_flags_and_rev()
-{
-	test "$PARSE_CACHE" = "$*" && return 0 # optimisation
-	PARSE_CACHE="$*"
-
-	IS_STASH_LIKE=
-	IS_STASH_REF=
-	INDEX_OPTION=
-	s=
-	w_commit=
-	b_commit=
-	i_commit=
-	u_commit=
-	w_tree=
-	b_tree=
-	i_tree=
-	u_tree=
-
-	FLAGS=
-	REV=
-	for opt
-	do
-		case "$opt" in
-			-q|--quiet)
-				GIT_QUIET=-t
-			;;
-			--index)
-				INDEX_OPTION=--index
-			;;
-			--help)
-				show_help
-			;;
-			-*)
-				test "$ALLOW_UNKNOWN_FLAGS" = t ||
-					die "$(eval_gettext "unknown option: \$opt")"
-				FLAGS="${FLAGS}${FLAGS:+ }$opt"
-			;;
-			*)
-				REV="${REV}${REV:+ }'$opt'"
-			;;
-		esac
-	done
-
-	eval set -- $REV
-
-	case $# in
-		0)
-			have_stash || die "$(gettext "No stash entries found.")"
-			set -- ${ref_stash}@{0}
-		;;
-		1)
-			:
-		;;
-		*)
-			die "$(eval_gettext "Too many revisions specified: \$REV")"
-		;;
-	esac
-
-	case "$1" in
-		*[!0-9]*)
-			:
-		;;
-		*)
-			set -- "${ref_stash}@{$1}"
-		;;
-	esac
-
-	REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
-		reference="$1"
-		die "$(eval_gettext "\$reference is not a valid reference")"
-	}
-
-	i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
-	set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
-	s=$1 &&
-	w_commit=$1 &&
-	b_commit=$2 &&
-	w_tree=$3 &&
-	b_tree=$4 &&
-	i_tree=$5 &&
-	IS_STASH_LIKE=t &&
-	test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
-	IS_STASH_REF=t
-
-	u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
-	u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
-}
-
-is_stash_like()
-{
-	parse_flags_and_rev "$@"
-	test -n "$IS_STASH_LIKE"
-}
-
-assert_stash_like() {
-	is_stash_like "$@" || {
-		args="$*"
-		die "$(eval_gettext "'\$args' is not a stash-like commit")"
-	}
-}
-
 test "$1" = "-p" && set "push" "$@"
 
 PARSE_CACHE='--not-parsed'
@@ -573,7 +443,7 @@ list)
 	;;
 show)
 	shift
-	show_stash "$@"
+	git stash--helper show "$@"
 	;;
 save)
 	shift
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 12/20] stash: mention options in `show` synopsis.
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (10 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 11/20] stash: convert show " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 13/20] stash: convert store to builtin Paul-Sebastian Ungureanu
                       ` (11 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Mention in the usage text and in the documentation, that `show`
accepts any option known to `git diff`.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 Documentation/git-stash.txt | 4 ++--
 builtin/stash--helper.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 7ef8c47911..e31ea7d303 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git stash' list [<options>]
-'git stash' show [<stash>]
+'git stash' show [<options>] [<stash>]
 'git stash' drop [-q|--quiet] [<stash>]
 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
 'git stash' branch <branchname> [<stash>]
@@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash
 The command takes options applicable to the 'git log'
 command to control what is shown and how. See linkgit:git-log[1].
 
-show [<stash>]::
+show [<options>] [<stash>]::
 
 	Show the changes recorded in the stash entry as a diff between the
 	stashed contents and the commit back when the stash entry was first
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 1dba7e6853..02b593e0cd 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -15,7 +15,7 @@
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
-	N_("git stash--helper show [<stash>]"),
+	N_("git stash--helper show [<options>] [<stash>]"),
 	N_("git stash--helper drop [-q|--quiet] [<stash>]"),
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
@@ -29,7 +29,7 @@ static const char * const git_stash_helper_list_usage[] = {
 };
 
 static const char * const git_stash_helper_show_usage[] = {
-	N_("git stash--helper show [<stash>]"),
+	N_("git stash--helper show [<options>] [<stash>]"),
 	NULL
 };
 
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 13/20] stash: convert store to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (11 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 12/20] stash: mention options in `show` synopsis Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-09-03 13:24       ` Johannes Schindelin
  2018-08-30 21:40     ` [GSoC][PATCH v8 14/20] stash: convert create " Paul-Sebastian Ungureanu
                       ` (10 subsequent siblings)
  23 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Add stash store to the helper and delete the store_stash function
from the shell script.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 55 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 43 ++------------------------------
 2 files changed, 57 insertions(+), 41 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 02b593e0cd..87568b0f34 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -58,6 +58,11 @@ static const char * const git_stash_helper_clear_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_store_usage[] = {
+	N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -723,6 +728,54 @@ static int show_stash(int argc, const char **argv, const char *prefix)
 	return diff_result_code(&rev.diffopt, 0);
 }
 
+static int do_store_stash(const char *w_commit, const char *stash_msg,
+			  int quiet)
+{
+	int ret = 0;
+	int need_to_free = 0;
+	struct object_id obj;
+
+	if (!stash_msg) {
+		need_to_free = 1;
+		stash_msg  = xstrdup("Created via \"git stash store\".");
+	}
+
+	ret = get_oid(w_commit, &obj);
+	if (!ret) {
+		ret = update_ref(stash_msg, ref_stash, &obj, NULL,
+				 REF_FORCE_CREATE_REFLOG,
+				 quiet ? UPDATE_REFS_QUIET_ON_ERR :
+				 UPDATE_REFS_MSG_ON_ERR);
+	}
+	if (ret && !quiet)
+		fprintf_ln(stderr, _("Cannot update %s with %s"),
+			   ref_stash, w_commit);
+	if (need_to_free)
+		free((char *) stash_msg);
+	return ret;
+}
+
+static int store_stash(int argc, const char **argv, const char *prefix)
+{
+	const char *stash_msg = NULL;
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+		OPT_STRING('m', "message", &stash_msg, "message", N_("stash message")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_store_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	if (argc != 1) {
+		fprintf_ln(stderr, _("\"git stash store\" requires one <commit> argument"));
+		return -1;
+	}
+
+	return do_store_stash(argv[0], stash_msg, quiet);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -757,6 +810,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!list_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "show"))
 		return !!show_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "store"))
+		return !!store_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 0d05cbc1e5..5739c51527 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -191,45 +191,6 @@ create_stash () {
 	die "$(gettext "Cannot record working tree state")"
 }
 
-store_stash () {
-	while test $# != 0
-	do
-		case "$1" in
-		-m|--message)
-			shift
-			stash_msg="$1"
-			;;
-		-m*)
-			stash_msg=${1#-m}
-			;;
-		--message=*)
-			stash_msg=${1#--message=}
-			;;
-		-q|--quiet)
-			quiet=t
-			;;
-		*)
-			break
-			;;
-		esac
-		shift
-	done
-	test $# = 1 ||
-	die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
-
-	w_commit="$1"
-	if test -z "$stash_msg"
-	then
-		stash_msg="Created via \"git stash store\"."
-	fi
-
-	git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
-	ret=$?
-	test $ret != 0 && test -z "$quiet" &&
-	die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
-	return $ret
-}
-
 push_stash () {
 	keep_index=
 	patch_mode=
@@ -308,7 +269,7 @@ push_stash () {
 		clear_stash || die "$(gettext "Cannot initialize stash")"
 
 	create_stash -m "$stash_msg" -u "$untracked" -- "$@"
-	store_stash -m "$stash_msg" -q $w_commit ||
+	git stash--helper store -m "$stash_msg" -q $w_commit ||
 	die "$(gettext "Cannot save the current status")"
 	say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
 
@@ -468,7 +429,7 @@ create)
 	;;
 store)
 	shift
-	store_stash "$@"
+	git stash--helper store "$@"
 	;;
 drop)
 	shift
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 14/20] stash: convert create to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (12 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 13/20] stash: convert store to builtin Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-09-03 16:00       ` Johannes Schindelin
  2018-08-30 21:40     ` [GSoC][PATCH v8 15/20] stash: convert push " Paul-Sebastian Ungureanu
                       ` (9 subsequent siblings)
  23 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Add stash create to the helper.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 430 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   2 +-
 2 files changed, 431 insertions(+), 1 deletion(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 87568b0f34..ce360a569d 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -12,6 +12,7 @@
 #include "rerere.h"
 #include "revision.h"
 #include "log-tree.h"
+#include "diffcore.h"
 
 static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper list [<options>]"),
@@ -63,6 +64,11 @@ static const char * const git_stash_helper_store_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_create_usage[] = {
+	N_("git stash--helper create [<message>]"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -290,6 +296,18 @@ static int reset_head(void)
 	return run_command(&cp);
 }
 
+static void add_diff_to_buf(struct diff_queue_struct *q,
+			    struct diff_options *options,
+			    void *data)
+{
+	int i;
+	for (i = 0; i < q->nr; i++) {
+		struct diff_filepair *p = q->queue[i];
+		strbuf_addstr(data, p->one->path);
+		strbuf_addch(data, 0);
+	}
+}
+
 static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
@@ -776,6 +794,416 @@ static int store_stash(int argc, const char **argv, const char *prefix)
 	return do_store_stash(argv[0], stash_msg, quiet);
 }
 
+/*
+ * `out` will be filled with the names of untracked files. The return value is:
+ *
+ * = 0 if there are not any untracked files
+ * > 0 if there are untracked files
+ */
+static int get_untracked_files(struct pathspec ps, int include_untracked,
+			       struct strbuf *out)
+{
+	int max_len;
+	int i;
+	char *seen;
+	struct dir_struct dir;
+
+	memset(&dir, 0, sizeof(dir));
+	if (include_untracked != 2)
+		setup_standard_excludes(&dir);
+
+	seen = xcalloc(ps.nr, 1);
+
+	max_len = fill_directory(&dir, the_repository->index, &ps);
+	for (i = 0; i < dir.nr; i++) {
+		struct dir_entry *ent = dir.entries[i];
+		if (!dir_path_match(&the_index, ent, &ps, max_len, seen)) {
+			free(ent);
+			continue;
+		}
+		strbuf_addf(out, "%s%c", ent->name, '\0');
+		free(ent);
+	}
+
+	free(dir.entries);
+	free(dir.ignored);
+	clear_directory(&dir);
+	free(seen);
+	return out->len;
+}
+
+/*
+ * The return value of `check_changes()` can be:
+ *
+ * < 0 if there was an error
+ * = 0 if there are no changes.
+ * > 0 if there are changes.
+ */
+static int check_changes(struct pathspec ps, int include_untracked)
+{
+	int result;
+	int ret = 0;
+	struct rev_info rev;
+	struct object_id dummy;
+	struct strbuf out = STRBUF_INIT;
+
+	init_revisions(&rev, NULL);
+	rev.prune_data = ps;
+
+	rev.diffopt.flags.quick = 1;
+	rev.diffopt.flags.ignore_submodules = 1;
+	rev.abbrev = 0;
+
+	/* No initial commit. */
+	if (get_oid("HEAD", &dummy))
+		return -1;
+
+	add_head_to_pending(&rev);
+	diff_setup_done(&rev.diffopt);
+
+	if (read_cache() < 0)
+		return 1;
+	result = run_diff_index(&rev, 1);
+	if (diff_result_code(&rev.diffopt, result))
+		return 1;
+
+	object_array_clear(&rev.pending);
+	result = run_diff_files(&rev, 0);
+	if (diff_result_code(&rev.diffopt, result))
+		return 1;
+
+	if (include_untracked && get_untracked_files(ps, include_untracked,
+						     &out)) {
+		strbuf_release(&out);
+		return 1;
+	}
+
+	strbuf_release(&out);
+	return 0;
+}
+
+static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
+				struct strbuf *in)
+{
+	int ret = 0;
+	struct strbuf untracked_msg = STRBUF_INIT;
+	struct strbuf out = STRBUF_INIT;
+	struct child_process cp_upd_index = CHILD_PROCESS_INIT;
+	struct child_process cp_write_tree = CHILD_PROCESS_INIT;
+
+	cp_upd_index.git_cmd = 1;
+	argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
+			 "--remove", "--stdin", NULL);
+	argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+
+	strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
+	if (pipe_command(&cp_upd_index, in->buf, in->len, NULL, 0, NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp_write_tree.git_cmd = 1;
+	argv_array_push(&cp_write_tree.args, "write-tree");
+	argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+	get_oid_hex(out.buf, &info->u_tree);
+
+	if (commit_tree(untracked_msg.buf, untracked_msg.len,
+			&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
+		ret = -1;
+		goto done;
+	}
+
+done:
+	strbuf_release(&untracked_msg);
+	strbuf_release(&out);
+	remove_path(stash_index_path.buf);
+	return ret;
+}
+
+static struct strbuf patch = STRBUF_INIT;
+
+static int stash_patch(struct stash_info *info, struct pathspec ps)
+{
+	int i;
+	int ret = 0;
+	struct strbuf out = STRBUF_INIT;
+	struct child_process cp_read_tree = CHILD_PROCESS_INIT;
+	struct child_process cp_add_i = CHILD_PROCESS_INIT;
+	struct child_process cp_write_tree = CHILD_PROCESS_INIT;
+	struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
+
+	remove_path(stash_index_path.buf);
+
+	cp_read_tree.git_cmd = 1;
+	argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
+	argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (run_command(&cp_read_tree)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp_add_i.git_cmd = 1;
+	argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
+			"--", NULL);
+	for (i = 0; i < ps.nr; ++i)
+		argv_array_push(&cp_add_i.args, ps.items[i].match);
+	argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (run_command(&cp_add_i)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp_write_tree.git_cmd = 1;
+	argv_array_push(&cp_write_tree.args, "write-tree");
+	argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	get_oid_hex(out.buf, &info->w_tree);
+
+	cp_diff_tree.git_cmd = 1;
+	argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
+			 oid_to_hex(&info->w_tree), "--", NULL);
+	if (pipe_command(&cp_diff_tree, NULL, 0, &patch, 0, NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	if (!patch.len) {
+		fprintf_ln(stderr, _("No changes selected"));
+		ret = 1;
+	}
+
+done:
+	strbuf_release(&out);
+	remove_path(stash_index_path.buf);
+	return ret;
+}
+
+static int stash_working_tree(struct stash_info *info, struct pathspec ps)
+{
+	int ret = 0;
+	struct child_process cp_upd_index = CHILD_PROCESS_INIT;
+	struct child_process cp_write_tree = CHILD_PROCESS_INIT;
+	struct strbuf out = STRBUF_INIT;
+	struct strbuf diff_output = STRBUF_INIT;
+	struct rev_info rev;
+
+	set_alternate_index_output(stash_index_path.buf);
+	if (reset_tree(&info->i_tree, 0, 0)) {
+		ret = -1;
+		goto done;
+	}
+	set_alternate_index_output(NULL);
+
+	git_config(git_diff_basic_config, NULL);
+	init_revisions(&rev, NULL);
+	rev.prune_data = ps;
+	rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
+	rev.diffopt.format_callback = add_diff_to_buf;
+	rev.diffopt.format_callback_data = &diff_output;
+
+	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
+		ret = -1;
+		goto done;
+	}
+
+	add_pending_object(&rev, parse_object(the_repository, &info->b_commit), "");
+	if (run_diff_index(&rev, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp_upd_index.git_cmd = 1;
+	argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
+			 "--remove", "--stdin", NULL);
+	argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+
+	if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
+			 NULL, 0, NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	cp_write_tree.git_cmd = 1;
+	argv_array_push(&cp_write_tree.args, "write-tree");
+	argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s",
+			 stash_index_path.buf);
+	if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {
+		ret = -1;
+		goto done;
+	}
+
+	get_oid_hex(out.buf, &info->w_tree);
+
+done:
+	UNLEAK(rev);
+	strbuf_release(&out);
+	object_array_clear(&rev.pending);
+	strbuf_release(&diff_output);
+	remove_path(stash_index_path.buf);
+	return ret;
+}
+
+static int do_create_stash(struct pathspec ps, const char **stash_msg,
+			   int include_untracked, int patch_mode,
+			   struct stash_info *info)
+{
+	int untracked_commit_option = 0;
+	int ret = 0;
+	int flags;
+	const char *head_short_sha1 = NULL;
+	const char *branch_ref = NULL;
+	const char *branch_name = "(no branch)";
+	struct commit *head_commit = NULL;
+	struct commit_list *parents = NULL;
+	struct strbuf msg = STRBUF_INIT;
+	struct strbuf commit_tree_label = STRBUF_INIT;
+	struct strbuf out = STRBUF_INIT;
+	struct strbuf stash_msg_buf = STRBUF_INIT;
+
+	read_cache_preload(NULL);
+	refresh_cache(REFRESH_QUIET);
+
+	if (!check_changes(ps, include_untracked)) {
+		ret = 1;
+		*stash_msg = NULL;
+		goto done;
+	}
+
+	if (get_oid("HEAD", &info->b_commit)) {
+		fprintf_ln(stderr, _("You do not have the initial commit yet"));
+		ret = -1;
+		*stash_msg = NULL;
+		goto done;
+	} else {
+		head_commit = lookup_commit(the_repository, &info->b_commit);
+	}
+
+	branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
+	if (flags & REF_ISSYMREF)
+		branch_name = strrchr(branch_ref, '/') + 1;
+	head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
+					     DEFAULT_ABBREV);
+	strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
+	pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
+
+	strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
+	commit_list_insert(head_commit, &parents);
+	if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
+	    commit_tree(commit_tree_label.buf, commit_tree_label.len,
+			&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
+		fprintf_ln(stderr, _("Cannot save the current index state"));
+		ret = -1;
+		*stash_msg = NULL;
+		goto done;
+	}
+
+	if (include_untracked && get_untracked_files(ps, include_untracked,
+						     &out)) {
+		if (save_untracked_files(info, &msg, &out)) {
+			fprintf_ln(stderr, _("Cannot save the untracked files"));
+			ret = -1;
+			*stash_msg = NULL;
+			goto done;
+		}
+		untracked_commit_option = 1;
+	}
+	if (patch_mode) {
+		ret = stash_patch(info, ps);
+		*stash_msg = NULL;
+		if (ret < 0) {
+			fprintf_ln(stderr, _("Cannot save the current worktree state"));
+			goto done;
+		} else if (ret > 0) {
+			goto done;
+		}
+	} else {
+		if (stash_working_tree(info, ps)) {
+			fprintf_ln(stderr, _("Cannot save the current worktree state"));
+			ret = -1;
+			*stash_msg = NULL;
+			goto done;
+		}
+	}
+
+	if (!*stash_msg || !strlen(*stash_msg))
+		strbuf_addf(&stash_msg_buf, "WIP on %s", msg.buf);
+	else
+		strbuf_addf(&stash_msg_buf, "On %s: %s", branch_name,
+			    *stash_msg);
+	*stash_msg = strbuf_detach(&stash_msg_buf, NULL);
+
+	/*
+	 * `parents` will be empty after calling `commit_tree()`, so there is
+	 * no need to call `free_commit_list()`
+	 */
+	parents = NULL;
+	if (untracked_commit_option)
+		commit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);
+	commit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);
+	commit_list_insert(head_commit, &parents);
+
+	if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
+			parents, &info->w_commit, NULL, NULL)) {
+		fprintf_ln(stderr, _("Cannot record working tree state"));
+		ret = -1;
+		goto done;
+	}
+
+done:
+	strbuf_release(&commit_tree_label);
+	strbuf_release(&msg);
+	strbuf_release(&out);
+	strbuf_release(&stash_msg_buf);
+	return ret;
+}
+
+static int create_stash(int argc, const char **argv, const char *prefix)
+{
+	int include_untracked = 0;
+	int ret = 0;
+	const char *stash_msg = NULL;
+	struct stash_info info;
+	struct pathspec ps;
+	struct option options[] = {
+		OPT_BOOL('u', "include-untracked", &include_untracked,
+			 N_("include untracked files in stash")),
+		OPT_STRING('m', "message", &stash_msg, N_("message"),
+			 N_("stash message")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_create_usage,
+			     0);
+
+	memset(&ps, 0, sizeof(ps));
+	ret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);
+
+	if (!ret)
+		printf_ln("%s", oid_to_hex(&info.w_commit));
+
+	/*
+	 * ret can be 1 if there were no changes. In this case, we should
+	 * not error out.
+	 */
+	free((char *) stash_msg);
+	return ret < 0;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -812,6 +1240,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!show_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "store"))
 		return !!store_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "create"))
+		return !!create_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index 5739c51527..ab06e4ffb8 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -425,7 +425,7 @@ clear)
 	;;
 create)
 	shift
-	create_stash -m "$*" && echo "$w_commit"
+	git stash--helper create --message "$*"
 	;;
 store)
 	shift
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 15/20] stash: convert push to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (13 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 14/20] stash: convert create " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-09-03 14:40       ` Johannes Schindelin
  2018-08-30 21:40     ` [GSoC][PATCH v8 16/20] stash: make push -q quiet Paul-Sebastian Ungureanu
                       ` (8 subsequent siblings)
  23 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Add stash push to the helper.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 229 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   6 +-
 2 files changed, 233 insertions(+), 2 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index ce360a569d..23670321d8 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -21,6 +21,9 @@ static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
 	N_("git stash--helper branch <branchname> [<stash>]"),
 	N_("git stash--helper clear"),
+	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
+	   "          [--] [<pathspec>...]]"),
 	NULL
 };
 
@@ -69,6 +72,13 @@ static const char * const git_stash_helper_create_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_push_usage[] = {
+	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
+	   "          [--] [<pathspec>...]]"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -1204,6 +1214,223 @@ static int create_stash(int argc, const char **argv, const char *prefix)
 	return ret < 0;
 }
 
+static void add_ps_items_to_argv_array(struct argv_array *args,
+				       struct pathspec ps) {
+	int i;
+	for (i = 0; i < ps.nr; ++i)
+		argv_array_push(args, ps.items[i].match);
+}
+
+static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
+			 int keep_index, int patch_mode, int include_untracked)
+{
+	int ret = 0;
+	struct stash_info info;
+	if (patch_mode && keep_index == -1)
+		keep_index = 1;
+
+	if (patch_mode && include_untracked) {
+		fprintf_ln(stderr, _("Can't use --patch and --include-untracked or --all at the same time"));
+		return -1;
+	}
+
+	read_cache_preload(NULL);
+	if (!include_untracked && ps.nr) {
+		int i;
+		char *ps_matched = xcalloc(ps.nr, 1);
+
+		for (i = 0; i < active_nr; ++i) {
+			const struct cache_entry *ce = active_cache[i];
+			ce_path_match(&the_index, ce, &ps, ps_matched);
+		}
+
+		if (report_path_error(ps_matched, &ps, NULL)) {
+			fprintf_ln(stderr, _("Did you forget to 'git add'?"));
+			return -1;
+		}
+		free(ps_matched);
+	}
+
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	if (!check_changes(ps, include_untracked)) {
+		printf_ln(_("No local changes to save"));
+		return 0;
+	}
+
+	if (!reflog_exists(ref_stash) && do_clear_stash()) {
+		fprintf_ln(stderr, _("Cannot initialize stash"));
+		return -1;
+	}
+
+	if (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,
+			    &info)) {
+		ret = -1;
+		goto done;
+	}
+
+	if (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {
+		fprintf(stderr, _("Cannot save the current status"));
+		ret = -1;
+		goto done;
+	}
+
+	printf_ln(_("Saved working directory and index state %s"), stash_msg);
+
+	if (!patch_mode) {
+		if (include_untracked && !ps.nr) {
+			struct child_process cp = CHILD_PROCESS_INIT;
+
+			cp.git_cmd = 1;
+			argv_array_pushl(&cp.args, "clean", "--force",
+					 "--quiet", "-d", NULL);
+			if (include_untracked == 2)
+				argv_array_push(&cp.args, "-x");
+			if (run_command(&cp)) {
+				ret = -1;
+				goto done;
+			}
+		}
+		if (ps.nr) {
+			struct child_process cp1 = CHILD_PROCESS_INIT;
+			struct child_process cp2 = CHILD_PROCESS_INIT;
+			struct child_process cp3 = CHILD_PROCESS_INIT;
+			struct strbuf out = STRBUF_INIT;
+
+			cp1.git_cmd = 1;
+			argv_array_push(&cp1.args, "add");
+			if (!include_untracked)
+				argv_array_push(&cp1.args, "-u");
+			if (include_untracked == 2)
+				argv_array_push(&cp1.args, "--force");
+			argv_array_push(&cp1.args, "--");
+			add_ps_items_to_argv_array(&cp1.args, ps);
+			if (run_command(&cp1)) {
+				ret = -1;
+				goto done;
+			}
+
+			cp2.git_cmd = 1;
+			argv_array_pushl(&cp2.args, "diff-index", "-p",
+					 "--cached", "--binary", "HEAD", "--",
+					 NULL);
+			add_ps_items_to_argv_array(&cp2.args, ps);
+			if (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0)) {
+				ret = -1;
+				goto done;
+			}
+
+			cp3.git_cmd = 1;
+			argv_array_pushl(&cp3.args, "apply", "--index", "-R",
+					 NULL);
+			if (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,
+					 0)) {
+				ret = -1;
+				goto done;
+			}
+		} else {
+			struct child_process cp = CHILD_PROCESS_INIT;
+			cp.git_cmd = 1;
+			argv_array_pushl(&cp.args, "reset", "--hard", "-q",
+					 NULL);
+			if (run_command(&cp)) {
+				ret = -1;
+				goto done;
+			}
+		}
+
+		if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
+			struct child_process cp1 = CHILD_PROCESS_INIT;
+			struct child_process cp2 = CHILD_PROCESS_INIT;
+			struct strbuf out = STRBUF_INIT;
+
+			if (reset_tree(&info.i_tree, 0, 1)) {
+				ret = -1;
+				goto done;
+			}
+
+			cp1.git_cmd = 1;
+			argv_array_pushl(&cp1.args, "ls-files", "-z",
+					 "--modified", "--", NULL);
+			add_ps_items_to_argv_array(&cp1.args, ps);
+			if (pipe_command(&cp1, NULL, 0, &out, 0, NULL, 0)) {
+				ret = -1;
+				goto done;
+			}
+
+			cp2.git_cmd = 1;
+			argv_array_pushl(&cp2.args, "checkout-index", "-z",
+					 "--force", "--stdin", NULL);
+			if (pipe_command(&cp2, out.buf, out.len, NULL, 0, NULL,
+					 0)) {
+				ret = -1;
+				goto done;
+			}
+		}
+	} else {
+		struct child_process cp = CHILD_PROCESS_INIT;
+
+		cp.git_cmd = 1;
+		argv_array_pushl(&cp.args, "apply", "-R", NULL);
+
+		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
+			fprintf_ln(stderr, _("Cannot remove worktree changes"));
+			ret = -1;
+			goto done;
+		}
+
+		if (keep_index < 1) {
+			int i;
+			struct child_process cp = CHILD_PROCESS_INIT;
+
+			cp.git_cmd = 1;
+			argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
+			for (i = 0; i < ps.nr; ++i)
+				argv_array_push(&cp.args, ps.items[i].match);
+			if (run_command(&cp)) {
+				ret = -1;
+				goto done;
+			}
+		}
+	}
+done:
+	free((char *) stash_msg);
+	return ret;
+}
+
+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_SET_INT('k', "keep-index", &keep_index,
+			N_("keep index"), 1),
+		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()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_push_usage,
+			     0);
+
+	parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
+	return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
+			     include_untracked);
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -1242,6 +1469,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!store_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "create"))
 		return !!create_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "push"))
+		return !!push_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index ab06e4ffb8..c3146f62ab 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -412,7 +412,8 @@ save)
 	;;
 push)
 	shift
-	push_stash "$@"
+	cd "$START_DIR"
+	git stash--helper push "$@"
 	;;
 apply)
 	shift
@@ -448,7 +449,8 @@ branch)
 *)
 	case $# in
 	0)
-		push_stash &&
+		cd "$START_DIR"
+		git stash--helper push &&
 		say "$(gettext "(To restore them type \"git stash apply\")")"
 		;;
 	*)
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 16/20] stash: make push -q quiet
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (14 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 15/20] stash: convert push " Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-08-30 21:40     ` [GSoC][PATCH v8 17/20] stash: convert save to builtin Paul-Sebastian Ungureanu
                       ` (7 subsequent siblings)
  23 siblings, 0 replies; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

There is a change in behaviour with this commit. When there was
no initial commit, the shell version of stash would still display
a message. This commit makes `push` to not display any message if
`--quiet` or `-q` is specified.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 47 ++++++++++++++++++++++++++---------------
 t/t3903-stash.sh        | 23 ++++++++++++++++++++
 2 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 23670321d8..e5153a63ea 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -938,7 +938,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
 
 static struct strbuf patch = STRBUF_INIT;
 
-static int stash_patch(struct stash_info *info, struct pathspec ps)
+static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)
 {
 	int i;
 	int ret = 0;
@@ -991,7 +991,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps)
 	}
 
 	if (!patch.len) {
-		fprintf_ln(stderr, _("No changes selected"));
+		if (!quiet)
+			fprintf_ln(stderr, _("No changes selected"));
 		ret = 1;
 	}
 
@@ -1069,7 +1070,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
 
 static int do_create_stash(struct pathspec ps, const char **stash_msg,
 			   int include_untracked, int patch_mode,
-			   struct stash_info *info)
+			   struct stash_info *info, int quiet)
 {
 	int untracked_commit_option = 0;
 	int ret = 0;
@@ -1094,7 +1095,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,
 	}
 
 	if (get_oid("HEAD", &info->b_commit)) {
-		fprintf_ln(stderr, _("You do not have the initial commit yet"));
+		if (!quiet)
+			fprintf_ln(stderr, _("You do not have the initial commit yet"));
 		ret = -1;
 		*stash_msg = NULL;
 		goto done;
@@ -1115,7 +1117,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,
 	if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
 	    commit_tree(commit_tree_label.buf, commit_tree_label.len,
 			&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
-		fprintf_ln(stderr, _("Cannot save the current index state"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot save the current index state"));
 		ret = -1;
 		*stash_msg = NULL;
 		goto done;
@@ -1124,7 +1127,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,
 	if (include_untracked && get_untracked_files(ps, include_untracked,
 						     &out)) {
 		if (save_untracked_files(info, &msg, &out)) {
-			fprintf_ln(stderr, _("Cannot save the untracked files"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot save the untracked files"));
 			ret = -1;
 			*stash_msg = NULL;
 			goto done;
@@ -1132,17 +1136,19 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,
 		untracked_commit_option = 1;
 	}
 	if (patch_mode) {
-		ret = stash_patch(info, ps);
+		ret = stash_patch(info, ps, quiet);
 		*stash_msg = NULL;
 		if (ret < 0) {
-			fprintf_ln(stderr, _("Cannot save the current worktree state"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot save the current worktree state"));
 			goto done;
 		} else if (ret > 0) {
 			goto done;
 		}
 	} else {
 		if (stash_working_tree(info, ps)) {
-			fprintf_ln(stderr, _("Cannot save the current worktree state"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot save the current worktree state"));
 			ret = -1;
 			*stash_msg = NULL;
 			goto done;
@@ -1168,7 +1174,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,
 
 	if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
 			parents, &info->w_commit, NULL, NULL)) {
-		fprintf_ln(stderr, _("Cannot record working tree state"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot record working tree state"));
 		ret = -1;
 		goto done;
 	}
@@ -1201,7 +1208,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
 			     0);
 
 	memset(&ps, 0, sizeof(ps));
-	ret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);
+	ret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info, 0);
 
 	if (!ret)
 		printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1255,28 +1262,33 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
 		return -1;
 
 	if (!check_changes(ps, include_untracked)) {
-		printf_ln(_("No local changes to save"));
+		if (!quiet)
+			printf_ln(_("No local changes to save"));
 		return 0;
 	}
 
 	if (!reflog_exists(ref_stash) && do_clear_stash()) {
-		fprintf_ln(stderr, _("Cannot initialize stash"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot initialize stash"));
 		return -1;
 	}
 
 	if (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,
-			    &info)) {
+			    &info, quiet)) {
 		ret = -1;
 		goto done;
 	}
 
 	if (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {
-		fprintf(stderr, _("Cannot save the current status"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot save the current status"));
 		ret = -1;
 		goto done;
 	}
 
-	printf_ln(_("Saved working directory and index state %s"), stash_msg);
+	if (!quiet)
+		printf_ln(_("Saved working directory and index state %s"),
+			stash_msg);
 
 	if (!patch_mode) {
 		if (include_untracked && !ps.nr) {
@@ -1375,7 +1387,8 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
 		argv_array_pushl(&cp.args, "apply", "-R", NULL);
 
 		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
-			fprintf_ln(stderr, _("Cannot remove worktree changes"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot remove worktree changes"));
 			ret = -1;
 			goto done;
 		}
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 3114c7bc4c..ed4611d3d8 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1064,6 +1064,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '
 	test_path_is_file untracked
 '
 
+test_expect_success 'push: -q is quiet with changes' '
+	>foo &&
+	git add foo &&
+	git stash push -q >output 2>&1 &&
+	test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet with no changes' '
+	git stash push -q >output 2>&1 &&
+	test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet even if there is no initial commit' '
+	git init foo_dir &&
+	test_when_finished rm -rf foo_dir &&
+	(
+		cd foo_dir &&
+		>bar &&
+		test_must_fail git stash push -q >output 2>&1 &&
+		test_must_be_empty output
+	)
+'
+
 test_expect_success 'untracked files are left in place when -u is not given' '
 	>file &&
 	git add file &&
-- 
2.19.0.rc0.22.gc26283d74e


^ permalink raw reply related	[flat|nested] 181+ messages in thread

* [GSoC][PATCH v8 17/20] stash: convert save to builtin
  2018-08-30 21:40   ` [GSoC][PATCH v8 00/20] " Paul-Sebastian Ungureanu
                       ` (15 preceding siblings ...)
  2018-08-30 21:40     ` [GSoC][PATCH v8 16/20] stash: make push -q quiet Paul-Sebastian Ungureanu
@ 2018-08-30 21:40     ` Paul-Sebastian Ungureanu
  2018-09-03 18:44       ` Johannes Schindelin
  2018-08-30 21:40     ` [GSoC][PATCH v8 18/20] stash: convert `stash--helper.c` into `stash.c` Paul-Sebastian Ungureanu
                       ` (6 subsequent siblings)
  23 siblings, 1 reply; 181+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-08-30 21:40 UTC (permalink / raw)
  To: git

Add stash save to the helper and delete functions which are no
longer needed (`show_help()`, `save_stash()`, `push_stash()`,
`create_stash()`, `clear_stash()`, `untracked_files()` and
`no_changes()`).

The `-m` option is no longer supported as it might not make
sense to have two ways of passing a message. Even if this is
a change in behaviour, the documentation remains the same
because the `-m` parameter was omitted before.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c |  52 +++++++
 git-stash.sh            | 311 +---------------------------------------
 2 files changed, 54 insertions(+), 309 deletions(-)

diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index e5153a63ea..1269f2548c 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -24,6 +24,8 @@ static const char * const git_stash_helper_usage[] = {
 	N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
 	   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
 	   "          [--] [<pathspec>...]]"),
+	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
 	NULL
 };
 
@@ -79,6 +81,12 @@ static const char * const git_stash_helper_push_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_helper_save_usage[] = {
+	N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
+	   "          [-u|--include-untracked] [-a|--all] [<message>]"),
+	NULL
+};
+
 static const char *ref_stash = "refs/stash";
 static int quiet;
 static struct strbuf stash_index_path = STRBUF_INIT;
@@ -1444,6 +1452,48 @@ static int push_stash(int argc, const char **argv, const char *prefix)
 			     include_untracked);
 }
 
+static int save_stash(int argc, const char **argv, const char *prefix)
+{
+	int i;
+	int keep_index = -1;
+	int patch_mode = 0;
+	int include_untracked = 0;
+	int quiet = 0;
+	int ret = 0;
+	const char *stash_msg = NULL;
+	char *to_free = NULL;
+	struct strbuf stash_msg_buf = STRBUF_INIT;
+	struct pathspec ps;
+	struct option options[] = {
+		OPT_SET_INT('k', "keep-index", &keep_index,
+			N_("keep index"), 1),
+		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_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_helper_save_usage,
+			     0);
+
+	for (i = 0; i < argc; ++i)
+		strbuf_addf(&stash_msg_buf, "%s ", argv[i]);
+	stash_msg = strbuf_detach(&stash_msg_buf, NULL);
+	to_free = (char *) stash_msg;
+
+	memset(&ps, 0, sizeof(ps));
+	ret = do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
+			    include_untracked);
+
+	free(to_free);
+	return ret;
+}
+
 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 {
 	pid_t pid = getpid();
@@ -1484,6 +1534,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
 		return !!create_stash(argc, argv, prefix);
 	else if (!strcmp(argv[0], "push"))
 		return !!push_stash(argc, argv, prefix);
+	else if (!strcmp(argv[0], "save"))
+		return !!save_stash(argc, argv, prefix);
 
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
 		      git_stash_helper_usage, options);
diff --git a/git-stash.sh b/git-stash.sh
index c3146f62ab..695f1feba3 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -36,314 +36,6 @@ else
        reset_color=
 fi
 
-no_changes () {
-	git diff-index --quiet --cached HEAD --ignore-submodules -- "$@" &&
-	git diff-files --quiet --ignore-submodules -- "$@" &&
-	(test -z "$untracked" || test -z "$(untracked_files "$@")")
-}
-
-untracked_files () {
-	if test "$1" = "-z"
-	then
-		shift
-		z=-z
-	else
-		z=
-	fi
-	excl_opt=--exclude-standard
-	test "$untracked" = "all" && excl_opt=
-	git ls-files -o $z $excl_opt -- "$@"
-}
-
-clear_stash () {
-	if test $# != 0
-	then
-		die "$(gettext "git stash clear with parameters is unimplemented")"
-	fi
-	if current=$(git rev-parse --verify --quiet $ref_stash)
-	then
-		git update-ref -d $ref_stash $current
-	fi
-}
-
-create_stash () {
-	stash_msg=
-	untracked=
-	while test $# != 0
-	do
-		case "$1" in
-		-m|--message)
-			shift
-			stash_msg=${1?"BUG: create_stash () -m requires an argument"}
-			;;
-		-m*)
-			stash_msg=${1#-m}
-			;;
-		--message=*)
-			stash_msg=${1#--message=}
-			;;
-		-u|--include-untracked)
-			shift
-			untracked=${1?"BUG: create_stash () -u requires an argument"}
-			;;
-		--)
-			shift
-			break
-			;;
-		esac
-		shift
-	done
-
-	git update-index -q --refresh
-	if no_changes "$@"
-	then
-		exit 0
-	fi
-
-	# state of the base commit
-	if b_commit=$(git rev-parse --verify HEAD)
-	then
-		head=$(git rev-list --oneline -n 1 HEAD --)
-	else
-		die "$(gettext "You do not have the initial commit yet")"
-	fi
-
-	if branch=$(git symbolic-ref -q HEAD)
-	then
-		branch=${branch#refs/heads/}
-	else
-		branch='(no branch)'
-	fi
-	msg=$(printf '%s: %s' "$branch" "$head")
-
-	# state of the index
-	i_tree=$(git write-tree) &&
-	i_commit=$(printf 'index on %s\n' "$msg" |
-		git commit-tree $i_tree -p $b_commit) ||
-		die "$(gettext "Cannot save the current index state")"
-
-	if test -n "$untracked"
-	then
-		# Untracked files are stored by themselves in a parentless commit, for
-		# ease of unpacking later.
-		u_commit=$(
-			untracked_files -z "$@" | (
-				GIT_INDEX_FILE="$TMPindex" &&
-				export GIT_INDEX_FILE &&
-				rm -f "$TMPindex" &&
-				git update-index -z --add --remove --stdin &&
-				u_tree=$(git write-tree) &&
-				printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree  &&
-				rm -f "$TMPindex"
-		) ) || die "$(gettext "Cannot save the untracked files")"
-
-		untracked_commit_option="-p $u_commit";
-	else
-		untracked_commit_option=
-	fi
-
-	if test -z "$patch_mode"
-	then
-
-		# state of the working tree
-		w_tree=$( (
-			git read-tree --index-output="$TMPindex" -m $i_tree &&
-			GIT_INDEX_FILE="$TMPindex" &&
-			export GIT_INDEX_FILE &&
-			git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" &&
-			git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
-			git write-tree &&
-			rm -f "$TMPindex"
-		) ) ||
-			die "$(gettext "Cannot save the current worktree state")"
-
-	else
-
-		rm -f "$TMP-index" &&
-		GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
-
-		# find out what the user wants
-		GIT_INDEX_FILE="$TMP-index" \
-			git add--interactive --patch=stash -- "$@" &&
-
-		# state of the working tree
-		w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
-		die "$(gettext "Cannot save the current worktree state")"
-
-		git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
-		test -s "$TMP-patch" ||
-		die "$(gettext "No changes selected")"
-
-		rm -f "$TMP-index" ||
-		die "$(gettext "Cannot remove temporary index (can't happen)")"
-
-	fi
-
-	# create the stash
-	if test -z "$stash_msg"
-	then
-		stash_msg=$(printf 'WIP on %s' "$msg")
-	else
-		stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
-	fi
-	w_commit=$(printf '%s\n' "$stash_msg" |
-	git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
-	die "$(gettext "Cannot record working tree state")"
-}
-
-push_stash () {
-	keep_index=
-	patch_mode=
-	untracked=
-	stash_msg=
-	while test $# != 0
-	do
-		case "$1" in
-		-k|--keep-index)
-			keep_index=t
-			;;
-		--no-keep-index)
-			keep_index=n
-			;;
-		-p|--patch)
-			patch_mode=t
-			# only default to keep if we don't already have an override
-			test -z "$keep_index" && keep_index=t
-			;;
-		-q|--quiet)
-			GIT_QUIET=t
-			;;
-		-u|--include-untracked)
-			untracked=untracked
-			;;
-		-a|--all)
-			untracked=all
-			;;
-		-m|--message)
-			shift
-			test -z ${1+x} && usage
-			stash_msg=$1
-			;;
-		-m*)
-			stash_msg=${1#-m}
-			;;
-		--message=*)
-			stash_msg=${1#--message=}
-			;;
-		--help)
-			show_help
-			;;
-		--)
-			shift
-			break
-			;;
-		-*)
-			option="$1"
-			eval_gettextln "error: unknown option for 'stash push': \$option"
-			usage
-			;;
-		*)
-			break
-			;;
-		esac
-		shift
-	done
-
-	eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
-
-	if test -n "$patch_mode" && test -n "$untracked"
-	then
-		die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"
-	fi
-
-	test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1
-
-	git update-index -q --refresh
-	if no_changes "$@"
-	then
-		say "$(gettext "No local changes to save")"
-		exit 0
-	fi
-
-	git reflog exists $ref_stash ||
-		clear_stash || die "$(gettext "Cannot initialize stash")"
-
-	create_stash -m "$stash_msg" -u "$untracked" -- "$@"
-	git stash--helper store -m "$stash_msg" -q $w_commit ||
-	die "$(gettext "Cannot save the current status")"
-	say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
-
-	if test -z "$patch_mode"
-	then
-		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
-		if test -n "$untracked" && test $# = 0
-		then
-			git clean --force --quiet -d $CLEAN_X_OPTION
-		fi
-
-		if test $# != 0
-		then
-			test -z "$untracked" && UPDATE_OPTION="-u" || UPDATE_OPTION=
-			test "$untracked" = "all" && FORCE_OPTION="--force" || FORCE_OPTION=
-			git add $UPDATE_OPTION $FORCE_OPTION -- "$@"
-			git diff-index -p --cached --binary HEAD -- "$@" |
-			git apply --index -R
-		else
-			git rese