Skip to contentSkip to conversation

Convert some stash functionality to a builtin

PATCH In review Joel Teichroeb 9 participants 181 messages Apr 5, 2018 – Nov 9, 2018 1 message not in the archive Page 1 of 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover Convert some stash functionality to a builtin

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/[email protected]/
v3: https://public-inbox.org/git/[email protected]/
v4: https://public-inbox.org/git/[email protected]/

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
Signature · 1 line
2.16.3
#1 lore raw

PATCH v5 1/5 stash: improve option parsing test coverage

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.

---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
t/t3903-stash.sh +35 -0
index aefde7b172..4eaa4cae9a 100755
@@ -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" &&
Signature · 1 line
2.16.3
#2 lore raw

Re: [PATCH v5 1/5] stash: improve option parsing test coverage

Hi Joel,

Quoted 3 lines · 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.

Very good.

Quoted 10 lines
---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
t/t3903-stash.sh +0 -0
index aefde7b172..4eaa4cae9a 100755

t/t3903-stash.sh test_expect_failure 'stash file to directory' ' · line 447

Quoted 4 lines
t/t3903-stash.sh +1 -0
@@ -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/

t/t3903-stash.sh test_expect_failure 'stash file to directory' ' · line 459

Quoted 12 lines
t/t3903-stash.sh +12 -0
+	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 ;-)

t/t3903-stash.sh test_expect_failure 'stash file to directory' ' · line 463

Quoted 4 lines
t/t3903-stash.sh +4 -0
+	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)' '

t/t3903-stash.sh test_expect_failure 'stash file to directory' ' · line 464

Quoted 1 line
t/t3903-stash.sh +1 -0
+	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).

t/t3903-stash.sh test_expect_failure 'stash file to directory' ' · line 469

Quoted 5 lines
t/t3903-stash.sh +5 -0
+	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.

t/t3903-stash.sh test_expect_failure 'stash file to directory' ' · line 471

Quoted 2 lines
t/t3903-stash.sh +2 -0
+
+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

#3 lore raw

Re: [PATCH v5 1/5] stash: improve option parsing test coverage

Quoted 9 lines · Johannes Schindelin
Quoted 9 lines
t/t3903-stash.sh +9 -0
+	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
Quoted 8 lines

t/t3903-stash.sh test_expect_failure 'stash file to directory' ' · line 459

Quoted 3 lines
t/t3903-stash.sh +3 -0
+		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/[email protected]/

Best regards,
Paul Ungureanu

#4 lore raw

Re: [PATCH v5 1/5] stash: improve option parsing test coverage

Hi Paul,

Quoted 20 lines · Paul-Sebastian Ungureanu
Quoted 10 lines · Johannes Schindelin
Quoted 10 lines
t/t3903-stash.sh +10 -0
+	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 &&
Quoted 8 lines
Quoted 3 lines
(quoted diff) +2 -0
+		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 ...).

Quoted 6 lines

Hi,

I submitted a patch for this [1].

[1]
https://public-inbox.org/git/[email protected]/

Excellent, thank you so much!
Johannes

#5 lore raw

PATCH v5 2/5 stash: convert apply to builtin

In reply to 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.

---
 .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
.gitignore +1 -0
index 833ef3b0b7..296d5f376d 100644
@@ -152,6 +152,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
Makefile +1 -0
index 96f6138f63..6cfdbe9a32 100644
@@ -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
builtin.h +1 -0
index 42378f3aa4..a14fd85b0e 100644
@@ -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);
builtin/stash--helper.c new file +431 -0

431 lines not on this page · raw message

git-stash.sh +5 -70

81 lines not on this page · raw message

git.c +1 -0
index ceaa58ef40..6ffe6364ac 100644
@@ -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},
Signature · 1 line
2.16.3
#6 lore raw

Re: [PATCH v5 2/5] stash: convert apply to builtin

Quoted 143 lines · 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.

---
 .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
.gitignore +1 -0
index 833ef3b0b7..296d5f376d 100644
@@ -152,6 +152,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
Makefile +1 -0
index 96f6138f63..6cfdbe9a32 100644
@@ -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
builtin.h +1 -0
index 42378f3aa4..a14fd85b0e 100644
@@ -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);
builtin/stash--helper.c new file +77 -0
new file mode 100644
index 0000000000..9d00a9547d
@@ -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;

builtin/stash--helper.c line 87

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+       }
+
+       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.

Quoted 5 lines
builtin/stash--helper.c new file +5 -0
+               }
+
+               strbuf_addf(&info->revision, "%s@{0}", ref_stash);
+       } else if (strspn(commit, "0123456789") == strlen(commit)) {
+               strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);

builtin/stash--helper.c line 102

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+       } 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.

builtin/stash--helper.c line 112

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+       }
+
+       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.

Quoted 61 lines
builtin/stash--helper.c new file +61 -0
+       }
+
+       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;

builtin/stash--helper.c line 183

Quoted 10 lines
builtin/stash--helper.c new file +10 -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. */

Quoted 4 lines
builtin/stash--helper.c new file +4 -0
+       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);
+

builtin/stash--helper.c line 197

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+       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.

Quoted 4 lines
builtin/stash--helper.c new file +4 -0
+       cp.git_cmd = 1;
+       argv_array_pushl(&cp.args, "apply", "--cached", NULL);
+       return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
+}

[...]

builtin/stash--helper.c line 249

Quoted 10 lines
builtin/stash--helper.c new file +10 -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.

Quoted 56 lines
builtin/stash--helper.c new file +56 -0
+       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))

builtin/stash--helper.c line 315

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+                               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"));
Quoted 45 lines
builtin/stash--helper.c new file +45 -0
+       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)

builtin/stash--helper.c line 371

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+                       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.

Quoted 6 lines
builtin/stash--helper.c new file +6 -0
+               argv_array_push(&args, "status");
+               cmd_status(args.argc, args.argv, prefix);
+       }
+
+       return 0;
+}

[...]

Quoted 10 lines
builtin/stash--helper.c new file +10 -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()
+       };
+

builtin/stash--helper.c line 421

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+       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) ...

Quoted 1 line

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

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

builtin/stash--helper.c line 427

Quoted 5 lines
builtin/stash--helper.c new file +5 -0
+               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.

builtin/stash--helper.c line 431

Quoted 4 lines
builtin/stash--helper.c new file +4 -0
+       }
+
+       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);
}
#7 lore raw

Re: [PATCH v5 2/5] stash: convert apply to builtin

Quoted 6 lines · Christian Couder

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().

Quoted 8 lines
       }

       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);
}
#8 lore raw

Re: [PATCH v5 2/5] stash: convert apply to builtin

Quoted 18 lines · Christian Couder
Quoted 6 lines · Christian Couder

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().

Quoted 7 lines
       }

       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".

#9 lore raw

Re: [PATCH v5 2/5] stash: convert apply to builtin

In reply to Christian Couder

Hi Christian,

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

Quoted 13 lines · Christian Couder

builtin/stash--helper.c line 102

Quoted 9 lines · Joel Teichroeb

[...]

builtin/stash--helper.c new file +7 -0
+
+       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:

Quoted 4 lines
               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.

Quoted 10 lines

builtin/stash--helper.c line 112

Quoted 8 lines
builtin/stash--helper.c new file +8 -0
+       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.

Quoted 9 lines

builtin/stash--helper.c line 315

Quoted 4 lines
builtin/stash--helper.c new file +4 -0
+       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.

Quoted 9 lines

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".

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

Ciao,
Dscho

#10 lore raw

Re: [PATCH v5 2/5] stash: convert apply to builtin

In reply to Joel Teichroeb

Hi Joel,

Quoted 3 lines · 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

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

Quoted 22 lines

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.

[...]

builtin/stash--helper.c new file +4 -0
new file mode 100644
index 0000000000..9d00a9547d
@@ -0,0 +1,431 @@
+#include "builtin.h"
+#include "config.h"
+#include "parse-options.h"
+#include "refs.h"

builtin/stash--helper.c line 14

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+#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)

Quoted 27 lines
builtin/stash--helper.c new file +27 -0
+	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;
+};
+

builtin/stash--helper.c line 51

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+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).

Quoted 4 lines
builtin/stash--helper.c new file +4 -0
+static void free_stash_info(struct stash_info *info)
+{
+	strbuf_release(&info->revision);
+}

builtin/stash--helper.c line 66

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+
+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.

builtin/stash--helper.c line 74

Quoted 8 lines
builtin/stash--helper.c new file +8 -0
+
+	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.

builtin/stash--helper.c line 81

Quoted 7 lines
builtin/stash--helper.c new file +7 -0
+		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.

builtin/stash--helper.c line 84

Quoted 3 lines
builtin/stash--helper.c new file +3 -0
+
+	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.

