{"thread":{"id":"48162","subject":"[PATCH v3 0/5] Convert some stash functionality to a builtin","startedAt":"2018-03-27T05:45:31Z","lastAt":"2018-03-27T16:20:29Z","messageCount":10,"participants":["Joel Teichroeb","Johannes Schindelin"],"isPatch":true,"patchVersion":3,"patchTotal":5},"messages":[{"id":"343101","messageId":"20180327054432.26419-1-joel@teichroeb.net","threadId":"48162","inReplyTo":null,"subject":"[PATCH v3 0/5] Convert some stash functionality to a builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-03-27T05:44:27Z","receivedAt":"2018-03-27T05:45:31Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"I've been working on converting all of git stash to be a\nbuiltin, however it's hard to get it all working at once with\nlimited time, so I've moved around half of it to a new\nstash--helper builtin and called these functions from the shell\nscript. Once this is stabalized, it should be easier to convert\nthe rest of the commands one at a time without breaking\nanything.\n\nI've sent most of this code before, but that was targetting a\nfull replacement of stash. The code is overall the same, but\nwith some code review changes and updates for internal api\nchanges.\n\nSince there seems to be interest from GSOC students who want to\nwork on converting builtins, I figured I should finish what I\nhave that works now so they could build on top of it.\n\nPrevious threads:\nv1: https://public-inbox.org/git/20180325173916.GE10909@hank/T/\nv2: https://public-inbox.org/git/20180326011426.19159-1-joel@teichroeb.net/\n\nChanges from v2:\n - Fixed formatting (I keep forgetting to set vim to tabs)\n - Renamed destroy to free\n - Redid my tests to validate more (Thanks Johannes)\n - Deleted more shell code that isn't needed anymore\n\nJoel Teichroeb (5):\n  stash: improve option parsing test coverage\n  stash: convert apply to builtin\n  stash: convert drop and clear to builtin\n  stash: convert branch to builtin\n  stash: convert pop to builtin\n\n .gitignore              |   1 +\n Makefile                |   1 +\n builtin.h               |   1 +\n builtin/stash--helper.c | 631 ++++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 136 +----------\n git.c                   |   1 +\n t/t3903-stash.sh        |  16 ++\n 7 files changed, 659 insertions(+), 128 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\n-- \n2.16.2\n\n"},{"id":"343102","messageId":"20180327054432.26419-2-joel@teichroeb.net","threadId":"48162","inReplyTo":"20180327054432.26419-1-joel@teichroeb.net","subject":"[PATCH v3 1/5] stash: improve option parsing test coverage","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-03-27T05:44:28Z","receivedAt":"2018-03-27T05:45:33Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"In preparation for converting the stash command incrementally to\na builtin command, this patch improves test coverage of the option\nparsing. Both for having too many paramerters, or too few.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\n---\n t/t3903-stash.sh | 16 ++++++++++++++++\n 1 file changed, 16 insertions(+)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex aefde7b17..8a666c60c 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -84,6 +84,17 @@ test_expect_success 'apply stashed changes (including index)' '\n \ttest 1 = $(git show HEAD:file)\n '\n \n+test_expect_success 'giving too many ref agruments does nothing' '\n+\n+\tfor type in apply drop pop show \"branch stash-branch\"\n+\tdo\n+\t\ttest-chmtime =123456789 file &&\n+\t\ttest_must_fail git stash $type stash@{0} stash@{1} 2>err &&\n+\t\ttest_i18ngrep \"Too many\" err &&\n+\t\ttest 123456789 = $(test-chmtime -v +0 file | sed 's/[^0-9].*$//') || return 1\n+\tdone\n+'\n+\n test_expect_success 'unstashing in a subdirectory' '\n \tgit reset --hard HEAD &&\n \tmkdir subdir &&\n@@ -479,6 +490,11 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n+test_expect_success 'stash branch complains with no arguments' '\n+\ttest_must_fail git stash branch 2>err &&\n+\ttest_i18ngrep \"No branch name specified\" err\n+'\n+\n test_expect_success 'stash show format defaults to --stat' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n-- \n2.16.2\n\n"},{"id":"343103","messageId":"20180327054432.26419-3-joel@teichroeb.net","threadId":"48162","inReplyTo":"20180327054432.26419-1-joel@teichroeb.net","subject":"[PATCH v3 2/5] stash: convert apply to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-03-27T05:44:29Z","receivedAt":"2018-03-27T05:45:44Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"Add a bulitin helper for performing stash commands. Converting\nall at once proved hard to review, so starting with just apply\nlet conversion get started without the other command being\nfinished.\n\nThe helper is being implemented as a drop in replacement for\nstash so that when it is complete it can simply be renamed and\nthe shell script deleted.\n\nDelete the contents of the apply_stash shell function and replace\nit with a call to stash--helper apply until pop is also\nconverted.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\n---\n .gitignore              |   1 +\n Makefile                |   1 +\n builtin.h               |   1 +\n builtin/stash--helper.c | 442 ++++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  75 +-------\n git.c                   |   1 +\n 6 files changed, 451 insertions(+), 70 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 833ef3b0b..296d5f376 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -152,6 +152,7 @@\n /git-show-ref\n /git-stage\n /git-stash\n+/git-stash--helper\n /git-status\n /git-stripspace\n /git-submodule\ndiff --git a/Makefile b/Makefile\nindex 96f6138f6..6cfdbe9a3 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1028,6 +1028,7 @@ BUILTIN_OBJS += builtin/send-pack.o\n BUILTIN_OBJS += builtin/shortlog.o\n BUILTIN_OBJS += builtin/show-branch.o\n BUILTIN_OBJS += builtin/show-ref.o\n+BUILTIN_OBJS += builtin/stash--helper.o\n BUILTIN_OBJS += builtin/stripspace.o\n BUILTIN_OBJS += builtin/submodule--helper.o\n BUILTIN_OBJS += builtin/symbolic-ref.o\ndiff --git a/builtin.h b/builtin.h\nindex 42378f3aa..a14fd85b0 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -219,6 +219,7 @@ extern int cmd_shortlog(int argc, const char **argv, const char *prefix);\n extern int cmd_show(int argc, const char **argv, const char *prefix);\n extern int cmd_show_branch(int argc, const char **argv, const char *prefix);\n extern int cmd_status(int argc, const char **argv, const char *prefix);\n+extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);\n extern int cmd_stripspace(int argc, const char **argv, const char *prefix);\n extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);\n extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nnew file mode 100644\nindex 000000000..df3c8e1e6\n--- /dev/null\n+++ b/builtin/stash--helper.c\n@@ -0,0 +1,442 @@\n+#include \"builtin.h\"\n+#include \"config.h\"\n+#include \"parse-options.h\"\n+#include \"refs.h\"\n+#include \"lockfile.h\"\n+#include \"cache-tree.h\"\n+#include \"unpack-trees.h\"\n+#include \"merge-recursive.h\"\n+#include \"argv-array.h\"\n+#include \"run-command.h\"\n+#include \"dir.h\"\n+\n+static const char * const git_stash_helper_usage[] = {\n+\tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n+\tNULL\n+};\n+\n+static const char * const git_stash_helper_apply_usage[] = {\n+\tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n+\tNULL\n+};\n+\n+static const char *ref_stash = \"refs/stash\";\n+static int quiet;\n+static char stash_index_path[PATH_MAX];\n+\n+struct stash_info {\n+\tstruct object_id w_commit;\n+\tstruct object_id b_commit;\n+\tstruct object_id i_commit;\n+\tstruct object_id u_commit;\n+\tstruct object_id w_tree;\n+\tstruct object_id b_tree;\n+\tstruct object_id i_tree;\n+\tstruct object_id u_tree;\n+\tstruct strbuf revision;\n+\tint is_stash_ref;\n+\tint has_u;\n+};\n+\n+static int get_symbolic_name(const char *symbolic, struct strbuf *out)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"rev-parse\", \"--symbolic-full-name\", NULL);\n+\targv_array_push(&cp.args, symbolic);\n+\treturn pipe_command(&cp, NULL, 0, out, 0, NULL, 0);\n+}\n+\n+static int have_stash(void)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\tcp.git_cmd = 1;\n+\tcp.no_stdout = 1;\n+\targv_array_pushl(&cp.args, \"rev-parse\", \"--verify\", \"--quiet\", NULL);\n+\targv_array_push(&cp.args, ref_stash);\n+\treturn pipe_command(&cp, NULL, 0, NULL, 0, NULL, 0);\n+}\n+\n+static void free_stash_info(struct stash_info *info)\n+{\n+\tstrbuf_release(&info->revision);\n+}\n+\n+static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n+{\n+\tstruct strbuf w_commit_rev = STRBUF_INIT;\n+\tstruct strbuf b_commit_rev = STRBUF_INIT;\n+\tstruct strbuf w_tree_rev = STRBUF_INIT;\n+\tstruct strbuf b_tree_rev = STRBUF_INIT;\n+\tstruct strbuf i_tree_rev = STRBUF_INIT;\n+\tstruct strbuf u_tree_rev = STRBUF_INIT;\n+\tstruct strbuf symbolic = STRBUF_INIT;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tint ret;\n+\tconst char *revision;\n+\tconst char *commit = NULL;\n+\tchar *end_of_rev;\n+\tinfo->is_stash_ref = 0;\n+\n+\tif (argc > 1) {\n+\t\tint i;\n+\t\tstruct strbuf refs_msg = STRBUF_INIT;\n+\t\tfor (i = 0; i < argc; ++i)\n+\t\t\tstrbuf_addf(&refs_msg, \" '%s'\", argv[i]);\n+\n+\t\tfprintf_ln(stderr, _(\"Too many revisions specified:%s\"), refs_msg.buf);\n+\t\tstrbuf_release(&refs_msg);\n+\n+\t\treturn -1;\n+\t}\n+\n+\tif (argc == 1)\n+\t\tcommit = argv[0];\n+\n+\tstrbuf_init(&info->revision, 0);\n+\tif (commit == NULL) {\n+\t\tif (have_stash()) {\n+\t\t\tfree_stash_info(info);\n+\t\t\treturn error(_(\"No stash entries found.\"));\n+\t\t}\n+\n+\t\tstrbuf_addf(&info->revision, \"%s@{0}\", ref_stash);\n+\t} else if (strspn(commit, \"0123456789\") == strlen(commit)) {\n+\t\tstrbuf_addf(&info->revision, \"%s@{%s}\", ref_stash, commit);\n+\t} else {\n+\t\tstrbuf_addstr(&info->revision, commit);\n+\t}\n+\n+\trevision = info->revision.buf;\n+\n+\tstrbuf_addstr(&w_commit_rev, revision);\n+\n+\tret = !get_oid(w_commit_rev.buf, &info->w_commit);\n+\n+\tstrbuf_release(&w_commit_rev);\n+\n+\tif (!ret) {\n+\t\tfree_stash_info(info);\n+\t\treturn error(_(\"%s is not a valid reference\"), revision);\n+\t}\n+\n+\tstrbuf_addf(&b_commit_rev, \"%s^1\", revision);\n+\tstrbuf_addf(&w_tree_rev, \"%s:\", revision);\n+\tstrbuf_addf(&b_tree_rev, \"%s^1:\", revision);\n+\tstrbuf_addf(&i_tree_rev, \"%s^2:\", revision);\n+\n+\tret = !get_oid(b_commit_rev.buf, &info->b_commit) &&\n+\t\t!get_oid(w_tree_rev.buf, &info->w_tree) &&\n+\t\t!get_oid(b_tree_rev.buf, &info->b_tree) &&\n+\t\t!get_oid(i_tree_rev.buf, &info->i_tree);\n+\n+\tstrbuf_release(&b_commit_rev);\n+\tstrbuf_release(&w_tree_rev);\n+\tstrbuf_release(&b_tree_rev);\n+\tstrbuf_release(&i_tree_rev);\n+\n+\tif (!ret) {\n+\t\tfree_stash_info(info);\n+\t\treturn error(_(\"'%s' is not a stash-like commit\"), revision);\n+\t}\n+\n+\tstrbuf_addf(&u_tree_rev, \"%s^3:\", revision);\n+\n+\tinfo->has_u = !get_oid(u_tree_rev.buf, &info->u_tree);\n+\n+\tstrbuf_release(&u_tree_rev);\n+\n+\tend_of_rev = strchrnul(revision, '@');\n+\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n+\n+\tret = get_symbolic_name(symbolic.buf, &out);\n+\tstrbuf_release(&symbolic);\n+\tif (ret) {\n+\t\tfree_stash_info(info);\n+\t\tstrbuf_release(&out);\n+\t\treturn -1;\n+\t}\n+\n+\tif (out.len - 1 == strlen(ref_stash))\n+\t\tinfo->is_stash_ref = !strncmp(out.buf, ref_stash, out.len - 1);\n+\tstrbuf_release(&out);\n+\n+\treturn 0;\n+}\n+\n+static int reset_tree(struct object_id *i_tree, int update, int reset)\n+{\n+\tstruct unpack_trees_options opts;\n+\tint nr_trees = 1;\n+\tstruct tree_desc t[MAX_UNPACK_TREES];\n+\tstruct tree *tree;\n+\tstruct lock_file lock_file = LOCK_INIT;\n+\n+\tread_cache_preload(NULL);\n+\tif (refresh_cache(REFRESH_QUIET))\n+\t\treturn -1;\n+\n+\thold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);\n+\n+\tmemset(&opts, 0, sizeof(opts));\n+\n+\ttree = parse_tree_indirect(i_tree);\n+\tif (parse_tree(tree))\n+\t\treturn -1;\n+\n+\tinit_tree_desc(t, tree->buffer, tree->size);\n+\n+\topts.head_idx = 1;\n+\topts.src_index = &the_index;\n+\topts.dst_index = &the_index;\n+\topts.merge = 1;\n+\topts.reset = reset;\n+\topts.update = update;\n+\topts.fn = oneway_merge;\n+\n+\tif (unpack_trees(nr_trees, t, &opts))\n+\t\treturn -1;\n+\n+\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n+\t\treturn error(_(\"unable to write new index file\"));\n+\n+\treturn 0;\n+}\n+\n+static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tconst char *w_commit_hex = oid_to_hex(w_commit);\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"diff-tree\", \"--binary\", NULL);\n+\targv_array_pushf(&cp.args, \"%s^2^..%s^2\", w_commit_hex, w_commit_hex);\n+\n+\treturn pipe_command(&cp, NULL, 0, out, 0, NULL, 0);\n+}\n+\n+static int apply_cached(struct strbuf *out)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"apply\", \"--cached\", NULL);\n+\treturn pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);\n+}\n+\n+static int reset_head(const char *prefix)\n+{\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\n+\targv_array_push(&args, \"reset\");\n+\treturn cmd_reset(args.argc, args.argv, prefix);\n+}\n+\n+static int diff_cached_index(struct strbuf *out, struct object_id *c_tree)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tconst char *c_tree_hex = oid_to_hex(c_tree);\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"diff-index\", \"--cached\", \"--name-only\", \"--diff-filter=A\", NULL);\n+\targv_array_push(&cp.args, c_tree_hex);\n+\treturn pipe_command(&cp, NULL, 0, out, 0, NULL, 0);\n+}\n+\n+static int update_index(struct strbuf *out)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"update-index\", \"--add\", \"--stdin\", NULL);\n+\treturn pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);\n+}\n+\n+static int restore_untracked(struct object_id *u_tree)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tint res;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_push(&cp.args, \"read-tree\");\n+\targv_array_push(&cp.args, oid_to_hex(u_tree));\n+\targv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\", stash_index_path);\n+\tif (run_command(&cp)) {\n+\t\tremove_path(stash_index_path);\n+\t\treturn -1;\n+\t}\n+\n+\tchild_process_init(&cp);\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"checkout-index\", \"--all\", NULL);\n+\targv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\", stash_index_path);\n+\n+\tres = run_command(&cp);\n+\tremove_path(stash_index_path);\n+\treturn res;\n+}\n+\n+static int do_apply_stash(const char *prefix, struct stash_info *info, int index)\n+{\n+\tstruct merge_options o;\n+\tstruct object_id c_tree;\n+\tstruct object_id index_tree;\n+\tconst struct object_id *bases[1];\n+\tint bases_count = 1;\n+\tstruct commit *result;\n+\tint ret;\n+\tint has_index = index;\n+\n+\tread_cache_preload(NULL);\n+\tif (refresh_cache(REFRESH_QUIET))\n+\t\treturn -1;\n+\n+\tif (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))\n+\t\treturn error(_(\"Cannot apply a stash in the middle of a merge\"));\n+\n+\tif (index) {\n+\t\tif (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree, &info->i_tree)) {\n+\t\t\thas_index = 0;\n+\t\t} else {\n+\t\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\t\tif (diff_tree_binary(&out, &info->w_commit)) {\n+\t\t\t\tstrbuf_release(&out);\n+\t\t\t\treturn -1;\n+\t\t\t}\n+\n+\t\t\tret = apply_cached(&out);\n+\t\t\tstrbuf_release(&out);\n+\t\t\tif (ret)\n+\t\t\t\treturn -1;\n+\n+\t\t\tdiscard_cache();\n+\t\t\tread_cache();\n+\t\t\tif (write_cache_as_tree(&index_tree, 0, NULL))\n+\t\t\t\treturn -1;\n+\n+\t\t\treset_head(prefix);\n+\t\t}\n+\t}\n+\n+\tif (info->has_u) {\n+\t\tif (restore_untracked(&info->u_tree))\n+\t\t\treturn error(_(\"Could not restore untracked files from stash\"));\n+\t}\n+\n+\tinit_merge_options(&o);\n+\n+\to.branch1 = \"Updated upstream\";\n+\to.branch2 = \"Stashed changes\";\n+\n+\tif (!oidcmp(&info->b_tree, &c_tree))\n+\t\to.branch1 = \"Version stash was based on\";\n+\n+\tif (quiet)\n+\t\to.verbosity = 0;\n+\n+\tif (o.verbosity >= 3)\n+\t\tprintf_ln(_(\"Merging %s with %s\"), o.branch1, o.branch2);\n+\n+\tbases[0] = &info->b_tree;\n+\n+\tret = merge_recursive_generic(&o, &c_tree, &info->w_tree, bases_count, bases, &result);\n+\tif (ret != 0) {\n+\t\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\t\targv_array_push(&args, \"rerere\");\n+\t\tcmd_rerere(args.argc, args.argv, prefix);\n+\n+\t\tif (index)\n+\t\t\tfprintf_ln(stderr, _(\"Index was not unstashed.\"));\n+\n+\t\treturn ret;\n+\t}\n+\n+\tif (has_index) {\n+\t\tif (reset_tree(&index_tree, 0, 0))\n+\t\t\treturn -1;\n+\t} else {\n+\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\tif (diff_cached_index(&out, &c_tree)) {\n+\t\t\tstrbuf_release(&out);\n+\t\t\treturn -1;\n+\t\t}\n+\n+\t\tif (reset_tree(&c_tree, 0, 1)) {\n+\t\t\tstrbuf_release(&out);\n+\t\t\treturn -1;\n+\t\t}\n+\n+\t\tret = update_index(&out);\n+\t\tstrbuf_release(&out);\n+\t\tif (ret)\n+\t\t\treturn -1;\n+\n+\t\tdiscard_cache();\n+\t}\n+\n+\tif (!quiet) {\n+\t\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\t\targv_array_push(&args, \"status\");\n+\t\tcmd_status(args.argc, args.argv, prefix);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint index = 0;\n+\tstruct stash_info info;\n+\tint ret;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n+\t\tOPT_BOOL(0, \"index\", &index,\n+\t\t\tN_(\"attempt to recreate the index\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\tgit_stash_helper_apply_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tret = do_apply_stash(prefix, &info, index);\n+\tfree_stash_info(&info);\n+\treturn ret;\n+}\n+\n+int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n+{\n+\tint result = 0;\n+\tpid_t pid = getpid();\n+\tconst char *index_file;\n+\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\tgit_config(git_default_config, NULL);\n+\n+\targc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,\n+\t\tPARSE_OPT_KEEP_UNKNOWN|PARSE_OPT_KEEP_DASHDASH);\n+\n+\tindex_file = get_index_file();\n+\txsnprintf(stash_index_path, PATH_MAX, \"%s.stash.%d\", index_file, pid);\n+\n+\tif (argc < 1)\n+\t\tusage_with_options(git_stash_helper_usage, options);\n+\telse if (!strcmp(argv[0], \"apply\"))\n+\t\tresult = apply_stash(argc, argv, prefix);\n+\telse {\n+\t\terror(_(\"unknown subcommand: %s\"), argv[0]);\n+\t\tusage_with_options(git_stash_helper_usage, options);\n+\t\tresult = 1;\n+\t}\n+\n+\treturn result;\n+}\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 94793c1a9..0b5d1f374 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -566,76 +566,11 @@ assert_stash_ref() {\n }\n \n apply_stash () {\n-\n-\tassert_stash_like \"$@\"\n-\n-\tgit update-index -q --refresh || die \"$(gettext \"unable to refresh index\")\"\n-\n-\t# current index state\n-\tc_tree=$(git write-tree) ||\n-\t\tdie \"$(gettext \"Cannot apply a stash in the middle of a merge\")\"\n-\n-\tunstashed_index_tree=\n-\tif test -n \"$INDEX_OPTION\" && test \"$b_tree\" != \"$i_tree\" &&\n-\t\t\ttest \"$c_tree\" != \"$i_tree\"\n-\tthen\n-\t\tgit diff-tree --binary $s^2^..$s^2 | git apply --cached\n-\t\ttest $? -ne 0 &&\n-\t\t\tdie \"$(gettext \"Conflicts in index. Try without --index.\")\"\n-\t\tunstashed_index_tree=$(git write-tree) ||\n-\t\t\tdie \"$(gettext \"Could not save index tree\")\"\n-\t\tgit reset\n-\tfi\n-\n-\tif test -n \"$u_tree\"\n-\tthen\n-\t\tGIT_INDEX_FILE=\"$TMPindex\" git read-tree \"$u_tree\" &&\n-\t\tGIT_INDEX_FILE=\"$TMPindex\" git checkout-index --all &&\n-\t\trm -f \"$TMPindex\" ||\n-\t\tdie \"$(gettext \"Could not restore untracked files from stash entry\")\"\n-\tfi\n-\n-\teval \"\n-\t\tGITHEAD_$w_tree='Stashed changes' &&\n-\t\tGITHEAD_$c_tree='Updated upstream' &&\n-\t\tGITHEAD_$b_tree='Version stash was based on' &&\n-\t\texport GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree\n-\t\"\n-\n-\tif test -n \"$GIT_QUIET\"\n-\tthen\n-\t\tGIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY\n-\tfi\n-\tif git merge-recursive $b_tree -- $c_tree $w_tree\n-\tthen\n-\t\t# No conflict\n-\t\tif test -n \"$unstashed_index_tree\"\n-\t\tthen\n-\t\t\tgit read-tree \"$unstashed_index_tree\"\n-\t\telse\n-\t\t\ta=\"$TMP-added\" &&\n-\t\t\tgit diff-index --cached --name-only --diff-filter=A $c_tree >\"$a\" &&\n-\t\t\tgit read-tree --reset $c_tree &&\n-\t\t\tgit update-index --add --stdin <\"$a\" ||\n-\t\t\t\tdie \"$(gettext \"Cannot unstage modified files\")\"\n-\t\t\trm -f \"$a\"\n-\t\tfi\n-\t\tsquelch=\n-\t\tif test -n \"$GIT_QUIET\"\n-\t\tthen\n-\t\t\tsquelch='>/dev/null 2>&1'\n-\t\tfi\n-\t\t(cd \"$START_DIR\" && eval \"git status $squelch\") || :\n-\telse\n-\t\t# Merge conflict; keep the exit status from merge-recursive\n-\t\tstatus=$?\n-\t\tgit rerere\n-\t\tif test -n \"$INDEX_OPTION\"\n-\t\tthen\n-\t\t\tgettextln \"Index was not unstashed.\" >&2\n-\t\tfi\n-\t\texit $status\n-\tfi\n+\tcd \"$START_DIR\"\n+\tgit stash--helper apply \"$@\"\n+\tres=$?\n+\tcd_to_toplevel\n+\treturn $res\n }\n \n pop_stash() {\ndiff --git a/git.c b/git.c\nindex ceaa58ef4..6ffe6364a 100644\n--- a/git.c\n+++ b/git.c\n@@ -466,6 +466,7 @@ static struct cmd_struct commands[] = {\n \t{ \"show-branch\", cmd_show_branch, RUN_SETUP },\n \t{ \"show-ref\", cmd_show_ref, RUN_SETUP },\n \t{ \"stage\", cmd_add, RUN_SETUP | NEED_WORK_TREE },\n+\t{ \"stash--helper\", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },\n \t{ \"status\", cmd_status, RUN_SETUP | NEED_WORK_TREE },\n \t{ \"stripspace\", cmd_stripspace },\n \t{ \"submodule--helper\", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX},\n-- \n2.16.2\n\n"},{"id":"343104","messageId":"20180327054432.26419-4-joel@teichroeb.net","threadId":"48162","inReplyTo":"20180327054432.26419-1-joel@teichroeb.net","subject":"[PATCH v3 3/5] stash: convert drop and clear to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-03-27T05:44:30Z","receivedAt":"2018-03-27T05:45:46Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"Add the drop and clear commands to the builtin helper. These two\nare each simple, but are being added together as they are quite\nrelated.\n\nWe have to unfortunately keep the drop and clear functions in the\nshell script as functions are called with parameters internally\nthat are not valid when the commands are called externally. Once\npop is converted they can both be removed.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\n---\n builtin/stash--helper.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   4 +-\n 2 files changed, 103 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex df3c8e1e6..0d7a0d55e 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -11,7 +11,14 @@\n #include \"dir.h\"\n \n static const char * const git_stash_helper_usage[] = {\n+\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash--helper clear\"),\n+\tNULL\n+};\n+\n+static const char * const git_stash_helper_drop_usage[] = {\n+\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tNULL\n };\n \n@@ -20,6 +27,11 @@ static const char * const git_stash_helper_apply_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_clear_usage[] = {\n+\tN_(\"git stash--helper clear\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static char stash_index_path[PATH_MAX];\n@@ -166,6 +178,29 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n \treturn 0;\n }\n \n+static int do_clear_stash(void)\n+{\n+\tstruct object_id obj;\n+\tif (get_oid(ref_stash, &obj))\n+\t\treturn 0;\n+\n+\treturn delete_ref(NULL, ref_stash, &obj, 0);\n+}\n+\n+static int clear_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options, git_stash_helper_clear_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n+\n+\tif (argc != 0)\n+\t\treturn error(_(\"git stash--helper clear with parameters is unimplemented\"));\n+\n+\treturn do_clear_stash();\n+}\n+\n static int reset_tree(struct object_id *i_tree, int update, int reset)\n {\n \tstruct unpack_trees_options opts;\n@@ -410,6 +445,68 @@ static int apply_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int do_drop_stash(const char *prefix, struct stash_info *info)\n+{\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\tint ret;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\targv_array_pushl(&args, \"reflog\", \"delete\", \"--updateref\", \"--rewrite\", NULL);\n+\targv_array_push(&args, info->revision.buf);\n+\tret = cmd_reflog(args.argc, args.argv, prefix);\n+\tif (!ret) {\n+\t\tif (!quiet)\n+\t\t\tprintf(_(\"Dropped %s (%s)\\n\"), info->revision.buf, oid_to_hex(&info->w_commit));\n+\t} else {\n+\t\treturn error(_(\"%s: Could not drop stash entry\"), info->revision.buf);\n+\t}\n+\n+\tcp.git_cmd = 1;\n+\t/* Even though --quiet is specified, rev-parse still outputs the hash */\n+\tcp.no_stdout = 1;\n+\targv_array_pushl(&cp.args, \"rev-parse\", \"--verify\", \"--quiet\", NULL);\n+\targv_array_pushf(&cp.args, \"%s@{0}\", ref_stash);\n+\tret = run_command(&cp);\n+\n+\tif (ret)\n+\t\tdo_clear_stash();\n+\n+\treturn 0;\n+}\n+\n+static int assert_stash_ref(struct stash_info *info)\n+{\n+\tif (!info->is_stash_ref)\n+\t\treturn error(_(\"'%s' is not a stash reference\"), info->revision.buf);\n+\n+\treturn 0;\n+}\n+\n+static int drop_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct stash_info info;\n+\tint ret;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\tgit_stash_helper_drop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tif (assert_stash_ref(&info)) {\n+\t\tfree_stash_info(&info);\n+\t\treturn -1;\n+\t}\n+\n+\tret = do_drop_stash(prefix, &info);\n+\tfree_stash_info(&info);\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tint result = 0;\n@@ -432,6 +529,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tusage_with_options(git_stash_helper_usage, options);\n \telse if (!strcmp(argv[0], \"apply\"))\n \t\tresult = apply_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"clear\"))\n+\t\tresult = clear_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"drop\"))\n+\t\tresult = drop_stash(argc, argv, prefix);\n \telse {\n \t\terror(_(\"unknown subcommand: %s\"), argv[0]);\n \t\tusage_with_options(git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 0b5d1f374..0b8f07b38 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -652,7 +652,7 @@ apply)\n \t;;\n clear)\n \tshift\n-\tclear_stash \"$@\"\n+\tgit stash--helper clear \"$@\"\n \t;;\n create)\n \tshift\n@@ -664,7 +664,7 @@ store)\n \t;;\n drop)\n \tshift\n-\tdrop_stash \"$@\"\n+\tgit stash--helper drop \"$@\"\n \t;;\n pop)\n \tshift\n-- \n2.16.2\n\n"},{"id":"343105","messageId":"20180327054432.26419-5-joel@teichroeb.net","threadId":"48162","inReplyTo":"20180327054432.26419-1-joel@teichroeb.net","subject":"[PATCH v3 4/5] stash: convert branch to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-03-27T05:44:31Z","receivedAt":"2018-03-27T05:45:48Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"Add stash branch to the helper and delete the apply_to_branch\nfunction from the shell script.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\n---\n builtin/stash--helper.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 17 ++---------------\n 2 files changed, 49 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 0d7a0d55e..d755faf33 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,6 +13,7 @@\n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n \tNULL\n };\n@@ -27,6 +28,11 @@ static const char * const git_stash_helper_apply_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_branch_usage[] = {\n+\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n+\tNULL\n+};\n+\n static const char * const git_stash_helper_clear_usage[] = {\n \tN_(\"git stash--helper clear\"),\n \tNULL\n@@ -507,6 +513,45 @@ static int drop_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int branch_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tconst char *branch = NULL;\n+\tint ret;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\tstruct stash_info info;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\tgit_stash_helper_branch_usage, 0);\n+\n+\tif (argc == 0)\n+\t\treturn error(_(\"No branch name specified\"));\n+\n+\tbranch = argv[0];\n+\n+\tif (get_stash_info(&info, argc - 1, argv + 1))\n+\t\treturn -1;\n+\n+\targv_array_pushl(&args, \"checkout\", \"-b\", NULL);\n+\targv_array_push(&args, branch);\n+\targv_array_push(&args, oid_to_hex(&info.b_commit));\n+\tret = cmd_checkout(args.argc, args.argv, prefix);\n+\tif (ret) {\n+\t\tfree_stash_info(&info);\n+\t\treturn -1;\n+\t}\n+\n+\tret = do_apply_stash(prefix, &info, 1);\n+\tif (!ret && info.is_stash_ref)\n+\t\tret = do_drop_stash(prefix, &info);\n+\n+\tfree_stash_info(&info);\n+\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tint result = 0;\n@@ -533,6 +578,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tresult = clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\tresult = drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"branch\"))\n+\t\tresult = branch_stash(argc, argv, prefix);\n \telse {\n \t\terror(_(\"unknown subcommand: %s\"), argv[0]);\n \t\tusage_with_options(git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 0b8f07b38..c5fd4c6c4 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -598,20 +598,6 @@ drop_stash () {\n \tclear_stash\n }\n \n-apply_to_branch () {\n-\ttest -n \"$1\" || die \"$(gettext \"No branch name specified\")\"\n-\tbranch=$1\n-\tshift 1\n-\n-\tset -- --index \"$@\"\n-\tassert_stash_like \"$@\"\n-\n-\tgit checkout -b $branch $REV^ &&\n-\tapply_stash \"$@\" && {\n-\t\ttest -z \"$IS_STASH_REF\" || drop_stash \"$@\"\n-\t}\n-}\n-\n test \"$1\" = \"-p\" && set \"push\" \"$@\"\n \n PARSE_CACHE='--not-parsed'\n@@ -672,7 +658,8 @@ pop)\n \t;;\n branch)\n \tshift\n-\tapply_to_branch \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper branch \"$@\"\n \t;;\n *)\n \tcase $# in\n-- \n2.16.2\n\n"},{"id":"343106","messageId":"20180327054432.26419-6-joel@teichroeb.net","threadId":"48162","inReplyTo":"20180327054432.26419-1-joel@teichroeb.net","subject":"[PATCH v3 5/5] stash: convert pop to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-03-27T05:44:32Z","receivedAt":"2018-03-27T05:45:54Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"Add stash pop to the helper and delete the pop_stash, drop_stash,\nassert_stash_ref and pop_stash functions from the shell script\nnow that they are no longer needed.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\n---\n builtin/stash--helper.c | 41 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 50 ++++---------------------------------------------\n 2 files changed, 45 insertions(+), 46 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex d755faf33..53c0d2171 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,7 @@\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash--helper pop [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n@@ -23,6 +24,11 @@ static const char * const git_stash_helper_drop_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_pop_usage[] = {\n+\tN_(\"git stash--helper pop [--index] [-q|--quiet] [<stash>]\"),\n+\tNULL\n+};\n+\n static const char * const git_stash_helper_apply_usage[] = {\n \tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n \tNULL\n@@ -513,6 +519,39 @@ static int drop_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int pop_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint index = 0, ret;\n+\tstruct stash_info info;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n+\t\tOPT_BOOL(0, \"index\", &index,\n+\t\t\tN_(\"attempt to recreate the index\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\tgit_stash_helper_pop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tif (assert_stash_ref(&info)) {\n+\t\tfree_stash_info(&info);\n+\t\treturn -1;\n+\t}\n+\n+\tif (do_apply_stash(prefix, &info, index)) {\n+\t\tprintf_ln(_(\"The stash entry is kept in case you need it again.\"));\n+\t\tfree_stash_info(&info);\n+\t\treturn -1;\n+\t}\n+\n+\tret = do_drop_stash(prefix, &info);\n+\tfree_stash_info(&info);\n+\treturn ret;\n+}\n+\n static int branch_stash(int argc, const char **argv, const char *prefix)\n {\n \tconst char *branch = NULL;\n@@ -578,6 +617,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tresult = clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\tresult = drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"pop\"))\n+\t\tresult = pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\tresult = branch_stash(argc, argv, prefix);\n \telse {\ndiff --git a/git-stash.sh b/git-stash.sh\nindex c5fd4c6c4..8f2640fe9 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -554,50 +554,6 @@ assert_stash_like() {\n \t}\n }\n \n-is_stash_ref() {\n-\tis_stash_like \"$@\" && test -n \"$IS_STASH_REF\"\n-}\n-\n-assert_stash_ref() {\n-\tis_stash_ref \"$@\" || {\n-\t\targs=\"$*\"\n-\t\tdie \"$(eval_gettext \"'\\$args' is not a stash reference\")\"\n-\t}\n-}\n-\n-apply_stash () {\n-\tcd \"$START_DIR\"\n-\tgit stash--helper apply \"$@\"\n-\tres=$?\n-\tcd_to_toplevel\n-\treturn $res\n-}\n-\n-pop_stash() {\n-\tassert_stash_ref \"$@\"\n-\n-\tif apply_stash \"$@\"\n-\tthen\n-\t\tdrop_stash \"$@\"\n-\telse\n-\t\tstatus=$?\n-\t\tsay \"$(gettext \"The stash entry is kept in case you need it again.\")\"\n-\t\texit $status\n-\tfi\n-}\n-\n-drop_stash () {\n-\tassert_stash_ref \"$@\"\n-\n-\tgit reflog delete --updateref --rewrite \"${REV}\" &&\n-\t\tsay \"$(eval_gettext \"Dropped \\${REV} (\\$s)\")\" ||\n-\t\tdie \"$(eval_gettext \"\\${REV}: Could not drop stash entry\")\"\n-\n-\t# clear_stash if we just dropped the last stash entry\n-\tgit rev-parse --verify --quiet \"$ref_stash@{0}\" >/dev/null ||\n-\tclear_stash\n-}\n-\n test \"$1\" = \"-p\" && set \"push\" \"$@\"\n \n PARSE_CACHE='--not-parsed'\n@@ -634,7 +590,8 @@ push)\n \t;;\n apply)\n \tshift\n-\tapply_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper apply \"$@\"\n \t;;\n clear)\n \tshift\n@@ -654,7 +611,8 @@ drop)\n \t;;\n pop)\n \tshift\n-\tpop_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper pop \"$@\"\n \t;;\n branch)\n \tshift\n-- \n2.16.2\n\n"},{"id":"343130","messageId":"nycvar.QRO.7.76.6.1803271646520.77@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48162","inReplyTo":"20180327054432.26419-1-joel@teichroeb.net","subject":"Re: [PATCH v3 0/5] Convert some stash functionality to a builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-03-27T14:47:43Z","receivedAt":"2018-03-27T14:47:54Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Mon, 26 Mar 2018, Joel Teichroeb wrote:\n\n> I've been working on converting all of git stash to be a\n> builtin, however it's hard to get it all working at once with\n> limited time, so I've moved around half of it to a new\n> stash--helper builtin and called these functions from the shell\n> script. Once this is stabalized, it should be easier to convert\n> the rest of the commands one at a time without breaking\n> anything.\n> \n> I've sent most of this code before, but that was targetting a\n> full replacement of stash. The code is overall the same, but\n> with some code review changes and updates for internal api\n> changes.\n> \n> Since there seems to be interest from GSOC students who want to\n> work on converting builtins, I figured I should finish what I\n> have that works now so they could build on top of it.\n\nGreat! This will help tremendously, I am sure.\n\nCiao,\nDscho\n"},{"id":"343134","messageId":"nycvar.QRO.7.76.6.1803271705210.77@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48162","inReplyTo":"20180327054432.26419-3-joel@teichroeb.net","subject":"Re: [PATCH v3 2/5] stash: convert apply to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-03-27T15:07:18Z","receivedAt":"2018-03-27T15:07:26Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Mon, 26 Mar 2018, Joel Teichroeb wrote:\n\n> Add a bulitin helper for performing stash commands. Converting\n> all at once proved hard to review, so starting with just apply\n> let conversion get started without the other command being\n> finished.\n> \n> The helper is being implemented as a drop in replacement for\n> stash so that when it is complete it can simply be renamed and\n> the shell script deleted.\n> \n> Delete the contents of the apply_stash shell function and replace\n> it with a call to stash--helper apply until pop is also\n> converted.\n> \n> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>\n\nVery good!\n\nIn the interest of as incremental a change as possible, I would wager a\nbet that this is the best way we can go about it, later replacing the\nparts that still spawn Git processes (such as get_symbolic_name and\nhave_stash) with direct calls into libgit.a, one by one.\n\nThank you!\nDscho\n"},{"id":"343140","messageId":"nycvar.QRO.7.76.6.1803271744370.77@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48162","inReplyTo":"20180327054432.26419-3-joel@teichroeb.net","subject":"Re: [PATCH v3 2/5] stash: convert apply to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-03-27T16:02:30Z","receivedAt":"2018-03-27T16:02:39Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Mon, 26 Mar 2018, Joel Teichroeb wrote:\n\n> Add a bulitin helper for performing stash commands. Converting\n> all at once proved hard to review, so starting with just apply\n> let conversion get started without the other command being\n> finished.\n> \n> The helper is being implemented as a drop in replacement for\n> stash so that when it is complete it can simply be renamed and\n> the shell script deleted.\n> \n> Delete the contents of the apply_stash shell function and replace\n> it with a call to stash--helper apply until pop is also\n> converted.\n> \n> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>\n\nMakes sense.\n\nI need a couple of adjustments before it compiles on Windows:\n\n> [...]\n> +\n> +static int do_apply_stash(const char *prefix, struct stash_info *info, int index)\n> +{\n> +\tstruct merge_options o;\n> +\tstruct object_id c_tree;\n> +\tstruct object_id index_tree;\n> +\tconst struct object_id *bases[1];\n> +\tint bases_count = 1;\n> +\tstruct commit *result;\n> +\tint ret;\n> +\tint has_index = index;\n> +\n> +\tread_cache_preload(NULL);\n> +\tif (refresh_cache(REFRESH_QUIET))\n> +\t\treturn -1;\n> +\n> +\tif (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))\n\nWhen applied on top of current `master`, I need to replace the &c_tree by\nc_tree.hash.\n\nLikewise...\n\n> +\t\treturn error(_(\"Cannot apply a stash in the middle of a merge\"));\n> +\n> +\tif (index) {\n> +\t\tif (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree, &info->i_tree)) {\n> +\t\t\thas_index = 0;\n> +\t\t} else {\n> +\t\t\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t\t\tif (diff_tree_binary(&out, &info->w_commit)) {\n> +\t\t\t\tstrbuf_release(&out);\n> +\t\t\t\treturn -1;\n> +\t\t\t}\n> +\n> +\t\t\tret = apply_cached(&out);\n> +\t\t\tstrbuf_release(&out);\n> +\t\t\tif (ret)\n> +\t\t\t\treturn -1;\n> +\n> +\t\t\tdiscard_cache();\n> +\t\t\tread_cache();\n> +\t\t\tif (write_cache_as_tree(&index_tree, 0, NULL))\n\n... &index_tree -> index_tree.hash.\n\nThese are probably changed to use object_id's already in `pu`, I guess.\n\nI also need this change:\n\n> [...]\n> +\n> +\tindex_file = get_index_file();\n> +\txsnprintf(stash_index_path, PATH_MAX, \"%s.stash.%d\", index_file, pid);\n\nSince `pid_t` is `unsigned long long` on Windows, I changed the %d\" to\n%\"PRIuMAX and cast `pid` to `(uintmax_t)`.\n\nWith those changes, the entire patch series compiles here.\n\nBTW t3903 runs in 13m30s here with this patch series, 14m30s otherwise.\nThat might not seem like much, until you realize that t3903 *still*\nperforms a metric ton of Unix shell scripting outside of `git stash` (and\nthat is the reason for the slowness).\n\nCiao,\nDscho\n"},{"id":"343145","messageId":"CA+CzEk8wUbV1mbcejyBuJ1fC87XvQPHWBNzF7qGQmLcDyytWyQ@mail.gmail.com","threadId":"48162","inReplyTo":"nycvar.QRO.7.76.6.1803271744370.77@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","subject":"Re: [PATCH v3 2/5] stash: convert apply to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-03-27T16:20:00Z","receivedAt":"2018-03-27T16:20:29Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"On Tue, Mar 27, 2018 at 9:02 AM, Johannes Schindelin\n<Johannes.Schindelin@gmx.de> wrote:\n> Hi Joel,\n>\n> [...]\n>> +\n>> +static int do_apply_stash(const char *prefix, struct stash_info *info, int index)\n>> +{\n>> +     struct merge_options o;\n>> +     struct object_id c_tree;\n>> +     struct object_id index_tree;\n>> +     const struct object_id *bases[1];\n>> +     int bases_count = 1;\n>> +     struct commit *result;\n>> +     int ret;\n>> +     int has_index = index;\n>> +\n>> +     read_cache_preload(NULL);\n>> +     if (refresh_cache(REFRESH_QUIET))\n>> +             return -1;\n>> +\n>> +     if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0))\n>\n> When applied on top of current `master`, I need to replace the &c_tree by\n> c_tree.hash.\n>\n> Likewise...\n>\n\nI based this revision off next because of the object_id changes. I\nprobably should have mentioned in my cover-letter.\n\n>> [...]\n>> +\n>> +     index_file = get_index_file();\n>> +     xsnprintf(stash_index_path, PATH_MAX, \"%s.stash.%d\", index_file, pid);\n>\n> Since `pid_t` is `unsigned long long` on Windows, I changed the %d\" to\n> %\"PRIuMAX and cast `pid` to `(uintmax_t)`.\n\nThanks for testing on windows! I'll have that fixed in the next revision.\n"}]}