builtin/stash--helper.c line 87

Quoted 3 lines
builtin/stash--helper.c new file +3 -0
+		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).

Quoted 16 lines
builtin/stash--helper.c new file +16 -0
+		}
+
+		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;
+	}

builtin/stash--helper.c line 113

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+
+	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.

builtin/stash--helper.c line 118

Quoted 4 lines
builtin/stash--helper.c new file +4 -0
+	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...)

Quoted 46 lines
builtin/stash--helper.c new file +46 -0
+
+	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;

builtin/stash--helper.c line 174

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+	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).

Quoted 5 lines
builtin/stash--helper.c new file +5 -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);

builtin/stash--helper.c line 189

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+
+	/* 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.

Quoted 23 lines
builtin/stash--helper.c new file +23 -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);
+}
+

builtin/stash--helper.c line 223

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+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.

Quoted 42 lines
builtin/stash--helper.c new file +42 -0
+	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);

builtin/stash--helper.c line 275

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+	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? ;-)

Quoted 16 lines
builtin/stash--helper.c new file +16 -0
+	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;

builtin/stash--helper.c line 301

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+
+			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.

builtin/stash--helper.c line 304

Quoted 3 lines
builtin/stash--helper.c new file +3 -0
+
+			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.)

Quoted 60 lines
builtin/stash--helper.c new file +60 -0
+			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();

builtin/stash--helper.c line 374

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+	}
+
+	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 {
		...
Quoted 13 lines
builtin/stash--helper.c new file +13 -0
+
+	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")),

builtin/stash--helper.c line 397

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+		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.

Quoted 8 lines
builtin/stash--helper.c new file +8 -0
+	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();

builtin/stash--helper.c line 415

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+	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.

Quoted 87 lines
builtin/stash--helper.c new file +15 -0
+	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;
+}
git-stash.sh +0 -64
index 94793c1a91..0b5d1f3743 100755
@@ -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

git-stash.sh assert_stash_ref() { · line 572

Quoted 10 lines
git-stash.sh +4 -6
-		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)...

Quoted 18 lines
git-stash.sh +1 -0
+	return $res
 }
 
 pop_stash() {
git.c +1 -0
index ceaa58ef40..6ffe6364ac 100644
@@ -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},
Signature · 1 line
2.16.3

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

Ciao,
Dscho

#11 lore raw

Re: [PATCH v5 2/5] stash: convert apply to builtin

In reply to Joel Teichroeb

Hi Joel,

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

Quoted 20 lines · Joel Teichroeb
builtin/stash--helper.c new file +20 -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);

builtin/stash--helper.c line 431

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+	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

#12 lore raw

PATCH v5 3/5 stash: convert drop and clear to builtin

In reply to 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.

---
 builtin/stash--helper.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   4 +-
 2 files changed, 109 insertions(+), 2 deletions(-)
builtin/stash--helper.c +107 -0
index 9d00a9547d..520cd746c4 100644
@@ -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);
git-stash.sh +2 -2
index 0b5d1f3743..0b8f07b38a 100755
@@ -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
Signature · 1 line
2.16.3
#13 lore raw

Re: [PATCH v5 3/5] stash: convert drop and clear to builtin

Hi Joel,

Quoted 3 lines · 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.

Makes sense.

Quoted 4 lines

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.

Quoted 43 lines
builtin/stash--helper.c +28 -0
index 9d00a9547d..520cd746c4 100644
@@ -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;

builtin/stash--helper.c static int apply_stash(int argc, const char **argv, const char *prefix) · line 451

Quoted 10 lines
builtin/stash--helper.c +10 -0
+
+	/* 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.

builtin/stash--helper.c static int apply_stash(int argc, const char **argv, const char *prefix) · line 464

Quoted 13 lines
builtin/stash--helper.c +13 -0
+	} 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.

builtin/stash--helper.c static int apply_stash(int argc, const char **argv, const char *prefix) · line 467

Quoted 3 lines
builtin/stash--helper.c +3 -0
+
+	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...

builtin/stash--helper.c static int apply_stash(int argc, const char **argv, const char *prefix) · line 478

Quoted 11 lines
builtin/stash--helper.c +11 -0
+
+	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

#14 lore raw

PATCH v5 4/5 stash: convert branch to builtin

In reply to Joel Teichroeb

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

---
 builtin/stash--helper.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 17 ++---------------
 2 files changed, 53 insertions(+), 15 deletions(-)
builtin/stash--helper.c +51 -0
index 520cd746c4..486796bb6a 100644
@@ -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);
git-stash.sh +2 -15
index 0b8f07b38a..c5fd4c6c44 100755
@@ -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
Signature · 1 line
2.16.3
#15 lore raw

Re: [PATCH v5 4/5] stash: convert branch to builtin

Hi Joel,

Quoted 13 lines · Joel Teichroeb
builtin/stash--helper.c +5 -0
index 520cd746c4..486796bb6a 100644
@@ -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;

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 525

Quoted 10 lines
builtin/stash--helper.c +10 -0
+	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...

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 535

Quoted 10 lines
builtin/stash--helper.c +10 -0
+
+	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?

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 547

Quoted 12 lines
builtin/stash--helper.c +12 -0
+	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?

Quoted 44 lines
builtin/stash--helper.c +8 -0
+
+	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);
git-stash.sh +0 -14
index 0b8f07b38a..c5fd4c6c44 100755
@@ -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'

git-stash.sh pop) · line 665

Quoted 9 lines
git-stash.sh +2 -1
@@ -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

#16 lore raw

PATCH v5 5/5 stash: convert pop to builtin

In reply to 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.

---
 builtin/stash--helper.c | 41 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 50 ++++---------------------------------------------
 2 files changed, 45 insertions(+), 46 deletions(-)
builtin/stash--helper.c +41 -0
index 486796bb6a..7c8950bc90 100644
@@ -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 {
git-stash.sh +4 -46

68 lines not on this page · raw message

Signature · 1 line
2.16.3
#17 lore raw

Re: [PATCH v5 5/5] stash: convert pop to builtin

Hi Joel,

builtin/stash--helper.c line 17

Quoted 10 lines · Joel Teichroeb
builtin/stash--helper.c +1 -0
index 486796bb6a..7c8950bc90 100644
@@ -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.

Quoted 7 lines
builtin/stash--helper.c +3 -0
@@ -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;

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 529

Quoted 10 lines
builtin/stash--helper.c +10 -0
+	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.

Quoted 4 lines
builtin/stash--helper.c +4 -0
+
+	if (get_stash_info(&info, argc, argv))
+		return -1;
+

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 543

Quoted 10 lines
builtin/stash--helper.c +10 -0
+	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.

Quoted 84 lines
builtin/stash--helper.c +8 -0
+
+	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 {
git-stash.sh +2 -45
index c5fd4c6c44..8f2640fe90 100755
@@ -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

git-stash.sh drop) · line 618

Quoted 9 lines
git-stash.sh +2 -1
@@ -654,7 +611,8 @@ drop)
 	;;
 pop)
 	shift
-	pop_stash "$@"
+	cd "$START_DIR"
+	git stash--helper pop "$@"
 	;;
 branch)
 	shift

Nice!

Ciao,
Dscho

#18 lore raw
In reply to Joel Teichroeb

Hi Joel,

Quoted 13 lines · 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.

Quoted 7 lines

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

#19 lore raw

Hello Joel,

Quoted 3 lines
Quoted 3 lines

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

#20 lore raw

Hi,

Quoted 8 lines · Paul-Sebastian Ungureanu
Quoted 3 lines
Quoted 3 lines

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 ;-).

A message that is not in the archive (sent off-list or not yet received): CA+CzEk8c1Pt+n9Jy5vL9_K60Q=6VKnLTdBY1JFRnb-POuRFv0Q@mail.gmail.com

#22 lore raw

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

Quoted 28 lines · Joel Teichroeb

Hi Paul,

That sounds great to me!

Thanks,
Joel

Quoted 18 lines · Paul-Sebastian Ungureanu

Hello Joel,

Quoted 3 lines
Quoted 3 lines

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

#23 lore raw

v6 cover stash: add new tests and introduce a new helper function

In reply to Joel Teichroeb

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(-)
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#24 lore raw

PATCH v6 1/4 sha1-name.c: added 'get_oidf', which acts like 'get_oid'

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.

---
 cache.h     |  1 +
 sha1-name.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
cache.h +1 -0
index 89a107a7f..9252a4546 100644
@@ -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);
sha1-name.c +19 -0
index 60d9ef3c7..80ee8f742 100644
@@ -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
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#25 lore raw

Re: [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid'

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'

Quoted 3 lines · Paul-Sebastian Ungureanu

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

#26 lore raw

PATCH v6 1/4 stash: convert apply to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 .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
.gitignore +1 -0
index 388cc4bee..56e63dd24 100644
@@ -155,6 +155,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
Makefile +1 -0
index 1d27f3636..ca6072053 100644
@@ -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
builtin.h +1 -0
index 4e0f64723..ba63ce0f6 100644
@@ -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);
builtin/stash--helper.c new file +442 -0

442 lines not on this page · raw message

git-stash.sh +7 -71

90 lines not on this page · raw message

git.c +1 -0
index c2f48d53d..d43c42e31 100644
@@ -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 },
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#27 lore raw

PATCH v6 2/4 stash: improve option parsing test coverage

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
t/t3903-stash.sh +35 -0
index 1f871d3cc..af7586d43 100755
@@ -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" &&
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#28 lore raw

PATCH v6 2/4 stash: convert drop and clear to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 builtin/stash--helper.c | 112 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   4 +-
 2 files changed, 114 insertions(+), 2 deletions(-)
builtin/stash--helper.c +112 -0
index 1c4387b10..84a537f39 100644
@@ -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);
git-stash.sh +2 -2
index 809b1c2d1..a99d5dc9e 100755
@@ -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
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#29 lore raw

Re: [PATCH v6 2/4] stash: convert drop and clear to builtin

Hi Paul,

Quoted 30 lines · Paul-Sebastian Ungureanu
builtin/stash--helper.c +22 -0
index 1c4387b10..84a537f39 100644
@@ -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);
+	}
+

builtin/stash--helper.c static int apply_stash(int argc, const char **argv, const char *prefix) · line 485

Quoted 10 lines
builtin/stash--helper.c +10 -0
+	/*
+	 * 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

#30 lore raw

Re: [PATCH v6 2/4] stash: convert drop and clear to builtin

Hi,

Quoted 3 lines · Johannes Schindelin

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

#31 lore raw

PATCH v6 3/4 stash: update test cases conform to coding guidelines

In reply to Paul-Sebastian Ungureanu

Removed whitespaces after redirection operators.

---
 t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 59 deletions(-)
t/t3903-stash.sh +61 -59
index af7586d43..de6cab1fe 100755
@@ -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
 '
 
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#32 lore raw

Re: [PATCH v6 3/4] stash: update test cases conform to coding guidelines

Hi Paul,

Quoted 1 line · Paul-Sebastian Ungureanu

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

#33 lore raw

PATCH v6 3/4 stash: convert branch to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 builtin/stash--helper.c | 43 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 17 ++--------------
 2 files changed, 45 insertions(+), 15 deletions(-)
builtin/stash--helper.c +43 -0
index 84a537f39..fbf78249c 100644
@@ -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);
git-stash.sh +2 -15
index a99d5dc9e..29d9f4425 100755
@@ -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
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#34 lore raw

Re: [PATCH v6 3/4] stash: convert branch to builtin

Hi Paul,

Quoted 22 lines · Paul-Sebastian Ungureanu
builtin/stash--helper.c +14 -0
index 84a537f39..fbf78249c 100644
@@ -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)

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 554

Quoted 10 lines
builtin/stash--helper.c +10 -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);

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 555

Quoted 1 line
builtin/stash--helper.c +1 -0
+	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

#35 lore raw

Re: [PATCH v6 3/4] stash: convert branch to builtin

Quoted 6 lines · Johannes Schindelin

builtin/stash--helper.c static int drop_stash(int argc, const char **argv, const char *prefix) · line 555

Quoted 1 line
builtin/stash--helper.c +1 -0
+	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.

#36 lore raw

Re: [PATCH v6 3/4] stash: convert branch to builtin

Hello,

Quoted 14 lines · Junio C Hamano

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").

Quoted 4 lines

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

#37 lore raw

PATCH v6 4/4 stash: renamed test cases to be more descriptive

In reply to Paul-Sebastian Ungureanu

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

---
 t/t3903-stash.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
t/t3903-stash.sh +7 -7
index de6cab1fe..8d002a7f2 100755
@@ -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
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#38 lore raw

Re: [PATCH v6 4/4] stash: renamed test cases to be more descriptive

Hi Paul,

Quoted 4 lines · Paul-Sebastian Ungureanu

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

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

#39 lore raw

PATCH v6 4/4 stash: convert pop to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-
 git-stash.sh            | 47 ++---------------------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)
builtin/stash--helper.c +35 -1
index fbf78249c..a38d6ae8a 100644
@@ -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);
 
git-stash.sh +2 -45

59 lines not on this page · raw message

Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#40 lore raw

Re: [PATCH v6 4/4] stash: convert pop to builtin

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

Quoted 150 lines · Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-
 git-stash.sh            | 47 ++---------------------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)
builtin/stash--helper.c +35 -1
index fbf78249c..a38d6ae8a 100644
@@ -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);
 
git-stash.sh +2 -45
index 29d9f4425..8f2640fe9 100755
@@ -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
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#41 lore raw

PATCH v6 1/6 stash: implement the "list" command in the builtin

In reply to Paul-Sebastian Ungureanu

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

---
 builtin/stash--helper.c | 33 +++++++++++++++++++++++++++++++++
 git-stash.sh            |  7 +------
 2 files changed, 34 insertions(+), 6 deletions(-)
builtin/stash--helper.c +33 -0
index a38d6ae8a..2ed21f5d1 100644
@@ -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);
git-stash.sh +1 -6
index 8f2640fe9..6052441aa 100755
@@ -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
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#42 lore raw

PATCH v6 2/6 stash: convert show to builtin

In reply to Paul-Sebastian Ungureanu

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.

---
 builtin/stash--helper.c |  77 +++++++++++++++++++++++
 git-stash.sh            | 132 +---------------------------------------
 2 files changed, 78 insertions(+), 131 deletions(-)
builtin/stash--helper.c +77 -0
index 2ed21f5d1..a7eba3fba 100644
@@ -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);
git-stash.sh +1 -131

150 lines not on this page · raw message

Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#43 lore raw

PATCH v6 3/6 stash: change `git stash show` usage text and documentation

In reply to Paul-Sebastian Ungureanu

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.

---
 Documentation/git-stash.txt | 4 ++--
 builtin/stash--helper.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Documentation/git-stash.txt +2 -2
index 7ef8c4791..e31ea7d30 100644
@@ -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
builtin/stash--helper.c +2 -2
index a7eba3fba..4f49fd04b 100644
@@ -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
 };
 
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#44 lore raw

PATCH v6 4/6 stash: refactor `show_stash()` to use the diff API

In reply to Paul-Sebastian Ungureanu

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.

---
 builtin/stash--helper.c | 72 +++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 27 deletions(-)
builtin/stash--helper.c +45 -27
index 4f49fd04b..4589e12d6 100644
@@ -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)
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#45 lore raw

PATCH v6 5/6 stash: update `git stash show` documentation

In reply to Paul-Sebastian Ungureanu

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)

---
 Documentation/git-stash.txt | 3 +++
 1 file changed, 3 insertions(+)
Documentation/git-stash.txt +3 -0
index e31ea7d30..d60ebdb96 100644
@@ -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
Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#46 lore raw

PATCH v6 6/6 stash: convert store to builtin

In reply to Paul-Sebastian Ungureanu

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.

---
 builtin/stash--helper.c | 48 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 43 ++----------------------------------
 2 files changed, 50 insertions(+), 41 deletions(-)
builtin/stash--helper.c +48 -0
index 4589e12d6..556d91b20 100644
@@ -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);
git-stash.sh +2 -41

61 lines not on this page · raw message

Signature · 1 line
2.18.0.rc2.13.g506fc12fb
#47 lore raw

Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function

In reply to Paul-Sebastian Ungureanu

Hi,

Quoted 4 lines · Paul-Sebastian Ungureanu

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

#48 lore raw

Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function

Quoted 9 lines · Johannes Schindelin

Hi,

Quoted 4 lines · Paul-Sebastian Ungureanu

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.

Quoted 7 lines

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.

#49 lore raw

Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function

Hello,

Quoted 9 lines

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)

Quoted 8 lines

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

#50 lore raw

Re: [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin

In reply to Paul-Sebastian Ungureanu

Hi Paul,

Quoted 9 lines · Paul-Sebastian Ungureanu

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

#51 lore raw

Re: [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin

Hello,

Quoted 8 lines · Johannes Schindelin
Quoted 5 lines

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

#52 lore raw

v7 cover Convert "git stash" to C builtin

In reply to Paul-Sebastian Ungureanu

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
Signature · 1 line
2.18.0.573.g56500d98f
#53 lore raw

PATCH v7 1/26 sha1-name.c: added 'get_oidf', which acts like 'get_oid'

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.

---
 cache.h     |  1 +
 sha1-name.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
cache.h +1 -0
index 8dc7134f0..4ec09b336 100644
@@ -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);
sha1-name.c +19 -0
index c9cc1318b..261b960bb 100644
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#54 lore raw

PATCH v7 2/26 stash: improve option parsing test coverage

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
t/t3903-stash.sh +35 -0
index 1f871d3cc..af7586d43 100755
@@ -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" &&
Signature · 1 line
2.18.0.573.g56500d98f
#55 lore raw

PATCH v7 3/26 stash: update test cases conform to coding guidelines

In reply to Paul-Sebastian Ungureanu

Removed whitespaces after redirection operators.

---
 t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 59 deletions(-)
t/t3903-stash.sh +61 -59
index af7586d43..de6cab1fe 100755
@@ -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
 '
 
Signature · 1 line
2.18.0.573.g56500d98f
#56 lore raw

PATCH v7 4/26 stash: renamed test cases to be more descriptive

In reply to Paul-Sebastian Ungureanu

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

---
 t/t3903-stash.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
t/t3903-stash.sh +7 -7
index de6cab1fe..8d002a7f2 100755
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#57 lore raw

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

Quoted 1 line

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.

Quoted 31 lines · Paul-Sebastian Ungureanu

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

---
 t/t3903-stash.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
t/t3903-stash.sh +2 -2
index de6cab1fe..8d002a7f2 100755
@@ -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 &&

t/t3903-stash.sh test_expect_success 'invalid ref of the form "n", n >= N' ' · line 685

Quoted 5 lines
t/t3903-stash.sh +1 -1
@@ -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.

Quoted 13 lines
t/t3903-stash.sh +1 -1
 	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 &&

t/t3903-stash.sh test_expect_success 'stash branch should not drop the stash if the apply fails' · line 710

Quoted 5 lines
t/t3903-stash.sh +1 -1
@@ -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.

t/t3903-stash.sh test_expect_success 'stash push -p with pathspec shows no changes only once' ' · line 1051

Quoted 9 lines
t/t3903-stash.sh +1 -1
 	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.

t/t3903-stash.sh test_expect_success 'stash push with pathspec shows no changes when there are no · line 1061

Quoted 9 lines
t/t3903-stash.sh +1 -1
 	>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.

Quoted 6 lines
t/t3903-stash.sh +0 -0
 	>untracked &&
 	test_must_fail git stash push untracked &&
 	test_path_is_file untracked
Signature · 1 line
2.18.0.573.g56500d98f
#58 lore raw

PATCH v7 5/26 stash: convert apply to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 .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
.gitignore +1 -0
index 3284a1e9b..3abbba81a 100644
@@ -156,6 +156,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
Makefile +1 -0
index bc4fc8eea..af82bc7ee 100644
@@ -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
builtin.h +1 -0
index 0362f1ce2..55ceb1574 100644
@@ -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);
builtin/stash--helper.c new file +452 -0

452 lines not on this page · raw message

git-stash.sh +7 -71

90 lines not on this page · raw message

git.c +1 -0
index fc7d15d54..a3c83f57e 100644
@@ -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 },
Signature · 1 line
2.18.0.573.g56500d98f
#59 lore raw

Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin

Quoted 11 lines · Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

Quoted 6 lines

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

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

Quoted 8 lines
builtin/stash--helper.c new file +2 -0
new file mode 100644
index 000000000..ef6a9d30d
@@ -0,0 +1,452 @@
+#include "builtin.h"
+#include "config.h"

builtin/stash--helper.c line 12

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+#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.

builtin/stash--helper.c line 58

Quoted 11 lines
builtin/stash--helper.c new file +11 -0
+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.

Quoted 5 lines
builtin/stash--helper.c new file +5 -0
+{
+	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)) {

builtin/stash--helper.c line 91

Quoted 10 lines
builtin/stash--helper.c new file +8 -0
+		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.

builtin/stash--helper.c line 210

Quoted 13 lines
builtin/stash--helper.c new file +13 -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);
+}

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.

builtin/stash--helper.c

Quoted 2 lines
builtin/stash--helper.c new file +2 -0
+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.

#60 lore raw

Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin

Hello,

Quoted 17 lines · Junio C Hamano
Quoted 15 lines · Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

Quoted 5 lines
Quoted 2 lines

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.

Quoted 39 lines
Quoted 8 lines
builtin/stash--helper.c new file +2 -0
new file mode 100644
index 000000000..ef6a9d30d
@@ -0,0 +1,452 @@
+#include "builtin.h"
+#include "config.h"

builtin/stash--helper.c line 12

Quoted 10 lines
builtin/stash--helper.c new file +10 -0
+#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.

builtin/stash--helper.c line 58

Quoted 11 lines
builtin/stash--helper.c new file +11 -0
+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.

Quoted 1 line

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

Will do so.

Quoted 29 lines
Quoted 5 lines
builtin/stash--helper.c new file +5 -0
+{
+	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)) {

builtin/stash--helper.c line 91

Quoted 10 lines
builtin/stash--helper.c new file +8 -0
+		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.

Quoted 10 lines
builtin/stash--helper.c new file +10 -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;
Quoted 8 lines

builtin/stash--helper.c line 210

Quoted 3 lines
builtin/stash--helper.c new file +3 -0
+	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/

Quoted 6 lines

builtin/stash--helper.c

Quoted 2 lines
builtin/stash--helper.c new file +2 -0
+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

#61 lore raw

Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin

Quoted 4 lines · Paul-Sebastian Ungureanu
Quoted 2 lines

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.

#62 lore raw

Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin

Hellom

Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Paul-Sebastian Ungureanu
Quoted 2 lines

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

#63 lore raw

Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin

In reply to Paul-Sebastian Ungureanu

builtin/stash--helper.c line 100

Quoted 5 lines · Paul-Sebastian Ungureanu
builtin/stash--helper.c new file +5 -0
+       strbuf_init(&info->revision, 0);
+       if (!commit) {
+               if (!ref_exists(ref_stash)) {
+                       free_stash_info(info);
+                       fprintf_ln(stderr, "No stash entries found.");

Maybe _() ?

Signature · 1 line
Duy
#64 lore raw

PATCH v7 6/26 stash: convert drop and clear to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 builtin/stash--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   4 +-
 2 files changed, 117 insertions(+), 2 deletions(-)
builtin/stash--helper.c +115 -0
index ef6a9d30d..ae719b7fc 100644
@@ -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);
git-stash.sh +2 -2
index 809b1c2d1..a99d5dc9e 100755
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#65 lore raw

PATCH v7 7/26 stash: convert branch to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 builtin/stash--helper.c | 44 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 17 ++--------------
 2 files changed, 46 insertions(+), 15 deletions(-)
builtin/stash--helper.c +44 -0
index ae719b7fc..1e4d07295 100644
@@ -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);
git-stash.sh +2 -15
index a99d5dc9e..29d9f4425 100755
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#66 lore raw

PATCH v7 8/26 stash: convert pop to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-
 git-stash.sh            | 47 ++---------------------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)
builtin/stash--helper.c +35 -1
index 1e4d07295..d6bd468e0 100644
@@ -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);
 
git-stash.sh +2 -45

59 lines not on this page · raw message

Signature · 1 line
2.18.0.573.g56500d98f
#67 lore raw

PATCH v7 9/26 stash: implement the "list" command in the builtin

In reply to Paul-Sebastian Ungureanu

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

---
 builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++
 git-stash.sh            |  7 +------
 2 files changed, 32 insertions(+), 6 deletions(-)
builtin/stash--helper.c +31 -0
index d6bd468e0..daa4d0034 100644
@@ -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);
git-stash.sh +1 -6
index 8f2640fe9..6052441aa 100755
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#68 lore raw

Re: [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin

Quoted 1 line

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.

Quoted 100 lines · Paul-Sebastian Ungureanu

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

---
 builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++
 git-stash.sh            |  7 +------
 2 files changed, 32 insertions(+), 6 deletions(-)
builtin/stash--helper.c +31 -0
index d6bd468e0..daa4d0034 100644
@@ -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);
git-stash.sh +1 -6
index 8f2640fe9..6052441aa 100755
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#69 lore raw

Re: [GSoC][PATCH v7 09/26] stash: implement the "list" command in the builtin

Quoted 4 lines · Thomas Gummerer
Quoted 1 line

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.

#70 lore raw

PATCH v7 10/26 stash: convert show to builtin

In reply to Paul-Sebastian Ungureanu

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.

---
 builtin/stash--helper.c |  78 ++++++++++++++++++++++++
 git-stash.sh            | 132 +---------------------------------------
 2 files changed, 79 insertions(+), 131 deletions(-)
builtin/stash--helper.c +78 -0
index daa4d0034..e764cd33e 100644
@@ -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);
git-stash.sh +1 -131

150 lines not on this page · raw message

Signature · 1 line
2.18.0.573.g56500d98f
#71 lore raw

Re: [GSoC][PATCH v7 10/26] stash: convert show to builtin

Quoted 8 lines · Paul-Sebastian Ungureanu

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.

Quoted 276 lines
---
 builtin/stash--helper.c |  78 ++++++++++++++++++++++++
 git-stash.sh            | 132 +---------------------------------------
 2 files changed, 79 insertions(+), 131 deletions(-)
builtin/stash--helper.c +78 -0
index daa4d0034..e764cd33e 100644
@@ -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);
git-stash.sh +1 -131
index 6052441aa..0d05cbc1e 100755
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#72 lore raw

Re: [GSoC][PATCH v7 10/26] stash: convert show to builtin

Quoted 4 lines · Thomas Gummerer

'--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.

#73 lore raw

PATCH v7 11/26 stash: change `git stash show` usage text and documentation

In reply to Paul-Sebastian Ungureanu

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.

---
 Documentation/git-stash.txt | 4 ++--
 builtin/stash--helper.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Documentation/git-stash.txt +2 -2
index 7ef8c4791..e31ea7d30 100644
@@ -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
builtin/stash--helper.c +2 -2
index e764cd33e..0c1efca6b 100644
@@ -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
 };
 
Signature · 1 line
2.18.0.573.g56500d98f
#74 lore raw

Re: [GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation

Quoted 1 line

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!

Quoted 57 lines · Paul-Sebastian Ungureanu

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.

---
 Documentation/git-stash.txt | 4 ++--
 builtin/stash--helper.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Documentation/git-stash.txt +2 -2
index 7ef8c4791..e31ea7d30 100644
@@ -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
builtin/stash--helper.c +2 -2
index e764cd33e..0c1efca6b 100644
@@ -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
 };
 
Signature · 1 line
2.18.0.573.g56500d98f
#75 lore raw

PATCH v7 12/26 stash: refactor `show_stash()` to use the diff API

In reply to Paul-Sebastian Ungureanu

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.

---
 builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 28 deletions(-)
builtin/stash--helper.c +45 -28
index 0c1efca6b..ec8c38c6f 100644
@@ -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)
Signature · 1 line
2.18.0.573.g56500d98f
#76 lore raw

Re: [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API

Quoted 7 lines · Paul-Sebastian Ungureanu

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?

Quoted 67 lines
---
 builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 28 deletions(-)
builtin/stash--helper.c +14 -19
index 0c1efca6b..ec8c38c6f 100644
@@ -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.
 	 */

builtin/stash--helper.c static int git_stash_config(const char *var, const char *value, void *cb) · line 700

Quoted 10 lines
builtin/stash--helper.c +6 -2
-	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.

Quoted 13 lines
builtin/stash--helper.c +6 -4
+	}
 
-		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)

builtin/stash--helper.c static int git_stash_config(const char *var, const char *value, void *cb) · line 718

Quoted 10 lines
builtin/stash--helper.c +8 -1
+		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?

builtin/stash--helper.c static int git_stash_config(const char *var, const char *value, void *cb) · line 725

Quoted 7 lines
builtin/stash--helper.c +7 -0
+	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.

Quoted 14 lines
builtin/stash--helper.c +4 -2
+	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)
Signature · 1 line
2.18.0.573.g56500d98f
#77 lore raw

Re: [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API

Quoted 9 lines · Thomas Gummerer
Quoted 7 lines · Paul-Sebastian Ungureanu

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.

Quoted 6 lines

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.

Quoted 95 lines
Quoted 67 lines
---
 builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 28 deletions(-)
builtin/stash--helper.c +14 -19
index 0c1efca6b..ec8c38c6f 100644
@@ -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.
       */

builtin/stash--helper.c static int git_stash_config(const char *var, const char *value, void *cb) · line 700

Quoted 10 lines
builtin/stash--helper.c +6 -2
-     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.

Quoted 3 lines

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.

Quoted 18 lines
Quoted 18 lines
builtin/stash--helper.c +10 -4
+     }
 
-             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);
 
Quoted 8 lines

builtin/stash--helper.c static int git_stash_config(const char *var, const char *value, void *cb) · line 718

Quoted 5 lines
builtin/stash--helper.c +4 -1
-     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.

Quoted 29 lines

builtin/stash--helper.c static int git_stash_config(const char *var, const char *value, void *cb) · line 725

Quoted 7 lines
builtin/stash--helper.c +7 -0
+     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.

Quoted 14 lines
builtin/stash--helper.c +4 -3
+     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

#78 lore raw

PATCH v7 13/26 stash: update `git stash show` documentation

In reply to Paul-Sebastian Ungureanu

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)

---
 Documentation/git-stash.txt | 3 +++
 1 file changed, 3 insertions(+)
Documentation/git-stash.txt +3 -0
index e31ea7d30..d60ebdb96 100644
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#79 lore raw

Re: [GSoC][PATCH v7 13/26] stash: update `git stash show` documentation

Quoted 3 lines · Paul-Sebastian Ungureanu

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/

Quoted 1 line

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.

Quoted 10 lines
---
 Documentation/git-stash.txt | 3 +++
 1 file changed, 3 insertions(+)
Documentation/git-stash.txt +0 -0
index e31ea7d30..d60ebdb96 100644

Documentation/git-stash.txt show [<options>] [<stash>]:: · line 120

Quoted 4 lines
Documentation/git-stash.txt +1 -0
@@ -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.

Quoted 8 lines
Documentation/git-stash.txt +2 -0
+	`--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
Signature · 1 line
2.18.0.573.g56500d98f
#80 lore raw

PATCH v7 14/26 stash: convert store to builtin

In reply to Paul-Sebastian Ungureanu

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.

---
 builtin/stash--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 43 ++--------------------------------
 2 files changed, 54 insertions(+), 41 deletions(-)
builtin/stash--helper.c +52 -0
index ec8c38c6f..5ff810f8c 100644
@@ -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);
git-stash.sh +2 -41

61 lines not on this page · raw message

Signature · 1 line
2.18.0.573.g56500d98f
#81 lore raw

Re: [GSoC][PATCH v7 14/26] stash: convert store to builtin

Quoted 4 lines · Paul-Sebastian Ungureanu

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.

Quoted 32 lines
---
 builtin/stash--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            | 43 ++--------------------------------
 2 files changed, 54 insertions(+), 41 deletions(-)
builtin/stash--helper.c +6 -0
index ec8c38c6f..5ff810f8c 100644
@@ -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;

builtin/stash--helper.c static int show_stash(int argc, const char **argv, const char *prefix) · line 747

Quoted 10 lines
builtin/stash--helper.c +8 -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.

Quoted 119 lines
builtin/stash--helper.c +38 -0
+
+	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);
git-stash.sh +2 -41
index 0d05cbc1e..5739c5152 100755
@@ -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
Signature · 1 line
2.18.0.573.g56500d98f
#82 lore raw

PATCH v7 15/26 stash: convert create to builtin

In reply to Paul-Sebastian Ungureanu

Add stash create to the helper.

---
 builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   2 +-
 2 files changed, 407 insertions(+), 1 deletion(-)
builtin/stash--helper.c +406 -0
index 5ff810f8c..a4e57899b 100644
@@ -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);
git-stash.sh +1 -1
index 5739c5152..ab06e4ffb 100755
@@ -425,7 +425,7 @@ clear)
 	;;
 create)
 	shift
-	create_stash -m "$*" && echo "$w_commit"
+	git stash--helper create --message "$*"
 	;;
 store)
 	shift
Signature · 1 line
2.18.0.573.g56500d98f
#83 lore raw

Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin

Quoted 54 lines · Paul-Sebastian Ungureanu

Add stash create to the helper.

---
 builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   2 +-
 2 files changed, 407 insertions(+), 1 deletion(-)
builtin/stash--helper.c +22 -0
index 5ff810f8c..a4e57899b 100644
@@ -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;

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1069

Quoted 10 lines
builtin/stash--helper.c +10 -0
+	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.

Quoted 9 lines
builtin/stash--helper.c +9 -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);
+	}

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1088

Quoted 10 lines
builtin/stash--helper.c +10 -0
+
+	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').

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1095

Quoted 7 lines
builtin/stash--helper.c +7 -0
+
+	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.

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1103

Quoted 8 lines
builtin/stash--helper.c +8 -0
+		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.

Quoted 17 lines
builtin/stash--helper.c +17 -0
+			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;

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1130

Quoted 10 lines
builtin/stash--helper.c +10 -0
+			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.

Quoted 36 lines
builtin/stash--helper.c +36 -0
+
+	/*
+	 * `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"),

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1176

Quoted 10 lines
builtin/stash--helper.c +10 -0
+			 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.

Quoted 39 lines
builtin/stash--helper.c +13 -0
+
+	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);
git-stash.sh +1 -1
index 5739c5152..ab06e4ffb 100755
@@ -425,7 +425,7 @@ clear)
 	;;
 create)
 	shift
-	create_stash -m "$*" && echo "$w_commit"
+	git stash--helper create --message "$*"
 	;;
 store)
 	shift
Signature · 1 line
2.18.0.573.g56500d98f
#84 lore raw

Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin

Quoted 63 lines · Thomas Gummerer
Quoted 62 lines · Paul-Sebastian Ungureanu

Add stash create to the helper.

---
 builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   2 +-
 2 files changed, 407 insertions(+), 1 deletion(-)
builtin/stash--helper.c +30 -0
index 5ff810f8c..a4e57899b 100644
@@ -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)) {
Quoted 8 lines

builtin/stash--helper.c

Quoted 2 lines
builtin/stash--helper.c +2 -0
+             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).

Quoted 28 lines
Quoted 9 lines
builtin/stash--helper.c +9 -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);
+     }

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1088

Quoted 10 lines
builtin/stash--helper.c +10 -0
+
+     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');
Quoted 3 lines

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.

Quoted 21 lines

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1095

Quoted 7 lines
builtin/stash--helper.c +7 -0
+
+     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.

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1103

Quoted 8 lines
builtin/stash--helper.c +8 -0
+             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");
Quoted 6 lines

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.

Quoted 76 lines
Quoted 17 lines
builtin/stash--helper.c +17 -0
+                     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;

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1130

Quoted 10 lines
builtin/stash--helper.c +10 -0
+                     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.

Quoted 43 lines
builtin/stash--helper.c +43 -0
+
+     /*
+      * `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);
Quoted 8 lines

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1176

Quoted 3 lines
builtin/stash--helper.c +3 -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.

Quoted 31 lines
Quoted 31 lines
builtin/stash--helper.c +13 -0
+
+     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);
git-stash.sh +0 -0
index 5739c5152..ab06e4ffb 100755
@@ -425,7 +425,7 @@ clear)
      ;;
 create)
      shift
Quoted 8 lines
Quoted 8 lines
git-stash.sh +1 -2
-     create_stash -m "$*" && echo "$w_commit"
+     git stash--helper create --message "$*"
      ;;
 store)
      shift
--

2.18.0.573.g56500d98f

Thanks!

#85 lore raw

Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin

Quoted 90 lines · Paul Sebastian Ungureanu
Quoted 80 lines · Thomas Gummerer
Quoted 20 lines · Paul-Sebastian Ungureanu

[...]

builtin/stash--helper.c +17 -0
+                     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;

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1130

Quoted 10 lines
builtin/stash--helper.c +10 -0
+                     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.

Quoted 43 lines
builtin/stash--helper.c +43 -0
+
+     /*
+      * `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);
Quoted 8 lines

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 1176

Quoted 3 lines
builtin/stash--helper.c +3 -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.

Quoted 2 lines

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?

#86 lore raw

PATCH v7 16/26 stash: replace spawning a "read-tree" process

In reply to Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +3 -6
index a4e57899b..887b78d05 100644
@@ -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",
Signature · 1 line
2.18.0.573.g56500d98f
#87 lore raw

Re: [GSoC][PATCH v7 16/26] stash: replace spawning a "read-tree" process

Quoted 22 lines · Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +0 -2
index a4e57899b..887b78d05 100644
@@ -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;

builtin/stash--helper.c static int stash_patch(struct stash_info *info, const char **argv) · line 998

Quoted 10 lines
builtin/stash--helper.c +3 -4
-	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.

Quoted 6 lines
builtin/stash--helper.c +0 -0
 	cp1.git_cmd = 1;
 	argv_array_pushl(&cp1.args, "diff-index", "--name-only", "-z",
Signature · 1 line
2.18.0.573.g56500d98f
#88 lore raw

PATCH v7 17/26 stash: avoid spawning a "diff-index" process

In reply to Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +42 -14
index 887b78d05..f905d3908 100644
@@ -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;
Signature · 1 line
2.18.0.573.g56500d98f
#89 lore raw

Re: [GSoC][PATCH v7 17/26] stash: avoid spawning a "diff-index" process

Quoted 2 lines · Paul-Sebastian Ungureanu

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.

Quoted 19 lines
---
 builtin/stash--helper.c | 56 ++++++++++++++++++++++++++++++-----------
 1 file changed, 42 insertions(+), 14 deletions(-)
builtin/stash--helper.c +1 -0
index 887b78d05..f905d3908 100644
@@ -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);
 }

builtin/stash--helper.c static int reset_head(const char *prefix) · line 309

Quoted 10 lines
builtin/stash--helper.c +9 -0
 
+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'.

Quoted 10 lines
builtin/stash--helper.c +3 -0
+	}
+}
+
 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;
 }
 

builtin/stash--helper.c static int stash_patch(struct stash_info *info, const char **argv) · line 1003

Quoted 10 lines
builtin/stash--helper.c +2 -3
-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?

Quoted 7 lines
builtin/stash--helper.c +3 -0
+	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)) {

builtin/stash--helper.c static int stash_working_tree(struct stash_info *info, const char **argv) · line 1015

Quoted 7 lines
builtin/stash--helper.c +1 -3
@@ -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?

Quoted 63 lines
builtin/stash--helper.c +23 -8
 	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;
Signature · 1 line
2.18.0.573.g56500d98f
#90 lore raw

PATCH v7 18/26 stash: convert push to builtin

In reply to Paul-Sebastian Ungureanu

Add stash push to the helper.

---
 builtin/stash--helper.c | 209 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   6 +-
 2 files changed, 213 insertions(+), 2 deletions(-)
builtin/stash--helper.c +209 -0
index f905d3908..c26cad3d5 100644
@@ -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);
git-stash.sh +4 -2
index ab06e4ffb..c3146f62a 100755
@@ -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\")")"
 		;;
 	*)
Signature · 1 line
2.18.0.573.g56500d98f
#91 lore raw

Re: [GSoC][PATCH v7 18/26] stash: convert push to builtin

Quoted 2 lines · Paul-Sebastian Ungureanu

Add stash push to the helper.
---

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

Quoted 38 lines
 builtin/stash--helper.c | 209 ++++++++++++++++++++++++++++++++++++++++
 git-stash.sh            |   6 +-
 2 files changed, 213 insertions(+), 2 deletions(-)
builtin/stash--helper.c +12 -0
index f905d3908..c26cad3d5 100644
@@ -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,

builtin/stash--helper.c static int create_stash(int argc, const char **argv, const char *prefix) · line 1234

Quoted 10 lines
builtin/stash--helper.c +10 -0
+			 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.

Quoted 15 lines
builtin/stash--helper.c +15 -0
+		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))

builtin/stash--helper.c static int create_stash(int argc, const char **argv, const char *prefix) · line 1259

Quoted 10 lines
builtin/stash--helper.c +10 -0
+				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.

Quoted 6 lines
builtin/stash--helper.c +6 -0
+	if (refresh_cache(REFRESH_QUIET))
+		return -1;
+
+	if (!check_changes(argv, include_untracked, prefix)) {
+		fprintf_ln(stdout, "No local changes to save");
+		return 0;

builtin/stash--helper.c static int create_stash(int argc, const char **argv, const char *prefix) · line 1275

Quoted 10 lines
builtin/stash--helper.c +10 -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.

Quoted 11 lines
builtin/stash--helper.c +11 -0
+
+	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;

builtin/stash--helper.c static int create_stash(int argc, const char **argv, const char *prefix) · line 1296

Quoted 10 lines
builtin/stash--helper.c +10 -0
+
+			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'.

Quoted 5 lines
builtin/stash--helper.c +5 -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;

builtin/stash--helper.c static int create_stash(int argc, const char **argv, const char *prefix) · line 1311

Quoted 10 lines
builtin/stash--helper.c +10 -0
+
+			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?

Quoted 81 lines
builtin/stash--helper.c +81 -0
+			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;

builtin/stash--helper.c static int create_stash(int argc, const char **argv, const char *prefix) · line 1402

Quoted 10 lines
builtin/stash--helper.c +10 -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.

Quoted 56 lines
builtin/stash--helper.c +19 -0
+		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);
git-stash.sh +4 -2
index ab06e4ffb..c3146f62a 100755
@@ -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\")")"
 		;;
 	*)
Signature · 1 line
2.18.0.573.g56500d98f
#92 lore raw

PATCH v7 19/26 stash: make push to be quiet

In reply to Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +27 -14
index c26cad3d5..4fd79532c 100644
@@ -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;
 		}
 
Signature · 1 line
2.18.0.573.g56500d98f
#93 lore raw

Re: [GSoC][PATCH v7 19/26] stash: make push to be quiet

Quoted 1 line

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.

Quoted 4 lines · Paul-Sebastian Ungureanu

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?

Quoted 135 lines
---
 builtin/stash--helper.c | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)
builtin/stash--helper.c +27 -14
index c26cad3d5..4fd79532c 100644
@@ -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;
 		}
 
Signature · 1 line
2.18.0.573.g56500d98f
#94 lore raw

PATCH v7 20/26 stash: add tests for `git stash push -q`

In reply to Paul-Sebastian Ungureanu

This commit introduces more tests for the quiet option of git stash push.

---
 t/t3903-stash.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
t/t3903-stash.sh +21 -0
index 8d002a7f2..b78db74ae 100755
@@ -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 &&
Signature · 1 line
2.18.0.573.g56500d98f
#95 lore raw

Re: [GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q`

Quoted 2 lines · Paul-Sebastian Ungureanu

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/[email protected]/

Quoted 9 lines
---
 t/t3903-stash.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
t/t3903-stash.sh +0 -0
index 8d002a7f2..b78db74ae 100755

t/t3903-stash.sh test_expect_success 'push: <pathspec> not in the repository errors out' ' · line 1069

Quoted 6 lines
t/t3903-stash.sh +3 -0
@@ -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.

t/t3903-stash.sh test_expect_success 'push: <pathspec> not in the repository errors out' ' · line 1081

Quoted 12 lines
t/t3903-stash.sh +12 -0
+	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.

t/t3903-stash.sh test_expect_success 'push: <pathspec> not in the repository errors out' ' · line 1084

Quoted 3 lines
t/t3903-stash.sh +3 -0
+	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.

t/t3903-stash.sh test_expect_success 'push: <pathspec> not in the repository errors out' ' · line 1085

Quoted 1 line
t/t3903-stash.sh +1 -0
+	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.

Quoted 8 lines
t/t3903-stash.sh +2 -0
+'
+
 test_expect_success 'untracked files are left in place when -u is not given' '
 	>file &&
 	git add file &&
Signature · 1 line
2.18.0.573.g56500d98f
#96 lore raw

PATCH v7 21/26 stash: replace spawning `git ls-files` child process

In reply to Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +32 -17
index 4fd79532c..5c27f5dcf 100644
@@ -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)
Signature · 1 line
2.18.0.573.g56500d98f
#97 lore raw

Re: [GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process

Quoted 47 lines · Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +12 -14
index 4fd79532c..5c27f5dcf 100644
@@ -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);

builtin/stash--helper.c static int store_stash(int argc, const char **argv, const char *prefix) · line 844

Quoted 10 lines
builtin/stash--helper.c +10 -0
+	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).

Quoted 40 lines
builtin/stash--helper.c +10 -3
+		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)
Signature · 1 line
2.18.0.573.g56500d98f
#98 lore raw

PATCH v7 22/26 stash: convert save to builtin

In reply to Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +48 -0
index 5c27f5dcf..f54a476e3 100644
@@ -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);
git-stash.sh +2 -309

323 lines not on this page · raw message

Signature · 1 line
2.18.0.573.g56500d98f
#99 lore raw

Re: [GSoC][PATCH v7 22/26] stash: convert save to builtin

Quoted 45 lines · Paul-Sebastian Ungureanu

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(-)
builtin/stash--helper.c +14 -0
index 5c27f5dcf..f54a476e3 100644
@@ -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;

builtin/stash--helper.c static int push_stash(int argc, const char **argv, const char *prefix) · line 1471

Quoted 10 lines
builtin/stash--helper.c +10 -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.

Quoted 7 lines
builtin/stash--helper.c +7 -0
+		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()

builtin/stash--helper.c static int push_stash(int argc, const char **argv, const char *prefix) · line 1488

Quoted 10 lines
builtin/stash--helper.c +10 -0
+	};
+
+	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.

Quoted 19 lines
builtin/stash--helper.c +7 -0
+
+	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);
 
[...]
#100 lore raw

PATCH v7 23/26 stash: convert `stash--helper.c` into `stash.c`

In reply to Paul-Sebastian Ungureanu

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
.gitignore +0 -1
index 3abbba81a..3284a1e9b 100644
@@ -156,7 +156,6 @@
 /git-show-ref
 /git-stage
 /git-stash
-/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
Makefile +1 -2
index af82bc7ee..c41e31ad8 100644
@@ -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
builtin.h +1 -1
index 55ceb1574..e8102d825 100644
@@ -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);
builtin/stash.c renamed from builtin/stash--helper.c +71 -61

288 lines not on this page · raw message

git-stash.sh deleted +0 -153

153 lines not on this page · raw message

git.c +1 -1
index a3c83f57e..add13c823 100644
@@ -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 },
Signature · 1 line
2.18.0.573.g56500d98f
#101 lore raw

Re: [GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c`

Quoted 31 lines · Paul-Sebastian Ungureanu

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
builtin/stash.c renamed from builtin/stash--helper.c +0 -0
similarity index 91%
rename from builtin/stash--helper.c
rename to builtin/stash.c
index f54a476e3..0ef88408a 100644
 
[...]

builtin/stash.c static int push_stash(int argc, const char **argv, const char *prefix) · line 1454

Quoted 10 lines
builtin/stash.c renamed from builtin/stash--helper.c +4 -3
@@ -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?

Quoted 42 lines
builtin/stash.c renamed from builtin/stash--helper.c +6 -6
 	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);

builtin/stash.c int cmd_stash__helper(int argc, const char **argv, const char *prefix) · line 1545

Quoted 9 lines
builtin/stash.c renamed from builtin/stash--helper.c +6 -0
@@ -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/[email protected]/

Quoted 13 lines
builtin/stash.c renamed from builtin/stash--helper.c +1 -1
 	usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
-		      git_stash_helper_usage, options);
+		      git_stash_usage, options);
 }
git-stash.sh deleted +0 -0
deleted file mode 100755
index 695f1feba..000000000
@@ -1,153 +0,0 @@
 
[...]
#102 lore raw

PATCH v7 24/26 stash: optimize `get_untracked_files()` and `check_changes()`

In reply to Paul-Sebastian Ungureanu

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(-)
builtin/stash.c +36 -14
index 0ef88408a..4d5c0d16e 100644
@@ -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;
 }
Signature · 1 line
2.18.0.573.g56500d98f
#103 lore raw

Re: [GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()`

Quoted 4 lines · Paul-Sebastian Ungureanu

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:

Quoted 8 lines

* 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.

#104 lore raw

PATCH v7 25/26 stash: replace all `write-tree` child processes with API calls

In reply to Paul-Sebastian Ungureanu

This commit replaces spawning git write-tree with API calls.

---
 builtin/stash.c | 40 ++++++++++++----------------------------
 1 file changed, 12 insertions(+), 28 deletions(-)
builtin/stash.c +12 -28
index 4d5c0d16e..46e76a34e 100644
@@ -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);
Signature · 1 line
2.18.0.573.g56500d98f
#105 lore raw

Re: [GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls

Quoted 4 lines · Paul-Sebastian Ungureanu

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!

builtin/stash.c static int save_untracked_files(struct stash_info *info, struct strbuf *msg) · line 953

Quoted 13 lines
builtin/stash.c +1 -2
index 4d5c0d16e..46e76a34e 100644
@@ -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.

Quoted 92 lines
builtin/stash.c +6 -24
 	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)) {

builtin/stash.c static int stash_working_tree(struct stash_info *info, · line 1100

Quoted 10 lines
builtin/stash.c +5 -1
+	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.

Quoted 10 lines
builtin/stash.c +0 -1
 done:
 	UNLEAK(rev);
-	strbuf_release(&out3);
 	argv_array_clear(&args);
 	object_array_clear(&rev.pending);
 	strbuf_release(&diff_output);
Signature · 1 line
2.18.0.573.g56500d98f
#106 lore raw

PATCH v7 26/26 stash: replace all "git apply" child processes with API calls

In reply to Paul-Sebastian Ungureanu

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(-)
builtin/stash.c +34 -27
index 46e76a34e..74eda822c 100644
@@ -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;
Signature · 1 line
2.18.0.573.g56500d98f
#107 lore raw

Re: [GSoC][PATCH v7 26/26] stash: replace all "git apply" child processes with API calls

Quoted 6 lines · Paul-Sebastian Ungureanu

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.

Quoted 17 lines
---
 builtin/stash.c | 61 +++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 27 deletions(-)
builtin/stash.c +1 -0
index 46e76a34e..74eda822c 100644
@@ -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>]"),

builtin/stash.c static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) · line 283

Quoted 7 lines
builtin/stash.c +0 -4
@@ -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).

Quoted 6 lines
builtin/stash.c +0 -0
 	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);
 }

builtin/stash.c static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) · line 294

Quoted 10 lines
builtin/stash.c +6 -2
 
-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.

builtin/stash.c static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) · line 299

Quoted 7 lines
builtin/stash.c +1 -5
+	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 :)

Quoted 76 lines
builtin/stash.c +26 -16
-	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;
Signature · 1 line
2.18.0.573.g56500d98f
#108 lore raw

Re: [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin

In reply to Paul-Sebastian Ungureanu
Quoted 6 lines · Paul-Sebastian Ungureanu

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!

Quoted 18 lines

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
Quoted 7 lines
        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!

Quoted 69 lines

* 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
Signature · 1 line
2.18.0.573.g56500d98f
#109 lore raw

Re: [GSoC][PATCH v7 00/26] Convert "git stash" to C builtin

Hi,

Quoted 8 lines · Thomas Gummerer
Quoted 6 lines · Paul-Sebastian Ungureanu

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").

Quoted 5 lines

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.

Quoted 7 lines

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.

Quoted 8 lines

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.

Quoted 1 line

Hope this helps!

It helps a lot. Thank you!

Quoted 22 lines
Quoted 22 lines

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
Quoted 5 lines
Quoted 3 lines
        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

#110 lore raw

v8 cover Convert "git stash" to C builtin

In reply to Paul-Sebastian Ungureanu

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/[email protected]/

[2]:
https://public-inbox.org/git/[email protected]/

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
Signature · 1 line
2.19.0.rc0.22.gc26283d74e
#111 lore raw

PATCH v8 1/20 sha1-name.c: add `get_oidf()` which acts like `get_oid()`

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.

---
 cache.h     |  1 +
 sha1-name.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
cache.h +1 -0
index b1fd3d58ab..d93b2e25a5 100644
@@ -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);
sha1-name.c +19 -0
index c9cc1318b7..261b960bbd 100644
@@ -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
Signature · 1 line
2.19.0.rc0.22.gc26283d74e
#112 lore raw

PATCH v8 2/20 stash: improve option parsing test coverage

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
t/t3903-stash.sh +35 -0
index 1f871d3cca..af7586d43d 100755
@@ -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" &&
Signature · 1 line
2.19.0.rc0.22.gc26283d74e
#113 lore raw

PATCH v8 3/20 stash: update test cases conform to coding guidelines

In reply to Paul-Sebastian Ungureanu

Removed whitespaces after redirection operators.

---
 t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 59 deletions(-)
t/t3903-stash.sh +61 -59
index af7586d43d..de6cab1fe7 100755
@@ -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
 '
 
Signature · 1 line
2.19.0.rc0.22.gc26283d74e
#114 lore raw

Re: [GSoC][PATCH v8 03/20] stash: update test cases conform to coding guidelines

Quoted 1 line · Paul-Sebastian Ungureanu

Removed whitespaces after redirection operators.

A clean-up that is long overdue. Thanks.

The result still has some rooms to improve (e.g. the preparation of 'expect' file probably should be done in a test_expect_success block of the setup step; some are called 'expect' while others are called 'expected'; some test_cmp invocations do not have expected output as their first argument), but that can be done later.

#115 lore raw

PATCH v8 4/20 stash: rename test cases to be more descriptive

In reply to Paul-Sebastian Ungureanu

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

---
 t/t3903-stash.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
t/t3903-stash.sh +7 -7
index de6cab1fe7..3114c7bc4c 100755
@@ -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
Signature · 1 line
2.19.0.rc0.22.gc26283d74e
#116 lore raw

PATCH v8 5/20 stash: add tests for `git stash show` config

In reply to Paul-Sebastian Ungureanu

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.

---
 t/t3907-stash-show-config.sh | 81 ++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100755 t/t3907-stash-show-config.sh
t/t3907-stash-show-config.sh new file +81 -0
new file mode 100755
index 0000000000..8fe369c1a1
@@ -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
Signature · 1 line
2.19.0.rc0.22.gc26283d74e
#117 lore raw

Re: [GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config

Quoted 31 lines · Paul-Sebastian Ungureanu

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.

---
 t/t3907-stash-show-config.sh | 81 ++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100755 t/t3907-stash-show-config.sh
t/t3907-stash-show-config.sh new file +15 -0
new file mode 100755
index 0000000000..8fe369c1a1
@@ -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 () {

t/t3907-stash-show-config.sh line 25

Quoted 10 lines
t/t3907-stash-show-config.sh new file +10 -0
+	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

I think you are trying to protect yourself from an error triggered by unsetting what is not set, but for that, test_unconfig is probably a better choice, as it still catches errors of other types and ignores only that "unset a variable that is not set" error.

t/t3907-stash-show-config.sh line 31

Quoted 6 lines
t/t3907-stash-show-config.sh new file +6 -0
+	else
+		test_config stash.showPatch "$2"
+	fi &&
+
+	shift &&
+	shift &&

You can use "shift 2 &&" here (not worth a reroll).

t/t3907-stash-show-config.sh line 33

Quoted 3 lines
t/t3907-stash-show-config.sh new file +2 -0
+	echo 2 >file.t &&
 
+	git diff "$@" >expect &&

When the caller does not give $3 to this function, it does not look at 'expect'. I think it is clearer if you did

	if test $# != 0
	then
		git diff "$@" >expect
	fi &&

here, and ...

t/t3907-stash-show-config.sh line 37

Quoted 4 lines
t/t3907-stash-show-config.sh new file +4 -0
+	git stash &&
+	git stash show >actual &&
+
+	if test -z "$1"

... wrote this as

	if test $# = 0

The only difference between '-z "$1"' and '$# = 0' is when he caller passes an empty string to the function as $3, which you never do, so the distinction is theoretical, but using $# makes your intention clear that you do not mean to treat an empty string any specially.

Quoted 44 lines
t/t3907-stash-show-config.sh new file +44 -0
+	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
#118 lore raw

Re: [GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config

Hi Junio,

Quoted 17 lines · Junio C Hamano

t/t3907-stash-show-config.sh line 25

Quoted 11 lines · Paul-Sebastian Ungureanu
t/t3907-stash-show-config.sh new file +11 -0
+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

I think you are trying to protect yourself from an error triggered by unsetting what is not set, but for that, test_unconfig is probably a better choice, as it still catches errors of other types and ignores only that "unset a variable that is not set" error.

My mistake. I did not realize that tehere is a test_unconfig when I suggested the current version.

Quoted 4 lines

t/t3907-stash-show-config.sh line 31

Quoted 2 lines
t/t3907-stash-show-config.sh new file +2 -0
+	shift &&
+	shift &&

You can use "shift 2 &&" here (not worth a reroll).

Again, my mistake. I was not sure how portable that construct is. (Is it?)

Quoted 22 lines

t/t3907-stash-show-config.sh line 33

Quoted 4 lines
t/t3907-stash-show-config.sh new file +2 -0
+	echo 2 >file.t &&
 
+	git diff "$@" >expect &&

When the caller does not give $3 to this function, it does not look at 'expect'. I think it is clearer if you did

	if test $# != 0
	then
		git diff "$@" >expect
	fi &&

here, and ...

t/t3907-stash-show-config.sh line 37

Quoted 4 lines
t/t3907-stash-show-config.sh new file +4 -0
+	git stash &&
+	git stash show >actual &&
+
+	if test -z "$1"

... wrote this as

Quoted 6 lines
	if test $# = 0

The only difference between '-z "$1"' and '$# = 0' is when he caller passes an empty string to the function as $3, which you never do, so the distinction is theoretical, but using $# makes your intention clear that you do not mean to treat an empty string any specially.

Good advice in general (in this case, we know the callers).

Paul, when it comes to shell scripting, listen to Junio more than myself because he is even more of a shell script wiz than I am.

Ciao,
Dscho

#119 lore raw

PATCH v8 6/20 stash: convert apply to builtin

In reply to Paul-Sebastian Ungureanu

From: Joel Teichroeb <[email protected]>

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.

---
 .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
.gitignore +1 -0
index ffceea7d59..b59661cb88 100644
@@ -157,6 +157,7 @@
 /git-show-ref
 /git-stage
 /git-stash
+/git-stash--helper
 /git-status
 /git-stripspace
 /git-submodule
Makefile +1 -0
index d03df31c2a..f900c68e69 100644
@@ -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
builtin.h +1 -0
index 99206df4bd..317bc338f7 100644
@@ -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);
builtin/stash--helper.c new file +452 -0

452 lines not on this page · raw message

git-stash.sh +7 -71

90 lines not on this page · raw message

git.c +1 -0
index c27c38738b..3c0e762d7d 100644
@@ -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 },
Signature · 1 line
2.19.0.rc0.22.gc26283d74e