{"thread":{"id":"48222","subject":"[PATCH v5 0/5] Convert some stash functionality to a builtin","startedAt":"2018-04-05T02:28:30Z","lastAt":"2018-11-09T12:27:57Z","messageCount":181,"participants":["Joel Teichroeb","Christian Couder","Johannes Schindelin","Paul-Sebastian Ungureanu","Junio C Hamano","Thomas Gummerer","Paul Sebastian Ungureanu","Duy Nguyen","Ævar Arnfjörð Bjarmason","Eric Sunshine"],"isPatch":true,"patchVersion":5,"patchTotal":5},"messages":[{"id":"343833","messageId":"20180405022810.15796-1-joel@teichroeb.net","threadId":"48222","inReplyTo":null,"subject":"[PATCH v5 0/5] Convert some stash functionality to a builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-04-05T02:28:05Z","receivedAt":"2018-04-05T02:28:30Z","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\nThe code is based on next as write_cache_as_tree was changed to\ntake an object ID. It can easily be rebase on master by changing\nthe two calls to write_cache_as_tree to use tha hash.\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/\nv3: https://public-inbox.org/git/20180327054432.26419-1-joel@teichroeb.net/\nv4: https://public-inbox.org/git/20180328222129.22192-1-joel@teichroeb.net/\n\nChanges from v4:\n - Fixed a typo (Thanks Eric)\n - Redid my test again with input from Junio\n - Cleaned up usages of get_oid (Thanks Junio)\n - Slightly reduced calls to builtin functions (rerere, rev-parse)\n - Added comments clarifying why when forking/cmd_* is used\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 | 630 ++++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 136 +----------\n git.c                   |   1 +\n t/t3903-stash.sh        |  35 +++\n 7 files changed, 677 insertions(+), 128 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\n-- \n2.16.3\n\n"},{"id":"343834","messageId":"20180405022810.15796-2-joel@teichroeb.net","threadId":"48222","inReplyTo":"20180405022810.15796-1-joel@teichroeb.net","subject":"[PATCH v5 1/5] stash: improve option parsing test coverage","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-04-05T02:28:06Z","receivedAt":"2018-04-05T02:28:32Z","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 parameters, or too few.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\n---\n t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++\n 1 file changed, 35 insertions(+)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex aefde7b172..4eaa4cae9a 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '\n \ttest foo = \"$(cat file/file)\"\n '\n \n+test_expect_success 'giving too many ref agruments does not modify files' '\n+\tgit stash clear &&\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\techo foo >file2 &&\n+\tgit stash &&\n+\techo bar >file2 &&\n+\tgit stash &&\n+\ttest-chmtime =123456789 file2 &&\n+\tfor type in apply pop \"branch stash-branch\"\n+\tdo\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 file2 | sed 's/[^0-9].*$//') || return 1\n+\tdone\n+'\n+\n+test_expect_success 'giving too many ref agruments to drop does not drop anything' '\n+\tgit stash list > stashlist1 &&\n+\ttest_must_fail git stash drop stash@{0} stash@{1} 2>err &&\n+\ttest_i18ngrep \"Too many\" err &&\n+\tgit stash list > stashlist2 &&\n+\ttest_cmp stashlist1 stashlist2\n+'\n+\n+test_expect_success 'giving too many ref agruments to show does not show anything' '\n+\ttest_must_fail git stash show stash@{0} stash@{1} 2>err 1>out && # show must not show anything\n+\ttest_i18ngrep \"Too many\" err &&\n+\ttest_must_be_empty out\n+'\n+\n test_expect_success 'stash create - no changes' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n@@ -479,6 +509,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.3\n\n"},{"id":"343835","messageId":"20180405022810.15796-4-joel@teichroeb.net","threadId":"48222","inReplyTo":"20180405022810.15796-1-joel@teichroeb.net","subject":"[PATCH v5 3/5] stash: convert drop and clear to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-04-05T02:28:08Z","receivedAt":"2018-04-05T02:28:34Z","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 | 107 ++++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   4 +-\n 2 files changed, 109 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 9d00a9547d..520cd746c4 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,7 +12,14 @@\n #include \"rerere.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@@ -21,6 +28,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 struct strbuf stash_index_path = STRBUF_INIT;\n@@ -134,6 +146,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@@ -399,6 +434,74 @@ 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+\t/* reflog does not provide a simple function for deleting refs. One will\n+\t * need to be added to avoid implementing too much reflog code here\n+\t */\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+\t/* This could easily be replaced by get_oid, but currently it will throw a\n+\t * fatal error when a reflog is empty, which we can not recover from\n+\t */\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@@ -421,6 +524,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 0b5d1f3743..0b8f07b38a 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.3\n\n"},{"id":"343836","messageId":"20180405022810.15796-5-joel@teichroeb.net","threadId":"48222","inReplyTo":"20180405022810.15796-1-joel@teichroeb.net","subject":"[PATCH v5 4/5] stash: convert branch to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-04-05T02:28:09Z","receivedAt":"2018-04-05T02:28:36Z","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 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 17 ++---------------\n 2 files changed, 53 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 520cd746c4..486796bb6a 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -14,6 +14,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@@ -28,6 +29,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@@ -502,6 +508,49 @@ 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+\t/* Checkout does not currently provide a function for checking out a branch\n+\t * as cmd_checkout does a large amount of sanity checks first that we\n+\t * require here.\n+\t */\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@@ -528,6 +577,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 0b8f07b38a..c5fd4c6c44 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.3\n\n"},{"id":"343837","messageId":"20180405022810.15796-6-joel@teichroeb.net","threadId":"48222","inReplyTo":"20180405022810.15796-1-joel@teichroeb.net","subject":"[PATCH v5 5/5] stash: convert pop to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-04-05T02:28:10Z","receivedAt":"2018-04-05T02:28:37Z","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 486796bb6a..7c8950bc90 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,6 +13,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@@ -24,6 +25,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@@ -508,6 +514,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@@ -577,6 +616,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 c5fd4c6c44..8f2640fe90 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.3\n\n"},{"id":"343838","messageId":"20180405022810.15796-3-joel@teichroeb.net","threadId":"48222","inReplyTo":"20180405022810.15796-1-joel@teichroeb.net","subject":"[PATCH v5 2/5] stash: convert apply to builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-04-05T02:28:07Z","receivedAt":"2018-04-05T02:28:42Z","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 | 431 ++++++++++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  75 +--------\n git.c                   |   1 +\n 6 files changed, 440 insertions(+), 70 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 833ef3b0b7..296d5f376d 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 96f6138f63..6cfdbe9a32 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 42378f3aa4..a14fd85b0e 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 0000000000..9d00a9547d\n--- /dev/null\n+++ b/builtin/stash--helper.c\n@@ -0,0 +1,431 @@\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+#include \"rerere.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 struct strbuf stash_index_path = STRBUF_INIT;\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 grab_oid(struct object_id *oid, const char *fmt, const char *rev)\n+{\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tint ret;\n+\n+\tstrbuf_addf(&buf, fmt, rev);\n+\tret = get_oid(buf.buf, oid);\n+\tstrbuf_release(&buf);\n+\treturn ret;\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 symbolic = STRBUF_INIT;\n+\tint ret;\n+\tconst char *revision;\n+\tconst char *commit = NULL;\n+\tchar *end_of_rev;\n+\tchar *expanded_ref;\n+\tstruct object_id discard;\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 (!ref_exists(ref_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+\tif (get_oid(revision, &info->w_commit)) {\n+\t\terror(_(\"%s is not a valid reference\"), revision);\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\n+\n+\tif (grab_oid(&info->b_commit, \"%s^1\", revision) ||\n+\t\tgrab_oid(&info->w_tree, \"%s:\", revision) ||\n+\t\tgrab_oid(&info->b_tree, \"%s^1:\", revision) ||\n+\t\tgrab_oid(&info->i_tree, \"%s^2:\", revision)) {\n+\n+\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\n+\n+\tinfo->has_u = !grab_oid(&info->u_tree, \"%s^3:\", revision);\n+\n+\tend_of_rev = strchrnul(revision, '@');\n+\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n+\n+\tret = dwim_ref(symbolic.buf, symbolic.len, &discard, &expanded_ref);\n+\tstrbuf_release(&symbolic);\n+\tswitch (ret) {\n+\tcase 0: /* Not found, but valid ref */\n+\t\tinfo->is_stash_ref = 0;\n+\t\tbreak;\n+\tcase 1:\n+\t\tinfo->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n+\t\tbreak;\n+\tdefault: /* Invalid or ambiguous */\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\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+\t/* Diff-tree would not be very hard to replace with a native function,\n+\t * however it should be done together with apply_cached.\n+\t */\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+\t/* Apply currently only reads either from stdin or a file, thus\n+\t * apply_all_patches would have to be updated to optionally take a buffer\n+\t */\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+\t/* Reset is overall quite simple, however there is no current public API\n+\t * for resetting.\n+\t */\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+\t/* diff-index is very similar to diff-tree above, and should be converted\n+\t * together with update_index\n+\t */\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+\t/* Update-index is very complicated and may need to have a public function\n+\t * exposed in order to remove this forking\n+\t */\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+\t/*\n+\t * We need to run restore files from a given index, but without affecting\n+\t * the current index, so we use GIT_INDEX_FILE with run_command to fork\n+\t * processes that will not interfere.\n+\t */\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.buf);\n+\tif (run_command(&cp)) {\n+\t\tremove_path(stash_index_path.buf);\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.buf);\n+\n+\tres = run_command(&cp);\n+\tremove_path(stash_index_path.buf);\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\trerere(0);\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\t/* Status is quite simple and could be replaced with calls to wt_status\n+\t\t * in the future, but it adds complexities which may require more tests\n+\t\t */\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+\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file, (uintmax_t)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 94793c1a91..0b5d1f3743 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 ceaa58ef40..6ffe6364ac 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.3\n\n"},{"id":"343850","messageId":"CAP8UFD3SM9e+_VGpxVNzMoEYnyzkF0EzjAPjvbFakr9t9Z-muQ@mail.gmail.com","threadId":"48222","inReplyTo":"20180405022810.15796-3-joel@teichroeb.net","subject":"Re: [PATCH v5 2/5] stash: convert apply to builtin","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2018-04-05T07:50:39Z","receivedAt":"2018-04-05T07:50:46Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, Apr 5, 2018 at 4:28 AM, Joel Teichroeb <joel@teichroeb.net> wrote:\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> ---\n>  .gitignore              |   1 +\n>  Makefile                |   1 +\n>  builtin.h               |   1 +\n>  builtin/stash--helper.c | 431 ++++++++++++++++++++++++++++++++++++++++++++++++\n>  git-stash.sh            |  75 +--------\n>  git.c                   |   1 +\n>  6 files changed, 440 insertions(+), 70 deletions(-)\n>  create mode 100644 builtin/stash--helper.c\n>\n> diff --git a/.gitignore b/.gitignore\n> index 833ef3b0b7..296d5f376d 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\n> diff --git a/Makefile b/Makefile\n> index 96f6138f63..6cfdbe9a32 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\n> diff --git a/builtin.h b/builtin.h\n> index 42378f3aa4..a14fd85b0e 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);\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> new file mode 100644\n> index 0000000000..9d00a9547d\n> --- /dev/null\n> +++ b/builtin/stash--helper.c\n> @@ -0,0 +1,431 @@\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> +#include \"rerere.h\"\n> +\n> +static const char * const git_stash_helper_usage[] = {\n> +       N_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n> +       NULL\n> +};\n> +\n> +static const char * const git_stash_helper_apply_usage[] = {\n> +       N_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n> +       NULL\n> +};\n> +\n> +static const char *ref_stash = \"refs/stash\";\n> +static int quiet;\n> +static struct strbuf stash_index_path = STRBUF_INIT;\n> +\n> +struct stash_info {\n> +       struct object_id w_commit;\n> +       struct object_id b_commit;\n> +       struct object_id i_commit;\n> +       struct object_id u_commit;\n> +       struct object_id w_tree;\n> +       struct object_id b_tree;\n> +       struct object_id i_tree;\n> +       struct object_id u_tree;\n> +       struct strbuf revision;\n> +       int is_stash_ref;\n> +       int has_u;\n> +};\n> +\n> +static int grab_oid(struct object_id *oid, const char *fmt, const char *rev)\n> +{\n> +       struct strbuf buf = STRBUF_INIT;\n> +       int ret;\n> +\n> +       strbuf_addf(&buf, fmt, rev);\n> +       ret = get_oid(buf.buf, oid);\n> +       strbuf_release(&buf);\n> +       return ret;\n> +}\n> +\n> +static void free_stash_info(struct stash_info *info)\n> +{\n> +       strbuf_release(&info->revision);\n> +}\n> +\n> +static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n> +{\n> +       struct strbuf symbolic = STRBUF_INIT;\n> +       int ret;\n> +       const char *revision;\n> +       const char *commit = NULL;\n> +       char *end_of_rev;\n> +       char *expanded_ref;\n> +       struct object_id discard;\n> +\n> +       if (argc > 1) {\n> +               int i;\n> +               struct strbuf refs_msg = STRBUF_INIT;\n> +               for (i = 0; i < argc; ++i)\n> +                       strbuf_addf(&refs_msg, \" '%s'\", argv[i]);\n> +\n> +               fprintf_ln(stderr, _(\"Too many revisions specified:%s\"), refs_msg.buf);\n> +               strbuf_release(&refs_msg);\n> +\n> +               return -1;\n> +       }\n> +\n> +       if (argc == 1)\n> +               commit = argv[0];\n> +\n> +       strbuf_init(&info->revision, 0);\n> +       if (commit == NULL) {\n> +               if (!ref_exists(ref_stash)) {\n> +                       free_stash_info(info);\n> +                       return error(_(\"No stash entries found.\"));\n\nHere `info` is freed first and then `return error(...)` is used.\n\n> +               }\n> +\n> +               strbuf_addf(&info->revision, \"%s@{0}\", ref_stash);\n> +       } else if (strspn(commit, \"0123456789\") == strlen(commit)) {\n> +               strbuf_addf(&info->revision, \"%s@{%s}\", ref_stash, commit);\n> +       } else {\n> +               strbuf_addstr(&info->revision, commit);\n> +       }\n> +\n> +       revision = info->revision.buf;\n> +\n> +       if (get_oid(revision, &info->w_commit)) {\n> +               error(_(\"%s is not a valid reference\"), revision);\n> +               free_stash_info(info);\n> +               return -1;\n\nMaybe:\n\n               free_stash_info(info);\n               return error(_(\"%s is not a valid reference\"), revision);\n\nto save one line and be more consistent with above.\n\n> +       }\n> +\n> +       if (grab_oid(&info->b_commit, \"%s^1\", revision) ||\n> +               grab_oid(&info->w_tree, \"%s:\", revision) ||\n> +               grab_oid(&info->b_tree, \"%s^1:\", revision) ||\n> +               grab_oid(&info->i_tree, \"%s^2:\", revision)) {\n> +\n> +               error(_(\"'%s' is not a stash-like commit\"), revision);\n> +               free_stash_info(info);\n> +               return -1;\n\nHere also.\n\n> +       }\n> +\n> +       info->has_u = !grab_oid(&info->u_tree, \"%s^3:\", revision);\n> +\n> +       end_of_rev = strchrnul(revision, '@');\n> +       strbuf_add(&symbolic, revision, end_of_rev - revision);\n> +\n> +       ret = dwim_ref(symbolic.buf, symbolic.len, &discard, &expanded_ref);\n> +       strbuf_release(&symbolic);\n> +       switch (ret) {\n> +       case 0: /* Not found, but valid ref */\n> +               info->is_stash_ref = 0;\n> +               break;\n> +       case 1:\n> +               info->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n> +               break;\n> +       default: /* Invalid or ambiguous */\n> +               free_stash_info(info);\n> +               return -1;\n> +       }\n> +\n> +       return 0;\n> +}\n> +\n> +static int reset_tree(struct object_id *i_tree, int update, int reset)\n> +{\n> +       struct unpack_trees_options opts;\n> +       int nr_trees = 1;\n> +       struct tree_desc t[MAX_UNPACK_TREES];\n> +       struct tree *tree;\n> +       struct lock_file lock_file = LOCK_INIT;\n> +\n> +       read_cache_preload(NULL);\n> +       if (refresh_cache(REFRESH_QUIET))\n> +               return -1;\n> +\n> +       hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);\n> +\n> +       memset(&opts, 0, sizeof(opts));\n> +\n> +       tree = parse_tree_indirect(i_tree);\n> +       if (parse_tree(tree))\n> +               return -1;\n> +\n> +       init_tree_desc(t, tree->buffer, tree->size);\n> +\n> +       opts.head_idx = 1;\n> +       opts.src_index = &the_index;\n> +       opts.dst_index = &the_index;\n> +       opts.merge = 1;\n> +       opts.reset = reset;\n> +       opts.update = update;\n> +       opts.fn = oneway_merge;\n> +\n> +       if (unpack_trees(nr_trees, t, &opts))\n> +               return -1;\n> +\n> +       if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n> +               return error(_(\"unable to write new index file\"));\n> +\n> +       return 0;\n> +}\n> +\n> +static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)\n> +{\n> +       struct child_process cp = CHILD_PROCESS_INIT;\n> +       const char *w_commit_hex = oid_to_hex(w_commit);\n> +\n> +       /* Diff-tree would not be very hard to replace with a native function,\n> +        * however it should be done together with apply_cached.\n> +        */\n\nOur multi line comments are more like:\n\n       /*\n        * Diff-tree would not be very hard to replace with a native function,\n        * however it should be done together with apply_cached.\n        */\n\n> +       cp.git_cmd = 1;\n> +       argv_array_pushl(&cp.args, \"diff-tree\", \"--binary\", NULL);\n> +       argv_array_pushf(&cp.args, \"%s^2^..%s^2\", w_commit_hex, w_commit_hex);\n> +\n> +       return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);\n> +}\n> +\n> +static int apply_cached(struct strbuf *out)\n> +{\n> +       struct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +       /* Apply currently only reads either from stdin or a file, thus\n> +        * apply_all_patches would have to be updated to optionally take a buffer\n> +        */\n\nHere also and below the multi line comment style is not the recommended one.\n\n> +       cp.git_cmd = 1;\n> +       argv_array_pushl(&cp.args, \"apply\", \"--cached\", NULL);\n> +       return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);\n> +}\n\n[...]\n\n> +static int restore_untracked(struct object_id *u_tree)\n> +{\n> +       struct child_process cp = CHILD_PROCESS_INIT;\n> +       int res;\n> +\n> +       /*\n> +        * We need to run restore files from a given index, but without affecting\n> +        * the current index, so we use GIT_INDEX_FILE with run_command to fork\n> +        * processes that will not interfere.\n> +        */\n\nHere it is the recommended style.\n\n> +       cp.git_cmd = 1;\n> +       argv_array_push(&cp.args, \"read-tree\");\n> +       argv_array_push(&cp.args, oid_to_hex(u_tree));\n> +       argv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\", stash_index_path.buf);\n> +       if (run_command(&cp)) {\n> +               remove_path(stash_index_path.buf);\n> +               return -1;\n> +       }\n> +\n> +       child_process_init(&cp);\n> +       cp.git_cmd = 1;\n> +       argv_array_pushl(&cp.args, \"checkout-index\", \"--all\", NULL);\n> +       argv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\", stash_index_path.buf);\n> +\n> +       res = run_command(&cp);\n> +       remove_path(stash_index_path.buf);\n> +       return res;\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> +               return error(_(\"Cannot apply a stash in the middle of a merge\"));\n> +\n> +       if (index) {\n> +               if (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree, &info->i_tree)) {\n> +                       has_index = 0;\n> +               } else {\n> +                       struct strbuf out = STRBUF_INIT;\n> +\n> +                       if (diff_tree_binary(&out, &info->w_commit)) {\n> +                               strbuf_release(&out);\n> +                               return -1;\n> +                       }\n> +\n> +                       ret = apply_cached(&out);\n> +                       strbuf_release(&out);\n> +                       if (ret)\n> +                               return -1;\n> +\n> +                       discard_cache();\n> +                       read_cache();\n> +                       if (write_cache_as_tree(&index_tree, 0, NULL))\n> +                               return -1;\n> +\n> +                       reset_head(prefix);\n> +               }\n> +       }\n> +\n> +       if (info->has_u) {\n> +               if (restore_untracked(&info->u_tree))\n> +                       return error(_(\"Could not restore untracked files from stash\"));\n> +       }\n\nMaybe:\n\n       if (info->has_u && restore_untracked(&info->u_tree))\n               return error(_(\"Could not restore untracked files from stash\"));\n\n> +       init_merge_options(&o);\n> +\n> +       o.branch1 = \"Updated upstream\";\n> +       o.branch2 = \"Stashed changes\";\n> +\n> +       if (!oidcmp(&info->b_tree, &c_tree))\n> +               o.branch1 = \"Version stash was based on\";\n> +\n> +       if (quiet)\n> +               o.verbosity = 0;\n> +\n> +       if (o.verbosity >= 3)\n> +               printf_ln(_(\"Merging %s with %s\"), o.branch1, o.branch2);\n> +\n> +       bases[0] = &info->b_tree;\n> +\n> +       ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, bases_count, bases, &result);\n> +       if (ret != 0) {\n> +               rerere(0);\n> +\n> +               if (index)\n> +                       fprintf_ln(stderr, _(\"Index was not unstashed.\"));\n> +\n> +               return ret;\n> +       }\n> +\n> +       if (has_index) {\n> +               if (reset_tree(&index_tree, 0, 0))\n> +                       return -1;\n> +       } else {\n> +               struct strbuf out = STRBUF_INIT;\n> +\n> +               if (diff_cached_index(&out, &c_tree)) {\n> +                       strbuf_release(&out);\n> +                       return -1;\n> +               }\n> +\n> +               if (reset_tree(&c_tree, 0, 1)) {\n> +                       strbuf_release(&out);\n> +                       return -1;\n> +               }\n> +\n> +               ret = update_index(&out);\n> +               strbuf_release(&out);\n> +               if (ret)\n> +                       return -1;\n> +\n> +               discard_cache();\n> +       }\n> +\n> +       if (!quiet) {\n> +               struct argv_array args = ARGV_ARRAY_INIT;\n> +               /* Status is quite simple and could be replaced with calls to wt_status\n> +                * in the future, but it adds complexities which may require more tests\n> +                */\n\nMulti line comment style.\n\n> +               argv_array_push(&args, \"status\");\n> +               cmd_status(args.argc, args.argv, prefix);\n> +       }\n> +\n> +       return 0;\n> +}\n\n[...]\n\n> +int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n> +{\n> +       int result = 0;\n> +       pid_t pid = getpid();\n> +       const char *index_file;\n> +\n> +       struct option options[] = {\n> +               OPT_END()\n> +       };\n> +\n> +       git_config(git_default_config, NULL);\n> +\n> +       argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,\n> +               PARSE_OPT_KEEP_UNKNOWN|PARSE_OPT_KEEP_DASHDASH);\n> +\n> +       index_file = get_index_file();\n> +       strbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file, (uintmax_t)pid);\n> +\n> +       if (argc < 1)\n> +               usage_with_options(git_stash_helper_usage, options);\n\nusage_with_options() does an `exit(129)` ...\n\n> +       else if (!strcmp(argv[0], \"apply\"))\n\n... so we can get rid of the `else` above...\n\n> +               result = apply_stash(argc, argv, prefix);\n> +       else {\n> +               error(_(\"unknown subcommand: %s\"), argv[0]);\n> +               usage_with_options(git_stash_helper_usage, options);\n> +               result = 1;\n\n... and here we don't need to set `result`.\n\n> +       }\n> +\n> +       return result;\n> +}\n\nSo maybe we can get rid of `result` and have something like:\n\n       if (argc < 1) {\n               error(_(\"at least one argument is required\"));\n               usage_with_options(git_stash_helper_usage, options);\n       }\n\n       if (!strcmp(argv[0], \"apply\"))\n               return apply_stash(argc, argv, prefix);\n\n       error(_(\"unknown subcommand: %s\"), argv[0]);\n       usage_with_options(git_stash_helper_usage, options);\n}\n"},{"id":"343852","messageId":"CAP8UFD0yz3yH7-fpxr=69z=wXAVjxraFWsuDVa7Ke_6ByV5B8Q@mail.gmail.com","threadId":"48222","inReplyTo":"CAP8UFD3SM9e+_VGpxVNzMoEYnyzkF0EzjAPjvbFakr9t9Z-muQ@mail.gmail.com","subject":"Re: [PATCH v5 2/5] stash: convert apply to builtin","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2018-04-05T07:59:39Z","receivedAt":"2018-04-05T07:59:48Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, Apr 5, 2018 at 9:50 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n>\n> So maybe we can get rid of `result` and have something like:\n>\n>        if (argc < 1) {\n>                error(_(\"at least one argument is required\"));\n>                usage_with_options(git_stash_helper_usage, options);\n\nMaybe we could also simplify these 2 lines by using usage_msg_opt().\n\n>        }\n>\n>        if (!strcmp(argv[0], \"apply\"))\n>                return apply_stash(argc, argv, prefix);\n>\n>        error(_(\"unknown subcommand: %s\"), argv[0]);\n>        usage_with_options(git_stash_helper_usage, options);\n> }\n"},{"id":"343853","messageId":"CAP8UFD1zEu1kbKWiYKxYdkrVL4m5XmCMbqz47r+JNQjD2_MEwQ@mail.gmail.com","threadId":"48222","inReplyTo":"CAP8UFD0yz3yH7-fpxr=69z=wXAVjxraFWsuDVa7Ke_6ByV5B8Q@mail.gmail.com","subject":"Re: [PATCH v5 2/5] stash: convert apply to builtin","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2018-04-05T08:13:59Z","receivedAt":"2018-04-05T08:14:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, Apr 5, 2018 at 9:59 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> On Thu, Apr 5, 2018 at 9:50 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>>\n>> So maybe we can get rid of `result` and have something like:\n>>\n>>        if (argc < 1) {\n>>                error(_(\"at least one argument is required\"));\n>>                usage_with_options(git_stash_helper_usage, options);\n>\n> Maybe we could also simplify these 2 lines by using usage_msg_opt().\n>\n>>        }\n>>\n>>        if (!strcmp(argv[0], \"apply\"))\n>>                return apply_stash(argc, argv, prefix);\n>>\n>>        error(_(\"unknown subcommand: %s\"), argv[0]);\n>>        usage_with_options(git_stash_helper_usage, options);\n\nAnd here actually we could improve the above 2 lines using something like:\n\nusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n                              git_stash_helper_usage, options);\n\nIt's better than using `error()` because the printed message will\nstart with \"fatal\" instead of \"error\".\n"},{"id":"343864","messageId":"nycvar.QRO.7.76.6.1804051528560.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"CAP8UFD3SM9e+_VGpxVNzMoEYnyzkF0EzjAPjvbFakr9t9Z-muQ@mail.gmail.com","subject":"Re: [PATCH v5 2/5] stash: convert apply to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-05T13:34:55Z","receivedAt":"2018-04-05T13:35:07Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Christian,\n\n[please cull a *lot* more of the quoted mail when you do not reference any\nof it... Thanks]\n\nOn Thu, 5 Apr 2018, Christian Couder wrote:\n\n> On Thu, Apr 5, 2018 at 4:28 AM, Joel Teichroeb <joel@teichroeb.net> wrote:\n> >\n> > [...]\n> > +\n> > +       revision = info->revision.buf;\n> > +\n> > +       if (get_oid(revision, &info->w_commit)) {\n> > +               error(_(\"%s is not a valid reference\"), revision);\n> > +               free_stash_info(info);\n> > +               return -1;\n> \n> Maybe:\n> \n>                free_stash_info(info);\n>                return error(_(\"%s is not a valid reference\"), revision);\n> \n> to save one line and be more consistent with above.\n\nNo. The parameter `revision` of the `error()` call is assigned just above\nthe `if()` block and clearly points into the `info` structure. So you must\nnot release that `info` before printing the error. The order of statements\nis correct.\n\n> > +       if (grab_oid(&info->b_commit, \"%s^1\", revision) ||\n> > +               grab_oid(&info->w_tree, \"%s:\", revision) ||\n> > +               grab_oid(&info->b_tree, \"%s^1:\", revision) ||\n> > +               grab_oid(&info->i_tree, \"%s^2:\", revision)) {\n> > +\n> > +               error(_(\"'%s' is not a stash-like commit\"), revision);\n> > +               free_stash_info(info);\n> > +               return -1;\n> \n> Here also.\n\nYes, here, too, `revision` points at a field inside `info`, so we must not\nrelease it before using it.\n\n> > +       if (info->has_u) {\n> > +               if (restore_untracked(&info->u_tree))\n> > +                       return error(_(\"Could not restore untracked files from stash\"));\n> > +       }\n> \n> Maybe:\n> \n>        if (info->has_u && restore_untracked(&info->u_tree))\n>                return error(_(\"Could not restore untracked files from stash\"));\n\nI agree with this, as it avoids an unncessary indentation level.\n\n> So maybe we can get rid of `result` and have something like:\n> \n>        if (argc < 1) {\n>                error(_(\"at least one argument is required\"));\n>                usage_with_options(git_stash_helper_usage, options);\n>        }\n> \n>        if (!strcmp(argv[0], \"apply\"))\n>                return apply_stash(argc, argv, prefix);\n\n... except we have to use !!apply_stash() here: apply_stash() probably\nreturns -1 in case of error (at least that would be consistent with our\ncoding conventions), and the return value from cmd_*() is handed to exit()\nas exit status. The `!!` trick turns any non-zero value into a 1, also\nconsistent with our coding conventions where we set exit code 1 upon error\nin the \"business logic\".\n\n> \n>        error(_(\"unknown subcommand: %s\"), argv[0]);\n>        usage_with_options(git_stash_helper_usage, options);\n> }\n\nCiao,\nDscho\n"},{"id":"343960","messageId":"nycvar.QRO.7.76.6.1804061439100.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"20180405022810.15796-2-joel@teichroeb.net","subject":"Re: [PATCH v5 1/5] stash: improve option parsing test coverage","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-06T13:06:57Z","receivedAt":"2018-04-06T13:07:29Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Wed, 4 Apr 2018, Joel Teichroeb wrote:\n\n> In preparation for converting the stash command incrementally to\n> a builtin command, this patch improves test coverage of the option\n> parsing. Both for having too many parameters, or too few.\n\nVery good.\n\n> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>\n> ---\n>  t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++\n>  1 file changed, 35 insertions(+)\n> \n> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\n> index aefde7b172..4eaa4cae9a 100755\n> --- a/t/t3903-stash.sh\n> +++ b/t/t3903-stash.sh\n> @@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '\n>  \ttest foo = \"$(cat file/file)\"\n>  '\n>  \n> +test_expect_success 'giving too many ref agruments does not modify files' '\n\nQuick, before Eric beats me to it! A typo! s/agruments/arguments/\n\n> +\tgit stash clear &&\n> +\ttest_when_finished \"git reset --hard HEAD\" &&\n> +\techo foo >file2 &&\n> +\tgit stash &&\n> +\techo bar >file2 &&\n> +\tgit stash &&\n> +\ttest-chmtime =123456789 file2 &&\n> +\tfor type in apply pop \"branch stash-branch\"\n> +\tdo\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 file2 | sed 's/[^0-9].*$//') || return 1\n\nNot your problem, but if there is future work on this (read: if I get to\nmentor a GSoC student, and if I get them to work on it: this idiom\n`test-chmtime -v +0 ... | sed ...` is too common, there really *should* be\na `test-chmtime --get ...`).\n\nAny prospective GSoC student: you know what I have in stock for you ;-)\n\n> +\tdone\n> +'\n> +\n> +test_expect_success 'giving too many ref agruments to drop does not drop anything' '\n\ns/agruments/arguments/\n\nAlso, we try to keep the lines to <= 80 columns. So maybe\n\n+test_expect_success 'drop: too many arguments errors out (does nothing)' '\n\n> +\tgit stash list > stashlist1 &&\n\nAgain, before Eric can beat me to it: we prefer the syntax `>file` (i.e.\nwithout a space between the `>` and the file name).\n\nBTW is there a public branch with your patches? I am not so much of a fan\nof reviewing and having all the work being punted back to the contributor,\nespecially when we failed you for so long in reviewing these patches. I\nwould love, for example, to open a PR on GitHub (even if that would only\napply to code changes, not so much to commit message changes, not until we\nhave rebase support reword! and drop! keywords).\n\n> +\ttest_must_fail git stash drop stash@{0} stash@{1} 2>err &&\n> +\ttest_i18ngrep \"Too many\" err &&\n> +\tgit stash list > stashlist2 &&\n> +\ttest_cmp stashlist1 stashlist2\n> +'\n\nIt might make sense to rename stashlist1 to `expect` an `stashlist2` to\n`actual`, to clarify the roles.\n\n> +\n> +test_expect_success 'giving too many ref agruments to show does not show anything' '\n\nMaybe\n\ntest_expect_success 'show: error on too may arguments (show nothing)'\n\nto keep within the 80 columns/line limit?\n\nThe rest looks obviously correct to me.\n\nThanks!\nDscho\n"},{"id":"343964","messageId":"nycvar.QRO.7.76.6.1804061520060.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"20180405022810.15796-3-joel@teichroeb.net","subject":"Re: [PATCH v5 2/5] stash: convert apply to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-06T15:10:51Z","receivedAt":"2018-04-06T15:11:22Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Wed, 4 Apr 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\ns/let/lets the/ s/command/commands/\n\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> [...]\n>\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> new file mode 100644\n> index 0000000000..9d00a9547d\n> --- /dev/null\n> +++ b/builtin/stash--helper.c\n> @@ -0,0 +1,431 @@\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> +#include \"rerere.h\"\n> +\n> +static const char * const git_stash_helper_usage[] = {\n\nNote to other reviewers: while it would appear that our convention is\nusually to cuddle the `*` with the identifier afterwards (i.e. `char\n*const` instead of `char * const`), it would *also* appear that we somehow\nprefer spaces on both sides when it comes to the _usage:\n\n$ git grep 'usage\\[' builtin/*.c | grep -c ' \\* '\n88\n\n$ git grep 'usage\\[' builtin/*.c | grep -c ' \\*[^ ]'\n18\n\nSo much to clean up and make more consistent... (not your problem, Joel)\n\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 struct strbuf stash_index_path = STRBUF_INIT;\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 grab_oid(struct object_id *oid, const char *fmt, const char *rev)\n> +{\n> +\tstruct strbuf buf = STRBUF_INIT;\n> +\tint ret;\n> +\n> +\tstrbuf_addf(&buf, fmt, rev);\n> +\tret = get_oid(buf.buf, oid);\n> +\tstrbuf_release(&buf);\n> +\treturn ret;\n> +}\n\nIt could be even more reusable, by using\n\nint get_oidf(struct object_id *oid, const char *fmt, ...)\n{\n\tva_list ap;\n\tchar *p;\n\tint ret;\n\n\tva_start(ap, fmt);\n\tp = xstrvfmt(fmt, ap);\n\tva_end(ap);\n\tret = get_oid(p, oid);\n\tfree(p);\n\n\treturn ret;\n}\n\nFeel free to do this as a preparatory patch (it should probably live next\nto get_oid()), otherwise there is always an opportunity for a\nmicro-project (or not so micro- one).\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 symbolic = STRBUF_INIT;\n> +\tint ret;\n> +\tconst char *revision;\n> +\tconst char *commit = NULL;\n> +\tchar *end_of_rev;\n> +\tchar *expanded_ref;\n> +\tstruct object_id discard;\n\nI think elsewhere we prefer the name \"dummy\" for variables that are\nassigned without really needing the values.\n\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\nNote to interested parties: while it may have been okay for a mere Unix\nshell script to output such a message directly to stderr, in C, we need to\nuse `error()` and start the error message with lower-case. This should\n*not* be done as part of the conversion to C, which is kind of bug-for-bug.\n\n> +\t\tstrbuf_release(&refs_msg);\n> +\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tif (argc == 1)\n> +\t\tcommit = argv[0];\n\nIt is probably a bit too magical to assign `commit = argv[0]` right\naway... but it would work, as `argv` is guaranteed to be NULL-terminated.\n\n> +\n> +\tstrbuf_init(&info->revision, 0);\n> +\tif (commit == NULL) {\n\nWe usually use `!commit` instead of `commit == NULL`. It's just the style\nwe use, not a judgement.\n\n> +\t\tif (!ref_exists(ref_stash)) {\n> +\t\t\tfree_stash_info(info);\n> +\t\t\treturn error(_(\"No stash entries found.\"));\n\nTechnically, the shell version used `die`. We could use the same here,\ntoo, but `error()` seems to be fine, too.\n\nHowever, if you use `error()` here, you should use it also instead of the\n`fprintf(stderr, ...);` above, for \"Too many revisions specified\".\n\nAlso: our convention is to start error messages lower-case because they\nare prefixed with `error: `.\n\nBut in the interest of a faithful conversion to C, maybe we should start\nby using `fprintf(stderr, ...)` first, and only convert to `error()` later\n(because that will require changes to the test suite, too).\n\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> +\tif (get_oid(revision, &info->w_commit)) {\n> +\t\terror(_(\"%s is not a valid reference\"), revision);\n> +\t\tfree_stash_info(info);\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tif (grab_oid(&info->b_commit, \"%s^1\", revision) ||\n> +\t\tgrab_oid(&info->w_tree, \"%s:\", revision) ||\n> +\t\tgrab_oid(&info->b_tree, \"%s^1:\", revision) ||\n> +\t\tgrab_oid(&info->i_tree, \"%s^2:\", revision)) {\n> +\n> +\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n> +\t\tfree_stash_info(info);\n> +\t\treturn -1;\n> +\t}\n\nFans of keeping Git commands in the form of Unix shell scripts, please\nnote that this is a deviation from the shell script, but in a good way:\nbefore, if any of those revisions could not be parsed by `git rev-parse`,\nthe error would not have resulted in a fatal error. Instead, IS_STASH_LIKE\nand IS_STASH_REF would have been unchanged, as would be w_commit,\nb_commit, w_tree, b_tree and i_tree.\n\nThe error checking in C is much better for end users.\n\n> +\tinfo->has_u = !grab_oid(&info->u_tree, \"%s^3:\", revision);\n> +\n> +\tend_of_rev = strchrnul(revision, '@');\n> +\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n\nThis is a literal translation of the shell script's `\"${REV%@*}\"`, but I\nwonder whether we can do better in C. After all,\n`refs/stash@my-laptop@{1}` is possibly a valid stash, when I called `git\nfetch laptop refs/stash:refs/stash@my-laptop` regularly.\n\nLet's see.\n\nNope, we really don't have anything in our \"API\" to peel off reflog\nsuffixes... Another micro-project for the future. Nothing that needs to\nhold up this patch series. (I am saying this mainly for the benefit of\nother reviewers...)\n\n> +\n> +\tret = dwim_ref(symbolic.buf, symbolic.len, &discard, &expanded_ref);\n> +\tstrbuf_release(&symbolic);\n> +\tswitch (ret) {\n> +\tcase 0: /* Not found, but valid ref */\n> +\t\tinfo->is_stash_ref = 0;\n> +\t\tbreak;\n> +\tcase 1:\n> +\t\tinfo->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n> +\t\tbreak;\n> +\tdefault: /* Invalid or ambiguous */\n> +\t\tfree_stash_info(info);\n> +\t\treturn -1;\n> +\t}\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\nOh how I wish we had that as API:\n\n$ git grep -W unpack_trees \\*.c | grep init_tree_desc\narchive.c-              init_tree_desc(&t, args->tree->buffer,\nargs->tree->size);\nbuiltin/am.c-   init_tree_desc(&t[0], head->buffer, head->size);\nbuiltin/am.c-   init_tree_desc(&t[1], remote->buffer, remote->size);\nbuiltin/am.c-   init_tree_desc(&t[0], tree->buffer, tree->size);\nbuiltin/checkout.c-     init_tree_desc(&tree_desc, tree->buffer, tree->size);\nbuiltin/checkout.c-             init_tree_desc(&trees[0], tree->buffer, tree->size);\nbuiltin/checkout.c-             init_tree_desc(&trees[1], tree->buffer, tree->size);\nbuiltin/clone.c-        init_tree_desc(&t, tree->buffer, tree->size);\nbuiltin/commit.c-       init_tree_desc(&t, tree->buffer, tree->size);\nbuiltin/merge.c-                init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);\nbuiltin/read-tree.c-            init_tree_desc(t+i, tree->buffer, tree->size);\ndiff-lib.c-     init_tree_desc(&t, tree->buffer, tree->size);\nmerge-recursive.c-      init_tree_desc_from_tree(t+0, common);\nmerge-recursive.c-      init_tree_desc_from_tree(t+1, head);\nmerge-recursive.c-      init_tree_desc_from_tree(t+2, merge);\nmerge.c-                init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);\n\nLooks like a lot of duplication to me. (Again, this is just a lament, and\na suggestion for a future project, not a micro-project, though).\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> +\t/* Diff-tree would not be very hard to replace with a native function,\n> +\t * however it should be done together with apply_cached.\n> +\t */\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\nAs I stated before: I think this is good enough for the moment. We can\nalways spend time later to reduce these spawned processes and call\nin-process functions instead. One teeny tiny step at a time.\n\n> +static int apply_cached(struct strbuf *out)\n> +{\n> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t/* Apply currently only reads either from stdin or a file, thus\n> +\t * apply_all_patches would have to be updated to optionally take a buffer\n> +\t */\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> +\t/* Reset is overall quite simple, however there is no current public API\n> +\t * for resetting.\n> +\t */\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> +\t/* diff-index is very similar to diff-tree above, and should be converted\n> +\t * together with update_index\n> +\t */\n> +\tcp.git_cmd = 1;\n> +\targv_array_pushl(&cp.args, \"diff-index\", \"--cached\", \"--name-only\", \"--diff-filter=A\", NULL);\n\nWe should probably note in the name of this function that we return a list\nonly of the added files.\n\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> +\t/* Update-index is very complicated and may need to have a public function\n> +\t * exposed in order to remove this forking\n> +\t */\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> +\t/*\n> +\t * We need to run restore files from a given index, but without affecting\n> +\t * the current index, so we use GIT_INDEX_FILE with run_command to fork\n> +\t * processes that will not interfere.\n> +\t */\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.buf);\n> +\tif (run_command(&cp)) {\n> +\t\tremove_path(stash_index_path.buf);\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.buf);\n> +\n> +\tres = run_command(&cp);\n> +\tremove_path(stash_index_path.buf);\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\nIt is always 1, maybe not waste a variable on it? ;-)\n\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\nA more faithful conversion would pipe the output of `git diff-tree\n--binary` into `git apply --cached`, but I do not think it is worth\ncomplicating things here.\n\n> +\n> +\t\t\tdiscard_cache();\n> +\t\t\tread_cache();\n\n$ git grep -A1 discard_cache | grep -c read_cache\n10\n\nI guess that's another idiom we use a lot. (Again, micro-project here.)\n\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\trerere(0);\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\t/* Status is quite simple and could be replaced with calls to wt_status\n> +\t\t * in the future, but it adds complexities which may require more tests\n> +\t\t */\n> +\t\targv_array_push(&args, \"status\");\n> +\t\tcmd_status(args.argc, args.argv, prefix);\n> +\t}\n\nThis is a change in behavior. The shell script calls `git status` always,\nand only *suppresses* the output if `--quiet` was passed. I guess the\ndifference in behavior really boils down only to refreshing the index.\nMaybe we can do that here, too? Something like\n\n\tif (quiet) {\n\t\tif (refresh_cache(REFRESH_QUIET))\n\t\t\twarning(\"could not refresh index\");\n\t} else {\n\t\t...\n\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\nThis is the only caller of that function. But I guess that `pop` also\nneeds to call it, so I guess it makes sense to keep `do_apply_stash()`\nseparate.\n\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\nPlease keep spaces around the pipe symbol.\n\n> +\tindex_file = get_index_file();\n> +\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file, (uintmax_t)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> +}\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 94793c1a91..0b5d1f3743 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\nIs it important to cd to top-level when we are exiting anyway? I guess it\nis, because we are not exiting right away, necessarily: both pop_stash()\nand apply_to_branch() call this function. But then, both callers only call\ndrop_stash() afterwards (which does not perform worktree operations)...\n\n> +\treturn $res\n>  }\n>  \n>  pop_stash() {\n> diff --git a/git.c b/git.c\n> index ceaa58ef40..6ffe6364ac 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> -- \n> 2.16.3\n\nThis patch (apart from the minor suggestions I had) looks very good to me\nalready. Thank you so much!\n\nCiao,\nDscho\n"},{"id":"343965","messageId":"nycvar.QRO.7.76.6.1804061715020.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"20180405022810.15796-3-joel@teichroeb.net","subject":"Re: [PATCH v5 2/5] stash: convert apply to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-06T15:17:05Z","receivedAt":"2018-04-06T15:17:30Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\ncompletely forgot to reiterate this (my reply to Christian was probably\nburied in the thread):\n\nOn Wed, 4 Apr 2018, Joel Teichroeb wrote:\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> +\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file, (uintmax_t)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> +}\n\nThe `result` variable can contain negative values, while the exit status\nreally should be either 0 or 1 (and the return value of\n`cmd_stash__helper()` is what `run_builtin()` hands to `exit()` as exit\nstatus). So please use\n\n\treturn !!result;\n\nhere (which returns 0 if result is 0, and 1 otherwise).\n\nCiao,\nDscho\n"},{"id":"343966","messageId":"nycvar.QRO.7.76.6.1804061712230.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"20180405022810.15796-4-joel@teichroeb.net","subject":"Re: [PATCH v5 3/5] stash: convert drop and clear to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-06T15:39:32Z","receivedAt":"2018-04-06T15:40:00Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Wed, 4 Apr 2018, Joel Teichroeb wrote:\n\n> Add the drop and clear commands to the builtin helper. These two\n> are each simple, but are being added together as they are quite\n> related.\n\nMakes sense.\n\n> We have to unfortunately keep the drop and clear functions in the\n> shell script as functions are called with parameters internally\n> that are not valid when the commands are called externally. Once\n> pop is converted they can both be removed.\n\nExcellent explanation.\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 9d00a9547d..520cd746c4 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -134,6 +146,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> @@ -399,6 +434,74 @@ 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> +\t/* reflog does not provide a simple function for deleting refs. One will\n> +\t * need to be added to avoid implementing too much reflog code here\n> +\t */\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\nThis is a correct conversion. In the future, we will want to print this to\nstderr, though.\n\n> +\t} else {\n> +\t\treturn error(_(\"%s: Could not drop stash entry\"), info->revision.buf);\n> +\t}\n> +\n> +\t/* This could easily be replaced by get_oid, but currently it will throw a\n> +\t * fatal error when a reflog is empty, which we can not recover from\n> +\t */\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\nSince we already have `grab_oid()` (or `get_oidf()` if you heed my\nsuggestion), we could use that here right away.\n\n> +\n> +\tif (ret)\n> +\t\tdo_clear_stash();\n\nI was a bit puzzled why we clear the entire stash when we only wanted to\ndrop one? Going back to the shell script, there is this helpful comment:\n\n\t# clear_stash if we just dropped the last stash entry\n\nCan you please add that comment here, too? It really helps readers, such\nas myself...\n\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\nI am a bit curious why that did not appear in the previous patch, as\n`apply_stash()` in the shell script already called `assert_stash_ref()`...\n\nThe rest of the patch looks pretty good to me! Thank you.\n\nCiao,\nDscho\n"},{"id":"343967","messageId":"nycvar.QRO.7.76.6.1804061743210.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"20180405022810.15796-5-joel@teichroeb.net","subject":"Re: [PATCH v5 4/5] stash: convert branch to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-06T15:50:12Z","receivedAt":"2018-04-06T15:50:43Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Wed, 4 Apr 2018, Joel Teichroeb wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 520cd746c4..486796bb6a 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -502,6 +508,49 @@ 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\nWe should probably also test for argc > 2 and error out in that case...\n\n> +\n> +\tbranch = argv[0];\n> +\n> +\tif (get_stash_info(&info, argc - 1, argv + 1))\n> +\t\treturn -1;\n> +\n> +\t/* Checkout does not currently provide a function for checking out a branch\n> +\t * as cmd_checkout does a large amount of sanity checks first that we\n> +\t * require here.\n> +\t */\n\nWhile this is true, this code comment is *prone* to become stale. Maybe\nmove this remark into the commit message?\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\nAn alternative way to translate that &&-chain would be to do this:\n\n\tret = cmd_checkout(args.argc, args.argv, prefix);\n\tif (!ret)\n\t\tret = do_apply_stash(prefix, &info, 1);\n\tif (!ret && info.is_stash_ref)\n\t\tret = do_drop_stash(prefix, &info);\n\n... which might be a bit easier to read and to maintain?\n\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> @@ -528,6 +577,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);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 0b8f07b38a..c5fd4c6c44 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\nThe rest looks obviously good to me (I am not all that sure about the `cd\n\"$START_DIR\"` but it definitely does not hurt).\n\nCiao,\nDscho\n"},{"id":"343969","messageId":"nycvar.QRO.7.76.6.1804061755000.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"20180405022810.15796-6-joel@teichroeb.net","subject":"Re: [PATCH v5 5/5] stash: convert pop to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-06T16:12:04Z","receivedAt":"2018-04-06T16:12:30Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\nOn Wed, 4 Apr 2018, Joel Teichroeb wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 486796bb6a..7c8950bc90 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -13,6 +13,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\nIn the shell version, it says `( pop | apply )`. I think we should do that\nhere, too.\n\n> @@ -508,6 +514,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\nI think we should test argc after this, too.\n\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\nThe same `if (!ret)` trick to convert &&-chains as I mentioned earlier\ncould be used here, too.\n\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> @@ -577,6 +616,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 {\n> diff --git a/git-stash.sh b/git-stash.sh\n> index c5fd4c6c44..8f2640fe90 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\nNice!\n\nCiao,\nDscho\n"},{"id":"343971","messageId":"nycvar.QRO.7.76.6.1804061813240.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"20180405022810.15796-1-joel@teichroeb.net","subject":"Re: [PATCH v5 0/5] Convert some stash functionality to a builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-06T16:15:36Z","receivedAt":"2018-04-06T16:16:02Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Joel,\n\n\nOn Wed, 4 Apr 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> \n> The code is based on next as write_cache_as_tree was changed to\n> take an object ID. It can easily be rebase on master by changing\n> the two calls to write_cache_as_tree to use tha hash.\n\nVery nice, thank you!\n\nI reviewed all five patches, and offered a couple of suggestion.\n\nAs is tradition on this list, I also offered way more comments that do not\nrequire any action from you, please use discretion when reading those: I\ndefinitely do not expect you to do anything about those \"for the future\"\ncomments.\n\nThis patch series is in a very good shape, methinks, and should not need a\nwhole lot more work to enter `next`. From that point on, a GSoC student\nworking on the `stash` project (if we get any) could take over.\n\nThank you so much!\nDscho\n"},{"id":"344023","messageId":"a64d281b-a18c-7c6c-1524-92aef869209f@gmail.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1804061439100.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","subject":"Re: [PATCH v5 1/5] stash: improve option parsing test coverage","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-04-06T22:48:16Z","receivedAt":"2018-04-06T22:48:23Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"\nOn 06.04.2018 16:06, Johannes Schindelin wrote:\n>> +\tgit stash clear &&\n>> +\ttest_when_finished \"git reset --hard HEAD\" &&\n>> +\techo foo >file2 &&\n>> +\tgit stash &&\n>> +\techo bar >file2 &&\n>> +\tgit stash &&\n>> +\ttest-chmtime =123456789 file2 &&\n>> +\tfor type in apply pop \"branch stash-branch\"\n>> +\tdo\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 file2 | sed 's/[^0-9].*$//') || return 1\n> \n> Not your problem, but if there is future work on this (read: if I get to\n> mentor a GSoC student, and if I get them to work on it: this idiom\n> `test-chmtime -v +0 ... | sed ...` is too common, there really *should* be\n> a `test-chmtime --get ...`).\n\nHi,\n\nI submitted a patch for this [1].\n\n[1]\nhttps://public-inbox.org/git/20180406221947.28402-1-ungureanupaulsebastian@gmail.com/\n\nBest regards,\nPaul Ungureanu\n"},{"id":"344182","messageId":"nycvar.QRO.7.76.6.1804091032380.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48222","inReplyTo":"a64d281b-a18c-7c6c-1524-92aef869209f@gmail.com","subject":"Re: [PATCH v5 1/5] stash: improve option parsing test coverage","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-09T08:32:53Z","receivedAt":"2018-04-09T08:33:02Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Sat, 7 Apr 2018, Paul-Sebastian Ungureanu wrote:\n\n> On 06.04.2018 16:06, Johannes Schindelin wrote:\n> > > +\tgit stash clear &&\n> > > +\ttest_when_finished \"git reset --hard HEAD\" &&\n> > > +\techo foo >file2 &&\n> > > +\tgit stash &&\n> > > +\techo bar >file2 &&\n> > > +\tgit stash &&\n> > > +\ttest-chmtime =123456789 file2 &&\n> > > +\tfor type in apply pop \"branch stash-branch\"\n> > > +\tdo\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 file2 | sed\n> > > 's/[^0-9].*$//') || return 1\n> > \n> > Not your problem, but if there is future work on this (read: if I get to\n> > mentor a GSoC student, and if I get them to work on it: this idiom\n> > `test-chmtime -v +0 ... | sed ...` is too common, there really *should* be\n> > a `test-chmtime --get ...`).\n> \n> Hi,\n> \n> I submitted a patch for this [1].\n> \n> [1]\n> https://public-inbox.org/git/20180406221947.28402-1-ungureanupaulsebastian@gmail.com/\n\nExcellent, thank you so much!\nJohannes\n"},{"id":"346053","messageId":"80781666-fe94-f718-ff57-4faeddb9a183@gmail.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1804061813240.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","subject":"Re: [PATCH v5 0/5] Convert some stash functionality to a builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-04-28T22:06:47Z","receivedAt":"2018-04-28T22:06:54Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello Joel,\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\nFirst of all, I must thank you for submitting this series of patches. It \nis a great starting point to convert 'git stash'.\n\nI would like to continue your work on \"git stash\" if that is fine with \nyou. I will continue to build on top of it, starting with applying some \npatches in order to implement what was already suggested in this thread. \n  During the summer, I am planning to finish this process and, \nhopefully, have a 100% C, built-in 'git stash'.\n\nBest regards,\nPaul\n"},{"id":"346074","messageId":"nycvar.QRO.7.76.6.1804291503470.79@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"80781666-fe94-f718-ff57-4faeddb9a183@gmail.com","subject":"Re: [PATCH v5 0/5] Convert some stash functionality to a builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-04-29T13:04:59Z","receivedAt":"2018-04-29T13:05:31Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 29 Apr 2018, Paul-Sebastian Ungureanu wrote:\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> \n> First of all, I must thank you for submitting this series of patches. It is a\n> great starting point to convert 'git stash'.\n> \n> I would like to continue your work on \"git stash\" if that is fine with you.\n\nSo you mean you would like to take custody of Joel's patches and address\nthe reviews so far, then re-submit?\n\nCiao,\nDscho\n\nP.S.: Yes, I think I know that you want to do that (as we discussed on\nIRC), but I just wanted to clarify on the mailing list, to avoid\nmisunderstandings ;-).\n"},{"id":"346165","messageId":"CA+CzEk_qwHs5qUstyFeepzwvCBR=9SvH90+__f-gfxFySETZzQ@mail.gmail.com","threadId":"48222","inReplyTo":"CA+CzEk8c1Pt+n9Jy5vL9_K60Q=6VKnLTdBY1JFRnb-POuRFv0Q@mail.gmail.com","subject":"Re: [PATCH v5 0/5] Convert some stash functionality to a builtin","fromName":"Joel Teichroeb","fromEmail":"joel@teichroeb.net","sentAt":"2018-04-30T15:43:56Z","receivedAt":"2018-04-30T15:44:20Z","isPatch":true,"sender":{"key":"joel@teichroeb.net","avatar":"https://avatars.githubusercontent.com/u/240865?v=4"},"body":"Re-sending, but this time in plain text (why doesn't gmail for android\nsupport that...)\n\nOn Mon, Apr 30, 2018 at 7:35 AM, Joel Teichroeb <joel@teichroeb.net> wrote:\n> Hi Paul,\n>\n> That sounds great to me!\n>\n> Thanks,\n> Joel\n>\n>\n> On Sat, Apr 28, 2018, 3:06 PM Paul-Sebastian Ungureanu\n> <ungureanupaulsebastian@gmail.com> wrote:\n>>\n>> Hello Joel,\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>>\n>> First of all, I must thank you for submitting this series of patches. It\n>> is a great starting point to convert 'git stash'.\n>>\n>> I would like to continue your work on \"git stash\" if that is fine with\n>> you. I will continue to build on top of it, starting with applying some\n>> patches in order to implement what was already suggested in this thread.\n>>   During the summer, I am planning to finish this process and,\n>> hopefully, have a 100% C, built-in 'git stash'.\n>>\n>> Best regards,\n>> Paul\n"},{"id":"350849","messageId":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"20180405022810.15796-1-joel@teichroeb.net","subject":"[PATCH v6 0/4] stash: add new tests and introduce a new helper function","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:42:23Z","receivedAt":"2018-06-25T16:42:46Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nThis first series of patches does bring some changes and improvements to the\ntest suite. One of the patches also introduces a new function `get_oidf()` which\nwill be hepful for the incoming patches related to `git stash`.\n\nThanks,\nPaul\n\n\nJoel Teichroeb (1):\n  stash: improve option parsing test coverage\n\nPaul-Sebastian Ungureanu (3):\n  sha1-name.c: added 'get_oidf', which acts like 'get_oid'\n  stash: update test cases conform to coding guidelines\n  stash: renamed test cases to be more descriptive\n\n cache.h          |   1 +\n sha1-name.c      |  19 ++++++\n t/t3903-stash.sh | 169 +++++++++++++++++++++++++++++------------------\n 3 files changed, 123 insertions(+), 66 deletions(-)\n\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350850","messageId":"4c1f71b067ac235c3003d58e1f3dfb07866be129.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid'","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:42:24Z","receivedAt":"2018-06-25T16:42:49Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Compared to 'get_oid', 'get_oidf' has as parameters a\nprintf format string and the additional arguments. This\nwill help simplify the code in subsequent commits.\n\nOriginal-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n cache.h     |  1 +\n sha1-name.c | 19 +++++++++++++++++++\n 2 files changed, 20 insertions(+)\n\ndiff --git a/cache.h b/cache.h\nindex 89a107a7f..9252a4546 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1321,6 +1321,7 @@ struct object_context {\n \tGET_OID_BLOB)\n \n extern int get_oid(const char *str, struct object_id *oid);\n+extern int get_oidf(struct object_id *oid, const char *fmt, ...);\n extern int get_oid_commit(const char *str, struct object_id *oid);\n extern int get_oid_committish(const char *str, struct object_id *oid);\n extern int get_oid_tree(const char *str, struct object_id *oid);\ndiff --git a/sha1-name.c b/sha1-name.c\nindex 60d9ef3c7..80ee8f742 100644\n--- a/sha1-name.c\n+++ b/sha1-name.c\n@@ -1466,6 +1466,25 @@ int get_oid(const char *name, struct object_id *oid)\n \treturn get_oid_with_context(name, 0, oid, &unused);\n }\n \n+/*\n+ * This returns a non-zero value if the string (built using printf\n+ * format and the given arguments) is not a valid object.\n+ */\n+int get_oidf(struct object_id *oid, const char *fmt, ...)\n+{\n+\tva_list ap;\n+\tint ret;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tva_start(ap, fmt);\n+\tstrbuf_vaddf(&sb, fmt, ap);\n+\tva_end(ap);\n+\n+\tret = get_oid(sb.buf, oid);\n+\tstrbuf_release(&sb);\n+\n+\treturn ret;\n+}\n \n /*\n  * Many callers know that the user meant to name a commit-ish by\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350851","messageId":"16a4ed177d885ab0db270201ba52f07ed2234ea1.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 2/4] stash: improve option parsing test coverage","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:42:25Z","receivedAt":"2018-06-25T16:42:51Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nIn 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 parameters, or too few.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++\n 1 file changed, 35 insertions(+)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex 1f871d3cc..af7586d43 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '\n \ttest foo = \"$(cat file/file)\"\n '\n \n+test_expect_success 'giving too many ref arguments does not modify files' '\n+\tgit stash clear &&\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\techo foo >file2 &&\n+\tgit stash &&\n+\techo bar >file2 &&\n+\tgit stash &&\n+\ttest-tool chmtime =123456789 file2 &&\n+\tfor type in apply pop \"branch stash-branch\"\n+\tdo\n+\t\ttest_must_fail git stash $type stash@{0} stash@{1} 2>err &&\n+\t\ttest_i18ngrep \"Too many revisions\" err &&\n+\t\ttest 123456789 = $(test-tool chmtime -g file2) || return 1\n+\tdone\n+'\n+\n+test_expect_success 'drop: too many arguments errors out (does nothing)' '\n+\tgit stash list >expect &&\n+\ttest_must_fail git stash drop stash@{0} stash@{1} 2>err &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\tgit stash list >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'show: too many arguments errors out (does nothing)' '\n+\ttest_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\ttest_must_be_empty out\n+'\n+\n test_expect_success 'stash create - no changes' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n@@ -479,6 +509,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.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350852","messageId":"2fe77c0946231dd9235d88922e4d56dd7f5c66c7.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 3/4] stash: update test cases conform to coding guidelines","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:42:26Z","receivedAt":"2018-06-25T16:42:52Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Removed whitespaces after redirection operators.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------\n 1 file changed, 61 insertions(+), 59 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex af7586d43..de6cab1fe 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -8,22 +8,22 @@ test_description='Test git stash'\n . ./test-lib.sh\n \n test_expect_success 'stash some dirty working directory' '\n-\techo 1 > file &&\n+\techo 1 >file &&\n \tgit add file &&\n \techo unrelated >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m initial &&\n-\techo 2 > file &&\n+\techo 2 >file &&\n \tgit add file &&\n-\techo 3 > file &&\n+\techo 3 >file &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file b/file\n index 0cfbf08..00750ed 100644\n --- a/file\n@@ -35,7 +35,7 @@ EOF\n \n test_expect_success 'parents of stash' '\n \ttest $(git rev-parse stash^) = $(git rev-parse HEAD) &&\n-\tgit diff stash^2..stash > output &&\n+\tgit diff stash^2..stash >output &&\n \ttest_cmp output expect\n '\n \n@@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '\n \n test_expect_success 'apply stashed changes (including index)' '\n \tgit reset --hard HEAD^ &&\n-\techo 6 > other-file &&\n+\techo 6 >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m other-file &&\n@@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '\n \n test_expect_success 'drop top stash' '\n \tgit reset --hard &&\n-\tgit stash list > stashlist1 &&\n-\techo 7 > file &&\n+\tgit stash list >expected &&\n+\techo 7 >file &&\n \tgit stash &&\n \tgit stash drop &&\n-\tgit stash list > stashlist2 &&\n-\ttest_cmp stashlist1 stashlist2 &&\n+\tgit stash list >actual &&\n+\ttest_cmp expected actual &&\n \tgit stash apply &&\n \ttest 3 = $(cat file) &&\n \ttest 1 = $(git show :file) &&\n@@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '\n \n test_expect_success 'drop middle stash' '\n \tgit reset --hard &&\n-\techo 8 > file &&\n+\techo 8 >file &&\n \tgit stash &&\n-\techo 9 > file &&\n+\techo 9 >file &&\n \tgit stash &&\n \tgit stash drop stash@{1} &&\n \ttest 2 = $(git stash list | wc -l) &&\n@@ -160,7 +160,7 @@ test_expect_success 'stash pop' '\n \ttest 0 = $(git stash list | wc -l)\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file2 b/file2\n new file mode 100644\n index 0000000..1fe912c\n@@ -170,7 +170,7 @@ index 0000000..1fe912c\n +bar2\n EOF\n \n-cat > expect1 << EOF\n+cat >expect1 <<EOF\n diff --git a/file b/file\n index 257cc56..5716ca5 100644\n --- a/file\n@@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644\n +bar\n EOF\n \n-cat > expect2 << EOF\n+cat >expect2 <<EOF\n diff --git a/file b/file\n index 7601807..5716ca5 100644\n --- a/file\n@@ -198,79 +198,79 @@ index 0000000..1fe912c\n EOF\n \n test_expect_success 'stash branch' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit commit file -m first &&\n-\techo bar > file &&\n-\techo bar2 > file2 &&\n+\techo bar >file &&\n+\techo bar2 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n-\techo baz > file &&\n+\techo baz >file &&\n \tgit commit file -m second &&\n \tgit stash branch stashbranch &&\n \ttest refs/heads/stashbranch = $(git symbolic-ref HEAD) &&\n \ttest $(git rev-parse HEAD) = $(git rev-parse master^) &&\n-\tgit diff --cached > output &&\n+\tgit diff --cached >output &&\n \ttest_cmp output expect &&\n-\tgit diff > output &&\n+\tgit diff >output &&\n \ttest_cmp output expect1 &&\n \tgit add file &&\n \tgit commit -m alternate\\ second &&\n-\tgit diff master..stashbranch > output &&\n+\tgit diff master..stashbranch >output &&\n \ttest_cmp output expect2 &&\n \ttest 0 = $(git stash list | wc -l)\n '\n \n test_expect_success 'apply -q is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\tgit stash apply -q > output.out 2>&1 &&\n+\tgit stash apply -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'save -q is quiet' '\n-\tgit stash save --quiet > output.out 2>&1 &&\n+\tgit stash save --quiet >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q is quiet' '\n-\tgit stash pop -q > output.out 2>&1 &&\n+\tgit stash pop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q --index works and is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit add file &&\n \tgit stash save --quiet &&\n-\tgit stash pop -q --index > output.out 2>&1 &&\n+\tgit stash pop -q --index >output.out 2>&1 &&\n \ttest foo = \"$(git show :file)\" &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'drop -q is quiet' '\n \tgit stash &&\n-\tgit stash drop -q > output.out 2>&1 &&\n+\tgit stash drop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'stash -k' '\n-\techo bar3 > file &&\n-\techo bar4 > file2 &&\n+\techo bar3 >file &&\n+\techo bar4 >file2 &&\n \tgit add file2 &&\n \tgit stash -k &&\n \ttest bar,bar4 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --no-keep-index' '\n-\techo bar33 > file &&\n-\techo bar44 > file2 &&\n+\techo bar33 >file &&\n+\techo bar44 >file2 &&\n \tgit add file2 &&\n \tgit stash --no-keep-index &&\n \ttest bar,bar2 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --invalid-option' '\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \ttest_must_fail git stash --invalid-option &&\n \ttest_must_fail git stash save --invalid-option &&\n@@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash drop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash pop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \n test_expect_success 'ref with non-existent reflog' '\n \tgit stash clear &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git rev-parse --quiet --verify does-not-exist &&\n@@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '\n test_expect_success 'invalid ref of the form stash@{n}, n >= N' '\n \tgit stash clear &&\n \ttest_must_fail git stash drop stash@{0} &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git stash drop stash@{1} &&\n@@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu\n \ttest_i18ncmp expect actual\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/HEAD b/HEAD\n new file mode 100644\n index 0000000..fe0cbee\n@@ -737,14 +739,14 @@ EOF\n test_expect_success 'stash where working directory contains \"HEAD\" file' '\n \tgit stash clear &&\n \tgit reset --hard &&\n-\techo file-not-a-ref > HEAD &&\n+\techo file-not-a-ref >HEAD &&\n \tgit add HEAD &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD &&\n \ttest \"$(git rev-parse stash^)\" = \"$(git rev-parse HEAD)\" &&\n-\tgit diff stash^..stash > output &&\n+\tgit diff stash^..stash >output &&\n \ttest_cmp output expect\n '\n \n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350853","messageId":"0f49fc90bee5e6a297c49224f1451a7aee4c8635.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 4/4] stash: renamed test cases to be more descriptive","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:42:27Z","receivedAt":"2018-06-25T16:42:57Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Renamed some test cases' labels to be more descriptive and under 80\ncharacters per line.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex de6cab1fe..8d002a7f2 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \ttest_cmp expected actual\n '\n \n-test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'drop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit reset --hard HEAD\n '\n \n-test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'pop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form \"n\", n >= N' '\n \tgit stash drop\n '\n \n-test_expect_success 'stash branch should not drop the stash if the branch exists' '\n+test_expect_success 'branch: should not drop the stash if the branch exists' '\n \tgit stash clear &&\n \techo foo >file &&\n \tgit add file &&\n@@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash branch should not drop the stash if the apply fails' '\n+test_expect_success 'branch: should not drop the stash if the apply fails' '\n \tgit stash clear &&\n \tgit reset HEAD~1 --hard &&\n \techo foo >file &&\n@@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash apply shows status same as git status (relative to current directory)' '\n+test_expect_success 'apply: shows same status as git status (relative to ./)' '\n \tgit stash clear &&\n \techo 1 >subdir/subfile1 &&\n \techo 2 >subdir/subfile2 &&\n@@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec shows no changes when there are none' '\n+test_expect_success 'push: <pathspec> shows no changes when there are none' '\n \t>foo &&\n \tgit add foo &&\n \tgit commit -m \"tmp\" &&\n@@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec not in the repository errors out' '\n+test_expect_success 'push: <pathspec> not in the repository errors out' '\n \t>untracked &&\n \ttest_must_fail git stash push untracked &&\n \ttest_path_is_file untracked\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350854","messageId":"5373f422a82da2357828b8cc4a2b84fb2bedd780.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 2/4] stash: convert drop and clear to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:43:24Z","receivedAt":"2018-06-25T16:43:41Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 112 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   4 +-\n 2 files changed, 114 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 1c4387b10..84a537f39 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,7 +12,14 @@\n #include \"rerere.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@@ -21,6 +28,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 struct strbuf stash_index_path = STRBUF_INIT;\n@@ -139,6 +151,31 @@ 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,\n+\t\t\t     git_stash_helper_clear_usage,\n+\t\t\t     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@@ -414,6 +451,77 @@ 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+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tint ret;\n+\n+\t/*\n+\t * reflog does not provide a simple function for deleting refs. One will\n+\t * need to be added to avoid implementing too much reflog code here\n+\t */\n+\targv_array_pushl(&args, \"reflog\", \"delete\", \"--updateref\", \"--rewrite\",\n+\t\t\t 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,\n+\t\t\t       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+\t/*\n+\t * This could easily be replaced by get_oid, but currently it will throw a\n+\t * fatal error when a reflog is empty, which we can not recover from\n+\t */\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+\t/* do_clear_stash if we just dropped the last stash entry */\n+\tif (ret)\n+\t\tdo_clear_stash();\n+\n+\treturn 0;\n+}\n+\n+static void assert_stash_ref(struct stash_info *info)\n+{\n+\tif (!info->is_stash_ref) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash reference\"), info->revision.buf);\n+\t\texit(128);\n+\t}\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\t     git_stash_helper_drop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\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 \tpid_t pid = getpid();\n@@ -436,6 +544,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tusage_with_options(git_stash_helper_usage, options);\n \tif (!strcmp(argv[0], \"apply\"))\n \t\treturn !!apply_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"clear\"))\n+\t\treturn !!clear_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"drop\"))\n+\t\treturn !!drop_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 809b1c2d1..a99d5dc9e 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -653,7 +653,7 @@ apply)\n \t;;\n clear)\n \tshift\n-\tclear_stash \"$@\"\n+\tgit stash--helper clear \"$@\"\n \t;;\n create)\n \tshift\n@@ -665,7 +665,7 @@ store)\n \t;;\n drop)\n \tshift\n-\tdrop_stash \"$@\"\n+\tgit stash--helper drop \"$@\"\n \t;;\n pop)\n \tshift\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350855","messageId":"45df77241155792bef2ce44c29f1fca09d8fa485.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 1/4] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:43:23Z","receivedAt":"2018-06-25T16:43:43Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd a builtin helper for performing stash commands. Converting\nall at once proved hard to review, so starting with just apply\nlets conversion get started without the other commands 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n .gitignore              |   1 +\n Makefile                |   1 +\n builtin.h               |   1 +\n builtin/stash--helper.c | 442 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  78 +------\n git.c                   |   1 +\n 6 files changed, 453 insertions(+), 71 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 388cc4bee..56e63dd24 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -155,6 +155,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 1d27f3636..ca6072053 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1078,6 +1078,7 @@ BUILTIN_OBJS += builtin/serve.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 4e0f64723..ba63ce0f6 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -221,6 +221,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..1c4387b10\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+#include \"rerere.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 struct strbuf stash_index_path = STRBUF_INIT;\n+\n+/*\n+ * w_commit is set to the commit containing the working tree\n+ * b_commit is set to the base commit\n+ * i_commit is set to the commit containing the index tree\n+ * u_commit is set to the commit containing the untracked files tree\n+ * w_tree is set to the working tree\n+ * b_tree is set to the base tree\n+ * i_tree is set to the index tree\n+ * u_tree is set to the untracked files tree\n+ */\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 void free_stash_info(struct stash_info *info)\n+{\n+\tstrbuf_release(&info->revision);\n+}\n+\n+static void assert_stash_like(struct stash_info *info, const char * revision)\n+{\n+\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n+\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n+\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n+\t    get_oidf(&info->i_tree, \"%s^2:\", revision)) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n+\t\texit(128);\n+\t}\n+}\n+\n+static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n+{\n+\tstruct strbuf symbolic = STRBUF_INIT;\n+\tint ret;\n+\tconst char *revision;\n+\tconst char *commit = NULL;\n+\tchar *end_of_rev;\n+\tchar *expanded_ref;\n+\tstruct object_id dummy;\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) {\n+\t\tif (!ref_exists(ref_stash)) {\n+\t\t\tfree_stash_info(info);\n+\t\t\tfprintf_ln(stderr, \"No stash entries found.\");\n+\t\t\treturn -1;\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+\tif (get_oid(revision, &info->w_commit)) {\n+\t\terror(_(\"%s is not a valid reference\"), revision);\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\n+\n+\tassert_stash_like(info, revision);\n+\n+\tinfo->has_u = !get_oidf(&info->u_tree, \"%s^3:\", revision);\n+\n+\tend_of_rev = strchrnul(revision, '@');\n+\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n+\n+\tret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);\n+\tstrbuf_release(&symbolic);\n+\tswitch (ret) {\n+\tcase 0: /* Not found, but valid ref */\n+\t\tinfo->is_stash_ref = 0;\n+\t\tbreak;\n+\tcase 1:\n+\t\tinfo->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n+\t\tbreak;\n+\tdefault: /* Invalid or ambiguous */\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\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+\t/*\n+\t * Diff-tree would not be very hard to replace with a native function,\n+\t * however it should be done together with apply_cached.\n+\t */\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+\t/*\n+\t * Apply currently only reads either from stdin or a file, thus\n+\t * apply_all_patches would have to be updated to optionally take a buffer\n+\t */\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+\t/*\n+\t * Reset is overall quite simple, however there is no current public API\n+\t * for resetting.\n+\t */\n+\targv_array_push(&args, \"reset\");\n+\treturn cmd_reset(args.argc, args.argv, prefix);\n+}\n+\n+static int get_newly_staged(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+\t/*\n+\t * diff-index is very similar to diff-tree above, and should be converted\n+\t * together with update_index\n+\t */\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"diff-index\", \"--cached\", \"--name-only\",\n+\t\t\t \"--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+\t/*\n+\t * Update-index is very complicated and may need to have a public function\n+\t * exposed in order to remove this forking\n+\t */\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+\t/*\n+\t * We need to run restore files from a given index, but without affecting\n+\t * the current index, so we use GIT_INDEX_FILE with run_command to fork\n+\t * processes that will not interfere.\n+\t */\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.buf);\n+\tif (run_command(&cp)) {\n+\t\tremove_path(stash_index_path.buf);\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.buf);\n+\n+\tres = run_command(&cp);\n+\tremove_path(stash_index_path.buf);\n+\treturn res;\n+}\n+\n+static int do_apply_stash(const char *prefix, struct stash_info *info,\n+\tint 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+\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,\n+\t\t\t&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 && restore_untracked(&info->u_tree))\n+\t\treturn error(_(\"Could not restore untracked files from stash\"));\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, 1, bases,\n+\t\t\t\t      &result);\n+\tif (ret != 0) {\n+\t\trerere(0);\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 (get_newly_staged(&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\tif (refresh_cache(REFRESH_QUIET))\n+\t\t\twarning(\"could not refresh index\");\n+\t} else {\n+\t\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\t\t/*\n+\t\t * Status is quite simple and could be replaced with calls to wt_status\n+\t\t * in the future, but it adds complexities which may require more tests\n+\t\t */\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\t     git_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+\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\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n+\n+\tindex_file = get_index_file();\n+\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n+\t\t    (uintmax_t)pid);\n+\n+\tif (argc < 1)\n+\t\tusage_with_options(git_stash_helper_usage, options);\n+\tif (!strcmp(argv[0], \"apply\"))\n+\t\treturn !!apply_stash(argc, argv, prefix);\n+\n+\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n+\t\t      git_stash_helper_usage, options);\n+}\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 94793c1a9..809b1c2d1 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() {\n@@ -713,7 +648,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\ndiff --git a/git.c b/git.c\nindex c2f48d53d..d43c42e31 100644\n--- a/git.c\n+++ b/git.c\n@@ -539,6 +539,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 | NO_PARSEOPT },\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350856","messageId":"5d5b05caecc01d8f252ef5f8495549663815ed63.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 4/4] stash: convert pop to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:43:26Z","receivedAt":"2018-06-25T16:43:45Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash pop to the helper and delete the pop_stash, drop_stash,\nassert_stash_ref functions from the shell script now that they\nare no longer needed.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-\n git-stash.sh            | 47 ++---------------------------------------\n 2 files changed, 37 insertions(+), 46 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex fbf78249c..a38d6ae8a 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,7 +13,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 apply [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n \tNULL\n@@ -24,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@@ -528,6 +533,33 @@ 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\t     git_stash_helper_pop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\n+\tif ((ret = do_apply_stash(prefix, &info, index)))\n+\t\tprintf_ln(_(\"The stash entry is kept in case you need it again.\"));\n+\telse\n+\t\tret = do_drop_stash(prefix, &info);\n+\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@@ -589,6 +621,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"pop\"))\n+\t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n \ndiff --git a/git-stash.sh b/git-stash.sh\nindex 29d9f4425..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@@ -655,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.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350857","messageId":"ac0a24da8a583a137176d1d215df65bb1a3ada0e.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 3/4] stash: convert branch to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:43:25Z","receivedAt":"2018-06-25T16:43:53Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash branch to the helper and delete the apply_to_branch\nfunction from the shell script.\n\nCheckout does not currently provide a function for checking out\na branch as cmd_checkout does a large amount of sanity checks\nfirst that we require here.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 43 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 17 ++--------------\n 2 files changed, 45 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 84a537f39..fbf78249c 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -14,6 +14,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@@ -28,6 +29,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@@ -522,6 +528,41 @@ 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\t     git_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\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 \tpid_t pid = getpid();\n@@ -548,6 +589,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"branch\"))\n+\t\treturn !!branch_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex a99d5dc9e..29d9f4425 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@@ -673,7 +659,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.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350858","messageId":"a4b25124d0d3f28c5d122f8226a50978e39c9200.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 1/6] stash: implement the \"list\" command in the builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:46:34Z","receivedAt":"2018-06-25T16:46:52Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash list to the helper and delete the list_stash function\nfrom the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 33 +++++++++++++++++++++++++++++++++\n git-stash.sh            |  7 +------\n 2 files changed, 34 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex a38d6ae8a..2ed21f5d1 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,7 @@\n #include \"rerere.h\"\n \n static const char * const git_stash_helper_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_list_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\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@@ -595,6 +601,31 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int list_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint ret;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_list_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (!ref_exists(ref_stash))\n+\t\treturn 0;\n+\n+\targv_array_pushl(&args, \"log\", \"--format=%gd: %gs\", \"-g\",\n+\t\t\t \"--first-parent\", \"-m\", NULL);\n+\targv_array_pushv(&args, argv);\n+\targv_array_push(&args, ref_stash);\n+\targv_array_push(&args, \"--\");\n+\tret = cmd_log(args.argc, args.argv, prefix);\n+\targv_array_clear(&args);\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -625,6 +656,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"list\"))\n+\t\treturn !!list_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 8f2640fe9..6052441aa 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -382,11 +382,6 @@ have_stash () {\n \tgit rev-parse --verify --quiet $ref_stash >/dev/null\n }\n \n-list_stash () {\n-\thave_stash || return 0\n-\tgit log --format=\"%gd: %gs\" -g --first-parent -m \"$@\" $ref_stash --\n-}\n-\n show_stash () {\n \tALLOW_UNKNOWN_FLAGS=t\n \tassert_stash_like \"$@\"\n@@ -574,7 +569,7 @@ test -n \"$seen_non_option\" || set \"push\" \"$@\"\n case \"$1\" in\n list)\n \tshift\n-\tlist_stash \"$@\"\n+\tgit stash--helper list \"$@\"\n \t;;\n show)\n \tshift\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350859","messageId":"8d428b70338821bddd5c3210cf49cc84753685f0.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 3/6] stash: change `git stash show` usage text and documentation","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:46:36Z","receivedAt":"2018-06-25T16:46:55Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"It is already stated in documentation that it will accept any\noption known to `git diff`, but not in the usage text and some\nparts of the documentation.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n Documentation/git-stash.txt | 4 ++--\n builtin/stash--helper.c     | 4 ++--\n 2 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\nindex 7ef8c4791..e31ea7d30 100644\n--- a/Documentation/git-stash.txt\n+++ b/Documentation/git-stash.txt\n@@ -9,7 +9,7 @@ SYNOPSIS\n --------\n [verse]\n 'git stash' list [<options>]\n-'git stash' show [<stash>]\n+'git stash' show [<options>] [<stash>]\n 'git stash' drop [-q|--quiet] [<stash>]\n 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n 'git stash' branch <branchname> [<stash>]\n@@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash\n The command takes options applicable to the 'git log'\n command to control what is shown and how. See linkgit:git-log[1].\n \n-show [<stash>]::\n+show [<options>] [<stash>]::\n \n \tShow the changes recorded in the stash entry as a diff between the\n \tstashed contents and the commit back when the stash entry was first\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex a7eba3fba..4f49fd04b 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,7 +13,7 @@\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -27,7 +27,7 @@ static const char * const git_stash_helper_list_usage[] = {\n };\n \n static const char * const git_stash_helper_show_usage[] = {\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tNULL\n };\n \n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350860","messageId":"a75e7208e62b9008524dbb993d692fb8d9604cbe.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 6/6] stash: convert store to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:46:39Z","receivedAt":"2018-06-25T16:47:00Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash store to the helper and delete the store_stash function\nfrom the shell script.\n\nAdd the usage string which was forgotten in the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 48 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 43 ++----------------------------------\n 2 files changed, 50 insertions(+), 41 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 4589e12d6..556d91b20 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -20,6 +20,7 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n+\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n \tNULL\n };\n \n@@ -58,6 +59,11 @@ static const char * const git_stash_helper_clear_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_store_usage[] = {\n+\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -719,6 +725,46 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n \treturn 0;\n }\n \n+static int store_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct object_id obj;\n+\tint ret = 0;\n+\tconst char *stash_msg = NULL;\n+\tconst char *w_commit = NULL;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\", N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_store_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (argc != 1) {\n+\t\tfprintf(stderr, _(\"\\\"git stash--helper store\\\" requires one <commit> argument\\n\"));\n+\t\treturn -1;\n+\t}\n+\n+\tw_commit = argv[0];\n+\n+\tif (!stash_msg)\n+\t\tstash_msg  = xstrdup(\"Created via \\\"git stash--helper store\\\".\");\n+\n+\tret = get_oid(w_commit, &obj);\n+\tif (!ret) {\n+\t\tret = update_ref(stash_msg, ref_stash, &obj, NULL,\n+\t\t\t\t REF_FORCE_CREATE_REFLOG,\n+\t\t\t\t quiet ? UPDATE_REFS_QUIET_ON_ERR :\n+\t\t\t\t UPDATE_REFS_MSG_ON_ERR);\n+\t}\n+\n+\tif (ret && !quiet)\n+\t\tfprintf_ln(stderr, _(\"Cannot update %s with %s\"), ref_stash, w_commit);\n+\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -753,6 +799,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!list_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"show\"))\n \t\treturn !!show_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"store\"))\n+\t\treturn !!store_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 0d05cbc1e..5739c5152 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -191,45 +191,6 @@ create_stash () {\n \tdie \"$(gettext \"Cannot record working tree state\")\"\n }\n \n-store_stash () {\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\tstash_msg=\"$1\"\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t-q|--quiet)\n-\t\t\tquiet=t\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\ttest $# = 1 ||\n-\tdie \"$(eval_gettext \"\\\"$dashless store\\\" requires one <commit> argument\")\"\n-\n-\tw_commit=\"$1\"\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tstash_msg=\"Created via \\\"git stash store\\\".\"\n-\tfi\n-\n-\tgit update-ref --create-reflog -m \"$stash_msg\" $ref_stash $w_commit\n-\tret=$?\n-\ttest $ret != 0 && test -z \"$quiet\" &&\n-\tdie \"$(eval_gettext \"Cannot update \\$ref_stash with \\$w_commit\")\"\n-\treturn $ret\n-}\n-\n push_stash () {\n \tkeep_index=\n \tpatch_mode=\n@@ -308,7 +269,7 @@ push_stash () {\n \t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n \n \tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n-\tstore_stash -m \"$stash_msg\" -q $w_commit ||\n+\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n \tdie \"$(gettext \"Cannot save the current status\")\"\n \tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n \n@@ -468,7 +429,7 @@ create)\n \t;;\n store)\n \tshift\n-\tstore_stash \"$@\"\n+\tgit stash--helper store \"$@\"\n \t;;\n drop)\n \tshift\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350861","messageId":"b5c8ad3ae96a1464cf024cabc219d0191709715a.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 2/6] stash: convert show to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:46:35Z","receivedAt":"2018-06-25T16:47:04Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash show to the helper and delete the show_stash, have_stash,\nassert_stash_like, is_stash_like and parse_flags_and_rev functions\nfrom the shell script now that they are no longer needed.\n\nBefore this commit, `git stash show` would ignore `--index` and\n`--quiet` options. Now, `git stash show` errors out on `--index`\nand does not display any message on `--quiet`, but errors out\nif the stash is not empty.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c |  77 +++++++++++++++++++++++\n git-stash.sh            | 132 +---------------------------------------\n 2 files changed, 78 insertions(+), 131 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 2ed21f5d1..a7eba3fba 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,6 +13,7 @@\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n+\tN_(\"git stash--helper show [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -25,6 +26,11 @@ static const char * const git_stash_helper_list_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_show_usage[] = {\n+\tN_(\"git stash--helper show [<stash>]\"),\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@@ -626,6 +632,75 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int show_stat = 1;\n+static int show_patch;\n+\n+static int git_stash_config(const char *var, const char *value, void *cb)\n+{\n+\tif (!strcmp(var, \"stash.showStat\")) {\n+\t\tshow_stat = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(var, \"stash.showPatch\")) {\n+\t\tshow_patch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\treturn git_default_config(var, value, cb);\n+}\n+\n+static int show_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint i, ret = 0;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\tstruct argv_array args_refs = 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\t     git_stash_helper_show_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\targv_array_push(&args, \"diff\");\n+\n+\t/* Push args which are not options into args_refs. */\n+\tfor (i = 0; i < argc; ++i) {\n+\t\tif (argv[i][0] == '-')\n+\t\t\targv_array_push(&args, argv[i]);\n+\t\telse\n+\t\t\targv_array_push(&args_refs, argv[i]);\n+\t}\n+\n+\tif (get_stash_info(&info, args_refs.argc, args_refs.argv)) {\n+\t\targv_array_clear(&args);\n+\t\targv_array_clear(&args_refs);\n+\t\treturn -1;\n+\t}\n+\n+\t/*\n+\t * The config settings are applied only if there are not passed\n+\t * any flags.\n+\t */\n+\tif (args.argc == 1) {\n+\t\tgit_config(git_stash_config, NULL);\n+\t\tif (show_stat)\n+\t\t\targv_array_push(&args, \"--stat\");\n+\n+\t\tif (show_patch)\n+\t\t\targv_array_push(&args, \"-p\");\n+\t}\n+\n+\targv_array_pushl(&args, oid_to_hex(&info.b_commit),\n+\t\t\t oid_to_hex(&info.w_commit), NULL);\n+\n+\tret = cmd_diff(args.argc, args.argv, prefix);\n+\n+\tfree_stash_info(&info);\n+\targv_array_clear(&args);\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -658,6 +733,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!branch_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"list\"))\n \t\treturn !!list_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"show\"))\n+\t\treturn !!show_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 6052441aa..0d05cbc1e 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -378,35 +378,6 @@ save_stash () {\n \tfi\n }\n \n-have_stash () {\n-\tgit rev-parse --verify --quiet $ref_stash >/dev/null\n-}\n-\n-show_stash () {\n-\tALLOW_UNKNOWN_FLAGS=t\n-\tassert_stash_like \"$@\"\n-\n-\tif test -z \"$FLAGS\"\n-\tthen\n-\t\tif test \"$(git config --bool stash.showStat || echo true)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=--stat\n-\t\tfi\n-\n-\t\tif test \"$(git config --bool stash.showPatch || echo false)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=${FLAGS}${FLAGS:+ }-p\n-\t\tfi\n-\n-\t\tif test -z \"$FLAGS\"\n-\t\tthen\n-\t\t\treturn 0\n-\t\tfi\n-\tfi\n-\n-\tgit diff ${FLAGS} $b_commit $w_commit\n-}\n-\n show_help () {\n \texec git help stash\n \texit 1\n@@ -448,107 +419,6 @@ show_help () {\n #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n #\n \n-parse_flags_and_rev()\n-{\n-\ttest \"$PARSE_CACHE\" = \"$*\" && return 0 # optimisation\n-\tPARSE_CACHE=\"$*\"\n-\n-\tIS_STASH_LIKE=\n-\tIS_STASH_REF=\n-\tINDEX_OPTION=\n-\ts=\n-\tw_commit=\n-\tb_commit=\n-\ti_commit=\n-\tu_commit=\n-\tw_tree=\n-\tb_tree=\n-\ti_tree=\n-\tu_tree=\n-\n-\tFLAGS=\n-\tREV=\n-\tfor opt\n-\tdo\n-\t\tcase \"$opt\" in\n-\t\t\t-q|--quiet)\n-\t\t\t\tGIT_QUIET=-t\n-\t\t\t;;\n-\t\t\t--index)\n-\t\t\t\tINDEX_OPTION=--index\n-\t\t\t;;\n-\t\t\t--help)\n-\t\t\t\tshow_help\n-\t\t\t;;\n-\t\t\t-*)\n-\t\t\t\ttest \"$ALLOW_UNKNOWN_FLAGS\" = t ||\n-\t\t\t\t\tdie \"$(eval_gettext \"unknown option: \\$opt\")\"\n-\t\t\t\tFLAGS=\"${FLAGS}${FLAGS:+ }$opt\"\n-\t\t\t;;\n-\t\t\t*)\n-\t\t\t\tREV=\"${REV}${REV:+ }'$opt'\"\n-\t\t\t;;\n-\t\tesac\n-\tdone\n-\n-\teval set -- $REV\n-\n-\tcase $# in\n-\t\t0)\n-\t\t\thave_stash || die \"$(gettext \"No stash entries found.\")\"\n-\t\t\tset -- ${ref_stash}@{0}\n-\t\t;;\n-\t\t1)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tdie \"$(eval_gettext \"Too many revisions specified: \\$REV\")\"\n-\t\t;;\n-\tesac\n-\n-\tcase \"$1\" in\n-\t\t*[!0-9]*)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tset -- \"${ref_stash}@{$1}\"\n-\t\t;;\n-\tesac\n-\n-\tREV=$(git rev-parse --symbolic --verify --quiet \"$1\") || {\n-\t\treference=\"$1\"\n-\t\tdie \"$(eval_gettext \"\\$reference is not a valid reference\")\"\n-\t}\n-\n-\ti_commit=$(git rev-parse --verify --quiet \"$REV^2\") &&\n-\tset -- $(git rev-parse \"$REV\" \"$REV^1\" \"$REV:\" \"$REV^1:\" \"$REV^2:\" 2>/dev/null) &&\n-\ts=$1 &&\n-\tw_commit=$1 &&\n-\tb_commit=$2 &&\n-\tw_tree=$3 &&\n-\tb_tree=$4 &&\n-\ti_tree=$5 &&\n-\tIS_STASH_LIKE=t &&\n-\ttest \"$ref_stash\" = \"$(git rev-parse --symbolic-full-name \"${REV%@*}\")\" &&\n-\tIS_STASH_REF=t\n-\n-\tu_commit=$(git rev-parse --verify --quiet \"$REV^3\") &&\n-\tu_tree=$(git rev-parse \"$REV^3:\" 2>/dev/null)\n-}\n-\n-is_stash_like()\n-{\n-\tparse_flags_and_rev \"$@\"\n-\ttest -n \"$IS_STASH_LIKE\"\n-}\n-\n-assert_stash_like() {\n-\tis_stash_like \"$@\" || {\n-\t\targs=\"$*\"\n-\t\tdie \"$(eval_gettext \"'\\$args' is not a stash-like commit\")\"\n-\t}\n-}\n-\n test \"$1\" = \"-p\" && set \"push\" \"$@\"\n \n PARSE_CACHE='--not-parsed'\n@@ -573,7 +443,7 @@ list)\n \t;;\n show)\n \tshift\n-\tshow_stash \"$@\"\n+\tgit stash--helper show \"$@\"\n \t;;\n save)\n \tshift\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350862","messageId":"dcd10e941418cbf4bbc50719f1f567f4025fe123.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 5/6] stash: update `git stash show` documentation","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:46:38Z","receivedAt":"2018-06-25T16:47:06Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add in documentation about the change of behavior regarding\nthe `--quiet` option, which was introduced in the last commit.\n(the `--quiet` option does not exit anymore with erorr if it\nis given an empty stash as argument)\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n Documentation/git-stash.txt | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\nindex e31ea7d30..d60ebdb96 100644\n--- a/Documentation/git-stash.txt\n+++ b/Documentation/git-stash.txt\n@@ -117,6 +117,9 @@ show [<options>] [<stash>]::\n \tYou can use stash.showStat and/or stash.showPatch config variables\n \tto change the default behavior.\n \n+\tIt accepts any option known to `git diff`, but acts different on\n+\t`--quiet` option and exit with zero regardless of differences.\n+\n pop [--index] [-q|--quiet] [<stash>]::\n \n \tRemove a single stashed state from the stash list and apply it\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"350863","messageId":"a3b02c25de08579df65ecb1de62a7b8f0434077e.1529943789.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v6 4/6] stash: refactor `show_stash()` to use the diff API","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-25T16:46:37Z","receivedAt":"2018-06-25T16:47:10Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Currently, `show_stash()` uses `cmd_diff()` to generate\nthe output. After this commit, the output will be generated\nusing the internal API.\n\nBefore this commit, `git stash show --quiet` would act like\n`git diff` and error out if the stash is not empty. Now, the\n`--quiet` option does not error out given an empty stash.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 72 +++++++++++++++++++++++++----------------\n 1 file changed, 45 insertions(+), 27 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 4f49fd04b..4589e12d6 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -10,6 +10,8 @@\n #include \"run-command.h\"\n #include \"dir.h\"\n #include \"rerere.h\"\n+#include \"revision.h\"\n+#include \"log-tree.h\"\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n@@ -650,55 +652,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)\n \n static int show_stash(int argc, const char **argv, const char *prefix)\n {\n-\tint i, ret = 0;\n-\tstruct argv_array args = ARGV_ARRAY_INIT;\n-\tstruct argv_array args_refs = ARGV_ARRAY_INIT;\n+\tint i;\n+\tint flags = 0;\n \tstruct stash_info info;\n+\tstruct rev_info rev;\n+\tstruct argv_array stash_args = ARGV_ARRAY_INIT;\n \tstruct option options[] = {\n \t\tOPT_END()\n \t};\n \n-\targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_show_usage,\n-\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\tinit_diff_ui_defaults();\n+\tgit_config(git_diff_ui_config, NULL);\n \n-\targv_array_push(&args, \"diff\");\n+\tinit_revisions(&rev, prefix);\n \n-\t/* Push args which are not options into args_refs. */\n-\tfor (i = 0; i < argc; ++i) {\n-\t\tif (argv[i][0] == '-')\n-\t\t\targv_array_push(&args, argv[i]);\n+\t/* Push args which are not options into stash_args. */\n+\tfor (i = 1; i < argc; ++i) {\n+\t\tif (argv[i][0] != '-')\n+\t\t\targv_array_push(&stash_args, argv[i]);\n \t\telse\n-\t\t\targv_array_push(&args_refs, argv[i]);\n-\t}\n-\n-\tif (get_stash_info(&info, args_refs.argc, args_refs.argv)) {\n-\t\targv_array_clear(&args);\n-\t\targv_array_clear(&args_refs);\n-\t\treturn -1;\n+\t\t\tflags++;\n \t}\n \n \t/*\n \t * The config settings are applied only if there are not passed\n \t * any flags.\n \t */\n-\tif (args.argc == 1) {\n+\tif (!flags) {\n \t\tgit_config(git_stash_config, NULL);\n \t\tif (show_stat)\n-\t\t\targv_array_push(&args, \"--stat\");\n+\t\t\trev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;\n+\t\tif (show_patch) {\n+\t\t\trev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;\n+\t\t\trev.diffopt.output_format |= DIFF_FORMAT_PATCH;\n+\t\t}\n+\t}\n \n-\t\tif (show_patch)\n-\t\t\targv_array_push(&args, \"-p\");\n+\tif (get_stash_info(&info, stash_args.argc, stash_args.argv)) {\n+\t\targv_array_clear(&stash_args);\n+\t\treturn -1;\n \t}\n \n-\targv_array_pushl(&args, oid_to_hex(&info.b_commit),\n-\t\t\t oid_to_hex(&info.w_commit), NULL);\n+\targc = setup_revisions(argc, argv, &rev, NULL);\n+\tif (!rev.diffopt.output_format)\n+\t\trev.diffopt.output_format = DIFF_FORMAT_PATCH;\n+\tdiff_setup_done(&rev.diffopt);\n+\trev.diffopt.flags.recursive = 1;\n+\tsetup_diff_pager(&rev.diffopt);\n \n-\tret = cmd_diff(args.argc, args.argv, prefix);\n+\t/*\n+\t * We can return early if there was any option not recognised by\n+\t * `diff_opt_parse()`, besides the word `stash`.\n+\t */\n+\tif (argc > 1) {\n+\t\tfree_stash_info(&info);\n+\t\targv_array_clear(&stash_args);\n+\t\tusage_with_options(git_stash_helper_show_usage, options);\n+\t}\n+\n+\t/* Do the diff thing. */\n+\tdiff_tree_oid(&info.b_commit, &info.w_commit, \"\", &rev.diffopt);\n+\tlog_tree_diff_flush(&rev);\n \n \tfree_stash_info(&info);\n-\targv_array_clear(&args);\n-\treturn ret;\n+\targv_array_clear(&stash_args);\n+\treturn 0;\n }\n \n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n-- \n2.18.0.rc2.13.g506fc12fb\n\n"},{"id":"351045","messageId":"nycvar.QRO.7.76.6.1806262357400.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T21:59:43Z","receivedAt":"2018-06-26T21:59:52Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> This first series of patches does bring some changes and improvements to\n> the test suite. One of the patches also introduces a new function\n> `get_oidf()` which will be hepful for the incoming patches related to\n> `git stash`.\n\nFor reviewers: it is *my* fault that this patch submission is a bit funny\nwith two 1/4 and one 1/6 patches... *I* suggested to not send a 14-strong\npatch series but split it into three, and then I failed to explain the\ncorrect invocation to do that from the command-line.\n\nMy sincere apologies,\nDscho\n"},{"id":"351046","messageId":"nycvar.QRO.7.76.6.1806270000180.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"4c1f71b067ac235c3003d58e1f3dfb07866be129.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 1/4] sha1-name.c: added 'get_oidf', which acts like 'get_oid'","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T22:02:22Z","receivedAt":"2018-06-26T22:02:30Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nas a general rule, we try to keep the commit subjects in the imperative,\ni.e.\n\n\tsha1-name.c: add 'get_oidf', which acts like 'get_oid'\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> Compared to 'get_oid', 'get_oidf' has as parameters a\n> printf format string and the additional arguments. This\n> will help simplify the code in subsequent commits.\n\nGood. The patch is obviously correct, from my perspective.\n\nCiao,\nDscho\n"},{"id":"351047","messageId":"nycvar.QRO.7.76.6.1806270003150.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"2fe77c0946231dd9235d88922e4d56dd7f5c66c7.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 3/4] stash: update test cases conform to coding guidelines","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T22:08:17Z","receivedAt":"2018-06-26T22:08:24Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> Removed whitespaces after redirection operators.\n\nThat is accurate a description of what the patch does. Let's add the why\n(and change the tense to present tense, as is the custom in our commit\nmessages):\n\n\tThis adjusts t3903-stash.sh to conform with our coding conventions\n\tbetter, by removing whitespace between redirection operators and\n\ttheir file names.\n\nThe patch does two more things, though: it breaks at least one long line,\nand it renames at least two non-descriptive files to the much better name\n`expected`. Maybe you want to mention that in the commit message, to?\n\nThanks,\nDscho\n"},{"id":"351048","messageId":"nycvar.QRO.7.76.6.1806270008440.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"0f49fc90bee5e6a297c49224f1451a7aee4c8635.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 4/4] stash: renamed test cases to be more descriptive","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T22:09:42Z","receivedAt":"2018-06-26T22:09:51Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> Renamed some test cases' labels to be more descriptive and under 80\n> characters per line.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n\nAs I suggested this kind of change, I am obviously happy with this patch.\n\nApart from minor suggestions, I think this patch series is good to go.\n\nThank you!\nDscho\n"},{"id":"351049","messageId":"nycvar.QRO.7.76.6.1806270011441.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T22:12:11Z","receivedAt":"2018-06-26T22:12:19Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> This second series of patches contains commits to convert `apply`, `drop`,\n> `clear`, `branch`, `pop` stash subcommands to builtins.\n> \n> \n> Joel Teichroeb (4):\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\nWere there any changes since v5 in these patches?\n\nCiao,\nDscho\n"},{"id":"351050","messageId":"nycvar.QRO.7.76.6.1806270015060.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"5373f422a82da2357828b8cc4a2b84fb2bedd780.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 2/4] stash: convert drop and clear to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T22:17:51Z","receivedAt":"2018-06-26T22:17:59Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 1c4387b10..84a537f39 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -414,6 +451,77 @@ 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> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\tint ret;\n> +\n> +\t/*\n> +\t * reflog does not provide a simple function for deleting refs. One will\n> +\t * need to be added to avoid implementing too much reflog code here\n> +\t */\n> +\targv_array_pushl(&args, \"reflog\", \"delete\", \"--updateref\", \"--rewrite\",\n> +\t\t\t 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,\n> +\t\t\t       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> +\t/*\n> +\t * This could easily be replaced by get_oid, but currently it will throw a\n> +\t * fatal error when a reflog is empty, which we can not recover from\n> +\t */\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\nI thought you had introduced `get_oidf()` specifically so you could avoid\nthe `rev-parse` call... `get_oidf(&dummy_oid, \"%s@{0}\", ref_stash)` should\ndo this, right?\n\nCiao,\nDscho\n"},{"id":"351052","messageId":"nycvar.QRO.7.76.6.1806270018580.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"ac0a24da8a583a137176d1d215df65bb1a3ada0e.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 3/4] stash: convert branch to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T22:23:53Z","receivedAt":"2018-06-26T22:24:01Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 84a537f39..fbf78249c 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -522,6 +528,41 @@ 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\t     git_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\nWhy not combine these? _pushl() takes a NULL-terminated list:\n\n\targv_array_pushl(&args, \"checkout\", \"-b\", branch,\n\t\t\t oid_to_hex(&info.b_commit), NULL);\n\n> +\tret = cmd_checkout(args.argc, args.argv, prefix);\n\nI guess it is okay to run that, but the cmd_() functions are not *really*\nmeant to be called this way... Personally, I would be more comfortable\nwith a `run_command()` here, i.e. with a spawned process, until the time\nwhen checkout.c/checkout.h have learned enough API for a direct call.\n\nCiao,\nDscho\n"},{"id":"351054","messageId":"nycvar.QRO.7.76.6.1806270029450.21419@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"5d5b05caecc01d8f252ef5f8495549663815ed63.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v6 4/4] stash: convert pop to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-06-26T22:31:37Z","receivedAt":"2018-06-26T22:31:42Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nI think I had revewied these 4 patches before, and I'd wager a bet that\nyou addressed all of my suggestions, if any.\n\nI had a look over patches 2-4, and want to take a little bit more time\ntomorrow to pour over patch 1 (which is a little larger, as it lays a lot\nof ground work), to make sure that I cannot find anything else to improve.\n\nWell done so far!\nDscho\n\n\nOn Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n\n> From: Joel Teichroeb <joel@teichroeb.net>\n> \n> Add stash pop to the helper and delete the pop_stash, drop_stash,\n> assert_stash_ref functions from the shell script now that they\n> are no longer needed.\n> \n> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-\n>  git-stash.sh            | 47 ++---------------------------------------\n>  2 files changed, 37 insertions(+), 46 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index fbf78249c..a38d6ae8a 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -13,7 +13,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 apply [--index] [-q|--quiet] [<stash>]\"),\n> +\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n>  \tN_(\"git stash--helper clear\"),\n>  \tNULL\n> @@ -24,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> @@ -528,6 +533,33 @@ 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\t     git_stash_helper_pop_usage, 0);\n> +\n> +\tif (get_stash_info(&info, argc, argv))\n> +\t\treturn -1;\n> +\n> +\tassert_stash_ref(&info);\n> +\tif ((ret = do_apply_stash(prefix, &info, index)))\n> +\t\tprintf_ln(_(\"The stash entry is kept in case you need it again.\"));\n> +\telse\n> +\t\tret = do_drop_stash(prefix, &info);\n> +\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> @@ -589,6 +621,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!clear_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"drop\"))\n>  \t\treturn !!drop_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"pop\"))\n> +\t\treturn !!pop_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"branch\"))\n>  \t\treturn !!branch_stash(argc, argv, prefix);\n>  \n> diff --git a/git-stash.sh b/git-stash.sh\n> index 29d9f4425..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> @@ -655,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> -- \n> 2.18.0.rc2.13.g506fc12fb\n> \n> \n"},{"id":"351148","messageId":"xmqqfu18f4zz.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1806270018580.21419@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v6 3/4] stash: convert branch to builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-06-27T18:39:44Z","receivedAt":"2018-06-27T18:39:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n>> +\tret = cmd_checkout(args.argc, args.argv, prefix);\n>\n> I guess it is okay to run that, but the cmd_() functions are not *really*\n> meant to be called this way... Personally, I would be more comfortable\n> with a `run_command()` here, i.e. with a spawned process, until the time\n> when checkout.c/checkout.h have learned enough API for a direct call.\n\nThanks for pointing it out.  I'll add a bit more for those who are\nreading from sideline (iow, if you are Dscho, you do not have to\nread the remainder, as I know Dscho knows all of this).\n\nIt is not OK to use cmd_$foo() as subroutine; depending on the value\nof $foo, where the call is made and the number of times the call is\nmade, it may happen to work OK in the current code, but that is a\nticking timebomb waiting to go off.\n\nThis is primarily because cmd_$foo() is designed to be replacement\nof \"main()\" in usual programs---it is allowed to assume the global\nvariables it uses have their initial values and nobody cares the\nstate it leaves behind when it returns.  Argument parser may flip\nbits in file-scope static variables to record which options are\nseen, assuming that these variables are initialized to all-zero, and\nthat assumption would not hold for the second call to cmd_$foo(),\netc.  cmd_$foo() also is allowed to call die() when there is an\nunrecoverable error condition in the context of carrying out $foo; a\nscripted Porcelain that used $foo as a building block to implement a\nlarger operation (e.g. \"stash\" that used \"checkout\" to switch to a\ndifferent branch) may want to react to the failure and do something\nextra (i.e. \"git checkout || do something more\").  \n\nUsing run_command() interface would not cause any of these problems;\nthe subcommand will run in a clean environment, and the caller can\nreact to its failure.\n"},{"id":"351150","messageId":"xmqqbmbwf4mf.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1806262357400.21419@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-06-27T18:47:52Z","receivedAt":"2018-06-27T18:47:59Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> Hi,\n>\n> On Mon, 25 Jun 2018, Paul-Sebastian Ungureanu wrote:\n>\n>> This first series of patches does bring some changes and improvements to\n>> the test suite. One of the patches also introduces a new function\n>> `get_oidf()` which will be hepful for the incoming patches related to\n>> `git stash`.\n>\n> For reviewers: it is *my* fault that this patch submission is a bit funny\n> with two 1/4 and one 1/6 patches... *I* suggested to not send a 14-strong\n> patch series but split it into three, and then I failed to explain the\n> correct invocation to do that from the command-line.\n>\n> My sincere apologies,\n> Dscho\n\nHeh, what is more useful than apology is to tell us which order\nthese three (apparent) series build on top of each other ;-)\n\nThe answer, IIUC, is that \n\n * oidf+tests come first, then\n * apply/drop/branch/pop (as these rely on oidf) on top, and finally\n * list (as it wants to add to stash--helper that is a new file in the middle)\n\nWhen there is clear dependency like that, I agree that it would help\nreaders to emphasize that these cannot be applied in an arbitrary\norder.  It is especially true as the second part of the above _will_\napply more-or-less cleanly without the first one, and then fail to\ncompile due to lack of oidf.\n\nThanks.\n"},{"id":"351231","messageId":"5677e7cd-a0f9-194c-a7bd-5eea48c56e3d@gmail.com","threadId":"48222","inReplyTo":"xmqqfu18f4zz.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v6 3/4] stash: convert branch to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-28T16:17:58Z","receivedAt":"2018-06-28T16:18:05Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nOn 27.06.2018 21:39, Junio C Hamano wrote:\n> This is primarily because cmd_$foo() is designed to be replacement\n> of \"main()\" in usual programs---it is allowed to assume the global\n> variables it uses have their initial values and nobody cares the\n> state it leaves behind when it returns.  Argument parser may flip\n> bits in file-scope static variables to record which options are\n> seen, assuming that these variables are initialized to all-zero, and\n> that assumption would not hold for the second call to cmd_$foo(),\n> etc.  cmd_$foo() also is allowed to call die() when there is an\n> unrecoverable error condition in the context of carrying out $foo; a\n> scripted Porcelain that used $foo as a building block to implement a\n> larger operation (e.g. \"stash\" that used \"checkout\" to switch to a\n> different branch) may want to react to the failure and do something\n> extra (i.e. \"git checkout || do something more\").\n> \n> Using run_command() interface would not cause any of these problems;\n> the subcommand will run in a clean environment, and the caller can\n> react to its failure.\n> \n\nThank you a lot for this great explanation. When I will submit a new \niteration, I will make sure to replace calls of `cmd_$foo()` with \n`run_command()` (or with API, if possible).\n\nBest regards,\nPaul Ungureanu\n"},{"id":"351273","messageId":"41665162-3020-be5c-1040-fd3ced67436a@gmail.com","threadId":"48222","inReplyTo":"xmqqbmbwf4mf.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v6 0/4] stash: add new tests and introduce a new helper function","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-28T22:32:56Z","receivedAt":"2018-06-28T22:33:03Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\n> Heh, what is more useful than apology is to tell us which order\n> these three (apparent) series build on top of each other ;-)\n> \n> The answer, IIUC, is that\n> \n>   * oidf+tests come first, then\n>   * apply/drop/branch/pop (as these rely on oidf) on top, and finally\n>   * list (as it wants to add to stash--helper that is a new file in the middle)\n> \n> When there is clear dependency like that, I agree that it would help\n> readers to emphasize that these cannot be applied in an arbitrary\n> order.  It is especially true as the second part of the above _will_\n> apply more-or-less cleanly without the first one, and then fail to\n> compile due to lack of oidf.\n> \n> Thanks.\n> \n\nI would actually say that it was 100% my fault (I could have tested \nbefore sending patches or ask if I was not completely sure) and I would \nlike to apologize for this.\n\nThe order of the commits is actually good. The only thing that is not \nright are the cover letters, which are missing. Every 1/N marks the \nbeginning of a series of patches. Just to be more clear, this is the \nright order:\n\nFirst patch:\nsha1-name.c: added 'get_oidf', which acts like 'get_oid'\nstash: improve option parsing test coverage\nstash: update test cases conform to coding guidelines\nstash: renamed test cases to be more descriptive\n\nSecond patch:\nstash: convert apply to builtin\nstash: convert drop and clear to builtin\nstash: convert branch to builtin\nstash: convert pop to builtin\n\nThird patch:\nstash: implement the \"list\" command in the builtin\nstash: convert show to builtin\nstash: change `git stash show` usage text and documentation\nstash: refactor `show_stash()` to use the diff API\nstash: update `git stash show` documentation\nstash: convert store to builtin\n\nThe only thing which was in the cover letters and might be worth \nmentioning here is related to `git stash show`. Although it might not be \nefficient, a 1:1 conversion is more easily to follow and review. Because \nof that, the first commit related to `git stash show` approaches a 1:1 \nconversion to C. There is a subsequent patch (`refactor ....`) which \nmakes the `show` subcommand use the existent API. Any change of behavior \nis noted in the commit message which introduces that change.\n\nBest,\nPaul\n"},{"id":"351275","messageId":"16aa7eed-19af-9793-508e-a13caef020a2@gmail.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1806270015060.21419@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v6 2/4] stash: convert drop and clear to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-28T22:51:46Z","receivedAt":"2018-06-28T22:51:52Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hi,\n\nOn 27.06.2018 01:17, Johannes Schindelin wrote:\n> I thought you had introduced `get_oidf()` specifically so you could avoid\n> the `rev-parse` call... `get_oidf(&dummy_oid, \"%s@{0}\", ref_stash)` should\n> do this, right?\n\nWe discussed this over the IRC channel, but since not everybody might \nfollow the chat, I would like to put it also here.\n\nThe main reason why `get_oidf()` was introduced is to make \n`assert_stash_like()` less tedious. I am sure that this is not the only \nplace where this function could be useful.\n\nOver the last weeks, I have been working in parallel on adding a new \nflag (`GET_OID_GENTLY`) for `get_oid_with_context()`. By using this \nflag, the `rev-parse` call could be replaced with an internal API call. \nI hope that I will be able to send this patch soon enough.\n\nBest regards,\nPaul\n"},{"id":"351277","messageId":"2882d57b-2e98-b3f6-6f85-4e6f8c6feca6@gmail.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1806270011441.21419@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v6 0/4] stash: Convert some `git stash` commands to a builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-06-28T23:14:14Z","receivedAt":"2018-06-28T23:14:23Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nOn 27.06.2018 01:12, Johannes Schindelin wrote:\n\n>> Joel Teichroeb (4):\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> Were there any changes since v5 in these patches?\n> \n\nThere are some changes since v5 based on the reviews.\n\nThere are also some changes which were not suggested in the reviews, but \nwhich I considered to be good (I hope they really are). For example, in \nthe previous version of the patch that introduces the `stash--helper`, \nthere was no `assert_stash_like()` function (it was part of \n`get_stash_info()`). In v6, it was implemented as a separate function in \norder to keep it more alike to `git-stash.sh` script. Another difference \nwas to change `assert_stash_ref()` function type from `int` to `void` \nand call `exit()` inside. This was introduced in order to make it more \nsimilar with `assert()` from `assert.h`. I do not think of these changes \nto be something of great importance, but I hope they might make the code \neasier to read.\n\nBest,\nPaul\n"},{"id":"354886","messageId":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 00/26] Convert \"git stash\" to C builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:39Z","receivedAt":"2018-08-08T18:59:18Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nHere is the whole `git stash` C version. Some of the previous\npatches were already reviewed (up to and including \"stash: convert\nstore to builtin\"), but there are some which were not\n(starting with \"stash: convert create to builtin\").\n\nIn order to see the difference between the shell version and\nthe C version, I ran `time` on:\n\n* git test suite (t3903-stash.sh, t3904-stash-patch.sh,\nt3905-stash-include-untracked.sh and t3906-stash-submodule.sh)\n\n        t3903-stash.sh:\n        ** SHELL: 12,69s user 9,95s system 109% cpu 20,730 total\n        **     C:  2,67s user 2,84s system 105% cpu  5,206 total\n\n        t3904-stash-patch.sh:\n        ** SHELL: 1,43s user 0,94s system 106% cpu 2,242 total\n        **     C: 1,01s user 0,58s system 104% cpu 1,530 total\n\n        t3905-stash-include-untracked.sh\n        ** SHELL: 2,22s user 1,73s system 110% cpu 3,569 total\n        **     C: 0,59s user 0,57s system 106% cpu 1,085 total\n\n        t3906-stash-submodule.sh\n        ** SHELL: 2,89s user 2,99s system 106% cpu 5,527 total\n        **     C: 2,21s user 2,61s system 105% cpu 4,568 total\n\n        TOTAL:\n        ** SHELL: 19.23s user 15.61s system\n        **     C:  6.48s user  6.60s system\n\n* a git repository with 4000 files: 1000 not changed,\n1000 staged files, 1000 unstaged files, 1000 untracked.\nIn this case I ran some of the most used commands:\n\n        git stash push:\n\n        ** SHELL: 0,12s user 0,21s system 101% cpu 0,329 total\n        **     C: 0,06s user 0,13s system 105% cpu 0,185 total\n\n        git stash push -u:\n\n        ** SHELL: 0,18s user 0,27s system  108% cpu 0,401 total\n        **     C: 0,09s user 0,19s system  103% cpu 0,267 total\n\n        git stash pop:\n\n        ** SHELL: 0,16s user 0,26s system 103% cpu 0,399 total\n        **     C: 0,13s user 0,19s system 102% cpu 0,308 total\n\nBest regards,\nPaul Ungureanu\n\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\nPaul-Sebastian Ungureanu (21):\n  sha1-name.c: added 'get_oidf', which acts like 'get_oid'\n  stash: update test cases conform to coding guidelines\n  stash: renamed test cases to be more descriptive\n  stash: implement the \"list\" command in the builtin\n  stash: convert show to builtin\n  stash: change `git stash show` usage text and documentation\n  stash: refactor `show_stash()` to use the diff API\n  stash: update `git stash show` documentation\n  stash: convert store to builtin\n  stash: convert create to builtin\n  stash: replace spawning a \"read-tree\" process\n  stash: avoid spawning a \"diff-index\" process\n  stash: convert push to builtin\n  stash: make push to be quiet\n  stash: add tests for `git stash push -q`\n  stash: replace spawning `git ls-files` child process\n  stash: convert save to builtin\n  stash: convert `stash--helper.c` into `stash.c`\n  stash: optimize `get_untracked_files()` and `check_changes()`\n  stash: replace all `write-tree` child processes with API calls\n  stash: replace all \"git apply\" child processes with API calls\n\n Documentation/git-stash.txt |    7 +-\n Makefile                    |    2 +-\n builtin.h                   |    1 +\n builtin/stash.c             | 1562 +++++++++++++++++++++++++++++++++++\n cache.h                     |    1 +\n git-stash.sh                |  752 -----------------\n git.c                       |    1 +\n sha1-name.c                 |   19 +\n t/t3903-stash.sh            |  190 +++--\n 9 files changed, 1714 insertions(+), 821 deletions(-)\n create mode 100644 builtin/stash.c\n delete mode 100755 git-stash.sh\n\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354887","messageId":"03006713a841adf68340f0dcf39ee17c99060eeb.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 01/26] sha1-name.c: added 'get_oidf', which acts like 'get_oid'","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:40Z","receivedAt":"2018-08-08T18:59:19Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Compared to 'get_oid', 'get_oidf' has as parameters a\nprintf format string and the additional arguments. This\nwill help simplify the code in subsequent commits.\n\nOriginal-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n cache.h     |  1 +\n sha1-name.c | 19 +++++++++++++++++++\n 2 files changed, 20 insertions(+)\n\ndiff --git a/cache.h b/cache.h\nindex 8dc7134f0..4ec09b336 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1321,6 +1321,7 @@ struct object_context {\n \tGET_OID_BLOB)\n \n extern int get_oid(const char *str, struct object_id *oid);\n+extern int get_oidf(struct object_id *oid, const char *fmt, ...);\n extern int get_oid_commit(const char *str, struct object_id *oid);\n extern int get_oid_committish(const char *str, struct object_id *oid);\n extern int get_oid_tree(const char *str, struct object_id *oid);\ndiff --git a/sha1-name.c b/sha1-name.c\nindex c9cc1318b..261b960bb 100644\n--- a/sha1-name.c\n+++ b/sha1-name.c\n@@ -1471,6 +1471,25 @@ int get_oid(const char *name, struct object_id *oid)\n \treturn get_oid_with_context(name, 0, oid, &unused);\n }\n \n+/*\n+ * This returns a non-zero value if the string (built using printf\n+ * format and the given arguments) is not a valid object.\n+ */\n+int get_oidf(struct object_id *oid, const char *fmt, ...)\n+{\n+\tva_list ap;\n+\tint ret;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tva_start(ap, fmt);\n+\tstrbuf_vaddf(&sb, fmt, ap);\n+\tva_end(ap);\n+\n+\tret = get_oid(sb.buf, oid);\n+\tstrbuf_release(&sb);\n+\n+\treturn ret;\n+}\n \n /*\n  * Many callers know that the user meant to name a commit-ish by\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354888","messageId":"57c8f7ebbd3cf5631bbec51c0cae9cb9978ae2c0.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 02/26] stash: improve option parsing test coverage","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:41Z","receivedAt":"2018-08-08T18:59:20Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nIn 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 parameters, or too few.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++\n 1 file changed, 35 insertions(+)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex 1f871d3cc..af7586d43 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '\n \ttest foo = \"$(cat file/file)\"\n '\n \n+test_expect_success 'giving too many ref arguments does not modify files' '\n+\tgit stash clear &&\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\techo foo >file2 &&\n+\tgit stash &&\n+\techo bar >file2 &&\n+\tgit stash &&\n+\ttest-tool chmtime =123456789 file2 &&\n+\tfor type in apply pop \"branch stash-branch\"\n+\tdo\n+\t\ttest_must_fail git stash $type stash@{0} stash@{1} 2>err &&\n+\t\ttest_i18ngrep \"Too many revisions\" err &&\n+\t\ttest 123456789 = $(test-tool chmtime -g file2) || return 1\n+\tdone\n+'\n+\n+test_expect_success 'drop: too many arguments errors out (does nothing)' '\n+\tgit stash list >expect &&\n+\ttest_must_fail git stash drop stash@{0} stash@{1} 2>err &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\tgit stash list >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'show: too many arguments errors out (does nothing)' '\n+\ttest_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\ttest_must_be_empty out\n+'\n+\n test_expect_success 'stash create - no changes' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n@@ -479,6 +509,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.18.0.573.g56500d98f\n\n"},{"id":"354889","messageId":"b37da07c02064bb2f427196234710021b0c49184.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 03/26] stash: update test cases conform to coding guidelines","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:42Z","receivedAt":"2018-08-08T18:59:22Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Removed whitespaces after redirection operators.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------\n 1 file changed, 61 insertions(+), 59 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex af7586d43..de6cab1fe 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -8,22 +8,22 @@ test_description='Test git stash'\n . ./test-lib.sh\n \n test_expect_success 'stash some dirty working directory' '\n-\techo 1 > file &&\n+\techo 1 >file &&\n \tgit add file &&\n \techo unrelated >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m initial &&\n-\techo 2 > file &&\n+\techo 2 >file &&\n \tgit add file &&\n-\techo 3 > file &&\n+\techo 3 >file &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file b/file\n index 0cfbf08..00750ed 100644\n --- a/file\n@@ -35,7 +35,7 @@ EOF\n \n test_expect_success 'parents of stash' '\n \ttest $(git rev-parse stash^) = $(git rev-parse HEAD) &&\n-\tgit diff stash^2..stash > output &&\n+\tgit diff stash^2..stash >output &&\n \ttest_cmp output expect\n '\n \n@@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '\n \n test_expect_success 'apply stashed changes (including index)' '\n \tgit reset --hard HEAD^ &&\n-\techo 6 > other-file &&\n+\techo 6 >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m other-file &&\n@@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '\n \n test_expect_success 'drop top stash' '\n \tgit reset --hard &&\n-\tgit stash list > stashlist1 &&\n-\techo 7 > file &&\n+\tgit stash list >expected &&\n+\techo 7 >file &&\n \tgit stash &&\n \tgit stash drop &&\n-\tgit stash list > stashlist2 &&\n-\ttest_cmp stashlist1 stashlist2 &&\n+\tgit stash list >actual &&\n+\ttest_cmp expected actual &&\n \tgit stash apply &&\n \ttest 3 = $(cat file) &&\n \ttest 1 = $(git show :file) &&\n@@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '\n \n test_expect_success 'drop middle stash' '\n \tgit reset --hard &&\n-\techo 8 > file &&\n+\techo 8 >file &&\n \tgit stash &&\n-\techo 9 > file &&\n+\techo 9 >file &&\n \tgit stash &&\n \tgit stash drop stash@{1} &&\n \ttest 2 = $(git stash list | wc -l) &&\n@@ -160,7 +160,7 @@ test_expect_success 'stash pop' '\n \ttest 0 = $(git stash list | wc -l)\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file2 b/file2\n new file mode 100644\n index 0000000..1fe912c\n@@ -170,7 +170,7 @@ index 0000000..1fe912c\n +bar2\n EOF\n \n-cat > expect1 << EOF\n+cat >expect1 <<EOF\n diff --git a/file b/file\n index 257cc56..5716ca5 100644\n --- a/file\n@@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644\n +bar\n EOF\n \n-cat > expect2 << EOF\n+cat >expect2 <<EOF\n diff --git a/file b/file\n index 7601807..5716ca5 100644\n --- a/file\n@@ -198,79 +198,79 @@ index 0000000..1fe912c\n EOF\n \n test_expect_success 'stash branch' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit commit file -m first &&\n-\techo bar > file &&\n-\techo bar2 > file2 &&\n+\techo bar >file &&\n+\techo bar2 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n-\techo baz > file &&\n+\techo baz >file &&\n \tgit commit file -m second &&\n \tgit stash branch stashbranch &&\n \ttest refs/heads/stashbranch = $(git symbolic-ref HEAD) &&\n \ttest $(git rev-parse HEAD) = $(git rev-parse master^) &&\n-\tgit diff --cached > output &&\n+\tgit diff --cached >output &&\n \ttest_cmp output expect &&\n-\tgit diff > output &&\n+\tgit diff >output &&\n \ttest_cmp output expect1 &&\n \tgit add file &&\n \tgit commit -m alternate\\ second &&\n-\tgit diff master..stashbranch > output &&\n+\tgit diff master..stashbranch >output &&\n \ttest_cmp output expect2 &&\n \ttest 0 = $(git stash list | wc -l)\n '\n \n test_expect_success 'apply -q is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\tgit stash apply -q > output.out 2>&1 &&\n+\tgit stash apply -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'save -q is quiet' '\n-\tgit stash save --quiet > output.out 2>&1 &&\n+\tgit stash save --quiet >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q is quiet' '\n-\tgit stash pop -q > output.out 2>&1 &&\n+\tgit stash pop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q --index works and is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit add file &&\n \tgit stash save --quiet &&\n-\tgit stash pop -q --index > output.out 2>&1 &&\n+\tgit stash pop -q --index >output.out 2>&1 &&\n \ttest foo = \"$(git show :file)\" &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'drop -q is quiet' '\n \tgit stash &&\n-\tgit stash drop -q > output.out 2>&1 &&\n+\tgit stash drop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'stash -k' '\n-\techo bar3 > file &&\n-\techo bar4 > file2 &&\n+\techo bar3 >file &&\n+\techo bar4 >file2 &&\n \tgit add file2 &&\n \tgit stash -k &&\n \ttest bar,bar4 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --no-keep-index' '\n-\techo bar33 > file &&\n-\techo bar44 > file2 &&\n+\techo bar33 >file &&\n+\techo bar44 >file2 &&\n \tgit add file2 &&\n \tgit stash --no-keep-index &&\n \ttest bar,bar2 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --invalid-option' '\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \ttest_must_fail git stash --invalid-option &&\n \ttest_must_fail git stash save --invalid-option &&\n@@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash drop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash pop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \n test_expect_success 'ref with non-existent reflog' '\n \tgit stash clear &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git rev-parse --quiet --verify does-not-exist &&\n@@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '\n test_expect_success 'invalid ref of the form stash@{n}, n >= N' '\n \tgit stash clear &&\n \ttest_must_fail git stash drop stash@{0} &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git stash drop stash@{1} &&\n@@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu\n \ttest_i18ncmp expect actual\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/HEAD b/HEAD\n new file mode 100644\n index 0000000..fe0cbee\n@@ -737,14 +739,14 @@ EOF\n test_expect_success 'stash where working directory contains \"HEAD\" file' '\n \tgit stash clear &&\n \tgit reset --hard &&\n-\techo file-not-a-ref > HEAD &&\n+\techo file-not-a-ref >HEAD &&\n \tgit add HEAD &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD &&\n \ttest \"$(git rev-parse stash^)\" = \"$(git rev-parse HEAD)\" &&\n-\tgit diff stash^..stash > output &&\n+\tgit diff stash^..stash >output &&\n \ttest_cmp output expect\n '\n \n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354890","messageId":"cb70dc2317b8bdbe640a50de7050cb405067c0f0.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:43Z","receivedAt":"2018-08-08T18:59:22Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Renamed some test cases' labels to be more descriptive and under 80\ncharacters per line.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex de6cab1fe..8d002a7f2 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \ttest_cmp expected actual\n '\n \n-test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'drop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit reset --hard HEAD\n '\n \n-test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'pop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form \"n\", n >= N' '\n \tgit stash drop\n '\n \n-test_expect_success 'stash branch should not drop the stash if the branch exists' '\n+test_expect_success 'branch: should not drop the stash if the branch exists' '\n \tgit stash clear &&\n \techo foo >file &&\n \tgit add file &&\n@@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash branch should not drop the stash if the apply fails' '\n+test_expect_success 'branch: should not drop the stash if the apply fails' '\n \tgit stash clear &&\n \tgit reset HEAD~1 --hard &&\n \techo foo >file &&\n@@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash apply shows status same as git status (relative to current directory)' '\n+test_expect_success 'apply: shows same status as git status (relative to ./)' '\n \tgit stash clear &&\n \techo 1 >subdir/subfile1 &&\n \techo 2 >subdir/subfile2 &&\n@@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec shows no changes when there are none' '\n+test_expect_success 'push: <pathspec> shows no changes when there are none' '\n \t>foo &&\n \tgit add foo &&\n \tgit commit -m \"tmp\" &&\n@@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec not in the repository errors out' '\n+test_expect_success 'push: <pathspec> not in the repository errors out' '\n \t>untracked &&\n \ttest_must_fail git stash push untracked &&\n \ttest_path_is_file untracked\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354891","messageId":"7ba9a8515d445d10af36a8a79071af51b90f5aef.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 05/26] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:44Z","receivedAt":"2018-08-08T18:59:25Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd a builtin helper for performing stash commands. Converting\nall at once proved hard to review, so starting with just apply\nlets conversion get started without the other commands 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n .gitignore              |   1 +\n Makefile                |   1 +\n builtin.h               |   1 +\n builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  78 +------\n git.c                   |   1 +\n 6 files changed, 463 insertions(+), 71 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 3284a1e9b..3abbba81a 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -156,6 +156,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 bc4fc8eea..af82bc7ee 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1083,6 +1083,7 @@ BUILTIN_OBJS += builtin/shortlog.o\n BUILTIN_OBJS += builtin/show-branch.o\n BUILTIN_OBJS += builtin/show-index.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 0362f1ce2..55ceb1574 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -222,6 +222,7 @@ 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_show_index(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..ef6a9d30d\n--- /dev/null\n+++ b/builtin/stash--helper.c\n@@ -0,0 +1,452 @@\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+#include \"rerere.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 struct strbuf stash_index_path = STRBUF_INIT;\n+\n+/*\n+ * w_commit is set to the commit containing the working tree\n+ * b_commit is set to the base commit\n+ * i_commit is set to the commit containing the index tree\n+ * u_commit is set to the commit containing the untracked files tree\n+ * w_tree is set to the working tree\n+ * b_tree is set to the base tree\n+ * i_tree is set to the index tree\n+ * u_tree is set to the untracked files tree\n+ */\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 void free_stash_info(struct stash_info *info)\n+{\n+\tstrbuf_release(&info->revision);\n+}\n+\n+static void assert_stash_like(struct stash_info *info, const char * revision)\n+{\n+\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n+\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n+\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n+\t    get_oidf(&info->i_tree, \"%s^2:\", revision)) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n+\t\texit(128);\n+\t}\n+}\n+\n+static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n+{\n+\tstruct strbuf symbolic = STRBUF_INIT;\n+\tint ret;\n+\tconst char *revision;\n+\tconst char *commit = NULL;\n+\tchar *end_of_rev;\n+\tchar *expanded_ref;\n+\tstruct object_id dummy;\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\"),\n+\t\t\t   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) {\n+\t\tif (!ref_exists(ref_stash)) {\n+\t\t\tfree_stash_info(info);\n+\t\t\tfprintf_ln(stderr, \"No stash entries found.\");\n+\t\t\treturn -1;\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+\tif (get_oid(revision, &info->w_commit)) {\n+\t\terror(_(\"%s is not a valid reference\"), revision);\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\n+\n+\tassert_stash_like(info, revision);\n+\n+\tinfo->has_u = !get_oidf(&info->u_tree, \"%s^3:\", revision);\n+\n+\tend_of_rev = strchrnul(revision, '@');\n+\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n+\n+\tret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);\n+\tstrbuf_release(&symbolic);\n+\tswitch (ret) {\n+\tcase 0: /* Not found, but valid ref */\n+\t\tinfo->is_stash_ref = 0;\n+\t\tbreak;\n+\tcase 1:\n+\t\tinfo->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n+\t\tbreak;\n+\tdefault: /* Invalid or ambiguous */\n+\t\tfree_stash_info(info);\n+\t}\n+\n+\tfree(expanded_ref);\n+\treturn !(ret == 0 || ret == 1);\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+\t/*\n+\t * Diff-tree would not be very hard to replace with a native function,\n+\t * however it should be done together with apply_cached.\n+\t */\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+\t/*\n+\t * Apply currently only reads either from stdin or a file, thus\n+\t * apply_all_patches would have to be updated to optionally take a\n+\t * buffer.\n+\t */\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 child_process cp = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * Reset is overall quite simple, however there is no current public\n+\t * API for resetting.\n+\t */\n+\tcp.git_cmd = 1;\n+\targv_array_push(&cp.args, \"reset\");\n+\n+\treturn run_command(&cp);\n+}\n+\n+static int get_newly_staged(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+\t/*\n+\t * diff-index is very similar to diff-tree above, and should be\n+\t * converted together with update_index.\n+\t */\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"diff-index\", \"--cached\", \"--name-only\",\n+\t\t\t \"--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+\t/*\n+\t * Update-index is very complicated and may need to have a public\n+\t * function exposed in order to remove this forking.\n+\t */\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+\t/*\n+\t * We need to run restore files from a given index, but without\n+\t * affecting the current index, so we use GIT_INDEX_FILE with\n+\t * run_command to fork processes that will not interfere.\n+\t */\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\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp)) {\n+\t\tremove_path(stash_index_path.buf);\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\",\n+\t\t\t stash_index_path.buf);\n+\n+\tres = run_command(&cp);\n+\tremove_path(stash_index_path.buf);\n+\treturn res;\n+}\n+\n+static int do_apply_stash(const char *prefix, struct stash_info *info,\n+\tint 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+\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,\n+\t\t\t&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 && restore_untracked(&info->u_tree))\n+\t\treturn error(_(\"Could not restore untracked files from stash\"));\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, 1, bases,\n+\t\t\t\t      &result);\n+\tif (ret != 0) {\n+\t\trerere(0);\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 (get_newly_staged(&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\tif (refresh_cache(REFRESH_QUIET))\n+\t\t\twarning(\"could not refresh index\");\n+\t} else {\n+\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t/*\n+\t\t * Status is quite simple and could be replaced with calls to\n+\t\t * wt_status in the future, but it adds complexities which may\n+\t\t * require more tests.\n+\t\t */\n+\t\tcp.git_cmd = 1;\n+\t\tcp.dir = prefix;\n+\t\targv_array_push(&cp.args, \"status\");\n+\t\trun_command(&cp);\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\t     git_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+\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\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n+\n+\tindex_file = get_index_file();\n+\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n+\t\t    (uintmax_t)pid);\n+\n+\tif (argc < 1)\n+\t\tusage_with_options(git_stash_helper_usage, options);\n+\tif (!strcmp(argv[0], \"apply\"))\n+\t\treturn !!apply_stash(argc, argv, prefix);\n+\n+\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n+\t\t      git_stash_helper_usage, options);\n+}\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 94793c1a9..809b1c2d1 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() {\n@@ -713,7 +648,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\ndiff --git a/git.c b/git.c\nindex fc7d15d54..a3c83f57e 100644\n--- a/git.c\n+++ b/git.c\n@@ -543,6 +543,7 @@ static struct cmd_struct commands[] = {\n \t{ \"show-index\", cmd_show_index },\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 | NO_PARSEOPT },\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354892","messageId":"ee70cc83b9b57c43672cfa5065d3c6156d089d92.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 07/26] stash: convert branch to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:46Z","receivedAt":"2018-08-08T18:59:25Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash branch to the helper and delete the apply_to_branch\nfunction from the shell script.\n\nCheckout does not currently provide a function for checking out\na branch as cmd_checkout does a large amount of sanity checks\nfirst that we require here.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 44 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 17 ++--------------\n 2 files changed, 46 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex ae719b7fc..1e4d07295 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -14,6 +14,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@@ -28,6 +29,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@@ -535,6 +541,42 @@ 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 child_process cp = CHILD_PROCESS_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\t     git_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+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"checkout\", \"-b\", NULL);\n+\targv_array_push(&cp.args, branch);\n+\targv_array_push(&cp.args, oid_to_hex(&info.b_commit));\n+\tret = run_command(&cp);\n+\tif (!ret)\n+\t\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 \tpid_t pid = getpid();\n@@ -561,6 +603,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"branch\"))\n+\t\treturn !!branch_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex a99d5dc9e..29d9f4425 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@@ -673,7 +659,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.18.0.573.g56500d98f\n\n"},{"id":"354893","messageId":"6831636969a588833f42530fda29602447085848.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 06/26] stash: convert drop and clear to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:45Z","receivedAt":"2018-08-08T18:59:26Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   4 +-\n 2 files changed, 117 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex ef6a9d30d..ae719b7fc 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,7 +12,14 @@\n #include \"rerere.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@@ -21,6 +28,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 struct strbuf stash_index_path = STRBUF_INIT;\n@@ -140,6 +152,31 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n \treturn !(ret == 0 || ret == 1);\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,\n+\t\t\t     git_stash_helper_clear_usage,\n+\t\t\t     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@@ -424,6 +461,80 @@ 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 child_process cp_reflog = CHILD_PROCESS_INIT;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tint ret;\n+\n+\t/*\n+\t * reflog does not provide a simple function for deleting refs. One will\n+\t * need to be added to avoid implementing too much reflog code here\n+\t */\n+\n+\tcp_reflog.git_cmd = 1;\n+\targv_array_pushl(&cp_reflog.args, \"reflog\", \"delete\", \"--updateref\",\n+\t\t\t \"--rewrite\", NULL);\n+\targv_array_push(&cp_reflog.args, info->revision.buf);\n+\tret = run_command(&cp_reflog);\n+\tif (!ret) {\n+\t\tif (!quiet)\n+\t\t\tprintf(_(\"Dropped %s (%s)\\n\"), info->revision.buf,\n+\t\t\t       oid_to_hex(&info->w_commit));\n+\t} else {\n+\t\treturn error(_(\"%s: Could not drop stash entry\"),\n+\t\t\t     info->revision.buf);\n+\t}\n+\n+\t/*\n+\t * This could easily be replaced by get_oid, but currently it will throw\n+\t * a fatal error when a reflog is empty, which we can not recover from.\n+\t */\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+\t/* do_clear_stash if we just dropped the last stash entry */\n+\tif (ret)\n+\t\tdo_clear_stash();\n+\n+\treturn 0;\n+}\n+\n+static void assert_stash_ref(struct stash_info *info)\n+{\n+\tif (!info->is_stash_ref) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash reference\"), info->revision.buf);\n+\t\texit(128);\n+\t}\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\t     git_stash_helper_drop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\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 \tpid_t pid = getpid();\n@@ -446,6 +557,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tusage_with_options(git_stash_helper_usage, options);\n \tif (!strcmp(argv[0], \"apply\"))\n \t\treturn !!apply_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"clear\"))\n+\t\treturn !!clear_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"drop\"))\n+\t\treturn !!drop_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 809b1c2d1..a99d5dc9e 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -653,7 +653,7 @@ apply)\n \t;;\n clear)\n \tshift\n-\tclear_stash \"$@\"\n+\tgit stash--helper clear \"$@\"\n \t;;\n create)\n \tshift\n@@ -665,7 +665,7 @@ store)\n \t;;\n drop)\n \tshift\n-\tdrop_stash \"$@\"\n+\tgit stash--helper drop \"$@\"\n \t;;\n pop)\n \tshift\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354894","messageId":"0996e18f410508602525c71e49a0d6614169b77e.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 08/26] stash: convert pop to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:47Z","receivedAt":"2018-08-08T18:59:27Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash pop to the helper and delete the pop_stash, drop_stash,\nassert_stash_ref functions from the shell script now that they\nare no longer needed.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-\n git-stash.sh            | 47 ++---------------------------------------\n 2 files changed, 37 insertions(+), 46 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 1e4d07295..d6bd468e0 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,7 +13,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 apply [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n \tNULL\n@@ -24,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@@ -541,6 +546,33 @@ 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\t     git_stash_helper_pop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\n+\tif ((ret = do_apply_stash(prefix, &info, index)))\n+\t\tprintf_ln(_(\"The stash entry is kept in case you need it again.\"));\n+\telse\n+\t\tret = do_drop_stash(prefix, &info);\n+\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@@ -603,6 +635,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"pop\"))\n+\t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n \ndiff --git a/git-stash.sh b/git-stash.sh\nindex 29d9f4425..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@@ -655,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.18.0.573.g56500d98f\n\n"},{"id":"354895","messageId":"05246e813c810156c07385052ec59d6dec34623e.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 10/26] stash: convert show to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:49Z","receivedAt":"2018-08-08T18:59:28Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash show to the helper and delete the show_stash, have_stash,\nassert_stash_like, is_stash_like and parse_flags_and_rev functions\nfrom the shell script now that they are no longer needed.\n\nBefore this commit, `git stash show` would ignore `--index` and\n`--quiet` options. Now, `git stash show` errors out on `--index`\nand does not display any message on `--quiet`, but errors out\nif the stash is not empty.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c |  78 ++++++++++++++++++++++++\n git-stash.sh            | 132 +---------------------------------------\n 2 files changed, 79 insertions(+), 131 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex daa4d0034..e764cd33e 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,6 +13,7 @@\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n+\tN_(\"git stash--helper show [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -25,6 +26,11 @@ static const char * const git_stash_helper_list_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_show_usage[] = {\n+\tN_(\"git stash--helper show [<stash>]\"),\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@@ -638,6 +644,76 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n \treturn run_command(&cp);\n }\n \n+static int show_stat = 1;\n+static int show_patch;\n+\n+static int git_stash_config(const char *var, const char *value, void *cb)\n+{\n+\tif (!strcmp(var, \"stash.showStat\")) {\n+\t\tshow_stat = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(var, \"stash.showPatch\")) {\n+\t\tshow_patch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\treturn git_default_config(var, value, cb);\n+}\n+\n+static int show_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint i, ret = 0;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tstruct argv_array args_refs = 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\t     git_stash_helper_show_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tcp.git_cmd = 1;\n+\targv_array_push(&cp.args, \"diff\");\n+\n+\t/* Push arguments which are not options into args_refs. */\n+\tfor (i = 0; i < argc; ++i) {\n+\t\tif (argv[i][0] == '-')\n+\t\t\targv_array_push(&cp.args, argv[i]);\n+\t\telse\n+\t\t\targv_array_push(&args_refs, argv[i]);\n+\t}\n+\n+\tif (get_stash_info(&info, args_refs.argc, args_refs.argv)) {\n+\t\tchild_process_clear(&cp);\n+\t\targv_array_clear(&args_refs);\n+\t\treturn -1;\n+\t}\n+\n+\t/*\n+\t * The config settings are applied only if there are not passed\n+\t * any flags.\n+\t */\n+\tif (cp.args.argc == 1) {\n+\t\tgit_config(git_stash_config, NULL);\n+\t\tif (show_stat)\n+\t\t\targv_array_push(&cp.args, \"--stat\");\n+\n+\t\tif (show_patch)\n+\t\t\targv_array_push(&cp.args, \"-p\");\n+\t}\n+\n+\targv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),\n+\t\t\t oid_to_hex(&info.w_commit), NULL);\n+\n+\tret = run_command(&cp);\n+\n+\tfree_stash_info(&info);\n+\targv_array_clear(&args_refs);\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -670,6 +746,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!branch_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"list\"))\n \t\treturn !!list_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"show\"))\n+\t\treturn !!show_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 6052441aa..0d05cbc1e 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -378,35 +378,6 @@ save_stash () {\n \tfi\n }\n \n-have_stash () {\n-\tgit rev-parse --verify --quiet $ref_stash >/dev/null\n-}\n-\n-show_stash () {\n-\tALLOW_UNKNOWN_FLAGS=t\n-\tassert_stash_like \"$@\"\n-\n-\tif test -z \"$FLAGS\"\n-\tthen\n-\t\tif test \"$(git config --bool stash.showStat || echo true)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=--stat\n-\t\tfi\n-\n-\t\tif test \"$(git config --bool stash.showPatch || echo false)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=${FLAGS}${FLAGS:+ }-p\n-\t\tfi\n-\n-\t\tif test -z \"$FLAGS\"\n-\t\tthen\n-\t\t\treturn 0\n-\t\tfi\n-\tfi\n-\n-\tgit diff ${FLAGS} $b_commit $w_commit\n-}\n-\n show_help () {\n \texec git help stash\n \texit 1\n@@ -448,107 +419,6 @@ show_help () {\n #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n #\n \n-parse_flags_and_rev()\n-{\n-\ttest \"$PARSE_CACHE\" = \"$*\" && return 0 # optimisation\n-\tPARSE_CACHE=\"$*\"\n-\n-\tIS_STASH_LIKE=\n-\tIS_STASH_REF=\n-\tINDEX_OPTION=\n-\ts=\n-\tw_commit=\n-\tb_commit=\n-\ti_commit=\n-\tu_commit=\n-\tw_tree=\n-\tb_tree=\n-\ti_tree=\n-\tu_tree=\n-\n-\tFLAGS=\n-\tREV=\n-\tfor opt\n-\tdo\n-\t\tcase \"$opt\" in\n-\t\t\t-q|--quiet)\n-\t\t\t\tGIT_QUIET=-t\n-\t\t\t;;\n-\t\t\t--index)\n-\t\t\t\tINDEX_OPTION=--index\n-\t\t\t;;\n-\t\t\t--help)\n-\t\t\t\tshow_help\n-\t\t\t;;\n-\t\t\t-*)\n-\t\t\t\ttest \"$ALLOW_UNKNOWN_FLAGS\" = t ||\n-\t\t\t\t\tdie \"$(eval_gettext \"unknown option: \\$opt\")\"\n-\t\t\t\tFLAGS=\"${FLAGS}${FLAGS:+ }$opt\"\n-\t\t\t;;\n-\t\t\t*)\n-\t\t\t\tREV=\"${REV}${REV:+ }'$opt'\"\n-\t\t\t;;\n-\t\tesac\n-\tdone\n-\n-\teval set -- $REV\n-\n-\tcase $# in\n-\t\t0)\n-\t\t\thave_stash || die \"$(gettext \"No stash entries found.\")\"\n-\t\t\tset -- ${ref_stash}@{0}\n-\t\t;;\n-\t\t1)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tdie \"$(eval_gettext \"Too many revisions specified: \\$REV\")\"\n-\t\t;;\n-\tesac\n-\n-\tcase \"$1\" in\n-\t\t*[!0-9]*)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tset -- \"${ref_stash}@{$1}\"\n-\t\t;;\n-\tesac\n-\n-\tREV=$(git rev-parse --symbolic --verify --quiet \"$1\") || {\n-\t\treference=\"$1\"\n-\t\tdie \"$(eval_gettext \"\\$reference is not a valid reference\")\"\n-\t}\n-\n-\ti_commit=$(git rev-parse --verify --quiet \"$REV^2\") &&\n-\tset -- $(git rev-parse \"$REV\" \"$REV^1\" \"$REV:\" \"$REV^1:\" \"$REV^2:\" 2>/dev/null) &&\n-\ts=$1 &&\n-\tw_commit=$1 &&\n-\tb_commit=$2 &&\n-\tw_tree=$3 &&\n-\tb_tree=$4 &&\n-\ti_tree=$5 &&\n-\tIS_STASH_LIKE=t &&\n-\ttest \"$ref_stash\" = \"$(git rev-parse --symbolic-full-name \"${REV%@*}\")\" &&\n-\tIS_STASH_REF=t\n-\n-\tu_commit=$(git rev-parse --verify --quiet \"$REV^3\") &&\n-\tu_tree=$(git rev-parse \"$REV^3:\" 2>/dev/null)\n-}\n-\n-is_stash_like()\n-{\n-\tparse_flags_and_rev \"$@\"\n-\ttest -n \"$IS_STASH_LIKE\"\n-}\n-\n-assert_stash_like() {\n-\tis_stash_like \"$@\" || {\n-\t\targs=\"$*\"\n-\t\tdie \"$(eval_gettext \"'\\$args' is not a stash-like commit\")\"\n-\t}\n-}\n-\n test \"$1\" = \"-p\" && set \"push\" \"$@\"\n \n PARSE_CACHE='--not-parsed'\n@@ -573,7 +443,7 @@ list)\n \t;;\n show)\n \tshift\n-\tshow_stash \"$@\"\n+\tgit stash--helper show \"$@\"\n \t;;\n save)\n \tshift\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354896","messageId":"5e477e5a62c3e2cc7066487bf6e0a68c922457a5.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:50Z","receivedAt":"2018-08-08T18:59:29Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"It is already stated in documentation that it will accept any\noption known to `git diff`, but not in the usage text and some\nparts of the documentation.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n Documentation/git-stash.txt | 4 ++--\n builtin/stash--helper.c     | 4 ++--\n 2 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\nindex 7ef8c4791..e31ea7d30 100644\n--- a/Documentation/git-stash.txt\n+++ b/Documentation/git-stash.txt\n@@ -9,7 +9,7 @@ SYNOPSIS\n --------\n [verse]\n 'git stash' list [<options>]\n-'git stash' show [<stash>]\n+'git stash' show [<options>] [<stash>]\n 'git stash' drop [-q|--quiet] [<stash>]\n 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n 'git stash' branch <branchname> [<stash>]\n@@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash\n The command takes options applicable to the 'git log'\n command to control what is shown and how. See linkgit:git-log[1].\n \n-show [<stash>]::\n+show [<options>] [<stash>]::\n \n \tShow the changes recorded in the stash entry as a diff between the\n \tstashed contents and the commit back when the stash entry was first\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex e764cd33e..0c1efca6b 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,7 +13,7 @@\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -27,7 +27,7 @@ static const char * const git_stash_helper_list_usage[] = {\n };\n \n static const char * const git_stash_helper_show_usage[] = {\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tNULL\n };\n \n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354897","messageId":"35e6fdfab85c21f0554da061cfab3f643d1aa794.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:51Z","receivedAt":"2018-08-08T18:59:30Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Currently, `show_stash()` uses `cmd_diff()` to generate\nthe output. After this commit, the output will be generated\nusing the internal API.\n\nBefore this commit, `git stash show --quiet` would act like\n`git diff` and error out if the stash is not empty. Now, the\n`--quiet` option does not error out given an empty stash.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------\n 1 file changed, 45 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 0c1efca6b..ec8c38c6f 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -10,6 +10,8 @@\n #include \"run-command.h\"\n #include \"dir.h\"\n #include \"rerere.h\"\n+#include \"revision.h\"\n+#include \"log-tree.h\"\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n@@ -662,56 +664,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)\n \n static int show_stash(int argc, const char **argv, const char *prefix)\n {\n-\tint i, ret = 0;\n-\tstruct child_process cp = CHILD_PROCESS_INIT;\n-\tstruct argv_array args_refs = ARGV_ARRAY_INIT;\n+\tint i;\n+\tint flags = 0;\n \tstruct stash_info info;\n+\tstruct rev_info rev;\n+\tstruct argv_array stash_args = ARGV_ARRAY_INIT;\n \tstruct option options[] = {\n \t\tOPT_END()\n \t};\n \n-\targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_show_usage,\n-\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\tinit_diff_ui_defaults();\n+\tgit_config(git_diff_ui_config, NULL);\n \n-\tcp.git_cmd = 1;\n-\targv_array_push(&cp.args, \"diff\");\n+\tinit_revisions(&rev, prefix);\n \n-\t/* Push arguments which are not options into args_refs. */\n-\tfor (i = 0; i < argc; ++i) {\n-\t\tif (argv[i][0] == '-')\n-\t\t\targv_array_push(&cp.args, argv[i]);\n+\t/* Push arguments which are not options into stash_args. */\n+\tfor (i = 1; i < argc; ++i) {\n+\t\tif (argv[i][0] != '-')\n+\t\t\targv_array_push(&stash_args, argv[i]);\n \t\telse\n-\t\t\targv_array_push(&args_refs, argv[i]);\n-\t}\n-\n-\tif (get_stash_info(&info, args_refs.argc, args_refs.argv)) {\n-\t\tchild_process_clear(&cp);\n-\t\targv_array_clear(&args_refs);\n-\t\treturn -1;\n+\t\t\tflags++;\n \t}\n \n \t/*\n \t * The config settings are applied only if there are not passed\n \t * any flags.\n \t */\n-\tif (cp.args.argc == 1) {\n+\tif (!flags) {\n \t\tgit_config(git_stash_config, NULL);\n \t\tif (show_stat)\n-\t\t\targv_array_push(&cp.args, \"--stat\");\n+\t\t\trev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;\n+\t\tif (show_patch) {\n+\t\t\trev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;\n+\t\t\trev.diffopt.output_format |= DIFF_FORMAT_PATCH;\n+\t\t}\n+\t}\n \n-\t\tif (show_patch)\n-\t\t\targv_array_push(&cp.args, \"-p\");\n+\tif (get_stash_info(&info, stash_args.argc, stash_args.argv)) {\n+\t\targv_array_clear(&stash_args);\n+\t\treturn -1;\n \t}\n \n-\targv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),\n-\t\t\t oid_to_hex(&info.w_commit), NULL);\n+\targc = setup_revisions(argc, argv, &rev, NULL);\n+\tif (!rev.diffopt.output_format)\n+\t\trev.diffopt.output_format = DIFF_FORMAT_PATCH;\n+\tdiff_setup_done(&rev.diffopt);\n+\trev.diffopt.flags.recursive = 1;\n+\tsetup_diff_pager(&rev.diffopt);\n \n-\tret = run_command(&cp);\n+\t/*\n+\t * We can return early if there was any option not recognised by\n+\t * `diff_opt_parse()`, besides the word `stash`.\n+\t */\n+\tif (argc > 1) {\n+\t\tfree_stash_info(&info);\n+\t\targv_array_clear(&stash_args);\n+\t\tusage_with_options(git_stash_helper_show_usage, options);\n+\t}\n+\n+\t/* Do the diff thing. */\n+\tdiff_tree_oid(&info.b_commit, &info.w_commit, \"\", &rev.diffopt);\n+\tlog_tree_diff_flush(&rev);\n \n \tfree_stash_info(&info);\n-\targv_array_clear(&args_refs);\n-\treturn ret;\n+\targv_array_clear(&stash_args);\n+\treturn 0;\n }\n \n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354899","messageId":"46b04e256f045a53111d52439de376a5f332cb55.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 13/26] stash: update `git stash show` documentation","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:52Z","receivedAt":"2018-08-08T18:59:31Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add in documentation about the change of behavior regarding\nthe `--quiet` option, which was introduced in the last commit.\n(the `--quiet` option does not exit anymore with erorr if it\nis given an empty stash as argument)\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n Documentation/git-stash.txt | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\nindex e31ea7d30..d60ebdb96 100644\n--- a/Documentation/git-stash.txt\n+++ b/Documentation/git-stash.txt\n@@ -117,6 +117,9 @@ show [<options>] [<stash>]::\n \tYou can use stash.showStat and/or stash.showPatch config variables\n \tto change the default behavior.\n \n+\tIt accepts any option known to `git diff`, but acts different on\n+\t`--quiet` option and exit with zero regardless of differences.\n+\n pop [--index] [-q|--quiet] [<stash>]::\n \n \tRemove a single stashed state from the stash list and apply it\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354898","messageId":"47556d40a9944e8cc45ba3df8e12c80a1898b160.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 09/26] stash: implement the \"list\" command in the builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:48Z","receivedAt":"2018-08-08T18:59:32Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash list to the helper and delete the list_stash function\nfrom the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++\n git-stash.sh            |  7 +------\n 2 files changed, 32 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex d6bd468e0..daa4d0034 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,7 @@\n #include \"rerere.h\"\n \n static const char * const git_stash_helper_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_list_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\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@@ -609,6 +615,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int list_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_list_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (!ref_exists(ref_stash))\n+\t\treturn 0;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"log\", \"--format=%gd: %gs\", \"-g\",\n+\t\t\t \"--first-parent\", \"-m\", NULL);\n+\targv_array_pushv(&cp.args, argv);\n+\targv_array_push(&cp.args, ref_stash);\n+\targv_array_push(&cp.args, \"--\");\n+\treturn run_command(&cp);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -639,6 +668,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"list\"))\n+\t\treturn !!list_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 8f2640fe9..6052441aa 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -382,11 +382,6 @@ have_stash () {\n \tgit rev-parse --verify --quiet $ref_stash >/dev/null\n }\n \n-list_stash () {\n-\thave_stash || return 0\n-\tgit log --format=\"%gd: %gs\" -g --first-parent -m \"$@\" $ref_stash --\n-}\n-\n show_stash () {\n \tALLOW_UNKNOWN_FLAGS=t\n \tassert_stash_like \"$@\"\n@@ -574,7 +569,7 @@ test -n \"$seen_non_option\" || set \"push\" \"$@\"\n case \"$1\" in\n list)\n \tshift\n-\tlist_stash \"$@\"\n+\tgit stash--helper list \"$@\"\n \t;;\n show)\n \tshift\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354900","messageId":"0683dfe7cf0528c0288b57914e7626b5c7108909.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 16/26] stash: replace spawning a \"read-tree\" process","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:55Z","receivedAt":"2018-08-08T18:59:33Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Instead of spawning a child process, make use of `reset_tree()`\nfunction already implemented in `stash-helper.c`.\n---\n builtin/stash--helper.c | 9 +++------\n 1 file changed, 3 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex a4e57899b..887b78d05 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -984,21 +984,18 @@ static int stash_patch(struct stash_info *info, const char **argv)\n static int stash_working_tree(struct stash_info *info, const char **argv)\n {\n \tint ret = 0;\n-\tstruct child_process cp0 = CHILD_PROCESS_INIT;\n \tstruct child_process cp1 = CHILD_PROCESS_INIT;\n \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n \tstruct child_process cp3 = CHILD_PROCESS_INIT;\n \tstruct strbuf out1 = STRBUF_INIT;\n \tstruct strbuf out3 = STRBUF_INIT;\n \n-\tcp0.git_cmd = 1;\n-\targv_array_push(&cp0.args, \"read-tree\");\n-\targv_array_pushf(&cp0.args, \"--index-output=%s\", stash_index_path.buf);\n-\targv_array_pushl(&cp0.args, \"-m\", oid_to_hex(&info->i_tree), NULL);\n-\tif (run_command(&cp0)) {\n+\tset_alternate_index_output(stash_index_path.buf);\n+\tif (reset_tree(&info->i_tree, 0, 0)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n+\tset_alternate_index_output(\".git/index\");\n \n \tcp1.git_cmd = 1;\n \targv_array_pushl(&cp1.args, \"diff-index\", \"--name-only\", \"-z\",\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354901","messageId":"84b0086287c683d33bd456957700199b22fd1c49.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 14/26] stash: convert store to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:53Z","receivedAt":"2018-08-08T18:59:33Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash store to the helper and delete the store_stash function\nfrom the shell script.\n\nAdd the usage string which was forgotten in the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 43 ++--------------------------------\n 2 files changed, 54 insertions(+), 41 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex ec8c38c6f..5ff810f8c 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -20,6 +20,7 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n+\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n \tNULL\n };\n \n@@ -58,6 +59,11 @@ static const char * const git_stash_helper_clear_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_store_usage[] = {\n+\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -731,6 +737,50 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n \treturn 0;\n }\n \n+static int do_store_stash(const char *w_commit, const char *stash_msg,\n+\t\t\t  int quiet)\n+{\n+\tint ret = 0;\n+\tstruct object_id obj;\n+\n+\tif (!stash_msg)\n+\t\tstash_msg  = xstrdup(\"Created via \\\"git stash--helper store\\\".\");\n+\n+\tret = get_oid(w_commit, &obj);\n+\tif (!ret) {\n+\t\tret = update_ref(stash_msg, ref_stash, &obj, NULL,\n+\t\t\t\t REF_FORCE_CREATE_REFLOG,\n+\t\t\t\t quiet ? UPDATE_REFS_QUIET_ON_ERR :\n+\t\t\t\t UPDATE_REFS_MSG_ON_ERR);\n+\t}\n+\tif (ret && !quiet)\n+\t\tfprintf_ln(stderr, _(\"Cannot update %s with %s\"),\n+\t\t\t   ref_stash, w_commit);\n+\n+\treturn ret;\n+}\n+\n+static int store_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tconst char *stash_msg = NULL;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\", N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_store_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (argc != 1) {\n+\t\tfprintf(stderr, _(\"\\\"git stash--helper store\\\" requires one <commit> argument\\n\"));\n+\t\treturn -1;\n+\t}\n+\n+\treturn do_store_stash(argv[0], stash_msg, quiet);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -765,6 +815,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!list_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"show\"))\n \t\treturn !!show_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"store\"))\n+\t\treturn !!store_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 0d05cbc1e..5739c5152 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -191,45 +191,6 @@ create_stash () {\n \tdie \"$(gettext \"Cannot record working tree state\")\"\n }\n \n-store_stash () {\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\tstash_msg=\"$1\"\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t-q|--quiet)\n-\t\t\tquiet=t\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\ttest $# = 1 ||\n-\tdie \"$(eval_gettext \"\\\"$dashless store\\\" requires one <commit> argument\")\"\n-\n-\tw_commit=\"$1\"\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tstash_msg=\"Created via \\\"git stash store\\\".\"\n-\tfi\n-\n-\tgit update-ref --create-reflog -m \"$stash_msg\" $ref_stash $w_commit\n-\tret=$?\n-\ttest $ret != 0 && test -z \"$quiet\" &&\n-\tdie \"$(eval_gettext \"Cannot update \\$ref_stash with \\$w_commit\")\"\n-\treturn $ret\n-}\n-\n push_stash () {\n \tkeep_index=\n \tpatch_mode=\n@@ -308,7 +269,7 @@ push_stash () {\n \t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n \n \tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n-\tstore_stash -m \"$stash_msg\" -q $w_commit ||\n+\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n \tdie \"$(gettext \"Cannot save the current status\")\"\n \tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n \n@@ -468,7 +429,7 @@ create)\n \t;;\n store)\n \tshift\n-\tstore_stash \"$@\"\n+\tgit stash--helper store \"$@\"\n \t;;\n drop)\n \tshift\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354903","messageId":"0393bbd09daeb7ab67da4a343ba58d824c35532b.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 15/26] stash: convert create to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:54Z","receivedAt":"2018-08-08T18:59:34Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash create to the helper.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   2 +-\n 2 files changed, 407 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 5ff810f8c..a4e57899b 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -21,6 +21,7 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n \tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+\tN_(\"git stash--helper create [<message>]\"),\n \tNULL\n };\n \n@@ -64,6 +65,11 @@ static const char * const git_stash_helper_store_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_create_usage[] = {\n+\tN_(\"git stash--helper create [<message>]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -781,6 +787,404 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n \treturn do_store_stash(argv[0], stash_msg, quiet);\n }\n \n+/*\n+ * `out` will be filled with the names of untracked files. The return value is:\n+ *\n+ * < 0 if there was a bug (any arg given outside the repo will be detected\n+ *     by `setup_revision()`)\n+ * = 0 if there are not any untracked files\n+ * > 0 if there are untracked files\n+ */\n+static int get_untracked_files(const char **argv, int line_term,\n+\t\t\t       int include_untracked, struct strbuf *out)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"ls-files\", \"-o\", NULL);\n+\tif (line_term)\n+\t\targv_array_push(&cp.args, \"-z\");\n+\tif (include_untracked != 2)\n+\t\targv_array_push(&cp.args, \"--exclude-standard\");\n+\targv_array_push(&cp.args, \"--\");\n+\tif (argv)\n+\t\targv_array_pushv(&cp.args, argv);\n+\n+\tif (pipe_command(&cp, NULL, 0, out, 0, NULL, 0))\n+\t\treturn -1;\n+\treturn out->len;\n+}\n+\n+/*\n+ * The return value of `check_changes()` can be:\n+ *\n+ * < 0 if there was an error\n+ * = 0 if there are no changes.\n+ * > 0 if there are changes.\n+ */\n+static int check_changes(const char **argv, int include_untracked,\n+\t\t\t const char *prefix)\n+{\n+\tint result;\n+\tint ret = 0;\n+\tstruct rev_info rev;\n+\tstruct object_id dummy;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\n+\tinit_revisions(&rev, prefix);\n+\tparse_pathspec(&rev.prune_data, 0, PATHSPEC_PREFER_FULL,\n+\t\t       prefix, argv);\n+\n+\trev.diffopt.flags.quick = 1;\n+\trev.diffopt.flags.ignore_submodules = 1;\n+\trev.abbrev = 0;\n+\n+\t/* No initial commit. */\n+\tif (get_oid(\"HEAD\", &dummy)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tadd_head_to_pending(&rev);\n+\tdiff_setup_done(&rev.diffopt);\n+\n+\tif (read_cache() < 0) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\tresult = run_diff_index(&rev, 1);\n+\tif (diff_result_code(&rev.diffopt, result)) {\n+\t\tret = 1;\n+\t\tgoto done;\n+\t}\n+\n+\tobject_array_clear(&rev.pending);\n+\tresult = run_diff_files(&rev, 0);\n+\tif (diff_result_code(&rev.diffopt, result)) {\n+\t\tret = 1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (include_untracked && get_untracked_files(argv, 0,\n+\t\t\t\t\t\t     include_untracked, &out))\n+\t\tret = 1;\n+\n+done:\n+\tstrbuf_release(&out);\n+\targv_array_clear(&args);\n+\treturn ret;\n+}\n+\n+static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n+\t\t\t\tstruct strbuf *out)\n+{\n+\tint ret = 0;\n+\tstruct strbuf untracked_msg = STRBUF_INIT;\n+\tstruct strbuf out2 = STRBUF_INIT;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"update-index\", \"-z\", \"--add\",\n+\t\t\t \"--remove\", \"--stdin\", NULL);\n+\targv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\n+\tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n+\tif (pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp2.git_cmd = 1;\n+\targv_array_push(&cp2.args, \"write-tree\");\n+\targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\tget_oid_hex(out2.buf, &info->u_tree);\n+\n+\tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n+\t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+done:\n+\tstrbuf_release(&untracked_msg);\n+\tstrbuf_release(&out2);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static struct strbuf patch = STRBUF_INIT;\n+\n+static int stash_patch(struct stash_info *info, const char **argv)\n+{\n+\tint ret = 0;\n+\tstruct strbuf out2 = STRBUF_INIT;\n+\tstruct child_process cp0 = CHILD_PROCESS_INIT;\n+\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n+\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n+\n+\tremove_path(stash_index_path.buf);\n+\n+\tcp0.git_cmd = 1;\n+\targv_array_pushl(&cp0.args, \"read-tree\", \"HEAD\", NULL);\n+\targv_array_pushf(&cp0.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp1.git_cmd = 1;\n+\targv_array_pushl(&cp1.args, \"add--interactive\", \"--patch=stash\",\n+\t\t\t\"--\", NULL);\n+\tif (argv)\n+\t\targv_array_pushv(&cp1.args, argv);\n+\targv_array_pushf(&cp1.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp1)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp2.git_cmd = 1;\n+\targv_array_push(&cp2.args, \"write-tree\");\n+\targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tget_oid_hex(out2.buf, &info->w_tree);\n+\n+\tcp3.git_cmd = 1;\n+\targv_array_pushl(&cp3.args, \"diff-tree\", \"-p\", \"HEAD\",\n+\t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n+\tif (pipe_command(&cp3, NULL, 0, &patch, 0, NULL, 0))\n+\t\tret = -1;\n+\n+\tif (!patch.len) {\n+\t\tfprintf_ln(stdout, \"No changes selected\");\n+\t\tret = 1;\n+\t}\n+\n+done:\n+\tstrbuf_release(&out2);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static int stash_working_tree(struct stash_info *info, const char **argv)\n+{\n+\tint ret = 0;\n+\tstruct child_process cp0 = CHILD_PROCESS_INIT;\n+\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n+\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n+\tstruct strbuf out1 = STRBUF_INIT;\n+\tstruct strbuf out3 = STRBUF_INIT;\n+\n+\tcp0.git_cmd = 1;\n+\targv_array_push(&cp0.args, \"read-tree\");\n+\targv_array_pushf(&cp0.args, \"--index-output=%s\", stash_index_path.buf);\n+\targv_array_pushl(&cp0.args, \"-m\", oid_to_hex(&info->i_tree), NULL);\n+\tif (run_command(&cp0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp1.git_cmd = 1;\n+\targv_array_pushl(&cp1.args, \"diff-index\", \"--name-only\", \"-z\",\n+\t\t\t\"HEAD\", \"--\", NULL);\n+\tif (argv)\n+\t\targv_array_pushv(&cp1.args, argv);\n+\targv_array_pushf(&cp1.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\n+\tif (pipe_command(&cp1, NULL, 0, &out1, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp2.git_cmd = 1;\n+\targv_array_pushl(&cp2.args, \"update-index\", \"-z\", \"--add\",\n+\t\t\t \"--remove\", \"--stdin\", NULL);\n+\targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\n+\tif (pipe_command(&cp2, out1.buf, out1.len, NULL, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp3.git_cmd = 1;\n+\targv_array_push(&cp3.args, \"write-tree\");\n+\targv_array_pushf(&cp3.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp3, NULL, 0, &out3, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tget_oid_hex(out3.buf, &info->w_tree);\n+\n+done:\n+\tstrbuf_release(&out1);\n+\tstrbuf_release(&out3);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static int do_create_stash(int argc, const char **argv, const char *prefix,\n+\t\t\t   const char **stash_msg, int include_untracked,\n+\t\t\t   int patch_mode, struct stash_info *info)\n+{\n+\tint untracked_commit_option = 0;\n+\tint ret = 0;\n+\tint subject_len;\n+\tint flags;\n+\tconst char *head_short_sha1 = NULL;\n+\tconst char *branch_ref = NULL;\n+\tconst char *head_subject = NULL;\n+\tconst char *branch_name = \"(no branch)\";\n+\tstruct commit *head_commit = NULL;\n+\tstruct commit_list *parents = NULL;\n+\tstruct strbuf msg = STRBUF_INIT;\n+\tstruct strbuf commit_tree_label = STRBUF_INIT;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct strbuf final_stash_msg = STRBUF_INIT;\n+\n+\tread_cache_preload(NULL);\n+\trefresh_cache(REFRESH_QUIET);\n+\n+\tif (!check_changes(argv, include_untracked, prefix)) {\n+\t\tret = 1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (get_oid(\"HEAD\", &info->b_commit)) {\n+\t\tfprintf_ln(stderr, \"You do not have the initial commit yet\");\n+\t\tret = -1;\n+\t\tgoto done;\n+\t} else {\n+\t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n+\t}\n+\n+\tbranch_ref = resolve_ref_unsafe(\"HEAD\", 0, NULL, &flags);\n+\tif (flags & REF_ISSYMREF)\n+\t\tbranch_name = strrchr(branch_ref, '/') + 1;\n+\thead_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n+\t\t\t\t\t     DEFAULT_ABBREV);\n+\tsubject_len = find_commit_subject(get_commit_buffer(head_commit, NULL),\n+\t\t\t\t\t  &head_subject);\n+\tstrbuf_addf(&msg, \"%s: %s %.*s\\n\", branch_name, head_short_sha1,\n+\t\t    subject_len, head_subject);\n+\n+\tstrbuf_addf(&commit_tree_label, \"index on %s\\n\", msg.buf);\n+\tcommit_list_insert(head_commit, &parents);\n+\tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n+\t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n+\t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n+\t\tfprintf_ln(stderr, \"Cannot save the current index state\");\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (include_untracked && get_untracked_files(argv, 1,\n+\t\t\t\t\t\t     include_untracked, &out)) {\n+\t\tif (save_untracked_files(info, &msg, &out)) {\n+\t\t\tprintf_ln(\"Cannot save the untracked files\");\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\t\tuntracked_commit_option = 1;\n+\t}\n+\tif (patch_mode) {\n+\t\tret = stash_patch(info, argv);\n+\t\tif (ret < 0) {\n+\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n+\t\t\tgoto done;\n+\t\t} else if (ret > 0) {\n+\t\t\tgoto done;\n+\t\t}\n+\t} else {\n+\t\tif (stash_working_tree(info, argv)) {\n+\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\t}\n+\n+\tif (!*stash_msg || !strlen(*stash_msg))\n+\t\tstrbuf_addf(&final_stash_msg, \"WIP on %s\", msg.buf);\n+\telse\n+\t\tstrbuf_addf(&final_stash_msg, \"On %s: %s\\n\", branch_name,\n+\t\t\t    *stash_msg);\n+\t*stash_msg = strbuf_detach(&final_stash_msg, NULL);\n+\n+\t/*\n+\t * `parents` will be empty after calling `commit_tree()`, so there is\n+\t * no need to call `free_commit_list()`\n+\t */\n+\tparents = NULL;\n+\tif (untracked_commit_option)\n+\t\tcommit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n+\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n+\tcommit_list_insert(head_commit, &parents);\n+\n+\tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n+\t\t\tparents, &info->w_commit, NULL, NULL)) {\n+\t\tprintf_ln(\"Cannot record working tree state\");\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+done:\n+\tstrbuf_release(&commit_tree_label);\n+\tstrbuf_release(&msg);\n+\tstrbuf_release(&out);\n+\tstrbuf_release(&final_stash_msg);\n+\treturn ret;\n+}\n+\n+static int create_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint include_untracked = 0;\n+\tint ret = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct stash_info info;\n+\tstruct option options[] = {\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_create_usage,\n+\t\t\t     0);\n+\n+\tret = do_create_stash(argc, argv, prefix, &stash_msg,\n+\t\t\t      include_untracked, 0, &info);\n+\n+\tif (!ret)\n+\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n+\n+\t/*\n+\t * ret can be 1 if there were no changes. In this case, we should\n+\t * not error out.\n+\t */\n+\treturn ret < 0;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -817,6 +1221,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!show_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"store\"))\n \t\treturn !!store_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"create\"))\n+\t\treturn !!create_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 5739c5152..ab06e4ffb 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -425,7 +425,7 @@ clear)\n \t;;\n create)\n \tshift\n-\tcreate_stash -m \"$*\" && echo \"$w_commit\"\n+\tgit stash--helper create --message \"$*\"\n \t;;\n store)\n \tshift\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354902","messageId":"ec5b2c9ac9e0e58710bdc385e03a01e2a7fd10bd.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 17/26] stash: avoid spawning a \"diff-index\" process","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:56Z","receivedAt":"2018-08-08T18:59:35Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commits replaces spawning `diff-index` child process by using\nthe already existing `diff` API\n---\n builtin/stash--helper.c | 56 ++++++++++++++++++++++++++++++-----------\n 1 file changed, 42 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 887b78d05..f905d3908 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,7 @@\n #include \"rerere.h\"\n #include \"revision.h\"\n #include \"log-tree.h\"\n+#include \"diffcore.h\"\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n@@ -297,6 +298,18 @@ static int reset_head(const char *prefix)\n \treturn run_command(&cp);\n }\n \n+static void add_diff_to_buf(struct diff_queue_struct *q,\n+\t\t\t    struct diff_options *options,\n+\t\t\t    void *data)\n+{\n+\tint i;\n+\tfor (i = 0; i < q->nr; i++) {\n+\t\tstruct diff_filepair *p = q->queue[i];\n+\t\tstrbuf_addstr(data, p->one->path);\n+\t\tstrbuf_addch(data, '\\n');\n+\t}\n+}\n+\n static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)\n {\n \tstruct child_process cp = CHILD_PROCESS_INIT;\n@@ -981,14 +994,16 @@ static int stash_patch(struct stash_info *info, const char **argv)\n \treturn ret;\n }\n \n-static int stash_working_tree(struct stash_info *info, const char **argv)\n+static int stash_working_tree(struct stash_info *info,\n+\t\t\t      const char **argv, const char *prefix)\n {\n \tint ret = 0;\n-\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n \tstruct child_process cp3 = CHILD_PROCESS_INIT;\n-\tstruct strbuf out1 = STRBUF_INIT;\n \tstruct strbuf out3 = STRBUF_INIT;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\tstruct strbuf diff_output = STRBUF_INIT;\n+\tstruct rev_info rev;\n \n \tset_alternate_index_output(stash_index_path.buf);\n \tif (reset_tree(&info->i_tree, 0, 0)) {\n@@ -997,26 +1012,36 @@ static int stash_working_tree(struct stash_info *info, const char **argv)\n \t}\n \tset_alternate_index_output(\".git/index\");\n \n-\tcp1.git_cmd = 1;\n-\targv_array_pushl(&cp1.args, \"diff-index\", \"--name-only\", \"-z\",\n-\t\t\t\"HEAD\", \"--\", NULL);\n+\targv_array_push(&args, \"dummy\");\n \tif (argv)\n-\t\targv_array_pushv(&cp1.args, argv);\n-\targv_array_pushf(&cp1.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n+\t\targv_array_pushv(&args, argv);\n+\tgit_config(git_diff_basic_config, NULL);\n+\tinit_revisions(&rev, prefix);\n+\targs.argc = setup_revisions(args.argc, args.argv, &rev, NULL);\n+\n+\trev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;\n+\trev.diffopt.format_callback = add_diff_to_buf;\n+\trev.diffopt.format_callback_data = &diff_output;\n+\n+\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n \n-\tif (pipe_command(&cp1, NULL, 0, &out1, 0, NULL, 0)) {\n+\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit), \"\");\n+\tif (run_diff_index(&rev, 0)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n \tcp2.git_cmd = 1;\n-\targv_array_pushl(&cp2.args, \"update-index\", \"-z\", \"--add\",\n+\targv_array_pushl(&cp2.args, \"update-index\", \"--add\",\n \t\t\t \"--remove\", \"--stdin\", NULL);\n \targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n \t\t\t stash_index_path.buf);\n \n-\tif (pipe_command(&cp2, out1.buf, out1.len, NULL, 0, NULL, 0)) {\n+\tif (pipe_command(&cp2, diff_output.buf, diff_output.len,\n+\t\t\t NULL, 0, NULL, 0)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1033,8 +1058,11 @@ static int stash_working_tree(struct stash_info *info, const char **argv)\n \tget_oid_hex(out3.buf, &info->w_tree);\n \n done:\n-\tstrbuf_release(&out1);\n+\tUNLEAK(rev);\n \tstrbuf_release(&out3);\n+\targv_array_clear(&args);\n+\tobject_array_clear(&rev.pending);\n+\tstrbuf_release(&diff_output);\n \tremove_path(stash_index_path.buf);\n \treturn ret;\n }\n@@ -1112,7 +1140,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \t\t\tgoto done;\n \t\t}\n \t} else {\n-\t\tif (stash_working_tree(info, argv)) {\n+\t\tif (stash_working_tree(info, argv, prefix)) {\n \t\t\tprintf_ln(\"Cannot save the current worktree state\");\n \t\t\tret = -1;\n \t\t\tgoto done;\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354904","messageId":"b41bff3b5188ac014f9cb5024e535d6804ef1834.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 18/26] stash: convert push to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:57Z","receivedAt":"2018-08-08T18:59:36Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash push to the helper.\n---\n builtin/stash--helper.c | 209 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   6 +-\n 2 files changed, 213 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex f905d3908..c26cad3d5 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper clear\"),\n \tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n \tN_(\"git stash--helper create [<message>]\"),\n+\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n+\t   \"          [--] [<pathspec>...]]\"),\n \tNULL\n };\n \n@@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_push_usage[] = {\n+\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n+\t   \"          [--] [<pathspec>...]]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -1210,6 +1220,203 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \treturn ret < 0;\n }\n \n+static int do_push_stash(int argc, const char **argv, const char *prefix,\n+\t\t\t int keep_index, int patch_mode, int include_untracked,\n+\t\t\t int quiet, const char *stash_msg)\n+{\n+\tint ret = 0;\n+\tstruct pathspec ps;\n+\tstruct stash_info info;\n+\tif (patch_mode && keep_index == -1)\n+\t\tkeep_index = 1;\n+\n+\tif (patch_mode && include_untracked) {\n+\t\tfprintf_ln(stderr, \"Can't use --patch and --include-untracked or --all at the same time\");\n+\t\treturn -1;\n+\t}\n+\n+\tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n+\n+\tif (read_cache() < 0)\n+\t\tdie(_(\"index file corrupt\"));\n+\n+\tif (!include_untracked && ps.nr) {\n+\t\tint i;\n+\t\tchar *ps_matched = xcalloc(ps.nr, 1);\n+\n+\t\tfor (i = 0; i < active_nr; ++i) {\n+\t\t\tconst struct cache_entry *ce = active_cache[i];\n+\t\t\tif (!ce_path_match(ce, &ps, ps_matched))\n+\t\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (report_path_error(ps_matched, &ps, prefix)) {\n+\t\t\tfprintf_ln(stderr, \"Did you forget to 'git add'?\");\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\tread_cache_preload(NULL);\n+\tif (refresh_cache(REFRESH_QUIET))\n+\t\treturn -1;\n+\n+\tif (!check_changes(argv, include_untracked, prefix)) {\n+\t\tfprintf_ln(stdout, \"No local changes to save\");\n+\t\treturn 0;\n+\t}\n+\n+\tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n+\t\tfprintf_ln(stderr, \"Cannot initialize stash\");\n+\t\treturn -1;\n+\t}\n+\n+\tif ((ret = do_create_stash(argc, argv, prefix, &stash_msg,\n+\t\t\t\t   include_untracked, patch_mode, &info)))\n+\t\treturn ret;\n+\n+\tif (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {\n+\t\tfprintf(stderr, \"Cannot save the current status\");\n+\t\treturn -1;\n+\t}\n+\n+\tfprintf(stdout, \"Saved working directory and index state %s\", stash_msg);\n+\n+\tif (!patch_mode) {\n+\t\tif (include_untracked && ps.nr == 0) {\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"clean\", \"--force\",\n+\t\t\t\t\t \"--quiet\", \"-d\", NULL);\n+\t\t\tif (include_untracked == 2)\n+\t\t\t\targv_array_push(&cp.args, \"-x\");\n+\t\t\tif (run_command(&cp))\n+\t\t\t\treturn -1;\n+\t\t}\n+\t\tif (argc != 0) {\n+\t\t\tint i;\n+\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n+\t\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\t\tcp1.git_cmd = 1;\n+\t\t\targv_array_push(&cp1.args, \"add\");\n+\t\t\tif (!include_untracked)\n+\t\t\t\targv_array_push(&cp1.args, \"-u\");\n+\t\t\tif (include_untracked == 2)\n+\t\t\t\targv_array_push(&cp1.args, \"--force\");\n+\t\t\targv_array_push(&cp1.args, \"--\");\n+\t\t\tfor (i = 0; i < ps.nr; ++i)\n+\t\t\t\targv_array_push(&cp1.args, ps.items[i].match);\n+\t\t\tif (run_command(&cp1))\n+\t\t\t\treturn -1;\n+\n+\t\t\tcp2.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp2.args, \"diff-index\", \"-p\",\n+\t\t\t\t\t \"--cached\", \"--binary\", \"HEAD\", \"--\",\n+\t\t\t\t\t NULL);\n+\t\t\tfor (i = 0; i < ps.nr; ++i)\n+\t\t\t\targv_array_push(&cp2.args, ps.items[i].match);\n+\t\t\tif (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))\n+\t\t\t\treturn -1;\n+\n+\t\t\tcp3.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp3.args, \"apply\", \"--index\", \"-R\",\n+\t\t\t\t\t NULL);\n+\t\t\tif (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,\n+\t\t\t\t\t 0))\n+\t\t\t\treturn -1;\n+\t\t} else {\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"reset\", \"--hard\", \"-q\",\n+\t\t\t\t\t NULL);\n+\t\t\tif (run_command(&cp))\n+\t\t\t\treturn -1;\n+\t\t}\n+\n+\t\tif (keep_index == 1 && !is_null_oid(&info.i_tree)) {\n+\t\t\tint i;\n+\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\t\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\t\tif (reset_tree(&info.i_tree, 0, 1))\n+\t\t\t\treturn -1;\n+\n+\t\t\tcp1.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp1.args, \"ls-files\", \"-z\",\n+\t\t\t\t\t \"--modified\", \"--\", NULL);\n+\t\t\tfor (i = 0; i < ps.nr; ++i)\n+\t\t\t\targv_array_push(&cp1.args, ps.items[i].match);\n+\t\t\tif (pipe_command(&cp1, NULL, 0, &out, 0, NULL, 0))\n+\t\t\t\treturn -1;\n+\n+\t\t\tcp2.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp2.args, \"checkout-index\", \"-z\",\n+\t\t\t\t\t \"--force\", \"--stdin\", NULL);\n+\t\t\tif (pipe_command(&cp2, out.buf, out.len, NULL, 0, NULL,\n+\t\t\t\t\t 0))\n+\t\t\t\treturn -1;\n+\t\t}\n+\t} else {\n+\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\tcp.git_cmd = 1;\n+\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n+\n+\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n+\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n+\t\t\treturn -1;\n+\t\t}\n+\n+\t\tif (keep_index < 1) {\n+\t\t\tint i;\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"reset\", \"-q\", \"--\", NULL);\n+\t\t\tfor (i = 0; i < ps.nr; ++i)\n+\t\t\t\targv_array_push(&cp.args, ps.items[i].match);\n+\t\t\tif (run_command(&cp))\n+\t\t\t\treturn -1;\n+\t\t}\n+\t}\n+\treturn 0;\n+}\n+\n+static int push_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct option options[] = {\n+\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n+\t\t\tN_(\"keep index\"), 1),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\tN_(\"stash in patch mode\")),\n+\t\tOPT_BOOL('q', \"quiet\", &quiet,\n+\t\t\tN_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_push_usage,\n+\t\t\t     0);\n+\n+\treturn do_push_stash(argc, argv, prefix, keep_index, patch_mode,\n+\t\t\t     include_untracked, quiet, stash_msg);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1248,6 +1455,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!store_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"create\"))\n \t\treturn !!create_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"push\"))\n+\t\treturn !!push_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex ab06e4ffb..c3146f62a 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -412,7 +412,8 @@ save)\n \t;;\n push)\n \tshift\n-\tpush_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper push \"$@\"\n \t;;\n apply)\n \tshift\n@@ -448,7 +449,8 @@ branch)\n *)\n \tcase $# in\n \t0)\n-\t\tpush_stash &&\n+\t\tcd \"$START_DIR\"\n+\t\tgit stash--helper push &&\n \t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n \t\t;;\n \t*)\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354905","messageId":"5ec3429340f43caad79e4213a7994738cb5d33c7.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:59Z","receivedAt":"2018-08-08T18:59:37Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commit introduces more tests for the quiet option of\n`git stash push`.\n---\n t/t3903-stash.sh | 21 +++++++++++++++++++++\n 1 file changed, 21 insertions(+)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex 8d002a7f2..b78db74ae 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -1064,6 +1064,27 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '\n \ttest_path_is_file untracked\n '\n \n+test_expect_success 'push: -q is quiet with changes' '\n+\t>foo &&\n+\tgit stash push -q >output 2>&1 &&\n+\ttest_must_be_empty output\n+'\n+\n+test_expect_success 'push: -q is quiet with no changes' '\n+\tgit stash push -q >output 2>&1 &&\n+\ttest_must_be_empty output\n+'\n+\n+test_expect_success 'push: -q is quiet even if there is no initial commit' '\n+\tgit init foo_dir &&\n+\tcd foo_dir &&\n+\ttouch bar &&\n+\ttest_must_fail git stash push -q >output 2>&1 &&\n+\ttest_must_be_empty output &&\n+\tcd .. &&\n+\trm -rf foo_dir\n+'\n+\n test_expect_success 'untracked files are left in place when -u is not given' '\n \t>file &&\n \tgit add file &&\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354906","messageId":"c778a6aa99edd1ab58cfebc0976f1475f759611c.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 19/26] stash: make push to be quiet","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:58:58Z","receivedAt":"2018-08-08T18:59:38Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"There is a change in behaviour with this commit. When there was\nno initial commit, the shell version of stash would still display\na message. This commit makes `push` to not display any message if\n`--quiet` or `-q` is specified.\n---\n builtin/stash--helper.c | 41 +++++++++++++++++++++++++++--------------\n 1 file changed, 27 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex c26cad3d5..4fd79532c 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -1079,7 +1079,7 @@ static int stash_working_tree(struct stash_info *info,\n \n static int do_create_stash(int argc, const char **argv, const char *prefix,\n \t\t\t   const char **stash_msg, int include_untracked,\n-\t\t\t   int patch_mode, struct stash_info *info)\n+\t\t\t   int patch_mode, struct stash_info *info, int quiet)\n {\n \tint untracked_commit_option = 0;\n \tint ret = 0;\n@@ -1105,7 +1105,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \t}\n \n \tif (get_oid(\"HEAD\", &info->b_commit)) {\n-\t\tfprintf_ln(stderr, \"You do not have the initial commit yet\");\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, \"You do not have the initial commit yet\");\n \t\tret = -1;\n \t\tgoto done;\n \t} else {\n@@ -1127,7 +1128,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n \t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n \t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n-\t\tfprintf_ln(stderr, \"Cannot save the current index state\");\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, \"Cannot save the current index state\");\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1135,7 +1137,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \tif (include_untracked && get_untracked_files(argv, 1,\n \t\t\t\t\t\t     include_untracked, &out)) {\n \t\tif (save_untracked_files(info, &msg, &out)) {\n-\t\t\tprintf_ln(\"Cannot save the untracked files\");\n+\t\t\tif (!quiet)\n+\t\t\t\tprintf_ln(\"Cannot save the untracked files\");\n \t\t\tret = -1;\n \t\t\tgoto done;\n \t\t}\n@@ -1144,14 +1147,16 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \tif (patch_mode) {\n \t\tret = stash_patch(info, argv);\n \t\tif (ret < 0) {\n-\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n+\t\t\tif (!quiet)\n+\t\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n \t\t\tgoto done;\n \t\t} else if (ret > 0) {\n \t\t\tgoto done;\n \t\t}\n \t} else {\n \t\tif (stash_working_tree(info, argv, prefix)) {\n-\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n+\t\t\tif (!quiet)\n+\t\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n \t\t\tret = -1;\n \t\t\tgoto done;\n \t\t}\n@@ -1176,7 +1181,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \n \tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n \t\t\tparents, &info->w_commit, NULL, NULL)) {\n-\t\tprintf_ln(\"Cannot record working tree state\");\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(\"Cannot record working tree state\");\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1208,7 +1214,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     0);\n \n \tret = do_create_stash(argc, argv, prefix, &stash_msg,\n-\t\t\t      include_untracked, 0, &info);\n+\t\t\t      include_untracked, 0, &info, 0);\n \n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n@@ -1261,25 +1267,31 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n \t\treturn -1;\n \n \tif (!check_changes(argv, include_untracked, prefix)) {\n-\t\tfprintf_ln(stdout, \"No local changes to save\");\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stdout, \"No local changes to save\");\n \t\treturn 0;\n \t}\n \n \tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n-\t\tfprintf_ln(stderr, \"Cannot initialize stash\");\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, \"Cannot initialize stash\");\n \t\treturn -1;\n \t}\n \n \tif ((ret = do_create_stash(argc, argv, prefix, &stash_msg,\n-\t\t\t\t   include_untracked, patch_mode, &info)))\n+\t\t\t\t   include_untracked, patch_mode, &info,\n+\t\t\t\t   quiet)))\n \t\treturn ret;\n \n \tif (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {\n-\t\tfprintf(stderr, \"Cannot save the current status\");\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, \"Cannot save the current status\");\n \t\treturn -1;\n \t}\n \n-\tfprintf(stdout, \"Saved working directory and index state %s\", stash_msg);\n+\tif (!quiet)\n+\t\tfprintf(stdout, \"Saved working directory and index state %s\",\n+\t\t\tstash_msg);\n \n \tif (!patch_mode) {\n \t\tif (include_untracked && ps.nr == 0) {\n@@ -1367,7 +1379,8 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n \t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n \n \t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n-\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n \t\t\treturn -1;\n \t\t}\n \n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354907","messageId":"3286e8443ed1c8f9560aa6bef01aa18ed78266df.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:59:00Z","receivedAt":"2018-08-08T18:59:39Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commit replaces spawning `git ls-files` child process with\nAPI calls to get the untracked files.\n---\n builtin/stash--helper.c | 49 +++++++++++++++++++++++++++--------------\n 1 file changed, 32 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 4fd79532c..5c27f5dcf 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -813,27 +813,42 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n /*\n  * `out` will be filled with the names of untracked files. The return value is:\n  *\n- * < 0 if there was a bug (any arg given outside the repo will be detected\n- *     by `setup_revision()`)\n  * = 0 if there are not any untracked files\n  * > 0 if there are untracked files\n  */\n-static int get_untracked_files(const char **argv, int line_term,\n+static int get_untracked_files(const char **argv, const char *prefix,\n \t\t\t       int include_untracked, struct strbuf *out)\n {\n-\tstruct child_process cp = CHILD_PROCESS_INIT;\n-\tcp.git_cmd = 1;\n-\targv_array_pushl(&cp.args, \"ls-files\", \"-o\", NULL);\n-\tif (line_term)\n-\t\targv_array_push(&cp.args, \"-z\");\n+\tint max_len;\n+\tint i;\n+\tchar *seen;\n+\tstruct dir_struct dir;\n+\tstruct pathspec pathspec;\n+\n+\tmemset(&dir, 0, sizeof(dir));\n \tif (include_untracked != 2)\n-\t\targv_array_push(&cp.args, \"--exclude-standard\");\n-\targv_array_push(&cp.args, \"--\");\n-\tif (argv)\n-\t\targv_array_pushv(&cp.args, argv);\n+\t\tsetup_standard_excludes(&dir);\n \n-\tif (pipe_command(&cp, NULL, 0, out, 0, NULL, 0))\n-\t\treturn -1;\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_FULL,\n+\t\t       prefix, argv);\n+\tseen = xcalloc(pathspec.nr, 1);\n+\n+\tmax_len = fill_directory(&dir, the_repository->index, &pathspec);\n+\tfor (i = 0; i < dir.nr; i++) {\n+\t\tstruct dir_entry *ent = dir.entries[i];\n+\t\tif (!dir_path_match(ent, &pathspec, max_len, seen)) {\n+\t\t\tfree(ent);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tstrbuf_addf(out, \"%s\\n\", ent->name);\n+\t\tfree(ent);\n+\t}\n+\n+\tfree(dir.entries);\n+\tfree(dir.ignored);\n+\tclear_directory(&dir);\n+\tfree(seen);\n \treturn out->len;\n }\n \n@@ -888,7 +903,7 @@ static int check_changes(const char **argv, int include_untracked,\n \t\tgoto done;\n \t}\n \n-\tif (include_untracked && get_untracked_files(argv, 0,\n+\tif (include_untracked && get_untracked_files(argv, prefix,\n \t\t\t\t\t\t     include_untracked, &out))\n \t\tret = 1;\n \n@@ -908,7 +923,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n \n \tcp.git_cmd = 1;\n-\targv_array_pushl(&cp.args, \"update-index\", \"-z\", \"--add\",\n+\targv_array_pushl(&cp.args, \"update-index\", \"--add\",\n \t\t\t \"--remove\", \"--stdin\", NULL);\n \targv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\",\n \t\t\t stash_index_path.buf);\n@@ -1134,7 +1149,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \t\tgoto done;\n \t}\n \n-\tif (include_untracked && get_untracked_files(argv, 1,\n+\tif (include_untracked && get_untracked_files(argv, prefix,\n \t\t\t\t\t\t     include_untracked, &out)) {\n \t\tif (save_untracked_files(info, &msg, &out)) {\n \t\t\tif (!quiet)\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354908","messageId":"d39aca1880ccf94f9f207cd09b4a5cc2c3c233e7.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 22/26] stash: convert save to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:59:01Z","receivedAt":"2018-08-08T18:59:42Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash save to the helper and delete functions which are no\nlonger needed (`show_help()`, `save_stash()`, `push_stash()`,\n`create_stash()`, `clear_stash()`, `untracked_files()` and\n`no_changes()`).\n---\n builtin/stash--helper.c |  48 +++++++\n git-stash.sh            | 311 +---------------------------------------\n 2 files changed, 50 insertions(+), 309 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 5c27f5dcf..f54a476e3 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -26,6 +26,8 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n+\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n \n@@ -81,6 +83,12 @@ static const char * const git_stash_helper_push_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_save_usage[] = {\n+\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -1445,6 +1453,44 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     include_untracked, quiet, stash_msg);\n }\n \n+static int save_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint i;\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tchar *stash_msg = NULL;\n+\tstruct strbuf alt_stash_msg = STRBUF_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n+\t\t\tN_(\"keep index\"), 1),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\tN_(\"stash in patch mode\")),\n+\t\tOPT_BOOL('q', \"quiet\", &quiet,\n+\t\t\tN_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_save_usage,\n+\t\t\t     0);\n+\n+\tfor (i = 0; i < argc; ++i)\n+\t\tstrbuf_addf(&alt_stash_msg, \"%s \", argv[i]);\n+\n+\tstash_msg = strbuf_detach(&alt_stash_msg, NULL);\n+\n+\treturn do_push_stash(0, NULL, prefix, keep_index, patch_mode,\n+\t\t\t     include_untracked, quiet, stash_msg);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1485,6 +1531,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!create_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"push\"))\n \t\treturn !!push_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"save\"))\n+\t\treturn !!save_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex c3146f62a..695f1feba 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -36,314 +36,6 @@ else\n        reset_color=\n fi\n \n-no_changes () {\n-\tgit diff-index --quiet --cached HEAD --ignore-submodules -- \"$@\" &&\n-\tgit diff-files --quiet --ignore-submodules -- \"$@\" &&\n-\t(test -z \"$untracked\" || test -z \"$(untracked_files \"$@\")\")\n-}\n-\n-untracked_files () {\n-\tif test \"$1\" = \"-z\"\n-\tthen\n-\t\tshift\n-\t\tz=-z\n-\telse\n-\t\tz=\n-\tfi\n-\texcl_opt=--exclude-standard\n-\ttest \"$untracked\" = \"all\" && excl_opt=\n-\tgit ls-files -o $z $excl_opt -- \"$@\"\n-}\n-\n-clear_stash () {\n-\tif test $# != 0\n-\tthen\n-\t\tdie \"$(gettext \"git stash clear with parameters is unimplemented\")\"\n-\tfi\n-\tif current=$(git rev-parse --verify --quiet $ref_stash)\n-\tthen\n-\t\tgit update-ref -d $ref_stash $current\n-\tfi\n-}\n-\n-create_stash () {\n-\tstash_msg=\n-\tuntracked=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\tstash_msg=${1?\"BUG: create_stash () -m requires an argument\"}\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t-u|--include-untracked)\n-\t\t\tshift\n-\t\t\tuntracked=${1?\"BUG: create_stash () -u requires an argument\"}\n-\t\t\t;;\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\tgit update-index -q --refresh\n-\tif no_changes \"$@\"\n-\tthen\n-\t\texit 0\n-\tfi\n-\n-\t# state of the base commit\n-\tif b_commit=$(git rev-parse --verify HEAD)\n-\tthen\n-\t\thead=$(git rev-list --oneline -n 1 HEAD --)\n-\telse\n-\t\tdie \"$(gettext \"You do not have the initial commit yet\")\"\n-\tfi\n-\n-\tif branch=$(git symbolic-ref -q HEAD)\n-\tthen\n-\t\tbranch=${branch#refs/heads/}\n-\telse\n-\t\tbranch='(no branch)'\n-\tfi\n-\tmsg=$(printf '%s: %s' \"$branch\" \"$head\")\n-\n-\t# state of the index\n-\ti_tree=$(git write-tree) &&\n-\ti_commit=$(printf 'index on %s\\n' \"$msg\" |\n-\t\tgit commit-tree $i_tree -p $b_commit) ||\n-\t\tdie \"$(gettext \"Cannot save the current index state\")\"\n-\n-\tif test -n \"$untracked\"\n-\tthen\n-\t\t# Untracked files are stored by themselves in a parentless commit, for\n-\t\t# ease of unpacking later.\n-\t\tu_commit=$(\n-\t\t\tuntracked_files -z \"$@\" | (\n-\t\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n-\t\t\t\texport GIT_INDEX_FILE &&\n-\t\t\t\trm -f \"$TMPindex\" &&\n-\t\t\t\tgit update-index -z --add --remove --stdin &&\n-\t\t\t\tu_tree=$(git write-tree) &&\n-\t\t\t\tprintf 'untracked files on %s\\n' \"$msg\" | git commit-tree $u_tree  &&\n-\t\t\t\trm -f \"$TMPindex\"\n-\t\t) ) || die \"$(gettext \"Cannot save the untracked files\")\"\n-\n-\t\tuntracked_commit_option=\"-p $u_commit\";\n-\telse\n-\t\tuntracked_commit_option=\n-\tfi\n-\n-\tif test -z \"$patch_mode\"\n-\tthen\n-\n-\t\t# state of the working tree\n-\t\tw_tree=$( (\n-\t\t\tgit read-tree --index-output=\"$TMPindex\" -m $i_tree &&\n-\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n-\t\t\texport GIT_INDEX_FILE &&\n-\t\t\tgit diff-index --name-only -z HEAD -- \"$@\" >\"$TMP-stagenames\" &&\n-\t\t\tgit update-index -z --add --remove --stdin <\"$TMP-stagenames\" &&\n-\t\t\tgit write-tree &&\n-\t\t\trm -f \"$TMPindex\"\n-\t\t) ) ||\n-\t\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n-\n-\telse\n-\n-\t\trm -f \"$TMP-index\" &&\n-\t\tGIT_INDEX_FILE=\"$TMP-index\" git read-tree HEAD &&\n-\n-\t\t# find out what the user wants\n-\t\tGIT_INDEX_FILE=\"$TMP-index\" \\\n-\t\t\tgit add--interactive --patch=stash -- \"$@\" &&\n-\n-\t\t# state of the working tree\n-\t\tw_tree=$(GIT_INDEX_FILE=\"$TMP-index\" git write-tree) ||\n-\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n-\n-\t\tgit diff-tree -p HEAD $w_tree -- >\"$TMP-patch\" &&\n-\t\ttest -s \"$TMP-patch\" ||\n-\t\tdie \"$(gettext \"No changes selected\")\"\n-\n-\t\trm -f \"$TMP-index\" ||\n-\t\tdie \"$(gettext \"Cannot remove temporary index (can't happen)\")\"\n-\n-\tfi\n-\n-\t# create the stash\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tstash_msg=$(printf 'WIP on %s' \"$msg\")\n-\telse\n-\t\tstash_msg=$(printf 'On %s: %s' \"$branch\" \"$stash_msg\")\n-\tfi\n-\tw_commit=$(printf '%s\\n' \"$stash_msg\" |\n-\tgit commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||\n-\tdie \"$(gettext \"Cannot record working tree state\")\"\n-}\n-\n-push_stash () {\n-\tkeep_index=\n-\tpatch_mode=\n-\tuntracked=\n-\tstash_msg=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-k|--keep-index)\n-\t\t\tkeep_index=t\n-\t\t\t;;\n-\t\t--no-keep-index)\n-\t\t\tkeep_index=n\n-\t\t\t;;\n-\t\t-p|--patch)\n-\t\t\tpatch_mode=t\n-\t\t\t# only default to keep if we don't already have an override\n-\t\t\ttest -z \"$keep_index\" && keep_index=t\n-\t\t\t;;\n-\t\t-q|--quiet)\n-\t\t\tGIT_QUIET=t\n-\t\t\t;;\n-\t\t-u|--include-untracked)\n-\t\t\tuntracked=untracked\n-\t\t\t;;\n-\t\t-a|--all)\n-\t\t\tuntracked=all\n-\t\t\t;;\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\ttest -z ${1+x} && usage\n-\t\t\tstash_msg=$1\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t--help)\n-\t\t\tshow_help\n-\t\t\t;;\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\t-*)\n-\t\t\toption=\"$1\"\n-\t\t\teval_gettextln \"error: unknown option for 'stash push': \\$option\"\n-\t\t\tusage\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\teval \"set $(git rev-parse --sq --prefix \"$prefix\" -- \"$@\")\"\n-\n-\tif test -n \"$patch_mode\" && test -n \"$untracked\"\n-\tthen\n-\t\tdie \"$(gettext \"Can't use --patch and --include-untracked or --all at the same time\")\"\n-\tfi\n-\n-\ttest -n \"$untracked\" || git ls-files --error-unmatch -- \"$@\" >/dev/null || exit 1\n-\n-\tgit update-index -q --refresh\n-\tif no_changes \"$@\"\n-\tthen\n-\t\tsay \"$(gettext \"No local changes to save\")\"\n-\t\texit 0\n-\tfi\n-\n-\tgit reflog exists $ref_stash ||\n-\t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n-\n-\tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n-\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n-\tdie \"$(gettext \"Cannot save the current status\")\"\n-\tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n-\n-\tif test -z \"$patch_mode\"\n-\tthen\n-\t\ttest \"$untracked\" = \"all\" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=\n-\t\tif test -n \"$untracked\" && test $# = 0\n-\t\tthen\n-\t\t\tgit clean --force --quiet -d $CLEAN_X_OPTION\n-\t\tfi\n-\n-\t\tif test $# != 0\n-\t\tthen\n-\t\t\ttest -z \"$untracked\" && UPDATE_OPTION=\"-u\" || UPDATE_OPTION=\n-\t\t\ttest \"$untracked\" = \"all\" && FORCE_OPTION=\"--force\" || FORCE_OPTION=\n-\t\t\tgit add $UPDATE_OPTION $FORCE_OPTION -- \"$@\"\n-\t\t\tgit diff-index -p --cached --binary HEAD -- \"$@\" |\n-\t\t\tgit apply --index -R\n-\t\telse\n-\t\t\tgit reset --hard -q\n-\t\tfi\n-\n-\t\tif test \"$keep_index\" = \"t\" && test -n \"$i_tree\"\n-\t\tthen\n-\t\t\tgit read-tree --reset $i_tree\n-\t\t\tgit ls-files -z --modified -- \"$@\" |\n-\t\t\tgit checkout-index -z --force --stdin\n-\t\tfi\n-\telse\n-\t\tgit apply -R < \"$TMP-patch\" ||\n-\t\tdie \"$(gettext \"Cannot remove worktree changes\")\"\n-\n-\t\tif test \"$keep_index\" != \"t\"\n-\t\tthen\n-\t\t\tgit reset -q -- \"$@\"\n-\t\tfi\n-\tfi\n-}\n-\n-save_stash () {\n-\tpush_options=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\t-*)\n-\t\t\t# pass all options through to push_stash\n-\t\t\tpush_options=\"$push_options $1\"\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\tstash_msg=\"$*\"\n-\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tpush_stash $push_options\n-\telse\n-\t\tpush_stash $push_options -m \"$stash_msg\"\n-\tfi\n-}\n-\n-show_help () {\n-\texec git help stash\n-\texit 1\n-}\n-\n #\n # Parses the remaining options looking for flags and\n # at most one revision defaulting to ${ref_stash}@{0}\n@@ -408,7 +100,8 @@ show)\n \t;;\n save)\n \tshift\n-\tsave_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper save \"$@\"\n \t;;\n push)\n \tshift\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354909","messageId":"9bde85eacb94db54763aab35efe287c5af7c8316.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:59:03Z","receivedAt":"2018-08-08T18:59:43Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commits introduces a optimization by avoiding calling the\nsame functions again. For example, `git stash push -u`\nwould call at some points the following functions:\n\n * `check_changes()`\n * `do_create_stash()`, which calls: `check_changes()` and\n`get_untracked_files()`\n\nNote that `check_changes()` also calls `get_untracked_files()`.\nSo, `check_changes()` is called 2 times and `get_untracked_files()`\n3 times. By checking at the beginning of the function if we already\nperformed a check, we can avoid making useless calls.\n---\n builtin/stash.c | 50 +++++++++++++++++++++++++++++++++++--------------\n 1 file changed, 36 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 0ef88408a..4d5c0d16e 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -819,13 +819,23 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n }\n \n /*\n- * `out` will be filled with the names of untracked files. The return value is:\n+ * `has_untracked_files` is:\n+ * -2 if `get_untracked_files()` hasn't been called\n+ * -1 if there were errors\n+ *  0 if there are no untracked files\n+ *  1 if there are untracked files\n+ *\n+ * `untracked_files` will be filled with the names of untracked files.\n+ * The return value is:\n  *\n  * = 0 if there are not any untracked files\n  * > 0 if there are untracked files\n  */\n+static struct strbuf untracked_files = STRBUF_INIT;\n+static int has_untracked_files = -2;\n+\n static int get_untracked_files(const char **argv, const char *prefix,\n-\t\t\t       int include_untracked, struct strbuf *out)\n+\t\t\t       int include_untracked)\n {\n \tint max_len;\n \tint i;\n@@ -833,6 +843,9 @@ static int get_untracked_files(const char **argv, const char *prefix,\n \tstruct dir_struct dir;\n \tstruct pathspec pathspec;\n \n+\tif (has_untracked_files != -2)\n+\t\treturn has_untracked_files;\n+\n \tmemset(&dir, 0, sizeof(dir));\n \tif (include_untracked != 2)\n \t\tsetup_standard_excludes(&dir);\n@@ -849,7 +862,7 @@ static int get_untracked_files(const char **argv, const char *prefix,\n \t\t\tfree(ent);\n \t\t\tcontinue;\n \t\t}\n-\t\tstrbuf_addf(out, \"%s\\n\", ent->name);\n+\t\tstrbuf_addf(&untracked_files, \"%s\\n\", ent->name);\n \t\tfree(ent);\n \t}\n \n@@ -857,16 +870,25 @@ static int get_untracked_files(const char **argv, const char *prefix,\n \tfree(dir.ignored);\n \tclear_directory(&dir);\n \tfree(seen);\n-\treturn out->len;\n+\thas_untracked_files = untracked_files.len;\n+\treturn untracked_files.len;\n }\n \n /*\n+ * `changes` is:\n+ * -2 if `check_changes()` hasn't been called\n+ * -1 if there were any errors\n+ *  0 if there are no changes\n+ *  1 if there are changes\n+ *\n  * The return value of `check_changes()` can be:\n  *\n  * < 0 if there was an error\n  * = 0 if there are no changes.\n  * > 0 if there are changes.\n  */\n+static int changes = -2;\n+\n static int check_changes(const char **argv, int include_untracked,\n \t\t\t const char *prefix)\n {\n@@ -874,9 +896,11 @@ static int check_changes(const char **argv, int include_untracked,\n \tint ret = 0;\n \tstruct rev_info rev;\n \tstruct object_id dummy;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct argv_array args = ARGV_ARRAY_INIT;\n \n+\tif (changes != -2)\n+\t\treturn changes;\n+\n \tinit_revisions(&rev, prefix);\n \tparse_pathspec(&rev.prune_data, 0, PATHSPEC_PREFER_FULL,\n \t\t       prefix, argv);\n@@ -912,17 +936,16 @@ static int check_changes(const char **argv, int include_untracked,\n \t}\n \n \tif (include_untracked && get_untracked_files(argv, prefix,\n-\t\t\t\t\t\t     include_untracked, &out))\n+\t\t\t\t\t\t     include_untracked))\n \t\tret = 1;\n \n done:\n-\tstrbuf_release(&out);\n+\tchanges = ret;\n \targv_array_clear(&args);\n \treturn ret;\n }\n \n-static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n-\t\t\t\tstruct strbuf *out)\n+static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n {\n \tint ret = 0;\n \tstruct strbuf untracked_msg = STRBUF_INIT;\n@@ -937,7 +960,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \t\t\t stash_index_path.buf);\n \n \tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n-\tif (pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0)) {\n+\tif (pipe_command(&cp, untracked_files.buf, untracked_files.len,\n+\t\t\t NULL, 0, NULL, 0)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1116,7 +1140,6 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \tstruct commit_list *parents = NULL;\n \tstruct strbuf msg = STRBUF_INIT;\n \tstruct strbuf commit_tree_label = STRBUF_INIT;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct strbuf final_stash_msg = STRBUF_INIT;\n \n \tread_cache_preload(NULL);\n@@ -1158,8 +1181,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \t}\n \n \tif (include_untracked && get_untracked_files(argv, prefix,\n-\t\t\t\t\t\t     include_untracked, &out)) {\n-\t\tif (save_untracked_files(info, &msg, &out)) {\n+\t\t\t\t\t\t     include_untracked)) {\n+\t\tif (save_untracked_files(info, &msg)) {\n \t\t\tif (!quiet)\n \t\t\t\tprintf_ln(\"Cannot save the untracked files\");\n \t\t\tret = -1;\n@@ -1213,7 +1236,6 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n done:\n \tstrbuf_release(&commit_tree_label);\n \tstrbuf_release(&msg);\n-\tstrbuf_release(&out);\n \tstrbuf_release(&final_stash_msg);\n \treturn ret;\n }\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354910","messageId":"3b4a3b766598589df30e16cddf175f039eb4e290.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:59:04Z","receivedAt":"2018-08-08T18:59:44Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commit replaces spawning `git write-tree` with API calls.\n---\n builtin/stash.c | 40 ++++++++++++----------------------------\n 1 file changed, 12 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 4d5c0d16e..46e76a34e 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -949,9 +949,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n {\n \tint ret = 0;\n \tstruct strbuf untracked_msg = STRBUF_INIT;\n-\tstruct strbuf out2 = STRBUF_INIT;\n \tstruct child_process cp = CHILD_PROCESS_INIT;\n-\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\tstruct index_state state = { NULL };\n \n \tcp.git_cmd = 1;\n \targv_array_pushl(&cp.args, \"update-index\", \"--add\",\n@@ -966,15 +965,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n \t\tgoto done;\n \t}\n \n-\tcp2.git_cmd = 1;\n-\targv_array_push(&cp2.args, \"write-tree\");\n-\targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->u_tree, &state, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-\tget_oid_hex(out2.buf, &info->u_tree);\n \n \tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n \t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n@@ -984,7 +979,6 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n \n done:\n \tstrbuf_release(&untracked_msg);\n-\tstrbuf_release(&out2);\n \tremove_path(stash_index_path.buf);\n \treturn ret;\n }\n@@ -994,11 +988,10 @@ static struct strbuf patch = STRBUF_INIT;\n static int stash_patch(struct stash_info *info, const char **argv)\n {\n \tint ret = 0;\n-\tstruct strbuf out2 = STRBUF_INIT;\n \tstruct child_process cp0 = CHILD_PROCESS_INIT;\n \tstruct child_process cp1 = CHILD_PROCESS_INIT;\n-\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n \tstruct child_process cp3 = CHILD_PROCESS_INIT;\n+\tstruct index_state state = { NULL };\n \n \tremove_path(stash_index_path.buf);\n \n@@ -1023,17 +1016,12 @@ static int stash_patch(struct stash_info *info, const char **argv)\n \t\tgoto done;\n \t}\n \n-\tcp2.git_cmd = 1;\n-\targv_array_push(&cp2.args, \"write-tree\");\n-\targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n-\tget_oid_hex(out2.buf, &info->w_tree);\n-\n \tcp3.git_cmd = 1;\n \targv_array_pushl(&cp3.args, \"diff-tree\", \"-p\", \"HEAD\",\n \t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n@@ -1046,7 +1034,6 @@ static int stash_patch(struct stash_info *info, const char **argv)\n \t}\n \n done:\n-\tstrbuf_release(&out2);\n \tremove_path(stash_index_path.buf);\n \treturn ret;\n }\n@@ -1056,11 +1043,10 @@ static int stash_working_tree(struct stash_info *info,\n {\n \tint ret = 0;\n \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n-\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n-\tstruct strbuf out3 = STRBUF_INIT;\n \tstruct argv_array args = ARGV_ARRAY_INIT;\n \tstruct strbuf diff_output = STRBUF_INIT;\n \tstruct rev_info rev;\n+\tstruct index_state state = { NULL };\n \n \tset_alternate_index_output(stash_index_path.buf);\n \tif (reset_tree(&info->i_tree, 0, 0)) {\n@@ -1103,20 +1089,18 @@ static int stash_working_tree(struct stash_info *info,\n \t\tgoto done;\n \t}\n \n-\tcp3.git_cmd = 1;\n-\targv_array_push(&cp3.args, \"write-tree\");\n-\targv_array_pushf(&cp3.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp3, NULL, 0, &out3, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n+\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n-\tget_oid_hex(out3.buf, &info->w_tree);\n+\tdiscard_cache();\n+\tread_cache();\n \n done:\n \tUNLEAK(rev);\n-\tstrbuf_release(&out3);\n \targv_array_clear(&args);\n \tobject_array_clear(&rev.pending);\n \tstrbuf_release(&diff_output);\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354911","messageId":"c659174b1be950072580ee176d59f85574cdb96c.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:59:02Z","receivedAt":"2018-08-08T18:59:45Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"The old shell script `git-stash.sh`  was removed and replaced\nentirely by `builtin/stash.c`. In order to do that, `create` and\n`push` were adapted to work without `stash.sh`. For example, before\nthis commit, `git stash create` called `git stash--helper create\n--message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\nsome of these changes wouldn't have been necessary.\n\nThis commit also removes the word `helper` since now stash is\ncalled directly and not by a shell script.\n---\n .gitignore                           |   1 -\n Makefile                             |   3 +-\n builtin.h                            |   2 +-\n builtin/{stash--helper.c => stash.c} | 132 ++++++++++++-----------\n git-stash.sh                         | 153 ---------------------------\n git.c                                |   2 +-\n 6 files changed, 74 insertions(+), 219 deletions(-)\n rename builtin/{stash--helper.c => stash.c} (91%)\n delete mode 100755 git-stash.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex 3abbba81a..3284a1e9b 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -156,7 +156,6 @@\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 af82bc7ee..c41e31ad8 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -612,7 +612,6 @@ SCRIPT_SH += git-quiltimport.sh\n SCRIPT_SH += git-rebase.sh\n SCRIPT_SH += git-remote-testgit.sh\n SCRIPT_SH += git-request-pull.sh\n-SCRIPT_SH += git-stash.sh\n SCRIPT_SH += git-submodule.sh\n SCRIPT_SH += git-web--browse.sh\n \n@@ -1083,7 +1082,7 @@ BUILTIN_OBJS += builtin/shortlog.o\n BUILTIN_OBJS += builtin/show-branch.o\n BUILTIN_OBJS += builtin/show-index.o\n BUILTIN_OBJS += builtin/show-ref.o\n-BUILTIN_OBJS += builtin/stash--helper.o\n+BUILTIN_OBJS += builtin/stash.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 55ceb1574..e8102d825 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -222,7 +222,7 @@ 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_show_index(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_stash(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.c\nsimilarity index 91%\nrename from builtin/stash--helper.c\nrename to builtin/stash.c\nindex f54a476e3..0ef88408a 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash.c\n@@ -14,77 +14,77 @@\n #include \"log-tree.h\"\n #include \"diffcore.h\"\n \n-static const char * const git_stash_helper_usage[] = {\n-\tN_(\"git stash--helper list [<options>]\"),\n-\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n-\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n-\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n-\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n-\tN_(\"git stash--helper clear\"),\n-\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n-\tN_(\"git stash--helper create [<message>]\"),\n-\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_usage[] = {\n+\tN_(\"git stash list [<options>]\"),\n+\tN_(\"git stash show [<options>] [<stash>]\"),\n+\tN_(\"git stash drop [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash branch <branchname> [<stash>]\"),\n+\tN_(\"git stash clear\"),\n+\tN_(\"git stash store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+\tN_(\"git stash create [<message>]\"),\n+\tN_(\"git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n-\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\tN_(\"git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_list_usage[] = {\n-\tN_(\"git stash--helper list [<options>]\"),\n+static const char * const git_stash_list_usage[] = {\n+\tN_(\"git stash list [<options>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_show_usage[] = {\n-\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n+static const char * const git_stash_show_usage[] = {\n+\tN_(\"git stash show [<options>] [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_drop_usage[] = {\n-\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n+static const char * const git_stash_drop_usage[] = {\n+\tN_(\"git stash drop [-q|--quiet] [<stash>]\"),\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+static const char * const git_stash_pop_usage[] = {\n+\tN_(\"git stash 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+static const char * const git_stash_apply_usage[] = {\n+\tN_(\"git stash apply [--index] [-q|--quiet] [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_branch_usage[] = {\n-\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n+static const char * const git_stash_branch_usage[] = {\n+\tN_(\"git stash branch <branchname> [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_clear_usage[] = {\n-\tN_(\"git stash--helper clear\"),\n+static const char * const git_stash_clear_usage[] = {\n+\tN_(\"git stash clear\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_store_usage[] = {\n-\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+static const char * const git_stash_store_usage[] = {\n+\tN_(\"git stash store [-m|--message <message>] [-q|--quiet] <commit>\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_create_usage[] = {\n-\tN_(\"git stash--helper create [<message>]\"),\n+static const char * const git_stash_create_usage[] = {\n+\tN_(\"git stash create [<message>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_push_usage[] = {\n-\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_push_usage[] = {\n+\tN_(\"git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_save_usage[] = {\n-\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_save_usage[] = {\n+\tN_(\"git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n@@ -224,11 +224,11 @@ static int clear_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_clear_usage,\n+\t\t\t     git_stash_clear_usage,\n \t\t\t     PARSE_OPT_STOP_AT_NON_OPTION);\n \n \tif (argc != 0)\n-\t\treturn error(_(\"git stash--helper clear with parameters is unimplemented\"));\n+\t\treturn error(_(\"git stash clear with parameters is unimplemented\"));\n \n \treturn do_clear_stash();\n }\n@@ -519,7 +519,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_apply_usage, 0);\n+\t\t\t     git_stash_apply_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -591,7 +591,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_drop_usage, 0);\n+\t\t\t     git_stash_drop_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -615,7 +615,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_pop_usage, 0);\n+\t\t\t     git_stash_pop_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -641,7 +641,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_branch_usage, 0);\n+\t\t\t     git_stash_branch_usage, 0);\n \n \tif (argc == 0)\n \t\treturn error(_(\"No branch name specified\"));\n@@ -674,7 +674,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_list_usage,\n+\t\t\t     git_stash_list_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n \n \tif (!ref_exists(ref_stash))\n@@ -762,7 +762,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n \tif (argc > 1) {\n \t\tfree_stash_info(&info);\n \t\targv_array_clear(&stash_args);\n-\t\tusage_with_options(git_stash_helper_show_usage, options);\n+\t\tusage_with_options(git_stash_show_usage, options);\n \t}\n \n \t/* Do the diff thing. */\n@@ -781,7 +781,7 @@ static int do_store_stash(const char *w_commit, const char *stash_msg,\n \tstruct object_id obj;\n \n \tif (!stash_msg)\n-\t\tstash_msg  = xstrdup(\"Created via \\\"git stash--helper store\\\".\");\n+\t\tstash_msg  = xstrdup(\"Created via \\\"git stash store\\\".\");\n \n \tret = get_oid(w_commit, &obj);\n \tif (!ret) {\n@@ -807,11 +807,11 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_store_usage,\n+\t\t\t     git_stash_store_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n \n \tif (argc != 1) {\n-\t\tfprintf(stderr, _(\"\\\"git stash--helper store\\\" requires one <commit> argument\\n\"));\n+\t\tfprintf(stderr, _(\"\\\"git stash store\\\" requires one <commit> argument\\n\"));\n \t\treturn -1;\n \t}\n \n@@ -1220,23 +1220,26 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n \n static int create_stash(int argc, const char **argv, const char *prefix)\n {\n+\tint i;\n \tint include_untracked = 0;\n \tint ret = 0;\n-\tconst char *stash_msg = NULL;\n \tstruct stash_info info;\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n+\tconst char *stash_msg;\n \tstruct option options[] = {\n-\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n-\t\t\t N_(\"include untracked files in stash\")),\n-\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n-\t\t\t N_(\"stash message\")),\n \t\tOPT_END()\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_create_usage,\n+\t\t\t     git_stash_create_usage,\n \t\t\t     0);\n \n-\tret = do_create_stash(argc, argv, prefix, &stash_msg,\n+\tfor (i = 0; i < argc; ++i)\n+\t\tstrbuf_addf(&stash_msg_buf, \"%s \", argv[i]);\n+\n+\tstash_msg = strbuf_detach(&stash_msg_buf, NULL);\n+\n+\tret = do_create_stash(0, NULL, prefix, &stash_msg,\n \t\t\t      include_untracked, 0, &info, 0);\n \n \tif (!ret)\n@@ -1445,9 +1448,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_push_usage,\n-\t\t\t     0);\n+\tif (argc)\n+\t\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t\t     git_stash_push_usage,\n+\t\t\t\t     0);\n \n \treturn do_push_stash(argc, argv, prefix, keep_index, patch_mode,\n \t\t\t     include_untracked, quiet, stash_msg);\n@@ -1479,7 +1483,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_save_usage,\n+\t\t\t     git_stash_save_usage,\n \t\t\t     0);\n \n \tfor (i = 0; i < argc; ++i)\n@@ -1491,7 +1495,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     include_untracked, quiet, stash_msg);\n }\n \n-int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n+int cmd_stash(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n \tconst char *index_file;\n@@ -1502,16 +1506,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \n \tgit_config(git_default_config, NULL);\n \n-\targc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,\n+\targc = parse_options(argc, argv, prefix, options, git_stash_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n \n \tindex_file = get_index_file();\n \tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n \t\t    (uintmax_t)pid);\n \n-\tif (argc < 1)\n-\t\tusage_with_options(git_stash_helper_usage, options);\n-\tif (!strcmp(argv[0], \"apply\"))\n+\tif (argc == 0)\n+\t\treturn !!push_stash(0, NULL, prefix);\n+\telse if (!strcmp(argv[0], \"apply\"))\n \t\treturn !!apply_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"clear\"))\n \t\treturn !!clear_stash(argc, argv, prefix);\n@@ -1533,7 +1537,13 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!push_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"save\"))\n \t\treturn !!save_stash(argc, argv, prefix);\n+\tif (*argv[0] == '-') {\n+\t\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\t\targv_array_push(&args, \"push\");\n+\t\targv_array_pushv(&args, argv);\n+\t\treturn !!push_stash(args.argc, args.argv, prefix);\n+\t}\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n-\t\t      git_stash_helper_usage, options);\n+\t\t      git_stash_usage, options);\n }\ndiff --git a/git-stash.sh b/git-stash.sh\ndeleted file mode 100755\nindex 695f1feba..000000000\n--- a/git-stash.sh\n+++ /dev/null\n@@ -1,153 +0,0 @@\n-#!/bin/sh\n-# Copyright (c) 2007, Nanako Shiraishi\n-\n-dashless=$(basename \"$0\" | sed -e 's/-/ /')\n-USAGE=\"list [<options>]\n-   or: $dashless show [<stash>]\n-   or: $dashless drop [-q|--quiet] [<stash>]\n-   or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n-   or: $dashless branch <branchname> [<stash>]\n-   or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n-\t\t      [-u|--include-untracked] [-a|--all] [<message>]\n-   or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n-\t\t       [-u|--include-untracked] [-a|--all] [-m <message>]\n-\t\t       [-- <pathspec>...]]\n-   or: $dashless clear\"\n-\n-SUBDIRECTORY_OK=Yes\n-OPTIONS_SPEC=\n-START_DIR=$(pwd)\n-. git-sh-setup\n-require_work_tree\n-prefix=$(git rev-parse --show-prefix) || exit 1\n-cd_to_toplevel\n-\n-TMP=\"$GIT_DIR/.git-stash.$$\"\n-TMPindex=${GIT_INDEX_FILE-\"$(git rev-parse --git-path index)\"}.stash.$$\n-trap 'rm -f \"$TMP-\"* \"$TMPindex\"' 0\n-\n-ref_stash=refs/stash\n-\n-if git config --get-colorbool color.interactive; then\n-       help_color=\"$(git config --get-color color.interactive.help 'red bold')\"\n-       reset_color=\"$(git config --get-color '' reset)\"\n-else\n-       help_color=\n-       reset_color=\n-fi\n-\n-#\n-# Parses the remaining options looking for flags and\n-# at most one revision defaulting to ${ref_stash}@{0}\n-# if none found.\n-#\n-# Derives related tree and commit objects from the\n-# revision, if one is found.\n-#\n-# stash records the work tree, and is a merge between the\n-# base commit (first parent) and the index tree (second parent).\n-#\n-#   REV is set to the symbolic version of the specified stash-like commit\n-#   IS_STASH_LIKE is non-blank if ${REV} looks like a stash\n-#   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref\n-#   s is set to the SHA1 of the stash commit\n-#   w_commit is set to the commit containing the working tree\n-#   b_commit is set to the base commit\n-#   i_commit is set to the commit containing the index tree\n-#   u_commit is set to the commit containing the untracked files tree\n-#   w_tree is set to the working tree\n-#   b_tree is set to the base tree\n-#   i_tree is set to the index tree\n-#   u_tree is set to the untracked files tree\n-#\n-#   GIT_QUIET is set to t if -q is specified\n-#   INDEX_OPTION is set to --index if --index is specified.\n-#   FLAGS is set to the remaining flags (if allowed)\n-#\n-# dies if:\n-#   * too many revisions specified\n-#   * no revision is specified and there is no stash stack\n-#   * a revision is specified which cannot be resolve to a SHA1\n-#   * a non-existent stash reference is specified\n-#   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n-#\n-\n-test \"$1\" = \"-p\" && set \"push\" \"$@\"\n-\n-PARSE_CACHE='--not-parsed'\n-# The default command is \"push\" if nothing but options are given\n-seen_non_option=\n-for opt\n-do\n-\tcase \"$opt\" in\n-\t--) break ;;\n-\t-*) ;;\n-\t*) seen_non_option=t; break ;;\n-\tesac\n-done\n-\n-test -n \"$seen_non_option\" || set \"push\" \"$@\"\n-\n-# Main command set\n-case \"$1\" in\n-list)\n-\tshift\n-\tgit stash--helper list \"$@\"\n-\t;;\n-show)\n-\tshift\n-\tgit stash--helper show \"$@\"\n-\t;;\n-save)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper save \"$@\"\n-\t;;\n-push)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper push \"$@\"\n-\t;;\n-apply)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper apply \"$@\"\n-\t;;\n-clear)\n-\tshift\n-\tgit stash--helper clear \"$@\"\n-\t;;\n-create)\n-\tshift\n-\tgit stash--helper create --message \"$*\"\n-\t;;\n-store)\n-\tshift\n-\tgit stash--helper store \"$@\"\n-\t;;\n-drop)\n-\tshift\n-\tgit stash--helper drop \"$@\"\n-\t;;\n-pop)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper pop \"$@\"\n-\t;;\n-branch)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper branch \"$@\"\n-\t;;\n-*)\n-\tcase $# in\n-\t0)\n-\t\tcd \"$START_DIR\"\n-\t\tgit stash--helper push &&\n-\t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n-\t\t;;\n-\t*)\n-\t\tusage\n-\tesac\n-\t;;\n-esac\ndiff --git a/git.c b/git.c\nindex a3c83f57e..add13c823 100644\n--- a/git.c\n+++ b/git.c\n@@ -543,7 +543,7 @@ static struct cmd_struct commands[] = {\n \t{ \"show-index\", cmd_show_index },\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{ \"stash\", cmd_stash, 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 | NO_PARSEOPT },\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354912","messageId":"56500d98f9d5daaa5f21a43767885baede86e3a0.1533753605.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v7 26/26] stash: replace all \"git apply\" child processes with API calls","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-08T18:59:05Z","receivedAt":"2018-08-08T18:59:47Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"`apply_all_patches()` does not provide a method to apply patches\nfrom strbuf. Because of this, this commit introduces a new\nfunction `apply_patch_from_buf()` which applies a patch from buf.\nIt works by saving the strbuf as a file. This way we can call\n`apply_all_patches()`. Before returning, the created file is\nremoved.\n---\n builtin/stash.c | 61 +++++++++++++++++++++++++++----------------------\n 1 file changed, 34 insertions(+), 27 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 46e76a34e..74eda822c 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -13,6 +13,7 @@\n #include \"revision.h\"\n #include \"log-tree.h\"\n #include \"diffcore.h\"\n+#include \"apply.h\"\n \n static const char * const git_stash_usage[] = {\n \tN_(\"git stash list [<options>]\"),\n@@ -277,10 +278,6 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)\n \tstruct child_process cp = CHILD_PROCESS_INIT;\n \tconst char *w_commit_hex = oid_to_hex(w_commit);\n \n-\t/*\n-\t * Diff-tree would not be very hard to replace with a native function,\n-\t * however it should be done together with apply_cached.\n-\t */\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@@ -288,18 +285,36 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)\n \treturn pipe_command(&cp, NULL, 0, out, 0, NULL, 0);\n }\n \n-static int apply_cached(struct strbuf *out)\n+static int apply_patch_from_buf(struct strbuf *patch, int cached, int reverse,\n+\t\t\t\tint check_index)\n {\n-\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tint ret = 0;\n+\tstruct apply_state state;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n+\tconst char *patch_path = \".git/stash_patch.patch\";\n+\tFILE *patch_file;\n \n-\t/*\n-\t * Apply currently only reads either from stdin or a file, thus\n-\t * apply_all_patches would have to be updated to optionally take a\n-\t * buffer.\n-\t */\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+\tif (init_apply_state(&state, NULL))\n+\t\treturn -1;\n+\n+\tstate.cached = cached;\n+\tstate.apply_in_reverse = reverse;\n+\tstate.check_index = check_index;\n+\tif (state.cached)\n+\t\tstate.check_index = 1;\n+\tif (state.check_index)\n+\t\tstate.unsafe_paths = 0;\n+\n+\tpatch_file = fopen(patch_path, \"w\");\n+\tstrbuf_write(patch, patch_file);\n+\tfclose(patch_file);\n+\n+\targv_array_push(&args, patch_path);\n+\tret = apply_all_patches(&state, args.argc, args.argv, 0);\n+\n+\tremove_path(patch_path);\n+\tclear_apply_state(&state);\n+\treturn ret;\n }\n \n static int reset_head(const char *prefix)\n@@ -418,7 +433,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,\n \t\t\t\treturn -1;\n \t\t\t}\n \n-\t\t\tret = apply_cached(&out);\n+\t\t\tret = apply_patch_from_buf(&out, 1, 0, 0);\n \t\t\tstrbuf_release(&out);\n \t\t\tif (ret)\n \t\t\t\treturn -1;\n@@ -1341,7 +1356,6 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n \t\t\tint i;\n \t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n \t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n-\t\t\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n \t\t\tstruct strbuf out = STRBUF_INIT;\n \n \t\t\tcp1.git_cmd = 1;\n@@ -1365,11 +1379,9 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n \t\t\tif (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))\n \t\t\t\treturn -1;\n \n-\t\t\tcp3.git_cmd = 1;\n-\t\t\targv_array_pushl(&cp3.args, \"apply\", \"--index\", \"-R\",\n-\t\t\t\t\t NULL);\n-\t\t\tif (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,\n-\t\t\t\t\t 0))\n+\t\t\tdiscard_cache();\n+\t\t\tread_cache();\n+\t\t\tif (apply_patch_from_buf(&out, 0, 1, 1))\n \t\t\t\treturn -1;\n \t\t} else {\n \t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n@@ -1405,12 +1417,7 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n \t\t\t\treturn -1;\n \t\t}\n \t} else {\n-\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n-\n-\t\tcp.git_cmd = 1;\n-\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n-\n-\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n+\t\tif (apply_patch_from_buf(&patch, 0, 1, 0)) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n \t\t\treturn -1;\n-- \n2.18.0.573.g56500d98f\n\n"},{"id":"354925","messageId":"xmqqa7pwfw84.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"7ba9a8515d445d10af36a8a79071af51b90f5aef.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-08-08T20:18:35Z","receivedAt":"2018-08-08T20:18:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n\n> From: Joel Teichroeb <joel@teichroeb.net>\n>\n> Add a builtin helper for performing stash commands. Converting\n> all at once proved hard to review, so starting with just apply\n> lets conversion get started without the other commands 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> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n\nGood to see that the right way to forward a patch from another\nperson is used, but is this a GSoC project?\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> new file mode 100644\n> index 000000000..ef6a9d30d\n> --- /dev/null\n> +++ b/builtin/stash--helper.c\n> @@ -0,0 +1,452 @@\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> +#include \"rerere.h\"\n\nWow, \"apply\" is a biggie, as you'd pretty much have to do\neverything, like merging and updating the index and asking rerere to\nauto-resolve.  Quite a many include files.\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 void assert_stash_like(struct stash_info *info, const char * revision)\n\nThis inherits an unfortunate name from the scripted version (it does\nmore than asserting), but it is OK to keep the original name,\nespecially in this early step in the series.\n\nLose the SP in \"* revision\"; the asterisk sticks to the variable/parameter name.\n\n> +{\n> +\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n> +\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n> +\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n> +\t    get_oidf(&info->i_tree, \"%s^2:\", revision)) {\n> +\t\tfree_stash_info(info);\n> +\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n> +\t\texit(128);\n> +\t}\n> +}\n\n> +static int reset_tree(struct object_id *i_tree, int update, int reset)\n> +{\n> ...\n> +}\n\nKind-a surprising that there is no helper function to do this\nalready.  The implementation looked OK, though.\n\n> +static int apply_cached(struct strbuf *out)\n> +{\n> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t/*\n> +\t * Apply currently only reads either from stdin or a file, thus\n> +\t * apply_all_patches would have to be updated to optionally take a\n> +\t * buffer.\n> +\t */\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\nApplying and updating the index is more resource intensive than\nspawning a process, and not having to worry about the process dying\nis a benefit, so overall, making this into an internal call would be\na lot lower priority, I would guess.\n\n> +static int reset_head(const char *prefix)\n> +{\n\nThis is resetting the index to the HEAD, right?  reset_head sounds\nas if it takes a commit-ish and moves HEAD there.\n\n"},{"id":"355033","messageId":"1ba17df0-e6da-3358-622b-c19092c20eb0@gmail.com","threadId":"48222","inReplyTo":"xmqqa7pwfw84.fsf@gitster-ct.c.googlers.com","subject":"Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-09T20:01:09Z","receivedAt":"2018-08-09T20:01:15Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nOn 08.08.2018 23:18, Junio C Hamano wrote:\n> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n> \n>> From: Joel Teichroeb <joel@teichroeb.net>\n>>\n>> Add a builtin helper for performing stash commands. Converting\n>> all at once proved hard to review, so starting with just apply\n>> lets conversion get started without the other commands 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>> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> \n> Good to see that the right way to forward a patch from another\n> person is used, but is this a GSoC project?\n\nYes, it is. I forgot to add the [GSoC] tag in the last series of patches.\n\n>> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n>> new file mode 100644\n>> index 000000000..ef6a9d30d\n>> --- /dev/null\n>> +++ b/builtin/stash--helper.c\n>> @@ -0,0 +1,452 @@\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>> +#include \"rerere.h\"\n> \n> Wow, \"apply\" is a biggie, as you'd pretty much have to do\n> everything, like merging and updating the index and asking rerere to\n> auto-resolve.  Quite a many include files.\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 void assert_stash_like(struct stash_info *info, const char * revision)\n> \n> This inherits an unfortunate name from the scripted version (it does\n> more than asserting), but it is OK to keep the original name,\n> especially in this early step in the series.\n> \n> Lose the SP in \"* revision\"; the asterisk sticks to the variable/parameter name.\n\nWill do so.\n\n>> +{\n>> +\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n>> +\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n>> +\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n>> +\t    get_oidf(&info->i_tree, \"%s^2:\", revision)) {\n>> +\t\tfree_stash_info(info);\n>> +\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n>> +\t\texit(128);\n>> +\t}\n>> +}\n> \n>> +static int reset_tree(struct object_id *i_tree, int update, int reset)\n>> +{\n>> ...\n>> +}\n> \n> Kind-a surprising that there is no helper function to do this\n> already.  The implementation looked OK, though.\n> \n>> +static int apply_cached(struct strbuf *out)\n>> +{\n>> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n>> +\n>> +\t/*\n>> +\t * Apply currently only reads either from stdin or a file, thus\n>> +\t * apply_all_patches would have to be updated to optionally take a\n>> +\t * buffer.\n>> +\t */\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> Applying and updating the index is more resource intensive than\n> spawning a process, and not having to worry about the process dying\n> is a benefit, so overall, making this into an internal call would be\n> a lot lower priority, I would guess.\n\nIndeed. In the last but one patch [1], I tried to convert all of the \n\"apply\" processes. At the moment, `apply_all_patches()` cannot take a \nbuffer. A solution for this was to write the buffer into a file and pass \nthe name of that file to the function. Of course, this might not be a \nbright idea and for this reason I am not sure if that patch is worth.\n\n[1]\nhttps://public-inbox.org/git/56500d98f9d5daaa5f21a43767885baede86e3a0.1533753605.git.ungureanupaulsebastian@gmail.com/\n\n>> +static int reset_head(const char *prefix)\n>> +{\n> \n> This is resetting the index to the HEAD, right?  reset_head sounds\n> as if it takes a commit-ish and moves HEAD there.\n> \n\nYes, it resets the index to the HEAD.\n\nBest,\nPaul\n"},{"id":"355036","messageId":"xmqqftznb6hc.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"1ba17df0-e6da-3358-622b-c19092c20eb0@gmail.com","subject":"Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-08-09T21:00:31Z","receivedAt":"2018-08-09T21:00:35Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n\n>> Good to see that the right way to forward a patch from another\n>> person is used, but is this a GSoC project?\n>\n> Yes, it is. I forgot to add the [GSoC] tag in the last series of patches.\n\nThe reason I asked was because IIRC GSoC was not supposed to be team\neffort but \"summer job\" by individual students.\n"},{"id":"355080","messageId":"60b38370-b8ac-fe13-abf7-db3166a04dee@gmail.com","threadId":"48222","inReplyTo":"xmqqftznb6hc.fsf@gitster-ct.c.googlers.com","subject":"Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-10T15:35:47Z","receivedAt":"2018-08-10T15:35:55Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hellom\n\nOn 10.08.2018 00:00, Junio C Hamano wrote:\n> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n> \n>>> Good to see that the right way to forward a patch from another\n>>> person is used, but is this a GSoC project?\n>>\n>> Yes, it is. I forgot to add the [GSoC] tag in the last series of patches.\n> \n> The reason I asked was because IIRC GSoC was not supposed to be team\n> effort but \"summer job\" by individual students.\n> \n\nBefore starting working on this, I discussed with Joel and ask\nhim whether I could take custody of this patches. He was open\nto the idea, so I continued to build on top of them. Since then,\nI have made some changes to his code and also added a lot more of\nmy own.\n\nWhat I did was to completely convert git stash and apply some\nmore optimizations on top of that, like reducing the number of\nspawned processes. Due to this kind of improvements I was able\nto significantly reduce the execution time and this has visible\neffects, running about 3 times faster.\n\nPlease do not get me wrong, I do not want to bash Joel or his work.\nI just wanted to make it clear that I did not copy his work or made\nit look like it was mine. After Joel agreed to hand the patches off\nto me [1], every line of code that I wrote was written by myself, \nwithout his (or anyone else) assistance. I really did my best to finish \nthis project and I believe that my mentor, dscho, can also confirm this. \nI am really sorry for the confusion.\n\n[1]\nhttps://public-inbox.org/git/CA+CzEk_qwHs5qUstyFeepzwvCBR=9SvH90+__f-gfxFySETZzQ@mail.gmail.com/\n\nBest regards,\nPaul Ungureanu\n"},{"id":"355748","messageId":"20180815193133.GJ2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"cb70dc2317b8bdbe640a50de7050cb405067c0f0.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T19:31:33Z","receivedAt":"2018-08-15T19:31:39Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"> Subject: Re: [GSoC][PATCH v7 04/26] stash: renamed test cases to be more descriptive\n\nPlease use the imperative mood in the title and the commit messages\nthemselves.  From Documentation/SubmittingPatches:\n\n    Describe your changes in imperative mood, e.g. \"make xyzzy do frotz\"\n    instead of \"[This patch] makes xyzzy do frotz\" or \"[I] changed xyzzy\n    to do frotz\", as if you are giving orders to the codebase to change\n    its behavior.\n\nFrom a quick skim over the rest of the series, this also applies to\nsome of the subsequent patches in the series. \n\nOn 08/08, Paul-Sebastian Ungureanu wrote:\n> Renamed some test cases' labels to be more descriptive and under 80\n> characters per line.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  t/t3903-stash.sh | 14 +++++++-------\n>  1 file changed, 7 insertions(+), 7 deletions(-)\n> \n> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\n> index de6cab1fe..8d002a7f2 100755\n> --- a/t/t3903-stash.sh\n> +++ b/t/t3903-stash.sh\n> @@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n>  \ttest_cmp expected actual\n>  '\n>  \n> -test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '\n> +test_expect_success 'drop: fail early if specified stash is not a stash ref' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n>  \tgit reset --hard &&\n> @@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n>  \tgit reset --hard HEAD\n>  '\n>  \n> -test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '\n> +test_expect_success 'pop: fail early if specified stash is not a stash ref' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n>  \tgit reset --hard &&\n> @@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form \"n\", n >= N' '\n>  \tgit stash drop\n>  '\n>  \n> -test_expect_success 'stash branch should not drop the stash if the branch exists' '\n> +test_expect_success 'branch: should not drop the stash if the branch exists' '\n\nSince we're adjusting the titles of the tests here I'll allow myself\nto nitpick a little :)\n\nMaybe \"branch: do not drop the stash if the branch exists\", which\nsounds more like an assertion, as the \"pop\" and \"drop\" titles above.\n\n>  \tgit stash clear &&\n>  \techo foo >file &&\n>  \tgit add file &&\n> @@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists\n>  \tgit rev-parse stash@{0} --\n>  '\n>  \n> -test_expect_success 'stash branch should not drop the stash if the apply fails' '\n> +test_expect_success 'branch: should not drop the stash if the apply fails' '\n>  \tgit stash clear &&\n>  \tgit reset HEAD~1 --hard &&\n>  \techo foo >file &&\n> @@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'\n>  \tgit rev-parse stash@{0} --\n>  '\n>  \n> -test_expect_success 'stash apply shows status same as git status (relative to current directory)' '\n> +test_expect_success 'apply: shows same status as git status (relative to ./)' '\n\ns/shows/show/ above maybe?  This used to be a full sentence\npreviously, where 'shows' was appropriate, but I think \"show\" sounds\nbetter after the colon.\n\n>  \tgit stash clear &&\n>  \techo 1 >subdir/subfile1 &&\n>  \techo 2 >subdir/subfile2 &&\n> @@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '\n>  \ttest_i18ncmp expect actual\n>  '\n>  \n> -test_expect_success 'stash push with pathspec shows no changes when there are none' '\n> +test_expect_success 'push: <pathspec> shows no changes when there are none' '\n\nMaybe \"push <pathspec>: show no changes when there are none\"?  \"push\n<pathspec>\" would be the rest of the 'git stash' command, having the\ncolon in between them seems a bit odd.\n\n>  \t>foo &&\n>  \tgit add foo &&\n>  \tgit commit -m \"tmp\" &&\n> @@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no\n>  \ttest_i18ncmp expect actual\n>  '\n>  \n> -test_expect_success 'stash push with pathspec not in the repository errors out' '\n> +test_expect_success 'push: <pathspec> not in the repository errors out' '\n\nThis one makes sense to me.\n\n>  \t>untracked &&\n>  \ttest_must_fail git stash push untracked &&\n>  \ttest_path_is_file untracked\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355751","messageId":"20180815194120.GK2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"47556d40a9944e8cc45ba3df8e12c80a1898b160.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 09/26] stash: implement the \"list\" command in the builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T19:41:20Z","receivedAt":"2018-08-15T19:41:25Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"> Subject: stash: implement the \"list\" command in the builtin\n\nNit: The previous commit messages all have the format \"stash: convert\n<command> to builtin\", maybe follow the same pattern here?\n\nThe rest of the patch looks good to me.\n\nOn 08/08, Paul-Sebastian Ungureanu wrote:\n> Add stash list to the helper and delete the list_stash function\n> from the shell script.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++\n>  git-stash.sh            |  7 +------\n>  2 files changed, 32 insertions(+), 6 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index d6bd468e0..daa4d0034 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -12,6 +12,7 @@\n>  #include \"rerere.h\"\n>  \n>  static const char * const git_stash_helper_usage[] = {\n> +\tN_(\"git stash--helper list [<options>]\"),\n>  \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n> @@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_list_usage[] = {\n> +\tN_(\"git stash--helper list [<options>]\"),\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> @@ -609,6 +615,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n>  \treturn ret;\n>  }\n>  \n> +static int list_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\tstruct option options[] = {\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_list_usage,\n> +\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n> +\n> +\tif (!ref_exists(ref_stash))\n> +\t\treturn 0;\n> +\n> +\tcp.git_cmd = 1;\n> +\targv_array_pushl(&cp.args, \"log\", \"--format=%gd: %gs\", \"-g\",\n> +\t\t\t \"--first-parent\", \"-m\", NULL);\n> +\targv_array_pushv(&cp.args, argv);\n> +\targv_array_push(&cp.args, ref_stash);\n> +\targv_array_push(&cp.args, \"--\");\n> +\treturn run_command(&cp);\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -639,6 +668,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!pop_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"branch\"))\n>  \t\treturn !!branch_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"list\"))\n> +\t\treturn !!list_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 8f2640fe9..6052441aa 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -382,11 +382,6 @@ have_stash () {\n>  \tgit rev-parse --verify --quiet $ref_stash >/dev/null\n>  }\n>  \n> -list_stash () {\n> -\thave_stash || return 0\n> -\tgit log --format=\"%gd: %gs\" -g --first-parent -m \"$@\" $ref_stash --\n> -}\n> -\n>  show_stash () {\n>  \tALLOW_UNKNOWN_FLAGS=t\n>  \tassert_stash_like \"$@\"\n> @@ -574,7 +569,7 @@ test -n \"$seen_non_option\" || set \"push\" \"$@\"\n>  case \"$1\" in\n>  list)\n>  \tshift\n> -\tlist_stash \"$@\"\n> +\tgit stash--helper list \"$@\"\n>  \t;;\n>  show)\n>  \tshift\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355753","messageId":"20180815202010.GL2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"05246e813c810156c07385052ec59d6dec34623e.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 10/26] stash: convert show to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T20:20:10Z","receivedAt":"2018-08-15T20:20:16Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Add stash show to the helper and delete the show_stash, have_stash,\n> assert_stash_like, is_stash_like and parse_flags_and_rev functions\n> from the shell script now that they are no longer needed.\n> \n> Before this commit, `git stash show` would ignore `--index` and\n> `--quiet` options. Now, `git stash show` errors out on `--index`\n> and does not display any message on `--quiet`, but errors out\n> if the stash is not empty.\n\nI think \"errors out\" is slightly misleading here.  Maybe \"but exits\nwith an exit code similar to 'git diff'\" instead?\n\nLooking at why we ignored them before, it's because we filtered them\nout in 'parse_flags_and_rev', which looks more accidental than\nintentional, and I think we could consider a bug, so this change in\nbehaviour here is okay.\n\n'--quiet' doesn't make too much sense to use with 'git stash show', so\nI'm not sure whether or not it makes sense to support it at all.  But\nwe do promise to pass all options through to in our documentation, so\nthe new behaviour is what we are documenting.\n\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c |  78 ++++++++++++++++++++++++\n>  git-stash.sh            | 132 +---------------------------------------\n>  2 files changed, 79 insertions(+), 131 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index daa4d0034..e764cd33e 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -13,6 +13,7 @@\n>  \n>  static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper list [<options>]\"),\n> +\tN_(\"git stash--helper show [<stash>]\"),\n>  \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n> @@ -25,6 +26,11 @@ static const char * const git_stash_helper_list_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_show_usage[] = {\n> +\tN_(\"git stash--helper show [<stash>]\"),\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> @@ -638,6 +644,76 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n>  \treturn run_command(&cp);\n>  }\n>  \n> +static int show_stat = 1;\n> +static int show_patch;\n> +\n> +static int git_stash_config(const char *var, const char *value, void *cb)\n> +{\n> +\tif (!strcmp(var, \"stash.showStat\")) {\n> +\t\tshow_stat = git_config_bool(var, value);\n> +\t\treturn 0;\n> +\t}\n> +\tif (!strcmp(var, \"stash.showPatch\")) {\n> +\t\tshow_patch = git_config_bool(var, value);\n> +\t\treturn 0;\n> +\t}\n> +\treturn git_default_config(var, value, cb);\n> +}\n> +\n> +static int show_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint i, ret = 0;\n> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\tstruct argv_array args_refs = 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\t     git_stash_helper_show_usage,\n> +\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n> +\n> +\tcp.git_cmd = 1;\n> +\targv_array_push(&cp.args, \"diff\");\n> +\n> +\t/* Push arguments which are not options into args_refs. */\n> +\tfor (i = 0; i < argc; ++i) {\n> +\t\tif (argv[i][0] == '-')\n> +\t\t\targv_array_push(&cp.args, argv[i]);\n> +\t\telse\n> +\t\t\targv_array_push(&args_refs, argv[i]);\n> +\t}\n> +\n> +\tif (get_stash_info(&info, args_refs.argc, args_refs.argv)) {\n> +\t\tchild_process_clear(&cp);\n> +\t\targv_array_clear(&args_refs);\n> +\t\treturn -1;\n> +\t}\n> +\n> +\t/*\n> +\t * The config settings are applied only if there are not passed\n> +\t * any flags.\n> +\t */\n> +\tif (cp.args.argc == 1) {\n> +\t\tgit_config(git_stash_config, NULL);\n> +\t\tif (show_stat)\n> +\t\t\targv_array_push(&cp.args, \"--stat\");\n> +\n> +\t\tif (show_patch)\n> +\t\t\targv_array_push(&cp.args, \"-p\");\n> +\t}\n> +\n> +\targv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),\n> +\t\t\t oid_to_hex(&info.w_commit), NULL);\n> +\n> +\tret = run_command(&cp);\n> +\n> +\tfree_stash_info(&info);\n> +\targv_array_clear(&args_refs);\n> +\treturn ret;\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -670,6 +746,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!branch_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"list\"))\n>  \t\treturn !!list_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"show\"))\n> +\t\treturn !!show_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 6052441aa..0d05cbc1e 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -378,35 +378,6 @@ save_stash () {\n>  \tfi\n>  }\n>  \n> -have_stash () {\n> -\tgit rev-parse --verify --quiet $ref_stash >/dev/null\n> -}\n> -\n> -show_stash () {\n> -\tALLOW_UNKNOWN_FLAGS=t\n> -\tassert_stash_like \"$@\"\n> -\n> -\tif test -z \"$FLAGS\"\n> -\tthen\n> -\t\tif test \"$(git config --bool stash.showStat || echo true)\" = \"true\"\n> -\t\tthen\n> -\t\t\tFLAGS=--stat\n> -\t\tfi\n> -\n> -\t\tif test \"$(git config --bool stash.showPatch || echo false)\" = \"true\"\n> -\t\tthen\n> -\t\t\tFLAGS=${FLAGS}${FLAGS:+ }-p\n> -\t\tfi\n> -\n> -\t\tif test -z \"$FLAGS\"\n> -\t\tthen\n> -\t\t\treturn 0\n> -\t\tfi\n> -\tfi\n> -\n> -\tgit diff ${FLAGS} $b_commit $w_commit\n> -}\n> -\n>  show_help () {\n>  \texec git help stash\n>  \texit 1\n> @@ -448,107 +419,6 @@ show_help () {\n>  #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n>  #\n>  \n> -parse_flags_and_rev()\n> -{\n> -\ttest \"$PARSE_CACHE\" = \"$*\" && return 0 # optimisation\n> -\tPARSE_CACHE=\"$*\"\n> -\n> -\tIS_STASH_LIKE=\n> -\tIS_STASH_REF=\n> -\tINDEX_OPTION=\n> -\ts=\n> -\tw_commit=\n> -\tb_commit=\n> -\ti_commit=\n> -\tu_commit=\n> -\tw_tree=\n> -\tb_tree=\n> -\ti_tree=\n> -\tu_tree=\n> -\n> -\tFLAGS=\n> -\tREV=\n> -\tfor opt\n> -\tdo\n> -\t\tcase \"$opt\" in\n> -\t\t\t-q|--quiet)\n> -\t\t\t\tGIT_QUIET=-t\n> -\t\t\t;;\n> -\t\t\t--index)\n> -\t\t\t\tINDEX_OPTION=--index\n> -\t\t\t;;\n> -\t\t\t--help)\n> -\t\t\t\tshow_help\n> -\t\t\t;;\n> -\t\t\t-*)\n> -\t\t\t\ttest \"$ALLOW_UNKNOWN_FLAGS\" = t ||\n> -\t\t\t\t\tdie \"$(eval_gettext \"unknown option: \\$opt\")\"\n> -\t\t\t\tFLAGS=\"${FLAGS}${FLAGS:+ }$opt\"\n> -\t\t\t;;\n> -\t\t\t*)\n> -\t\t\t\tREV=\"${REV}${REV:+ }'$opt'\"\n> -\t\t\t;;\n> -\t\tesac\n> -\tdone\n> -\n> -\teval set -- $REV\n> -\n> -\tcase $# in\n> -\t\t0)\n> -\t\t\thave_stash || die \"$(gettext \"No stash entries found.\")\"\n> -\t\t\tset -- ${ref_stash}@{0}\n> -\t\t;;\n> -\t\t1)\n> -\t\t\t:\n> -\t\t;;\n> -\t\t*)\n> -\t\t\tdie \"$(eval_gettext \"Too many revisions specified: \\$REV\")\"\n> -\t\t;;\n> -\tesac\n> -\n> -\tcase \"$1\" in\n> -\t\t*[!0-9]*)\n> -\t\t\t:\n> -\t\t;;\n> -\t\t*)\n> -\t\t\tset -- \"${ref_stash}@{$1}\"\n> -\t\t;;\n> -\tesac\n> -\n> -\tREV=$(git rev-parse --symbolic --verify --quiet \"$1\") || {\n> -\t\treference=\"$1\"\n> -\t\tdie \"$(eval_gettext \"\\$reference is not a valid reference\")\"\n> -\t}\n> -\n> -\ti_commit=$(git rev-parse --verify --quiet \"$REV^2\") &&\n> -\tset -- $(git rev-parse \"$REV\" \"$REV^1\" \"$REV:\" \"$REV^1:\" \"$REV^2:\" 2>/dev/null) &&\n> -\ts=$1 &&\n> -\tw_commit=$1 &&\n> -\tb_commit=$2 &&\n> -\tw_tree=$3 &&\n> -\tb_tree=$4 &&\n> -\ti_tree=$5 &&\n> -\tIS_STASH_LIKE=t &&\n> -\ttest \"$ref_stash\" = \"$(git rev-parse --symbolic-full-name \"${REV%@*}\")\" &&\n> -\tIS_STASH_REF=t\n> -\n> -\tu_commit=$(git rev-parse --verify --quiet \"$REV^3\") &&\n> -\tu_tree=$(git rev-parse \"$REV^3:\" 2>/dev/null)\n> -}\n> -\n> -is_stash_like()\n> -{\n> -\tparse_flags_and_rev \"$@\"\n> -\ttest -n \"$IS_STASH_LIKE\"\n> -}\n> -\n> -assert_stash_like() {\n> -\tis_stash_like \"$@\" || {\n> -\t\targs=\"$*\"\n> -\t\tdie \"$(eval_gettext \"'\\$args' is not a stash-like commit\")\"\n> -\t}\n> -}\n> -\n>  test \"$1\" = \"-p\" && set \"push\" \"$@\"\n>  \n>  PARSE_CACHE='--not-parsed'\n> @@ -573,7 +443,7 @@ list)\n>  \t;;\n>  show)\n>  \tshift\n> -\tshow_stash \"$@\"\n> +\tgit stash--helper show \"$@\"\n>  \t;;\n>  save)\n>  \tshift\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355754","messageId":"20180815202655.GM2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"5e477e5a62c3e2cc7066487bf6e0a68c922457a5.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 11/26] stash: change `git stash show` usage text and documentation","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T20:26:55Z","receivedAt":"2018-08-15T20:27:00Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"> Subject: stash: change `git stash show` usage text and documentation\n\nAnother nitpick about commit messages.  \"change ... usage text and\ndocumentation\" doesn't say much about what the actual change is.\nHow about something like \"stash: mention options in \"show\" synopsis\"\ninstead?\n\nThe change itself looks good to me, thanks!\n\nOn 08/08, Paul-Sebastian Ungureanu wrote:\n> It is already stated in documentation that it will accept any\n> option known to `git diff`, but not in the usage text and some\n> parts of the documentation.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  Documentation/git-stash.txt | 4 ++--\n>  builtin/stash--helper.c     | 4 ++--\n>  2 files changed, 4 insertions(+), 4 deletions(-)\n> \n> diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\n> index 7ef8c4791..e31ea7d30 100644\n> --- a/Documentation/git-stash.txt\n> +++ b/Documentation/git-stash.txt\n> @@ -9,7 +9,7 @@ SYNOPSIS\n>  --------\n>  [verse]\n>  'git stash' list [<options>]\n> -'git stash' show [<stash>]\n> +'git stash' show [<options>] [<stash>]\n>  'git stash' drop [-q|--quiet] [<stash>]\n>  'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n>  'git stash' branch <branchname> [<stash>]\n> @@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash\n>  The command takes options applicable to the 'git log'\n>  command to control what is shown and how. See linkgit:git-log[1].\n>  \n> -show [<stash>]::\n> +show [<options>] [<stash>]::\n>  \n>  \tShow the changes recorded in the stash entry as a diff between the\n>  \tstashed contents and the commit back when the stash entry was first\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index e764cd33e..0c1efca6b 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -13,7 +13,7 @@\n>  \n>  static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper list [<options>]\"),\n> -\tN_(\"git stash--helper show [<stash>]\"),\n> +\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n>  \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n> @@ -27,7 +27,7 @@ static const char * const git_stash_helper_list_usage[] = {\n>  };\n>  \n>  static const char * const git_stash_helper_show_usage[] = {\n> -\tN_(\"git stash--helper show [<stash>]\"),\n> +\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n>  \tNULL\n>  };\n>  \n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355762","messageId":"20180815210142.GN2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"35e6fdfab85c21f0554da061cfab3f643d1aa794.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T21:01:42Z","receivedAt":"2018-08-15T21:01:48Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Currently, `show_stash()` uses `cmd_diff()` to generate\n> the output. After this commit, the output will be generated\n> using the internal API.\n> \n> Before this commit, `git stash show --quiet` would act like\n> `git diff` and error out if the stash is not empty. Now, the\n> `--quiet` option does not error out given an empty stash.\n\nI think this needs a bit more justification.  As I mentioned in my\ncomment to a previous patch, I'm not sure '--quiet' makes much sense\nwith 'git stash show' (it will show nothing, and will always exit with\nan error code, as the stash will always contain something), but if we\nare supporting the same flags as 'git diff', and essentially just\nforwarding them, shouldn't they keep the same behaviour as well?\n\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------\n>  1 file changed, 45 insertions(+), 28 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 0c1efca6b..ec8c38c6f 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -10,6 +10,8 @@\n>  #include \"run-command.h\"\n>  #include \"dir.h\"\n>  #include \"rerere.h\"\n> +#include \"revision.h\"\n> +#include \"log-tree.h\"\n>  \n>  static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper list [<options>]\"),\n> @@ -662,56 +664,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)\n>  \n>  static int show_stash(int argc, const char **argv, const char *prefix)\n>  {\n> -\tint i, ret = 0;\n> -\tstruct child_process cp = CHILD_PROCESS_INIT;\n> -\tstruct argv_array args_refs = ARGV_ARRAY_INIT;\n> +\tint i;\n> +\tint flags = 0;\n>  \tstruct stash_info info;\n> +\tstruct rev_info rev;\n> +\tstruct argv_array stash_args = ARGV_ARRAY_INIT;\n>  \tstruct option options[] = {\n>  \t\tOPT_END()\n>  \t};\n>  \n> -\targc = parse_options(argc, argv, prefix, options,\n> -\t\t\t     git_stash_helper_show_usage,\n> -\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n> +\tinit_diff_ui_defaults();\n> +\tgit_config(git_diff_ui_config, NULL);\n>  \n> -\tcp.git_cmd = 1;\n> -\targv_array_push(&cp.args, \"diff\");\n> +\tinit_revisions(&rev, prefix);\n>  \n> -\t/* Push arguments which are not options into args_refs. */\n> -\tfor (i = 0; i < argc; ++i) {\n> -\t\tif (argv[i][0] == '-')\n> -\t\t\targv_array_push(&cp.args, argv[i]);\n> +\t/* Push arguments which are not options into stash_args. */\n> +\tfor (i = 1; i < argc; ++i) {\n> +\t\tif (argv[i][0] != '-')\n> +\t\t\targv_array_push(&stash_args, argv[i]);\n>  \t\telse\n> -\t\t\targv_array_push(&args_refs, argv[i]);\n> -\t}\n> -\n> -\tif (get_stash_info(&info, args_refs.argc, args_refs.argv)) {\n> -\t\tchild_process_clear(&cp);\n> -\t\targv_array_clear(&args_refs);\n> -\t\treturn -1;\n> +\t\t\tflags++;\n>  \t}\n>  \n>  \t/*\n>  \t * The config settings are applied only if there are not passed\n>  \t * any flags.\n>  \t */\n> -\tif (cp.args.argc == 1) {\n> +\tif (!flags) {\n>  \t\tgit_config(git_stash_config, NULL);\n>  \t\tif (show_stat)\n> -\t\t\targv_array_push(&cp.args, \"--stat\");\n> +\t\t\trev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;\n> +\t\tif (show_patch) {\n> +\t\t\trev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;\n> +\t\t\trev.diffopt.output_format |= DIFF_FORMAT_PATCH;\n> +\t\t}\n\nI failed to notice this in the previous patch (the problem existed\nthere as well), but this changes the behaviour of 'git -c\nstash.showStat=false stash show <stash>'.  Previously doing this would\nnot show anything, which is the correct behaviour, while now still\nshows the diffstat.\n\nI think the show_stat variable is interpreted the wrong way around in\nthe previous patch.\n\nSomething else I noticed now that I was playing around more with the\nconfig options is that the parsing of the config options is not\ncorrectly done in the previous patch.  It does a 'strcmp(var,\n\"stash.showStat\"))', but the config API makes all variables lowercase\n(config options are case insensitive, and making everything lowercase\nis the way to ensure that), so it should be 'strcmp(var, \"stash.showstat\"))', \nand similar for the 'stash.showpatch' config option.\n\nThis all sounds like it would be nice to have some tests for these\nconfig options, to make sure we get it right, and won't break them in\nthe future.\n\n> +\t}\n>  \n> -\t\tif (show_patch)\n> -\t\t\targv_array_push(&cp.args, \"-p\");\n> +\tif (get_stash_info(&info, stash_args.argc, stash_args.argv)) {\n> +\t\targv_array_clear(&stash_args);\n> +\t\treturn -1;\n>  \t}\n>  \n> -\targv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),\n> -\t\t\t oid_to_hex(&info.w_commit), NULL);\n> +\targc = setup_revisions(argc, argv, &rev, NULL);\n> +\tif (!rev.diffopt.output_format)\n> +\t\trev.diffopt.output_format = DIFF_FORMAT_PATCH;\n> +\tdiff_setup_done(&rev.diffopt);\n> +\trev.diffopt.flags.recursive = 1;\n> +\tsetup_diff_pager(&rev.diffopt);\n>  \n> -\tret = run_command(&cp);\n> +\t/*\n> +\t * We can return early if there was any option not recognised by\n> +\t * `diff_opt_parse()`, besides the word `stash`.\n> +\t */\n\nI'm not sure I follow what you mean with this comment.  I don't see\nany 'diff_opt_parse' in the code above?  \n\n> +\tif (argc > 1) {\n> +\t\tfree_stash_info(&info);\n> +\t\targv_array_clear(&stash_args);\n> +\t\tusage_with_options(git_stash_helper_show_usage, options);\n> +\t}\n> +\n> +\t/* Do the diff thing. */\n\nThis comment should be dropped.  It's obvious that we are doing the\ndiff thing below from the code, so the comment is redundant at best,\nmakes people read more lines, and could possibly get outdated.\n\nI'd also drop the other comments above, as they do not provide a lot\nof extra value imho.\n\n> +\tdiff_tree_oid(&info.b_commit, &info.w_commit, \"\", &rev.diffopt);\n> +\tlog_tree_diff_flush(&rev);\n>  \n>  \tfree_stash_info(&info);\n> -\targv_array_clear(&args_refs);\n> -\treturn ret;\n> +\targv_array_clear(&stash_args);\n> +\treturn 0;\n>  }\n>  \n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355765","messageId":"20180815210817.GO2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"46b04e256f045a53111d52439de376a5f332cb55.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 13/26] stash: update `git stash show` documentation","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T21:08:17Z","receivedAt":"2018-08-15T21:08:21Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Add in documentation about the change of behavior regarding\n> the `--quiet` option, which was introduced in the last commit.\n> (the `--quiet` option does not exit anymore with erorr if it\n\ns/erorr/error/\n\n> is given an empty stash as argument)\n\nIf we want to keep the change in behaviour here (which I'm not sure\nabout as mentioned in my comment on the previous patch), I think this\nshould be folded into the previous patch.  I don't think there's much\nvalue in having this as a separate commit, and folding it into the\nprevious commit has the advantage that we can easily see that the new\nbehaviour is documented.\n\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  Documentation/git-stash.txt | 3 +++\n>  1 file changed, 3 insertions(+)\n> \n> diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\n> index e31ea7d30..d60ebdb96 100644\n> --- a/Documentation/git-stash.txt\n> +++ b/Documentation/git-stash.txt\n> @@ -117,6 +117,9 @@ show [<options>] [<stash>]::\n>  \tYou can use stash.showStat and/or stash.showPatch config variables\n>  \tto change the default behavior.\n>  \n> +\tIt accepts any option known to `git diff`, but acts different on\n\nI notice that we are using single quotes for git commands in some\nplaces and backticks in other places in this man page.  We may want to\nclean that up at some point.  I wouldn't want to do it in this series\nthough, as this is already long enough, and we've had this\ninconsistency for a while already.\n\n> +\t`--quiet` option and exit with zero regardless of differences.\n> +\n>  pop [--index] [-q|--quiet] [<stash>]::\n>  \n>  \tRemove a single stashed state from the stash list and apply it\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355769","messageId":"20180815212655.GP2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"84b0086287c683d33bd456957700199b22fd1c49.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 14/26] stash: convert store to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T21:26:55Z","receivedAt":"2018-08-15T21:27:00Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Add stash store to the helper and delete the store_stash function\n> from the shell script.\n> \n> Add the usage string which was forgotten in the shell script.\n\nI think similarly to 'git stash create', which also doesn't appear in\nthe usage, this was intentionally omitted in the shell script.  The\nreason for the omission is that this is only intended to be useful in\nscripts, and not in interactive usage.  As such it doesn't add much\nvalue in showing it in 'git stash -h'.  Meanwhile it is in the\nsynopsis in the man page.\n\nIf we want to add it to the help output, I think it would be best to\ndo so in a separate commit, and for 'git stash create' as well.  But\nI'm not sure that's a good change.\n\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 52 +++++++++++++++++++++++++++++++++++++++++\n>  git-stash.sh            | 43 ++--------------------------------\n>  2 files changed, 54 insertions(+), 41 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index ec8c38c6f..5ff810f8c 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -20,6 +20,7 @@ static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n>  \tN_(\"git stash--helper clear\"),\n> +\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n>  \tNULL\n>  };\n>  \n> @@ -58,6 +59,11 @@ static const char * const git_stash_helper_clear_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_store_usage[] = {\n> +\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n> +\tNULL\n> +};\n> +\n>  static const char *ref_stash = \"refs/stash\";\n>  static int quiet;\n>  static struct strbuf stash_index_path = STRBUF_INIT;\n> @@ -731,6 +737,50 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n>  \treturn 0;\n>  }\n>  \n> +static int do_store_stash(const char *w_commit, const char *stash_msg,\n> +\t\t\t  int quiet)\n> +{\n> +\tint ret = 0;\n> +\tstruct object_id obj;\n> +\n> +\tif (!stash_msg)\n> +\t\tstash_msg  = xstrdup(\"Created via \\\"git stash--helper store\\\".\");\n\nI assume we're going to s/--helper// in a later commit?  Not sure\nadding the '--helper' here is necessary, as a user would never invoke\n'git stash--helper' directly, so they would expect the stash to be\ncreated by 'git stash store'.  Anyway that's fairly minor, as I assume\nthis is going to change by the end of the patch series.\n\n> +\n> +\tret = get_oid(w_commit, &obj);\n> +\tif (!ret) {\n> +\t\tret = update_ref(stash_msg, ref_stash, &obj, NULL,\n> +\t\t\t\t REF_FORCE_CREATE_REFLOG,\n> +\t\t\t\t quiet ? UPDATE_REFS_QUIET_ON_ERR :\n> +\t\t\t\t UPDATE_REFS_MSG_ON_ERR);\n> +\t}\n> +\tif (ret && !quiet)\n> +\t\tfprintf_ln(stderr, _(\"Cannot update %s with %s\"),\n> +\t\t\t   ref_stash, w_commit);\n> +\n> +\treturn ret;\n> +}\n> +\n> +static int store_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tconst char *stash_msg = NULL;\n> +\tstruct option options[] = {\n> +\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\", N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_store_usage,\n> +\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n> +\n> +\tif (argc != 1) {\n> +\t\tfprintf(stderr, _(\"\\\"git stash--helper store\\\" requires one <commit> argument\\n\"));\n> +\t\treturn -1;\n> +\t}\n> +\n> +\treturn do_store_stash(argv[0], stash_msg, quiet);\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -765,6 +815,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!list_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"show\"))\n>  \t\treturn !!show_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"store\"))\n> +\t\treturn !!store_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 0d05cbc1e..5739c5152 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -191,45 +191,6 @@ create_stash () {\n>  \tdie \"$(gettext \"Cannot record working tree state\")\"\n>  }\n>  \n> -store_stash () {\n> -\twhile test $# != 0\n> -\tdo\n> -\t\tcase \"$1\" in\n> -\t\t-m|--message)\n> -\t\t\tshift\n> -\t\t\tstash_msg=\"$1\"\n> -\t\t\t;;\n> -\t\t-m*)\n> -\t\t\tstash_msg=${1#-m}\n> -\t\t\t;;\n> -\t\t--message=*)\n> -\t\t\tstash_msg=${1#--message=}\n> -\t\t\t;;\n> -\t\t-q|--quiet)\n> -\t\t\tquiet=t\n> -\t\t\t;;\n> -\t\t*)\n> -\t\t\tbreak\n> -\t\t\t;;\n> -\t\tesac\n> -\t\tshift\n> -\tdone\n> -\ttest $# = 1 ||\n> -\tdie \"$(eval_gettext \"\\\"$dashless store\\\" requires one <commit> argument\")\"\n> -\n> -\tw_commit=\"$1\"\n> -\tif test -z \"$stash_msg\"\n> -\tthen\n> -\t\tstash_msg=\"Created via \\\"git stash store\\\".\"\n> -\tfi\n> -\n> -\tgit update-ref --create-reflog -m \"$stash_msg\" $ref_stash $w_commit\n> -\tret=$?\n> -\ttest $ret != 0 && test -z \"$quiet\" &&\n> -\tdie \"$(eval_gettext \"Cannot update \\$ref_stash with \\$w_commit\")\"\n> -\treturn $ret\n> -}\n> -\n>  push_stash () {\n>  \tkeep_index=\n>  \tpatch_mode=\n> @@ -308,7 +269,7 @@ push_stash () {\n>  \t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n>  \n>  \tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n> -\tstore_stash -m \"$stash_msg\" -q $w_commit ||\n> +\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n>  \tdie \"$(gettext \"Cannot save the current status\")\"\n>  \tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n>  \n> @@ -468,7 +429,7 @@ create)\n>  \t;;\n>  store)\n>  \tshift\n> -\tstore_stash \"$@\"\n> +\tgit stash--helper store \"$@\"\n>  \t;;\n>  drop)\n>  \tshift\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355774","messageId":"20180815221305.GQ2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"0393bbd09daeb7ab67da4a343ba58d824c35532b.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T22:13:05Z","receivedAt":"2018-08-15T22:13:10Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Add stash create to the helper.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++\n>  git-stash.sh            |   2 +-\n>  2 files changed, 407 insertions(+), 1 deletion(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 5ff810f8c..a4e57899b 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -21,6 +21,7 @@ static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n>  \tN_(\"git stash--helper clear\"),\n>  \tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n> +\tN_(\"git stash--helper create [<message>]\"),\n>  \tNULL\n>  };\n>  \n> @@ -64,6 +65,11 @@ static const char * const git_stash_helper_store_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_create_usage[] = {\n> +\tN_(\"git stash--helper create [<message>]\"),\n> +\tNULL\n> +};\n> +\n>  static const char *ref_stash = \"refs/stash\";\n>  static int quiet;\n>  static struct strbuf stash_index_path = STRBUF_INIT;\n> @@ -781,6 +787,404 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n>  \treturn do_store_stash(argv[0], stash_msg, quiet);\n>  }\n>\n> [...]\n> \n> +\n> +static int do_create_stash(int argc, const char **argv, const char *prefix,\n> +\t\t\t   const char **stash_msg, int include_untracked,\n> +\t\t\t   int patch_mode, struct stash_info *info)\n> +{\n> +\tint untracked_commit_option = 0;\n> +\tint ret = 0;\n> +\tint subject_len;\n> +\tint flags;\n> +\tconst char *head_short_sha1 = NULL;\n> +\tconst char *branch_ref = NULL;\n> +\tconst char *head_subject = NULL;\n> +\tconst char *branch_name = \"(no branch)\";\n> +\tstruct commit *head_commit = NULL;\n> +\tstruct commit_list *parents = NULL;\n> +\tstruct strbuf msg = STRBUF_INIT;\n> +\tstruct strbuf commit_tree_label = STRBUF_INIT;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct strbuf final_stash_msg = STRBUF_INIT;\n> +\n> +\tread_cache_preload(NULL);\n> +\trefresh_cache(REFRESH_QUIET);\n> +\n> +\tif (!check_changes(argv, include_untracked, prefix)) {\n> +\t\tret = 1;\n> +\t\tgoto done;\n\nI wonder if we can just 'exit(0)' here, instead of returning.  This\nwhole command is a builtin, and I *think* outside of 'libgit.a' exiting\nearly is fine.  It does mean that we're not free'ing the memory\nthough, which means a leak checker would probably complain.  So\ndunno.  It would simplify the code a little, but not sure it's worth it.\n\n> +\t}\n> +\n> +\tif (get_oid(\"HEAD\", &info->b_commit)) {\n> +\t\tfprintf_ln(stderr, \"You do not have the initial commit yet\");\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t} else {\n> +\t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n> +\t}\n> +\n> +\tbranch_ref = resolve_ref_unsafe(\"HEAD\", 0, NULL, &flags);\n> +\tif (flags & REF_ISSYMREF)\n> +\t\tbranch_name = strrchr(branch_ref, '/') + 1;\n> +\thead_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n> +\t\t\t\t\t     DEFAULT_ABBREV);\n> +\tsubject_len = find_commit_subject(get_commit_buffer(head_commit, NULL),\n> +\t\t\t\t\t  &head_subject);\n> +\tstrbuf_addf(&msg, \"%s: %s %.*s\\n\", branch_name, head_short_sha1,\n> +\t\t    subject_len, head_subject);\n\nI think this can be written in a slightly simpler way:\n\n\thead_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n\t\t\t\t\t     DEFAULT_ABBREV);\n\tstrbuf_addf(&msg, \"%s: %s\", branch_name, head_short_sha1);\n\tpp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);\n\tstrbuf_addch(&msg, '\\n');\n\nThe other advantage this brings is that it is consistent with other\nplaces where we print/use the subject of a commit (e.g. in 'git reset\n--hard').\n\n> +\n> +\tstrbuf_addf(&commit_tree_label, \"index on %s\\n\", msg.buf);\n> +\tcommit_list_insert(head_commit, &parents);\n> +\tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n> +\t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n> +\t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n> +\t\tfprintf_ln(stderr, \"Cannot save the current index state\");\n\nLooks like this message is translated in the current 'git stash'\nimplementation, so it should be here as well.  Same for the messages\nbelow.\n\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (include_untracked && get_untracked_files(argv, 1,\n> +\t\t\t\t\t\t     include_untracked, &out)) {\n> +\t\tif (save_untracked_files(info, &msg, &out)) {\n> +\t\t\tprintf_ln(\"Cannot save the untracked files\");\n\nWhy does this go to stdout, whereas \"Cannot save the current index\nstate\" above goes to stderr?  In the shell version of git stash these\nall go to stderr fwiw.  There are a few similar cases, it would\nprobably be worth going through all the print statements here and see\nif they need to be translated, and to which output stream they should\ngo.\n\n> +\t\t\tret = -1;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t\tuntracked_commit_option = 1;\n> +\t}\n> +\tif (patch_mode) {\n> +\t\tret = stash_patch(info, argv);\n> +\t\tif (ret < 0) {\n> +\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n> +\t\t\tgoto done;\n> +\t\t} else if (ret > 0) {\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t} else {\n> +\t\tif (stash_working_tree(info, argv)) {\n> +\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n> +\t\t\tret = -1;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t}\n> +\n> +\tif (!*stash_msg || !strlen(*stash_msg))\n> +\t\tstrbuf_addf(&final_stash_msg, \"WIP on %s\", msg.buf);\n> +\telse\n> +\t\tstrbuf_addf(&final_stash_msg, \"On %s: %s\\n\", branch_name,\n> +\t\t\t    *stash_msg);\n> +\t*stash_msg = strbuf_detach(&final_stash_msg, NULL);\n\nstrbuf_detach means we're taking ownership of the memory, so we'll\nhave to free it afterwards. Looking at this we may not even want to\nre-use the 'stash_msg' variable here, but instead introduce another\nvariable for it, so it doesn't have a dual meaning in this function.\n\n> +\n> +\t/*\n> +\t * `parents` will be empty after calling `commit_tree()`, so there is\n> +\t * no need to call `free_commit_list()`\n> +\t */\n> +\tparents = NULL;\n> +\tif (untracked_commit_option)\n> +\t\tcommit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n> +\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n> +\tcommit_list_insert(head_commit, &parents);\n> +\n> +\tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n> +\t\t\tparents, &info->w_commit, NULL, NULL)) {\n> +\t\tprintf_ln(\"Cannot record working tree state\");\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +done:\n> +\tstrbuf_release(&commit_tree_label);\n> +\tstrbuf_release(&msg);\n> +\tstrbuf_release(&out);\n> +\tstrbuf_release(&final_stash_msg);\n> +\treturn ret;\n> +}\n> +\n> +static int create_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint include_untracked = 0;\n> +\tint ret = 0;\n> +\tconst char *stash_msg = NULL;\n> +\tstruct stash_info info;\n> +\tstruct option options[] = {\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> +\t\t\t N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_create_usage,\n> +\t\t\t     0);\n> +\n> +\tret = do_create_stash(argc, argv, prefix, &stash_msg,\n> +\t\t\t      include_untracked, 0, &info);\n\nstash_msg doesn't have to be passed as a pointer to a pointer here, as\nwe never need the modified value after this function returns.  I think\njust passing 'stash_msg' here instead of '&stash_msg' will make\n'do_create_stash' slightly easier to read.\n\n> +\n> +\tif (!ret)\n> +\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n> +\n> +\t/*\n> +\t * ret can be 1 if there were no changes. In this case, we should\n> +\t * not error out.\n> +\t */\n> +\treturn ret < 0;\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -817,6 +1221,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!show_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"store\"))\n>  \t\treturn !!store_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"create\"))\n> +\t\treturn !!create_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 5739c5152..ab06e4ffb 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -425,7 +425,7 @@ clear)\n>  \t;;\n>  create)\n>  \tshift\n> -\tcreate_stash -m \"$*\" && echo \"$w_commit\"\n> +\tgit stash--helper create --message \"$*\"\n>  \t;;\n>  store)\n>  \tshift\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355775","messageId":"20180815222503.GR2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"cover.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 00/26] Convert \"git stash\" to C builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-15T22:25:03Z","receivedAt":"2018-08-15T22:25:09Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Hello,\n> \n> Here is the whole `git stash` C version. Some of the previous\n> patches were already reviewed (up to and including \"stash: convert\n> store to builtin\"), but there are some which were not\n> (starting with \"stash: convert create to builtin\").\n\nThanks for this new iteration, and sorry I took a while to find some\ntime to review this.  I had another read through the patches up until\npatch 15, and left some comments, before running out of time again.  I\nhope to find some time in the next few days to go through the rest of\nthe series as well.\n\nOne more comment in terms of the structure of the series.  The\npatches doing the actual conversion from shell to C seem to be\ninterleaved with cleanup patches and patches that make the C version\nuse more internal APIs.  I'd suggest putting all the cleanup patches\n(e.g. \"stash: change `git stash show` usage text and documentation\")\nto the front of the series, as that's more likely to be\nuncontroversial, and could maybe even be merged by itself.\n\nThen I'd put all the conversion from shell to C patches, and only once\neverything is converted I'd put the patches to use more of the\ninternal APIs rather than using run_command everywhere.  A possible\nalternative would be to squash the patches to replace the run_command\ncalls with patches that use the internal API directly, to save the\nreviewers some time by reading through less churn.  Though I'm kind of\non the fence with that, as a faithful conversion using 'run_command'\nmay be easier to review as a first step.\n\nHope this helps!\n\n> In order to see the difference between the shell version and\n> the C version, I ran `time` on:\n> \n> * git test suite (t3903-stash.sh, t3904-stash-patch.sh,\n> t3905-stash-include-untracked.sh and t3906-stash-submodule.sh)\n> \n>         t3903-stash.sh:\n>         ** SHELL: 12,69s user 9,95s system 109% cpu 20,730 total\n>         **     C:  2,67s user 2,84s system 105% cpu  5,206 total\n> \n>         t3904-stash-patch.sh:\n>         ** SHELL: 1,43s user 0,94s system 106% cpu 2,242 total\n>         **     C: 1,01s user 0,58s system 104% cpu 1,530 total\n> \n>         t3905-stash-include-untracked.sh\n>         ** SHELL: 2,22s user 1,73s system 110% cpu 3,569 total\n>         **     C: 0,59s user 0,57s system 106% cpu 1,085 total\n> \n>         t3906-stash-submodule.sh\n>         ** SHELL: 2,89s user 2,99s system 106% cpu 5,527 total\n>         **     C: 2,21s user 2,61s system 105% cpu 4,568 total\n> \n>         TOTAL:\n>         ** SHELL: 19.23s user 15.61s system\n>         **     C:  6.48s user  6.60s system\n\nAwesome!\n\n> * a git repository with 4000 files: 1000 not changed,\n> 1000 staged files, 1000 unstaged files, 1000 untracked.\n> In this case I ran some of the most used commands:\n> \n>         git stash push:\n> \n>         ** SHELL: 0,12s user 0,21s system 101% cpu 0,329 total\n>         **     C: 0,06s user 0,13s system 105% cpu 0,185 total\n> \n>         git stash push -u:\n> \n>         ** SHELL: 0,18s user 0,27s system  108% cpu 0,401 total\n>         **     C: 0,09s user 0,19s system  103% cpu 0,267 total\n> \n>         git stash pop:\n> \n>         ** SHELL: 0,16s user 0,26s system 103% cpu 0,399 total\n>         **     C: 0,13s user 0,19s system 102% cpu 0,308 total\n> \n> Best regards,\n> Paul Ungureanu\n> \n> \n> Joel 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> Paul-Sebastian Ungureanu (21):\n>   sha1-name.c: added 'get_oidf', which acts like 'get_oid'\n>   stash: update test cases conform to coding guidelines\n>   stash: renamed test cases to be more descriptive\n>   stash: implement the \"list\" command in the builtin\n>   stash: convert show to builtin\n>   stash: change `git stash show` usage text and documentation\n>   stash: refactor `show_stash()` to use the diff API\n>   stash: update `git stash show` documentation\n>   stash: convert store to builtin\n>   stash: convert create to builtin\n>   stash: replace spawning a \"read-tree\" process\n>   stash: avoid spawning a \"diff-index\" process\n>   stash: convert push to builtin\n>   stash: make push to be quiet\n>   stash: add tests for `git stash push -q`\n>   stash: replace spawning `git ls-files` child process\n>   stash: convert save to builtin\n>   stash: convert `stash--helper.c` into `stash.c`\n>   stash: optimize `get_untracked_files()` and `check_changes()`\n>   stash: replace all `write-tree` child processes with API calls\n>   stash: replace all \"git apply\" child processes with API calls\n> \n>  Documentation/git-stash.txt |    7 +-\n>  Makefile                    |    2 +-\n>  builtin.h                   |    1 +\n>  builtin/stash.c             | 1562 +++++++++++++++++++++++++++++++++++\n>  cache.h                     |    1 +\n>  git-stash.sh                |  752 -----------------\n>  git.c                       |    1 +\n>  sha1-name.c                 |   19 +\n>  t/t3903-stash.sh            |  190 +++--\n>  9 files changed, 1714 insertions(+), 821 deletions(-)\n>  create mode 100644 builtin/stash.c\n>  delete mode 100755 git-stash.sh\n> \n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355882","messageId":"CADzBBBYXQRda28oq6KmfpNNJvG9OCeTxMd1Ra-VfjP1S4FEJ-g@mail.gmail.com","threadId":"48222","inReplyTo":"20180815222503.GR2734@hank.intra.tgummerer.com","subject":"Re: [GSoC][PATCH v7 00/26] Convert \"git stash\" to C builtin","fromName":"Paul Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-16T21:25:40Z","receivedAt":"2018-08-16T21:25:44Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hi,\n\nOn Thu, Aug 16, 2018 at 1:25 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:\n> On 08/08, Paul-Sebastian Ungureanu wrote:\n>> Hello,\n>>\n>> Here is the whole `git stash` C version. Some of the previous\n>> patches were already reviewed (up to and including \"stash: convert\n>> store to builtin\"), but there are some which were not\n>> (starting with \"stash: convert create to builtin\").\n>\n> Thanks for this new iteration, and sorry I took a while to find some\n> time to review this.  I had another read through the patches up until\n> patch 15, and left some comments, before running out of time again.  I\n> hope to find some time in the next few days to go through the rest of\n> the series as well.\n\nThank you a lot for taking time to review my patches. It really means a lot.\n\n> One more comment in terms of the structure of the series.  The\n> patches doing the actual conversion from shell to C seem to be\n> interleaved with cleanup patches and patches that make the C version\n> use more internal APIs.  I'd suggest putting all the cleanup patches\n> (e.g. \"stash: change `git stash show` usage text and documentation\")\n> to the front of the series, as that's more likely to be\n> uncontroversial, and could maybe even be merged by itself.\n\nGood point.\n\n> Then I'd put all the conversion from shell to C patches, and only once\n> everything is converted I'd put the patches to use more of the\n> internal APIs rather than using run_command everywhere.  A possible\n> alternative would be to squash the patches to replace the run_command\n> calls with patches that use the internal API directly, to save the\n> reviewers some time by reading through less churn.  Though I'm kind of\n> on the fence with that, as a faithful conversion using 'run_command'\n> may be easier to review as a first step.\n\nMakes sense. Actually, as you said, the patches that replace `run_command()`\nor `pipe_command()` were not squashed because I thought it might be more\neasier for reviewers. The `stash: replace all \"git apply\" child\nprocesses with API\ncalls` patch is a exception to the rule because I was highly in doubts\nif the patch\nwould actually be good.\n\n> Hope this helps!\n\nIt helps a lot. Thank you!\n\n>> In order to see the difference between the shell version and\n>> the C version, I ran `time` on:\n>>\n>> * git test suite (t3903-stash.sh, t3904-stash-patch.sh,\n>> t3905-stash-include-untracked.sh and t3906-stash-submodule.sh)\n>>\n>>         t3903-stash.sh:\n>>         ** SHELL: 12,69s user 9,95s system 109% cpu 20,730 total\n>>         **     C:  2,67s user 2,84s system 105% cpu  5,206 total\n>>\n>>         t3904-stash-patch.sh:\n>>         ** SHELL: 1,43s user 0,94s system 106% cpu 2,242 total\n>>         **     C: 1,01s user 0,58s system 104% cpu 1,530 total\n>>\n>>         t3905-stash-include-untracked.sh\n>>         ** SHELL: 2,22s user 1,73s system 110% cpu 3,569 total\n>>         **     C: 0,59s user 0,57s system 106% cpu 1,085 total\n>>\n>>         t3906-stash-submodule.sh\n>>         ** SHELL: 2,89s user 2,99s system 106% cpu 5,527 total\n>>         **     C: 2,21s user 2,61s system 105% cpu 4,568 total\n>>\n>>         TOTAL:\n>>         ** SHELL: 19.23s user 15.61s system\n>>         **     C:  6.48s user  6.60s system\n>\n> Awesome!\n\nI hope that it will get even better.\n\nBest regards,\nPaul\n"},{"id":"355973","messageId":"CADzBBBa+xCPNNyjpq43_CBEDETSmnLeeTF4ewkZ6C3tTkCzfCw@mail.gmail.com","threadId":"48222","inReplyTo":"20180815194120.GK2734@hank.intra.tgummerer.com","subject":"Re: [GSoC][PATCH v7 09/26] stash: implement the \"list\" command in the builtin","fromName":"Paul Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-18T11:44:46Z","receivedAt":"2018-08-18T11:44:50Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"On Wed, Aug 15, 2018 at 10:41 PM, Thomas Gummerer <t.gummerer@gmail.com> wrote:\n>> Subject: stash: implement the \"list\" command in the builtin\n>\n> Nit: The previous commit messages all have the format \"stash: convert\n> <command> to builtin\", maybe follow the same pattern here?\n\nAt first, the subject of the commit matched the pattern. I think I\nchanged it in order\nto avoid any confusion with \"list\" as data structure. Now, I guess\nthat \"stash:\" at the\nbeginning of the commit message removes any doubt.\n"},{"id":"355974","messageId":"CADzBBBbBVKCiGXtZi=OPD7szApTeA56bmt-kUmRv=LaAbsCk+g@mail.gmail.com","threadId":"48222","inReplyTo":"20180815202010.GL2734@hank.intra.tgummerer.com","subject":"Re: [GSoC][PATCH v7 10/26] stash: convert show to builtin","fromName":"Paul Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-18T12:11:35Z","receivedAt":"2018-08-18T12:12:22Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"On Wed, Aug 15, 2018 at 11:20 PM, Thomas Gummerer <t.gummerer@gmail.com> wrote:\n> '--quiet' doesn't make too much sense to use with 'git stash show', so\n> I'm not sure whether or not it makes sense to support it at all.  But\n> we do promise to pass all options through to in our documentation, so\n> the new behaviour is what we are documenting.\n\nIndeed, I guess it doesn't make sense at all. I cannot come up\nwith a situation where `--quiet` would be useful. I will think more\nabout it and come back with a reply if I find any good example\nwhere it would be useful.\n"},{"id":"355987","messageId":"CADzBBBbrE7DFQ_wN1VEYriagjfWqYZzcFM26LfnNP-3MmYmyzQ@mail.gmail.com","threadId":"48222","inReplyTo":"20180815210142.GN2734@hank.intra.tgummerer.com","subject":"Re: [GSoC][PATCH v7 12/26] stash: refactor `show_stash()` to use the diff API","fromName":"Paul Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-18T15:11:26Z","receivedAt":"2018-08-18T15:11:31Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"On Thu, Aug 16, 2018 at 12:01 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:\n> On 08/08, Paul-Sebastian Ungureanu wrote:\n>> Currently, `show_stash()` uses `cmd_diff()` to generate\n>> the output. After this commit, the output will be generated\n>> using the internal API.\n>>\n>> Before this commit, `git stash show --quiet` would act like\n>> `git diff` and error out if the stash is not empty. Now, the\n>> `--quiet` option does not error out given an empty stash.\n>\n> I think this needs a bit more justification.  As I mentioned in my\n> comment to a previous patch, I'm not sure '--quiet' makes much sense\n> with 'git stash show' (it will show nothing, and will always exit with\n> an error code, as the stash will always contain something), but if we\n> are supporting the same flags as 'git diff', and essentially just\n> forwarding them, shouldn't they keep the same behaviour as well?\n\nIf we are going to support them, I guess there wouldn't be a problem\nif any change in behaviour is noted in documentation (but as you said,\nthe next commit should be squashed into this). Indeed, the big question\nis if we should support them. I would say no considering there is no\nbenefit.\n\n>> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n>> ---\n>>  builtin/stash--helper.c | 73 +++++++++++++++++++++++++----------------\n>>  1 file changed, 45 insertions(+), 28 deletions(-)\n>>\n>> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n>> index 0c1efca6b..ec8c38c6f 100644\n>> --- a/builtin/stash--helper.c\n>> +++ b/builtin/stash--helper.c\n>> @@ -10,6 +10,8 @@\n>>  #include \"run-command.h\"\n>>  #include \"dir.h\"\n>>  #include \"rerere.h\"\n>> +#include \"revision.h\"\n>> +#include \"log-tree.h\"\n>>\n>>  static const char * const git_stash_helper_usage[] = {\n>>       N_(\"git stash--helper list [<options>]\"),\n>> @@ -662,56 +664,71 @@ static int git_stash_config(const char *var, const char *value, void *cb)\n>>\n>>  static int show_stash(int argc, const char **argv, const char *prefix)\n>>  {\n>> -     int i, ret = 0;\n>> -     struct child_process cp = CHILD_PROCESS_INIT;\n>> -     struct argv_array args_refs = ARGV_ARRAY_INIT;\n>> +     int i;\n>> +     int flags = 0;\n>>       struct stash_info info;\n>> +     struct rev_info rev;\n>> +     struct argv_array stash_args = ARGV_ARRAY_INIT;\n>>       struct option options[] = {\n>>               OPT_END()\n>>       };\n>>\n>> -     argc = parse_options(argc, argv, prefix, options,\n>> -                          git_stash_helper_show_usage,\n>> -                          PARSE_OPT_KEEP_UNKNOWN);\n>> +     init_diff_ui_defaults();\n>> +     git_config(git_diff_ui_config, NULL);\n>>\n>> -     cp.git_cmd = 1;\n>> -     argv_array_push(&cp.args, \"diff\");\n>> +     init_revisions(&rev, prefix);\n>>\n>> -     /* Push arguments which are not options into args_refs. */\n>> -     for (i = 0; i < argc; ++i) {\n>> -             if (argv[i][0] == '-')\n>> -                     argv_array_push(&cp.args, argv[i]);\n>> +     /* Push arguments which are not options into stash_args. */\n>> +     for (i = 1; i < argc; ++i) {\n>> +             if (argv[i][0] != '-')\n>> +                     argv_array_push(&stash_args, argv[i]);\n>>               else\n>> -                     argv_array_push(&args_refs, argv[i]);\n>> -     }\n>> -\n>> -     if (get_stash_info(&info, args_refs.argc, args_refs.argv)) {\n>> -             child_process_clear(&cp);\n>> -             argv_array_clear(&args_refs);\n>> -             return -1;\n>> +                     flags++;\n>>       }\n>>\n>>       /*\n>>        * The config settings are applied only if there are not passed\n>>        * any flags.\n>>        */\n>> -     if (cp.args.argc == 1) {\n>> +     if (!flags) {\n>>               git_config(git_stash_config, NULL);\n>>               if (show_stat)\n>> -                     argv_array_push(&cp.args, \"--stat\");\n>> +                     rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT;\n>> +             if (show_patch) {\n>> +                     rev.diffopt.output_format = ~DIFF_FORMAT_NO_OUTPUT;\n>> +                     rev.diffopt.output_format |= DIFF_FORMAT_PATCH;\n>> +             }\n>\n> I failed to notice this in the previous patch (the problem existed\n> there as well), but this changes the behaviour of 'git -c\n> stash.showStat=false stash show <stash>'.  Previously doing this would\n> not show anything, which is the correct behaviour, while now still\n> shows the diffstat.\n>\n> I think the show_stat variable is interpreted the wrong way around in\n> the previous patch.\n>\n> Something else I noticed now that I was playing around more with the\n> config options is that the parsing of the config options is not\n> correctly done in the previous patch.  It does a 'strcmp(var,\n> \"stash.showStat\"))', but the config API makes all variables lowercase\n> (config options are case insensitive, and making everything lowercase\n> is the way to ensure that), so it should be 'strcmp(var, \"stash.showstat\"))',\n> and similar for the 'stash.showpatch' config option.\n>\n> This all sounds like it would be nice to have some tests for these\n> config options, to make sure we get it right, and won't break them in\n> the future.\n\nThanks! Great suggestion! I will add some tests for this.\n\n>> +     }\n>>\n>> -             if (show_patch)\n>> -                     argv_array_push(&cp.args, \"-p\");\n>> +     if (get_stash_info(&info, stash_args.argc, stash_args.argv)) {\n>> +             argv_array_clear(&stash_args);\n>> +             return -1;\n>>       }\n>>\n>> -     argv_array_pushl(&cp.args, oid_to_hex(&info.b_commit),\n>> -                      oid_to_hex(&info.w_commit), NULL);\n>> +     argc = setup_revisions(argc, argv, &rev, NULL);\n>> +     if (!rev.diffopt.output_format)\n>> +             rev.diffopt.output_format = DIFF_FORMAT_PATCH;\n>> +     diff_setup_done(&rev.diffopt);\n>> +     rev.diffopt.flags.recursive = 1;\n>> +     setup_diff_pager(&rev.diffopt);\n>>\n>> -     ret = run_command(&cp);\n>> +     /*\n>> +      * We can return early if there was any option not recognised by\n>> +      * `diff_opt_parse()`, besides the word `stash`.\n>> +      */\n>\n> I'm not sure I follow what you mean with this comment.  I don't see\n> any 'diff_opt_parse' in the code above?\n\nOh, right. `diff_opt_parse()` is called by `setup_revision()` at some point,\nbut as you said a little bit below, these two comments (the previous one\nand the next one) are redundant.\n\n>> +     if (argc > 1) {\n>> +             free_stash_info(&info);\n>> +             argv_array_clear(&stash_args);\n>> +             usage_with_options(git_stash_helper_show_usage, options);\n>> +     }\n>> +\n>> +     /* Do the diff thing. */\n>\n> This comment should be dropped.  It's obvious that we are doing the\n> diff thing below from the code, so the comment is redundant at best,\n> makes people read more lines, and could possibly get outdated.\n>\n> I'd also drop the other comments above, as they do not provide a lot\n> of extra value imho.\n>\n>> +     diff_tree_oid(&info.b_commit, &info.w_commit, \"\", &rev.diffopt);\n>> +     log_tree_diff_flush(&rev);\n>>\n>>       free_stash_info(&info);\n>> -     argv_array_clear(&args_refs);\n>> -     return ret;\n>> +     argv_array_clear(&stash_args);\n>> +     return 0;\n>>  }\n>>\n>>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>> --\n>> 2.18.0.573.g56500d98f\n>>\n"},{"id":"355988","messageId":"20180818153604.GB11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"b41bff3b5188ac014f9cb5024e535d6804ef1834.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 18/26] stash: convert push to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T15:36:04Z","receivedAt":"2018-08-18T15:36:09Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Add stash push to the helper.\n> ---\n\nThis (and the previous two and I think most subsequent patches) are\nmissing your sign-off.\n\n>  builtin/stash--helper.c | 209 ++++++++++++++++++++++++++++++++++++++++\n>  git-stash.sh            |   6 +-\n>  2 files changed, 213 insertions(+), 2 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index f905d3908..c26cad3d5 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper clear\"),\n>  \tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n>  \tN_(\"git stash--helper create [<message>]\"),\n> +\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n> +\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n> +\t   \"          [--] [<pathspec>...]]\"),\n>  \tNULL\n>  };\n>  \n> @@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_push_usage[] = {\n> +\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n> +\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n> +\t   \"          [--] [<pathspec>...]]\"),\n> +\tNULL\n> +};\n> +\n>  static const char *ref_stash = \"refs/stash\";\n>  static int quiet;\n>  static struct strbuf stash_index_path = STRBUF_INIT;\n> @@ -1210,6 +1220,203 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n>  \treturn ret < 0;\n>  }\n>  \n> +static int do_push_stash(int argc, const char **argv, const char *prefix,\n> +\t\t\t int keep_index, int patch_mode, int include_untracked,\n> +\t\t\t int quiet, const char *stash_msg)\n> +{\n> +\tint ret = 0;\n> +\tstruct pathspec ps;\n> +\tstruct stash_info info;\n> +\tif (patch_mode && keep_index == -1)\n> +\t\tkeep_index = 1;\n> +\n> +\tif (patch_mode && include_untracked) {\n> +\t\tfprintf_ln(stderr, \"Can't use --patch and --include-untracked or --all at the same time\");\n\nThis should be marked for translation.  Similar for the messages\nbelow.  I noticed this in a previous patch as well, so it may be worth\nreviewing all the output, and checking that it's going to the right\nstream and is marked for translation.\n\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n> +\n> +\tif (read_cache() < 0)\n> +\t\tdie(_(\"index file corrupt\"));\n> +\n> +\tif (!include_untracked && ps.nr) {\n> +\t\tint i;\n> +\t\tchar *ps_matched = xcalloc(ps.nr, 1);\n> +\n> +\t\tfor (i = 0; i < active_nr; ++i) {\n> +\t\t\tconst struct cache_entry *ce = active_cache[i];\n> +\t\t\tif (!ce_path_match(ce, &ps, ps_matched))\n> +\t\t\t\tcontinue;\n> +\t\t}\n> +\n> +\t\tif (report_path_error(ps_matched, &ps, prefix)) {\n> +\t\t\tfprintf_ln(stderr, \"Did you forget to 'git add'?\");\n> +\t\t\treturn -1;\n> +\t\t}\n> +\t}\n> +\n> +\tread_cache_preload(NULL);\n\nInstead of doing a 'read_cache' before and 'read_cache_preload(NULL)'\nhere, we could just use 'read_cache_preload(NULL)' above.\n'read_cache' does return early if the index has already been read, so\nthere's no big harm in doing this twice, but just having one call is\nstill neater I think.\n\nIt would make the command slightly slower in the error case above, but\nI doubt that's worth worrying about.\n\n> +\tif (refresh_cache(REFRESH_QUIET))\n> +\t\treturn -1;\n> +\n> +\tif (!check_changes(argv, include_untracked, prefix)) {\n> +\t\tfprintf_ln(stdout, \"No local changes to save\");\n> +\t\treturn 0;\n> +\t}\n> +\n> +\tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n> +\t\tfprintf_ln(stderr, \"Cannot initialize stash\");\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tif ((ret = do_create_stash(argc, argv, prefix, &stash_msg,\n> +\t\t\t\t   include_untracked, patch_mode, &info)))\n> +\t\treturn ret;\n\nShould this be 'return ret < 0'?  'ret == 1' means there are no\nchanges, for which we currently get a 0 exit code.  Though on second\nthought that can't happen, because we already have 'check_changes'\nabove.  Why do we want the 'ret' variable here?\n\nSomething I notice here is that we are passing 'argc' and 'argv'\naround a lot.  We passed that through parse-options already, and it\nseems to me that we're mostly left with pathspecs here, rather than\n'argv'.  It looks to me like we could just parse the pathspecs in the\ncallers (which we do in some places, but maybe not in all of them) and\nthen pass 'struct pathspec' around instead of the leftover argv, which\nis easier to understand, and gives all these functions a neater/easier\nto understand interface.\n\nAlso looking at 'do_create_stash', the 'argc' argument seems to be\nunused?  I failed to notice that earlier obviously, but we should get\nrid of that unused argument.\n\n> +\n> +\tif (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {\n> +\t\tfprintf(stderr, \"Cannot save the current status\");\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tfprintf(stdout, \"Saved working directory and index state %s\", stash_msg);\n> +\n> +\tif (!patch_mode) {\n> +\t\tif (include_untracked && ps.nr == 0) {\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"clean\", \"--force\",\n> +\t\t\t\t\t \"--quiet\", \"-d\", NULL);\n> +\t\t\tif (include_untracked == 2)\n> +\t\t\t\targv_array_push(&cp.args, \"-x\");\n> +\t\t\tif (run_command(&cp))\n> +\t\t\t\treturn -1;\n> +\t\t}\n> +\t\tif (argc != 0) {\n\nWe use 'ps.nr' in the check above, and 'argc' here.  I think we should\nconsistently use 'ps.nr'.\n\n> +\t\t\tint i;\n> +\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t\t\tcp1.git_cmd = 1;\n> +\t\t\targv_array_push(&cp1.args, \"add\");\n> +\t\t\tif (!include_untracked)\n> +\t\t\t\targv_array_push(&cp1.args, \"-u\");\n> +\t\t\tif (include_untracked == 2)\n> +\t\t\t\targv_array_push(&cp1.args, \"--force\");\n> +\t\t\targv_array_push(&cp1.args, \"--\");\n> +\t\t\tfor (i = 0; i < ps.nr; ++i)\n> +\t\t\t\targv_array_push(&cp1.args, ps.items[i].match);\n\nSince we're doing this 'argv_array_push' with pathspec elements a few\ntimes already (and may do it even more often if the code is being\nrefactored to pass 'struct pathspec' around), would it be worth\nfactoring this out as a tiny helper function?\n\n> +\t\t\tif (run_command(&cp1))\n> +\t\t\t\treturn -1;\n> +\n> +\t\t\tcp2.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp2.args, \"diff-index\", \"-p\",\n> +\t\t\t\t\t \"--cached\", \"--binary\", \"HEAD\", \"--\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tfor (i = 0; i < ps.nr; ++i)\n> +\t\t\t\targv_array_push(&cp2.args, ps.items[i].match);\n> +\t\t\tif (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))\n> +\t\t\t\treturn -1;\n> +\n> +\t\t\tcp3.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp3.args, \"apply\", \"--index\", \"-R\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tif (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,\n> +\t\t\t\t\t 0))\n> +\t\t\t\treturn -1;\n> +\t\t} else {\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"reset\", \"--hard\", \"-q\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tif (run_command(&cp))\n> +\t\t\t\treturn -1;\n> +\t\t}\n> +\n> +\t\tif (keep_index == 1 && !is_null_oid(&info.i_tree)) {\n> +\t\t\tint i;\n> +\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t\t\tif (reset_tree(&info.i_tree, 0, 1))\n> +\t\t\t\treturn -1;\n> +\n> +\t\t\tcp1.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp1.args, \"ls-files\", \"-z\",\n> +\t\t\t\t\t \"--modified\", \"--\", NULL);\n> +\t\t\tfor (i = 0; i < ps.nr; ++i)\n> +\t\t\t\targv_array_push(&cp1.args, ps.items[i].match);\n> +\t\t\tif (pipe_command(&cp1, NULL, 0, &out, 0, NULL, 0))\n> +\t\t\t\treturn -1;\n> +\n> +\t\t\tcp2.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp2.args, \"checkout-index\", \"-z\",\n> +\t\t\t\t\t \"--force\", \"--stdin\", NULL);\n> +\t\t\tif (pipe_command(&cp2, out.buf, out.len, NULL, 0, NULL,\n> +\t\t\t\t\t 0))\n> +\t\t\t\treturn -1;\n> +\t\t}\n> +\t} else {\n> +\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\tcp.git_cmd = 1;\n> +\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n> +\n> +\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n> +\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n> +\t\t\treturn -1;\n> +\t\t}\n> +\n> +\t\tif (keep_index < 1) {\n> +\t\t\tint i;\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"reset\", \"-q\", \"--\", NULL);\n> +\t\t\tfor (i = 0; i < ps.nr; ++i)\n> +\t\t\t\targv_array_push(&cp.args, ps.items[i].match);\n> +\t\t\tif (run_command(&cp))\n> +\t\t\t\treturn -1;\n> +\t\t}\n> +\t}\n> +\treturn 0;\n> +}\n> +\n> +static int push_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint keep_index = -1;\n> +\tint patch_mode = 0;\n> +\tint include_untracked = 0;\n> +\tint quiet = 0;\n> +\tconst char *stash_msg = NULL;\n> +\tstruct option options[] = {\n> +\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n> +\t\t\tN_(\"keep index\"), 1),\n> +\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n> +\t\t\tN_(\"stash in patch mode\")),\n> +\t\tOPT_BOOL('q', \"quiet\", &quiet,\n> +\t\t\tN_(\"quiet mode\")),\n\nWe should be able to use OPT__QUIET here.\n\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n> +\t\t\t    N_(\"include ignore files\"), 2),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> +\t\t\t N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_push_usage,\n> +\t\t\t     0);\n> +\n> +\treturn do_push_stash(argc, argv, prefix, keep_index, patch_mode,\n> +\t\t\t     include_untracked, quiet, stash_msg);\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -1248,6 +1455,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!store_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"create\"))\n>  \t\treturn !!create_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"push\"))\n> +\t\treturn !!push_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index ab06e4ffb..c3146f62a 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -412,7 +412,8 @@ save)\n>  \t;;\n>  push)\n>  \tshift\n> -\tpush_stash \"$@\"\n> +\tcd \"$START_DIR\"\n> +\tgit stash--helper push \"$@\"\n>  \t;;\n>  apply)\n>  \tshift\n> @@ -448,7 +449,8 @@ branch)\n>  *)\n>  \tcase $# in\n>  \t0)\n> -\t\tpush_stash &&\n> +\t\tcd \"$START_DIR\"\n> +\t\tgit stash--helper push &&\n>  \t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n>  \t\t;;\n>  \t*)\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355989","messageId":"CADzBBBaO3em0kcebL5kiE1JxSEsea2OFrzyRUgfEDjrwV6fxsw@mail.gmail.com","threadId":"48222","inReplyTo":"20180815221305.GQ2734@hank.intra.tgummerer.com","subject":"Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin","fromName":"Paul Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-18T15:39:08Z","receivedAt":"2018-08-18T15:39:12Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"On Thu, Aug 16, 2018 at 1:13 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:\n> On 08/08, Paul-Sebastian Ungureanu wrote:\n>> Add stash create to the helper.\n>>\n>> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n>> ---\n>>  builtin/stash--helper.c | 406 ++++++++++++++++++++++++++++++++++++++++\n>>  git-stash.sh            |   2 +-\n>>  2 files changed, 407 insertions(+), 1 deletion(-)\n>>\n>> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n>> index 5ff810f8c..a4e57899b 100644\n>> --- a/builtin/stash--helper.c\n>> +++ b/builtin/stash--helper.c\n>> @@ -21,6 +21,7 @@ static const char * const git_stash_helper_usage[] = {\n>>       N_(\"git stash--helper branch <branchname> [<stash>]\"),\n>>       N_(\"git stash--helper clear\"),\n>>       N_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n>> +     N_(\"git stash--helper create [<message>]\"),\n>>       NULL\n>>  };\n>>\n>> @@ -64,6 +65,11 @@ static const char * const git_stash_helper_store_usage[] = {\n>>       NULL\n>>  };\n>>\n>> +static const char * const git_stash_helper_create_usage[] = {\n>> +     N_(\"git stash--helper create [<message>]\"),\n>> +     NULL\n>> +};\n>> +\n>>  static const char *ref_stash = \"refs/stash\";\n>>  static int quiet;\n>>  static struct strbuf stash_index_path = STRBUF_INIT;\n>> @@ -781,6 +787,404 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n>>       return do_store_stash(argv[0], stash_msg, quiet);\n>>  }\n>>\n>> [...]\n>>\n>> +\n>> +static int do_create_stash(int argc, const char **argv, const char *prefix,\n>> +                        const char **stash_msg, int include_untracked,\n>> +                        int patch_mode, struct stash_info *info)\n>> +{\n>> +     int untracked_commit_option = 0;\n>> +     int ret = 0;\n>> +     int subject_len;\n>> +     int flags;\n>> +     const char *head_short_sha1 = NULL;\n>> +     const char *branch_ref = NULL;\n>> +     const char *head_subject = NULL;\n>> +     const char *branch_name = \"(no branch)\";\n>> +     struct commit *head_commit = NULL;\n>> +     struct commit_list *parents = NULL;\n>> +     struct strbuf msg = STRBUF_INIT;\n>> +     struct strbuf commit_tree_label = STRBUF_INIT;\n>> +     struct strbuf out = STRBUF_INIT;\n>> +     struct strbuf final_stash_msg = STRBUF_INIT;\n>> +\n>> +     read_cache_preload(NULL);\n>> +     refresh_cache(REFRESH_QUIET);\n>> +\n>> +     if (!check_changes(argv, include_untracked, prefix)) {\n>> +             ret = 1;\n>> +             goto done;\n>\n> I wonder if we can just 'exit(0)' here, instead of returning.  This\n> whole command is a builtin, and I *think* outside of 'libgit.a' exiting\n> early is fine.  It does mean that we're not free'ing the memory\n> though, which means a leak checker would probably complain.  So\n> dunno.  It would simplify the code a little, but not sure it's worth it.\n\nIndeed, there shouldn't be any problem by calling exit(0).\n\n>> +     }\n>> +\n>> +     if (get_oid(\"HEAD\", &info->b_commit)) {\n>> +             fprintf_ln(stderr, \"You do not have the initial commit yet\");\n>> +             ret = -1;\n>> +             goto done;\n>> +     } else {\n>> +             head_commit = lookup_commit(the_repository, &info->b_commit);\n>> +     }\n>> +\n>> +     branch_ref = resolve_ref_unsafe(\"HEAD\", 0, NULL, &flags);\n>> +     if (flags & REF_ISSYMREF)\n>> +             branch_name = strrchr(branch_ref, '/') + 1;\n>> +     head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n>> +                                          DEFAULT_ABBREV);\n>> +     subject_len = find_commit_subject(get_commit_buffer(head_commit, NULL),\n>> +                                       &head_subject);\n>> +     strbuf_addf(&msg, \"%s: %s %.*s\\n\", branch_name, head_short_sha1,\n>> +                 subject_len, head_subject);\n>\n> I think this can be written in a slightly simpler way:\n>\n>         head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n>                                              DEFAULT_ABBREV);\n>         strbuf_addf(&msg, \"%s: %s\", branch_name, head_short_sha1);\n>         pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);\n>         strbuf_addch(&msg, '\\n');\n>\n> The other advantage this brings is that it is consistent with other\n> places where we print/use the subject of a commit (e.g. in 'git reset\n> --hard').\n\nThanks for this suggestion.\n\n>> +\n>> +     strbuf_addf(&commit_tree_label, \"index on %s\\n\", msg.buf);\n>> +     commit_list_insert(head_commit, &parents);\n>> +     if (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n>> +         commit_tree(commit_tree_label.buf, commit_tree_label.len,\n>> +                     &info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n>> +             fprintf_ln(stderr, \"Cannot save the current index state\");\n>\n> Looks like this message is translated in the current 'git stash'\n> implementation, so it should be here as well.  Same for the messages\n> below.\n>\n>> +             ret = -1;\n>> +             goto done;\n>> +     }\n>> +\n>> +     if (include_untracked && get_untracked_files(argv, 1,\n>> +                                                  include_untracked, &out)) {\n>> +             if (save_untracked_files(info, &msg, &out)) {\n>> +                     printf_ln(\"Cannot save the untracked files\");\n>\n> Why does this go to stdout, whereas \"Cannot save the current index\n> state\" above goes to stderr?  In the shell version of git stash these\n> all go to stderr fwiw.  There are a few similar cases, it would\n> probably be worth going through all the print statements here and see\n> if they need to be translated, and to which output stream they should\n> go.\n\nThis is what I am going to do.\n\n>> +                     ret = -1;\n>> +                     goto done;\n>> +             }\n>> +             untracked_commit_option = 1;\n>> +     }\n>> +     if (patch_mode) {\n>> +             ret = stash_patch(info, argv);\n>> +             if (ret < 0) {\n>> +                     printf_ln(\"Cannot save the current worktree state\");\n>> +                     goto done;\n>> +             } else if (ret > 0) {\n>> +                     goto done;\n>> +             }\n>> +     } else {\n>> +             if (stash_working_tree(info, argv)) {\n>> +                     printf_ln(\"Cannot save the current worktree state\");\n>> +                     ret = -1;\n>> +                     goto done;\n>> +             }\n>> +     }\n>> +\n>> +     if (!*stash_msg || !strlen(*stash_msg))\n>> +             strbuf_addf(&final_stash_msg, \"WIP on %s\", msg.buf);\n>> +     else\n>> +             strbuf_addf(&final_stash_msg, \"On %s: %s\\n\", branch_name,\n>> +                         *stash_msg);\n>> +     *stash_msg = strbuf_detach(&final_stash_msg, NULL);\n>\n> strbuf_detach means we're taking ownership of the memory, so we'll\n> have to free it afterwards. Looking at this we may not even want to\n> re-use the 'stash_msg' variable here, but instead introduce another\n> variable for it, so it doesn't have a dual meaning in this function.\n>\n>> +\n>> +     /*\n>> +      * `parents` will be empty after calling `commit_tree()`, so there is\n>> +      * no need to call `free_commit_list()`\n>> +      */\n>> +     parents = NULL;\n>> +     if (untracked_commit_option)\n>> +             commit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n>> +     commit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n>> +     commit_list_insert(head_commit, &parents);\n>> +\n>> +     if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n>> +                     parents, &info->w_commit, NULL, NULL)) {\n>> +             printf_ln(\"Cannot record working tree state\");\n>> +             ret = -1;\n>> +             goto done;\n>> +     }\n>> +\n>> +done:\n>> +     strbuf_release(&commit_tree_label);\n>> +     strbuf_release(&msg);\n>> +     strbuf_release(&out);\n>> +     strbuf_release(&final_stash_msg);\n>> +     return ret;\n>> +}\n>> +\n>> +static int create_stash(int argc, const char **argv, const char *prefix)\n>> +{\n>> +     int include_untracked = 0;\n>> +     int ret = 0;\n>> +     const char *stash_msg = NULL;\n>> +     struct stash_info info;\n>> +     struct option options[] = {\n>> +             OPT_BOOL('u', \"include-untracked\", &include_untracked,\n>> +                      N_(\"include untracked files in stash\")),\n>> +             OPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n>> +                      N_(\"stash message\")),\n>> +             OPT_END()\n>> +     };\n>> +\n>> +     argc = parse_options(argc, argv, prefix, options,\n>> +                          git_stash_helper_create_usage,\n>> +                          0);\n>> +\n>> +     ret = do_create_stash(argc, argv, prefix, &stash_msg,\n>> +                           include_untracked, 0, &info);\n>\n> stash_msg doesn't have to be passed as a pointer to a pointer here, as\n> we never need the modified value after this function returns.  I think\n> just passing 'stash_msg' here instead of '&stash_msg' will make\n> 'do_create_stash' slightly easier to read.\n\nThat's right, but `do_create_stash()` is also called by\n`do_push_stash()`, which will need the modified value.\n\n>> +\n>> +     if (!ret)\n>> +             printf_ln(\"%s\", oid_to_hex(&info.w_commit));\n>> +\n>> +     /*\n>> +      * ret can be 1 if there were no changes. In this case, we should\n>> +      * not error out.\n>> +      */\n>> +     return ret < 0;\n>> +}\n>> +\n>>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>>  {\n>>       pid_t pid = getpid();\n>> @@ -817,6 +1221,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>>               return !!show_stash(argc, argv, prefix);\n>>       else if (!strcmp(argv[0], \"store\"))\n>>               return !!store_stash(argc, argv, prefix);\n>> +     else if (!strcmp(argv[0], \"create\"))\n>> +             return !!create_stash(argc, argv, prefix);\n>>\n>>       usage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>>                     git_stash_helper_usage, options);\n>> diff --git a/git-stash.sh b/git-stash.sh\n>> index 5739c5152..ab06e4ffb 100755\n>> --- a/git-stash.sh\n>> +++ b/git-stash.sh\n>> @@ -425,7 +425,7 @@ clear)\n>>       ;;\n>>  create)\n>>       shift\n>> -     create_stash -m \"$*\" && echo \"$w_commit\"\n>> +     git stash--helper create --message \"$*\"\n>>       ;;\n>>  store)\n>>       shift\n>> --\n>> 2.18.0.573.g56500d98f\n>>\nThanks!\n"},{"id":"355990","messageId":"20180818154655.GC11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"c778a6aa99edd1ab58cfebc0976f1475f759611c.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 19/26] stash: make push to be quiet","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T15:46:55Z","receivedAt":"2018-08-18T15:47:01Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"> Subject: stash: make push to be quiet\n\nNit: maybe \"stash: make push -q quiet\"?  I think the subject should at\nleast mention the -q option.\n\nOn 08/08, Paul-Sebastian Ungureanu wrote:\n> There is a change in behaviour with this commit. When there was\n> no initial commit, the shell version of stash would still display\n> a message. This commit makes `push` to not display any message if\n> `--quiet` or `-q` is specified.\n\nYeah, not being quiet here cna be considered a bug, so this change in\nbehaviour makes sense.\n\nShould the \"No changes selected\" message in 'stash_patch' also be made\nquiet?\n\n> ---\n>  builtin/stash--helper.c | 41 +++++++++++++++++++++++++++--------------\n>  1 file changed, 27 insertions(+), 14 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index c26cad3d5..4fd79532c 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -1079,7 +1079,7 @@ static int stash_working_tree(struct stash_info *info,\n>  \n>  static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \t\t\t   const char **stash_msg, int include_untracked,\n> -\t\t\t   int patch_mode, struct stash_info *info)\n> +\t\t\t   int patch_mode, struct stash_info *info, int quiet)\n>  {\n>  \tint untracked_commit_option = 0;\n>  \tint ret = 0;\n> @@ -1105,7 +1105,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \t}\n>  \n>  \tif (get_oid(\"HEAD\", &info->b_commit)) {\n> -\t\tfprintf_ln(stderr, \"You do not have the initial commit yet\");\n> +\t\tif (!quiet)\n> +\t\t\tfprintf_ln(stderr, \"You do not have the initial commit yet\");\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t} else {\n> @@ -1127,7 +1128,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n>  \t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n>  \t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n> -\t\tfprintf_ln(stderr, \"Cannot save the current index state\");\n> +\t\tif (!quiet)\n> +\t\t\tfprintf_ln(stderr, \"Cannot save the current index state\");\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> @@ -1135,7 +1137,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \tif (include_untracked && get_untracked_files(argv, 1,\n>  \t\t\t\t\t\t     include_untracked, &out)) {\n>  \t\tif (save_untracked_files(info, &msg, &out)) {\n> -\t\t\tprintf_ln(\"Cannot save the untracked files\");\n> +\t\t\tif (!quiet)\n> +\t\t\t\tprintf_ln(\"Cannot save the untracked files\");\n>  \t\t\tret = -1;\n>  \t\t\tgoto done;\n>  \t\t}\n> @@ -1144,14 +1147,16 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \tif (patch_mode) {\n>  \t\tret = stash_patch(info, argv);\n>  \t\tif (ret < 0) {\n> -\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n> +\t\t\tif (!quiet)\n> +\t\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n>  \t\t\tgoto done;\n>  \t\t} else if (ret > 0) {\n>  \t\t\tgoto done;\n>  \t\t}\n>  \t} else {\n>  \t\tif (stash_working_tree(info, argv, prefix)) {\n> -\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n> +\t\t\tif (!quiet)\n> +\t\t\t\tprintf_ln(\"Cannot save the current worktree state\");\n>  \t\t\tret = -1;\n>  \t\t\tgoto done;\n>  \t\t}\n> @@ -1176,7 +1181,8 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \n>  \tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n>  \t\t\tparents, &info->w_commit, NULL, NULL)) {\n> -\t\tprintf_ln(\"Cannot record working tree state\");\n> +\t\tif (!quiet)\n> +\t\t\tprintf_ln(\"Cannot record working tree state\");\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> @@ -1208,7 +1214,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n>  \t\t\t     0);\n>  \n>  \tret = do_create_stash(argc, argv, prefix, &stash_msg,\n> -\t\t\t      include_untracked, 0, &info);\n> +\t\t\t      include_untracked, 0, &info, 0);\n>  \n>  \tif (!ret)\n>  \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n> @@ -1261,25 +1267,31 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n>  \t\treturn -1;\n>  \n>  \tif (!check_changes(argv, include_untracked, prefix)) {\n> -\t\tfprintf_ln(stdout, \"No local changes to save\");\n> +\t\tif (!quiet)\n> +\t\t\tfprintf_ln(stdout, \"No local changes to save\");\n>  \t\treturn 0;\n>  \t}\n>  \n>  \tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n> -\t\tfprintf_ln(stderr, \"Cannot initialize stash\");\n> +\t\tif (!quiet)\n> +\t\t\tfprintf_ln(stderr, \"Cannot initialize stash\");\n>  \t\treturn -1;\n>  \t}\n>  \n>  \tif ((ret = do_create_stash(argc, argv, prefix, &stash_msg,\n> -\t\t\t\t   include_untracked, patch_mode, &info)))\n> +\t\t\t\t   include_untracked, patch_mode, &info,\n> +\t\t\t\t   quiet)))\n>  \t\treturn ret;\n>  \n>  \tif (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {\n> -\t\tfprintf(stderr, \"Cannot save the current status\");\n> +\t\tif (!quiet)\n> +\t\t\tfprintf_ln(stderr, \"Cannot save the current status\");\n>  \t\treturn -1;\n>  \t}\n>  \n> -\tfprintf(stdout, \"Saved working directory and index state %s\", stash_msg);\n> +\tif (!quiet)\n> +\t\tfprintf(stdout, \"Saved working directory and index state %s\",\n> +\t\t\tstash_msg);\n>  \n>  \tif (!patch_mode) {\n>  \t\tif (include_untracked && ps.nr == 0) {\n> @@ -1367,7 +1379,8 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n>  \t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n>  \n>  \t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n> -\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n> +\t\t\tif (!quiet)\n> +\t\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n>  \t\t\treturn -1;\n>  \t\t}\n>  \n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"355995","messageId":"CACsJy8BSpVxXMKVTVOv0CMzi+zXLDNLxJ11HFL-cxTTX6Loz=w@mail.gmail.com","threadId":"48222","inReplyTo":"7ba9a8515d445d10af36a8a79071af51b90f5aef.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 05/26] stash: convert apply to builtin","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2018-08-18T16:09:15Z","receivedAt":"2018-08-18T16:09:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Aug 8, 2018 at 9:00 PM Paul-Sebastian Ungureanu\n<ungureanupaulsebastian@gmail.com> wrote:\n> +       strbuf_init(&info->revision, 0);\n> +       if (!commit) {\n> +               if (!ref_exists(ref_stash)) {\n> +                       free_stash_info(info);\n> +                       fprintf_ln(stderr, \"No stash entries found.\");\n\nMaybe _() ?\n-- \nDuy\n"},{"id":"355997","messageId":"20180818161257.GD11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"5ec3429340f43caad79e4213a7994738cb5d33c7.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 20/26] stash: add tests for `git stash push -q`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T16:12:57Z","receivedAt":"2018-08-18T16:13:03Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> This commit introduces more tests for the quiet option of\n> `git stash push`.\n\nI think this commit should be squashed into the previous one, so we\nhave implementation and tests in one commit.  That way it's easier to\nsee during review that there are tests for the change.  For more\ndiscussion on that also see [1].\n\n[1]: https://public-inbox.org/git/20180806144726.GB97564@aiede.svl.corp.google.com/\n\n> ---\n>  t/t3903-stash.sh | 21 +++++++++++++++++++++\n>  1 file changed, 21 insertions(+)\n> \n> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\n> index 8d002a7f2..b78db74ae 100755\n> --- a/t/t3903-stash.sh\n> +++ b/t/t3903-stash.sh\n> @@ -1064,6 +1064,27 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '\n>  \ttest_path_is_file untracked\n>  '\n>  \n> +test_expect_success 'push: -q is quiet with changes' '\n> +\t>foo &&\n> +\tgit stash push -q >output 2>&1 &&\n\nWe create an untracked file here and then call 'git stash push', which\nwill not create a new stash, as we don't use the --include-untracked\noption.  In fact, right now this test is doing the same thing as the\ntest below.  There should be a 'git add foo' above the 'git stash\npush' call to test what we're claiming to test here.\n\n> +\ttest_must_be_empty output\n> +'\n> +\n> +test_expect_success 'push: -q is quiet with no changes' '\n> +\tgit stash push -q >output 2>&1 &&\n> +\ttest_must_be_empty output\n> +'\n> +\n> +test_expect_success 'push: -q is quiet even if there is no initial commit' '\n> +\tgit init foo_dir &&\n> +\tcd foo_dir &&\n> +\ttouch bar &&\n\nThe typical style in the test suite for creating a new file is to use\n'>bar', unless you care about the 'mtime' the file has.  We don't seem\nto care about that in this test, so avoiding 'touch' would be better.\n\n> +\ttest_must_fail git stash push -q >output 2>&1 &&\n> +\ttest_must_be_empty output &&\n> +\tcd .. &&\n\nThe above should be in a subshell, i.e.\n\n    (\n        cd foo_dir &&\n\ttouch bar &&\n        test_must_fail git stash push -q >output 2>&1 &&\n        test_must_be_empty output &&\n    )\n\nthen you don't have to do the 'cd ..' in the end.  With the 'cd ..' in\nthe end, if one of the commands between the 'cd foo_dir' and 'cd ..'\nfails, all subsequent tests will be run inside of 'foo_dir', which\nputs them in a different environment than they expect.  That can cause\nall kinds of weirdness.\n\nIf inside a subshell, the current working directory of the parent\nshell is unaffected, so we don't have to worry about cd'ing back, and\nsubsequent tests will get the correct cwd even if things go wrong in\nthis test.\n\t\n\n> +\trm -rf foo_dir\n\nWe'll want to run this cleanup to run even if the test fails.  To do\nso, the 'test_when_finished' helper can be used.  Using that, this\nwould go at the top of the test, as 'test_when_finished rm -rf\nfoo_dir'.  Otherwise if any of the commands above fail, 'foo_dir' will\nnot be removed, and may interfere with subsequent tests.\n\n> +'\n> +\n>  test_expect_success 'untracked files are left in place when -u is not given' '\n>  \t>file &&\n>  \tgit add file &&\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"356001","messageId":"20180818163325.GE11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"d39aca1880ccf94f9f207cd09b4a5cc2c3c233e7.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 22/26] stash: convert save to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T16:33:25Z","receivedAt":"2018-08-18T16:33:31Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Add stash save to the helper and delete functions which are no\n> longer needed (`show_help()`, `save_stash()`, `push_stash()`,\n> `create_stash()`, `clear_stash()`, `untracked_files()` and\n> `no_changes()`).\n> ---\n>  builtin/stash--helper.c |  48 +++++++\n>  git-stash.sh            | 311 +---------------------------------------\n>  2 files changed, 50 insertions(+), 309 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 5c27f5dcf..f54a476e3 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -26,6 +26,8 @@ static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n>  \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n>  \t   \"          [--] [<pathspec>...]]\"),\n> +\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n> +\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n>  \tNULL\n>  };\n>  \n> @@ -81,6 +83,12 @@ static const char * const git_stash_helper_push_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_save_usage[] = {\n> +\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n> +\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n> +\tNULL\n> +};\n> +\n>  static const char *ref_stash = \"refs/stash\";\n>  static int quiet;\n>  static struct strbuf stash_index_path = STRBUF_INIT;\n> @@ -1445,6 +1453,44 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n>  \t\t\t     include_untracked, quiet, stash_msg);\n>  }\n>  \n> +static int save_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint i;\n> +\tint keep_index = -1;\n> +\tint patch_mode = 0;\n> +\tint include_untracked = 0;\n> +\tint quiet = 0;\n> +\tchar *stash_msg = NULL;\n> +\tstruct strbuf alt_stash_msg = STRBUF_INIT;\n> +\tstruct option options[] = {\n> +\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n> +\t\t\tN_(\"keep index\"), 1),\n> +\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n> +\t\t\tN_(\"stash in patch mode\")),\n> +\t\tOPT_BOOL('q', \"quiet\", &quiet,\n> +\t\t\tN_(\"quiet mode\")),\n\nThis could be OPT__QUIET again as mentioned in the previous patch as\nwell.\n\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n> +\t\t\t    N_(\"include ignore files\"), 2),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> +\t\t\t N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_save_usage,\n> +\t\t\t     0);\n> +\n> +\tfor (i = 0; i < argc; ++i)\n> +\t\tstrbuf_addf(&alt_stash_msg, \"%s \", argv[i]);\n> +\n> +\tstash_msg = strbuf_detach(&alt_stash_msg, NULL);\n\nWe unconditionally overwrite 'stash_msg' here, even if a '-m'\nparameter was given earlier, which I don't think is what we intended\nhere.  I think we started \"supporting\" (not erroring out on rather)\nthe '-m' flag accidentally (my bad, sorry) in 'git stash save' rather\nthan intentionally, and indeed it has the same behaviour as the code\nabove.\n\nHowever I think we should just not add support the '-m' flag here, as\nit doesn't make a lot of sense to have two ways of passing a message.\n\nWe also never free the memory we get back here from 'strbuf_detach'.\nAs this is not code in 'libgit.a' that's probably fine, and we can\njust add an 'UNLEAK(stash_msg)' here I think.\n\nIt may generally be interesting to consider using a leak checker, and\nsee how far we can get in making this leak free.  It may not be\npossible to make 'git stash' completely leak free, as the underlying\nAPIs may not be, but it may be interesting to see how far we can get\nfor the new code only.\n\n> +\n> +\treturn do_push_stash(0, NULL, prefix, keep_index, patch_mode,\n> +\t\t\t     include_untracked, quiet, stash_msg);\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -1485,6 +1531,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!create_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"push\"))\n>  \t\treturn !!push_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"save\"))\n> +\t\treturn !!save_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n>\n> [...]\n"},{"id":"356002","messageId":"20180818165102.GF11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"c659174b1be950072580ee176d59f85574cdb96c.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 23/26] stash: convert `stash--helper.c` into `stash.c`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T16:51:02Z","receivedAt":"2018-08-18T16:51:07Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> The old shell script `git-stash.sh`  was removed and replaced\n> entirely by `builtin/stash.c`. In order to do that, `create` and\n> `push` were adapted to work without `stash.sh`. For example, before\n> this commit, `git stash create` called `git stash--helper create\n> --message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\n> some of these changes wouldn't have been necessary.\n> \n> This commit also removes the word `helper` since now stash is\n> called directly and not by a shell script.\n> ---\n>  .gitignore                           |   1 -\n>  Makefile                             |   3 +-\n>  builtin.h                            |   2 +-\n>  builtin/{stash--helper.c => stash.c} | 132 ++++++++++++-----------\n>  git-stash.sh                         | 153 ---------------------------\n>  git.c                                |   2 +-\n>  6 files changed, 74 insertions(+), 219 deletions(-)\n>  rename builtin/{stash--helper.c => stash.c} (91%)\n>  delete mode 100755 git-stash.sh\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash.c\n> similarity index 91%\n> rename from builtin/stash--helper.c\n> rename to builtin/stash.c\n> index f54a476e3..0ef88408a 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash.c\n>\n> [...]\n>\n> @@ -1445,9 +1448,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n>  \t\tOPT_END()\n>  \t};\n>  \n> -\targc = parse_options(argc, argv, prefix, options,\n> -\t\t\t     git_stash_helper_push_usage,\n> -\t\t\t     0);\n> +\tif (argc)\n> +\t\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t\t     git_stash_push_usage,\n> +\t\t\t\t     0);\n\nThis change is a bit surprising here.  Why is this necessary?  I\nthought parse_options would handle no arguments just fine?\n\n>  \treturn do_push_stash(argc, argv, prefix, keep_index, patch_mode,\n>  \t\t\t     include_untracked, quiet, stash_msg);\n> @@ -1479,7 +1483,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n>  \t};\n>  \n>  \targc = parse_options(argc, argv, prefix, options,\n> -\t\t\t     git_stash_helper_save_usage,\n> +\t\t\t     git_stash_save_usage,\n>  \t\t\t     0);\n>  \n>  \tfor (i = 0; i < argc; ++i)\n> @@ -1491,7 +1495,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n>  \t\t\t     include_untracked, quiet, stash_msg);\n>  }\n>  \n> -int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n> +int cmd_stash(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n>  \tconst char *index_file;\n> @@ -1502,16 +1506,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \n>  \tgit_config(git_default_config, NULL);\n>  \n> -\targc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,\n> +\targc = parse_options(argc, argv, prefix, options, git_stash_usage,\n>  \t\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n>  \n>  \tindex_file = get_index_file();\n>  \tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n>  \t\t    (uintmax_t)pid);\n>  \n> -\tif (argc < 1)\n> -\t\tusage_with_options(git_stash_helper_usage, options);\n> -\tif (!strcmp(argv[0], \"apply\"))\n> +\tif (argc == 0)\n> +\t\treturn !!push_stash(0, NULL, prefix);\n> +\telse if (!strcmp(argv[0], \"apply\"))\n>  \t\treturn !!apply_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"clear\"))\n>  \t\treturn !!clear_stash(argc, argv, prefix);\n> @@ -1533,7 +1537,13 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!push_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"save\"))\n>  \t\treturn !!save_stash(argc, argv, prefix);\n> +\tif (*argv[0] == '-') {\n> +\t\tstruct argv_array args = ARGV_ARRAY_INIT;\n> +\t\targv_array_push(&args, \"push\");\n> +\t\targv_array_pushv(&args, argv);\n> +\t\treturn !!push_stash(args.argc, args.argv, prefix);\n> +\t}\n\nThis is a bit different than what the current code does.  Currently\nthe rules for when a plain 'git stash' becomes 'git stash push' are\nthe following:\n\n- If there are no arguments.\n- If all arguments are option arguments.\n- If the first argument of 'git stash' is '-p'.\n- If the first argument of 'git stash' is '--'.\n\nThis is to avoid someone typing 'git stash -q drop' for example, and\nthen being surprised that a new stash was created instead of an old\none being dropped, which what we have above would do.\n\nFor more reasoning about these aliasing rules see also the thread at [1].\n\n[1]: https://public-inbox.org/git/20170213200950.m3bcyp52wd25p737@sigill.intra.peff.net/\n\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n> -\t\t      git_stash_helper_usage, options);\n> +\t\t      git_stash_usage, options);\n>  }\n> diff --git a/git-stash.sh b/git-stash.sh\n> deleted file mode 100755\n> index 695f1feba..000000000\n> --- a/git-stash.sh\n> +++ /dev/null\n> @@ -1,153 +0,0 @@\n>\n> [...]\n"},{"id":"356004","messageId":"20180818202341.GS2734@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"CADzBBBaO3em0kcebL5kiE1JxSEsea2OFrzyRUgfEDjrwV6fxsw@mail.gmail.com","subject":"Re: [GSoC][PATCH v7 15/26] stash: convert create to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T20:23:41Z","receivedAt":"2018-08-18T20:23:47Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/18, Paul Sebastian Ungureanu wrote:\n> On Thu, Aug 16, 2018 at 1:13 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:\n> > On 08/08, Paul-Sebastian Ungureanu wrote:\n> >>\n> >>  [...]\n> >>\n> >> +                     ret = -1;\n> >> +                     goto done;\n> >> +             }\n> >> +             untracked_commit_option = 1;\n> >> +     }\n> >> +     if (patch_mode) {\n> >> +             ret = stash_patch(info, argv);\n> >> +             if (ret < 0) {\n> >> +                     printf_ln(\"Cannot save the current worktree state\");\n> >> +                     goto done;\n> >> +             } else if (ret > 0) {\n> >> +                     goto done;\n> >> +             }\n> >> +     } else {\n> >> +             if (stash_working_tree(info, argv)) {\n> >> +                     printf_ln(\"Cannot save the current worktree state\");\n> >> +                     ret = -1;\n> >> +                     goto done;\n> >> +             }\n> >> +     }\n> >> +\n> >> +     if (!*stash_msg || !strlen(*stash_msg))\n> >> +             strbuf_addf(&final_stash_msg, \"WIP on %s\", msg.buf);\n> >> +     else\n> >> +             strbuf_addf(&final_stash_msg, \"On %s: %s\\n\", branch_name,\n> >> +                         *stash_msg);\n> >> +     *stash_msg = strbuf_detach(&final_stash_msg, NULL);\n> >\n> > strbuf_detach means we're taking ownership of the memory, so we'll\n> > have to free it afterwards. Looking at this we may not even want to\n> > re-use the 'stash_msg' variable here, but instead introduce another\n> > variable for it, so it doesn't have a dual meaning in this function.\n> >\n> >> +\n> >> +     /*\n> >> +      * `parents` will be empty after calling `commit_tree()`, so there is\n> >> +      * no need to call `free_commit_list()`\n> >> +      */\n> >> +     parents = NULL;\n> >> +     if (untracked_commit_option)\n> >> +             commit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n> >> +     commit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n> >> +     commit_list_insert(head_commit, &parents);\n> >> +\n> >> +     if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n> >> +                     parents, &info->w_commit, NULL, NULL)) {\n> >> +             printf_ln(\"Cannot record working tree state\");\n> >> +             ret = -1;\n> >> +             goto done;\n> >> +     }\n> >> +\n> >> +done:\n> >> +     strbuf_release(&commit_tree_label);\n> >> +     strbuf_release(&msg);\n> >> +     strbuf_release(&out);\n> >> +     strbuf_release(&final_stash_msg);\n> >> +     return ret;\n> >> +}\n> >> +\n> >> +static int create_stash(int argc, const char **argv, const char *prefix)\n> >> +{\n> >> +     int include_untracked = 0;\n> >> +     int ret = 0;\n> >> +     const char *stash_msg = NULL;\n> >> +     struct stash_info info;\n> >> +     struct option options[] = {\n> >> +             OPT_BOOL('u', \"include-untracked\", &include_untracked,\n> >> +                      N_(\"include untracked files in stash\")),\n> >> +             OPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> >> +                      N_(\"stash message\")),\n> >> +             OPT_END()\n> >> +     };\n> >> +\n> >> +     argc = parse_options(argc, argv, prefix, options,\n> >> +                          git_stash_helper_create_usage,\n> >> +                          0);\n> >> +\n> >> +     ret = do_create_stash(argc, argv, prefix, &stash_msg,\n> >> +                           include_untracked, 0, &info);\n> >\n> > stash_msg doesn't have to be passed as a pointer to a pointer here, as\n> > we never need the modified value after this function returns.  I think\n> > just passing 'stash_msg' here instead of '&stash_msg' will make\n> > 'do_create_stash' slightly easier to read.\n> \n> That's right, but `do_create_stash()` is also called by\n> `do_push_stash()`, which will need the modified value.\n\nAh right, I didn't read that far yet when leaving this comment :)\n\nReading the original push_stash again though, do we actually need the\nmodified value in 'do_push_stash()'?  The original lines are:\n\n\tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n\tstore_stash -m \"$stash_msg\" -q $w_commit ||\n\tdie \"$(gettext \"Cannot save the current status\")\"\n\n'$stash_msg' gets passed in to 'create_stash()', but is the\n'stash_msg' variable inside of 'create_stash()' is local and only the\nlocal copy is modified, so 'store_stash()' would still get the\noriginal.  Or am I missing something?\n"},{"id":"356007","messageId":"20180818210734.GG11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"0683dfe7cf0528c0288b57914e7626b5c7108909.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 16/26] stash: replace spawning a \"read-tree\" process","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T21:07:34Z","receivedAt":"2018-08-18T21:07:40Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> Instead of spawning a child process, make use of `reset_tree()`\n> function already implemented in `stash-helper.c`.\n> ---\n>  builtin/stash--helper.c | 9 +++------\n>  1 file changed, 3 insertions(+), 6 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index a4e57899b..887b78d05 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -984,21 +984,18 @@ static int stash_patch(struct stash_info *info, const char **argv)\n>  static int stash_working_tree(struct stash_info *info, const char **argv)\n>  {\n>  \tint ret = 0;\n> -\tstruct child_process cp0 = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp1 = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp3 = CHILD_PROCESS_INIT;\n>  \tstruct strbuf out1 = STRBUF_INIT;\n>  \tstruct strbuf out3 = STRBUF_INIT;\n>  \n> -\tcp0.git_cmd = 1;\n> -\targv_array_push(&cp0.args, \"read-tree\");\n> -\targv_array_pushf(&cp0.args, \"--index-output=%s\", stash_index_path.buf);\n> -\targv_array_pushl(&cp0.args, \"-m\", oid_to_hex(&info->i_tree), NULL);\n> -\tif (run_command(&cp0)) {\n> +\tset_alternate_index_output(stash_index_path.buf);\n> +\tif (reset_tree(&info->i_tree, 0, 0)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> +\tset_alternate_index_output(\".git/index\");\n\nI think this second 'set_alternate_index_output()' should be\n'set_alternate_index_output(NULL)', which has slightly different\nsemantics than setting it to '.git/index'.  Having it set means that\nthe index is written unconditionally even if it is not set.\n\nAlso the index file could be something other than \".git/index\", if\nthe GIT_INDEX_FILE environment variable is set, so it should be\nreplaced with 'get_index_file()' if we were to keep this.\n\nI was also wondering if we could avoid writing a temporary index to\ndisk, in 'stash_working_tree', but I don't see an easy way for doing\nthat.\n\n>  \n>  \tcp1.git_cmd = 1;\n>  \targv_array_pushl(&cp1.args, \"diff-index\", \"--name-only\", \"-z\",\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"356011","messageId":"20180818220644.GH11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"ec5b2c9ac9e0e58710bdc385e03a01e2a7fd10bd.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 17/26] stash: avoid spawning a \"diff-index\" process","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T22:06:44Z","receivedAt":"2018-08-18T22:06:49Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> This commits replaces spawning `diff-index` child process by using\n> the already existing `diff` API\n\nI think this should be squashed into the previous commit.  It's easier\nto review a commit that replaces all the 'run_command'/'pipe_command'\ncalls in one function, rather than doing it call by call, especially\nif they interact with eachother.\n\nE.g. I was going to suggest replacing the 'write_tree' call as well,\nbut reading ahead in the series I see that that's already being done :)  \n\nWhile replacing all the calls of one type with the internal API call\nis probably easiest for writing the patches, at least I would find it\neasier to review replacing the run-command API calls in one codepath\nat a time.\n\n> ---\n>  builtin/stash--helper.c | 56 ++++++++++++++++++++++++++++++-----------\n>  1 file changed, 42 insertions(+), 14 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 887b78d05..f905d3908 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -12,6 +12,7 @@\n>  #include \"rerere.h\"\n>  #include \"revision.h\"\n>  #include \"log-tree.h\"\n> +#include \"diffcore.h\"\n>  \n>  static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper list [<options>]\"),\n> @@ -297,6 +298,18 @@ static int reset_head(const char *prefix)\n>  \treturn run_command(&cp);\n>  }\n>  \n> +static void add_diff_to_buf(struct diff_queue_struct *q,\n> +\t\t\t    struct diff_options *options,\n> +\t\t\t    void *data)\n> +{\n> +\tint i;\n> +\tfor (i = 0; i < q->nr; i++) {\n> +\t\tstruct diff_filepair *p = q->queue[i];\n> +\t\tstrbuf_addstr(data, p->one->path);\n> +\t\tstrbuf_addch(data, '\\n');\n\nWhat about filenames that include a '\\n'?  I think this in combination\nwith removing the '-z' flag from the 'update-index' call will break\nwith filenames that have a LF in them.  This should be a '\\0' instead\nof a '\\n', and we should still be using the '-z' flag in\n'update-index'.\n\n> +\t}\n> +}\n> +\n>  static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)\n>  {\n>  \tstruct child_process cp = CHILD_PROCESS_INIT;\n> @@ -981,14 +994,16 @@ static int stash_patch(struct stash_info *info, const char **argv)\n>  \treturn ret;\n>  }\n>  \n> -static int stash_working_tree(struct stash_info *info, const char **argv)\n> +static int stash_working_tree(struct stash_info *info,\n> +\t\t\t      const char **argv, const char *prefix)\n>  {\n>  \tint ret = 0;\n> -\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp3 = CHILD_PROCESS_INIT;\n> -\tstruct strbuf out1 = STRBUF_INIT;\n>  \tstruct strbuf out3 = STRBUF_INIT;\n\nWe're left with cp{2,3} and out3 here, which is a bit weird.  Then\nagain renaming them in this patch adds more churn, making it harder to\nreview.  Maybe instead of numbering them it would be better to name\nthem after the child process they are calling?  e.g. 'cp1' could\nbecome 'cp_di', and so on?\n\n> +\tstruct argv_array args = ARGV_ARRAY_INIT;\n> +\tstruct strbuf diff_output = STRBUF_INIT;\n> +\tstruct rev_info rev;\n>  \n>  \tset_alternate_index_output(stash_index_path.buf);\n>  \tif (reset_tree(&info->i_tree, 0, 0)) {\n> @@ -997,26 +1012,36 @@ static int stash_working_tree(struct stash_info *info, const char **argv)\n>  \t}\n>  \tset_alternate_index_output(\".git/index\");\n>  \n> -\tcp1.git_cmd = 1;\n> -\targv_array_pushl(&cp1.args, \"diff-index\", \"--name-only\", \"-z\",\n> -\t\t\t\"HEAD\", \"--\", NULL);\n> +\targv_array_push(&args, \"dummy\");\n\nNot being familiar with the setup_revisions code, I had to dig a bit\nto figure out why this makes sense.  This is a dummy replacement for\nargv[0] in normal operation.  In retrospect it's kind of obvious, but\nmaybe call \"dummy\" \"fake_argv0\" instead, to help nudge future readers\nin the right direction?\n\n>  \tif (argv)\n> -\t\targv_array_pushv(&cp1.args, argv);\n> -\targv_array_pushf(&cp1.env_array, \"GIT_INDEX_FILE=%s\",\n> -\t\t\t stash_index_path.buf);\n> +\t\targv_array_pushv(&args, argv);\n> +\tgit_config(git_diff_basic_config, NULL);\n> +\tinit_revisions(&rev, prefix);\n> +\targs.argc = setup_revisions(args.argc, args.argv, &rev, NULL);\n> +\n> +\trev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;\n> +\trev.diffopt.format_callback = add_diff_to_buf;\n> +\trev.diffopt.format_callback_data = &diff_output;\n> +\n> +\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n>  \n> -\tif (pipe_command(&cp1, NULL, 0, &out1, 0, NULL, 0)) {\n> +\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit), \"\");\n> +\tif (run_diff_index(&rev, 0)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n>  \n>  \tcp2.git_cmd = 1;\n> -\targv_array_pushl(&cp2.args, \"update-index\", \"-z\", \"--add\",\n> +\targv_array_pushl(&cp2.args, \"update-index\", \"--add\",\n>  \t\t\t \"--remove\", \"--stdin\", NULL);\n>  \targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n>  \t\t\t stash_index_path.buf);\n>  \n> -\tif (pipe_command(&cp2, out1.buf, out1.len, NULL, 0, NULL, 0)) {\n> +\tif (pipe_command(&cp2, diff_output.buf, diff_output.len,\n> +\t\t\t NULL, 0, NULL, 0)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> @@ -1033,8 +1058,11 @@ static int stash_working_tree(struct stash_info *info, const char **argv)\n>  \tget_oid_hex(out3.buf, &info->w_tree);\n>  \n>  done:\n> -\tstrbuf_release(&out1);\n> +\tUNLEAK(rev);\n>  \tstrbuf_release(&out3);\n> +\targv_array_clear(&args);\n> +\tobject_array_clear(&rev.pending);\n> +\tstrbuf_release(&diff_output);\n>  \tremove_path(stash_index_path.buf);\n>  \treturn ret;\n>  }\n> @@ -1112,7 +1140,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \t\t\tgoto done;\n>  \t\t}\n>  \t} else {\n> -\t\tif (stash_working_tree(info, argv)) {\n> +\t\tif (stash_working_tree(info, argv, prefix)) {\n>  \t\t\tprintf_ln(\"Cannot save the current worktree state\");\n>  \t\t\tret = -1;\n>  \t\t\tgoto done;\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"356013","messageId":"20180818221720.GI11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"3286e8443ed1c8f9560aa6bef01aa18ed78266df.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 21/26] stash: replace spawning `git ls-files` child process","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T22:17:20Z","receivedAt":"2018-08-18T22:17:59Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> This commit replaces spawning `git ls-files` child process with\n> API calls to get the untracked files.\n> ---\n>  builtin/stash--helper.c | 49 +++++++++++++++++++++++++++--------------\n>  1 file changed, 32 insertions(+), 17 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 4fd79532c..5c27f5dcf 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -813,27 +813,42 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n>  /*\n>   * `out` will be filled with the names of untracked files. The return value is:\n>   *\n> - * < 0 if there was a bug (any arg given outside the repo will be detected\n> - *     by `setup_revision()`)\n>   * = 0 if there are not any untracked files\n>   * > 0 if there are untracked files\n>   */\n> -static int get_untracked_files(const char **argv, int line_term,\n> +static int get_untracked_files(const char **argv, const char *prefix,\n>  \t\t\t       int include_untracked, struct strbuf *out)\n>  {\n> -\tstruct child_process cp = CHILD_PROCESS_INIT;\n> -\tcp.git_cmd = 1;\n> -\targv_array_pushl(&cp.args, \"ls-files\", \"-o\", NULL);\n> -\tif (line_term)\n> -\t\targv_array_push(&cp.args, \"-z\");\n> +\tint max_len;\n> +\tint i;\n> +\tchar *seen;\n> +\tstruct dir_struct dir;\n> +\tstruct pathspec pathspec;\n> +\n> +\tmemset(&dir, 0, sizeof(dir));\n>  \tif (include_untracked != 2)\n> -\t\targv_array_push(&cp.args, \"--exclude-standard\");\n> -\targv_array_push(&cp.args, \"--\");\n> -\tif (argv)\n> -\t\targv_array_pushv(&cp.args, argv);\n> +\t\tsetup_standard_excludes(&dir);\n>  \n> -\tif (pipe_command(&cp, NULL, 0, out, 0, NULL, 0))\n> -\t\treturn -1;\n> +\tparse_pathspec(&pathspec, 0,\n> +\t\t       PATHSPEC_PREFER_FULL,\n> +\t\t       prefix, argv);\n> +\tseen = xcalloc(pathspec.nr, 1);\n> +\n> +\tmax_len = fill_directory(&dir, the_repository->index, &pathspec);\n> +\tfor (i = 0; i < dir.nr; i++) {\n> +\t\tstruct dir_entry *ent = dir.entries[i];\n> +\t\tif (!dir_path_match(ent, &pathspec, max_len, seen)) {\n> +\t\t\tfree(ent);\n> +\t\t\tcontinue;\n> +\t\t}\n> +\t\tstrbuf_addf(out, \"%s\\n\", ent->name);\n\nAs mentioned in my comments about the 'diff-index' replacement, this\n'\\n' should probably be '\\0', and we should keep the '-z' flag in 'git\nupdate-index', in case somebody has a '\\n' in their filenames.\n\nWhile creating such a file is probably not a good idea anyway, we\nshould still be able to handle it (and have been before this, so we\nshouldn't break it).\n\n> +\t\tfree(ent);\n> +\t}\n> +\n> +\tfree(dir.entries);\n> +\tfree(dir.ignored);\n> +\tclear_directory(&dir);\n> +\tfree(seen);\n>  \treturn out->len;\n>  }\n>  \n> @@ -888,7 +903,7 @@ static int check_changes(const char **argv, int include_untracked,\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tif (include_untracked && get_untracked_files(argv, 0,\n> +\tif (include_untracked && get_untracked_files(argv, prefix,\n>  \t\t\t\t\t\t     include_untracked, &out))\n>  \t\tret = 1;\n>  \n> @@ -908,7 +923,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n>  \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n>  \n>  \tcp.git_cmd = 1;\n> -\targv_array_pushl(&cp.args, \"update-index\", \"-z\", \"--add\",\n> +\targv_array_pushl(&cp.args, \"update-index\", \"--add\",\n>  \t\t\t \"--remove\", \"--stdin\", NULL);\n>  \targv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\",\n>  \t\t\t stash_index_path.buf);\n> @@ -1134,7 +1149,7 @@ static int do_create_stash(int argc, const char **argv, const char *prefix,\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tif (include_untracked && get_untracked_files(argv, 1,\n> +\tif (include_untracked && get_untracked_files(argv, prefix,\n>  \t\t\t\t\t\t     include_untracked, &out)) {\n>  \t\tif (save_untracked_files(info, &msg, &out)) {\n>  \t\t\tif (!quiet)\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"356016","messageId":"20180818223329.GJ11326@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"9bde85eacb94db54763aab35efe287c5af7c8316.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 24/26] stash: optimize `get_untracked_files()` and `check_changes()`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-18T22:33:29Z","receivedAt":"2018-08-18T22:33:50Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> This commits introduces a optimization by avoiding calling the\n> same functions again. For example, `git stash push -u`\n> would call at some points the following functions:\n> \n>  * `check_changes()`\n>  * `do_create_stash()`, which calls: `check_changes()` and\n> `get_untracked_files()`\n> \n> Note that `check_changes()` also calls `get_untracked_files()`.\n> So, `check_changes()` is called 2 times and `get_untracked_files()`\n> 3 times. By checking at the beginning of the function if we already\n> performed a check, we can avoid making useless calls.\n\nWhile I can see that this may give us some performance gains, what's\nbeing described above sounds like we should look into why we are\nmaking these duplicate calls in the first place, rather than trying to\nreturn early from them.  I feel like the duplicate calls are mostly a\nremnant from the way the shell script was written, but not inherent to\nthe design of 'git stash'. \n\nFor example 'check_changes' could be called from 'create_stash'\ndirectly, so we don't have to call it in 'do_create_stash', in the\nprocess removing the duplicate call from the 'git stash push'\ncodepath.  That would provide the same improvements, and keep the code\ncleaner, rather than introducing more special cases for these\nfunctions.\n\nI haven't looked into the 'get_untracked_files()' call chain yet, but\nI imagine we can do something similar for that.\n"},{"id":"356029","messageId":"20180819081747.GE13316@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"3b4a3b766598589df30e16cddf175f039eb4e290.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 25/26] stash: replace all `write-tree` child processes with API calls","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-19T08:17:47Z","receivedAt":"2018-08-19T08:17:55Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> This commit replaces spawning `git write-tree` with API calls.\n> ---\n>  builtin/stash.c | 40 ++++++++++++----------------------------\n>  1 file changed, 12 insertions(+), 28 deletions(-)\n\nNice reduction in lines here!\n\n> \n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index 4d5c0d16e..46e76a34e 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -949,9 +949,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n>  {\n>  \tint ret = 0;\n>  \tstruct strbuf untracked_msg = STRBUF_INIT;\n> -\tstruct strbuf out2 = STRBUF_INIT;\n>  \tstruct child_process cp = CHILD_PROCESS_INIT;\n> -\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n> +\tstruct index_state state = { NULL };\n\nWe often call this 'istate' throughout the codebase.  Would be nice to\ncall it that here as well, to reduce the cognitive load for people\nalready familiar with the codebase.\n\n>  \n>  \tcp.git_cmd = 1;\n>  \targv_array_pushl(&cp.args, \"update-index\", \"--add\",\n> @@ -966,15 +965,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tcp2.git_cmd = 1;\n> -\targv_array_push(&cp2.args, \"write-tree\");\n> -\targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n> -\t\t\t stash_index_path.buf);\n> -\tif (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {\n> +\tif (write_index_as_tree(&info->u_tree, &state, stash_index_path.buf, 0,\n> +\t\t\t\tNULL)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> -\tget_oid_hex(out2.buf, &info->u_tree);\n>  \n>  \tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n>  \t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n> @@ -984,7 +979,6 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n>  \n>  done:\n>  \tstrbuf_release(&untracked_msg);\n> -\tstrbuf_release(&out2);\n>  \tremove_path(stash_index_path.buf);\n>  \treturn ret;\n>  }\n> @@ -994,11 +988,10 @@ static struct strbuf patch = STRBUF_INIT;\n>  static int stash_patch(struct stash_info *info, const char **argv)\n>  {\n>  \tint ret = 0;\n> -\tstruct strbuf out2 = STRBUF_INIT;\n>  \tstruct child_process cp0 = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp1 = CHILD_PROCESS_INIT;\n> -\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp3 = CHILD_PROCESS_INIT;\n> +\tstruct index_state state = { NULL };\n>  \n>  \tremove_path(stash_index_path.buf);\n>  \n> @@ -1023,17 +1016,12 @@ static int stash_patch(struct stash_info *info, const char **argv)\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tcp2.git_cmd = 1;\n> -\targv_array_push(&cp2.args, \"write-tree\");\n> -\targv_array_pushf(&cp2.env_array, \"GIT_INDEX_FILE=%s\",\n> -\t\t\t stash_index_path.buf);\n> -\tif (pipe_command(&cp2, NULL, 0, &out2, 0,NULL, 0)) {\n> +\tif (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,\n> +\t\t\t\tNULL)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tget_oid_hex(out2.buf, &info->w_tree);\n> -\n>  \tcp3.git_cmd = 1;\n>  \targv_array_pushl(&cp3.args, \"diff-tree\", \"-p\", \"HEAD\",\n>  \t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n> @@ -1046,7 +1034,6 @@ static int stash_patch(struct stash_info *info, const char **argv)\n>  \t}\n>  \n>  done:\n> -\tstrbuf_release(&out2);\n>  \tremove_path(stash_index_path.buf);\n>  \treturn ret;\n>  }\n> @@ -1056,11 +1043,10 @@ static int stash_working_tree(struct stash_info *info,\n>  {\n>  \tint ret = 0;\n>  \tstruct child_process cp2 = CHILD_PROCESS_INIT;\n> -\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n> -\tstruct strbuf out3 = STRBUF_INIT;\n>  \tstruct argv_array args = ARGV_ARRAY_INIT;\n>  \tstruct strbuf diff_output = STRBUF_INIT;\n>  \tstruct rev_info rev;\n> +\tstruct index_state state = { NULL };\n>  \n>  \tset_alternate_index_output(stash_index_path.buf);\n>  \tif (reset_tree(&info->i_tree, 0, 0)) {\n> @@ -1103,20 +1089,18 @@ static int stash_working_tree(struct stash_info *info,\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tcp3.git_cmd = 1;\n> -\targv_array_push(&cp3.args, \"write-tree\");\n> -\targv_array_pushf(&cp3.env_array, \"GIT_INDEX_FILE=%s\",\n> -\t\t\t stash_index_path.buf);\n> -\tif (pipe_command(&cp3, NULL, 0, &out3, 0,NULL, 0)) {\n> +\tif (write_index_as_tree(&info->w_tree, &state, stash_index_path.buf, 0,\n> +\t\t\t\tNULL)) {\n> +\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tget_oid_hex(out3.buf, &info->w_tree);\n> +\tdiscard_cache();\n> +\tread_cache();\n\nThis 'discard_cache()'/'read_cache()' pair surprises me a bit, and I\ncan't figure out why it's necessary now. 'write_index_as_tree()' reads\nand writes from the index file at 'stash_index_path', while\n'{discard,read}_cache()' operate on 'the_index', which should always be\ndistinct from the temporary index we are using here.  So this\nshouldn't be needed, at least not because of the changes we are making\nin this patch.\n\n>  \n>  done:\n>  \tUNLEAK(rev);\n> -\tstrbuf_release(&out3);\n>  \targv_array_clear(&args);\n>  \tobject_array_clear(&rev.pending);\n>  \tstrbuf_release(&diff_output);\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"356030","messageId":"20180819084009.GF13316@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"56500d98f9d5daaa5f21a43767885baede86e3a0.1533753605.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v7 26/26] stash: replace all \"git apply\" child processes with API calls","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-08-19T08:40:09Z","receivedAt":"2018-08-19T08:47:15Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 08/08, Paul-Sebastian Ungureanu wrote:\n> `apply_all_patches()` does not provide a method to apply patches\n> from strbuf. Because of this, this commit introduces a new\n> function `apply_patch_from_buf()` which applies a patch from buf.\n> It works by saving the strbuf as a file. This way we can call\n> `apply_all_patches()`. Before returning, the created file is\n> removed.\n\nI'm not a fan of this approach.  We're going from doing the operation\nin memory to using a temporary file that we write to disk and have to\nre-read afterwards, which I suspect might be slower than using the\n'run-command' API.\n\nFrom a quick look the 'apply_patch' function almost does what we want.\nIt reads the patch file, and then does everything else in memory.  It\nseems to me that by factoring out reading the patch file from that\nfunction, we should be able to use the rest to do the operation\nin-memory here, which would be much nicer.\n\n> ---\n>  builtin/stash.c | 61 +++++++++++++++++++++++++++----------------------\n>  1 file changed, 34 insertions(+), 27 deletions(-)\n> \n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index 46e76a34e..74eda822c 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -13,6 +13,7 @@\n>  #include \"revision.h\"\n>  #include \"log-tree.h\"\n>  #include \"diffcore.h\"\n> +#include \"apply.h\"\n>  \n>  static const char * const git_stash_usage[] = {\n>  \tN_(\"git stash list [<options>]\"),\n> @@ -277,10 +278,6 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)\n>  \tstruct child_process cp = CHILD_PROCESS_INIT;\n>  \tconst char *w_commit_hex = oid_to_hex(w_commit);\n>  \n> -\t/*\n> -\t * Diff-tree would not be very hard to replace with a native function,\n> -\t * however it should be done together with apply_cached.\n> -\t */\n\nHmm there was probably a good reason why we wrote this comment in the\nfirst place.  I can't recall what that reason was, but we should\nprobably explore that.  If there was no reason for it, then we should\nremove the comment where it was added in the series (since this is all\nnew code the comment comes from somewhere else in this series).\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> @@ -288,18 +285,36 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)\n>  \treturn pipe_command(&cp, NULL, 0, out, 0, NULL, 0);\n>  }\n>  \n> -static int apply_cached(struct strbuf *out)\n> +static int apply_patch_from_buf(struct strbuf *patch, int cached, int reverse,\n> +\t\t\t\tint check_index)\n>  {\n> -\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\tint ret = 0;\n> +\tstruct apply_state state;\n> +\tstruct argv_array args = ARGV_ARRAY_INIT;\n> +\tconst char *patch_path = \".git/stash_patch.patch\";\n\nWe should not rely on '.git/' here.  This will not work if 'GIT_DIR'\nis set, or even in a worktree, where '.git' is just a file, not a\ndirectory.\n\n> +\tFILE *patch_file;\n>  \n> -\t/*\n> -\t * Apply currently only reads either from stdin or a file, thus\n> -\t * apply_all_patches would have to be updated to optionally take a\n> -\t * buffer.\n> -\t */\n\nAh and indeed the comment here is suggesting a very similar thing to\nwhat I suggested above :)\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> +\tif (init_apply_state(&state, NULL))\n> +\t\treturn -1;\n> +\n> +\tstate.cached = cached;\n> +\tstate.apply_in_reverse = reverse;\n> +\tstate.check_index = check_index;\n> +\tif (state.cached)\n> +\t\tstate.check_index = 1;\n> +\tif (state.check_index)\n> +\t\tstate.unsafe_paths = 0;\n> +\n> +\tpatch_file = fopen(patch_path, \"w\");\n> +\tstrbuf_write(patch, patch_file);\n> +\tfclose(patch_file);\n> +\n> +\targv_array_push(&args, patch_path);\n> +\tret = apply_all_patches(&state, args.argc, args.argv, 0);\n> +\n> +\tremove_path(patch_path);\n> +\tclear_apply_state(&state);\n> +\treturn ret;\n>  }\n>  \n>  static int reset_head(const char *prefix)\n> @@ -418,7 +433,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,\n>  \t\t\t\treturn -1;\n>  \t\t\t}\n>  \n> -\t\t\tret = apply_cached(&out);\n> +\t\t\tret = apply_patch_from_buf(&out, 1, 0, 0);\n>  \t\t\tstrbuf_release(&out);\n>  \t\t\tif (ret)\n>  \t\t\t\treturn -1;\n> @@ -1341,7 +1356,6 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n>  \t\t\tint i;\n>  \t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n>  \t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n> -\t\t\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n>  \t\t\tstruct strbuf out = STRBUF_INIT;\n>  \n>  \t\t\tcp1.git_cmd = 1;\n> @@ -1365,11 +1379,9 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n>  \t\t\tif (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0))\n>  \t\t\t\treturn -1;\n>  \n> -\t\t\tcp3.git_cmd = 1;\n> -\t\t\targv_array_pushl(&cp3.args, \"apply\", \"--index\", \"-R\",\n> -\t\t\t\t\t NULL);\n> -\t\t\tif (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,\n> -\t\t\t\t\t 0))\n> +\t\t\tdiscard_cache();\n> +\t\t\tread_cache();\n> +\t\t\tif (apply_patch_from_buf(&out, 0, 1, 1))\n>  \t\t\t\treturn -1;\n>  \t\t} else {\n>  \t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> @@ -1405,12 +1417,7 @@ static int do_push_stash(int argc, const char **argv, const char *prefix,\n>  \t\t\t\treturn -1;\n>  \t\t}\n>  \t} else {\n> -\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> -\n> -\t\tcp.git_cmd = 1;\n> -\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n> -\n> -\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n> +\t\tif (apply_patch_from_buf(&patch, 0, 1, 0)) {\n>  \t\t\tif (!quiet)\n>  \t\t\t\tfprintf_ln(stderr, \"Cannot remove worktree changes\");\n>  \t\t\treturn -1;\n> -- \n> 2.18.0.573.g56500d98f\n> \n"},{"id":"357031","messageId":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1529943789.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 00/20] Convert \"git stash\" to C builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:30Z","receivedAt":"2018-08-30T21:41:41Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nThis a new iteration of `stash.c`. What is new?\n\n * Some commits got squashed. The commit related to replacing\n `git apply` child process was dropped since it wasn't the best\n idea.\n\n * In v7, there was a bug [1] related to config `git stash show`\n The bug was fixed and a test file was added for this.\n\n * Fixed `git stash [push]` [2]. In v7, `git stash -q drop` would\n act like `git stash push -q drop`.\n\n * Fixed coding-style nits. Verified that messages are marked\n for translation and are going to the correct output stream.\n\n * Fixed one memory leak (related to `strbuf_detach`).\n\n * Simplified the code a little bit.\n\n[1]:\nhttps://public-inbox.org/git/20180815210142.GN2734@hank.intra.tgummerer.com/\n\n[2]:\nhttps://public-inbox.org/git/20180818165102.GF11326@hank.intra.tgummerer.com/\n\nBest regards,\nPaul\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\nPaul-Sebastian Ungureanu (15):\n  sha1-name.c: add `get_oidf()` which acts like `get_oid()`\n  stash: update test cases conform to coding guidelines\n  stash: rename test cases to be more descriptive\n  stash: add tests for `git stash show` config\n  stash: convert list to builtin\n  stash: convert show to builtin\n  stash: mention options in `show` synopsis.\n  stash: convert store to builtin\n  stash: convert create to builtin\n  stash: convert push to builtin\n  stash: make push -q quiet\n  stash: convert save to builtin\n  stash: convert `stash--helper.c` into `stash.c`\n  stash: optimize `get_untracked_files()` and `check_changes()`\n  stash: replace all `write-tree` child processes with API calls\n\n Documentation/git-stash.txt  |    4 +-\n Makefile                     |    2 +-\n builtin.h                    |    1 +\n builtin/stash.c              | 1563 ++++++++++++++++++++++++++++++++++\n cache.h                      |    1 +\n git-stash.sh                 |  752 ----------------\n git.c                        |    1 +\n sha1-name.c                  |   19 +\n t/t3903-stash.sh             |  192 +++--\n t/t3907-stash-show-config.sh |   81 ++\n 10 files changed, 1795 insertions(+), 821 deletions(-)\n create mode 100644 builtin/stash.c\n delete mode 100755 git-stash.sh\n create mode 100755 t/t3907-stash-show-config.sh\n\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357032","messageId":"b21cdeb1636b679d80ff554c48bc76516aed2cf6.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 01/20] sha1-name.c: add `get_oidf()` which acts like `get_oid()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:31Z","receivedAt":"2018-08-30T21:41:43Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Compared to `get_oid()`, `get_oidf()` has as parameters\na pointer to `object_id`, a printf format string and\nadditional arguments. This will help simplify the code\nin subsequent commits.\n\nOriginal-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n cache.h     |  1 +\n sha1-name.c | 19 +++++++++++++++++++\n 2 files changed, 20 insertions(+)\n\ndiff --git a/cache.h b/cache.h\nindex b1fd3d58ab..d93b2e25a5 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1309,6 +1309,7 @@ struct object_context {\n \tGET_OID_BLOB)\n \n extern int get_oid(const char *str, struct object_id *oid);\n+extern int get_oidf(struct object_id *oid, const char *fmt, ...);\n extern int get_oid_commit(const char *str, struct object_id *oid);\n extern int get_oid_committish(const char *str, struct object_id *oid);\n extern int get_oid_tree(const char *str, struct object_id *oid);\ndiff --git a/sha1-name.c b/sha1-name.c\nindex c9cc1318b7..261b960bbd 100644\n--- a/sha1-name.c\n+++ b/sha1-name.c\n@@ -1471,6 +1471,25 @@ int get_oid(const char *name, struct object_id *oid)\n \treturn get_oid_with_context(name, 0, oid, &unused);\n }\n \n+/*\n+ * This returns a non-zero value if the string (built using printf\n+ * format and the given arguments) is not a valid object.\n+ */\n+int get_oidf(struct object_id *oid, const char *fmt, ...)\n+{\n+\tva_list ap;\n+\tint ret;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tva_start(ap, fmt);\n+\tstrbuf_vaddf(&sb, fmt, ap);\n+\tva_end(ap);\n+\n+\tret = get_oid(sb.buf, oid);\n+\tstrbuf_release(&sb);\n+\n+\treturn ret;\n+}\n \n /*\n  * Many callers know that the user meant to name a commit-ish by\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357033","messageId":"42423f6af38dd9029c7f7fe870593efbeabe3f1c.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 02/20] stash: improve option parsing test coverage","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:32Z","receivedAt":"2018-08-30T21:41:43Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nIn 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 parameters, or too few.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++\n 1 file changed, 35 insertions(+)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex 1f871d3cca..af7586d43d 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '\n \ttest foo = \"$(cat file/file)\"\n '\n \n+test_expect_success 'giving too many ref arguments does not modify files' '\n+\tgit stash clear &&\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\techo foo >file2 &&\n+\tgit stash &&\n+\techo bar >file2 &&\n+\tgit stash &&\n+\ttest-tool chmtime =123456789 file2 &&\n+\tfor type in apply pop \"branch stash-branch\"\n+\tdo\n+\t\ttest_must_fail git stash $type stash@{0} stash@{1} 2>err &&\n+\t\ttest_i18ngrep \"Too many revisions\" err &&\n+\t\ttest 123456789 = $(test-tool chmtime -g file2) || return 1\n+\tdone\n+'\n+\n+test_expect_success 'drop: too many arguments errors out (does nothing)' '\n+\tgit stash list >expect &&\n+\ttest_must_fail git stash drop stash@{0} stash@{1} 2>err &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\tgit stash list >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'show: too many arguments errors out (does nothing)' '\n+\ttest_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\ttest_must_be_empty out\n+'\n+\n test_expect_success 'stash create - no changes' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n@@ -479,6 +509,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.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357036","messageId":"2e7eef95b626721bac48d072aa0869452fe4297a.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 03/20] stash: update test cases conform to coding guidelines","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:33Z","receivedAt":"2018-08-30T21:41:45Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Removed whitespaces after redirection operators.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------\n 1 file changed, 61 insertions(+), 59 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex af7586d43d..de6cab1fe7 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -8,22 +8,22 @@ test_description='Test git stash'\n . ./test-lib.sh\n \n test_expect_success 'stash some dirty working directory' '\n-\techo 1 > file &&\n+\techo 1 >file &&\n \tgit add file &&\n \techo unrelated >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m initial &&\n-\techo 2 > file &&\n+\techo 2 >file &&\n \tgit add file &&\n-\techo 3 > file &&\n+\techo 3 >file &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file b/file\n index 0cfbf08..00750ed 100644\n --- a/file\n@@ -35,7 +35,7 @@ EOF\n \n test_expect_success 'parents of stash' '\n \ttest $(git rev-parse stash^) = $(git rev-parse HEAD) &&\n-\tgit diff stash^2..stash > output &&\n+\tgit diff stash^2..stash >output &&\n \ttest_cmp output expect\n '\n \n@@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '\n \n test_expect_success 'apply stashed changes (including index)' '\n \tgit reset --hard HEAD^ &&\n-\techo 6 > other-file &&\n+\techo 6 >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m other-file &&\n@@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '\n \n test_expect_success 'drop top stash' '\n \tgit reset --hard &&\n-\tgit stash list > stashlist1 &&\n-\techo 7 > file &&\n+\tgit stash list >expected &&\n+\techo 7 >file &&\n \tgit stash &&\n \tgit stash drop &&\n-\tgit stash list > stashlist2 &&\n-\ttest_cmp stashlist1 stashlist2 &&\n+\tgit stash list >actual &&\n+\ttest_cmp expected actual &&\n \tgit stash apply &&\n \ttest 3 = $(cat file) &&\n \ttest 1 = $(git show :file) &&\n@@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '\n \n test_expect_success 'drop middle stash' '\n \tgit reset --hard &&\n-\techo 8 > file &&\n+\techo 8 >file &&\n \tgit stash &&\n-\techo 9 > file &&\n+\techo 9 >file &&\n \tgit stash &&\n \tgit stash drop stash@{1} &&\n \ttest 2 = $(git stash list | wc -l) &&\n@@ -160,7 +160,7 @@ test_expect_success 'stash pop' '\n \ttest 0 = $(git stash list | wc -l)\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file2 b/file2\n new file mode 100644\n index 0000000..1fe912c\n@@ -170,7 +170,7 @@ index 0000000..1fe912c\n +bar2\n EOF\n \n-cat > expect1 << EOF\n+cat >expect1 <<EOF\n diff --git a/file b/file\n index 257cc56..5716ca5 100644\n --- a/file\n@@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644\n +bar\n EOF\n \n-cat > expect2 << EOF\n+cat >expect2 <<EOF\n diff --git a/file b/file\n index 7601807..5716ca5 100644\n --- a/file\n@@ -198,79 +198,79 @@ index 0000000..1fe912c\n EOF\n \n test_expect_success 'stash branch' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit commit file -m first &&\n-\techo bar > file &&\n-\techo bar2 > file2 &&\n+\techo bar >file &&\n+\techo bar2 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n-\techo baz > file &&\n+\techo baz >file &&\n \tgit commit file -m second &&\n \tgit stash branch stashbranch &&\n \ttest refs/heads/stashbranch = $(git symbolic-ref HEAD) &&\n \ttest $(git rev-parse HEAD) = $(git rev-parse master^) &&\n-\tgit diff --cached > output &&\n+\tgit diff --cached >output &&\n \ttest_cmp output expect &&\n-\tgit diff > output &&\n+\tgit diff >output &&\n \ttest_cmp output expect1 &&\n \tgit add file &&\n \tgit commit -m alternate\\ second &&\n-\tgit diff master..stashbranch > output &&\n+\tgit diff master..stashbranch >output &&\n \ttest_cmp output expect2 &&\n \ttest 0 = $(git stash list | wc -l)\n '\n \n test_expect_success 'apply -q is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\tgit stash apply -q > output.out 2>&1 &&\n+\tgit stash apply -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'save -q is quiet' '\n-\tgit stash save --quiet > output.out 2>&1 &&\n+\tgit stash save --quiet >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q is quiet' '\n-\tgit stash pop -q > output.out 2>&1 &&\n+\tgit stash pop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q --index works and is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit add file &&\n \tgit stash save --quiet &&\n-\tgit stash pop -q --index > output.out 2>&1 &&\n+\tgit stash pop -q --index >output.out 2>&1 &&\n \ttest foo = \"$(git show :file)\" &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'drop -q is quiet' '\n \tgit stash &&\n-\tgit stash drop -q > output.out 2>&1 &&\n+\tgit stash drop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'stash -k' '\n-\techo bar3 > file &&\n-\techo bar4 > file2 &&\n+\techo bar3 >file &&\n+\techo bar4 >file2 &&\n \tgit add file2 &&\n \tgit stash -k &&\n \ttest bar,bar4 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --no-keep-index' '\n-\techo bar33 > file &&\n-\techo bar44 > file2 &&\n+\techo bar33 >file &&\n+\techo bar44 >file2 &&\n \tgit add file2 &&\n \tgit stash --no-keep-index &&\n \ttest bar,bar2 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --invalid-option' '\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \ttest_must_fail git stash --invalid-option &&\n \ttest_must_fail git stash save --invalid-option &&\n@@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash drop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash pop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \n test_expect_success 'ref with non-existent reflog' '\n \tgit stash clear &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git rev-parse --quiet --verify does-not-exist &&\n@@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '\n test_expect_success 'invalid ref of the form stash@{n}, n >= N' '\n \tgit stash clear &&\n \ttest_must_fail git stash drop stash@{0} &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git stash drop stash@{1} &&\n@@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu\n \ttest_i18ncmp expect actual\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/HEAD b/HEAD\n new file mode 100644\n index 0000000..fe0cbee\n@@ -737,14 +739,14 @@ EOF\n test_expect_success 'stash where working directory contains \"HEAD\" file' '\n \tgit stash clear &&\n \tgit reset --hard &&\n-\techo file-not-a-ref > HEAD &&\n+\techo file-not-a-ref >HEAD &&\n \tgit add HEAD &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD &&\n \ttest \"$(git rev-parse stash^)\" = \"$(git rev-parse HEAD)\" &&\n-\tgit diff stash^..stash > output &&\n+\tgit diff stash^..stash >output &&\n \ttest_cmp output expect\n '\n \n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357034","messageId":"41fa6cc1a9df062ee35a0fdd20ff53b46ace1719.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 04/20] stash: rename test cases to be more descriptive","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:34Z","receivedAt":"2018-08-30T21:41:46Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Rename some test cases' labels to be more descriptive and under 80\ncharacters per line.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex de6cab1fe7..3114c7bc4c 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \ttest_cmp expected actual\n '\n \n-test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'drop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit reset --hard HEAD\n '\n \n-test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'pop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form \"n\", n >= N' '\n \tgit stash drop\n '\n \n-test_expect_success 'stash branch should not drop the stash if the branch exists' '\n+test_expect_success 'branch: do not drop the stash if the branch exists' '\n \tgit stash clear &&\n \techo foo >file &&\n \tgit add file &&\n@@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash branch should not drop the stash if the apply fails' '\n+test_expect_success 'branch: should not drop the stash if the apply fails' '\n \tgit stash clear &&\n \tgit reset HEAD~1 --hard &&\n \techo foo >file &&\n@@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash apply shows status same as git status (relative to current directory)' '\n+test_expect_success 'apply: show same status as git status (relative to ./)' '\n \tgit stash clear &&\n \techo 1 >subdir/subfile1 &&\n \techo 2 >subdir/subfile2 &&\n@@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec shows no changes when there are none' '\n+test_expect_success 'push <pathspec>: show no changes when there are none' '\n \t>foo &&\n \tgit add foo &&\n \tgit commit -m \"tmp\" &&\n@@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec not in the repository errors out' '\n+test_expect_success 'push: <pathspec> not in the repository errors out' '\n \t>untracked &&\n \ttest_must_fail git stash push untracked &&\n \ttest_path_is_file untracked\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357035","messageId":"65603caf56cfaeb1dff92546b7efe38dccb2d071.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:35Z","receivedAt":"2018-08-30T21:41:46Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commit introduces tests for `git stash show`\nconfig. It tests all the cases where `stash.showStat`\nand `stash.showPatch` are unset or set to true / false.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3907-stash-show-config.sh | 81 ++++++++++++++++++++++++++++++++++++\n 1 file changed, 81 insertions(+)\n create mode 100755 t/t3907-stash-show-config.sh\n\ndiff --git a/t/t3907-stash-show-config.sh b/t/t3907-stash-show-config.sh\nnew file mode 100755\nindex 0000000000..8fe369c1a1\n--- /dev/null\n+++ b/t/t3907-stash-show-config.sh\n@@ -0,0 +1,81 @@\n+#!/bin/sh\n+\n+test_description='Test git stash show configuration.'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit file\n+'\n+\n+# takes three parameters:\n+# 1. the stash.showStat value (or \"<unset>\")\n+# 2. the stash.showPatch value (or \"<unset>\")\n+# 3. the diff options of the expected output (or nothing for no output)\n+test_stat_and_patch () {\n+\tif test \"<unset>\" = \"$1\"\n+\tthen\n+\t\ttest_might_fail git config --unset stash.showStat\n+\telse\n+\t\ttest_config stash.showStat \"$1\"\n+\tfi &&\n+\n+\tif test \"<unset>\" = \"$2\"\n+\tthen\n+\t\ttest_might_fail git config --unset stash.showPatch\n+\telse\n+\t\ttest_config stash.showPatch \"$2\"\n+\tfi &&\n+\n+\tshift &&\n+\tshift &&\n+\techo 2 >file.t &&\n+\tgit diff \"$@\" >expect &&\n+\tgit stash &&\n+\tgit stash show >actual &&\n+\n+\tif test -z \"$1\"\n+\tthen\n+\t\ttest_must_be_empty actual\n+\telse\n+\t\ttest_cmp expect actual\n+\tfi\n+}\n+\n+test_expect_success 'showStat unset showPatch unset' '\n+\ttest_stat_and_patch \"<unset>\" \"<unset>\" --stat\n+'\n+\n+test_expect_success 'showStat unset showPatch false' '\n+\ttest_stat_and_patch \"<unset>\" false --stat\n+'\n+\n+test_expect_success 'showStat unset showPatch true' '\n+\ttest_stat_and_patch \"<unset>\" true --stat -p\n+'\n+\n+test_expect_success 'showStat false showPatch unset' '\n+\ttest_stat_and_patch false \"<unset>\"\n+'\n+\n+test_expect_success 'showStat false showPatch false' '\n+\ttest_stat_and_patch false false\n+'\n+\n+test_expect_success 'showStat false showPatch true' '\n+\ttest_stat_and_patch false true -p\n+'\n+\n+test_expect_success 'showStat true showPatch unset' '\n+\ttest_stat_and_patch true \"<unset>\" --stat\n+'\n+\n+test_expect_success 'showStat true showPatch false' '\n+\ttest_stat_and_patch true false --stat\n+'\n+\n+test_expect_success 'showStat true showPatch true' '\n+\ttest_stat_and_patch true true --stat -p\n+'\n+\n+test_done\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357038","messageId":"61ccfdc7b8951c7e2dc44231b53df08682221503.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 06/20] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:36Z","receivedAt":"2018-08-30T21:41:48Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd a builtin helper for performing stash commands. Converting\nall at once proved hard to review, so starting with just apply\nlets conversion get started without the other commands 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n .gitignore              |   1 +\n Makefile                |   1 +\n builtin.h               |   1 +\n builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  78 +------\n git.c                   |   1 +\n 6 files changed, 463 insertions(+), 71 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\ndiff --git a/.gitignore b/.gitignore\nindex ffceea7d59..b59661cb88 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -157,6 +157,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 d03df31c2a..f900c68e69 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1093,6 +1093,7 @@ BUILTIN_OBJS += builtin/shortlog.o\n BUILTIN_OBJS += builtin/show-branch.o\n BUILTIN_OBJS += builtin/show-index.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 99206df4bd..317bc338f7 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -223,6 +223,7 @@ 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_show_index(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 0000000000..fde795a764\n--- /dev/null\n+++ b/builtin/stash--helper.c\n@@ -0,0 +1,452 @@\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+#include \"rerere.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 struct strbuf stash_index_path = STRBUF_INIT;\n+\n+/*\n+ * w_commit is set to the commit containing the working tree\n+ * b_commit is set to the base commit\n+ * i_commit is set to the commit containing the index tree\n+ * u_commit is set to the commit containing the untracked files tree\n+ * w_tree is set to the working tree\n+ * b_tree is set to the base tree\n+ * i_tree is set to the index tree\n+ * u_tree is set to the untracked files tree\n+ */\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 void free_stash_info(struct stash_info *info)\n+{\n+\tstrbuf_release(&info->revision);\n+}\n+\n+static void assert_stash_like(struct stash_info *info, const char *revision)\n+{\n+\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n+\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n+\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n+\t    get_oidf(&info->i_tree, \"%s^2:\", revision)) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n+\t\texit(128);\n+\t}\n+}\n+\n+static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n+{\n+\tstruct strbuf symbolic = STRBUF_INIT;\n+\tint ret;\n+\tconst char *revision;\n+\tconst char *commit = NULL;\n+\tchar *end_of_rev;\n+\tchar *expanded_ref;\n+\tstruct object_id dummy;\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\"),\n+\t\t\t   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) {\n+\t\tif (!ref_exists(ref_stash)) {\n+\t\t\tfree_stash_info(info);\n+\t\t\tfprintf_ln(stderr, _(\"No stash entries found.\"));\n+\t\t\treturn -1;\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+\tif (get_oid(revision, &info->w_commit)) {\n+\t\terror(_(\"%s is not a valid reference\"), revision);\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\n+\n+\tassert_stash_like(info, revision);\n+\n+\tinfo->has_u = !get_oidf(&info->u_tree, \"%s^3:\", revision);\n+\n+\tend_of_rev = strchrnul(revision, '@');\n+\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n+\n+\tret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);\n+\tstrbuf_release(&symbolic);\n+\tswitch (ret) {\n+\tcase 0: /* Not found, but valid ref */\n+\t\tinfo->is_stash_ref = 0;\n+\t\tbreak;\n+\tcase 1:\n+\t\tinfo->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n+\t\tbreak;\n+\tdefault: /* Invalid or ambiguous */\n+\t\tfree_stash_info(info);\n+\t}\n+\n+\tfree(expanded_ref);\n+\treturn !(ret == 0 || ret == 1);\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+\t/*\n+\t * Diff-tree would not be very hard to replace with a native function,\n+\t * however it should be done together with apply_cached.\n+\t */\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+\t/*\n+\t * Apply currently only reads either from stdin or a file, thus\n+\t * apply_all_patches would have to be updated to optionally take a\n+\t * buffer.\n+\t */\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(void)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * Reset is overall quite simple, however there is no current public\n+\t * API for resetting.\n+\t */\n+\tcp.git_cmd = 1;\n+\targv_array_push(&cp.args, \"reset\");\n+\n+\treturn run_command(&cp);\n+}\n+\n+static int get_newly_staged(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+\t/*\n+\t * diff-index is very similar to diff-tree above, and should be\n+\t * converted together with update_index.\n+\t */\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"diff-index\", \"--cached\", \"--name-only\",\n+\t\t\t \"--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+\t/*\n+\t * Update-index is very complicated and may need to have a public\n+\t * function exposed in order to remove this forking.\n+\t */\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+\t/*\n+\t * We need to run restore files from a given index, but without\n+\t * affecting the current index, so we use GIT_INDEX_FILE with\n+\t * run_command to fork processes that will not interfere.\n+\t */\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\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp)) {\n+\t\tremove_path(stash_index_path.buf);\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\",\n+\t\t\t stash_index_path.buf);\n+\n+\tres = run_command(&cp);\n+\tremove_path(stash_index_path.buf);\n+\treturn res;\n+}\n+\n+static int do_apply_stash(const char *prefix, struct stash_info *info,\n+\tint 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+\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,\n+\t\t\t&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();\n+\t\t}\n+\t}\n+\n+\tif (info->has_u && restore_untracked(&info->u_tree))\n+\t\treturn error(_(\"Could not restore untracked files from stash\"));\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, 1, bases,\n+\t\t\t\t      &result);\n+\tif (ret) {\n+\t\trerere(0);\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 (get_newly_staged(&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\tif (refresh_cache(REFRESH_QUIET))\n+\t\t\twarning(\"could not refresh index\");\n+\t} else {\n+\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t/*\n+\t\t * Status is quite simple and could be replaced with calls to\n+\t\t * wt_status in the future, but it adds complexities which may\n+\t\t * require more tests.\n+\t\t */\n+\t\tcp.git_cmd = 1;\n+\t\tcp.dir = prefix;\n+\t\targv_array_push(&cp.args, \"status\");\n+\t\trun_command(&cp);\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\t     git_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+\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\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n+\n+\tindex_file = get_index_file();\n+\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n+\t\t    (uintmax_t)pid);\n+\n+\tif (argc < 1)\n+\t\tusage_with_options(git_stash_helper_usage, options);\n+\tif (!strcmp(argv[0], \"apply\"))\n+\t\treturn !!apply_stash(argc, argv, prefix);\n+\n+\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n+\t\t      git_stash_helper_usage, options);\n+}\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 94793c1a91..809b1c2d1d 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() {\n@@ -713,7 +648,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\ndiff --git a/git.c b/git.c\nindex c27c38738b..3c0e762d7d 100644\n--- a/git.c\n+++ b/git.c\n@@ -544,6 +544,7 @@ static struct cmd_struct commands[] = {\n \t{ \"show-index\", cmd_show_index },\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 | NO_PARSEOPT },\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357039","messageId":"8f8f25df2f68519533257b82af6a4b88e1e756cc.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 07/20] stash: convert drop and clear to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:37Z","receivedAt":"2018-08-30T21:41:48Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   4 +-\n 2 files changed, 117 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex fde795a764..cbe23fef11 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,7 +12,14 @@\n #include \"rerere.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@@ -21,6 +28,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 struct strbuf stash_index_path = STRBUF_INIT;\n@@ -140,6 +152,31 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n \treturn !(ret == 0 || ret == 1);\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,\n+\t\t\t     git_stash_helper_clear_usage,\n+\t\t\t     PARSE_OPT_STOP_AT_NON_OPTION);\n+\n+\tif (argc)\n+\t\treturn error(_(\"git stash 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@@ -424,6 +461,80 @@ 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 child_process cp_reflog = CHILD_PROCESS_INIT;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tint ret;\n+\n+\t/*\n+\t * reflog does not provide a simple function for deleting refs. One will\n+\t * need to be added to avoid implementing too much reflog code here\n+\t */\n+\n+\tcp_reflog.git_cmd = 1;\n+\targv_array_pushl(&cp_reflog.args, \"reflog\", \"delete\", \"--updateref\",\n+\t\t\t \"--rewrite\", NULL);\n+\targv_array_push(&cp_reflog.args, info->revision.buf);\n+\tret = run_command(&cp_reflog);\n+\tif (!ret) {\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"Dropped %s (%s)\"), info->revision.buf,\n+\t\t\t\t  oid_to_hex(&info->w_commit));\n+\t} else {\n+\t\treturn error(_(\"%s: Could not drop stash entry\"),\n+\t\t\t     info->revision.buf);\n+\t}\n+\n+\t/*\n+\t * This could easily be replaced by get_oid, but currently it will throw\n+\t * a fatal error when a reflog is empty, which we can not recover from.\n+\t */\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+\t/* do_clear_stash if we just dropped the last stash entry */\n+\tif (ret)\n+\t\tdo_clear_stash();\n+\n+\treturn 0;\n+}\n+\n+static void assert_stash_ref(struct stash_info *info)\n+{\n+\tif (!info->is_stash_ref) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash reference\"), info->revision.buf);\n+\t\texit(128);\n+\t}\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\t     git_stash_helper_drop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\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 \tpid_t pid = getpid();\n@@ -446,6 +557,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tusage_with_options(git_stash_helper_usage, options);\n \tif (!strcmp(argv[0], \"apply\"))\n \t\treturn !!apply_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"clear\"))\n+\t\treturn !!clear_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"drop\"))\n+\t\treturn !!drop_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 809b1c2d1d..a99d5dc9e5 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -653,7 +653,7 @@ apply)\n \t;;\n clear)\n \tshift\n-\tclear_stash \"$@\"\n+\tgit stash--helper clear \"$@\"\n \t;;\n create)\n \tshift\n@@ -665,7 +665,7 @@ store)\n \t;;\n drop)\n \tshift\n-\tdrop_stash \"$@\"\n+\tgit stash--helper drop \"$@\"\n \t;;\n pop)\n \tshift\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357037","messageId":"dd9cfad6db6050333c2356236cc2ef5748e2084c.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 09/20] stash: convert pop to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:39Z","receivedAt":"2018-08-30T21:41:49Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash pop to the helper and delete the pop_stash, drop_stash,\nassert_stash_ref functions from the shell script now that they\nare no longer needed.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 36 ++++++++++++++++++++++++++++++-\n git-stash.sh            | 47 ++---------------------------------------\n 2 files changed, 37 insertions(+), 46 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex dadc028649..9fb1003dbb 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,7 +13,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 apply [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n \tNULL\n@@ -24,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@@ -541,6 +546,33 @@ 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\t     git_stash_helper_pop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\n+\tif ((ret = do_apply_stash(prefix, &info, index)))\n+\t\tprintf_ln(_(\"The stash entry is kept in case you need it again.\"));\n+\telse\n+\t\tret = do_drop_stash(prefix, &info);\n+\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@@ -603,6 +635,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"pop\"))\n+\t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n \ndiff --git a/git-stash.sh b/git-stash.sh\nindex 29d9f44255..8f2640fe90 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@@ -655,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.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357040","messageId":"ed6aea4eb1e60dfe5c77ce8c9bc9d8409b658aed.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 08/20] stash: convert branch to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:38Z","receivedAt":"2018-08-30T21:41:50Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash branch to the helper and delete the apply_to_branch\nfunction from the shell script.\n\nCheckout does not currently provide a function for checking out\na branch as cmd_checkout does a large amount of sanity checks\nfirst that we require here.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 44 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 17 ++--------------\n 2 files changed, 46 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex cbe23fef11..dadc028649 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -14,6 +14,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@@ -28,6 +29,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@@ -535,6 +541,42 @@ 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 child_process cp = CHILD_PROCESS_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\t     git_stash_helper_branch_usage, 0);\n+\n+\tif (!argc)\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+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"checkout\", \"-b\", NULL);\n+\targv_array_push(&cp.args, branch);\n+\targv_array_push(&cp.args, oid_to_hex(&info.b_commit));\n+\tret = run_command(&cp);\n+\tif (!ret)\n+\t\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 \tpid_t pid = getpid();\n@@ -561,6 +603,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"branch\"))\n+\t\treturn !!branch_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex a99d5dc9e5..29d9f44255 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@@ -673,7 +659,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.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357042","messageId":"8d93bee7c627c5af94e6c35898e396b427db570f.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 11/20] stash: convert show to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:41Z","receivedAt":"2018-08-30T21:41:52Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash show to the helper and delete the show_stash, have_stash,\nassert_stash_like, is_stash_like and parse_flags_and_rev functions\nfrom the shell script now that they are no longer needed.\n\nIn shell version, although `git stash show` accepts `--index` and\n`--quiet` options, it ignores them. In C, both options are passed\nfurther to `git diff`.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c |  87 ++++++++++++++++++++++++++\n git-stash.sh            | 132 +---------------------------------------\n 2 files changed, 88 insertions(+), 131 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex c42a297078..1dba7e6853 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -10,9 +10,12 @@\n #include \"run-command.h\"\n #include \"dir.h\"\n #include \"rerere.h\"\n+#include \"revision.h\"\n+#include \"log-tree.h\"\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n+\tN_(\"git stash--helper show [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -25,6 +28,11 @@ static const char * const git_stash_helper_list_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_show_usage[] = {\n+\tN_(\"git stash--helper show [<stash>]\"),\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@@ -638,6 +646,83 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n \treturn run_command(&cp);\n }\n \n+static int show_stat = 1;\n+static int show_patch;\n+\n+static int git_stash_config(const char *var, const char *value, void *cb)\n+{\n+\tif (!strcmp(var, \"stash.showstat\")) {\n+\t\tshow_stat = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(var, \"stash.showpatch\")) {\n+\t\tshow_patch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\treturn git_default_config(var, value, cb);\n+}\n+\n+static int show_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint i;\n+\tint opts = 0;\n+\tint ret = 0;\n+\tstruct stash_info info;\n+\tstruct rev_info rev;\n+\tstruct argv_array stash_args = ARGV_ARRAY_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\tinit_diff_ui_defaults();\n+\tgit_config(git_diff_ui_config, NULL);\n+\tinit_revisions(&rev, prefix);\n+\n+\tfor (i = 1; i < argc; ++i) {\n+\t\tif (argv[i][0] != '-')\n+\t\t\targv_array_push(&stash_args, argv[i]);\n+\t\telse\n+\t\t\topts++;\n+\t}\n+\n+\tret = get_stash_info(&info, stash_args.argc, stash_args.argv);\n+\targv_array_clear(&stash_args);\n+\tif (ret)\n+\t\treturn -1;\n+\n+\t/*\n+\t * The config settings are applied only if there are not passed\n+\t * any options.\n+\t */\n+\tif (!opts) {\n+\t\tgit_config(git_stash_config, NULL);\n+\t\tif (show_stat)\n+\t\t\trev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;\n+\n+\t\tif (show_patch)\n+\t\t\trev.diffopt.output_format |= DIFF_FORMAT_PATCH;\n+\n+\t\tif (!show_stat && !show_patch) {\n+\t\t\tfree_stash_info(&info);\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n+\n+\targc = setup_revisions(argc, argv, &rev, NULL);\n+\tif (argc > 1) {\n+\t\tfree_stash_info(&info);\n+\t\tusage_with_options(git_stash_helper_show_usage, options);\n+\t}\n+\n+\trev.diffopt.flags.recursive = 1;\n+\tsetup_diff_pager(&rev.diffopt);\n+\tdiff_tree_oid(&info.b_commit, &info.w_commit, \"\", &rev.diffopt);\n+\tlog_tree_diff_flush(&rev);\n+\n+\tfree_stash_info(&info);\n+\treturn diff_result_code(&rev.diffopt, 0);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -670,6 +755,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!branch_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"list\"))\n \t\treturn !!list_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"show\"))\n+\t\treturn !!show_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 6052441aa2..0d05cbc1e5 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -378,35 +378,6 @@ save_stash () {\n \tfi\n }\n \n-have_stash () {\n-\tgit rev-parse --verify --quiet $ref_stash >/dev/null\n-}\n-\n-show_stash () {\n-\tALLOW_UNKNOWN_FLAGS=t\n-\tassert_stash_like \"$@\"\n-\n-\tif test -z \"$FLAGS\"\n-\tthen\n-\t\tif test \"$(git config --bool stash.showStat || echo true)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=--stat\n-\t\tfi\n-\n-\t\tif test \"$(git config --bool stash.showPatch || echo false)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=${FLAGS}${FLAGS:+ }-p\n-\t\tfi\n-\n-\t\tif test -z \"$FLAGS\"\n-\t\tthen\n-\t\t\treturn 0\n-\t\tfi\n-\tfi\n-\n-\tgit diff ${FLAGS} $b_commit $w_commit\n-}\n-\n show_help () {\n \texec git help stash\n \texit 1\n@@ -448,107 +419,6 @@ show_help () {\n #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n #\n \n-parse_flags_and_rev()\n-{\n-\ttest \"$PARSE_CACHE\" = \"$*\" && return 0 # optimisation\n-\tPARSE_CACHE=\"$*\"\n-\n-\tIS_STASH_LIKE=\n-\tIS_STASH_REF=\n-\tINDEX_OPTION=\n-\ts=\n-\tw_commit=\n-\tb_commit=\n-\ti_commit=\n-\tu_commit=\n-\tw_tree=\n-\tb_tree=\n-\ti_tree=\n-\tu_tree=\n-\n-\tFLAGS=\n-\tREV=\n-\tfor opt\n-\tdo\n-\t\tcase \"$opt\" in\n-\t\t\t-q|--quiet)\n-\t\t\t\tGIT_QUIET=-t\n-\t\t\t;;\n-\t\t\t--index)\n-\t\t\t\tINDEX_OPTION=--index\n-\t\t\t;;\n-\t\t\t--help)\n-\t\t\t\tshow_help\n-\t\t\t;;\n-\t\t\t-*)\n-\t\t\t\ttest \"$ALLOW_UNKNOWN_FLAGS\" = t ||\n-\t\t\t\t\tdie \"$(eval_gettext \"unknown option: \\$opt\")\"\n-\t\t\t\tFLAGS=\"${FLAGS}${FLAGS:+ }$opt\"\n-\t\t\t;;\n-\t\t\t*)\n-\t\t\t\tREV=\"${REV}${REV:+ }'$opt'\"\n-\t\t\t;;\n-\t\tesac\n-\tdone\n-\n-\teval set -- $REV\n-\n-\tcase $# in\n-\t\t0)\n-\t\t\thave_stash || die \"$(gettext \"No stash entries found.\")\"\n-\t\t\tset -- ${ref_stash}@{0}\n-\t\t;;\n-\t\t1)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tdie \"$(eval_gettext \"Too many revisions specified: \\$REV\")\"\n-\t\t;;\n-\tesac\n-\n-\tcase \"$1\" in\n-\t\t*[!0-9]*)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tset -- \"${ref_stash}@{$1}\"\n-\t\t;;\n-\tesac\n-\n-\tREV=$(git rev-parse --symbolic --verify --quiet \"$1\") || {\n-\t\treference=\"$1\"\n-\t\tdie \"$(eval_gettext \"\\$reference is not a valid reference\")\"\n-\t}\n-\n-\ti_commit=$(git rev-parse --verify --quiet \"$REV^2\") &&\n-\tset -- $(git rev-parse \"$REV\" \"$REV^1\" \"$REV:\" \"$REV^1:\" \"$REV^2:\" 2>/dev/null) &&\n-\ts=$1 &&\n-\tw_commit=$1 &&\n-\tb_commit=$2 &&\n-\tw_tree=$3 &&\n-\tb_tree=$4 &&\n-\ti_tree=$5 &&\n-\tIS_STASH_LIKE=t &&\n-\ttest \"$ref_stash\" = \"$(git rev-parse --symbolic-full-name \"${REV%@*}\")\" &&\n-\tIS_STASH_REF=t\n-\n-\tu_commit=$(git rev-parse --verify --quiet \"$REV^3\") &&\n-\tu_tree=$(git rev-parse \"$REV^3:\" 2>/dev/null)\n-}\n-\n-is_stash_like()\n-{\n-\tparse_flags_and_rev \"$@\"\n-\ttest -n \"$IS_STASH_LIKE\"\n-}\n-\n-assert_stash_like() {\n-\tis_stash_like \"$@\" || {\n-\t\targs=\"$*\"\n-\t\tdie \"$(eval_gettext \"'\\$args' is not a stash-like commit\")\"\n-\t}\n-}\n-\n test \"$1\" = \"-p\" && set \"push\" \"$@\"\n \n PARSE_CACHE='--not-parsed'\n@@ -573,7 +443,7 @@ list)\n \t;;\n show)\n \tshift\n-\tshow_stash \"$@\"\n+\tgit stash--helper show \"$@\"\n \t;;\n save)\n \tshift\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357041","messageId":"5466d911db63333c21317714e15b5bff827008a6.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 13/20] stash: convert store to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:43Z","receivedAt":"2018-08-30T21:41:53Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash store to the helper and delete the store_stash function\nfrom the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 55 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 43 ++------------------------------\n 2 files changed, 57 insertions(+), 41 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 02b593e0cd..87568b0f34 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -58,6 +58,11 @@ static const char * const git_stash_helper_clear_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_store_usage[] = {\n+\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -723,6 +728,54 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n \treturn diff_result_code(&rev.diffopt, 0);\n }\n \n+static int do_store_stash(const char *w_commit, const char *stash_msg,\n+\t\t\t  int quiet)\n+{\n+\tint ret = 0;\n+\tint need_to_free = 0;\n+\tstruct object_id obj;\n+\n+\tif (!stash_msg) {\n+\t\tneed_to_free = 1;\n+\t\tstash_msg  = xstrdup(\"Created via \\\"git stash store\\\".\");\n+\t}\n+\n+\tret = get_oid(w_commit, &obj);\n+\tif (!ret) {\n+\t\tret = update_ref(stash_msg, ref_stash, &obj, NULL,\n+\t\t\t\t REF_FORCE_CREATE_REFLOG,\n+\t\t\t\t quiet ? UPDATE_REFS_QUIET_ON_ERR :\n+\t\t\t\t UPDATE_REFS_MSG_ON_ERR);\n+\t}\n+\tif (ret && !quiet)\n+\t\tfprintf_ln(stderr, _(\"Cannot update %s with %s\"),\n+\t\t\t   ref_stash, w_commit);\n+\tif (need_to_free)\n+\t\tfree((char *) stash_msg);\n+\treturn ret;\n+}\n+\n+static int store_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tconst char *stash_msg = NULL;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\", N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_store_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (argc != 1) {\n+\t\tfprintf_ln(stderr, _(\"\\\"git stash store\\\" requires one <commit> argument\"));\n+\t\treturn -1;\n+\t}\n+\n+\treturn do_store_stash(argv[0], stash_msg, quiet);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -757,6 +810,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!list_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"show\"))\n \t\treturn !!show_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"store\"))\n+\t\treturn !!store_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 0d05cbc1e5..5739c51527 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -191,45 +191,6 @@ create_stash () {\n \tdie \"$(gettext \"Cannot record working tree state\")\"\n }\n \n-store_stash () {\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\tstash_msg=\"$1\"\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t-q|--quiet)\n-\t\t\tquiet=t\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\ttest $# = 1 ||\n-\tdie \"$(eval_gettext \"\\\"$dashless store\\\" requires one <commit> argument\")\"\n-\n-\tw_commit=\"$1\"\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tstash_msg=\"Created via \\\"git stash store\\\".\"\n-\tfi\n-\n-\tgit update-ref --create-reflog -m \"$stash_msg\" $ref_stash $w_commit\n-\tret=$?\n-\ttest $ret != 0 && test -z \"$quiet\" &&\n-\tdie \"$(eval_gettext \"Cannot update \\$ref_stash with \\$w_commit\")\"\n-\treturn $ret\n-}\n-\n push_stash () {\n \tkeep_index=\n \tpatch_mode=\n@@ -308,7 +269,7 @@ push_stash () {\n \t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n \n \tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n-\tstore_stash -m \"$stash_msg\" -q $w_commit ||\n+\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n \tdie \"$(gettext \"Cannot save the current status\")\"\n \tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n \n@@ -468,7 +429,7 @@ create)\n \t;;\n store)\n \tshift\n-\tstore_stash \"$@\"\n+\tgit stash--helper store \"$@\"\n \t;;\n drop)\n \tshift\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357044","messageId":"a4faed3c8aa5ea8f0d4c578b693f3b5de3e3a709.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 14/20] stash: convert create to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:44Z","receivedAt":"2018-08-30T21:41:54Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash create to the helper.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 430 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   2 +-\n 2 files changed, 431 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 87568b0f34..ce360a569d 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,7 @@\n #include \"rerere.h\"\n #include \"revision.h\"\n #include \"log-tree.h\"\n+#include \"diffcore.h\"\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n@@ -63,6 +64,11 @@ static const char * const git_stash_helper_store_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_create_usage[] = {\n+\tN_(\"git stash--helper create [<message>]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -290,6 +296,18 @@ static int reset_head(void)\n \treturn run_command(&cp);\n }\n \n+static void add_diff_to_buf(struct diff_queue_struct *q,\n+\t\t\t    struct diff_options *options,\n+\t\t\t    void *data)\n+{\n+\tint i;\n+\tfor (i = 0; i < q->nr; i++) {\n+\t\tstruct diff_filepair *p = q->queue[i];\n+\t\tstrbuf_addstr(data, p->one->path);\n+\t\tstrbuf_addch(data, 0);\n+\t}\n+}\n+\n static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)\n {\n \tstruct child_process cp = CHILD_PROCESS_INIT;\n@@ -776,6 +794,416 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n \treturn do_store_stash(argv[0], stash_msg, quiet);\n }\n \n+/*\n+ * `out` will be filled with the names of untracked files. The return value is:\n+ *\n+ * = 0 if there are not any untracked files\n+ * > 0 if there are untracked files\n+ */\n+static int get_untracked_files(struct pathspec ps, int include_untracked,\n+\t\t\t       struct strbuf *out)\n+{\n+\tint max_len;\n+\tint i;\n+\tchar *seen;\n+\tstruct dir_struct dir;\n+\n+\tmemset(&dir, 0, sizeof(dir));\n+\tif (include_untracked != 2)\n+\t\tsetup_standard_excludes(&dir);\n+\n+\tseen = xcalloc(ps.nr, 1);\n+\n+\tmax_len = fill_directory(&dir, the_repository->index, &ps);\n+\tfor (i = 0; i < dir.nr; i++) {\n+\t\tstruct dir_entry *ent = dir.entries[i];\n+\t\tif (!dir_path_match(&the_index, ent, &ps, max_len, seen)) {\n+\t\t\tfree(ent);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tstrbuf_addf(out, \"%s%c\", ent->name, '\\0');\n+\t\tfree(ent);\n+\t}\n+\n+\tfree(dir.entries);\n+\tfree(dir.ignored);\n+\tclear_directory(&dir);\n+\tfree(seen);\n+\treturn out->len;\n+}\n+\n+/*\n+ * The return value of `check_changes()` can be:\n+ *\n+ * < 0 if there was an error\n+ * = 0 if there are no changes.\n+ * > 0 if there are changes.\n+ */\n+static int check_changes(struct pathspec ps, int include_untracked)\n+{\n+\tint result;\n+\tint ret = 0;\n+\tstruct rev_info rev;\n+\tstruct object_id dummy;\n+\tstruct strbuf out = STRBUF_INIT;\n+\n+\tinit_revisions(&rev, NULL);\n+\trev.prune_data = ps;\n+\n+\trev.diffopt.flags.quick = 1;\n+\trev.diffopt.flags.ignore_submodules = 1;\n+\trev.abbrev = 0;\n+\n+\t/* No initial commit. */\n+\tif (get_oid(\"HEAD\", &dummy))\n+\t\treturn -1;\n+\n+\tadd_head_to_pending(&rev);\n+\tdiff_setup_done(&rev.diffopt);\n+\n+\tif (read_cache() < 0)\n+\t\treturn 1;\n+\tresult = run_diff_index(&rev, 1);\n+\tif (diff_result_code(&rev.diffopt, result))\n+\t\treturn 1;\n+\n+\tobject_array_clear(&rev.pending);\n+\tresult = run_diff_files(&rev, 0);\n+\tif (diff_result_code(&rev.diffopt, result))\n+\t\treturn 1;\n+\n+\tif (include_untracked && get_untracked_files(ps, include_untracked,\n+\t\t\t\t\t\t     &out)) {\n+\t\tstrbuf_release(&out);\n+\t\treturn 1;\n+\t}\n+\n+\tstrbuf_release(&out);\n+\treturn 0;\n+}\n+\n+static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n+\t\t\t\tstruct strbuf *in)\n+{\n+\tint ret = 0;\n+\tstruct strbuf untracked_msg = STRBUF_INIT;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\n+\tcp_upd_index.git_cmd = 1;\n+\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n+\t\t\t \"--remove\", \"--stdin\", NULL);\n+\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\n+\tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n+\tif (pipe_command(&cp_upd_index, in->buf, in->len, NULL, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_write_tree.git_cmd = 1;\n+\targv_array_push(&cp_write_tree.args, \"write-tree\");\n+\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\tget_oid_hex(out.buf, &info->u_tree);\n+\n+\tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n+\t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+done:\n+\tstrbuf_release(&untracked_msg);\n+\tstrbuf_release(&out);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static struct strbuf patch = STRBUF_INIT;\n+\n+static int stash_patch(struct stash_info *info, struct pathspec ps)\n+{\n+\tint i;\n+\tint ret = 0;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct child_process cp_read_tree = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_add_i = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n+\n+\tremove_path(stash_index_path.buf);\n+\n+\tcp_read_tree.git_cmd = 1;\n+\targv_array_pushl(&cp_read_tree.args, \"read-tree\", \"HEAD\", NULL);\n+\targv_array_pushf(&cp_read_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp_read_tree)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_add_i.git_cmd = 1;\n+\targv_array_pushl(&cp_add_i.args, \"add--interactive\", \"--patch=stash\",\n+\t\t\t\"--\", NULL);\n+\tfor (i = 0; i < ps.nr; ++i)\n+\t\targv_array_push(&cp_add_i.args, ps.items[i].match);\n+\targv_array_pushf(&cp_add_i.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp_add_i)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_write_tree.git_cmd = 1;\n+\targv_array_push(&cp_write_tree.args, \"write-tree\");\n+\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tget_oid_hex(out.buf, &info->w_tree);\n+\n+\tcp_diff_tree.git_cmd = 1;\n+\targv_array_pushl(&cp_diff_tree.args, \"diff-tree\", \"-p\", \"HEAD\",\n+\t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n+\tif (pipe_command(&cp_diff_tree, NULL, 0, &patch, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (!patch.len) {\n+\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n+\t\tret = 1;\n+\t}\n+\n+done:\n+\tstrbuf_release(&out);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n+{\n+\tint ret = 0;\n+\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct strbuf diff_output = STRBUF_INIT;\n+\tstruct rev_info rev;\n+\n+\tset_alternate_index_output(stash_index_path.buf);\n+\tif (reset_tree(&info->i_tree, 0, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\tset_alternate_index_output(NULL);\n+\n+\tgit_config(git_diff_basic_config, NULL);\n+\tinit_revisions(&rev, NULL);\n+\trev.prune_data = ps;\n+\trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n+\trev.diffopt.format_callback = add_diff_to_buf;\n+\trev.diffopt.format_callback_data = &diff_output;\n+\n+\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit), \"\");\n+\tif (run_diff_index(&rev, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_upd_index.git_cmd = 1;\n+\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n+\t\t\t \"--remove\", \"--stdin\", NULL);\n+\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\n+\tif (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,\n+\t\t\t NULL, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_write_tree.git_cmd = 1;\n+\targv_array_push(&cp_write_tree.args, \"write-tree\");\n+\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tget_oid_hex(out.buf, &info->w_tree);\n+\n+done:\n+\tUNLEAK(rev);\n+\tstrbuf_release(&out);\n+\tobject_array_clear(&rev.pending);\n+\tstrbuf_release(&diff_output);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static int do_create_stash(struct pathspec ps, const char **stash_msg,\n+\t\t\t   int include_untracked, int patch_mode,\n+\t\t\t   struct stash_info *info)\n+{\n+\tint untracked_commit_option = 0;\n+\tint ret = 0;\n+\tint flags;\n+\tconst char *head_short_sha1 = NULL;\n+\tconst char *branch_ref = NULL;\n+\tconst char *branch_name = \"(no branch)\";\n+\tstruct commit *head_commit = NULL;\n+\tstruct commit_list *parents = NULL;\n+\tstruct strbuf msg = STRBUF_INIT;\n+\tstruct strbuf commit_tree_label = STRBUF_INIT;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n+\n+\tread_cache_preload(NULL);\n+\trefresh_cache(REFRESH_QUIET);\n+\n+\tif (!check_changes(ps, include_untracked)) {\n+\t\tret = 1;\n+\t\t*stash_msg = NULL;\n+\t\tgoto done;\n+\t}\n+\n+\tif (get_oid(\"HEAD\", &info->b_commit)) {\n+\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n+\t\tret = -1;\n+\t\t*stash_msg = NULL;\n+\t\tgoto done;\n+\t} else {\n+\t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n+\t}\n+\n+\tbranch_ref = resolve_ref_unsafe(\"HEAD\", 0, NULL, &flags);\n+\tif (flags & REF_ISSYMREF)\n+\t\tbranch_name = strrchr(branch_ref, '/') + 1;\n+\thead_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n+\t\t\t\t\t     DEFAULT_ABBREV);\n+\tstrbuf_addf(&msg, \"%s: %s \", branch_name, head_short_sha1);\n+\tpp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);\n+\n+\tstrbuf_addf(&commit_tree_label, \"index on %s\\n\", msg.buf);\n+\tcommit_list_insert(head_commit, &parents);\n+\tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n+\t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n+\t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n+\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n+\t\tret = -1;\n+\t\t*stash_msg = NULL;\n+\t\tgoto done;\n+\t}\n+\n+\tif (include_untracked && get_untracked_files(ps, include_untracked,\n+\t\t\t\t\t\t     &out)) {\n+\t\tif (save_untracked_files(info, &msg, &out)) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n+\t\t\tret = -1;\n+\t\t\t*stash_msg = NULL;\n+\t\t\tgoto done;\n+\t\t}\n+\t\tuntracked_commit_option = 1;\n+\t}\n+\tif (patch_mode) {\n+\t\tret = stash_patch(info, ps);\n+\t\t*stash_msg = NULL;\n+\t\tif (ret < 0) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tgoto done;\n+\t\t} else if (ret > 0) {\n+\t\t\tgoto done;\n+\t\t}\n+\t} else {\n+\t\tif (stash_working_tree(info, ps)) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tret = -1;\n+\t\t\t*stash_msg = NULL;\n+\t\t\tgoto done;\n+\t\t}\n+\t}\n+\n+\tif (!*stash_msg || !strlen(*stash_msg))\n+\t\tstrbuf_addf(&stash_msg_buf, \"WIP on %s\", msg.buf);\n+\telse\n+\t\tstrbuf_addf(&stash_msg_buf, \"On %s: %s\", branch_name,\n+\t\t\t    *stash_msg);\n+\t*stash_msg = strbuf_detach(&stash_msg_buf, NULL);\n+\n+\t/*\n+\t * `parents` will be empty after calling `commit_tree()`, so there is\n+\t * no need to call `free_commit_list()`\n+\t */\n+\tparents = NULL;\n+\tif (untracked_commit_option)\n+\t\tcommit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n+\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n+\tcommit_list_insert(head_commit, &parents);\n+\n+\tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n+\t\t\tparents, &info->w_commit, NULL, NULL)) {\n+\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+done:\n+\tstrbuf_release(&commit_tree_label);\n+\tstrbuf_release(&msg);\n+\tstrbuf_release(&out);\n+\tstrbuf_release(&stash_msg_buf);\n+\treturn ret;\n+}\n+\n+static int create_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint include_untracked = 0;\n+\tint ret = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct stash_info info;\n+\tstruct pathspec ps;\n+\tstruct option options[] = {\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_create_usage,\n+\t\t\t     0);\n+\n+\tmemset(&ps, 0, sizeof(ps));\n+\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);\n+\n+\tif (!ret)\n+\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n+\n+\t/*\n+\t * ret can be 1 if there were no changes. In this case, we should\n+\t * not error out.\n+\t */\n+\tfree((char *) stash_msg);\n+\treturn ret < 0;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -812,6 +1240,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!show_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"store\"))\n \t\treturn !!store_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"create\"))\n+\t\treturn !!create_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 5739c51527..ab06e4ffb8 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -425,7 +425,7 @@ clear)\n \t;;\n create)\n \tshift\n-\tcreate_stash -m \"$*\" && echo \"$w_commit\"\n+\tgit stash--helper create --message \"$*\"\n \t;;\n store)\n \tshift\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357045","messageId":"fff86f1761070b2e48591527408220fec42506d8.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 12/20] stash: mention options in `show` synopsis.","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:42Z","receivedAt":"2018-08-30T21:41:55Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Mention in the usage text and in the documentation, that `show`\naccepts any option known to `git diff`.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n Documentation/git-stash.txt | 4 ++--\n builtin/stash--helper.c     | 4 ++--\n 2 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\nindex 7ef8c47911..e31ea7d303 100644\n--- a/Documentation/git-stash.txt\n+++ b/Documentation/git-stash.txt\n@@ -9,7 +9,7 @@ SYNOPSIS\n --------\n [verse]\n 'git stash' list [<options>]\n-'git stash' show [<stash>]\n+'git stash' show [<options>] [<stash>]\n 'git stash' drop [-q|--quiet] [<stash>]\n 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n 'git stash' branch <branchname> [<stash>]\n@@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash\n The command takes options applicable to the 'git log'\n command to control what is shown and how. See linkgit:git-log[1].\n \n-show [<stash>]::\n+show [<options>] [<stash>]::\n \n \tShow the changes recorded in the stash entry as a diff between the\n \tstashed contents and the commit back when the stash entry was first\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 1dba7e6853..02b593e0cd 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -15,7 +15,7 @@\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -29,7 +29,7 @@ static const char * const git_stash_helper_list_usage[] = {\n };\n \n static const char * const git_stash_helper_show_usage[] = {\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tNULL\n };\n \n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357043","messageId":"dbda179594b478686b8999e445ebe84f10b96ac4.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 10/20] stash: convert list to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:40Z","receivedAt":"2018-08-30T21:41:56Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash list to the helper and delete the list_stash function\nfrom the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++\n git-stash.sh            |  7 +------\n 2 files changed, 32 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 9fb1003dbb..c42a297078 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,7 @@\n #include \"rerere.h\"\n \n static const char * const git_stash_helper_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_list_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\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@@ -609,6 +615,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int list_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_list_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (!ref_exists(ref_stash))\n+\t\treturn 0;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"log\", \"--format=%gd: %gs\", \"-g\",\n+\t\t\t \"--first-parent\", \"-m\", NULL);\n+\targv_array_pushv(&cp.args, argv);\n+\targv_array_push(&cp.args, ref_stash);\n+\targv_array_push(&cp.args, \"--\");\n+\treturn run_command(&cp);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -639,6 +668,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"list\"))\n+\t\treturn !!list_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 8f2640fe90..6052441aa2 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -382,11 +382,6 @@ have_stash () {\n \tgit rev-parse --verify --quiet $ref_stash >/dev/null\n }\n \n-list_stash () {\n-\thave_stash || return 0\n-\tgit log --format=\"%gd: %gs\" -g --first-parent -m \"$@\" $ref_stash --\n-}\n-\n show_stash () {\n \tALLOW_UNKNOWN_FLAGS=t\n \tassert_stash_like \"$@\"\n@@ -574,7 +569,7 @@ test -n \"$seen_non_option\" || set \"push\" \"$@\"\n case \"$1\" in\n list)\n \tshift\n-\tlist_stash \"$@\"\n+\tgit stash--helper list \"$@\"\n \t;;\n show)\n \tshift\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357046","messageId":"7bbac617d67349182bbab2c67827639060b25e2f.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 16/20] stash: make push -q quiet","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:46Z","receivedAt":"2018-08-30T21:41:56Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"There is a change in behaviour with this commit. When there was\nno initial commit, the shell version of stash would still display\na message. This commit makes `push` to not display any message if\n`--quiet` or `-q` is specified.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 47 ++++++++++++++++++++++++++---------------\n t/t3903-stash.sh        | 23 ++++++++++++++++++++\n 2 files changed, 53 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 23670321d8..e5153a63ea 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -938,7 +938,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \n static struct strbuf patch = STRBUF_INIT;\n \n-static int stash_patch(struct stash_info *info, struct pathspec ps)\n+static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n {\n \tint i;\n \tint ret = 0;\n@@ -991,7 +991,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps)\n \t}\n \n \tif (!patch.len) {\n-\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n \t\tret = 1;\n \t}\n \n@@ -1069,7 +1070,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \n static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \t\t\t   int include_untracked, int patch_mode,\n-\t\t\t   struct stash_info *info)\n+\t\t\t   struct stash_info *info, int quiet)\n {\n \tint untracked_commit_option = 0;\n \tint ret = 0;\n@@ -1094,7 +1095,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \t}\n \n \tif (get_oid(\"HEAD\", &info->b_commit)) {\n-\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n \t\tret = -1;\n \t\t*stash_msg = NULL;\n \t\tgoto done;\n@@ -1115,7 +1117,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n \t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n \t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n-\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n \t\tret = -1;\n \t\t*stash_msg = NULL;\n \t\tgoto done;\n@@ -1124,7 +1127,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \tif (include_untracked && get_untracked_files(ps, include_untracked,\n \t\t\t\t\t\t     &out)) {\n \t\tif (save_untracked_files(info, &msg, &out)) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n \t\t\tret = -1;\n \t\t\t*stash_msg = NULL;\n \t\t\tgoto done;\n@@ -1132,17 +1136,19 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \t\tuntracked_commit_option = 1;\n \t}\n \tif (patch_mode) {\n-\t\tret = stash_patch(info, ps);\n+\t\tret = stash_patch(info, ps, quiet);\n \t\t*stash_msg = NULL;\n \t\tif (ret < 0) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n \t\t\tgoto done;\n \t\t} else if (ret > 0) {\n \t\t\tgoto done;\n \t\t}\n \t} else {\n \t\tif (stash_working_tree(info, ps)) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n \t\t\tret = -1;\n \t\t\t*stash_msg = NULL;\n \t\t\tgoto done;\n@@ -1168,7 +1174,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \n \tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n \t\t\tparents, &info->w_commit, NULL, NULL)) {\n-\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1201,7 +1208,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     0);\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);\n+\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info, 0);\n \n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n@@ -1255,28 +1262,33 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n \t\treturn -1;\n \n \tif (!check_changes(ps, include_untracked)) {\n-\t\tprintf_ln(_(\"No local changes to save\"));\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"No local changes to save\"));\n \t\treturn 0;\n \t}\n \n \tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n-\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n \t\treturn -1;\n \t}\n \n \tif (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,\n-\t\t\t    &info)) {\n+\t\t\t    &info, quiet)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n \tif (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {\n-\t\tfprintf(stderr, _(\"Cannot save the current status\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current status\"));\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n-\tprintf_ln(_(\"Saved working directory and index state %s\"), stash_msg);\n+\tif (!quiet)\n+\t\tprintf_ln(_(\"Saved working directory and index state %s\"),\n+\t\t\tstash_msg);\n \n \tif (!patch_mode) {\n \t\tif (include_untracked && !ps.nr) {\n@@ -1375,7 +1387,8 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n \t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n \n \t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n \t\t\tret = -1;\n \t\t\tgoto done;\n \t\t}\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex 3114c7bc4c..ed4611d3d8 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -1064,6 +1064,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '\n \ttest_path_is_file untracked\n '\n \n+test_expect_success 'push: -q is quiet with changes' '\n+\t>foo &&\n+\tgit add foo &&\n+\tgit stash push -q >output 2>&1 &&\n+\ttest_must_be_empty output\n+'\n+\n+test_expect_success 'push: -q is quiet with no changes' '\n+\tgit stash push -q >output 2>&1 &&\n+\ttest_must_be_empty output\n+'\n+\n+test_expect_success 'push: -q is quiet even if there is no initial commit' '\n+\tgit init foo_dir &&\n+\ttest_when_finished rm -rf foo_dir &&\n+\t(\n+\t\tcd foo_dir &&\n+\t\t>bar &&\n+\t\ttest_must_fail git stash push -q >output 2>&1 &&\n+\t\ttest_must_be_empty output\n+\t)\n+'\n+\n test_expect_success 'untracked files are left in place when -u is not given' '\n \t>file &&\n \tgit add file &&\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357047","messageId":"6832c979c8dca2a3584ed3cf828a6de060413cda.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 17/20] stash: convert save to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:47Z","receivedAt":"2018-08-30T21:41:57Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash save to the helper and delete functions which are no\nlonger needed (`show_help()`, `save_stash()`, `push_stash()`,\n`create_stash()`, `clear_stash()`, `untracked_files()` and\n`no_changes()`).\n\nThe `-m` option is no longer supported as it might not make\nsense to have two ways of passing a message. Even if this is\na change in behaviour, the documentation remains the same\nbecause the `-m` parameter was omitted before.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c |  52 +++++++\n git-stash.sh            | 311 +---------------------------------------\n 2 files changed, 54 insertions(+), 309 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex e5153a63ea..1269f2548c 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -24,6 +24,8 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n+\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n \n@@ -79,6 +81,12 @@ static const char * const git_stash_helper_push_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_save_usage[] = {\n+\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -1444,6 +1452,48 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     include_untracked);\n }\n \n+static int save_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint i;\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tint ret = 0;\n+\tconst char *stash_msg = NULL;\n+\tchar *to_free = NULL;\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n+\tstruct pathspec ps;\n+\tstruct option options[] = {\n+\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n+\t\t\tN_(\"keep index\"), 1),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\tN_(\"stash in patch mode\")),\n+\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_save_usage,\n+\t\t\t     0);\n+\n+\tfor (i = 0; i < argc; ++i)\n+\t\tstrbuf_addf(&stash_msg_buf, \"%s \", argv[i]);\n+\tstash_msg = strbuf_detach(&stash_msg_buf, NULL);\n+\tto_free = (char *) stash_msg;\n+\n+\tmemset(&ps, 0, sizeof(ps));\n+\tret = do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n+\t\t\t    include_untracked);\n+\n+\tfree(to_free);\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1484,6 +1534,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!create_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"push\"))\n \t\treturn !!push_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"save\"))\n+\t\treturn !!save_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex c3146f62ab..695f1feba3 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -36,314 +36,6 @@ else\n        reset_color=\n fi\n \n-no_changes () {\n-\tgit diff-index --quiet --cached HEAD --ignore-submodules -- \"$@\" &&\n-\tgit diff-files --quiet --ignore-submodules -- \"$@\" &&\n-\t(test -z \"$untracked\" || test -z \"$(untracked_files \"$@\")\")\n-}\n-\n-untracked_files () {\n-\tif test \"$1\" = \"-z\"\n-\tthen\n-\t\tshift\n-\t\tz=-z\n-\telse\n-\t\tz=\n-\tfi\n-\texcl_opt=--exclude-standard\n-\ttest \"$untracked\" = \"all\" && excl_opt=\n-\tgit ls-files -o $z $excl_opt -- \"$@\"\n-}\n-\n-clear_stash () {\n-\tif test $# != 0\n-\tthen\n-\t\tdie \"$(gettext \"git stash clear with parameters is unimplemented\")\"\n-\tfi\n-\tif current=$(git rev-parse --verify --quiet $ref_stash)\n-\tthen\n-\t\tgit update-ref -d $ref_stash $current\n-\tfi\n-}\n-\n-create_stash () {\n-\tstash_msg=\n-\tuntracked=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\tstash_msg=${1?\"BUG: create_stash () -m requires an argument\"}\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t-u|--include-untracked)\n-\t\t\tshift\n-\t\t\tuntracked=${1?\"BUG: create_stash () -u requires an argument\"}\n-\t\t\t;;\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\tgit update-index -q --refresh\n-\tif no_changes \"$@\"\n-\tthen\n-\t\texit 0\n-\tfi\n-\n-\t# state of the base commit\n-\tif b_commit=$(git rev-parse --verify HEAD)\n-\tthen\n-\t\thead=$(git rev-list --oneline -n 1 HEAD --)\n-\telse\n-\t\tdie \"$(gettext \"You do not have the initial commit yet\")\"\n-\tfi\n-\n-\tif branch=$(git symbolic-ref -q HEAD)\n-\tthen\n-\t\tbranch=${branch#refs/heads/}\n-\telse\n-\t\tbranch='(no branch)'\n-\tfi\n-\tmsg=$(printf '%s: %s' \"$branch\" \"$head\")\n-\n-\t# state of the index\n-\ti_tree=$(git write-tree) &&\n-\ti_commit=$(printf 'index on %s\\n' \"$msg\" |\n-\t\tgit commit-tree $i_tree -p $b_commit) ||\n-\t\tdie \"$(gettext \"Cannot save the current index state\")\"\n-\n-\tif test -n \"$untracked\"\n-\tthen\n-\t\t# Untracked files are stored by themselves in a parentless commit, for\n-\t\t# ease of unpacking later.\n-\t\tu_commit=$(\n-\t\t\tuntracked_files -z \"$@\" | (\n-\t\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n-\t\t\t\texport GIT_INDEX_FILE &&\n-\t\t\t\trm -f \"$TMPindex\" &&\n-\t\t\t\tgit update-index -z --add --remove --stdin &&\n-\t\t\t\tu_tree=$(git write-tree) &&\n-\t\t\t\tprintf 'untracked files on %s\\n' \"$msg\" | git commit-tree $u_tree  &&\n-\t\t\t\trm -f \"$TMPindex\"\n-\t\t) ) || die \"$(gettext \"Cannot save the untracked files\")\"\n-\n-\t\tuntracked_commit_option=\"-p $u_commit\";\n-\telse\n-\t\tuntracked_commit_option=\n-\tfi\n-\n-\tif test -z \"$patch_mode\"\n-\tthen\n-\n-\t\t# state of the working tree\n-\t\tw_tree=$( (\n-\t\t\tgit read-tree --index-output=\"$TMPindex\" -m $i_tree &&\n-\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n-\t\t\texport GIT_INDEX_FILE &&\n-\t\t\tgit diff-index --name-only -z HEAD -- \"$@\" >\"$TMP-stagenames\" &&\n-\t\t\tgit update-index -z --add --remove --stdin <\"$TMP-stagenames\" &&\n-\t\t\tgit write-tree &&\n-\t\t\trm -f \"$TMPindex\"\n-\t\t) ) ||\n-\t\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n-\n-\telse\n-\n-\t\trm -f \"$TMP-index\" &&\n-\t\tGIT_INDEX_FILE=\"$TMP-index\" git read-tree HEAD &&\n-\n-\t\t# find out what the user wants\n-\t\tGIT_INDEX_FILE=\"$TMP-index\" \\\n-\t\t\tgit add--interactive --patch=stash -- \"$@\" &&\n-\n-\t\t# state of the working tree\n-\t\tw_tree=$(GIT_INDEX_FILE=\"$TMP-index\" git write-tree) ||\n-\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n-\n-\t\tgit diff-tree -p HEAD $w_tree -- >\"$TMP-patch\" &&\n-\t\ttest -s \"$TMP-patch\" ||\n-\t\tdie \"$(gettext \"No changes selected\")\"\n-\n-\t\trm -f \"$TMP-index\" ||\n-\t\tdie \"$(gettext \"Cannot remove temporary index (can't happen)\")\"\n-\n-\tfi\n-\n-\t# create the stash\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tstash_msg=$(printf 'WIP on %s' \"$msg\")\n-\telse\n-\t\tstash_msg=$(printf 'On %s: %s' \"$branch\" \"$stash_msg\")\n-\tfi\n-\tw_commit=$(printf '%s\\n' \"$stash_msg\" |\n-\tgit commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||\n-\tdie \"$(gettext \"Cannot record working tree state\")\"\n-}\n-\n-push_stash () {\n-\tkeep_index=\n-\tpatch_mode=\n-\tuntracked=\n-\tstash_msg=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-k|--keep-index)\n-\t\t\tkeep_index=t\n-\t\t\t;;\n-\t\t--no-keep-index)\n-\t\t\tkeep_index=n\n-\t\t\t;;\n-\t\t-p|--patch)\n-\t\t\tpatch_mode=t\n-\t\t\t# only default to keep if we don't already have an override\n-\t\t\ttest -z \"$keep_index\" && keep_index=t\n-\t\t\t;;\n-\t\t-q|--quiet)\n-\t\t\tGIT_QUIET=t\n-\t\t\t;;\n-\t\t-u|--include-untracked)\n-\t\t\tuntracked=untracked\n-\t\t\t;;\n-\t\t-a|--all)\n-\t\t\tuntracked=all\n-\t\t\t;;\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\ttest -z ${1+x} && usage\n-\t\t\tstash_msg=$1\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t--help)\n-\t\t\tshow_help\n-\t\t\t;;\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\t-*)\n-\t\t\toption=\"$1\"\n-\t\t\teval_gettextln \"error: unknown option for 'stash push': \\$option\"\n-\t\t\tusage\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\teval \"set $(git rev-parse --sq --prefix \"$prefix\" -- \"$@\")\"\n-\n-\tif test -n \"$patch_mode\" && test -n \"$untracked\"\n-\tthen\n-\t\tdie \"$(gettext \"Can't use --patch and --include-untracked or --all at the same time\")\"\n-\tfi\n-\n-\ttest -n \"$untracked\" || git ls-files --error-unmatch -- \"$@\" >/dev/null || exit 1\n-\n-\tgit update-index -q --refresh\n-\tif no_changes \"$@\"\n-\tthen\n-\t\tsay \"$(gettext \"No local changes to save\")\"\n-\t\texit 0\n-\tfi\n-\n-\tgit reflog exists $ref_stash ||\n-\t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n-\n-\tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n-\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n-\tdie \"$(gettext \"Cannot save the current status\")\"\n-\tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n-\n-\tif test -z \"$patch_mode\"\n-\tthen\n-\t\ttest \"$untracked\" = \"all\" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=\n-\t\tif test -n \"$untracked\" && test $# = 0\n-\t\tthen\n-\t\t\tgit clean --force --quiet -d $CLEAN_X_OPTION\n-\t\tfi\n-\n-\t\tif test $# != 0\n-\t\tthen\n-\t\t\ttest -z \"$untracked\" && UPDATE_OPTION=\"-u\" || UPDATE_OPTION=\n-\t\t\ttest \"$untracked\" = \"all\" && FORCE_OPTION=\"--force\" || FORCE_OPTION=\n-\t\t\tgit add $UPDATE_OPTION $FORCE_OPTION -- \"$@\"\n-\t\t\tgit diff-index -p --cached --binary HEAD -- \"$@\" |\n-\t\t\tgit apply --index -R\n-\t\telse\n-\t\t\tgit reset --hard -q\n-\t\tfi\n-\n-\t\tif test \"$keep_index\" = \"t\" && test -n \"$i_tree\"\n-\t\tthen\n-\t\t\tgit read-tree --reset $i_tree\n-\t\t\tgit ls-files -z --modified -- \"$@\" |\n-\t\t\tgit checkout-index -z --force --stdin\n-\t\tfi\n-\telse\n-\t\tgit apply -R < \"$TMP-patch\" ||\n-\t\tdie \"$(gettext \"Cannot remove worktree changes\")\"\n-\n-\t\tif test \"$keep_index\" != \"t\"\n-\t\tthen\n-\t\t\tgit reset -q -- \"$@\"\n-\t\tfi\n-\tfi\n-}\n-\n-save_stash () {\n-\tpush_options=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\t-*)\n-\t\t\t# pass all options through to push_stash\n-\t\t\tpush_options=\"$push_options $1\"\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\tstash_msg=\"$*\"\n-\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tpush_stash $push_options\n-\telse\n-\t\tpush_stash $push_options -m \"$stash_msg\"\n-\tfi\n-}\n-\n-show_help () {\n-\texec git help stash\n-\texit 1\n-}\n-\n #\n # Parses the remaining options looking for flags and\n # at most one revision defaulting to ${ref_stash}@{0}\n@@ -408,7 +100,8 @@ show)\n \t;;\n save)\n \tshift\n-\tsave_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper save \"$@\"\n \t;;\n push)\n \tshift\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357049","messageId":"e4c17b20b0204386be06270e4a7f7283e6641f5a.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 19/20] stash: optimize `get_untracked_files()` and `check_changes()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:49Z","receivedAt":"2018-08-30T21:41:59Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commits introduces a optimization by avoiding calling the\nsame functions again. For example, `git stash push -u`\nwould call at some points the following functions:\n\n * `check_changes()` (inside `do_push_stash()`)\n * `do_create_stash()`, which calls: `check_changes()` and\n`get_untracked_files()`\n\nNote that `check_changes()` also calls `get_untracked_files()`.\nSo, `check_changes()` is called 2 times and `get_untracked_files()`\n3 times.\n\n`get_untracked_files()` has now only two parameters and it will\nfill a global strbuf called `untracked_files`.\n\nThe old function `check_changes()` now consists of two functions:\n`get_untracked_files()` and `check_changes_tracked_files()`.\n\nThese are the call chains for `push` and `create`:\n\n * `push_stash()` -> `do_push_stash()` -> `do_create_stash()`\n\n * `create_stash()` -> `do_create_stash()`\n\nTo prevent calling the same functions over and over again,\n`check_changes()` inside `do_create_stash()` is now placed\nin the caller functions (`create_stash()` and `do_push_stash()`).\nThis way `check_changes()` and `get_untracked files()` are called\nonly one time.\n\nhttps://public-inbox.org/git/20180818223329.GJ11326@hank.intra.tgummerer.com/\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash.c | 73 ++++++++++++++++++++++---------------------------\n 1 file changed, 33 insertions(+), 40 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 3d2316e3f7..ba5818e24e 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -813,13 +813,15 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n }\n \n /*\n- * `out` will be filled with the names of untracked files. The return value is:\n+ * `untracked_files` will be filled with the names of untracked files.\n+ * The return value is:\n  *\n  * = 0 if there are not any untracked files\n  * > 0 if there are untracked files\n  */\n-static int get_untracked_files(struct pathspec ps, int include_untracked,\n-\t\t\t       struct strbuf *out)\n+static struct strbuf untracked_files = STRBUF_INIT;\n+\n+static int get_untracked_files(struct pathspec ps, int include_untracked)\n {\n \tint max_len;\n \tint i;\n@@ -839,7 +841,7 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,\n \t\t\tfree(ent);\n \t\t\tcontinue;\n \t\t}\n-\t\tstrbuf_addf(out, \"%s%c\", ent->name, '\\0');\n+\t\tstrbuf_addf(&untracked_files, \"%s%c\", ent->name, '\\0');\n \t\tfree(ent);\n \t}\n \n@@ -847,23 +849,22 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,\n \tfree(dir.ignored);\n \tclear_directory(&dir);\n \tfree(seen);\n-\treturn out->len;\n+\treturn untracked_files.len;\n }\n \n /*\n- * The return value of `check_changes()` can be:\n+ * The return value of `check_changes_tracked_files()` can be:\n  *\n  * < 0 if there was an error\n  * = 0 if there are no changes.\n  * > 0 if there are changes.\n  */\n-static int check_changes(struct pathspec ps, int include_untracked)\n+\n+static int check_changes_tracked_files(struct pathspec ps)\n {\n \tint result;\n-\tint ret = 0;\n \tstruct rev_info rev;\n \tstruct object_id dummy;\n-\tstruct strbuf out = STRBUF_INIT;\n \n \tinit_revisions(&rev, NULL);\n \trev.prune_data = ps;\n@@ -890,18 +891,22 @@ static int check_changes(struct pathspec ps, int include_untracked)\n \tif (diff_result_code(&rev.diffopt, result))\n \t\treturn 1;\n \n-\tif (include_untracked && get_untracked_files(ps, include_untracked,\n-\t\t\t\t\t\t     &out)) {\n-\t\tstrbuf_release(&out);\n-\t\treturn 1;\n-\t}\n-\n-\tstrbuf_release(&out);\n \treturn 0;\n }\n \n-static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n-\t\t\t\tstruct strbuf *in)\n+static int check_changes(struct pathspec ps, int include_untracked)\n+{\n+\tint ret = 0;\n+\tif (check_changes_tracked_files(ps))\n+\t\tret = 1;\n+\n+\tif (include_untracked && get_untracked_files(ps, include_untracked))\n+\t\tret = 1;\n+\n+\treturn ret;\n+}\n+\n+static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n {\n \tint ret = 0;\n \tstruct strbuf untracked_msg = STRBUF_INIT;\n@@ -916,7 +921,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \t\t\t stash_index_path.buf);\n \n \tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n-\tif (pipe_command(&cp_upd_index, in->buf, in->len, NULL, 0, NULL, 0)) {\n+\tif (pipe_command(&cp_upd_index, untracked_files.buf, untracked_files.len,\n+\t\t\t NULL, 0, NULL, 0)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1090,18 +1096,11 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \tstruct commit_list *parents = NULL;\n \tstruct strbuf msg = STRBUF_INIT;\n \tstruct strbuf commit_tree_label = STRBUF_INIT;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n \n \tread_cache_preload(NULL);\n \trefresh_cache(REFRESH_QUIET);\n \n-\tif (!check_changes(ps, include_untracked)) {\n-\t\tret = 1;\n-\t\t*stash_msg = NULL;\n-\t\tgoto done;\n-\t}\n-\n \tif (get_oid(\"HEAD\", &info->b_commit)) {\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n@@ -1132,9 +1131,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \t\tgoto done;\n \t}\n \n-\tif (include_untracked && get_untracked_files(ps, include_untracked,\n-\t\t\t\t\t\t     &out)) {\n-\t\tif (save_untracked_files(info, &msg, &out)) {\n+\tif (include_untracked) {\n+\t\tif (save_untracked_files(info, &msg)) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n \t\t\tret = -1;\n@@ -1191,7 +1189,6 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n done:\n \tstrbuf_release(&commit_tree_label);\n \tstrbuf_release(&msg);\n-\tstrbuf_release(&out);\n \tstrbuf_release(&stash_msg_buf);\n \treturn ret;\n }\n@@ -1213,25 +1210,21 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     git_stash_create_usage,\n \t\t\t     0);\n \n+\tmemset(&ps, 0, sizeof(ps));\n+\tif (!check_changes_tracked_files(ps))\n+\t\treturn 0;\n+\n \tfor (i = 0; i < argc; ++i)\n \t\tstrbuf_addf(&stash_msg_buf, \"%s \", argv[i]);\n \tstash_msg = strbuf_detach(&stash_msg_buf, NULL);\n \tto_free = (char *) stash_msg;\n \n-\tmemset(&ps, 0, sizeof(ps));\n-\tret = do_create_stash(ps, &stash_msg, 0, 0, &info, 0);\n-\n-\tif (!ret)\n+\tif (!(ret = do_create_stash(ps, &stash_msg, 0, 0, &info, 0)))\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n \tfree(to_free);\n \tfree((char *) stash_msg);\n-\n-\t/*\n-\t * ret can be 1 if there were no changes. In this case, we should\n-\t * not error out.\n-\t */\n-\treturn ret < 0;\n+\treturn ret;\n }\n \n static void add_ps_items_to_argv_array(struct argv_array *args,\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357050","messageId":"82f5af08c6475765fac27e30f716a48039d56404.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 18/20] stash: convert `stash--helper.c` into `stash.c`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:48Z","receivedAt":"2018-08-30T21:41:59Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"The old shell script `git-stash.sh`  was removed and replaced\nentirely by `builtin/stash.c`. In order to do that, `create` and\n`push` were adapted to work without `stash.sh`. For example, before\nthis commit, `git stash create` called `git stash--helper create\n--message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\nsome of these changes wouldn't have been necessary.\n\nThis commit also removes the word `helper` since now stash is\ncalled directly and not by a shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n .gitignore                           |   1 -\n Makefile                             |   3 +-\n builtin.h                            |   2 +-\n builtin/{stash--helper.c => stash.c} | 161 +++++++++++++++++----------\n git-stash.sh                         | 153 -------------------------\n git.c                                |   2 +-\n 6 files changed, 106 insertions(+), 216 deletions(-)\n rename builtin/{stash--helper.c => stash.c} (90%)\n delete mode 100755 git-stash.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex b59661cb88..ffceea7d59 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -157,7 +157,6 @@\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 f900c68e69..ac1787d113 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -617,7 +617,6 @@ SCRIPT_SH += git-quiltimport.sh\n SCRIPT_SH += git-rebase.sh\n SCRIPT_SH += git-remote-testgit.sh\n SCRIPT_SH += git-request-pull.sh\n-SCRIPT_SH += git-stash.sh\n SCRIPT_SH += git-submodule.sh\n SCRIPT_SH += git-web--browse.sh\n \n@@ -1093,7 +1092,7 @@ BUILTIN_OBJS += builtin/shortlog.o\n BUILTIN_OBJS += builtin/show-branch.o\n BUILTIN_OBJS += builtin/show-index.o\n BUILTIN_OBJS += builtin/show-ref.o\n-BUILTIN_OBJS += builtin/stash--helper.o\n+BUILTIN_OBJS += builtin/stash.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 317bc338f7..e60f0fc58f 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -223,7 +223,7 @@ 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_show_index(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_stash(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.c\nsimilarity index 90%\nrename from builtin/stash--helper.c\nrename to builtin/stash.c\nindex 1269f2548c..3d2316e3f7 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash.c\n@@ -14,75 +14,75 @@\n #include \"log-tree.h\"\n #include \"diffcore.h\"\n \n-static const char * const git_stash_helper_usage[] = {\n-\tN_(\"git stash--helper list [<options>]\"),\n-\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n-\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n-\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n-\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n-\tN_(\"git stash--helper clear\"),\n-\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_usage[] = {\n+\tN_(\"git stash list [<options>]\"),\n+\tN_(\"git stash show [<options>] [<stash>]\"),\n+\tN_(\"git stash drop [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash branch <branchname> [<stash>]\"),\n+\tN_(\"git stash clear\"),\n+\tN_(\"git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n-\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\tN_(\"git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_list_usage[] = {\n-\tN_(\"git stash--helper list [<options>]\"),\n+static const char * const git_stash_list_usage[] = {\n+\tN_(\"git stash list [<options>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_show_usage[] = {\n-\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n+static const char * const git_stash_show_usage[] = {\n+\tN_(\"git stash show [<options>] [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_drop_usage[] = {\n-\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n+static const char * const git_stash_drop_usage[] = {\n+\tN_(\"git stash drop [-q|--quiet] [<stash>]\"),\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+static const char * const git_stash_pop_usage[] = {\n+\tN_(\"git stash 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+static const char * const git_stash_apply_usage[] = {\n+\tN_(\"git stash apply [--index] [-q|--quiet] [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_branch_usage[] = {\n-\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n+static const char * const git_stash_branch_usage[] = {\n+\tN_(\"git stash branch <branchname> [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_clear_usage[] = {\n-\tN_(\"git stash--helper clear\"),\n+static const char * const git_stash_clear_usage[] = {\n+\tN_(\"git stash clear\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_store_usage[] = {\n-\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+static const char * const git_stash_store_usage[] = {\n+\tN_(\"git stash store [-m|--message <message>] [-q|--quiet] <commit>\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_create_usage[] = {\n-\tN_(\"git stash--helper create [<message>]\"),\n+static const char * const git_stash_create_usage[] = {\n+\tN_(\"git stash create [<message>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_push_usage[] = {\n-\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_push_usage[] = {\n+\tN_(\"git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_save_usage[] = {\n-\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_save_usage[] = {\n+\tN_(\"git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n@@ -222,7 +222,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_clear_usage,\n+\t\t\t     git_stash_clear_usage,\n \t\t\t     PARSE_OPT_STOP_AT_NON_OPTION);\n \n \tif (argc)\n@@ -517,7 +517,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_apply_usage, 0);\n+\t\t\t     git_stash_apply_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -589,7 +589,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_drop_usage, 0);\n+\t\t\t     git_stash_drop_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -613,7 +613,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_pop_usage, 0);\n+\t\t\t     git_stash_pop_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -639,7 +639,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_branch_usage, 0);\n+\t\t\t     git_stash_branch_usage, 0);\n \n \tif (!argc)\n \t\treturn error(_(\"No branch name specified\"));\n@@ -672,7 +672,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_list_usage,\n+\t\t\t     git_stash_list_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n \n \tif (!ref_exists(ref_stash))\n@@ -752,7 +752,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n \targc = setup_revisions(argc, argv, &rev, NULL);\n \tif (argc > 1) {\n \t\tfree_stash_info(&info);\n-\t\tusage_with_options(git_stash_helper_show_usage, options);\n+\t\tusage_with_options(git_stash_show_usage, options);\n \t}\n \n \trev.diffopt.flags.recursive = 1;\n@@ -801,7 +801,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_store_usage,\n+\t\t\t     git_stash_store_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n \n \tif (argc != 1) {\n@@ -1198,34 +1198,39 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n \n static int create_stash(int argc, const char **argv, const char *prefix)\n {\n-\tint include_untracked = 0;\n+\tint i;\n \tint ret = 0;\n+\tchar *to_free = NULL;\n \tconst char *stash_msg = NULL;\n \tstruct stash_info info;\n \tstruct pathspec ps;\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n \tstruct option options[] = {\n-\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n-\t\t\t N_(\"include untracked files in stash\")),\n-\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n-\t\t\t N_(\"stash message\")),\n \t\tOPT_END()\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_create_usage,\n+\t\t\t     git_stash_create_usage,\n \t\t\t     0);\n \n+\tfor (i = 0; i < argc; ++i)\n+\t\tstrbuf_addf(&stash_msg_buf, \"%s \", argv[i]);\n+\tstash_msg = strbuf_detach(&stash_msg_buf, NULL);\n+\tto_free = (char *) stash_msg;\n+\n \tmemset(&ps, 0, sizeof(ps));\n-\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info, 0);\n+\tret = do_create_stash(ps, &stash_msg, 0, 0, &info, 0);\n \n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n+\tfree(to_free);\n+\tfree((char *) stash_msg);\n+\n \t/*\n \t * ret can be 1 if there were no changes. In this case, we should\n \t * not error out.\n \t */\n-\tfree((char *) stash_msg);\n \treturn ret < 0;\n }\n \n@@ -1443,9 +1448,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_push_usage,\n-\t\t\t     0);\n+\tif (argc)\n+\t\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t\t     git_stash_push_usage,\n+\t\t\t\t     0);\n \n \tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n \treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n@@ -1478,7 +1484,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_save_usage,\n+\t\t\t     git_stash_save_usage,\n \t\t\t     0);\n \n \tfor (i = 0; i < argc; ++i)\n@@ -1494,10 +1500,12 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n-int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n+int cmd_stash(int argc, const char **argv, const char *prefix)\n {\n+\tint i = -1;\n \tpid_t pid = getpid();\n \tconst char *index_file;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n \n \tstruct option options[] = {\n \t\tOPT_END()\n@@ -1505,16 +1513,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \n \tgit_config(git_default_config, NULL);\n \n-\targc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,\n+\targc = parse_options(argc, argv, prefix, options, git_stash_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n \n \tindex_file = get_index_file();\n \tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n \t\t    (uintmax_t)pid);\n \n-\tif (argc < 1)\n-\t\tusage_with_options(git_stash_helper_usage, options);\n-\tif (!strcmp(argv[0], \"apply\"))\n+\tif (argc == 0)\n+\t\treturn !!push_stash(0, NULL, prefix);\n+\telse if (!strcmp(argv[0], \"apply\"))\n \t\treturn !!apply_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"clear\"))\n \t\treturn !!clear_stash(argc, argv, prefix);\n@@ -1536,7 +1544,44 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!push_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"save\"))\n \t\treturn !!save_stash(argc, argv, prefix);\n+\telse if (*argv[0] != '-')\n+\t\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n+\t\t\t      git_stash_usage, options);\n+\n+\tif (strcmp(argv[0], \"-p\")) {\n+\t\twhile (++i < argc && strcmp(argv[i], \"--\")) {\n+\t\t\t/*\n+\t\t\t * `akpqu` is a string which contains all short options,\n+\t\t\t * except `-m` which is verified separately.\n+\t\t\t */\n+\t\t\tif ((strlen(argv[i]) == 2) && *argv[i] == '-' &&\n+\t\t\t    strchr(\"akpqu\", argv[i][1]))\n+\t\t\t\tcontinue;\n+\n+\t\t\tif (!strcmp(argv[i], \"--all\") ||\n+\t\t\t    !strcmp(argv[i], \"--keep-index\") ||\n+\t\t\t    !strcmp(argv[i], \"--no-keep-index\") ||\n+\t\t\t    !strcmp(argv[i], \"--patch\") ||\n+\t\t\t    !strcmp(argv[i], \"--quiet\") ||\n+\t\t\t    !strcmp(argv[i], \"--include-untracked\"))\n+\t\t\t\tcontinue;\n+\n+\t\t\t/*\n+\t\t\t * `-m` and `--message=` are verified separately because\n+\t\t\t * they need to be immediately followed by a string\n+\t\t\t * (i.e.`-m\"foobar\"` or `--message=\"foobar\"`).\n+\t\t\t */\n+\t\t\tif ((strlen(argv[i]) > 2 &&\n+\t\t\t     !strncmp(argv[i], \"-m\", 2)) ||\n+\t\t\t    (strlen(argv[i]) > 10 &&\n+\t\t\t     !strncmp(argv[i], \"--message=\", 10)))\n+\t\t\t\tcontinue;\n+\n+\t\t\tusage_with_options(git_stash_usage, options);\n+\t\t}\n+\t}\n \n-\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n-\t\t      git_stash_helper_usage, options);\n+\targv_array_push(&args, \"push\");\n+\targv_array_pushv(&args, argv);\n+\treturn !!push_stash(args.argc, args.argv, prefix);\n }\ndiff --git a/git-stash.sh b/git-stash.sh\ndeleted file mode 100755\nindex 695f1feba3..0000000000\n--- a/git-stash.sh\n+++ /dev/null\n@@ -1,153 +0,0 @@\n-#!/bin/sh\n-# Copyright (c) 2007, Nanako Shiraishi\n-\n-dashless=$(basename \"$0\" | sed -e 's/-/ /')\n-USAGE=\"list [<options>]\n-   or: $dashless show [<stash>]\n-   or: $dashless drop [-q|--quiet] [<stash>]\n-   or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n-   or: $dashless branch <branchname> [<stash>]\n-   or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n-\t\t      [-u|--include-untracked] [-a|--all] [<message>]\n-   or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n-\t\t       [-u|--include-untracked] [-a|--all] [-m <message>]\n-\t\t       [-- <pathspec>...]]\n-   or: $dashless clear\"\n-\n-SUBDIRECTORY_OK=Yes\n-OPTIONS_SPEC=\n-START_DIR=$(pwd)\n-. git-sh-setup\n-require_work_tree\n-prefix=$(git rev-parse --show-prefix) || exit 1\n-cd_to_toplevel\n-\n-TMP=\"$GIT_DIR/.git-stash.$$\"\n-TMPindex=${GIT_INDEX_FILE-\"$(git rev-parse --git-path index)\"}.stash.$$\n-trap 'rm -f \"$TMP-\"* \"$TMPindex\"' 0\n-\n-ref_stash=refs/stash\n-\n-if git config --get-colorbool color.interactive; then\n-       help_color=\"$(git config --get-color color.interactive.help 'red bold')\"\n-       reset_color=\"$(git config --get-color '' reset)\"\n-else\n-       help_color=\n-       reset_color=\n-fi\n-\n-#\n-# Parses the remaining options looking for flags and\n-# at most one revision defaulting to ${ref_stash}@{0}\n-# if none found.\n-#\n-# Derives related tree and commit objects from the\n-# revision, if one is found.\n-#\n-# stash records the work tree, and is a merge between the\n-# base commit (first parent) and the index tree (second parent).\n-#\n-#   REV is set to the symbolic version of the specified stash-like commit\n-#   IS_STASH_LIKE is non-blank if ${REV} looks like a stash\n-#   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref\n-#   s is set to the SHA1 of the stash commit\n-#   w_commit is set to the commit containing the working tree\n-#   b_commit is set to the base commit\n-#   i_commit is set to the commit containing the index tree\n-#   u_commit is set to the commit containing the untracked files tree\n-#   w_tree is set to the working tree\n-#   b_tree is set to the base tree\n-#   i_tree is set to the index tree\n-#   u_tree is set to the untracked files tree\n-#\n-#   GIT_QUIET is set to t if -q is specified\n-#   INDEX_OPTION is set to --index if --index is specified.\n-#   FLAGS is set to the remaining flags (if allowed)\n-#\n-# dies if:\n-#   * too many revisions specified\n-#   * no revision is specified and there is no stash stack\n-#   * a revision is specified which cannot be resolve to a SHA1\n-#   * a non-existent stash reference is specified\n-#   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n-#\n-\n-test \"$1\" = \"-p\" && set \"push\" \"$@\"\n-\n-PARSE_CACHE='--not-parsed'\n-# The default command is \"push\" if nothing but options are given\n-seen_non_option=\n-for opt\n-do\n-\tcase \"$opt\" in\n-\t--) break ;;\n-\t-*) ;;\n-\t*) seen_non_option=t; break ;;\n-\tesac\n-done\n-\n-test -n \"$seen_non_option\" || set \"push\" \"$@\"\n-\n-# Main command set\n-case \"$1\" in\n-list)\n-\tshift\n-\tgit stash--helper list \"$@\"\n-\t;;\n-show)\n-\tshift\n-\tgit stash--helper show \"$@\"\n-\t;;\n-save)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper save \"$@\"\n-\t;;\n-push)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper push \"$@\"\n-\t;;\n-apply)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper apply \"$@\"\n-\t;;\n-clear)\n-\tshift\n-\tgit stash--helper clear \"$@\"\n-\t;;\n-create)\n-\tshift\n-\tgit stash--helper create --message \"$*\"\n-\t;;\n-store)\n-\tshift\n-\tgit stash--helper store \"$@\"\n-\t;;\n-drop)\n-\tshift\n-\tgit stash--helper drop \"$@\"\n-\t;;\n-pop)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper pop \"$@\"\n-\t;;\n-branch)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper branch \"$@\"\n-\t;;\n-*)\n-\tcase $# in\n-\t0)\n-\t\tcd \"$START_DIR\"\n-\t\tgit stash--helper push &&\n-\t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n-\t\t;;\n-\t*)\n-\t\tusage\n-\tesac\n-\t;;\n-esac\ndiff --git a/git.c b/git.c\nindex 3c0e762d7d..78548397cf 100644\n--- a/git.c\n+++ b/git.c\n@@ -544,7 +544,7 @@ static struct cmd_struct commands[] = {\n \t{ \"show-index\", cmd_show_index },\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{ \"stash\", cmd_stash, 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 | NO_PARSEOPT },\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357048","messageId":"c26283d74e3f761a554940e3e0db13cef1b613d5.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 20/20] stash: replace all `write-tree` child processes with API calls","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:50Z","receivedAt":"2018-08-30T21:42:00Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commit replaces spawning `git write-tree` with API calls.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash.c | 41 ++++++++++++-----------------------------\n 1 file changed, 12 insertions(+), 29 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex ba5818e24e..dd1084afd4 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -910,9 +910,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n {\n \tint ret = 0;\n \tstruct strbuf untracked_msg = STRBUF_INIT;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n-\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\tstruct index_state istate = { NULL };\n \n \tcp_upd_index.git_cmd = 1;\n \targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n@@ -927,15 +926,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n \t\tgoto done;\n \t}\n \n-\tcp_write_tree.git_cmd = 1;\n-\targv_array_push(&cp_write_tree.args, \"write-tree\");\n-\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-\tget_oid_hex(out.buf, &info->u_tree);\n \n \tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n \t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n@@ -944,8 +939,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n \t}\n \n done:\n+\tdiscard_index(&istate);\n \tstrbuf_release(&untracked_msg);\n-\tstrbuf_release(&out);\n \tremove_path(stash_index_path.buf);\n \treturn ret;\n }\n@@ -956,11 +951,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n {\n \tint i;\n \tint ret = 0;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct child_process cp_read_tree = CHILD_PROCESS_INIT;\n \tstruct child_process cp_add_i = CHILD_PROCESS_INIT;\n-\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n+\tstruct index_state istate = { NULL };\n \n \tremove_path(stash_index_path.buf);\n \n@@ -985,17 +979,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n \t\tgoto done;\n \t}\n \n-\tcp_write_tree.git_cmd = 1;\n-\targv_array_push(&cp_write_tree.args, \"write-tree\");\n-\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n-\tget_oid_hex(out.buf, &info->w_tree);\n-\n \tcp_diff_tree.git_cmd = 1;\n \targv_array_pushl(&cp_diff_tree.args, \"diff-tree\", \"-p\", \"HEAD\",\n \t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n@@ -1011,7 +1000,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n \t}\n \n done:\n-\tstrbuf_release(&out);\n+\tdiscard_index(&istate);\n \tremove_path(stash_index_path.buf);\n \treturn ret;\n }\n@@ -1020,10 +1009,9 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n {\n \tint ret = 0;\n \tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n-\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct strbuf diff_output = STRBUF_INIT;\n \tstruct rev_info rev;\n+\tstruct index_state istate = { NULL };\n \n \tset_alternate_index_output(stash_index_path.buf);\n \tif (reset_tree(&info->i_tree, 0, 0)) {\n@@ -1062,20 +1050,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \t\tgoto done;\n \t}\n \n-\tcp_write_tree.git_cmd = 1;\n-\targv_array_push(&cp_write_tree.args, \"write-tree\");\n-\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n-\tget_oid_hex(out.buf, &info->w_tree);\n-\n done:\n+\tdiscard_index(&istate);\n \tUNLEAK(rev);\n-\tstrbuf_release(&out);\n \tobject_array_clear(&rev.pending);\n \tstrbuf_release(&diff_output);\n \tremove_path(stash_index_path.buf);\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357051","messageId":"18debd5c9b1dc11a763caac39e10d4e8d5894bcb.1535665109.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[GSoC][PATCH v8 15/20] stash: convert push to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-08-30T21:40:45Z","receivedAt":"2018-08-30T21:42:02Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash push to the helper.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 229 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   6 +-\n 2 files changed, 233 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex ce360a569d..23670321d8 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -21,6 +21,9 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n+\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n+\t   \"          [--] [<pathspec>...]]\"),\n \tNULL\n };\n \n@@ -69,6 +72,13 @@ static const char * const git_stash_helper_create_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_push_usage[] = {\n+\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n+\t   \"          [--] [<pathspec>...]]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static int quiet;\n static struct strbuf stash_index_path = STRBUF_INIT;\n@@ -1204,6 +1214,223 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \treturn ret < 0;\n }\n \n+static void add_ps_items_to_argv_array(struct argv_array *args,\n+\t\t\t\t       struct pathspec ps) {\n+\tint i;\n+\tfor (i = 0; i < ps.nr; ++i)\n+\t\targv_array_push(args, ps.items[i].match);\n+}\n+\n+static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n+\t\t\t int keep_index, int patch_mode, int include_untracked)\n+{\n+\tint ret = 0;\n+\tstruct stash_info info;\n+\tif (patch_mode && keep_index == -1)\n+\t\tkeep_index = 1;\n+\n+\tif (patch_mode && include_untracked) {\n+\t\tfprintf_ln(stderr, _(\"Can't use --patch and --include-untracked or --all at the same time\"));\n+\t\treturn -1;\n+\t}\n+\n+\tread_cache_preload(NULL);\n+\tif (!include_untracked && ps.nr) {\n+\t\tint i;\n+\t\tchar *ps_matched = xcalloc(ps.nr, 1);\n+\n+\t\tfor (i = 0; i < active_nr; ++i) {\n+\t\t\tconst struct cache_entry *ce = active_cache[i];\n+\t\t\tce_path_match(&the_index, ce, &ps, ps_matched);\n+\t\t}\n+\n+\t\tif (report_path_error(ps_matched, &ps, NULL)) {\n+\t\t\tfprintf_ln(stderr, _(\"Did you forget to 'git add'?\"));\n+\t\t\treturn -1;\n+\t\t}\n+\t\tfree(ps_matched);\n+\t}\n+\n+\tif (refresh_cache(REFRESH_QUIET))\n+\t\treturn -1;\n+\n+\tif (!check_changes(ps, include_untracked)) {\n+\t\tprintf_ln(_(\"No local changes to save\"));\n+\t\treturn 0;\n+\t}\n+\n+\tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n+\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n+\t\treturn -1;\n+\t}\n+\n+\tif (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,\n+\t\t\t    &info)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {\n+\t\tfprintf(stderr, _(\"Cannot save the current status\"));\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tprintf_ln(_(\"Saved working directory and index state %s\"), stash_msg);\n+\n+\tif (!patch_mode) {\n+\t\tif (include_untracked && !ps.nr) {\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"clean\", \"--force\",\n+\t\t\t\t\t \"--quiet\", \"-d\", NULL);\n+\t\t\tif (include_untracked == 2)\n+\t\t\t\targv_array_push(&cp.args, \"-x\");\n+\t\t\tif (run_command(&cp)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\t\tif (ps.nr) {\n+\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n+\t\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\t\tcp1.git_cmd = 1;\n+\t\t\targv_array_push(&cp1.args, \"add\");\n+\t\t\tif (!include_untracked)\n+\t\t\t\targv_array_push(&cp1.args, \"-u\");\n+\t\t\tif (include_untracked == 2)\n+\t\t\t\targv_array_push(&cp1.args, \"--force\");\n+\t\t\targv_array_push(&cp1.args, \"--\");\n+\t\t\tadd_ps_items_to_argv_array(&cp1.args, ps);\n+\t\t\tif (run_command(&cp1)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp2.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp2.args, \"diff-index\", \"-p\",\n+\t\t\t\t\t \"--cached\", \"--binary\", \"HEAD\", \"--\",\n+\t\t\t\t\t NULL);\n+\t\t\tadd_ps_items_to_argv_array(&cp2.args, ps);\n+\t\t\tif (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp3.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp3.args, \"apply\", \"--index\", \"-R\",\n+\t\t\t\t\t NULL);\n+\t\t\tif (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,\n+\t\t\t\t\t 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t} else {\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"reset\", \"--hard\", \"-q\",\n+\t\t\t\t\t NULL);\n+\t\t\tif (run_command(&cp)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (keep_index == 1 && !is_null_oid(&info.i_tree)) {\n+\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n+\t\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\t\tif (reset_tree(&info.i_tree, 0, 1)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp1.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp1.args, \"ls-files\", \"-z\",\n+\t\t\t\t\t \"--modified\", \"--\", NULL);\n+\t\t\tadd_ps_items_to_argv_array(&cp1.args, ps);\n+\t\t\tif (pipe_command(&cp1, NULL, 0, &out, 0, NULL, 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp2.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp2.args, \"checkout-index\", \"-z\",\n+\t\t\t\t\t \"--force\", \"--stdin\", NULL);\n+\t\t\tif (pipe_command(&cp2, out.buf, out.len, NULL, 0, NULL,\n+\t\t\t\t\t 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\t} else {\n+\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\tcp.git_cmd = 1;\n+\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n+\n+\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\n+\t\tif (keep_index < 1) {\n+\t\t\tint i;\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"reset\", \"-q\", \"--\", NULL);\n+\t\t\tfor (i = 0; i < ps.nr; ++i)\n+\t\t\t\targv_array_push(&cp.args, ps.items[i].match);\n+\t\t\tif (run_command(&cp)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\t}\n+done:\n+\tfree((char *) stash_msg);\n+\treturn ret;\n+}\n+\n+static int push_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct pathspec ps;\n+\tstruct option options[] = {\n+\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n+\t\t\tN_(\"keep index\"), 1),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\tN_(\"stash in patch mode\")),\n+\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_push_usage,\n+\t\t\t     0);\n+\n+\tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n+\treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n+\t\t\t     include_untracked);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1242,6 +1469,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!store_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"create\"))\n \t\treturn !!create_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"push\"))\n+\t\treturn !!push_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex ab06e4ffb8..c3146f62ab 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -412,7 +412,8 @@ save)\n \t;;\n push)\n \tshift\n-\tpush_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper push \"$@\"\n \t;;\n apply)\n \tshift\n@@ -448,7 +449,8 @@ branch)\n *)\n \tcase $# in\n \t0)\n-\t\tpush_stash &&\n+\t\tcd \"$START_DIR\"\n+\t\tgit stash--helper push &&\n \t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n \t\t;;\n \t*)\n-- \n2.19.0.rc0.22.gc26283d74e\n\n"},{"id":"357053","messageId":"xmqqpnxz33v4.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"65603caf56cfaeb1dff92546b7efe38dccb2d071.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-08-30T22:07:43Z","receivedAt":"2018-08-30T22:07:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n\n> This commit introduces tests for `git stash show`\n> config. It tests all the cases where `stash.showStat`\n> and `stash.showPatch` are unset or set to true / false.\n>\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  t/t3907-stash-show-config.sh | 81 ++++++++++++++++++++++++++++++++++++\n>  1 file changed, 81 insertions(+)\n>  create mode 100755 t/t3907-stash-show-config.sh\n>\n> diff --git a/t/t3907-stash-show-config.sh b/t/t3907-stash-show-config.sh\n> new file mode 100755\n> index 0000000000..8fe369c1a1\n> --- /dev/null\n> +++ b/t/t3907-stash-show-config.sh\n> @@ -0,0 +1,81 @@\n> +#!/bin/sh\n> +\n> +test_description='Test git stash show configuration.'\n> +\n> +. ./test-lib.sh\n> +\n> +test_expect_success 'setup' '\n> +\ttest_commit file\n> +'\n> +\n> +# takes three parameters:\n> +# 1. the stash.showStat value (or \"<unset>\")\n> +# 2. the stash.showPatch value (or \"<unset>\")\n> +# 3. the diff options of the expected output (or nothing for no output)\n> +test_stat_and_patch () {\n> +\tif test \"<unset>\" = \"$1\"\n> +\tthen\n> +\t\ttest_might_fail git config --unset stash.showStat\n> +\telse\n> +\t\ttest_config stash.showStat \"$1\"\n> +\tfi &&\n> +\n> +\tif test \"<unset>\" = \"$2\"\n> +\tthen\n> +\t\ttest_might_fail git config --unset stash.showPatch\n\nI think you are trying to protect yourself from an error triggered\nby unsetting what is not set, but for that, test_unconfig is\nprobably a better choice, as it still catches errors of other types\nand ignores only that \"unset a variable that is not set\" error.\n\n> +\telse\n> +\t\ttest_config stash.showPatch \"$2\"\n> +\tfi &&\n> +\n> +\tshift &&\n> +\tshift &&\n\nYou can use \"shift 2 &&\" here (not worth a reroll).\n\n> +\techo 2 >file.t &&\n\n> +\tgit diff \"$@\" >expect &&\n\nWhen the caller does not give $3 to this function, it does not look\nat 'expect'.  I think it is clearer if you did\n\n\tif test $# != 0\n\tthen\n\t\tgit diff \"$@\" >expect\n\tfi &&\n\nhere, and ...\n\n> +\tgit stash &&\n> +\tgit stash show >actual &&\n> +\n> +\tif test -z \"$1\"\n\n... wrote this as\n\n\tif test $# = 0\n\nThe only difference between '-z \"$1\"' and '$# = 0' is when he caller\npasses an empty string to the function as $3, which you never do, so\nthe distinction is theoretical, but using $# makes your intention\nclear that you do not mean to treat an empty string any specially.\n\n> +\tthen\n> +\t\ttest_must_be_empty actual\n> +\telse\n> +\t\ttest_cmp expect actual\n> +\tfi\n> +}\n> +\n> +test_expect_success 'showStat unset showPatch unset' '\n> +\ttest_stat_and_patch \"<unset>\" \"<unset>\" --stat\n> +'\n> +\n> +test_expect_success 'showStat unset showPatch false' '\n> +\ttest_stat_and_patch \"<unset>\" false --stat\n> +'\n> +\n> +test_expect_success 'showStat unset showPatch true' '\n> +\ttest_stat_and_patch \"<unset>\" true --stat -p\n> +'\n> +\n> +test_expect_success 'showStat false showPatch unset' '\n> +\ttest_stat_and_patch false \"<unset>\"\n> +'\n> +\n> +test_expect_success 'showStat false showPatch false' '\n> +\ttest_stat_and_patch false false\n> +'\n> +\n> +test_expect_success 'showStat false showPatch true' '\n> +\ttest_stat_and_patch false true -p\n> +'\n> +\n> +test_expect_success 'showStat true showPatch unset' '\n> +\ttest_stat_and_patch true \"<unset>\" --stat\n> +'\n> +\n> +test_expect_success 'showStat true showPatch false' '\n> +\ttest_stat_and_patch true false --stat\n> +'\n> +\n> +test_expect_success 'showStat true showPatch true' '\n> +\ttest_stat_and_patch true true --stat -p\n> +'\n> +\n> +test_done\n"},{"id":"357054","messageId":"xmqqlg8n33pj.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"2e7eef95b626721bac48d072aa0869452fe4297a.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 03/20] stash: update test cases conform to coding guidelines","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-08-30T22:11:04Z","receivedAt":"2018-08-30T22:11:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n\n> Removed whitespaces after redirection operators.\n\nA clean-up that is long overdue.  Thanks.\n\nThe result still has some rooms to improve (e.g. the preparation of\n'expect' file probably should be done in a test_expect_success block\nof the setup step; some are called 'expect' while others are called\n'expected'; some test_cmp invocations do not have expected output as\ntheir first argument), but that can be done later.\n\n"},{"id":"357055","messageId":"87ftyvcxa1.fsf@evledraar.gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 00/20] Convert \"git stash\" to C builtin","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2018-08-30T22:19:50Z","receivedAt":"2018-08-30T22:19:55Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Aug 30 2018, Paul-Sebastian Ungureanu wrote:\n\n> Hello,\n>\n> This a new iteration of `stash.c`. What is new?\n>\n>  * Some commits got squashed. The commit related to replacing\n>  `git apply` child process was dropped since it wasn't the best\n>  idea.\n>\n>  * In v7, there was a bug [1] related to config `git stash show`\n>  The bug was fixed and a test file was added for this.\n>\n>  * Fixed `git stash [push]` [2]. In v7, `git stash -q drop` would\n>  act like `git stash push -q drop`.\n>\n>  * Fixed coding-style nits. Verified that messages are marked\n>  for translation and are going to the correct output stream.\n>\n>  * Fixed one memory leak (related to `strbuf_detach`).\n>\n>  * Simplified the code a little bit.\n\nThis looks good, I read this and a previous version. I'e tested this on\nLinux, FreeBSD & AIX. All tests pass on all of them.\n\nOne style nit: These patches consistently indent wrapped lines not to\nalign with the opening \"(\" (as is our usual style), but just with\n\"\\n\\t\". I.e. this patch on top would make it like what we usually do\n(but should be squashed as appropriate):\n\n    diff --git a/builtin/stash.c b/builtin/stash.c\n    index dd1084afd4..6d849ed811 100644\n    --- a/builtin/stash.c\n    +++ b/builtin/stash.c\n    @@ -389,3 +389,3 @@ static int restore_untracked(struct object_id *u_tree)\n     static int do_apply_stash(const char *prefix, struct stash_info *info,\n    -\tint index)\n    +\t\t\t  int index)\n     {\n    @@ -408,3 +408,3 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,\n     \t\tif (!oidcmp(&info->b_tree, &info->i_tree) || !oidcmp(&c_tree,\n    -\t\t\t&info->i_tree)) {\n    +\t\t\t\t\t\t\t\t     &info->i_tree)) {\n     \t\t\thas_index = 0;\n    @@ -514,3 +514,3 @@ static int apply_stash(int argc, const char **argv, const char *prefix)\n     \t\tOPT_BOOL(0, \"index\", &index,\n    -\t\t\tN_(\"attempt to recreate the index\")),\n    +\t\t\t N_(\"attempt to recreate the index\")),\n     \t\tOPT_END()\n    @@ -610,3 +610,3 @@ static int pop_stash(int argc, const char **argv, const char *prefix)\n     \t\tOPT_BOOL(0, \"index\", &index,\n    -\t\t\tN_(\"attempt to recreate the index\")),\n    +\t\t\t N_(\"attempt to recreate the index\")),\n     \t\tOPT_END()\n    @@ -971,3 +971,3 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n     \targv_array_pushl(&cp_add_i.args, \"add--interactive\", \"--patch=stash\",\n    -\t\t\t\"--\", NULL);\n    +\t\t\t \"--\", NULL);\n     \tfor (i = 0; i < ps.nr; ++i)\n    @@ -1279,3 +1279,3 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n     \t\tprintf_ln(_(\"Saved working directory and index state %s\"),\n    -\t\t\tstash_msg);\n    +\t\t\t  stash_msg);\n\n    @@ -1413,5 +1413,5 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n     \t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n    -\t\t\tN_(\"keep index\"), 1),\n    +\t\t\t    N_(\"keep index\"), 1),\n     \t\tOPT_BOOL('p', \"patch\", &patch_mode,\n    -\t\t\tN_(\"stash in patch mode\")),\n    +\t\t\t N_(\"stash in patch mode\")),\n     \t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n    @@ -1422,3 +1422,3 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n     \t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n    -\t\t\t N_(\"stash message\")),\n    +\t\t\t   N_(\"stash message\")),\n     \t\tOPT_END()\n    @@ -1450,5 +1450,5 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n     \t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n    -\t\t\tN_(\"keep index\"), 1),\n    +\t\t\t    N_(\"keep index\"), 1),\n     \t\tOPT_BOOL('p', \"patch\", &patch_mode,\n    -\t\t\tN_(\"stash in patch mode\")),\n    +\t\t\t N_(\"stash in patch mode\")),\n     \t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n\nTip: It's also better to get feedback by CC-ing people who've had\nfeedback on previous versions.\n"},{"id":"357107","messageId":"xmqqzhx21jzn.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 00/20] Convert \"git stash\" to C builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-08-31T18:14:36Z","receivedAt":"2018-08-31T18:14:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n\n> This a new iteration of `stash.c`. What is new?\n>\n>  * Some commits got squashed. The commit related to replacing\n>  `git apply` child process was dropped since it wasn't the best\n>  idea.\n>\n>  * In v7, there was a bug [1] related to config `git stash show`\n>  The bug was fixed and a test file was added for this.\n>\n>  * Fixed `git stash [push]` [2]. In v7, `git stash -q drop` would\n>  act like `git stash push -q drop`.\n>\n>  * Fixed coding-style nits. Verified that messages are marked\n>  for translation and are going to the correct output stream.\n>\n>  * Fixed one memory leak (related to `strbuf_detach`).\n>\n>  * Simplified the code a little bit.\n\nAlso worth noting.\n\n  * Rebased on a recent 'master', in which the calling convention\n    for functions like dir_path_match() has been updated.  It won't\n    compile when applied to anything older than dc0f6f9e (\"Merge\n    branch 'nd/no-the-index'\", 2018-08-20).\n\n"},{"id":"357197","messageId":"nycvar.QRO.7.76.6.1809031409050.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"xmqqpnxz33v4.fsf@gitster-ct.c.googlers.com","subject":"Re: [GSoC][PATCH v8 05/20] stash: add tests for `git stash show` config","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T12:11:47Z","receivedAt":"2018-09-03T12:11:55Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Junio,\n\nOn Thu, 30 Aug 2018, Junio C Hamano wrote:\n\n> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n> > +test_stat_and_patch () {\n> > +\tif test \"<unset>\" = \"$1\"\n> > +\tthen\n> > +\t\ttest_might_fail git config --unset stash.showStat\n> > +\telse\n> > +\t\ttest_config stash.showStat \"$1\"\n> > +\tfi &&\n> > +\n> > +\tif test \"<unset>\" = \"$2\"\n> > +\tthen\n> > +\t\ttest_might_fail git config --unset stash.showPatch\n> \n> I think you are trying to protect yourself from an error triggered\n> by unsetting what is not set, but for that, test_unconfig is\n> probably a better choice, as it still catches errors of other types\n> and ignores only that \"unset a variable that is not set\" error.\n\nMy mistake. I did not realize that tehere is a `test_unconfig` when I\nsuggested the current version.\n\n> > +\tshift &&\n> > +\tshift &&\n> \n> You can use \"shift 2 &&\" here (not worth a reroll).\n\nAgain, my mistake. I was not sure how portable that construct is. (Is it?)\n\n> \n> > +\techo 2 >file.t &&\n> \n> > +\tgit diff \"$@\" >expect &&\n> \n> When the caller does not give $3 to this function, it does not look\n> at 'expect'.  I think it is clearer if you did\n> \n> \tif test $# != 0\n> \tthen\n> \t\tgit diff \"$@\" >expect\n> \tfi &&\n> \n> here, and ...\n> \n> > +\tgit stash &&\n> > +\tgit stash show >actual &&\n> > +\n> > +\tif test -z \"$1\"\n> \n> ... wrote this as\n> \n> \tif test $# = 0\n> \n> The only difference between '-z \"$1\"' and '$# = 0' is when he caller\n> passes an empty string to the function as $3, which you never do, so\n> the distinction is theoretical, but using $# makes your intention\n> clear that you do not mean to treat an empty string any specially.\n\nGood advice in general (in this case, we know the callers).\n\nPaul, when it comes to shell scripting, listen to Junio more than myself\nbecause he is even more of a shell script wiz than I am.\n\nCiao,\nDscho\n"},{"id":"357200","messageId":"nycvar.QRO.7.76.6.1809031512470.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"5466d911db63333c21317714e15b5bff827008a6.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 13/20] stash: convert store to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T13:24:05Z","receivedAt":"2018-09-03T13:24:10Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 02b593e0cd..87568b0f34 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -723,6 +728,54 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n>  \treturn diff_result_code(&rev.diffopt, 0);\n>  }\n>  \n> +static int do_store_stash(const char *w_commit, const char *stash_msg,\n> +\t\t\t  int quiet)\n> +{\n> +\tint ret = 0;\n> +\tint need_to_free = 0;\n\nNot worth sending another iteration, but if you end up doing so, I found\nan alternative paradigm more useful: instead of having a Boolean to\nindicate whether you need to free, have a `char *to_free` that is\ninitialized to `NULL`, then unconditionally release that:\n\n\tchar *to_free = NULL;\n\n\tif (!stash_msg)\n\t\tstash_msg = to_free = xstrdup(\"Created via \\\"git stash store\\\".\");\n\n\t[...]\n\n\tfree(to_free);\n\treturn ret;\n\nThis works because `free(NULL)` is defined as a no-op.\n\n> +\tstruct object_id obj;\n> +\n> +\tif (!stash_msg) {\n> +\t\tneed_to_free = 1;\n> +\t\tstash_msg  = xstrdup(\"Created via \\\"git stash store\\\".\");\n> +\t}\n> +\n> +\tret = get_oid(w_commit, &obj);\n\nIs `get_oid()` non-quiet? If so, we might need to shut it up when the\n`quiet` variable is non-zero. If it is quiet, we should probably mention\nsomething here when `quiet` is zero and `ret` indicates an error with\n`get_oid()`.\n\n> +\tif (!ret) {\n> +\t\tret = update_ref(stash_msg, ref_stash, &obj, NULL,\n> +\t\t\t\t REF_FORCE_CREATE_REFLOG,\n> +\t\t\t\t quiet ? UPDATE_REFS_QUIET_ON_ERR :\n> +\t\t\t\t UPDATE_REFS_MSG_ON_ERR);\n> +\t}\n> +\tif (ret && !quiet)\n> +\t\tfprintf_ln(stderr, _(\"Cannot update %s with %s\"),\n> +\t\t\t   ref_stash, w_commit);\n> +\tif (need_to_free)\n> +\t\tfree((char *) stash_msg);\n\nOkay, so all we need `stash_msg` for is the `update_ref()` call? And the\nfall-back message is constant. So how about\n\n\tif (!stash_msg)\n\t\tstash_msg = \"Created via \\\"git stash store\\\".\";\n\n? No need to allocate/release memory at all...\n\n> +\treturn ret;\n> +}\n> +\n> +static int store_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tconst char *stash_msg = NULL;\n> +\tstruct option options[] = {\n> +\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\", N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_store_usage,\n> +\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n> +\n> +\tif (argc != 1) {\n> +\t\tfprintf_ln(stderr, _(\"\\\"git stash store\\\" requires one <commit> argument\"));\n> +\t\treturn -1;\n> +\t}\n> +\n> +\treturn do_store_stash(argv[0], stash_msg, quiet);\n> +}\n\nI guess `store_stash()` and `do_store_stash()` are separate functions to\ndiscern between the higher-level function that parses the command-line,\nand the lower-level function that is already libified?\n\nIf so:\n\n1. I like it. Your code is already so much better prepared to serve as a proper\n   internal API than even some Git old-timers'.\n\n2. It might make sense to move the `get_oid()` call to `store_stash()`, as\n   it is also parsing: it is parsing the plain-text representation of the\n   revision into the internal representation (object ID).\n\nNeither of these suggestions are blockers, though.\n\nThanks,\nDscho\n\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -757,6 +810,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!list_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"show\"))\n>  \t\treturn !!show_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"store\"))\n> +\t\treturn !!store_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 0d05cbc1e5..5739c51527 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -191,45 +191,6 @@ create_stash () {\n>  \tdie \"$(gettext \"Cannot record working tree state\")\"\n>  }\n>  \n> -store_stash () {\n> -\twhile test $# != 0\n> -\tdo\n> -\t\tcase \"$1\" in\n> -\t\t-m|--message)\n> -\t\t\tshift\n> -\t\t\tstash_msg=\"$1\"\n> -\t\t\t;;\n> -\t\t-m*)\n> -\t\t\tstash_msg=${1#-m}\n> -\t\t\t;;\n> -\t\t--message=*)\n> -\t\t\tstash_msg=${1#--message=}\n> -\t\t\t;;\n> -\t\t-q|--quiet)\n> -\t\t\tquiet=t\n> -\t\t\t;;\n> -\t\t*)\n> -\t\t\tbreak\n> -\t\t\t;;\n> -\t\tesac\n> -\t\tshift\n> -\tdone\n> -\ttest $# = 1 ||\n> -\tdie \"$(eval_gettext \"\\\"$dashless store\\\" requires one <commit> argument\")\"\n> -\n> -\tw_commit=\"$1\"\n> -\tif test -z \"$stash_msg\"\n> -\tthen\n> -\t\tstash_msg=\"Created via \\\"git stash store\\\".\"\n> -\tfi\n> -\n> -\tgit update-ref --create-reflog -m \"$stash_msg\" $ref_stash $w_commit\n> -\tret=$?\n> -\ttest $ret != 0 && test -z \"$quiet\" &&\n> -\tdie \"$(eval_gettext \"Cannot update \\$ref_stash with \\$w_commit\")\"\n> -\treturn $ret\n> -}\n> -\n>  push_stash () {\n>  \tkeep_index=\n>  \tpatch_mode=\n> @@ -308,7 +269,7 @@ push_stash () {\n>  \t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n>  \n>  \tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n> -\tstore_stash -m \"$stash_msg\" -q $w_commit ||\n> +\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n>  \tdie \"$(gettext \"Cannot save the current status\")\"\n>  \tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n>  \n> @@ -468,7 +429,7 @@ create)\n>  \t;;\n>  store)\n>  \tshift\n> -\tstore_stash \"$@\"\n> +\tgit stash--helper store \"$@\"\n>  \t;;\n>  drop)\n>  \tshift\n> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357201","messageId":"nycvar.QRO.7.76.6.1809031527000.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"ed6aea4eb1e60dfe5c77ce8c9bc9d8409b658aed.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 08/20] stash: convert branch to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T13:29:20Z","receivedAt":"2018-09-03T13:29:24Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index cbe23fef11..dadc028649 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -535,6 +541,42 @@ 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 child_process cp = CHILD_PROCESS_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\t     git_stash_helper_branch_usage, 0);\n> +\n> +\tif (!argc)\n> +\t\treturn error(_(\"No branch name specified\"));\n\nVery minor: when calling `error()`, the message is prefixed with a\nlower-case \"error:\" and our convention is to continue in lower-case.\n\nCiao,\nDscho\n\n> +\n> +\tbranch = argv[0];\n> +\n> +\tif (get_stash_info(&info, argc - 1, argv + 1))\n> +\t\treturn -1;\n> +\n> +\tcp.git_cmd = 1;\n> +\targv_array_pushl(&cp.args, \"checkout\", \"-b\", NULL);\n> +\targv_array_push(&cp.args, branch);\n> +\targv_array_push(&cp.args, oid_to_hex(&info.b_commit));\n> +\tret = run_command(&cp);\n> +\tif (!ret)\n> +\t\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>  \tpid_t pid = getpid();\n> @@ -561,6 +603,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!clear_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"drop\"))\n>  \t\treturn !!drop_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"branch\"))\n> +\t\treturn !!branch_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index a99d5dc9e5..29d9f44255 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> @@ -673,7 +659,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> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357202","messageId":"nycvar.QRO.7.76.6.1809031613390.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"18debd5c9b1dc11a763caac39e10d4e8d5894bcb.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 15/20] stash: convert push to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T14:40:41Z","receivedAt":"2018-09-03T14:40:49Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index ce360a569d..23670321d8 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -1204,6 +1214,223 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n>  \treturn ret < 0;\n>  }\n>  \n> +static void add_ps_items_to_argv_array(struct argv_array *args,\n> +\t\t\t\t       struct pathspec ps) {\n\nAs this is a purely file-local function (and will probably stay that way),\n`add_pathspecs()` might be sweeter to the eyes.\n\n> +\tint i;\n> +\tfor (i = 0; i < ps.nr; ++i)\n\nI vaguely recall that Junio preferred an empty line between the\ndeclarations and the first statement. Let's give him his wish?\n\nAlso, he recently remarked in a different thread that he prefers `i++`\nwhen the value is not used immediately.\n\n> +\t\targv_array_push(args, ps.items[i].match);\n> +}\n> +\n> +static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n> +\t\t\t int keep_index, int patch_mode, int include_untracked)\n> +{\n> +\tint ret = 0;\n> +\tstruct stash_info info;\n> +\tif (patch_mode && keep_index == -1)\n> +\t\tkeep_index = 1;\n> +\n> +\tif (patch_mode && include_untracked) {\n> +\t\tfprintf_ln(stderr, _(\"Can't use --patch and --include-untracked or --all at the same time\"));\n\nWe usually wrap these long lines by using the C feature where you can\n\"interrupt\" a string literal by putting end quotes on the first line, and\nstart with a quote in the next line, and the compiler will handle that as\nif it was one string. In this instance:\n\n\t\tfprintf_ln(stderr, _(\"Can't use --patch and --include-untracked\"\n\t\t\t\t     \" or --all at the same time\"));\n\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tread_cache_preload(NULL);\n> +\tif (!include_untracked && ps.nr) {\n> +\t\tint i;\n> +\t\tchar *ps_matched = xcalloc(ps.nr, 1);\n> +\n> +\t\tfor (i = 0; i < active_nr; ++i) {\n> +\t\t\tconst struct cache_entry *ce = active_cache[i];\n> +\t\t\tce_path_match(&the_index, ce, &ps, ps_matched);\n> +\t\t}\n> +\n> +\t\tif (report_path_error(ps_matched, &ps, NULL)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Did you forget to 'git add'?\"));\n> +\t\t\treturn -1;\n> +\t\t}\n> +\t\tfree(ps_matched);\n> +\t}\n> +\n> +\tif (refresh_cache(REFRESH_QUIET))\n> +\t\treturn -1;\n> +\n> +\tif (!check_changes(ps, include_untracked)) {\n> +\t\tprintf_ln(_(\"No local changes to save\"));\n> +\t\treturn 0;\n> +\t}\n> +\n> +\tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n> +\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n> +\t\treturn -1;\n\nWhile technically not necessary, I would prefer to write `goto done` here,\ntoo. It is too easy to forget when introducing/changing code paths.\n\n> +\t}\n> +\n> +\tif (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,\n> +\t\t\t    &info)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (do_store_stash(oid_to_hex(&info.w_commit), stash_msg, 1)) {\n> +\t\tfprintf(stderr, _(\"Cannot save the current status\"));\n\nWe probably want to use fprintf_ln() here.\n\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tprintf_ln(_(\"Saved working directory and index state %s\"), stash_msg);\n> +\n> +\tif (!patch_mode) {\n\nIt might be easier to read if the `if (patch_mode)` block was handled\nfirst, which would call `goto done` also in case of success, not only in\ncase of failure.\n\n> +\t\tif (include_untracked && !ps.nr) {\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"clean\", \"--force\",\n> +\t\t\t\t\t \"--quiet\", \"-d\", NULL);\n> +\t\t\tif (include_untracked == 2)\n> +\t\t\t\targv_array_push(&cp.args, \"-x\");\n> +\t\t\tif (run_command(&cp)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\t\tif (ps.nr) {\n> +\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp3 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t\t\tcp1.git_cmd = 1;\n> +\t\t\targv_array_push(&cp1.args, \"add\");\n> +\t\t\tif (!include_untracked)\n> +\t\t\t\targv_array_push(&cp1.args, \"-u\");\n> +\t\t\tif (include_untracked == 2)\n\nI think it would make sense to turn this into an `enum`, or at least to\n`#define` the magic constant `2` into something like `INCLUDE_ALL`.\n\n> +\t\t\t\targv_array_push(&cp1.args, \"--force\");\n> +\t\t\targv_array_push(&cp1.args, \"--\");\n> +\t\t\tadd_ps_items_to_argv_array(&cp1.args, ps);\n> +\t\t\tif (run_command(&cp1)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp2.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp2.args, \"diff-index\", \"-p\",\n> +\t\t\t\t\t \"--cached\", \"--binary\", \"HEAD\", \"--\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tadd_ps_items_to_argv_array(&cp2.args, ps);\n> +\t\t\tif (pipe_command(&cp2, NULL, 0, &out, 0, NULL, 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp3.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp3.args, \"apply\", \"--index\", \"-R\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tif (pipe_command(&cp3, out.buf, out.len, NULL, 0, NULL,\n> +\t\t\t\t\t 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t} else {\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"reset\", \"--hard\", \"-q\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tif (run_command(&cp)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\n> +\t\tif (keep_index == 1 && !is_null_oid(&info.i_tree)) {\n> +\t\t\tstruct child_process cp1 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp2 = CHILD_PROCESS_INIT;\n> +\t\t\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t\t\tif (reset_tree(&info.i_tree, 0, 1)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp1.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp1.args, \"ls-files\", \"-z\",\n> +\t\t\t\t\t \"--modified\", \"--\", NULL);\n> +\t\t\tadd_ps_items_to_argv_array(&cp1.args, ps);\n> +\t\t\tif (pipe_command(&cp1, NULL, 0, &out, 0, NULL, 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp2.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp2.args, \"checkout-index\", \"-z\",\n> +\t\t\t\t\t \"--force\", \"--stdin\", NULL);\n> +\t\t\tif (pipe_command(&cp2, out.buf, out.len, NULL, 0, NULL,\n> +\t\t\t\t\t 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\t} else {\n> +\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\tcp.git_cmd = 1;\n> +\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n> +\n> +\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n> +\t\t\tret = -1;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\n> +\t\tif (keep_index < 1) {\n> +\t\t\tint i;\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"reset\", \"-q\", \"--\", NULL);\n> +\t\t\tfor (i = 0; i < ps.nr; ++i)\n> +\t\t\t\targv_array_push(&cp.args, ps.items[i].match);\n> +\t\t\tif (run_command(&cp)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\t}\n> +done:\n> +\tfree((char *) stash_msg);\n> +\treturn ret;\n> +}\n> +\n> +static int push_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint keep_index = -1;\n> +\tint patch_mode = 0;\n> +\tint include_untracked = 0;\n> +\tint quiet = 0;\n> +\tconst char *stash_msg = NULL;\n> +\tstruct pathspec ps;\n> +\tstruct option options[] = {\n> +\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n> +\t\t\tN_(\"keep index\"), 1),\n\nShouldn't this be an `OPT_BOOL`? I do not know whether `OPT_SET_INT`\nhandles the `--no-keep-index` as intended, but I do know that of\n`OPT_BOOL`...\n\nThanks,\nDscho\n\n> +\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n> +\t\t\tN_(\"stash in patch mode\")),\n> +\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n> +\t\t\t    N_(\"include ignore files\"), 2),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> +\t\t\t N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_push_usage,\n> +\t\t\t     0);\n> +\n> +\tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n> +\treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n> +\t\t\t     include_untracked);\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -1242,6 +1469,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!store_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"create\"))\n>  \t\treturn !!create_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"push\"))\n> +\t\treturn !!push_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index ab06e4ffb8..c3146f62ab 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -412,7 +412,8 @@ save)\n>  \t;;\n>  push)\n>  \tshift\n> -\tpush_stash \"$@\"\n> +\tcd \"$START_DIR\"\n> +\tgit stash--helper push \"$@\"\n>  \t;;\n>  apply)\n>  \tshift\n> @@ -448,7 +449,8 @@ branch)\n>  *)\n>  \tcase $# in\n>  \t0)\n> -\t\tpush_stash &&\n> +\t\tcd \"$START_DIR\"\n> +\t\tgit stash--helper push &&\n>  \t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n>  \t\t;;\n>  \t*)\n> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357214","messageId":"nycvar.QRO.7.76.6.1809031640490.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"82f5af08c6475765fac27e30f716a48039d56404.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 18/20] stash: convert `stash--helper.c` into `stash.c`","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T14:57:05Z","receivedAt":"2018-09-03T14:57:11Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash.c\n> similarity index 90%\n> rename from builtin/stash--helper.c\n> rename to builtin/stash.c\n> index 1269f2548c..3d2316e3f7 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash.c\n> @@ -1198,34 +1198,39 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n>  \n>  static int create_stash(int argc, const char **argv, const char *prefix)\n>  {\n> -\tint include_untracked = 0;\n> +\tint i;\n>  \tint ret = 0;\n> +\tchar *to_free = NULL;\n>  \tconst char *stash_msg = NULL;\n>  \tstruct stash_info info;\n>  \tstruct pathspec ps;\n> +\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n>  \tstruct option options[] = {\n> -\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> -\t\t\t N_(\"include untracked files in stash\")),\n> -\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> -\t\t\t N_(\"stash message\")),\n\nThis was indeed a bit surprising, but after re-reading the commit message,\nit became clear what this is all about.\n\nBut I wonder...\n\n>  \t\tOPT_END()\n>  \t};\n>  \n>  \targc = parse_options(argc, argv, prefix, options,\n> -\t\t\t     git_stash_helper_create_usage,\n> +\t\t\t     git_stash_create_usage,\n>  \t\t\t     0);\n\nAs we no longer accept any command-line arguments, (and as the `create`\ncommand *explicitly* allows messages starting with a dash), it would\nprobably make more sense to test `argc` directly:\n\n\tif (!argc)\n\t\tusage_with_options(git_stash_create_usage, options);\n\n>  \n> +\tfor (i = 0; i < argc; ++i)\n> +\t\tstrbuf_addf(&stash_msg_buf, \"%s \", argv[i]);\n\n... this adds an extra space at the end, right? I guess this should read\ninstead:\n\n\tfor (i = 0; i < argc; i++) {\n\t\tif (i > 0)\n\t\t\tstrbuf_addch(&stash_msg_buf, ' ');\n\t\tstrbuf_addstr(&stash_msg_buf, argv[i]);\n\t}\n\n> +\tstash_msg = strbuf_detach(&stash_msg_buf, NULL);\n\nAs we no longer have any way to set `stash_msg` via an option, this\nvariable should go away.\n\nAnd rather than detaching the strbuf here, it would be better to release\nit after the call to `do_create_stash()`. (And the `to_free` variable is\nno longer necessary, either.)\n\n> +\tto_free = (char *) stash_msg;\n> +\n>  \tmemset(&ps, 0, sizeof(ps));\n> -\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info, 0);\n> +\tret = do_create_stash(ps, &stash_msg, 0, 0, &info, 0);\n>  \n>  \tif (!ret)\n>  \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n>  \n> +\tfree(to_free);\n> +\tfree((char *) stash_msg);\n> +\n>  \t/*\n>  \t * ret can be 1 if there were no changes. In this case, we should\n>  \t * not error out.\n>  \t */\n> -\tfree((char *) stash_msg);\n>  \treturn ret < 0;\n>  }\n>  \n> @@ -1443,9 +1448,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n>  \t\tOPT_END()\n>  \t};\n>  \n> -\targc = parse_options(argc, argv, prefix, options,\n> -\t\t\t     git_stash_helper_push_usage,\n> -\t\t\t     0);\n> +\tif (argc)\n> +\t\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t\t     git_stash_push_usage,\n> +\t\t\t\t     0);\n\nIs this `if (argc)` guard necessary?\n\n>  \n>  \tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n>  \treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n> @@ -1478,7 +1484,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n>  \t};\n>  \n>  \targc = parse_options(argc, argv, prefix, options,\n> -\t\t\t     git_stash_helper_save_usage,\n> +\t\t\t     git_stash_save_usage,\n>  \t\t\t     0);\n>  \n>  \tfor (i = 0; i < argc; ++i)\n> @@ -1494,10 +1500,12 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n>  \treturn ret;\n>  }\n>  \n> -int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n> +int cmd_stash(int argc, const char **argv, const char *prefix)\n>  {\n> +\tint i = -1;\n>  \tpid_t pid = getpid();\n>  \tconst char *index_file;\n> +\tstruct argv_array args = ARGV_ARRAY_INIT;\n>  \n>  \tstruct option options[] = {\n>  \t\tOPT_END()\n> @@ -1505,16 +1513,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \n>  \tgit_config(git_default_config, NULL);\n>  \n> -\targc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,\n> +\targc = parse_options(argc, argv, prefix, options, git_stash_usage,\n>  \t\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n>  \n>  \tindex_file = get_index_file();\n>  \tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n>  \t\t    (uintmax_t)pid);\n>  \n> -\tif (argc < 1)\n> -\t\tusage_with_options(git_stash_helper_usage, options);\n> -\tif (!strcmp(argv[0], \"apply\"))\n> +\tif (argc == 0)\n> +\t\treturn !!push_stash(0, NULL, prefix);\n> +\telse if (!strcmp(argv[0], \"apply\"))\n>  \t\treturn !!apply_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"clear\"))\n>  \t\treturn !!clear_stash(argc, argv, prefix);\n> @@ -1536,7 +1544,44 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!push_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"save\"))\n>  \t\treturn !!save_stash(argc, argv, prefix);\n> +\telse if (*argv[0] != '-')\n> +\t\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n> +\t\t\t      git_stash_usage, options);\n> +\n> +\tif (strcmp(argv[0], \"-p\")) {\n> +\t\twhile (++i < argc && strcmp(argv[i], \"--\")) {\n> +\t\t\t/*\n> +\t\t\t * `akpqu` is a string which contains all short options,\n> +\t\t\t * except `-m` which is verified separately.\n> +\t\t\t */\n> +\t\t\tif ((strlen(argv[i]) == 2) && *argv[i] == '-' &&\n> +\t\t\t    strchr(\"akpqu\", argv[i][1]))\n\nI *think* this is missing the `n`.\n\nThanks,\nDscho\n\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\tif (!strcmp(argv[i], \"--all\") ||\n> +\t\t\t    !strcmp(argv[i], \"--keep-index\") ||\n> +\t\t\t    !strcmp(argv[i], \"--no-keep-index\") ||\n> +\t\t\t    !strcmp(argv[i], \"--patch\") ||\n> +\t\t\t    !strcmp(argv[i], \"--quiet\") ||\n> +\t\t\t    !strcmp(argv[i], \"--include-untracked\"))\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\t/*\n> +\t\t\t * `-m` and `--message=` are verified separately because\n> +\t\t\t * they need to be immediately followed by a string\n> +\t\t\t * (i.e.`-m\"foobar\"` or `--message=\"foobar\"`).\n> +\t\t\t */\n> +\t\t\tif ((strlen(argv[i]) > 2 &&\n> +\t\t\t     !strncmp(argv[i], \"-m\", 2)) ||\n> +\t\t\t    (strlen(argv[i]) > 10 &&\n> +\t\t\t     !strncmp(argv[i], \"--message=\", 10)))\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\tusage_with_options(git_stash_usage, options);\n> +\t\t}\n> +\t}\n>  \n> -\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n> -\t\t      git_stash_helper_usage, options);\n> +\targv_array_push(&args, \"push\");\n> +\targv_array_pushv(&args, argv);\n> +\treturn !!push_stash(args.argc, args.argv, prefix);\n>  }\n> diff --git a/git-stash.sh b/git-stash.sh\n> deleted file mode 100755\n> index 695f1feba3..0000000000\n> --- a/git-stash.sh\n> +++ /dev/null\n> @@ -1,153 +0,0 @@\n> -#!/bin/sh\n> -# Copyright (c) 2007, Nanako Shiraishi\n> -\n> -dashless=$(basename \"$0\" | sed -e 's/-/ /')\n> -USAGE=\"list [<options>]\n> -   or: $dashless show [<stash>]\n> -   or: $dashless drop [-q|--quiet] [<stash>]\n> -   or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n> -   or: $dashless branch <branchname> [<stash>]\n> -   or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n> -\t\t      [-u|--include-untracked] [-a|--all] [<message>]\n> -   or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n> -\t\t       [-u|--include-untracked] [-a|--all] [-m <message>]\n> -\t\t       [-- <pathspec>...]]\n> -   or: $dashless clear\"\n> -\n> -SUBDIRECTORY_OK=Yes\n> -OPTIONS_SPEC=\n> -START_DIR=$(pwd)\n> -. git-sh-setup\n> -require_work_tree\n> -prefix=$(git rev-parse --show-prefix) || exit 1\n> -cd_to_toplevel\n> -\n> -TMP=\"$GIT_DIR/.git-stash.$$\"\n> -TMPindex=${GIT_INDEX_FILE-\"$(git rev-parse --git-path index)\"}.stash.$$\n> -trap 'rm -f \"$TMP-\"* \"$TMPindex\"' 0\n> -\n> -ref_stash=refs/stash\n> -\n> -if git config --get-colorbool color.interactive; then\n> -       help_color=\"$(git config --get-color color.interactive.help 'red bold')\"\n> -       reset_color=\"$(git config --get-color '' reset)\"\n> -else\n> -       help_color=\n> -       reset_color=\n> -fi\n> -\n> -#\n> -# Parses the remaining options looking for flags and\n> -# at most one revision defaulting to ${ref_stash}@{0}\n> -# if none found.\n> -#\n> -# Derives related tree and commit objects from the\n> -# revision, if one is found.\n> -#\n> -# stash records the work tree, and is a merge between the\n> -# base commit (first parent) and the index tree (second parent).\n> -#\n> -#   REV is set to the symbolic version of the specified stash-like commit\n> -#   IS_STASH_LIKE is non-blank if ${REV} looks like a stash\n> -#   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref\n> -#   s is set to the SHA1 of the stash commit\n> -#   w_commit is set to the commit containing the working tree\n> -#   b_commit is set to the base commit\n> -#   i_commit is set to the commit containing the index tree\n> -#   u_commit is set to the commit containing the untracked files tree\n> -#   w_tree is set to the working tree\n> -#   b_tree is set to the base tree\n> -#   i_tree is set to the index tree\n> -#   u_tree is set to the untracked files tree\n> -#\n> -#   GIT_QUIET is set to t if -q is specified\n> -#   INDEX_OPTION is set to --index if --index is specified.\n> -#   FLAGS is set to the remaining flags (if allowed)\n> -#\n> -# dies if:\n> -#   * too many revisions specified\n> -#   * no revision is specified and there is no stash stack\n> -#   * a revision is specified which cannot be resolve to a SHA1\n> -#   * a non-existent stash reference is specified\n> -#   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n> -#\n> -\n> -test \"$1\" = \"-p\" && set \"push\" \"$@\"\n> -\n> -PARSE_CACHE='--not-parsed'\n> -# The default command is \"push\" if nothing but options are given\n> -seen_non_option=\n> -for opt\n> -do\n> -\tcase \"$opt\" in\n> -\t--) break ;;\n> -\t-*) ;;\n> -\t*) seen_non_option=t; break ;;\n> -\tesac\n> -done\n> -\n> -test -n \"$seen_non_option\" || set \"push\" \"$@\"\n> -\n> -# Main command set\n> -case \"$1\" in\n> -list)\n> -\tshift\n> -\tgit stash--helper list \"$@\"\n> -\t;;\n> -show)\n> -\tshift\n> -\tgit stash--helper show \"$@\"\n> -\t;;\n> -save)\n> -\tshift\n> -\tcd \"$START_DIR\"\n> -\tgit stash--helper save \"$@\"\n> -\t;;\n> -push)\n> -\tshift\n> -\tcd \"$START_DIR\"\n> -\tgit stash--helper push \"$@\"\n> -\t;;\n> -apply)\n> -\tshift\n> -\tcd \"$START_DIR\"\n> -\tgit stash--helper apply \"$@\"\n> -\t;;\n> -clear)\n> -\tshift\n> -\tgit stash--helper clear \"$@\"\n> -\t;;\n> -create)\n> -\tshift\n> -\tgit stash--helper create --message \"$*\"\n> -\t;;\n> -store)\n> -\tshift\n> -\tgit stash--helper store \"$@\"\n> -\t;;\n> -drop)\n> -\tshift\n> -\tgit stash--helper drop \"$@\"\n> -\t;;\n> -pop)\n> -\tshift\n> -\tcd \"$START_DIR\"\n> -\tgit stash--helper pop \"$@\"\n> -\t;;\n> -branch)\n> -\tshift\n> -\tcd \"$START_DIR\"\n> -\tgit stash--helper branch \"$@\"\n> -\t;;\n> -*)\n> -\tcase $# in\n> -\t0)\n> -\t\tcd \"$START_DIR\"\n> -\t\tgit stash--helper push &&\n> -\t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n> -\t\t;;\n> -\t*)\n> -\t\tusage\n> -\tesac\n> -\t;;\n> -esac\n> diff --git a/git.c b/git.c\n> index 3c0e762d7d..78548397cf 100644\n> --- a/git.c\n> +++ b/git.c\n> @@ -544,7 +544,7 @@ static struct cmd_struct commands[] = {\n>  \t{ \"show-index\", cmd_show_index },\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{ \"stash\", cmd_stash, 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 | NO_PARSEOPT },\n> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357215","messageId":"nycvar.QRO.7.76.6.1809031718230.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"a4faed3c8aa5ea8f0d4c578b693f3b5de3e3a709.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 14/20] stash: convert create to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T16:00:32Z","receivedAt":"2018-09-03T16:00:39Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 87568b0f34..ce360a569d 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -290,6 +296,18 @@ static int reset_head(void)\n>  \treturn run_command(&cp);\n>  }\n>  \n> +static void add_diff_to_buf(struct diff_queue_struct *q,\n> +\t\t\t    struct diff_options *options,\n> +\t\t\t    void *data)\n> +{\n> +\tint i;\n> +\tfor (i = 0; i < q->nr; i++) {\n> +\t\tstruct diff_filepair *p = q->queue[i];\n> +\t\tstrbuf_addstr(data, p->one->path);\n\nMaybe `q->queue[i]->one->path` would be okay, too? Dunno.\n\n> +\t\tstrbuf_addch(data, 0);\n\nNeat trick. However, I had to study the code to see that the output of\nthis function will be fed to `update-index -z` to see why the NUL is\nappended here. Maybe add a code comment here?\n\n> +\t}\n> +}\n> +\n>  static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)\n>  {\n>  \tstruct child_process cp = CHILD_PROCESS_INIT;\n> @@ -776,6 +794,416 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n>  \treturn do_store_stash(argv[0], stash_msg, quiet);\n>  }\n>  \n> +/*\n> + * `out` will be filled with the names of untracked files. The return value is:\n> + *\n> + * = 0 if there are not any untracked files\n> + * > 0 if there are untracked files\n> + */\n> +static int get_untracked_files(struct pathspec ps, int include_untracked,\n> +\t\t\t       struct strbuf *out)\n> +{\n> +\tint max_len;\n> +\tint i;\n> +\tchar *seen;\n> +\tstruct dir_struct dir;\n> +\n> +\tmemset(&dir, 0, sizeof(dir));\n> +\tif (include_untracked != 2)\n\nIf you follow my suggestion to replace this magic `2` by a symbol that\ndescribes the meaning \"include all\", then this line would need to be\nchanged, too.\n\n> +\t\tsetup_standard_excludes(&dir);\n> +\n> +\tseen = xcalloc(ps.nr, 1);\n> +\n> +\tmax_len = fill_directory(&dir, the_repository->index, &ps);\n> +\tfor (i = 0; i < dir.nr; i++) {\n> +\t\tstruct dir_entry *ent = dir.entries[i];\n> +\t\tif (!dir_path_match(&the_index, ent, &ps, max_len, seen)) {\n> +\t\t\tfree(ent);\n> +\t\t\tcontinue;\n> +\t\t}\n> +\t\tstrbuf_addf(out, \"%s%c\", ent->name, '\\0');\n> +\t\tfree(ent);\n\nThis is a *very* minor nit pick... I would have written it this way\ninstead:\n\n\t\tif (dir_path_match(&the_index, ent, &ps, max_len, seen)) {\n\t\t\tstrbuf_addstr(out, ent->name);\n\t\t\t/* NUL-terminate: will be fed to update-index -z */\n\t\t\tstrbuf_addch(out, 0);\n\t\t}\n\t\tfree(ent);\n\nJust an idea...\n\n> +\t}\n> +\n> +\tfree(dir.entries);\n> +\tfree(dir.ignored);\n> +\tclear_directory(&dir);\n> +\tfree(seen);\n> +\treturn out->len;\n> +}\n\nIf you introduce a local variable `found` and increase it whenever a match\nwas found, then you could also handle `out == NULL` by skipping the\n`strbuf_*()` calls, which would come in handin in the `check_changes()`\nfunction that does not actually want to use the output.\n\n> +\n> +/*\n> + * The return value of `check_changes()` can be:\n> + *\n> + * < 0 if there was an error\n> + * = 0 if there are no changes.\n> + * > 0 if there are changes.\n> + */\n> +static int check_changes(struct pathspec ps, int include_untracked)\n> +{\n> +\tint result;\n> +\tint ret = 0;\n> +\tstruct rev_info rev;\n> +\tstruct object_id dummy;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\tinit_revisions(&rev, NULL);\n> +\trev.prune_data = ps;\n> +\n> +\trev.diffopt.flags.quick = 1;\n> +\trev.diffopt.flags.ignore_submodules = 1;\n> +\trev.abbrev = 0;\n> +\n> +\t/* No initial commit. */\n> +\tif (get_oid(\"HEAD\", &dummy))\n> +\t\treturn -1;\n> +\n> +\tadd_head_to_pending(&rev);\n> +\tdiff_setup_done(&rev.diffopt);\n> +\n> +\tif (read_cache() < 0)\n> +\t\treturn 1;\n> +\tresult = run_diff_index(&rev, 1);\n> +\tif (diff_result_code(&rev.diffopt, result))\n> +\t\treturn 1;\n> +\n> +\tobject_array_clear(&rev.pending);\n> +\tresult = run_diff_files(&rev, 0);\n> +\tif (diff_result_code(&rev.diffopt, result))\n> +\t\treturn 1;\n\nThese lines look familiar... Maybe call has_unstaged_changes() and\nhas_uncommitted_changes() here? (I have to admit that I have no idea what\nthose functions do with unborn branches.)\n\n> +\n> +\tif (include_untracked && get_untracked_files(ps, include_untracked,\n> +\t\t\t\t\t\t     &out)) {\n> +\t\tstrbuf_release(&out);\n> +\t\treturn 1;\n> +\t}\n> +\n> +\tstrbuf_release(&out);\n> +\treturn 0;\n> +}\n> +\n> +static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n> +\t\t\t\tstruct strbuf *in)\n> +{\n> +\tint ret = 0;\n> +\tstruct strbuf untracked_msg = STRBUF_INIT;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> +\n> +\tcp_upd_index.git_cmd = 1;\n> +\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n> +\t\t\t \"--remove\", \"--stdin\", NULL);\n> +\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\n> +\tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n> +\tif (pipe_command(&cp_upd_index, in->buf, in->len, NULL, 0, NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tcp_write_tree.git_cmd = 1;\n> +\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> +\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\tget_oid_hex(out.buf, &info->u_tree);\n> +\n> +\tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n> +\t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +done:\n> +\tstrbuf_release(&untracked_msg);\n> +\tstrbuf_release(&out);\n> +\tremove_path(stash_index_path.buf);\n> +\treturn ret;\n> +}\n> +\n> +static struct strbuf patch = STRBUF_INIT;\n\nAs far as I can see, this variable should be local to `stash_patch` (and\nreleased at the end).\n\n[Coming back after reading the shell script code and realizing that it\nwrote the $TMP-patch file that is then used in `push_stash`]\n\nAh! So the output of `stash_patch()` is not just a return value indicating\nsuccess, but it also wants to return the patch. How about passing this in\nvia a parameter after `struct pathspec ps`, say, `struct strbuf\n*out_patch`?\n\n> +\n> +static int stash_patch(struct stash_info *info, struct pathspec ps)\n> +{\n> +\tint i;\n> +\tint ret = 0;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct child_process cp_read_tree = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_add_i = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n> +\n> +\tremove_path(stash_index_path.buf);\n> +\n> +\tcp_read_tree.git_cmd = 1;\n> +\targv_array_pushl(&cp_read_tree.args, \"read-tree\", \"HEAD\", NULL);\n> +\targv_array_pushf(&cp_read_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (run_command(&cp_read_tree)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n\nMaybe insert the comment \"find out what the user wants\" here?\n\n> +\tcp_add_i.git_cmd = 1;\n> +\targv_array_pushl(&cp_add_i.args, \"add--interactive\", \"--patch=stash\",\n> +\t\t\t\"--\", NULL);\n> +\tfor (i = 0; i < ps.nr; ++i)\n> +\t\targv_array_push(&cp_add_i.args, ps.items[i].match);\n\nI guess you could use `add_ps_items_to_argv_array(&cp_add_i.args, &ps)` here.\n\n> +\targv_array_pushf(&cp_add_i.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (run_command(&cp_add_i)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n\nMaybe insert the comment \"state of the working tree\" here?\n\n> +\tcp_write_tree.git_cmd = 1;\n> +\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> +\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tget_oid_hex(out.buf, &info->w_tree);\n> +\n> +\tcp_diff_tree.git_cmd = 1;\n> +\targv_array_pushl(&cp_diff_tree.args, \"diff-tree\", \"-p\", \"HEAD\",\n> +\t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n> +\tif (pipe_command(&cp_diff_tree, NULL, 0, &patch, 0, NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (!patch.len) {\n> +\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n> +\t\tret = 1;\n> +\t}\n> +\n> +done:\n> +\tstrbuf_release(&out);\n> +\tremove_path(stash_index_path.buf);\n> +\treturn ret;\n> +}\n> +\n> +static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n> +{\n> +\tint ret = 0;\n> +\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct strbuf diff_output = STRBUF_INIT;\n> +\tstruct rev_info rev;\n> +\n> +\tset_alternate_index_output(stash_index_path.buf);\n> +\tif (reset_tree(&info->i_tree, 0, 0)) {\n\nSo this is the `git read-tree --index-output=\"$TMPindex\" -m $i_tree` call.\n\nAt first, I thought that one of the zeros meant that the `merge` flag\nwould be turned off, but that is always turned on. Okay. (I guess\npositional parameters make this a bit less readable, not your fault, but\nC's.)\n\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\tset_alternate_index_output(NULL);\n\nSide note (i.e. rant): I wish this was more thread-safe. Granted, right\nnow it does not make sense to reset_tree() in parallel. But Stefan Beller\nis working on being able to reset_tree() submodules, in which case\nrunning reset_tree() in parallel will make a ton of sense.\n\n> +\n> +\tgit_config(git_diff_basic_config, NULL);\n\nIs this not called in as part of `git_config(git_default_config, NULL);`\nin cmd_stash() already?\n\n*clicketyclick*\n\nI guess not. But then, maybe it would make sense to run with\n`git_diff_basic_config` from the get go, to avoid having to run\n`git_config()` twice.\n\n> +\tinit_revisions(&rev, NULL);\n> +\trev.prune_data = ps;\n> +\trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n> +\trev.diffopt.format_callback = add_diff_to_buf;\n> +\trev.diffopt.format_callback_data = &diff_output;\n> +\n> +\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit), \"\");\n> +\tif (run_diff_index(&rev, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tcp_upd_index.git_cmd = 1;\n> +\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n> +\t\t\t \"--remove\", \"--stdin\", NULL);\n> +\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\n> +\tif (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,\n> +\t\t\t NULL, 0, NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tcp_write_tree.git_cmd = 1;\n> +\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> +\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tget_oid_hex(out.buf, &info->w_tree);\n> +\n> +done:\n> +\tUNLEAK(rev);\n> +\tstrbuf_release(&out);\n> +\tobject_array_clear(&rev.pending);\n> +\tstrbuf_release(&diff_output);\n> +\tremove_path(stash_index_path.buf);\n> +\treturn ret;\n> +}\n> +\n> +static int do_create_stash(struct pathspec ps, const char **stash_msg,\n\nAs the `stash_msg` will receive an allocated buffer that will need to be\nreleased by the caller, this should be a `char **stash_msg` (no `const`).\n\n> +\t\t\t   int include_untracked, int patch_mode,\n> +\t\t\t   struct stash_info *info)\n> +{\n> +\tint untracked_commit_option = 0;\n> +\tint ret = 0;\n> +\tint flags;\n> +\tconst char *head_short_sha1 = NULL;\n> +\tconst char *branch_ref = NULL;\n> +\tconst char *branch_name = \"(no branch)\";\n> +\tstruct commit *head_commit = NULL;\n> +\tstruct commit_list *parents = NULL;\n> +\tstruct strbuf msg = STRBUF_INIT;\n> +\tstruct strbuf commit_tree_label = STRBUF_INIT;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n> +\n> +\tread_cache_preload(NULL);\n> +\trefresh_cache(REFRESH_QUIET);\n> +\n> +\tif (!check_changes(ps, include_untracked)) {\n> +\t\tret = 1;\n> +\t\t*stash_msg = NULL;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (get_oid(\"HEAD\", &info->b_commit)) {\n> +\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n> +\t\tret = -1;\n> +\t\t*stash_msg = NULL;\n\nOh, so we actually do not even have to handle an unborn branch if we move\nthe `check_changes()` call after this block?\n\n> +\t\tgoto done;\n> +\t} else {\n> +\t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n> +\t}\n> +\n> +\tbranch_ref = resolve_ref_unsafe(\"HEAD\", 0, NULL, &flags);\n> +\tif (flags & REF_ISSYMREF)\n> +\t\tbranch_name = strrchr(branch_ref, '/') + 1;\n> +\thead_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n> +\t\t\t\t\t     DEFAULT_ABBREV);\n> +\tstrbuf_addf(&msg, \"%s: %s \", branch_name, head_short_sha1);\n> +\tpp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);\n> +\n> +\tstrbuf_addf(&commit_tree_label, \"index on %s\\n\", msg.buf);\n> +\tcommit_list_insert(head_commit, &parents);\n> +\tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n> +\t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n> +\t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n> +\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n> +\t\tret = -1;\n> +\t\t*stash_msg = NULL;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (include_untracked && get_untracked_files(ps, include_untracked,\n> +\t\t\t\t\t\t     &out)) {\n> +\t\tif (save_untracked_files(info, &msg, &out)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n> +\t\t\tret = -1;\n> +\t\t\t*stash_msg = NULL;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t\tuntracked_commit_option = 1;\n> +\t}\n> +\tif (patch_mode) {\n> +\t\tret = stash_patch(info, ps);\n> +\t\t*stash_msg = NULL;\n> +\t\tif (ret < 0) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n> +\t\t\tgoto done;\n> +\t\t} else if (ret > 0) {\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t} else {\n> +\t\tif (stash_working_tree(info, ps)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n> +\t\t\tret = -1;\n> +\t\t\t*stash_msg = NULL;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t}\n> +\n> +\tif (!*stash_msg || !strlen(*stash_msg))\n> +\t\tstrbuf_addf(&stash_msg_buf, \"WIP on %s\", msg.buf);\n> +\telse\n> +\t\tstrbuf_addf(&stash_msg_buf, \"On %s: %s\", branch_name,\n> +\t\t\t    *stash_msg);\n> +\t*stash_msg = strbuf_detach(&stash_msg_buf, NULL);\n> +\n> +\t/*\n> +\t * `parents` will be empty after calling `commit_tree()`, so there is\n> +\t * no need to call `free_commit_list()`\n\nIf it is empty, why do we need to set it to `NULL` explicitly?\n\n> +\t */\n> +\tparents = NULL;\n> +\tif (untracked_commit_option)\n> +\t\tcommit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n> +\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n> +\tcommit_list_insert(head_commit, &parents);\n> +\n> +\tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n> +\t\t\tparents, &info->w_commit, NULL, NULL)) {\n> +\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +done:\n> +\tstrbuf_release(&commit_tree_label);\n> +\tstrbuf_release(&msg);\n> +\tstrbuf_release(&out);\n> +\tstrbuf_release(&stash_msg_buf);\n> +\treturn ret;\n> +}\n> +\n> +static int create_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint include_untracked = 0;\n> +\tint ret = 0;\n> +\tconst char *stash_msg = NULL;\n\nAs we are taking custody of the buffer here, `stash_msg` should be of type\n`char *`, i.e. no `const`.\n\nThe rest of the patch looks good to me.\n\nPhew! What a patch! This took quite a while to review, so I am taking a\nbreak with this patch series here, and hope to continue in a few hours\nfrom now.\n\nThanks,\nDscho\n\n> +\tstruct stash_info info;\n> +\tstruct pathspec ps;\n> +\tstruct option options[] = {\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> +\t\t\t N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_create_usage,\n> +\t\t\t     0);\n> +\n> +\tmemset(&ps, 0, sizeof(ps));\n> +\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);\n> +\n> +\tif (!ret)\n> +\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n> +\n> +\t/*\n> +\t * ret can be 1 if there were no changes. In this case, we should\n> +\t * not error out.\n> +\t */\n> +\tfree((char *) stash_msg);\n> +\treturn ret < 0;\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -812,6 +1240,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!show_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"store\"))\n>  \t\treturn !!store_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"create\"))\n> +\t\treturn !!create_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 5739c51527..ab06e4ffb8 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -425,7 +425,7 @@ clear)\n>  \t;;\n>  create)\n>  \tshift\n> -\tcreate_stash -m \"$*\" && echo \"$w_commit\"\n> +\tgit stash--helper create --message \"$*\"\n>  \t;;\n>  store)\n>  \tshift\n> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357246","messageId":"nycvar.QRO.7.76.6.1809032031090.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"6832c979c8dca2a3584ed3cf828a6de060413cda.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 17/20] stash: convert save to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T18:44:23Z","receivedAt":"2018-09-03T18:44:28Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> Add stash save to the helper and delete functions which are no\n> longer needed (`show_help()`, `save_stash()`, `push_stash()`,\n> `create_stash()`, `clear_stash()`, `untracked_files()` and\n> `no_changes()`).\n> \n> The `-m` option is no longer supported as it might not make\n> sense to have two ways of passing a message. Even if this is\n> a change in behaviour, the documentation remains the same\n> because the `-m` parameter was omitted before.\n\nIt makes me slightly nervous to remove the `-m` option like this. And I\nwonder how difficult it would be to allow for the `-m` option, still, just\nto keep the occasional script running that might rely on this feature.\n\nI could imagine that it would be as easy as\n\n\tconst char *stash_msg = NULL;\n\t[...]\n\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\",\n\t\t\t   N_(\"stash message\")),\n\t[...]\n\tstruct strbuf buf = STRBUF_INIT;\n\n\t[... parse_options() ...]\n\tif (argc)\n\t\tstash_msg = strbuf_join_argv(&buf, argc, argv, ' ');\n\n\t[...]\n\n\tstrbuf_release(&buf);\n\nwhere `strbuf_join_argv()` would be implemented like this (and I would put\nit into strbuf.c and strbuf.h:\n\n\tconst char *strbuf_join_argv(struct strbuf *buf,\n\t\t\t\t     int argc, const char **argv, char delim)\n\t{\n\t\tif (!argc)\n\t\t\treturn buf->buf;\n\n\t\tstrbuf_addstr(buf, *argv);\n\t\twhile (--i) {\n\t\t\tstrbuf_addch(buf, delim);\n\t\t\tstrbuf_addstr(buf, *(++argv);\n\t\t}\n\n\t\treturn buf->buf;\n\t}\n\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index e5153a63ea..1269f2548c 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -1444,6 +1452,48 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n>  \t\t\t     include_untracked);\n>  }\n>  \n> +static int save_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint i;\n> +\tint keep_index = -1;\n> +\tint patch_mode = 0;\n> +\tint include_untracked = 0;\n> +\tint quiet = 0;\n> +\tint ret = 0;\n> +\tconst char *stash_msg = NULL;\n> +\tchar *to_free = NULL;\n> +\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n> +\tstruct pathspec ps;\n> +\tstruct option options[] = {\n> +\t\tOPT_SET_INT('k', \"keep-index\", &keep_index,\n> +\t\t\tN_(\"keep index\"), 1),\n> +\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n> +\t\t\tN_(\"stash in patch mode\")),\n> +\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n> +\t\t\t    N_(\"include ignore files\"), 2),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_save_usage,\n> +\t\t\t     0);\n> +\n> +\tfor (i = 0; i < argc; ++i)\n> +\t\tstrbuf_addf(&stash_msg_buf, \"%s \", argv[i]);\n\nI think I saw this in another patch already, and now it makes sense to\nintroduce the `strbuf_join_argv()` function, methinks.\n\n> +\tstash_msg = strbuf_detach(&stash_msg_buf, NULL);\n> +\tto_free = (char *) stash_msg;\n> +\n> +\tmemset(&ps, 0, sizeof(ps));\n> +\tret = do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n> +\t\t\t    include_untracked);\n> +\n> +\tfree(to_free);\n\nAs before, I would recommend to use stash_msg_buf.buf directly and call\nstrbuf_release() in the end (and drop the `to_free` variable altogether).\n\nThe rest looks good to me!\n\nThanks,\nDscho\n\n> +\treturn ret;\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -1484,6 +1534,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!create_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"push\"))\n>  \t\treturn !!push_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"save\"))\n> +\t\treturn !!save_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index c3146f62ab..695f1feba3 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -36,314 +36,6 @@ else\n>         reset_color=\n>  fi\n>  \n> -no_changes () {\n> -\tgit diff-index --quiet --cached HEAD --ignore-submodules -- \"$@\" &&\n> -\tgit diff-files --quiet --ignore-submodules -- \"$@\" &&\n> -\t(test -z \"$untracked\" || test -z \"$(untracked_files \"$@\")\")\n> -}\n> -\n> -untracked_files () {\n> -\tif test \"$1\" = \"-z\"\n> -\tthen\n> -\t\tshift\n> -\t\tz=-z\n> -\telse\n> -\t\tz=\n> -\tfi\n> -\texcl_opt=--exclude-standard\n> -\ttest \"$untracked\" = \"all\" && excl_opt=\n> -\tgit ls-files -o $z $excl_opt -- \"$@\"\n> -}\n> -\n> -clear_stash () {\n> -\tif test $# != 0\n> -\tthen\n> -\t\tdie \"$(gettext \"git stash clear with parameters is unimplemented\")\"\n> -\tfi\n> -\tif current=$(git rev-parse --verify --quiet $ref_stash)\n> -\tthen\n> -\t\tgit update-ref -d $ref_stash $current\n> -\tfi\n> -}\n> -\n> -create_stash () {\n> -\tstash_msg=\n> -\tuntracked=\n> -\twhile test $# != 0\n> -\tdo\n> -\t\tcase \"$1\" in\n> -\t\t-m|--message)\n> -\t\t\tshift\n> -\t\t\tstash_msg=${1?\"BUG: create_stash () -m requires an argument\"}\n> -\t\t\t;;\n> -\t\t-m*)\n> -\t\t\tstash_msg=${1#-m}\n> -\t\t\t;;\n> -\t\t--message=*)\n> -\t\t\tstash_msg=${1#--message=}\n> -\t\t\t;;\n> -\t\t-u|--include-untracked)\n> -\t\t\tshift\n> -\t\t\tuntracked=${1?\"BUG: create_stash () -u requires an argument\"}\n> -\t\t\t;;\n> -\t\t--)\n> -\t\t\tshift\n> -\t\t\tbreak\n> -\t\t\t;;\n> -\t\tesac\n> -\t\tshift\n> -\tdone\n> -\n> -\tgit update-index -q --refresh\n> -\tif no_changes \"$@\"\n> -\tthen\n> -\t\texit 0\n> -\tfi\n> -\n> -\t# state of the base commit\n> -\tif b_commit=$(git rev-parse --verify HEAD)\n> -\tthen\n> -\t\thead=$(git rev-list --oneline -n 1 HEAD --)\n> -\telse\n> -\t\tdie \"$(gettext \"You do not have the initial commit yet\")\"\n> -\tfi\n> -\n> -\tif branch=$(git symbolic-ref -q HEAD)\n> -\tthen\n> -\t\tbranch=${branch#refs/heads/}\n> -\telse\n> -\t\tbranch='(no branch)'\n> -\tfi\n> -\tmsg=$(printf '%s: %s' \"$branch\" \"$head\")\n> -\n> -\t# state of the index\n> -\ti_tree=$(git write-tree) &&\n> -\ti_commit=$(printf 'index on %s\\n' \"$msg\" |\n> -\t\tgit commit-tree $i_tree -p $b_commit) ||\n> -\t\tdie \"$(gettext \"Cannot save the current index state\")\"\n> -\n> -\tif test -n \"$untracked\"\n> -\tthen\n> -\t\t# Untracked files are stored by themselves in a parentless commit, for\n> -\t\t# ease of unpacking later.\n> -\t\tu_commit=$(\n> -\t\t\tuntracked_files -z \"$@\" | (\n> -\t\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n> -\t\t\t\texport GIT_INDEX_FILE &&\n> -\t\t\t\trm -f \"$TMPindex\" &&\n> -\t\t\t\tgit update-index -z --add --remove --stdin &&\n> -\t\t\t\tu_tree=$(git write-tree) &&\n> -\t\t\t\tprintf 'untracked files on %s\\n' \"$msg\" | git commit-tree $u_tree  &&\n> -\t\t\t\trm -f \"$TMPindex\"\n> -\t\t) ) || die \"$(gettext \"Cannot save the untracked files\")\"\n> -\n> -\t\tuntracked_commit_option=\"-p $u_commit\";\n> -\telse\n> -\t\tuntracked_commit_option=\n> -\tfi\n> -\n> -\tif test -z \"$patch_mode\"\n> -\tthen\n> -\n> -\t\t# state of the working tree\n> -\t\tw_tree=$( (\n> -\t\t\tgit read-tree --index-output=\"$TMPindex\" -m $i_tree &&\n> -\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n> -\t\t\texport GIT_INDEX_FILE &&\n> -\t\t\tgit diff-index --name-only -z HEAD -- \"$@\" >\"$TMP-stagenames\" &&\n> -\t\t\tgit update-index -z --add --remove --stdin <\"$TMP-stagenames\" &&\n> -\t\t\tgit write-tree &&\n> -\t\t\trm -f \"$TMPindex\"\n> -\t\t) ) ||\n> -\t\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n> -\n> -\telse\n> -\n> -\t\trm -f \"$TMP-index\" &&\n> -\t\tGIT_INDEX_FILE=\"$TMP-index\" git read-tree HEAD &&\n> -\n> -\t\t# find out what the user wants\n> -\t\tGIT_INDEX_FILE=\"$TMP-index\" \\\n> -\t\t\tgit add--interactive --patch=stash -- \"$@\" &&\n> -\n> -\t\t# state of the working tree\n> -\t\tw_tree=$(GIT_INDEX_FILE=\"$TMP-index\" git write-tree) ||\n> -\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n> -\n> -\t\tgit diff-tree -p HEAD $w_tree -- >\"$TMP-patch\" &&\n> -\t\ttest -s \"$TMP-patch\" ||\n> -\t\tdie \"$(gettext \"No changes selected\")\"\n> -\n> -\t\trm -f \"$TMP-index\" ||\n> -\t\tdie \"$(gettext \"Cannot remove temporary index (can't happen)\")\"\n> -\n> -\tfi\n> -\n> -\t# create the stash\n> -\tif test -z \"$stash_msg\"\n> -\tthen\n> -\t\tstash_msg=$(printf 'WIP on %s' \"$msg\")\n> -\telse\n> -\t\tstash_msg=$(printf 'On %s: %s' \"$branch\" \"$stash_msg\")\n> -\tfi\n> -\tw_commit=$(printf '%s\\n' \"$stash_msg\" |\n> -\tgit commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||\n> -\tdie \"$(gettext \"Cannot record working tree state\")\"\n> -}\n> -\n> -push_stash () {\n> -\tkeep_index=\n> -\tpatch_mode=\n> -\tuntracked=\n> -\tstash_msg=\n> -\twhile test $# != 0\n> -\tdo\n> -\t\tcase \"$1\" in\n> -\t\t-k|--keep-index)\n> -\t\t\tkeep_index=t\n> -\t\t\t;;\n> -\t\t--no-keep-index)\n> -\t\t\tkeep_index=n\n> -\t\t\t;;\n> -\t\t-p|--patch)\n> -\t\t\tpatch_mode=t\n> -\t\t\t# only default to keep if we don't already have an override\n> -\t\t\ttest -z \"$keep_index\" && keep_index=t\n> -\t\t\t;;\n> -\t\t-q|--quiet)\n> -\t\t\tGIT_QUIET=t\n> -\t\t\t;;\n> -\t\t-u|--include-untracked)\n> -\t\t\tuntracked=untracked\n> -\t\t\t;;\n> -\t\t-a|--all)\n> -\t\t\tuntracked=all\n> -\t\t\t;;\n> -\t\t-m|--message)\n> -\t\t\tshift\n> -\t\t\ttest -z ${1+x} && usage\n> -\t\t\tstash_msg=$1\n> -\t\t\t;;\n> -\t\t-m*)\n> -\t\t\tstash_msg=${1#-m}\n> -\t\t\t;;\n> -\t\t--message=*)\n> -\t\t\tstash_msg=${1#--message=}\n> -\t\t\t;;\n> -\t\t--help)\n> -\t\t\tshow_help\n> -\t\t\t;;\n> -\t\t--)\n> -\t\t\tshift\n> -\t\t\tbreak\n> -\t\t\t;;\n> -\t\t-*)\n> -\t\t\toption=\"$1\"\n> -\t\t\teval_gettextln \"error: unknown option for 'stash push': \\$option\"\n> -\t\t\tusage\n> -\t\t\t;;\n> -\t\t*)\n> -\t\t\tbreak\n> -\t\t\t;;\n> -\t\tesac\n> -\t\tshift\n> -\tdone\n> -\n> -\teval \"set $(git rev-parse --sq --prefix \"$prefix\" -- \"$@\")\"\n> -\n> -\tif test -n \"$patch_mode\" && test -n \"$untracked\"\n> -\tthen\n> -\t\tdie \"$(gettext \"Can't use --patch and --include-untracked or --all at the same time\")\"\n> -\tfi\n> -\n> -\ttest -n \"$untracked\" || git ls-files --error-unmatch -- \"$@\" >/dev/null || exit 1\n> -\n> -\tgit update-index -q --refresh\n> -\tif no_changes \"$@\"\n> -\tthen\n> -\t\tsay \"$(gettext \"No local changes to save\")\"\n> -\t\texit 0\n> -\tfi\n> -\n> -\tgit reflog exists $ref_stash ||\n> -\t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n> -\n> -\tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n> -\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n> -\tdie \"$(gettext \"Cannot save the current status\")\"\n> -\tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n> -\n> -\tif test -z \"$patch_mode\"\n> -\tthen\n> -\t\ttest \"$untracked\" = \"all\" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=\n> -\t\tif test -n \"$untracked\" && test $# = 0\n> -\t\tthen\n> -\t\t\tgit clean --force --quiet -d $CLEAN_X_OPTION\n> -\t\tfi\n> -\n> -\t\tif test $# != 0\n> -\t\tthen\n> -\t\t\ttest -z \"$untracked\" && UPDATE_OPTION=\"-u\" || UPDATE_OPTION=\n> -\t\t\ttest \"$untracked\" = \"all\" && FORCE_OPTION=\"--force\" || FORCE_OPTION=\n> -\t\t\tgit add $UPDATE_OPTION $FORCE_OPTION -- \"$@\"\n> -\t\t\tgit diff-index -p --cached --binary HEAD -- \"$@\" |\n> -\t\t\tgit apply --index -R\n> -\t\telse\n> -\t\t\tgit reset --hard -q\n> -\t\tfi\n> -\n> -\t\tif test \"$keep_index\" = \"t\" && test -n \"$i_tree\"\n> -\t\tthen\n> -\t\t\tgit read-tree --reset $i_tree\n> -\t\t\tgit ls-files -z --modified -- \"$@\" |\n> -\t\t\tgit checkout-index -z --force --stdin\n> -\t\tfi\n> -\telse\n> -\t\tgit apply -R < \"$TMP-patch\" ||\n> -\t\tdie \"$(gettext \"Cannot remove worktree changes\")\"\n> -\n> -\t\tif test \"$keep_index\" != \"t\"\n> -\t\tthen\n> -\t\t\tgit reset -q -- \"$@\"\n> -\t\tfi\n> -\tfi\n> -}\n> -\n> -save_stash () {\n> -\tpush_options=\n> -\twhile test $# != 0\n> -\tdo\n> -\t\tcase \"$1\" in\n> -\t\t--)\n> -\t\t\tshift\n> -\t\t\tbreak\n> -\t\t\t;;\n> -\t\t-*)\n> -\t\t\t# pass all options through to push_stash\n> -\t\t\tpush_options=\"$push_options $1\"\n> -\t\t\t;;\n> -\t\t*)\n> -\t\t\tbreak\n> -\t\t\t;;\n> -\t\tesac\n> -\t\tshift\n> -\tdone\n> -\n> -\tstash_msg=\"$*\"\n> -\n> -\tif test -z \"$stash_msg\"\n> -\tthen\n> -\t\tpush_stash $push_options\n> -\telse\n> -\t\tpush_stash $push_options -m \"$stash_msg\"\n> -\tfi\n> -}\n> -\n> -show_help () {\n> -\texec git help stash\n> -\texit 1\n> -}\n> -\n>  #\n>  # Parses the remaining options looking for flags and\n>  # at most one revision defaulting to ${ref_stash}@{0}\n> @@ -408,7 +100,8 @@ show)\n>  \t;;\n>  save)\n>  \tshift\n> -\tsave_stash \"$@\"\n> +\tcd \"$START_DIR\"\n> +\tgit stash--helper save \"$@\"\n>  \t;;\n>  push)\n>  \tshift\n> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357247","messageId":"nycvar.QRO.7.76.6.1809032046470.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"e4c17b20b0204386be06270e4a7f7283e6641f5a.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 19/20] stash: optimize `get_untracked_files()` and `check_changes()`","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T18:49:15Z","receivedAt":"2018-09-03T18:49:20Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> This commits introduces a optimization by avoiding calling the\n> same functions again. For example, `git stash push -u`\n> would call at some points the following functions:\n> \n>  * `check_changes()` (inside `do_push_stash()`)\n>  * `do_create_stash()`, which calls: `check_changes()` and\n> `get_untracked_files()`\n> \n> Note that `check_changes()` also calls `get_untracked_files()`.\n> So, `check_changes()` is called 2 times and `get_untracked_files()`\n> 3 times.\n> \n> `get_untracked_files()` has now only two parameters and it will\n> fill a global strbuf called `untracked_files`.\n> \n> The old function `check_changes()` now consists of two functions:\n> `get_untracked_files()` and `check_changes_tracked_files()`.\n> \n> These are the call chains for `push` and `create`:\n> \n>  * `push_stash()` -> `do_push_stash()` -> `do_create_stash()`\n> \n>  * `create_stash()` -> `do_create_stash()`\n> \n> To prevent calling the same functions over and over again,\n> `check_changes()` inside `do_create_stash()` is now placed\n> in the caller functions (`create_stash()` and `do_push_stash()`).\n> This way `check_changes()` and `get_untracked files()` are called\n> only one time.\n> \n> https://public-inbox.org/git/20180818223329.GJ11326@hank.intra.tgummerer.com/\n\nI like the idea, and I like most of the implementation of it. However...\n\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash.c | 73 ++++++++++++++++++++++---------------------------\n>  1 file changed, 33 insertions(+), 40 deletions(-)\n> \n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index 3d2316e3f7..ba5818e24e 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -813,13 +813,15 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n>  }\n>  \n>  /*\n> - * `out` will be filled with the names of untracked files. The return value is:\n> + * `untracked_files` will be filled with the names of untracked files.\n> + * The return value is:\n>   *\n>   * = 0 if there are not any untracked files\n>   * > 0 if there are untracked files\n>   */\n> -static int get_untracked_files(struct pathspec ps, int include_untracked,\n> -\t\t\t       struct strbuf *out)\n> +static struct strbuf untracked_files = STRBUF_INIT;\n\n... file-local variables invariably make it harder to reuse functions than\nnecessary. And as your code is pretty neat, I imagine that other\ndevelopers might want to reuse your functions by moving them out of the\nbuiltin/ realm into the \"internal API\", and it would be better to prepare\nthe functions that way.\n\nSo if you can think at all of a place (or more places) in callers that\ncould hold the `untracked_files` variable, that would make it much easier\nto \"libify\" your functions.\n\nThanks,\nDscho\n\n> +\n> +static int get_untracked_files(struct pathspec ps, int include_untracked)\n>  {\n>  \tint max_len;\n>  \tint i;\n> @@ -839,7 +841,7 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,\n>  \t\t\tfree(ent);\n>  \t\t\tcontinue;\n>  \t\t}\n> -\t\tstrbuf_addf(out, \"%s%c\", ent->name, '\\0');\n> +\t\tstrbuf_addf(&untracked_files, \"%s%c\", ent->name, '\\0');\n>  \t\tfree(ent);\n>  \t}\n>  \n> @@ -847,23 +849,22 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,\n>  \tfree(dir.ignored);\n>  \tclear_directory(&dir);\n>  \tfree(seen);\n> -\treturn out->len;\n> +\treturn untracked_files.len;\n>  }\n>  \n>  /*\n> - * The return value of `check_changes()` can be:\n> + * The return value of `check_changes_tracked_files()` can be:\n>   *\n>   * < 0 if there was an error\n>   * = 0 if there are no changes.\n>   * > 0 if there are changes.\n>   */\n> -static int check_changes(struct pathspec ps, int include_untracked)\n> +\n> +static int check_changes_tracked_files(struct pathspec ps)\n>  {\n>  \tint result;\n> -\tint ret = 0;\n>  \tstruct rev_info rev;\n>  \tstruct object_id dummy;\n> -\tstruct strbuf out = STRBUF_INIT;\n>  \n>  \tinit_revisions(&rev, NULL);\n>  \trev.prune_data = ps;\n> @@ -890,18 +891,22 @@ static int check_changes(struct pathspec ps, int include_untracked)\n>  \tif (diff_result_code(&rev.diffopt, result))\n>  \t\treturn 1;\n>  \n> -\tif (include_untracked && get_untracked_files(ps, include_untracked,\n> -\t\t\t\t\t\t     &out)) {\n> -\t\tstrbuf_release(&out);\n> -\t\treturn 1;\n> -\t}\n> -\n> -\tstrbuf_release(&out);\n>  \treturn 0;\n>  }\n>  \n> -static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n> -\t\t\t\tstruct strbuf *in)\n> +static int check_changes(struct pathspec ps, int include_untracked)\n> +{\n> +\tint ret = 0;\n> +\tif (check_changes_tracked_files(ps))\n> +\t\tret = 1;\n> +\n> +\tif (include_untracked && get_untracked_files(ps, include_untracked))\n> +\t\tret = 1;\n> +\n> +\treturn ret;\n> +}\n> +\n> +static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n>  {\n>  \tint ret = 0;\n>  \tstruct strbuf untracked_msg = STRBUF_INIT;\n> @@ -916,7 +921,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n>  \t\t\t stash_index_path.buf);\n>  \n>  \tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n> -\tif (pipe_command(&cp_upd_index, in->buf, in->len, NULL, 0, NULL, 0)) {\n> +\tif (pipe_command(&cp_upd_index, untracked_files.buf, untracked_files.len,\n> +\t\t\t NULL, 0, NULL, 0)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> @@ -1090,18 +1096,11 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n>  \tstruct commit_list *parents = NULL;\n>  \tstruct strbuf msg = STRBUF_INIT;\n>  \tstruct strbuf commit_tree_label = STRBUF_INIT;\n> -\tstruct strbuf out = STRBUF_INIT;\n>  \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n>  \n>  \tread_cache_preload(NULL);\n>  \trefresh_cache(REFRESH_QUIET);\n>  \n> -\tif (!check_changes(ps, include_untracked)) {\n> -\t\tret = 1;\n> -\t\t*stash_msg = NULL;\n> -\t\tgoto done;\n> -\t}\n> -\n>  \tif (get_oid(\"HEAD\", &info->b_commit)) {\n>  \t\tif (!quiet)\n>  \t\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n> @@ -1132,9 +1131,8 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tif (include_untracked && get_untracked_files(ps, include_untracked,\n> -\t\t\t\t\t\t     &out)) {\n> -\t\tif (save_untracked_files(info, &msg, &out)) {\n> +\tif (include_untracked) {\n> +\t\tif (save_untracked_files(info, &msg)) {\n>  \t\t\tif (!quiet)\n>  \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n>  \t\t\tret = -1;\n> @@ -1191,7 +1189,6 @@ static int do_create_stash(struct pathspec ps, const char **stash_msg,\n>  done:\n>  \tstrbuf_release(&commit_tree_label);\n>  \tstrbuf_release(&msg);\n> -\tstrbuf_release(&out);\n>  \tstrbuf_release(&stash_msg_buf);\n>  \treturn ret;\n>  }\n> @@ -1213,25 +1210,21 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n>  \t\t\t     git_stash_create_usage,\n>  \t\t\t     0);\n>  \n> +\tmemset(&ps, 0, sizeof(ps));\n> +\tif (!check_changes_tracked_files(ps))\n> +\t\treturn 0;\n> +\n>  \tfor (i = 0; i < argc; ++i)\n>  \t\tstrbuf_addf(&stash_msg_buf, \"%s \", argv[i]);\n>  \tstash_msg = strbuf_detach(&stash_msg_buf, NULL);\n>  \tto_free = (char *) stash_msg;\n>  \n> -\tmemset(&ps, 0, sizeof(ps));\n> -\tret = do_create_stash(ps, &stash_msg, 0, 0, &info, 0);\n> -\n> -\tif (!ret)\n> +\tif (!(ret = do_create_stash(ps, &stash_msg, 0, 0, &info, 0)))\n>  \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n>  \n>  \tfree(to_free);\n>  \tfree((char *) stash_msg);\n> -\n> -\t/*\n> -\t * ret can be 1 if there were no changes. In this case, we should\n> -\t * not error out.\n> -\t */\n> -\treturn ret < 0;\n> +\treturn ret;\n>  }\n>  \n>  static void add_ps_items_to_argv_array(struct argv_array *args,\n> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357248","messageId":"CAPig+cTGBi1P5V3uGwtKCvoCn=-17y1dY3dBkS13Yx1ikjpXcQ@mail.gmail.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1809032031090.71@tvgsbejvaqbjf.bet","subject":"Re: [GSoC][PATCH v8 17/20] stash: convert save to builtin","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2018-09-03T19:06:54Z","receivedAt":"2018-09-03T19:07:07Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Mon, Sep 3, 2018 at 2:44 PM Johannes Schindelin\n<Johannes.Schindelin@gmx.de> wrote:\n> where `strbuf_join_argv()` would be implemented like this (and I would put\n> it into strbuf.c and strbuf.h:\n>\n>         const char *strbuf_join_argv(struct strbuf *buf,\n>                                      int argc, const char **argv, char delim)\n>         {\n>                 if (!argc)\n>                         return buf->buf;\n>\n>                 strbuf_addstr(buf, *argv);\n>                 while (--i) {\n\ns/i/argc/\n\n>                         strbuf_addch(buf, delim);\n>                         strbuf_addstr(buf, *(++argv);\n>                 }\n>\n>                 return buf->buf;\n>         }\n"},{"id":"357249","messageId":"nycvar.QRO.7.76.6.1809032110520.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"c26283d74e3f761a554940e3e0db13cef1b613d5.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 20/20] stash: replace all `write-tree` child processes with API calls","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T19:11:02Z","receivedAt":"2018-09-03T19:11:07Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> This commit replaces spawning `git write-tree` with API calls.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash.c | 41 ++++++++++++-----------------------------\n>  1 file changed, 12 insertions(+), 29 deletions(-)\n\nVery nice!\n\nThanks,\nDscho\n\n> \n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index ba5818e24e..dd1084afd4 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -910,9 +910,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n>  {\n>  \tint ret = 0;\n>  \tstruct strbuf untracked_msg = STRBUF_INIT;\n> -\tstruct strbuf out = STRBUF_INIT;\n>  \tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n> -\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> +\tstruct index_state istate = { NULL };\n>  \n>  \tcp_upd_index.git_cmd = 1;\n>  \targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n> @@ -927,15 +926,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tcp_write_tree.git_cmd = 1;\n> -\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> -\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> -\t\t\t stash_index_path.buf);\n> -\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\tif (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,\n> +\t\t\t\tNULL)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> -\tget_oid_hex(out.buf, &info->u_tree);\n>  \n>  \tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n>  \t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n> @@ -944,8 +939,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg)\n>  \t}\n>  \n>  done:\n> +\tdiscard_index(&istate);\n>  \tstrbuf_release(&untracked_msg);\n> -\tstrbuf_release(&out);\n>  \tremove_path(stash_index_path.buf);\n>  \treturn ret;\n>  }\n> @@ -956,11 +951,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n>  {\n>  \tint i;\n>  \tint ret = 0;\n> -\tstruct strbuf out = STRBUF_INIT;\n>  \tstruct child_process cp_read_tree = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp_add_i = CHILD_PROCESS_INIT;\n> -\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n>  \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n> +\tstruct index_state istate = { NULL };\n>  \n>  \tremove_path(stash_index_path.buf);\n>  \n> @@ -985,17 +979,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tcp_write_tree.git_cmd = 1;\n> -\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> -\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> -\t\t\t stash_index_path.buf);\n> -\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n> +\t\t\t\tNULL)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tget_oid_hex(out.buf, &info->w_tree);\n> -\n>  \tcp_diff_tree.git_cmd = 1;\n>  \targv_array_pushl(&cp_diff_tree.args, \"diff-tree\", \"-p\", \"HEAD\",\n>  \t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n> @@ -1011,7 +1000,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, int quiet)\n>  \t}\n>  \n>  done:\n> -\tstrbuf_release(&out);\n> +\tdiscard_index(&istate);\n>  \tremove_path(stash_index_path.buf);\n>  \treturn ret;\n>  }\n> @@ -1020,10 +1009,9 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n>  {\n>  \tint ret = 0;\n>  \tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n> -\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> -\tstruct strbuf out = STRBUF_INIT;\n>  \tstruct strbuf diff_output = STRBUF_INIT;\n>  \tstruct rev_info rev;\n> +\tstruct index_state istate = { NULL };\n>  \n>  \tset_alternate_index_output(stash_index_path.buf);\n>  \tif (reset_tree(&info->i_tree, 0, 0)) {\n> @@ -1062,20 +1050,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tcp_write_tree.git_cmd = 1;\n> -\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> -\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> -\t\t\t stash_index_path.buf);\n> -\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n> +\t\t\t\tNULL)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tget_oid_hex(out.buf, &info->w_tree);\n> -\n>  done:\n> +\tdiscard_index(&istate);\n>  \tUNLEAK(rev);\n> -\tstrbuf_release(&out);\n>  \tobject_array_clear(&rev.pending);\n>  \tstrbuf_release(&diff_output);\n>  \tremove_path(stash_index_path.buf);\n> -- \n> 2.19.0.rc0.22.gc26283d74e\n> \n> \n"},{"id":"357251","messageId":"nycvar.QRO.7.76.6.1809032111100.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"Re: [GSoC][PATCH v8 00/20] Convert \"git stash\" to C builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T19:12:25Z","receivedAt":"2018-09-03T19:12:28Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Fri, 31 Aug 2018, Paul-Sebastian Ungureanu wrote:\n\n> This a new iteration of `stash.c`.\n\nThank you for the pleasant read!\n\nI read through all of the patches, spending particularly some time with\nthe `stash create` one.\n\nApart from the few comments I had, I only have positive things to say ;-)\n\nThanks,\nDscho\n"},{"id":"357256","messageId":"nycvar.QRO.7.76.6.1809032238240.71@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"CAPig+cTGBi1P5V3uGwtKCvoCn=-17y1dY3dBkS13Yx1ikjpXcQ@mail.gmail.com","subject":"Re: [GSoC][PATCH v8 17/20] stash: convert save to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-09-03T20:38:42Z","receivedAt":"2018-09-03T20:38:47Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Eric,\n\nOn Mon, 3 Sep 2018, Eric Sunshine wrote:\n\n> On Mon, Sep 3, 2018 at 2:44 PM Johannes Schindelin\n> <Johannes.Schindelin@gmx.de> wrote:\n> > where `strbuf_join_argv()` would be implemented like this (and I would put\n> > it into strbuf.c and strbuf.h:\n> >\n> >         const char *strbuf_join_argv(struct strbuf *buf,\n> >                                      int argc, const char **argv, char delim)\n> >         {\n> >                 if (!argc)\n> >                         return buf->buf;\n> >\n> >                 strbuf_addstr(buf, *argv);\n> >                 while (--i) {\n> \n> s/i/argc/\n\nRight you are!\n\nThank you,\nDscho\n\n> \n> >                         strbuf_addch(buf, delim);\n> >                         strbuf_addstr(buf, *(++argv);\n> >                 }\n> >\n> >                 return buf->buf;\n> >         }\n> \n"},{"id":"358881","messageId":"5ba50c37-2840-e1f9-4d35-877979018f1c@gmail.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1809031718230.71@tvgsbejvaqbjf.bet","subject":"Re: [GSoC][PATCH v8 14/20] stash: convert create to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:20:34Z","receivedAt":"2018-09-25T22:20:40Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hi,\n\nSorry for the late reply. I had a lot on my plate for the last couple of \nweeks.\n\n>> +\n>> +\tgit_config(git_diff_basic_config, NULL);\n> \n> Is this not called in as part of `git_config(git_default_config, NULL);`\n> in cmd_stash() already?\n> \n> *clicketyclick*\n> \n> I guess not. But then, maybe it would make sense to run with\n> `git_diff_basic_config` from the get go, to avoid having to run\n> `git_config()` twice.\n\nI am not sure I got it right, but if I did:\n\nRunning `git_config` with `git_diff_basic_config` from the\nbeginning wouldn't be pointless when we would use any other\ncommand than `create`, `push` and `save`? Although it might\nconfuse the reader a little bit, the stash should run without\nproblems.\n\nBest,\nPaul\n"},{"id":"358882","messageId":"4de1b725-8687-590a-f93f-e80843e5bf8f@gmail.com","threadId":"48222","inReplyTo":"nycvar.QRO.7.76.6.1809031640490.71@tvgsbejvaqbjf.bet","subject":"Re: [GSoC][PATCH v8 18/20] stash: convert `stash--helper.c` into `stash.c`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:31:07Z","receivedAt":"2018-09-25T22:31:13Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hi,\n\n>>   \n>> @@ -1443,9 +1448,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n>>   \t\tOPT_END()\n>>   \t};\n>>   \n>> -\targc = parse_options(argc, argv, prefix, options,\n>> -\t\t\t     git_stash_helper_push_usage,\n>> -\t\t\t     0);\n>> +\tif (argc)\n>> +\t\targc = parse_options(argc, argv, prefix, options,\n>> +\t\t\t\t     git_stash_push_usage,\n>> +\t\t\t\t     0);\n> \n> Is this `if (argc)` guard necessary?\n\nYes because without it, there would be a seg fault when\ncalling `git stash`. Why?\n\nAfter spawning `git stash`, in `push_stash()`: `argc` would be\n0 (and `argv` would be `NULL`).\n\n`parse_options()` calls `parse_options_start()` which does the following:\n\n\tctx->argc = ctx->total = argc - 1;\n\tctx->argv = argv + 1;\n\nSo, `ctx->argc` would be `-1`. After we are back to `parse_options()`,\n`parse_options_step()` would be called. In `parse_options_step()`:\n\n\tfor (; ctx->argc; ctx->argc--, ctx->argv++)\n\nWhich is an infinite loop, because `ctx->argc` is already -1.\n\nThis check is also done for `git notes list` (function `list()`\ninside `builtin/notes.c`). Now, that I relook at it, it seems to me\nthat this is a bug. Probably a better solution would be to check at the \nbeginning of `parse_options()` and return early if `argc` is less then one.\n\n>> @@ -1536,7 +1544,44 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>>   \t\treturn !!push_stash(argc, argv, prefix);\n>>   \telse if (!strcmp(argv[0], \"save\"))\n>>   \t\treturn !!save_stash(argc, argv, prefix);\n>> +\telse if (*argv[0] != '-')\n>> +\t\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>> +\t\t\t      git_stash_usage, options);\n>> +\n>> +\tif (strcmp(argv[0], \"-p\")) {\n>> +\t\twhile (++i < argc && strcmp(argv[i], \"--\")) {\n>> +\t\t\t/*\n>> +\t\t\t * `akpqu` is a string which contains all short options,\n>> +\t\t\t * except `-m` which is verified separately.\n>> +\t\t\t */\n>> +\t\t\tif ((strlen(argv[i]) == 2) && *argv[i] == '-' &&\n>> +\t\t\t    strchr(\"akpqu\", argv[i][1]))\n> \n> I *think* this is missing the `n`.\n\nI guess that by `n` you are referring to the short option of\n`--no-keep-index`, which is missing because it was also omitted\nin `stash.sh`. I am not sure whether it is worth adding it. In\ncase `stash` will learn any other option starting with `n`, this\nmight create confusion amongst users.\n\nBest regards,\nPaul\n"},{"id":"358884","messageId":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1535665109.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 00/21] Convert \"git stash\" to C builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:11Z","receivedAt":"2018-09-25T22:34:22Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nThis is a new iteration of `git stash`, based on the last review I got.\nThis new iteration brings mostly code styling fix issues in order to make\nthe code more readable. There is also a new patch \"strbuf.c: add\n`strbuf_join_argv()`\". By making some small changes, the code is now a\nlittle bit closer to be used as API.\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\nPaul-Sebastian Ungureanu (16):\n  sha1-name.c: add `get_oidf()` which acts like `get_oid()`\n  strbuf.c: add `strbuf_join_argv()`\n  stash: update test cases conform to coding guidelines\n  stash: rename test cases to be more descriptive\n  stash: add tests for `git stash show` config\n  stash: convert list to builtin\n  stash: convert show to builtin\n  stash: mention options in `show` synopsis.\n  stash: convert store to builtin\n  stash: convert create to builtin\n  stash: convert push to builtin\n  stash: make push -q quiet\n  stash: convert save to builtin\n  stash: convert `stash--helper.c` into `stash.c`\n  stash: optimize `get_untracked_files()` and `check_changes()`\n  stash: replace all `write-tree` child processes with API calls\n\n Documentation/git-stash.txt  |    4 +-\n Makefile                     |    2 +-\n builtin.h                    |    1 +\n builtin/stash.c              | 1595 ++++++++++++++++++++++++++++++++++\n cache.h                      |    1 +\n git-stash.sh                 |  752 ----------------\n git.c                        |    1 +\n sha1-name.c                  |   19 +\n strbuf.c                     |   15 +\n strbuf.h                     |    7 +\n t/t3903-stash.sh             |  192 ++--\n t/t3907-stash-show-config.sh |   83 ++\n 12 files changed, 1851 insertions(+), 821 deletions(-)\n create mode 100644 builtin/stash.c\n delete mode 100755 git-stash.sh\n create mode 100755 t/t3907-stash-show-config.sh\n\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358885","messageId":"b21cdeb1636b679d80ff554c48bc76516aed2cf6.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 01/21] sha1-name.c: add `get_oidf()` which acts like `get_oid()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:12Z","receivedAt":"2018-09-25T22:34:23Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Compared to `get_oid()`, `get_oidf()` has as parameters\na pointer to `object_id`, a printf format string and\nadditional arguments. This will help simplify the code\nin subsequent commits.\n\nOriginal-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n cache.h     |  1 +\n sha1-name.c | 19 +++++++++++++++++++\n 2 files changed, 20 insertions(+)\n\ndiff --git a/cache.h b/cache.h\nindex b1fd3d58ab..d93b2e25a5 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1309,6 +1309,7 @@ struct object_context {\n \tGET_OID_BLOB)\n \n extern int get_oid(const char *str, struct object_id *oid);\n+extern int get_oidf(struct object_id *oid, const char *fmt, ...);\n extern int get_oid_commit(const char *str, struct object_id *oid);\n extern int get_oid_committish(const char *str, struct object_id *oid);\n extern int get_oid_tree(const char *str, struct object_id *oid);\ndiff --git a/sha1-name.c b/sha1-name.c\nindex c9cc1318b7..261b960bbd 100644\n--- a/sha1-name.c\n+++ b/sha1-name.c\n@@ -1471,6 +1471,25 @@ int get_oid(const char *name, struct object_id *oid)\n \treturn get_oid_with_context(name, 0, oid, &unused);\n }\n \n+/*\n+ * This returns a non-zero value if the string (built using printf\n+ * format and the given arguments) is not a valid object.\n+ */\n+int get_oidf(struct object_id *oid, const char *fmt, ...)\n+{\n+\tva_list ap;\n+\tint ret;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tva_start(ap, fmt);\n+\tstrbuf_vaddf(&sb, fmt, ap);\n+\tva_end(ap);\n+\n+\tret = get_oid(sb.buf, oid);\n+\tstrbuf_release(&sb);\n+\n+\treturn ret;\n+}\n \n /*\n  * Many callers know that the user meant to name a commit-ish by\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358887","messageId":"1fb6d7a72c067fb594b04944dea4a5d223229d69.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 02/21] strbuf.c: add `strbuf_join_argv()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:14Z","receivedAt":"2018-09-25T22:34:25Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Implement `strbuf_join_argv()` to join arguments\ninto a strbuf.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n strbuf.c | 15 +++++++++++++++\n strbuf.h |  7 +++++++\n 2 files changed, 22 insertions(+)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex 64041c3c24..3eb431b2b0 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -259,6 +259,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)\n \tstrbuf_setlen(sb, sb->len + sb2->len);\n }\n \n+const char *strbuf_join_argv(struct strbuf *buf,\n+\t\t\t     int argc, const char **argv, char delim)\n+{\n+\tif (!argc)\n+\t\treturn buf->buf;\n+\n+\tstrbuf_addstr(buf, *argv);\n+\twhile (--argc) {\n+\t\tstrbuf_addch(buf, delim);\n+\t\tstrbuf_addstr(buf, *(++argv));\n+\t}\n+\n+\treturn buf->buf;\n+}\n+\n void strbuf_addchars(struct strbuf *sb, int c, size_t n)\n {\n \tstrbuf_grow(sb, n);\ndiff --git a/strbuf.h b/strbuf.h\nindex 60a35aef16..7ed859bb8a 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -284,6 +284,13 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)\n  */\n extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);\n \n+\n+/**\n+ *\n+ */\n+extern const char *strbuf_join_argv(struct strbuf *buf, int argc,\n+\t\t\t\t    const char **argv, char delim);\n+\n /**\n  * This function can be used to expand a format string containing\n  * placeholders. To that end, it parses the string and calls the specified\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358888","messageId":"b40495d6bdbba753840486888b02559513b43d80.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 03/21] stash: improve option parsing test coverage","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:15Z","receivedAt":"2018-09-25T22:34:26Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nIn 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 parameters, or too few.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++\n 1 file changed, 35 insertions(+)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex 1f871d3cca..af7586d43d 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' '\n \ttest foo = \"$(cat file/file)\"\n '\n \n+test_expect_success 'giving too many ref arguments does not modify files' '\n+\tgit stash clear &&\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\techo foo >file2 &&\n+\tgit stash &&\n+\techo bar >file2 &&\n+\tgit stash &&\n+\ttest-tool chmtime =123456789 file2 &&\n+\tfor type in apply pop \"branch stash-branch\"\n+\tdo\n+\t\ttest_must_fail git stash $type stash@{0} stash@{1} 2>err &&\n+\t\ttest_i18ngrep \"Too many revisions\" err &&\n+\t\ttest 123456789 = $(test-tool chmtime -g file2) || return 1\n+\tdone\n+'\n+\n+test_expect_success 'drop: too many arguments errors out (does nothing)' '\n+\tgit stash list >expect &&\n+\ttest_must_fail git stash drop stash@{0} stash@{1} 2>err &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\tgit stash list >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'show: too many arguments errors out (does nothing)' '\n+\ttest_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&\n+\ttest_i18ngrep \"Too many revisions\" err &&\n+\ttest_must_be_empty out\n+'\n+\n test_expect_success 'stash create - no changes' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n@@ -479,6 +509,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.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358891","messageId":"e2ced7320ffc205739133858a6b301c57893254f.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 04/21] stash: update test cases conform to coding guidelines","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:18Z","receivedAt":"2018-09-25T22:34:30Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Removed whitespaces after redirection operators.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------\n 1 file changed, 61 insertions(+), 59 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex af7586d43d..de6cab1fe7 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -8,22 +8,22 @@ test_description='Test git stash'\n . ./test-lib.sh\n \n test_expect_success 'stash some dirty working directory' '\n-\techo 1 > file &&\n+\techo 1 >file &&\n \tgit add file &&\n \techo unrelated >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m initial &&\n-\techo 2 > file &&\n+\techo 2 >file &&\n \tgit add file &&\n-\techo 3 > file &&\n+\techo 3 >file &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file b/file\n index 0cfbf08..00750ed 100644\n --- a/file\n@@ -35,7 +35,7 @@ EOF\n \n test_expect_success 'parents of stash' '\n \ttest $(git rev-parse stash^) = $(git rev-parse HEAD) &&\n-\tgit diff stash^2..stash > output &&\n+\tgit diff stash^2..stash >output &&\n \ttest_cmp output expect\n '\n \n@@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '\n \n test_expect_success 'apply stashed changes (including index)' '\n \tgit reset --hard HEAD^ &&\n-\techo 6 > other-file &&\n+\techo 6 >other-file &&\n \tgit add other-file &&\n \ttest_tick &&\n \tgit commit -m other-file &&\n@@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '\n \n test_expect_success 'drop top stash' '\n \tgit reset --hard &&\n-\tgit stash list > stashlist1 &&\n-\techo 7 > file &&\n+\tgit stash list >expected &&\n+\techo 7 >file &&\n \tgit stash &&\n \tgit stash drop &&\n-\tgit stash list > stashlist2 &&\n-\ttest_cmp stashlist1 stashlist2 &&\n+\tgit stash list >actual &&\n+\ttest_cmp expected actual &&\n \tgit stash apply &&\n \ttest 3 = $(cat file) &&\n \ttest 1 = $(git show :file) &&\n@@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '\n \n test_expect_success 'drop middle stash' '\n \tgit reset --hard &&\n-\techo 8 > file &&\n+\techo 8 >file &&\n \tgit stash &&\n-\techo 9 > file &&\n+\techo 9 >file &&\n \tgit stash &&\n \tgit stash drop stash@{1} &&\n \ttest 2 = $(git stash list | wc -l) &&\n@@ -160,7 +160,7 @@ test_expect_success 'stash pop' '\n \ttest 0 = $(git stash list | wc -l)\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/file2 b/file2\n new file mode 100644\n index 0000000..1fe912c\n@@ -170,7 +170,7 @@ index 0000000..1fe912c\n +bar2\n EOF\n \n-cat > expect1 << EOF\n+cat >expect1 <<EOF\n diff --git a/file b/file\n index 257cc56..5716ca5 100644\n --- a/file\n@@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644\n +bar\n EOF\n \n-cat > expect2 << EOF\n+cat >expect2 <<EOF\n diff --git a/file b/file\n index 7601807..5716ca5 100644\n --- a/file\n@@ -198,79 +198,79 @@ index 0000000..1fe912c\n EOF\n \n test_expect_success 'stash branch' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit commit file -m first &&\n-\techo bar > file &&\n-\techo bar2 > file2 &&\n+\techo bar >file &&\n+\techo bar2 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n-\techo baz > file &&\n+\techo baz >file &&\n \tgit commit file -m second &&\n \tgit stash branch stashbranch &&\n \ttest refs/heads/stashbranch = $(git symbolic-ref HEAD) &&\n \ttest $(git rev-parse HEAD) = $(git rev-parse master^) &&\n-\tgit diff --cached > output &&\n+\tgit diff --cached >output &&\n \ttest_cmp output expect &&\n-\tgit diff > output &&\n+\tgit diff >output &&\n \ttest_cmp output expect1 &&\n \tgit add file &&\n \tgit commit -m alternate\\ second &&\n-\tgit diff master..stashbranch > output &&\n+\tgit diff master..stashbranch >output &&\n \ttest_cmp output expect2 &&\n \ttest 0 = $(git stash list | wc -l)\n '\n \n test_expect_success 'apply -q is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\tgit stash apply -q > output.out 2>&1 &&\n+\tgit stash apply -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'save -q is quiet' '\n-\tgit stash save --quiet > output.out 2>&1 &&\n+\tgit stash save --quiet >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q is quiet' '\n-\tgit stash pop -q > output.out 2>&1 &&\n+\tgit stash pop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'pop -q --index works and is quiet' '\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit add file &&\n \tgit stash save --quiet &&\n-\tgit stash pop -q --index > output.out 2>&1 &&\n+\tgit stash pop -q --index >output.out 2>&1 &&\n \ttest foo = \"$(git show :file)\" &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'drop -q is quiet' '\n \tgit stash &&\n-\tgit stash drop -q > output.out 2>&1 &&\n+\tgit stash drop -q >output.out 2>&1 &&\n \ttest_must_be_empty output.out\n '\n \n test_expect_success 'stash -k' '\n-\techo bar3 > file &&\n-\techo bar4 > file2 &&\n+\techo bar3 >file &&\n+\techo bar4 >file2 &&\n \tgit add file2 &&\n \tgit stash -k &&\n \ttest bar,bar4 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --no-keep-index' '\n-\techo bar33 > file &&\n-\techo bar44 > file2 &&\n+\techo bar33 >file &&\n+\techo bar44 >file2 &&\n \tgit add file2 &&\n \tgit stash --no-keep-index &&\n \ttest bar,bar2 = $(cat file),$(cat file2)\n '\n \n test_expect_success 'stash --invalid-option' '\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \ttest_must_fail git stash --invalid-option &&\n \ttest_must_fail git stash save --invalid-option &&\n@@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tgit stash branch stash-branch ${STASH_ID} &&\n-\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n+\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n+\tgit branch -D stash-branch\" &&\n \ttest $(git ls-files --modified | wc -l) -eq 1\n '\n \n@@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tgit stash &&\n \ttest_when_finished \"git stash drop\" &&\n-\techo bar >> file &&\n+\techo bar >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \techo \"1\t0\tfile\" >expected &&\n@@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD\" &&\n \tgit reset --hard &&\n-\techo foo >> file &&\n+\techo foo >>file &&\n \tSTASH_ID=$(git stash create) &&\n \tgit reset --hard &&\n \tcat >expected <<-EOF &&\n@@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash drop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n-\techo foo > file &&\n+\techo foo >file &&\n \tgit stash &&\n-\techo bar > file &&\n+\techo bar >file &&\n \tgit stash &&\n \ttest_must_fail git stash pop $(git rev-parse stash@{0}) &&\n \tgit stash pop &&\n@@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n \n test_expect_success 'ref with non-existent reflog' '\n \tgit stash clear &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git rev-parse --quiet --verify does-not-exist &&\n@@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '\n test_expect_success 'invalid ref of the form stash@{n}, n >= N' '\n \tgit stash clear &&\n \ttest_must_fail git stash drop stash@{0} &&\n-\techo bar5 > file &&\n-\techo bar6 > file2 &&\n+\techo bar5 >file &&\n+\techo bar6 >file2 &&\n \tgit add file2 &&\n \tgit stash &&\n \ttest_must_fail git stash drop stash@{1} &&\n@@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu\n \ttest_i18ncmp expect actual\n '\n \n-cat > expect << EOF\n+cat >expect <<EOF\n diff --git a/HEAD b/HEAD\n new file mode 100644\n index 0000000..fe0cbee\n@@ -737,14 +739,14 @@ EOF\n test_expect_success 'stash where working directory contains \"HEAD\" file' '\n \tgit stash clear &&\n \tgit reset --hard &&\n-\techo file-not-a-ref > HEAD &&\n+\techo file-not-a-ref >HEAD &&\n \tgit add HEAD &&\n \ttest_tick &&\n \tgit stash &&\n \tgit diff-files --quiet &&\n \tgit diff-index --cached --quiet HEAD &&\n \ttest \"$(git rev-parse stash^)\" = \"$(git rev-parse HEAD)\" &&\n-\tgit diff stash^..stash > output &&\n+\tgit diff stash^..stash >output &&\n \ttest_cmp output expect\n '\n \n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358892","messageId":"13b53e154222fb28b1100e0ba3e0080fe9bbc2a1.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 05/21] stash: rename test cases to be more descriptive","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:20Z","receivedAt":"2018-09-25T22:34:31Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Rename some test cases' labels to be more descriptive and under 80\ncharacters per line.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3903-stash.sh | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex de6cab1fe7..3114c7bc4c 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n \ttest_cmp expected actual\n '\n \n-test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'drop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n \tgit reset --hard HEAD\n '\n \n-test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '\n+test_expect_success 'pop: fail early if specified stash is not a stash ref' '\n \tgit stash clear &&\n \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n \tgit reset --hard &&\n@@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form \"n\", n >= N' '\n \tgit stash drop\n '\n \n-test_expect_success 'stash branch should not drop the stash if the branch exists' '\n+test_expect_success 'branch: do not drop the stash if the branch exists' '\n \tgit stash clear &&\n \techo foo >file &&\n \tgit add file &&\n@@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash branch should not drop the stash if the apply fails' '\n+test_expect_success 'branch: should not drop the stash if the apply fails' '\n \tgit stash clear &&\n \tgit reset HEAD~1 --hard &&\n \techo foo >file &&\n@@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails'\n \tgit rev-parse stash@{0} --\n '\n \n-test_expect_success 'stash apply shows status same as git status (relative to current directory)' '\n+test_expect_success 'apply: show same status as git status (relative to ./)' '\n \tgit stash clear &&\n \techo 1 >subdir/subfile1 &&\n \techo 2 >subdir/subfile2 &&\n@@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' '\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec shows no changes when there are none' '\n+test_expect_success 'push <pathspec>: show no changes when there are none' '\n \t>foo &&\n \tgit add foo &&\n \tgit commit -m \"tmp\" &&\n@@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no\n \ttest_i18ncmp expect actual\n '\n \n-test_expect_success 'stash push with pathspec not in the repository errors out' '\n+test_expect_success 'push: <pathspec> not in the repository errors out' '\n \t>untracked &&\n \ttest_must_fail git stash push untracked &&\n \ttest_path_is_file untracked\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358893","messageId":"666ffbebb75a2d142023271d10ceb17fb6613a28.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 06/21] stash: add tests for `git stash show` config","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:21Z","receivedAt":"2018-09-25T22:34:32Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commit introduces tests for `git stash show`\nconfig. It tests all the cases where `stash.showStat`\nand `stash.showPatch` are unset or set to true / false.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n t/t3907-stash-show-config.sh | 83 ++++++++++++++++++++++++++++++++++++\n 1 file changed, 83 insertions(+)\n create mode 100755 t/t3907-stash-show-config.sh\n\ndiff --git a/t/t3907-stash-show-config.sh b/t/t3907-stash-show-config.sh\nnew file mode 100755\nindex 0000000000..10914bba7b\n--- /dev/null\n+++ b/t/t3907-stash-show-config.sh\n@@ -0,0 +1,83 @@\n+#!/bin/sh\n+\n+test_description='Test git stash show configuration.'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit file\n+'\n+\n+# takes three parameters:\n+# 1. the stash.showStat value (or \"<unset>\")\n+# 2. the stash.showPatch value (or \"<unset>\")\n+# 3. the diff options of the expected output (or nothing for no output)\n+test_stat_and_patch () {\n+\tif test \"<unset>\" = \"$1\"\n+\tthen\n+\t\ttest_unconfig stash.showStat\n+\telse\n+\t\ttest_config stash.showStat \"$1\"\n+\tfi &&\n+\n+\tif test \"<unset>\" = \"$2\"\n+\tthen\n+\t\ttest_unconfig stash.showPatch\n+\telse\n+\t\ttest_config stash.showPatch \"$2\"\n+\tfi &&\n+\n+\tshift 2 &&\n+\techo 2 >file.t &&\n+\tif test $# != 0\n+\tthen\n+\t\tgit diff \"$@\" >expect\n+\tfi &&\n+\tgit stash &&\n+\tgit stash show >actual &&\n+\n+\tif test $# = 0\n+\tthen\n+\t\ttest_must_be_empty actual\n+\telse\n+\t\ttest_cmp expect actual\n+\tfi\n+}\n+\n+test_expect_success 'showStat unset showPatch unset' '\n+\ttest_stat_and_patch \"<unset>\" \"<unset>\" --stat\n+'\n+\n+test_expect_success 'showStat unset showPatch false' '\n+\ttest_stat_and_patch \"<unset>\" false --stat\n+'\n+\n+test_expect_success 'showStat unset showPatch true' '\n+\ttest_stat_and_patch \"<unset>\" true --stat -p\n+'\n+\n+test_expect_success 'showStat false showPatch unset' '\n+\ttest_stat_and_patch false \"<unset>\"\n+'\n+\n+test_expect_success 'showStat false showPatch false' '\n+\ttest_stat_and_patch false false\n+'\n+\n+test_expect_success 'showStat false showPatch true' '\n+\ttest_stat_and_patch false true -p\n+'\n+\n+test_expect_success 'showStat true showPatch unset' '\n+\ttest_stat_and_patch true \"<unset>\" --stat\n+'\n+\n+test_expect_success 'showStat true showPatch false' '\n+\ttest_stat_and_patch true false --stat\n+'\n+\n+test_expect_success 'showStat true showPatch true' '\n+\ttest_stat_and_patch true true --stat -p\n+'\n+\n+test_done\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358895","messageId":"fc0e991d5612f60b254ae07369dfda0939e256dd.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 07/21] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:23Z","receivedAt":"2018-09-25T22:34:35Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd a builtin helper for performing stash commands. Converting\nall at once proved hard to review, so starting with just apply\nlets conversion get started without the other commands 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n .gitignore              |   1 +\n Makefile                |   1 +\n builtin.h               |   1 +\n builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  78 +------\n git.c                   |   1 +\n 6 files changed, 463 insertions(+), 71 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\ndiff --git a/.gitignore b/.gitignore\nindex ffceea7d59..b59661cb88 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -157,6 +157,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 d03df31c2a..f900c68e69 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1093,6 +1093,7 @@ BUILTIN_OBJS += builtin/shortlog.o\n BUILTIN_OBJS += builtin/show-branch.o\n BUILTIN_OBJS += builtin/show-index.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 99206df4bd..317bc338f7 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -223,6 +223,7 @@ 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_show_index(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 0000000000..7819dae332\n--- /dev/null\n+++ b/builtin/stash--helper.c\n@@ -0,0 +1,452 @@\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+#include \"rerere.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 struct strbuf stash_index_path = STRBUF_INIT;\n+\n+/*\n+ * w_commit is set to the commit containing the working tree\n+ * b_commit is set to the base commit\n+ * i_commit is set to the commit containing the index tree\n+ * u_commit is set to the commit containing the untracked files tree\n+ * w_tree is set to the working tree\n+ * b_tree is set to the base tree\n+ * i_tree is set to the index tree\n+ * u_tree is set to the untracked files tree\n+ */\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 void free_stash_info(struct stash_info *info)\n+{\n+\tstrbuf_release(&info->revision);\n+}\n+\n+static void assert_stash_like(struct stash_info *info, const char *revision)\n+{\n+\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n+\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n+\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n+\t    get_oidf(&info->i_tree, \"%s^2:\", revision)) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n+\t\texit(128);\n+\t}\n+}\n+\n+static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n+{\n+\tint ret;\n+\tchar *end_of_rev;\n+\tchar *expanded_ref;\n+\tconst char *revision;\n+\tconst char *commit = NULL;\n+\tstruct object_id dummy;\n+\tstruct strbuf symbolic = STRBUF_INIT;\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\"),\n+\t\t\t   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) {\n+\t\tif (!ref_exists(ref_stash)) {\n+\t\t\tfree_stash_info(info);\n+\t\t\tfprintf_ln(stderr, _(\"No stash entries found.\"));\n+\t\t\treturn -1;\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+\tif (get_oid(revision, &info->w_commit)) {\n+\t\terror(_(\"%s is not a valid reference\"), revision);\n+\t\tfree_stash_info(info);\n+\t\treturn -1;\n+\t}\n+\n+\tassert_stash_like(info, revision);\n+\n+\tinfo->has_u = !get_oidf(&info->u_tree, \"%s^3:\", revision);\n+\n+\tend_of_rev = strchrnul(revision, '@');\n+\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n+\n+\tret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);\n+\tstrbuf_release(&symbolic);\n+\tswitch (ret) {\n+\tcase 0: /* Not found, but valid ref */\n+\t\tinfo->is_stash_ref = 0;\n+\t\tbreak;\n+\tcase 1:\n+\t\tinfo->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n+\t\tbreak;\n+\tdefault: /* Invalid or ambiguous */\n+\t\tfree_stash_info(info);\n+\t}\n+\n+\tfree(expanded_ref);\n+\treturn !(ret == 0 || ret == 1);\n+}\n+\n+static int reset_tree(struct object_id *i_tree, int update, int reset)\n+{\n+\tint nr_trees = 1;\n+\tstruct unpack_trees_options opts;\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+\t/*\n+\t * Diff-tree would not be very hard to replace with a native function,\n+\t * however it should be done together with apply_cached.\n+\t */\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+\t/*\n+\t * Apply currently only reads either from stdin or a file, thus\n+\t * apply_all_patches would have to be updated to optionally take a\n+\t * buffer.\n+\t */\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(void)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * Reset is overall quite simple, however there is no current public\n+\t * API for resetting.\n+\t */\n+\tcp.git_cmd = 1;\n+\targv_array_push(&cp.args, \"reset\");\n+\n+\treturn run_command(&cp);\n+}\n+\n+static int get_newly_staged(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+\t/*\n+\t * diff-index is very similar to diff-tree above, and should be\n+\t * converted together with update_index.\n+\t */\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"diff-index\", \"--cached\", \"--name-only\",\n+\t\t\t \"--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+\t/*\n+\t * Update-index is very complicated and may need to have a public\n+\t * function exposed in order to remove this forking.\n+\t */\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+\tint res;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * We need to run restore files from a given index, but without\n+\t * affecting the current index, so we use GIT_INDEX_FILE with\n+\t * run_command to fork processes that will not interfere.\n+\t */\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\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp)) {\n+\t\tremove_path(stash_index_path.buf);\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\",\n+\t\t\t stash_index_path.buf);\n+\n+\tres = run_command(&cp);\n+\tremove_path(stash_index_path.buf);\n+\treturn res;\n+}\n+\n+static int do_apply_stash(const char *prefix, struct stash_info *info,\n+\t\t\t  int index, int quiet)\n+{\n+\tint ret;\n+\tint has_index = index;\n+\tstruct merge_options o;\n+\tstruct object_id c_tree;\n+\tstruct object_id index_tree;\n+\tstruct commit *result;\n+\tconst struct object_id *bases[1];\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) ||\n+\t\t    !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();\n+\t\t}\n+\t}\n+\n+\tif (info->has_u && restore_untracked(&info->u_tree))\n+\t\treturn error(_(\"could not restore untracked files from stash\"));\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, 1, bases,\n+\t\t\t\t      &result);\n+\tif (ret) {\n+\t\trerere(0);\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 (get_newly_staged(&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\tif (refresh_cache(REFRESH_QUIET))\n+\t\t\twarning(\"could not refresh index\");\n+\t} else {\n+\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t/*\n+\t\t * Status is quite simple and could be replaced with calls to\n+\t\t * wt_status in the future, but it adds complexities which may\n+\t\t * require more tests.\n+\t\t */\n+\t\tcp.git_cmd = 1;\n+\t\tcp.dir = prefix;\n+\t\targv_array_push(&cp.args, \"status\");\n+\t\trun_command(&cp);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint ret;\n+\tint quiet = 0;\n+\tint index = 0;\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\t N_(\"attempt to recreate the index\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_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, quiet);\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+\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\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n+\n+\tindex_file = get_index_file();\n+\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n+\t\t    (uintmax_t)pid);\n+\n+\tif (argc < 1)\n+\t\tusage_with_options(git_stash_helper_usage, options);\n+\tif (!strcmp(argv[0], \"apply\"))\n+\t\treturn !!apply_stash(argc, argv, prefix);\n+\n+\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n+\t\t      git_stash_helper_usage, options);\n+}\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 94793c1a91..809b1c2d1d 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() {\n@@ -713,7 +648,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\ndiff --git a/git.c b/git.c\nindex c27c38738b..3c0e762d7d 100644\n--- a/git.c\n+++ b/git.c\n@@ -544,6 +544,7 @@ static struct cmd_struct commands[] = {\n \t{ \"show-index\", cmd_show_index },\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 | NO_PARSEOPT },\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358896","messageId":"f7c5def6c24ca754cd937f17f9d16e45aef21678.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 08/21] stash: convert drop and clear to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:24Z","receivedAt":"2018-09-25T22:34:36Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd 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>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 116 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   4 +-\n 2 files changed, 118 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 7819dae332..72472eaeb7 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,7 +12,14 @@\n #include \"rerere.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@@ -21,6 +28,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 struct strbuf stash_index_path = STRBUF_INIT;\n \n@@ -139,6 +151,31 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n \treturn !(ret == 0 || ret == 1);\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,\n+\t\t\t     git_stash_helper_clear_usage,\n+\t\t\t     PARSE_OPT_STOP_AT_NON_OPTION);\n+\n+\tif (argc)\n+\t\treturn error(_(\"git stash 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 \tint nr_trees = 1;\n@@ -424,6 +461,81 @@ 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, int quiet)\n+{\n+\tint ret;\n+\tstruct child_process cp_reflog = CHILD_PROCESS_INIT;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * reflog does not provide a simple function for deleting refs. One will\n+\t * need to be added to avoid implementing too much reflog code here\n+\t */\n+\n+\tcp_reflog.git_cmd = 1;\n+\targv_array_pushl(&cp_reflog.args, \"reflog\", \"delete\", \"--updateref\",\n+\t\t\t \"--rewrite\", NULL);\n+\targv_array_push(&cp_reflog.args, info->revision.buf);\n+\tret = run_command(&cp_reflog);\n+\tif (!ret) {\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"Dropped %s (%s)\"), info->revision.buf,\n+\t\t\t\t  oid_to_hex(&info->w_commit));\n+\t} else {\n+\t\treturn error(_(\"%s: Could not drop stash entry\"),\n+\t\t\t     info->revision.buf);\n+\t}\n+\n+\t/*\n+\t * This could easily be replaced by get_oid, but currently it will throw\n+\t * a fatal error when a reflog is empty, which we can not recover from.\n+\t */\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+\t/* do_clear_stash if we just dropped the last stash entry */\n+\tif (ret)\n+\t\tdo_clear_stash();\n+\n+\treturn 0;\n+}\n+\n+static void assert_stash_ref(struct stash_info *info)\n+{\n+\tif (!info->is_stash_ref) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash reference\"), info->revision.buf);\n+\t\texit(128);\n+\t}\n+}\n+\n+static int drop_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint ret;\n+\tint quiet = 0;\n+\tstruct stash_info info;\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\t     git_stash_helper_drop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\n+\n+\tret = do_drop_stash(prefix, &info, quiet);\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 \tpid_t pid = getpid();\n@@ -446,6 +558,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tusage_with_options(git_stash_helper_usage, options);\n \tif (!strcmp(argv[0], \"apply\"))\n \t\treturn !!apply_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"clear\"))\n+\t\treturn !!clear_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"drop\"))\n+\t\treturn !!drop_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 809b1c2d1d..a99d5dc9e5 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -653,7 +653,7 @@ apply)\n \t;;\n clear)\n \tshift\n-\tclear_stash \"$@\"\n+\tgit stash--helper clear \"$@\"\n \t;;\n create)\n \tshift\n@@ -665,7 +665,7 @@ store)\n \t;;\n drop)\n \tshift\n-\tdrop_stash \"$@\"\n+\tgit stash--helper drop \"$@\"\n \t;;\n pop)\n \tshift\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358897","messageId":"716078e70b01febefd9bd5300ce60de6d5bbd6f4.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 10/21] stash: convert pop to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:26Z","receivedAt":"2018-09-25T22:34:37Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash pop to the helper and delete the pop_stash, drop_stash,\nassert_stash_ref functions from the shell script now that they\nare no longer needed.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 38 ++++++++++++++++++++++++++++++++-\n git-stash.sh            | 47 ++---------------------------------------\n 2 files changed, 39 insertions(+), 46 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 5841bd0e98..c1f78d3275 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -13,7 +13,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 apply [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n \tNULL\n@@ -24,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@@ -542,6 +547,35 @@ 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 ret;\n+\tint index = 0;\n+\tint quiet = 0;\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\t N_(\"attempt to recreate the index\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_pop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\n+\tif ((ret = do_apply_stash(prefix, &info, index, quiet)))\n+\t\tprintf_ln(_(\"The stash entry is kept in case you need it again.\"));\n+\telse\n+\t\tret = do_drop_stash(prefix, &info, quiet);\n+\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 \tint ret;\n@@ -606,6 +640,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"pop\"))\n+\t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n \ndiff --git a/git-stash.sh b/git-stash.sh\nindex 29d9f44255..8f2640fe90 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@@ -655,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.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358898","messageId":"d0ea2c4b94b451d498aff5e8829534cb535a691f.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 11/21] stash: convert list to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:27Z","receivedAt":"2018-09-25T22:34:38Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash list to the helper and delete the list_stash function\nfrom the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++\n git-stash.sh            |  7 +------\n 2 files changed, 32 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex c1f78d3275..9e256690ec 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,7 @@\n #include \"rerere.h\"\n \n static const char * const git_stash_helper_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_list_usage[] = {\n+\tN_(\"git stash--helper list [<options>]\"),\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@@ -614,6 +620,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int list_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_list_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (!ref_exists(ref_stash))\n+\t\treturn 0;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"log\", \"--format=%gd: %gs\", \"-g\",\n+\t\t\t \"--first-parent\", \"-m\", NULL);\n+\targv_array_pushv(&cp.args, argv);\n+\targv_array_push(&cp.args, ref_stash);\n+\targv_array_push(&cp.args, \"--\");\n+\treturn run_command(&cp);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -644,6 +673,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!pop_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"branch\"))\n \t\treturn !!branch_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"list\"))\n+\t\treturn !!list_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 8f2640fe90..6052441aa2 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -382,11 +382,6 @@ have_stash () {\n \tgit rev-parse --verify --quiet $ref_stash >/dev/null\n }\n \n-list_stash () {\n-\thave_stash || return 0\n-\tgit log --format=\"%gd: %gs\" -g --first-parent -m \"$@\" $ref_stash --\n-}\n-\n show_stash () {\n \tALLOW_UNKNOWN_FLAGS=t\n \tassert_stash_like \"$@\"\n@@ -574,7 +569,7 @@ test -n \"$seen_non_option\" || set \"push\" \"$@\"\n case \"$1\" in\n list)\n \tshift\n-\tlist_stash \"$@\"\n+\tgit stash--helper list \"$@\"\n \t;;\n show)\n \tshift\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358899","messageId":"f4df1a271333bcf82c5ea1c5d9604ceb1c0374f8.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 13/21] stash: mention options in `show` synopsis.","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:29Z","receivedAt":"2018-09-25T22:34:40Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Mention in the usage text and in the documentation, that `show`\naccepts any option known to `git diff`.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n Documentation/git-stash.txt | 4 ++--\n builtin/stash--helper.c     | 4 ++--\n 2 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\nindex 7ef8c47911..e31ea7d303 100644\n--- a/Documentation/git-stash.txt\n+++ b/Documentation/git-stash.txt\n@@ -9,7 +9,7 @@ SYNOPSIS\n --------\n [verse]\n 'git stash' list [<options>]\n-'git stash' show [<stash>]\n+'git stash' show [<options>] [<stash>]\n 'git stash' drop [-q|--quiet] [<stash>]\n 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n 'git stash' branch <branchname> [<stash>]\n@@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash\n The command takes options applicable to the 'git log'\n command to control what is shown and how. See linkgit:git-log[1].\n \n-show [<stash>]::\n+show [<options>] [<stash>]::\n \n \tShow the changes recorded in the stash entry as a diff between the\n \tstashed contents and the commit back when the stash entry was first\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 1bc838ee6b..1f02f5f2e9 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -15,7 +15,7 @@\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -29,7 +29,7 @@ static const char * const git_stash_helper_list_usage[] = {\n };\n \n static const char * const git_stash_helper_show_usage[] = {\n-\tN_(\"git stash--helper show [<stash>]\"),\n+\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n \tNULL\n };\n \n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358900","messageId":"7e4450668b56c402729a843b6348d2b1450d14c0.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 14/21] stash: convert store to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:30Z","receivedAt":"2018-09-25T22:34:41Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash store to the helper and delete the store_stash function\nfrom the shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 60 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 43 ++---------------------------\n 2 files changed, 62 insertions(+), 41 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 1f02f5f2e9..b7421b68aa 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -58,6 +58,11 @@ static const char * const git_stash_helper_clear_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_store_usage[] = {\n+\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static struct strbuf stash_index_path = STRBUF_INIT;\n \n@@ -728,6 +733,59 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n \treturn diff_result_code(&rev.diffopt, 0);\n }\n \n+static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,\n+\t\t\t  int quiet)\n+{\n+\tif (!stash_msg)\n+\t\tstash_msg = \"Created via \\\"git stash store\\\".\";\n+\n+\tif (update_ref(stash_msg, ref_stash, w_commit, NULL,\n+\t\t       REF_FORCE_CREATE_REFLOG,\n+\t\t       quiet ? UPDATE_REFS_QUIET_ON_ERR :\n+\t\t       UPDATE_REFS_MSG_ON_ERR)) {\n+\t\tif (!quiet) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot update %s with %s\"),\n+\t\t\t\t   ref_stash, oid_to_hex(w_commit));\n+\t\t}\n+\t\treturn -1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int store_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint quiet = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct object_id obj;\n+\tstruct object_context dummy;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet\")),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\", N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_store_usage,\n+\t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n+\n+\tif (argc != 1) {\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"\\\"git stash store\\\" requires one <commit> argument\"));\n+\t\treturn -1;\n+\t}\n+\n+\tif (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,\n+\t\t\t\t &dummy)) {\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot update %s with %s\"),\n+\t\t\t\t\t     ref_stash, argv[0]);\n+\t\treturn -1;\n+\t}\n+\n+\treturn do_store_stash(&obj, stash_msg, quiet);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -762,6 +820,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!list_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"show\"))\n \t\treturn !!show_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"store\"))\n+\t\treturn !!store_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 0d05cbc1e5..5739c51527 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -191,45 +191,6 @@ create_stash () {\n \tdie \"$(gettext \"Cannot record working tree state\")\"\n }\n \n-store_stash () {\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\tstash_msg=\"$1\"\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t-q|--quiet)\n-\t\t\tquiet=t\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\ttest $# = 1 ||\n-\tdie \"$(eval_gettext \"\\\"$dashless store\\\" requires one <commit> argument\")\"\n-\n-\tw_commit=\"$1\"\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tstash_msg=\"Created via \\\"git stash store\\\".\"\n-\tfi\n-\n-\tgit update-ref --create-reflog -m \"$stash_msg\" $ref_stash $w_commit\n-\tret=$?\n-\ttest $ret != 0 && test -z \"$quiet\" &&\n-\tdie \"$(eval_gettext \"Cannot update \\$ref_stash with \\$w_commit\")\"\n-\treturn $ret\n-}\n-\n push_stash () {\n \tkeep_index=\n \tpatch_mode=\n@@ -308,7 +269,7 @@ push_stash () {\n \t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n \n \tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n-\tstore_stash -m \"$stash_msg\" -q $w_commit ||\n+\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n \tdie \"$(gettext \"Cannot save the current status\")\"\n \tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n \n@@ -468,7 +429,7 @@ create)\n \t;;\n store)\n \tshift\n-\tstore_stash \"$@\"\n+\tgit stash--helper store \"$@\"\n \t;;\n drop)\n \tshift\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358901","messageId":"2aa411b3c677f4a4cf0cba0850a96c1bd0ac2d40.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 15/21] stash: convert create to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:31Z","receivedAt":"2018-09-25T22:34:42Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash create to the helper.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 450 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   2 +-\n 2 files changed, 451 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex b7421b68aa..49b05f2458 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -12,6 +12,9 @@\n #include \"rerere.h\"\n #include \"revision.h\"\n #include \"log-tree.h\"\n+#include \"diffcore.h\"\n+\n+#define INCLUDE_ALL_FILES 2\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n@@ -63,6 +66,11 @@ static const char * const git_stash_helper_store_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_create_usage[] = {\n+\tN_(\"git stash--helper create [<message>]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static struct strbuf stash_index_path = STRBUF_INIT;\n \n@@ -289,6 +297,24 @@ static int reset_head(void)\n \treturn run_command(&cp);\n }\n \n+static void add_diff_to_buf(struct diff_queue_struct *q,\n+\t\t\t    struct diff_options *options,\n+\t\t\t    void *data)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < q->nr; i++) {\n+\t\tstrbuf_addstr(data, q->queue[i]->one->path);\n+\n+\t\t/*\n+\t\t * The reason we add \"0\" at the end of this strbuf\n+\t\t * is because we will pass the output further to\n+\t\t * \"git update-index -z ...\".\n+\t\t */\n+\t\tstrbuf_addch(data, 0);\n+\t}\n+}\n+\n static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)\n {\n \tstruct child_process cp = CHILD_PROCESS_INIT;\n@@ -786,6 +812,428 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n \treturn do_store_stash(&obj, stash_msg, quiet);\n }\n \n+static void add_pathspecs(struct argv_array *args,\n+\t\t\t\t       struct pathspec ps) {\n+\tint i;\n+\n+\tfor (i = 0; i < ps.nr; i++)\n+\t\targv_array_push(args, ps.items[i].match);\n+}\n+\n+/*\n+ * `untracked_files` will be filled with the names of untracked files.\n+ * The return value is:\n+ *\n+ * = 0 if there are not any untracked files\n+ * > 0 if there are untracked files\n+ */\n+static int get_untracked_files(struct pathspec ps, int include_untracked,\n+\t\t\t       struct strbuf *untracked_files)\n+{\n+\tint i;\n+\tint max_len;\n+\tint found = 0;\n+\tchar *seen;\n+\tstruct dir_struct dir;\n+\n+\tmemset(&dir, 0, sizeof(dir));\n+\tif (include_untracked != INCLUDE_ALL_FILES)\n+\t\tsetup_standard_excludes(&dir);\n+\n+\tseen = xcalloc(ps.nr, 1);\n+\n+\tmax_len = fill_directory(&dir, the_repository->index, &ps);\n+\tfor (i = 0; i < dir.nr; i++) {\n+\t\tstruct dir_entry *ent = dir.entries[i];\n+\t\tif (dir_path_match(&the_index, ent, &ps, max_len, seen)) {\n+\t\t\tfound++;\n+\t\t\tstrbuf_addstr(untracked_files, ent->name);\n+\t\t\t/* NUL-terminate: will be fed to update-index -z */\n+\t\t\tstrbuf_addch(untracked_files, 0);\n+\t\t}\n+\t\tfree(ent);\n+\t}\n+\n+\tfree(seen);\n+\tfree(dir.entries);\n+\tfree(dir.ignored);\n+\tclear_directory(&dir);\n+\treturn found;\n+}\n+\n+/*\n+ * The return value of `check_changes()` can be:\n+ *\n+ * < 0 if there was an error\n+ * = 0 if there are no changes.\n+ * > 0 if there are changes.\n+ */\n+static int check_changes(struct pathspec ps, int include_untracked)\n+{\n+\tint result;\n+\tint ret = 0;\n+\tstruct rev_info rev;\n+\tstruct object_id dummy;\n+\tstruct strbuf out = STRBUF_INIT;\n+\n+\t/* No initial commit. */\n+\tif (get_oid(\"HEAD\", &dummy))\n+\t\treturn -1;\n+\n+\tif (read_cache() < 0)\n+\t\treturn -1;\n+\n+\tinit_revisions(&rev, NULL);\n+\trev.prune_data = ps;\n+\n+\trev.diffopt.flags.quick = 1;\n+\trev.diffopt.flags.ignore_submodules = 1;\n+\trev.abbrev = 0;\n+\n+\tadd_head_to_pending(&rev);\n+\tdiff_setup_done(&rev.diffopt);\n+\n+\tresult = run_diff_index(&rev, 1);\n+\tif (diff_result_code(&rev.diffopt, result))\n+\t\treturn 1;\n+\n+\tobject_array_clear(&rev.pending);\n+\tresult = run_diff_files(&rev, 0);\n+\tif (diff_result_code(&rev.diffopt, result))\n+\t\treturn 1;\n+\n+\tif (include_untracked && get_untracked_files(ps, include_untracked,\n+\t\t\t\t\t\t     &out)) {\n+\t\tstrbuf_release(&out);\n+\t\treturn 1;\n+\t}\n+\n+\tstrbuf_release(&out);\n+\treturn 0;\n+}\n+\n+static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n+\t\t\t\tstruct strbuf files)\n+{\n+\tint ret = 0;\n+\tstruct strbuf untracked_msg = STRBUF_INIT;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\n+\tcp_upd_index.git_cmd = 1;\n+\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n+\t\t\t \"--remove\", \"--stdin\", NULL);\n+\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\n+\tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n+\tif (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_write_tree.git_cmd = 1;\n+\targv_array_push(&cp_write_tree.args, \"write-tree\");\n+\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\tget_oid_hex(out.buf, &info->u_tree);\n+\n+\tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n+\t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+done:\n+\tstrbuf_release(&untracked_msg);\n+\tstrbuf_release(&out);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static int stash_patch(struct stash_info *info, struct pathspec ps,\n+\t\t       struct strbuf *out_patch)\n+{\n+\tint ret = 0;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct child_process cp_read_tree = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_add_i = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n+\n+\tremove_path(stash_index_path.buf);\n+\n+\tcp_read_tree.git_cmd = 1;\n+\targv_array_pushl(&cp_read_tree.args, \"read-tree\", \"HEAD\", NULL);\n+\targv_array_pushf(&cp_read_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp_read_tree)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\t/* Find out what the user wants. */\n+\tcp_add_i.git_cmd = 1;\n+\targv_array_pushl(&cp_add_i.args, \"add--interactive\", \"--patch=stash\",\n+\t\t\t \"--\", NULL);\n+\tadd_pathspecs(&cp_add_i.args, ps);\n+\targv_array_pushf(&cp_add_i.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (run_command(&cp_add_i)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\t/* State of the working tree. */\n+\tcp_write_tree.git_cmd = 1;\n+\targv_array_push(&cp_write_tree.args, \"write-tree\");\n+\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tget_oid_hex(out.buf, &info->w_tree);\n+\n+\tcp_diff_tree.git_cmd = 1;\n+\targv_array_pushl(&cp_diff_tree.args, \"diff-tree\", \"-p\", \"HEAD\",\n+\t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n+\tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (!out_patch->len) {\n+\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n+\t\tret = 1;\n+\t}\n+\n+done:\n+\tstrbuf_release(&out);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n+{\n+\tint ret = 0;\n+\tstruct rev_info rev;\n+\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n+\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\tstruct strbuf out = STRBUF_INIT;\n+\tstruct strbuf diff_output = STRBUF_INIT;\n+\n+\tset_alternate_index_output(stash_index_path.buf);\n+\tif (reset_tree(&info->i_tree, 0, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\tset_alternate_index_output(NULL);\n+\n+\tgit_config(git_diff_basic_config, NULL);\n+\tinit_revisions(&rev, NULL);\n+\trev.prune_data = ps;\n+\trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n+\trev.diffopt.format_callback = add_diff_to_buf;\n+\trev.diffopt.format_callback_data = &diff_output;\n+\n+\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit), \"\");\n+\tif (run_diff_index(&rev, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_upd_index.git_cmd = 1;\n+\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n+\t\t\t \"--remove\", \"--stdin\", NULL);\n+\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\n+\tif (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,\n+\t\t\t NULL, 0, NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tcp_write_tree.git_cmd = 1;\n+\targv_array_push(&cp_write_tree.args, \"write-tree\");\n+\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n+\t\t\t stash_index_path.buf);\n+\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tget_oid_hex(out.buf, &info->w_tree);\n+\n+done:\n+\tUNLEAK(rev);\n+\tstrbuf_release(&out);\n+\tobject_array_clear(&rev.pending);\n+\tstrbuf_release(&diff_output);\n+\tremove_path(stash_index_path.buf);\n+\treturn ret;\n+}\n+\n+static int do_create_stash(struct pathspec ps, char **stash_msg,\n+\t\t\t   int include_untracked, int patch_mode,\n+\t\t\t   struct stash_info *info)\n+{\n+\tint ret = 0;\n+\tint flags = 0;\n+\tint untracked_commit_option = 0;\n+\tconst char *head_short_sha1 = NULL;\n+\tconst char *branch_ref = NULL;\n+\tconst char *branch_name = \"(no branch)\";\n+\tstruct commit *head_commit = NULL;\n+\tstruct commit_list *parents = NULL;\n+\tstruct strbuf msg = STRBUF_INIT;\n+\tstruct strbuf commit_tree_label = STRBUF_INIT;\n+\tstruct strbuf untracked_files = STRBUF_INIT;\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n+\tstruct strbuf patch = STRBUF_INIT;\n+\n+\tread_cache_preload(NULL);\n+\trefresh_cache(REFRESH_QUIET);\n+\n+\tif (get_oid(\"HEAD\", &info->b_commit)) {\n+\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n+\t\tret = -1;\n+\t\t*stash_msg = NULL;\n+\t\tgoto done;\n+\t} else {\n+\t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n+\t}\n+\n+\tif (!check_changes(ps, include_untracked)) {\n+\t\tret = 1;\n+\t\t*stash_msg = NULL;\n+\t\tgoto done;\n+\t}\n+\n+\tbranch_ref = resolve_ref_unsafe(\"HEAD\", 0, NULL, &flags);\n+\tif (flags & REF_ISSYMREF)\n+\t\tbranch_name = strrchr(branch_ref, '/') + 1;\n+\thead_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n+\t\t\t\t\t     DEFAULT_ABBREV);\n+\tstrbuf_addf(&msg, \"%s: %s \", branch_name, head_short_sha1);\n+\tpp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);\n+\n+\tstrbuf_addf(&commit_tree_label, \"index on %s\\n\", msg.buf);\n+\tcommit_list_insert(head_commit, &parents);\n+\tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n+\t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n+\t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n+\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n+\t\tret = -1;\n+\t\t*stash_msg = NULL;\n+\t\tgoto done;\n+\t}\n+\n+\tif (include_untracked && get_untracked_files(ps, include_untracked,\n+\t\t\t\t\t\t     &untracked_files)) {\n+\t\tif (save_untracked_files(info, &msg, untracked_files)) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n+\t\t\tret = -1;\n+\t\t\t*stash_msg = NULL;\n+\t\t\tgoto done;\n+\t\t}\n+\t\tuntracked_commit_option = 1;\n+\t}\n+\tif (patch_mode) {\n+\t\tret = stash_patch(info, ps, &patch);\n+\t\t*stash_msg = NULL;\n+\t\tif (ret < 0) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tgoto done;\n+\t\t} else if (ret > 0) {\n+\t\t\tgoto done;\n+\t\t}\n+\t} else {\n+\t\tif (stash_working_tree(info, ps)) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tret = -1;\n+\t\t\t*stash_msg = NULL;\n+\t\t\tgoto done;\n+\t\t}\n+\t}\n+\n+\tif (!*stash_msg || !strlen(*stash_msg))\n+\t\tstrbuf_addf(&stash_msg_buf, \"WIP on %s\", msg.buf);\n+\telse\n+\t\tstrbuf_addf(&stash_msg_buf, \"On %s: %s\", branch_name,\n+\t\t\t    *stash_msg);\n+\t*stash_msg = strbuf_detach(&stash_msg_buf, NULL);\n+\n+\t/*\n+\t * `parents` will be empty after calling `commit_tree()`, so there is\n+\t * no need to call `free_commit_list()`\n+\t */\n+\tparents = NULL;\n+\tif (untracked_commit_option)\n+\t\tcommit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n+\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n+\tcommit_list_insert(head_commit, &parents);\n+\n+\tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n+\t\t\tparents, &info->w_commit, NULL, NULL)) {\n+\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+done:\n+\tstrbuf_release(&commit_tree_label);\n+\tstrbuf_release(&msg);\n+\tstrbuf_release(&untracked_files);\n+\tstrbuf_release(&stash_msg_buf);\n+\treturn ret;\n+}\n+\n+static int create_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint include_untracked = 0;\n+\tint ret = 0;\n+\tchar *stash_msg = NULL;\n+\tstruct stash_info info;\n+\tstruct pathspec ps;\n+\tstruct option options[] = {\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_create_usage,\n+\t\t\t     0);\n+\n+\tmemset(&ps, 0, sizeof(ps));\n+\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);\n+\n+\tif (!ret)\n+\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n+\n+\t/*\n+\t * ret can be 1 if there were no changes. In this case, we should\n+\t * not error out.\n+\t */\n+\tfree((char *) stash_msg);\n+\treturn ret < 0;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -822,6 +1270,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!show_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"store\"))\n \t\treturn !!store_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"create\"))\n+\t\treturn !!create_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 5739c51527..ab06e4ffb8 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -425,7 +425,7 @@ clear)\n \t;;\n create)\n \tshift\n-\tcreate_stash -m \"$*\" && echo \"$w_commit\"\n+\tgit stash--helper create --message \"$*\"\n \t;;\n store)\n \tshift\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358902","messageId":"6636ecc6b2a4da8dcde00d580455ea2911dbb218.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 16/21] stash: convert push to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:32Z","receivedAt":"2018-09-25T22:34:44Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash push to the helper.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 244 +++++++++++++++++++++++++++++++++++++++-\n git-stash.sh            |   6 +-\n 2 files changed, 244 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 49b05f2458..d79233d7ec 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n \tN_(\"git stash--helper clear\"),\n+\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n+\t   \"          [--] [<pathspec>...]]\"),\n \tNULL\n };\n \n@@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_push_usage[] = {\n+\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n+\t   \"          [--] [<pathspec>...]]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static struct strbuf stash_index_path = STRBUF_INIT;\n \n@@ -1088,7 +1098,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \n static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\t\t   int include_untracked, int patch_mode,\n-\t\t\t   struct stash_info *info)\n+\t\t\t   struct stash_info *info, struct strbuf *patch)\n {\n \tint ret = 0;\n \tint flags = 0;\n@@ -1102,7 +1112,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \tstruct strbuf commit_tree_label = STRBUF_INIT;\n \tstruct strbuf untracked_files = STRBUF_INIT;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n-\tstruct strbuf patch = STRBUF_INIT;\n \n \tread_cache_preload(NULL);\n \trefresh_cache(REFRESH_QUIET);\n@@ -1152,7 +1161,7 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\tuntracked_commit_option = 1;\n \t}\n \tif (patch_mode) {\n-\t\tret = stash_patch(info, ps, &patch);\n+\t\tret = stash_patch(info, ps, patch);\n \t\t*stash_msg = NULL;\n \t\tif (ret < 0) {\n \t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n@@ -1221,7 +1230,8 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     0);\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);\n+\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info,\n+\t\t\t      NULL);\n \n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n@@ -1234,6 +1244,230 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \treturn ret < 0;\n }\n \n+static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n+\t\t\t int keep_index, int patch_mode, int include_untracked)\n+{\n+\tint ret = 0;\n+\tstruct stash_info info;\n+\tstruct strbuf patch = STRBUF_INIT;\n+\n+\tif (patch_mode && keep_index == -1)\n+\t\tkeep_index = 1;\n+\n+\tif (patch_mode && include_untracked) {\n+\t\tfprintf_ln(stderr, _(\"Can't use --patch and --include-untracked\"\n+\t\t\t\t     \" or --all at the same time\"));\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tread_cache_preload(NULL);\n+\tif (!include_untracked && ps.nr) {\n+\t\tint i;\n+\t\tchar *ps_matched = xcalloc(ps.nr, 1);\n+\n+\t\tfor (i = 0; i < active_nr; i++)\n+\t\t\tce_path_match(&the_index, active_cache[i], &ps,\n+\t\t\t\t      ps_matched);\n+\n+\t\tif (report_path_error(ps_matched, &ps, NULL)) {\n+\t\t\tfprintf_ln(stderr, _(\"Did you forget to 'git add'?\"));\n+\t\t\tstash_msg = NULL;\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\t\tfree(ps_matched);\n+\t}\n+\n+\tif (refresh_cache(REFRESH_QUIET)) {\n+\t\tstash_msg = NULL;\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (!check_changes(ps, include_untracked)) {\n+\t\tstash_msg = NULL;\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"No local changes to save\"));\n+\t\tgoto done;\n+\t}\n+\n+\tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n+\t\tstash_msg = NULL;\n+\t\tret = -1;\n+\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n+\t\tgoto done;\n+\t}\n+\n+\tif (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,\n+\t\t\t    &info, &patch)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (do_store_stash(&info.w_commit, stash_msg, 1)) {\n+\t\tret = -1;\n+\t\tfprintf_ln(stderr, _(\"Cannot save the current status\"));\n+\t\tgoto done;\n+\t}\n+\n+\tprintf_ln(_(\"Saved working directory and index state %s\"), stash_msg);\n+\n+\tif (!patch_mode) {\n+\t\tif (include_untracked && !ps.nr) {\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"clean\", \"--force\",\n+\t\t\t\t\t \"--quiet\", \"-d\", NULL);\n+\t\t\tif (include_untracked == INCLUDE_ALL_FILES)\n+\t\t\t\targv_array_push(&cp.args, \"-x\");\n+\t\t\tif (run_command(&cp)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\t\tif (ps.nr) {\n+\t\t\tstruct child_process cp_add = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp_diff = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp_apply = CHILD_PROCESS_INIT;\n+\t\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\t\tcp_add.git_cmd = 1;\n+\t\t\targv_array_push(&cp_add.args, \"add\");\n+\t\t\tif (!include_untracked)\n+\t\t\t\targv_array_push(&cp_add.args, \"-u\");\n+\t\t\tif (include_untracked == INCLUDE_ALL_FILES)\n+\t\t\t\targv_array_push(&cp_add.args, \"--force\");\n+\t\t\targv_array_push(&cp_add.args, \"--\");\n+\t\t\tadd_pathspecs(&cp_add.args, ps);\n+\t\t\tif (run_command(&cp_add)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp_diff.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp_diff.args, \"diff-index\", \"-p\",\n+\t\t\t\t\t \"--cached\", \"--binary\", \"HEAD\", \"--\",\n+\t\t\t\t\t NULL);\n+\t\t\tadd_pathspecs(&cp_diff.args, ps);\n+\t\t\tif (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp_apply.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp_apply.args, \"apply\", \"--index\",\n+\t\t\t\t\t \"-R\", NULL);\n+\t\t\tif (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,\n+\t\t\t\t\t NULL, 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t} else {\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"reset\", \"--hard\", \"-q\",\n+\t\t\t\t\t NULL);\n+\t\t\tif (run_command(&cp)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (keep_index == 1 && !is_null_oid(&info.i_tree)) {\n+\t\t\tstruct child_process cp_ls = CHILD_PROCESS_INIT;\n+\t\t\tstruct child_process cp_checkout = CHILD_PROCESS_INIT;\n+\t\t\tstruct strbuf out = STRBUF_INIT;\n+\n+\t\t\tif (reset_tree(&info.i_tree, 0, 1)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp_ls.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp_ls.args, \"ls-files\", \"-z\",\n+\t\t\t\t\t \"--modified\", \"--\", NULL);\n+\n+\t\t\tadd_pathspecs(&cp_ls.args, ps);\n+\t\t\tif (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\n+\t\t\tcp_checkout.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp_checkout.args, \"checkout-index\",\n+\t\t\t\t\t \"-z\", \"--force\", \"--stdin\", NULL);\n+\t\t\tif (pipe_command(&cp_checkout, out.buf, out.len, NULL,\n+\t\t\t\t\t 0, NULL, 0)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\t\tgoto done;\n+\t} else {\n+\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\tcp.git_cmd = 1;\n+\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n+\n+\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n+\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\n+\t\tif (keep_index < 1) {\n+\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t\t\tcp.git_cmd = 1;\n+\t\t\targv_array_pushl(&cp.args, \"reset\", \"-q\", \"--\", NULL);\n+\t\t\tadd_pathspecs(&cp.args, ps);\n+\t\t\tif (run_command(&cp)) {\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t}\n+\t\tgoto done;\n+\t}\n+\n+done:\n+\tfree(stash_msg);\n+\treturn ret;\n+}\n+\n+static int push_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tchar *stash_msg = NULL;\n+\tstruct pathspec ps;\n+\tstruct option options[] = {\n+\t\tOPT_BOOL('k', \"keep-index\", &keep_index,\n+\t\t\t N_(\"keep index\")),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\t N_(\"stash in patch mode\")),\n+\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t   N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_push_usage,\n+\t\t\t     0);\n+\n+\tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n+\treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n+\t\t\t     include_untracked);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1272,6 +1506,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!store_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"create\"))\n \t\treturn !!create_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"push\"))\n+\t\treturn !!push_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex ab06e4ffb8..c3146f62ab 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -412,7 +412,8 @@ save)\n \t;;\n push)\n \tshift\n-\tpush_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper push \"$@\"\n \t;;\n apply)\n \tshift\n@@ -448,7 +449,8 @@ branch)\n *)\n \tcase $# in\n \t0)\n-\t\tpush_stash &&\n+\t\tcd \"$START_DIR\"\n+\t\tgit stash--helper push &&\n \t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n \t\t;;\n \t*)\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358903","messageId":"a09ae6260efec6a1d608a0df96b6e3680b5a15a1.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 17/21] stash: make push -q quiet","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:33Z","receivedAt":"2018-09-25T22:34:44Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"There is a change in behaviour with this commit. When there was\nno initial commit, the shell version of stash would still display\na message. This commit makes `push` to not display any message if\n`--quiet` or `-q` is specified.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 45 ++++++++++++++++++++++++++---------------\n t/t3903-stash.sh        | 23 +++++++++++++++++++++\n 2 files changed, 52 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex d79233d7ec..73bb22dc94 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -967,7 +967,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n }\n \n static int stash_patch(struct stash_info *info, struct pathspec ps,\n-\t\t       struct strbuf *out_patch)\n+\t\t       struct strbuf *out_patch, int quiet)\n {\n \tint ret = 0;\n \tstruct strbuf out = STRBUF_INIT;\n@@ -1020,7 +1020,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,\n \t}\n \n \tif (!out_patch->len) {\n-\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n \t\tret = 1;\n \t}\n \n@@ -1098,7 +1099,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \n static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\t\t   int include_untracked, int patch_mode,\n-\t\t\t   struct stash_info *info, struct strbuf *patch)\n+\t\t\t   struct stash_info *info, struct strbuf *patch,\n+\t\t\t   int quiet)\n {\n \tint ret = 0;\n \tint flags = 0;\n@@ -1117,7 +1119,8 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \trefresh_cache(REFRESH_QUIET);\n \n \tif (get_oid(\"HEAD\", &info->b_commit)) {\n-\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n \t\tret = -1;\n \t\t*stash_msg = NULL;\n \t\tgoto done;\n@@ -1144,7 +1147,8 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n \t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n \t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n-\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n \t\tret = -1;\n \t\t*stash_msg = NULL;\n \t\tgoto done;\n@@ -1153,7 +1157,8 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \tif (include_untracked && get_untracked_files(ps, include_untracked,\n \t\t\t\t\t\t     &untracked_files)) {\n \t\tif (save_untracked_files(info, &msg, untracked_files)) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n \t\t\tret = -1;\n \t\t\t*stash_msg = NULL;\n \t\t\tgoto done;\n@@ -1161,17 +1166,19 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\tuntracked_commit_option = 1;\n \t}\n \tif (patch_mode) {\n-\t\tret = stash_patch(info, ps, patch);\n+\t\tret = stash_patch(info, ps, patch, quiet);\n \t\t*stash_msg = NULL;\n \t\tif (ret < 0) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n \t\t\tgoto done;\n \t\t} else if (ret > 0) {\n \t\t\tgoto done;\n \t\t}\n \t} else {\n \t\tif (stash_working_tree(info, ps)) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n \t\t\tret = -1;\n \t\t\t*stash_msg = NULL;\n \t\t\tgoto done;\n@@ -1197,7 +1204,8 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \n \tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n \t\t\tparents, &info->w_commit, NULL, NULL)) {\n-\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1231,7 +1239,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \n \tmemset(&ps, 0, sizeof(ps));\n \tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info,\n-\t\t\t      NULL);\n+\t\t\t      NULL, 0);\n \n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n@@ -1295,23 +1303,27 @@ static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n \tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n \t\tstash_msg = NULL;\n \t\tret = -1;\n-\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n \t\tgoto done;\n \t}\n \n \tif (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,\n-\t\t\t    &info, &patch)) {\n+\t\t\t    &info, &patch, quiet)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n \tif (do_store_stash(&info.w_commit, stash_msg, 1)) {\n \t\tret = -1;\n-\t\tfprintf_ln(stderr, _(\"Cannot save the current status\"));\n+\t\tif (!quiet)\n+\t\t\tfprintf_ln(stderr, _(\"Cannot save the current status\"));\n \t\tgoto done;\n \t}\n \n-\tprintf_ln(_(\"Saved working directory and index state %s\"), stash_msg);\n+\tif (!quiet)\n+\t\tprintf_ln(_(\"Saved working directory and index state %s\"),\n+\t\t\t  stash_msg);\n \n \tif (!patch_mode) {\n \t\tif (include_untracked && !ps.nr) {\n@@ -1412,7 +1424,8 @@ static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n \t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n \n \t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n \t\t\tret = -1;\n \t\t\tgoto done;\n \t\t}\ndiff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\nindex 3114c7bc4c..ed4611d3d8 100755\n--- a/t/t3903-stash.sh\n+++ b/t/t3903-stash.sh\n@@ -1064,6 +1064,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '\n \ttest_path_is_file untracked\n '\n \n+test_expect_success 'push: -q is quiet with changes' '\n+\t>foo &&\n+\tgit add foo &&\n+\tgit stash push -q >output 2>&1 &&\n+\ttest_must_be_empty output\n+'\n+\n+test_expect_success 'push: -q is quiet with no changes' '\n+\tgit stash push -q >output 2>&1 &&\n+\ttest_must_be_empty output\n+'\n+\n+test_expect_success 'push: -q is quiet even if there is no initial commit' '\n+\tgit init foo_dir &&\n+\ttest_when_finished rm -rf foo_dir &&\n+\t(\n+\t\tcd foo_dir &&\n+\t\t>bar &&\n+\t\ttest_must_fail git stash push -q >output 2>&1 &&\n+\t\ttest_must_be_empty output\n+\t)\n+'\n+\n test_expect_success 'untracked files are left in place when -u is not given' '\n \t>file &&\n \tgit add file &&\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358904","messageId":"c695d8f53e494c7811efcdac00421cfa7dd2fc5d.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 18/21] stash: convert save to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:34Z","receivedAt":"2018-09-25T22:34:46Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash save to the helper and delete functions which are no\nlonger needed (`show_help()`, `save_stash()`, `push_stash()`,\n`create_stash()`, `clear_stash()`, `untracked_files()` and\n`no_changes()`).\n\nThe `-m` option is no longer supported as it might not make\nsense to have two ways of passing a message. Even if this is\na change in behaviour, the documentation remains the same\nbecause the `-m` parameter was omitted before.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c |  50 +++++++\n git-stash.sh            | 311 +---------------------------------------\n 2 files changed, 52 insertions(+), 309 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 73bb22dc94..96689a00e9 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -26,6 +26,8 @@ static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n+\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n \n@@ -81,6 +83,12 @@ static const char * const git_stash_helper_push_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_save_usage[] = {\n+\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n+\tNULL\n+};\n+\n static const char *ref_stash = \"refs/stash\";\n static struct strbuf stash_index_path = STRBUF_INIT;\n \n@@ -1481,6 +1489,46 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     include_untracked);\n }\n \n+static int save_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tint ret = 0;\n+\tchar *stash_msg = NULL;\n+\tstruct pathspec ps;\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_BOOL('k', \"keep-index\", &keep_index,\n+\t\t\t N_(\"keep index\")),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\t N_(\"stash in patch mode\")),\n+\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\",\n+\t\t\t   N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_save_usage,\n+\t\t\t     PARSE_OPT_KEEP_DASHDASH);\n+\n+\tif (argc)\n+\t\tstash_msg = (char*) strbuf_join_argv(&buf, argc, argv, ' ');\n+\n+\tmemset(&ps, 0, sizeof(ps));\n+\tret = do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n+\t\t\t    include_untracked);\n+\n+\tstrbuf_release(&buf);\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1521,6 +1569,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!create_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"push\"))\n \t\treturn !!push_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"save\"))\n+\t\treturn !!save_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex c3146f62ab..695f1feba3 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -36,314 +36,6 @@ else\n        reset_color=\n fi\n \n-no_changes () {\n-\tgit diff-index --quiet --cached HEAD --ignore-submodules -- \"$@\" &&\n-\tgit diff-files --quiet --ignore-submodules -- \"$@\" &&\n-\t(test -z \"$untracked\" || test -z \"$(untracked_files \"$@\")\")\n-}\n-\n-untracked_files () {\n-\tif test \"$1\" = \"-z\"\n-\tthen\n-\t\tshift\n-\t\tz=-z\n-\telse\n-\t\tz=\n-\tfi\n-\texcl_opt=--exclude-standard\n-\ttest \"$untracked\" = \"all\" && excl_opt=\n-\tgit ls-files -o $z $excl_opt -- \"$@\"\n-}\n-\n-clear_stash () {\n-\tif test $# != 0\n-\tthen\n-\t\tdie \"$(gettext \"git stash clear with parameters is unimplemented\")\"\n-\tfi\n-\tif current=$(git rev-parse --verify --quiet $ref_stash)\n-\tthen\n-\t\tgit update-ref -d $ref_stash $current\n-\tfi\n-}\n-\n-create_stash () {\n-\tstash_msg=\n-\tuntracked=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\tstash_msg=${1?\"BUG: create_stash () -m requires an argument\"}\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t-u|--include-untracked)\n-\t\t\tshift\n-\t\t\tuntracked=${1?\"BUG: create_stash () -u requires an argument\"}\n-\t\t\t;;\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\tgit update-index -q --refresh\n-\tif no_changes \"$@\"\n-\tthen\n-\t\texit 0\n-\tfi\n-\n-\t# state of the base commit\n-\tif b_commit=$(git rev-parse --verify HEAD)\n-\tthen\n-\t\thead=$(git rev-list --oneline -n 1 HEAD --)\n-\telse\n-\t\tdie \"$(gettext \"You do not have the initial commit yet\")\"\n-\tfi\n-\n-\tif branch=$(git symbolic-ref -q HEAD)\n-\tthen\n-\t\tbranch=${branch#refs/heads/}\n-\telse\n-\t\tbranch='(no branch)'\n-\tfi\n-\tmsg=$(printf '%s: %s' \"$branch\" \"$head\")\n-\n-\t# state of the index\n-\ti_tree=$(git write-tree) &&\n-\ti_commit=$(printf 'index on %s\\n' \"$msg\" |\n-\t\tgit commit-tree $i_tree -p $b_commit) ||\n-\t\tdie \"$(gettext \"Cannot save the current index state\")\"\n-\n-\tif test -n \"$untracked\"\n-\tthen\n-\t\t# Untracked files are stored by themselves in a parentless commit, for\n-\t\t# ease of unpacking later.\n-\t\tu_commit=$(\n-\t\t\tuntracked_files -z \"$@\" | (\n-\t\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n-\t\t\t\texport GIT_INDEX_FILE &&\n-\t\t\t\trm -f \"$TMPindex\" &&\n-\t\t\t\tgit update-index -z --add --remove --stdin &&\n-\t\t\t\tu_tree=$(git write-tree) &&\n-\t\t\t\tprintf 'untracked files on %s\\n' \"$msg\" | git commit-tree $u_tree  &&\n-\t\t\t\trm -f \"$TMPindex\"\n-\t\t) ) || die \"$(gettext \"Cannot save the untracked files\")\"\n-\n-\t\tuntracked_commit_option=\"-p $u_commit\";\n-\telse\n-\t\tuntracked_commit_option=\n-\tfi\n-\n-\tif test -z \"$patch_mode\"\n-\tthen\n-\n-\t\t# state of the working tree\n-\t\tw_tree=$( (\n-\t\t\tgit read-tree --index-output=\"$TMPindex\" -m $i_tree &&\n-\t\t\tGIT_INDEX_FILE=\"$TMPindex\" &&\n-\t\t\texport GIT_INDEX_FILE &&\n-\t\t\tgit diff-index --name-only -z HEAD -- \"$@\" >\"$TMP-stagenames\" &&\n-\t\t\tgit update-index -z --add --remove --stdin <\"$TMP-stagenames\" &&\n-\t\t\tgit write-tree &&\n-\t\t\trm -f \"$TMPindex\"\n-\t\t) ) ||\n-\t\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n-\n-\telse\n-\n-\t\trm -f \"$TMP-index\" &&\n-\t\tGIT_INDEX_FILE=\"$TMP-index\" git read-tree HEAD &&\n-\n-\t\t# find out what the user wants\n-\t\tGIT_INDEX_FILE=\"$TMP-index\" \\\n-\t\t\tgit add--interactive --patch=stash -- \"$@\" &&\n-\n-\t\t# state of the working tree\n-\t\tw_tree=$(GIT_INDEX_FILE=\"$TMP-index\" git write-tree) ||\n-\t\tdie \"$(gettext \"Cannot save the current worktree state\")\"\n-\n-\t\tgit diff-tree -p HEAD $w_tree -- >\"$TMP-patch\" &&\n-\t\ttest -s \"$TMP-patch\" ||\n-\t\tdie \"$(gettext \"No changes selected\")\"\n-\n-\t\trm -f \"$TMP-index\" ||\n-\t\tdie \"$(gettext \"Cannot remove temporary index (can't happen)\")\"\n-\n-\tfi\n-\n-\t# create the stash\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tstash_msg=$(printf 'WIP on %s' \"$msg\")\n-\telse\n-\t\tstash_msg=$(printf 'On %s: %s' \"$branch\" \"$stash_msg\")\n-\tfi\n-\tw_commit=$(printf '%s\\n' \"$stash_msg\" |\n-\tgit commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||\n-\tdie \"$(gettext \"Cannot record working tree state\")\"\n-}\n-\n-push_stash () {\n-\tkeep_index=\n-\tpatch_mode=\n-\tuntracked=\n-\tstash_msg=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t-k|--keep-index)\n-\t\t\tkeep_index=t\n-\t\t\t;;\n-\t\t--no-keep-index)\n-\t\t\tkeep_index=n\n-\t\t\t;;\n-\t\t-p|--patch)\n-\t\t\tpatch_mode=t\n-\t\t\t# only default to keep if we don't already have an override\n-\t\t\ttest -z \"$keep_index\" && keep_index=t\n-\t\t\t;;\n-\t\t-q|--quiet)\n-\t\t\tGIT_QUIET=t\n-\t\t\t;;\n-\t\t-u|--include-untracked)\n-\t\t\tuntracked=untracked\n-\t\t\t;;\n-\t\t-a|--all)\n-\t\t\tuntracked=all\n-\t\t\t;;\n-\t\t-m|--message)\n-\t\t\tshift\n-\t\t\ttest -z ${1+x} && usage\n-\t\t\tstash_msg=$1\n-\t\t\t;;\n-\t\t-m*)\n-\t\t\tstash_msg=${1#-m}\n-\t\t\t;;\n-\t\t--message=*)\n-\t\t\tstash_msg=${1#--message=}\n-\t\t\t;;\n-\t\t--help)\n-\t\t\tshow_help\n-\t\t\t;;\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\t-*)\n-\t\t\toption=\"$1\"\n-\t\t\teval_gettextln \"error: unknown option for 'stash push': \\$option\"\n-\t\t\tusage\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\teval \"set $(git rev-parse --sq --prefix \"$prefix\" -- \"$@\")\"\n-\n-\tif test -n \"$patch_mode\" && test -n \"$untracked\"\n-\tthen\n-\t\tdie \"$(gettext \"Can't use --patch and --include-untracked or --all at the same time\")\"\n-\tfi\n-\n-\ttest -n \"$untracked\" || git ls-files --error-unmatch -- \"$@\" >/dev/null || exit 1\n-\n-\tgit update-index -q --refresh\n-\tif no_changes \"$@\"\n-\tthen\n-\t\tsay \"$(gettext \"No local changes to save\")\"\n-\t\texit 0\n-\tfi\n-\n-\tgit reflog exists $ref_stash ||\n-\t\tclear_stash || die \"$(gettext \"Cannot initialize stash\")\"\n-\n-\tcreate_stash -m \"$stash_msg\" -u \"$untracked\" -- \"$@\"\n-\tgit stash--helper store -m \"$stash_msg\" -q $w_commit ||\n-\tdie \"$(gettext \"Cannot save the current status\")\"\n-\tsay \"$(eval_gettext \"Saved working directory and index state \\$stash_msg\")\"\n-\n-\tif test -z \"$patch_mode\"\n-\tthen\n-\t\ttest \"$untracked\" = \"all\" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=\n-\t\tif test -n \"$untracked\" && test $# = 0\n-\t\tthen\n-\t\t\tgit clean --force --quiet -d $CLEAN_X_OPTION\n-\t\tfi\n-\n-\t\tif test $# != 0\n-\t\tthen\n-\t\t\ttest -z \"$untracked\" && UPDATE_OPTION=\"-u\" || UPDATE_OPTION=\n-\t\t\ttest \"$untracked\" = \"all\" && FORCE_OPTION=\"--force\" || FORCE_OPTION=\n-\t\t\tgit add $UPDATE_OPTION $FORCE_OPTION -- \"$@\"\n-\t\t\tgit diff-index -p --cached --binary HEAD -- \"$@\" |\n-\t\t\tgit apply --index -R\n-\t\telse\n-\t\t\tgit reset --hard -q\n-\t\tfi\n-\n-\t\tif test \"$keep_index\" = \"t\" && test -n \"$i_tree\"\n-\t\tthen\n-\t\t\tgit read-tree --reset $i_tree\n-\t\t\tgit ls-files -z --modified -- \"$@\" |\n-\t\t\tgit checkout-index -z --force --stdin\n-\t\tfi\n-\telse\n-\t\tgit apply -R < \"$TMP-patch\" ||\n-\t\tdie \"$(gettext \"Cannot remove worktree changes\")\"\n-\n-\t\tif test \"$keep_index\" != \"t\"\n-\t\tthen\n-\t\t\tgit reset -q -- \"$@\"\n-\t\tfi\n-\tfi\n-}\n-\n-save_stash () {\n-\tpush_options=\n-\twhile test $# != 0\n-\tdo\n-\t\tcase \"$1\" in\n-\t\t--)\n-\t\t\tshift\n-\t\t\tbreak\n-\t\t\t;;\n-\t\t-*)\n-\t\t\t# pass all options through to push_stash\n-\t\t\tpush_options=\"$push_options $1\"\n-\t\t\t;;\n-\t\t*)\n-\t\t\tbreak\n-\t\t\t;;\n-\t\tesac\n-\t\tshift\n-\tdone\n-\n-\tstash_msg=\"$*\"\n-\n-\tif test -z \"$stash_msg\"\n-\tthen\n-\t\tpush_stash $push_options\n-\telse\n-\t\tpush_stash $push_options -m \"$stash_msg\"\n-\tfi\n-}\n-\n-show_help () {\n-\texec git help stash\n-\texit 1\n-}\n-\n #\n # Parses the remaining options looking for flags and\n # at most one revision defaulting to ${ref_stash}@{0}\n@@ -408,7 +100,8 @@ show)\n \t;;\n save)\n \tshift\n-\tsave_stash \"$@\"\n+\tcd \"$START_DIR\"\n+\tgit stash--helper save \"$@\"\n \t;;\n push)\n \tshift\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358905","messageId":"2e6b0734c0185053733c0aa47d4a618cf054c67e.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 19/21] stash: convert `stash--helper.c` into `stash.c`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:35Z","receivedAt":"2018-09-25T22:34:47Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"The old shell script `git-stash.sh`  was removed and replaced\nentirely by `builtin/stash.c`. In order to do that, `create` and\n`push` were adapted to work without `stash.sh`. For example, before\nthis commit, `git stash create` called `git stash--helper create\n--message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\nsome of these changes wouldn't have been necessary.\n\nThis commit also removes the word `helper` since now stash is\ncalled directly and not by a shell script.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n .gitignore                           |   1 -\n Makefile                             |   3 +-\n builtin.h                            |   2 +-\n builtin/{stash--helper.c => stash.c} | 162 ++++++++++++++++-----------\n git-stash.sh                         | 153 -------------------------\n git.c                                |   2 +-\n 6 files changed, 98 insertions(+), 225 deletions(-)\n rename builtin/{stash--helper.c => stash.c} (90%)\n delete mode 100755 git-stash.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex b59661cb88..ffceea7d59 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -157,7 +157,6 @@\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 f900c68e69..ac1787d113 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -617,7 +617,6 @@ SCRIPT_SH += git-quiltimport.sh\n SCRIPT_SH += git-rebase.sh\n SCRIPT_SH += git-remote-testgit.sh\n SCRIPT_SH += git-request-pull.sh\n-SCRIPT_SH += git-stash.sh\n SCRIPT_SH += git-submodule.sh\n SCRIPT_SH += git-web--browse.sh\n \n@@ -1093,7 +1092,7 @@ BUILTIN_OBJS += builtin/shortlog.o\n BUILTIN_OBJS += builtin/show-branch.o\n BUILTIN_OBJS += builtin/show-index.o\n BUILTIN_OBJS += builtin/show-ref.o\n-BUILTIN_OBJS += builtin/stash--helper.o\n+BUILTIN_OBJS += builtin/stash.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 317bc338f7..e60f0fc58f 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -223,7 +223,7 @@ 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_show_index(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_stash(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.c\nsimilarity index 90%\nrename from builtin/stash--helper.c\nrename to builtin/stash.c\nindex 96689a00e9..fc4a2050b1 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash.c\n@@ -16,75 +16,70 @@\n \n #define INCLUDE_ALL_FILES 2\n \n-static const char * const git_stash_helper_usage[] = {\n-\tN_(\"git stash--helper list [<options>]\"),\n-\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n-\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n-\tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n-\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n-\tN_(\"git stash--helper clear\"),\n-\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_usage[] = {\n+\tN_(\"git stash list [<options>]\"),\n+\tN_(\"git stash show [<options>] [<stash>]\"),\n+\tN_(\"git stash drop [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n+\tN_(\"git stash branch <branchname> [<stash>]\"),\n+\tN_(\"git stash clear\"),\n+\tN_(\"git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n-\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+\tN_(\"git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_list_usage[] = {\n-\tN_(\"git stash--helper list [<options>]\"),\n+static const char * const git_stash_list_usage[] = {\n+\tN_(\"git stash list [<options>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_show_usage[] = {\n-\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n+static const char * const git_stash_show_usage[] = {\n+\tN_(\"git stash show [<options>] [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_drop_usage[] = {\n-\tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n+static const char * const git_stash_drop_usage[] = {\n+\tN_(\"git stash drop [-q|--quiet] [<stash>]\"),\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+static const char * const git_stash_pop_usage[] = {\n+\tN_(\"git stash 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+static const char * const git_stash_apply_usage[] = {\n+\tN_(\"git stash apply [--index] [-q|--quiet] [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_branch_usage[] = {\n-\tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n+static const char * const git_stash_branch_usage[] = {\n+\tN_(\"git stash branch <branchname> [<stash>]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_clear_usage[] = {\n-\tN_(\"git stash--helper clear\"),\n+static const char * const git_stash_clear_usage[] = {\n+\tN_(\"git stash clear\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_store_usage[] = {\n-\tN_(\"git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>\"),\n+static const char * const git_stash_store_usage[] = {\n+\tN_(\"git stash store [-m|--message <message>] [-q|--quiet] <commit>\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_create_usage[] = {\n-\tN_(\"git stash--helper create [<message>]\"),\n-\tNULL\n-};\n-\n-static const char * const git_stash_helper_push_usage[] = {\n-\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_push_usage[] = {\n+\tN_(\"git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n \t   \"          [--] [<pathspec>...]]\"),\n \tNULL\n };\n \n-static const char * const git_stash_helper_save_usage[] = {\n-\tN_(\"git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n+static const char * const git_stash_save_usage[] = {\n+\tN_(\"git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n \t   \"          [-u|--include-untracked] [-a|--all] [<message>]\"),\n \tNULL\n };\n@@ -223,7 +218,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_clear_usage,\n+\t\t\t     git_stash_clear_usage,\n \t\t\t     PARSE_OPT_STOP_AT_NON_OPTION);\n \n \tif (argc)\n@@ -525,7 +520,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_apply_usage, 0);\n+\t\t\t     git_stash_apply_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -598,7 +593,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_drop_usage, 0);\n+\t\t\t     git_stash_drop_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -624,7 +619,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_pop_usage, 0);\n+\t\t\t     git_stash_pop_usage, 0);\n \n \tif (get_stash_info(&info, argc, argv))\n \t\treturn -1;\n@@ -650,7 +645,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_branch_usage, 0);\n+\t\t\t     git_stash_branch_usage, 0);\n \n \tif (!argc) {\n \t\tfprintf_ln(stderr, \"No branch name specified\");\n@@ -685,7 +680,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_list_usage,\n+\t\t\t     git_stash_list_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n \n \tif (!ref_exists(ref_stash))\n@@ -765,7 +760,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)\n \targc = setup_revisions(argc, argv, &rev, NULL);\n \tif (argc > 1) {\n \t\tfree_stash_info(&info);\n-\t\tusage_with_options(git_stash_helper_show_usage, options);\n+\t\tusage_with_options(git_stash_show_usage, options);\n \t}\n \n \trev.diffopt.flags.recursive = 1;\n@@ -810,7 +805,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_store_usage,\n+\t\t\t     git_stash_store_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN);\n \n \tif (argc != 1) {\n@@ -1228,35 +1223,28 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \n static int create_stash(int argc, const char **argv, const char *prefix)\n {\n-\tint include_untracked = 0;\n \tint ret = 0;\n \tchar *stash_msg = NULL;\n \tstruct stash_info info;\n \tstruct pathspec ps;\n-\tstruct option options[] = {\n-\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n-\t\t\t N_(\"include untracked files in stash\")),\n-\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n-\t\t\t N_(\"stash message\")),\n-\t\tOPT_END()\n-\t};\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n \n-\targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_create_usage,\n-\t\t\t     0);\n+\t/* Startinf with argv[1], since argv[0] is \"create\" */\n+\tstash_msg = (char*) strbuf_join_argv(&stash_msg_buf, argc - 1,\n+\t\t\t\t\t     ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info,\n-\t\t\t      NULL, 0);\n+\tret = do_create_stash(ps, &stash_msg, 0, 0, &info, NULL, 0);\n \n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n+\tstrbuf_release(&stash_msg_buf);\n+\n \t/*\n \t * ret can be 1 if there were no changes. In this case, we should\n \t * not error out.\n \t */\n-\tfree((char *) stash_msg);\n \treturn ret < 0;\n }\n \n@@ -1480,9 +1468,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_push_usage,\n-\t\t\t     0);\n+\tif (argc)\n+\t\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t\t     git_stash_push_usage,\n+\t\t\t\t     0);\n \n \tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n \treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n@@ -1515,7 +1504,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_save_usage,\n+\t\t\t     git_stash_save_usage,\n \t\t\t     PARSE_OPT_KEEP_DASHDASH);\n \n \tif (argc)\n@@ -1529,10 +1518,12 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n-int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n+int cmd_stash(int argc, const char **argv, const char *prefix)\n {\n+\tint i = -1;\n \tpid_t pid = getpid();\n \tconst char *index_file;\n+\tstruct argv_array args = ARGV_ARRAY_INIT;\n \n \tstruct option options[] = {\n \t\tOPT_END()\n@@ -1540,16 +1531,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \n \tgit_config(git_default_config, NULL);\n \n-\targc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,\n+\targc = parse_options(argc, argv, prefix, options, git_stash_usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n \n \tindex_file = get_index_file();\n \tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n \t\t    (uintmax_t)pid);\n \n-\tif (argc < 1)\n-\t\tusage_with_options(git_stash_helper_usage, options);\n-\tif (!strcmp(argv[0], \"apply\"))\n+\tif (argc == 0)\n+\t\treturn !!push_stash(0, NULL, prefix);\n+\telse if (!strcmp(argv[0], \"apply\"))\n \t\treturn !!apply_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"clear\"))\n \t\treturn !!clear_stash(argc, argv, prefix);\n@@ -1571,7 +1562,44 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!push_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"save\"))\n \t\treturn !!save_stash(argc, argv, prefix);\n+\telse if (*argv[0] != '-')\n+\t\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n+\t\t\t      git_stash_usage, options);\n+\n+\tif (strcmp(argv[0], \"-p\")) {\n+\t\twhile (++i < argc && strcmp(argv[i], \"--\")) {\n+\t\t\t/*\n+\t\t\t * `akpqu` is a string which contains all short options,\n+\t\t\t * except `-m` which is verified separately.\n+\t\t\t */\n+\t\t\tif ((strlen(argv[i]) == 2) && *argv[i] == '-' &&\n+\t\t\t    strchr(\"akpqu\", argv[i][1]))\n+\t\t\t\tcontinue;\n+\n+\t\t\tif (!strcmp(argv[i], \"--all\") ||\n+\t\t\t    !strcmp(argv[i], \"--keep-index\") ||\n+\t\t\t    !strcmp(argv[i], \"--no-keep-index\") ||\n+\t\t\t    !strcmp(argv[i], \"--patch\") ||\n+\t\t\t    !strcmp(argv[i], \"--quiet\") ||\n+\t\t\t    !strcmp(argv[i], \"--include-untracked\"))\n+\t\t\t\tcontinue;\n+\n+\t\t\t/*\n+\t\t\t * `-m` and `--message=` are verified separately because\n+\t\t\t * they need to be immediately followed by a string\n+\t\t\t * (i.e.`-m\"foobar\"` or `--message=\"foobar\"`).\n+\t\t\t */\n+\t\t\tif ((strlen(argv[i]) > 2 &&\n+\t\t\t     !strncmp(argv[i], \"-m\", 2)) ||\n+\t\t\t    (strlen(argv[i]) > 10 &&\n+\t\t\t     !strncmp(argv[i], \"--message=\", 10)))\n+\t\t\t\tcontinue;\n+\n+\t\t\tusage_with_options(git_stash_usage, options);\n+\t\t}\n+\t}\n \n-\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n-\t\t      git_stash_helper_usage, options);\n+\targv_array_push(&args, \"push\");\n+\targv_array_pushv(&args, argv);\n+\treturn !!push_stash(args.argc, args.argv, prefix);\n }\ndiff --git a/git-stash.sh b/git-stash.sh\ndeleted file mode 100755\nindex 695f1feba3..0000000000\n--- a/git-stash.sh\n+++ /dev/null\n@@ -1,153 +0,0 @@\n-#!/bin/sh\n-# Copyright (c) 2007, Nanako Shiraishi\n-\n-dashless=$(basename \"$0\" | sed -e 's/-/ /')\n-USAGE=\"list [<options>]\n-   or: $dashless show [<stash>]\n-   or: $dashless drop [-q|--quiet] [<stash>]\n-   or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n-   or: $dashless branch <branchname> [<stash>]\n-   or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n-\t\t      [-u|--include-untracked] [-a|--all] [<message>]\n-   or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n-\t\t       [-u|--include-untracked] [-a|--all] [-m <message>]\n-\t\t       [-- <pathspec>...]]\n-   or: $dashless clear\"\n-\n-SUBDIRECTORY_OK=Yes\n-OPTIONS_SPEC=\n-START_DIR=$(pwd)\n-. git-sh-setup\n-require_work_tree\n-prefix=$(git rev-parse --show-prefix) || exit 1\n-cd_to_toplevel\n-\n-TMP=\"$GIT_DIR/.git-stash.$$\"\n-TMPindex=${GIT_INDEX_FILE-\"$(git rev-parse --git-path index)\"}.stash.$$\n-trap 'rm -f \"$TMP-\"* \"$TMPindex\"' 0\n-\n-ref_stash=refs/stash\n-\n-if git config --get-colorbool color.interactive; then\n-       help_color=\"$(git config --get-color color.interactive.help 'red bold')\"\n-       reset_color=\"$(git config --get-color '' reset)\"\n-else\n-       help_color=\n-       reset_color=\n-fi\n-\n-#\n-# Parses the remaining options looking for flags and\n-# at most one revision defaulting to ${ref_stash}@{0}\n-# if none found.\n-#\n-# Derives related tree and commit objects from the\n-# revision, if one is found.\n-#\n-# stash records the work tree, and is a merge between the\n-# base commit (first parent) and the index tree (second parent).\n-#\n-#   REV is set to the symbolic version of the specified stash-like commit\n-#   IS_STASH_LIKE is non-blank if ${REV} looks like a stash\n-#   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref\n-#   s is set to the SHA1 of the stash commit\n-#   w_commit is set to the commit containing the working tree\n-#   b_commit is set to the base commit\n-#   i_commit is set to the commit containing the index tree\n-#   u_commit is set to the commit containing the untracked files tree\n-#   w_tree is set to the working tree\n-#   b_tree is set to the base tree\n-#   i_tree is set to the index tree\n-#   u_tree is set to the untracked files tree\n-#\n-#   GIT_QUIET is set to t if -q is specified\n-#   INDEX_OPTION is set to --index if --index is specified.\n-#   FLAGS is set to the remaining flags (if allowed)\n-#\n-# dies if:\n-#   * too many revisions specified\n-#   * no revision is specified and there is no stash stack\n-#   * a revision is specified which cannot be resolve to a SHA1\n-#   * a non-existent stash reference is specified\n-#   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n-#\n-\n-test \"$1\" = \"-p\" && set \"push\" \"$@\"\n-\n-PARSE_CACHE='--not-parsed'\n-# The default command is \"push\" if nothing but options are given\n-seen_non_option=\n-for opt\n-do\n-\tcase \"$opt\" in\n-\t--) break ;;\n-\t-*) ;;\n-\t*) seen_non_option=t; break ;;\n-\tesac\n-done\n-\n-test -n \"$seen_non_option\" || set \"push\" \"$@\"\n-\n-# Main command set\n-case \"$1\" in\n-list)\n-\tshift\n-\tgit stash--helper list \"$@\"\n-\t;;\n-show)\n-\tshift\n-\tgit stash--helper show \"$@\"\n-\t;;\n-save)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper save \"$@\"\n-\t;;\n-push)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper push \"$@\"\n-\t;;\n-apply)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper apply \"$@\"\n-\t;;\n-clear)\n-\tshift\n-\tgit stash--helper clear \"$@\"\n-\t;;\n-create)\n-\tshift\n-\tgit stash--helper create --message \"$*\"\n-\t;;\n-store)\n-\tshift\n-\tgit stash--helper store \"$@\"\n-\t;;\n-drop)\n-\tshift\n-\tgit stash--helper drop \"$@\"\n-\t;;\n-pop)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper pop \"$@\"\n-\t;;\n-branch)\n-\tshift\n-\tcd \"$START_DIR\"\n-\tgit stash--helper branch \"$@\"\n-\t;;\n-*)\n-\tcase $# in\n-\t0)\n-\t\tcd \"$START_DIR\"\n-\t\tgit stash--helper push &&\n-\t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n-\t\t;;\n-\t*)\n-\t\tusage\n-\tesac\n-\t;;\n-esac\ndiff --git a/git.c b/git.c\nindex 3c0e762d7d..78548397cf 100644\n--- a/git.c\n+++ b/git.c\n@@ -544,7 +544,7 @@ static struct cmd_struct commands[] = {\n \t{ \"show-index\", cmd_show_index },\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{ \"stash\", cmd_stash, 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 | NO_PARSEOPT },\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358906","messageId":"7d43996554895c86840f5fc2ee9c5239f3d6ff3a.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 12/21] stash: convert show to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:28Z","receivedAt":"2018-09-25T22:34:48Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash show to the helper and delete the show_stash, have_stash,\nassert_stash_like, is_stash_like and parse_flags_and_rev functions\nfrom the shell script now that they are no longer needed.\n\nIn shell version, although `git stash show` accepts `--index` and\n`--quiet` options, it ignores them. In C, both options are passed\nfurther to `git diff`.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c |  87 ++++++++++++++++++++++++++\n git-stash.sh            | 132 +---------------------------------------\n 2 files changed, 88 insertions(+), 131 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 9e256690ec..1bc838ee6b 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -10,9 +10,12 @@\n #include \"run-command.h\"\n #include \"dir.h\"\n #include \"rerere.h\"\n+#include \"revision.h\"\n+#include \"log-tree.h\"\n \n static const char * const git_stash_helper_usage[] = {\n \tN_(\"git stash--helper list [<options>]\"),\n+\tN_(\"git stash--helper show [<stash>]\"),\n \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n@@ -25,6 +28,11 @@ static const char * const git_stash_helper_list_usage[] = {\n \tNULL\n };\n \n+static const char * const git_stash_helper_show_usage[] = {\n+\tN_(\"git stash--helper show [<stash>]\"),\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@@ -643,6 +651,83 @@ static int list_stash(int argc, const char **argv, const char *prefix)\n \treturn run_command(&cp);\n }\n \n+static int show_stat = 1;\n+static int show_patch;\n+\n+static int git_stash_config(const char *var, const char *value, void *cb)\n+{\n+\tif (!strcmp(var, \"stash.showstat\")) {\n+\t\tshow_stat = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(var, \"stash.showpatch\")) {\n+\t\tshow_patch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\treturn git_default_config(var, value, cb);\n+}\n+\n+static int show_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint i;\n+\tint opts = 0;\n+\tint ret = 0;\n+\tstruct stash_info info;\n+\tstruct rev_info rev;\n+\tstruct argv_array stash_args = ARGV_ARRAY_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\tinit_diff_ui_defaults();\n+\tgit_config(git_diff_ui_config, NULL);\n+\tinit_revisions(&rev, prefix);\n+\n+\tfor (i = 1; i < argc; i++) {\n+\t\tif (argv[i][0] != '-')\n+\t\t\targv_array_push(&stash_args, argv[i]);\n+\t\telse\n+\t\t\topts++;\n+\t}\n+\n+\tret = get_stash_info(&info, stash_args.argc, stash_args.argv);\n+\targv_array_clear(&stash_args);\n+\tif (ret)\n+\t\treturn -1;\n+\n+\t/*\n+\t * The config settings are applied only if there are not passed\n+\t * any options.\n+\t */\n+\tif (!opts) {\n+\t\tgit_config(git_stash_config, NULL);\n+\t\tif (show_stat)\n+\t\t\trev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;\n+\n+\t\tif (show_patch)\n+\t\t\trev.diffopt.output_format |= DIFF_FORMAT_PATCH;\n+\n+\t\tif (!show_stat && !show_patch) {\n+\t\t\tfree_stash_info(&info);\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n+\n+\targc = setup_revisions(argc, argv, &rev, NULL);\n+\tif (argc > 1) {\n+\t\tfree_stash_info(&info);\n+\t\tusage_with_options(git_stash_helper_show_usage, options);\n+\t}\n+\n+\trev.diffopt.flags.recursive = 1;\n+\tsetup_diff_pager(&rev.diffopt);\n+\tdiff_tree_oid(&info.b_commit, &info.w_commit, \"\", &rev.diffopt);\n+\tlog_tree_diff_flush(&rev);\n+\n+\tfree_stash_info(&info);\n+\treturn diff_result_code(&rev.diffopt, 0);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -675,6 +760,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!branch_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"list\"))\n \t\treturn !!list_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"show\"))\n+\t\treturn !!show_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex 6052441aa2..0d05cbc1e5 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -378,35 +378,6 @@ save_stash () {\n \tfi\n }\n \n-have_stash () {\n-\tgit rev-parse --verify --quiet $ref_stash >/dev/null\n-}\n-\n-show_stash () {\n-\tALLOW_UNKNOWN_FLAGS=t\n-\tassert_stash_like \"$@\"\n-\n-\tif test -z \"$FLAGS\"\n-\tthen\n-\t\tif test \"$(git config --bool stash.showStat || echo true)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=--stat\n-\t\tfi\n-\n-\t\tif test \"$(git config --bool stash.showPatch || echo false)\" = \"true\"\n-\t\tthen\n-\t\t\tFLAGS=${FLAGS}${FLAGS:+ }-p\n-\t\tfi\n-\n-\t\tif test -z \"$FLAGS\"\n-\t\tthen\n-\t\t\treturn 0\n-\t\tfi\n-\tfi\n-\n-\tgit diff ${FLAGS} $b_commit $w_commit\n-}\n-\n show_help () {\n \texec git help stash\n \texit 1\n@@ -448,107 +419,6 @@ show_help () {\n #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not \"t\"\n #\n \n-parse_flags_and_rev()\n-{\n-\ttest \"$PARSE_CACHE\" = \"$*\" && return 0 # optimisation\n-\tPARSE_CACHE=\"$*\"\n-\n-\tIS_STASH_LIKE=\n-\tIS_STASH_REF=\n-\tINDEX_OPTION=\n-\ts=\n-\tw_commit=\n-\tb_commit=\n-\ti_commit=\n-\tu_commit=\n-\tw_tree=\n-\tb_tree=\n-\ti_tree=\n-\tu_tree=\n-\n-\tFLAGS=\n-\tREV=\n-\tfor opt\n-\tdo\n-\t\tcase \"$opt\" in\n-\t\t\t-q|--quiet)\n-\t\t\t\tGIT_QUIET=-t\n-\t\t\t;;\n-\t\t\t--index)\n-\t\t\t\tINDEX_OPTION=--index\n-\t\t\t;;\n-\t\t\t--help)\n-\t\t\t\tshow_help\n-\t\t\t;;\n-\t\t\t-*)\n-\t\t\t\ttest \"$ALLOW_UNKNOWN_FLAGS\" = t ||\n-\t\t\t\t\tdie \"$(eval_gettext \"unknown option: \\$opt\")\"\n-\t\t\t\tFLAGS=\"${FLAGS}${FLAGS:+ }$opt\"\n-\t\t\t;;\n-\t\t\t*)\n-\t\t\t\tREV=\"${REV}${REV:+ }'$opt'\"\n-\t\t\t;;\n-\t\tesac\n-\tdone\n-\n-\teval set -- $REV\n-\n-\tcase $# in\n-\t\t0)\n-\t\t\thave_stash || die \"$(gettext \"No stash entries found.\")\"\n-\t\t\tset -- ${ref_stash}@{0}\n-\t\t;;\n-\t\t1)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tdie \"$(eval_gettext \"Too many revisions specified: \\$REV\")\"\n-\t\t;;\n-\tesac\n-\n-\tcase \"$1\" in\n-\t\t*[!0-9]*)\n-\t\t\t:\n-\t\t;;\n-\t\t*)\n-\t\t\tset -- \"${ref_stash}@{$1}\"\n-\t\t;;\n-\tesac\n-\n-\tREV=$(git rev-parse --symbolic --verify --quiet \"$1\") || {\n-\t\treference=\"$1\"\n-\t\tdie \"$(eval_gettext \"\\$reference is not a valid reference\")\"\n-\t}\n-\n-\ti_commit=$(git rev-parse --verify --quiet \"$REV^2\") &&\n-\tset -- $(git rev-parse \"$REV\" \"$REV^1\" \"$REV:\" \"$REV^1:\" \"$REV^2:\" 2>/dev/null) &&\n-\ts=$1 &&\n-\tw_commit=$1 &&\n-\tb_commit=$2 &&\n-\tw_tree=$3 &&\n-\tb_tree=$4 &&\n-\ti_tree=$5 &&\n-\tIS_STASH_LIKE=t &&\n-\ttest \"$ref_stash\" = \"$(git rev-parse --symbolic-full-name \"${REV%@*}\")\" &&\n-\tIS_STASH_REF=t\n-\n-\tu_commit=$(git rev-parse --verify --quiet \"$REV^3\") &&\n-\tu_tree=$(git rev-parse \"$REV^3:\" 2>/dev/null)\n-}\n-\n-is_stash_like()\n-{\n-\tparse_flags_and_rev \"$@\"\n-\ttest -n \"$IS_STASH_LIKE\"\n-}\n-\n-assert_stash_like() {\n-\tis_stash_like \"$@\" || {\n-\t\targs=\"$*\"\n-\t\tdie \"$(eval_gettext \"'\\$args' is not a stash-like commit\")\"\n-\t}\n-}\n-\n test \"$1\" = \"-p\" && set \"push\" \"$@\"\n \n PARSE_CACHE='--not-parsed'\n@@ -573,7 +443,7 @@ list)\n \t;;\n show)\n \tshift\n-\tshow_stash \"$@\"\n+\tgit stash--helper show \"$@\"\n \t;;\n save)\n \tshift\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358907","messageId":"1fb9f40d88a813af81e3ddc9e143215571bc53f3.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 21/21] stash: replace all `write-tree` child processes with API calls","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:37Z","receivedAt":"2018-09-25T22:34:49Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commit replaces spawning `git write-tree` with API calls.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash.c | 41 ++++++++++++-----------------------------\n 1 file changed, 12 insertions(+), 29 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 43d0de1f13..86a7e0a5a5 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -941,9 +941,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n {\n \tint ret = 0;\n \tstruct strbuf untracked_msg = STRBUF_INIT;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n-\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n+\tstruct index_state istate = { NULL };\n \n \tcp_upd_index.git_cmd = 1;\n \targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n@@ -957,15 +956,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \t\tgoto done;\n \t}\n \n-\tcp_write_tree.git_cmd = 1;\n-\targv_array_push(&cp_write_tree.args, \"write-tree\");\n-\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-\tget_oid_hex(out.buf, &info->u_tree);\n \n \tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n \t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n@@ -974,8 +969,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \t}\n \n done:\n+\tdiscard_index(&istate);\n \tstrbuf_release(&untracked_msg);\n-\tstrbuf_release(&out);\n \tremove_path(stash_index_path.buf);\n \treturn ret;\n }\n@@ -984,11 +979,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,\n \t\t       struct strbuf *out_patch, int quiet)\n {\n \tint ret = 0;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct child_process cp_read_tree = CHILD_PROCESS_INIT;\n \tstruct child_process cp_add_i = CHILD_PROCESS_INIT;\n-\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n+\tstruct index_state istate = { NULL };\n \n \tremove_path(stash_index_path.buf);\n \n@@ -1014,17 +1008,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,\n \t}\n \n \t/* State of the working tree. */\n-\tcp_write_tree.git_cmd = 1;\n-\targv_array_push(&cp_write_tree.args, \"write-tree\");\n-\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n-\tget_oid_hex(out.buf, &info->w_tree);\n-\n \tcp_diff_tree.git_cmd = 1;\n \targv_array_pushl(&cp_diff_tree.args, \"diff-tree\", \"-p\", \"HEAD\",\n \t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n@@ -1040,7 +1029,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,\n \t}\n \n done:\n-\tstrbuf_release(&out);\n+\tdiscard_index(&istate);\n \tremove_path(stash_index_path.buf);\n \treturn ret;\n }\n@@ -1050,9 +1039,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \tint ret = 0;\n \tstruct rev_info rev;\n \tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n-\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n-\tstruct strbuf out = STRBUF_INIT;\n \tstruct strbuf diff_output = STRBUF_INIT;\n+\tstruct index_state istate = { NULL };\n \n \tset_alternate_index_output(stash_index_path.buf);\n \tif (reset_tree(&info->i_tree, 0, 0)) {\n@@ -1091,20 +1079,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \t\tgoto done;\n \t}\n \n-\tcp_write_tree.git_cmd = 1;\n-\targv_array_push(&cp_write_tree.args, \"write-tree\");\n-\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n-\t\t\t stash_index_path.buf);\n-\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n+\tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n+\t\t\t\tNULL)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n \n-\tget_oid_hex(out.buf, &info->w_tree);\n-\n done:\n+\tdiscard_index(&istate);\n \tUNLEAK(rev);\n-\tstrbuf_release(&out);\n \tobject_array_clear(&rev.pending);\n \tstrbuf_release(&diff_output);\n \tremove_path(stash_index_path.buf);\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358908","messageId":"c5d7df39656c3404a2469bdb9e7a09fad46d3227.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 20/21] stash: optimize `get_untracked_files()` and `check_changes()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:36Z","receivedAt":"2018-09-25T22:34:50Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"This commits introduces a optimization by avoiding calling the\nsame functions again. For example, `git stash push -u`\nwould call at some points the following functions:\n\n * `check_changes()` (inside `do_push_stash()`)\n * `do_create_stash()`, which calls: `check_changes()` and\n`get_untracked_files()`\n\nNote that `check_changes()` also calls `get_untracked_files()`.\nSo, `check_changes()` is called 2 times and `get_untracked_files()`\n3 times.\n\nThe old function `check_changes()` now consists of two functions:\n`get_untracked_files()` and `check_changes_tracked_files()`.\n\nThese are the call chains for `push` and `create`:\n\n * `push_stash()` -> `do_push_stash()` -> `do_create_stash()`\n\n * `create_stash()` -> `do_create_stash()`\n\nTo prevent calling the same functions over and over again,\n`check_changes()` inside `do_create_stash()` is now placed\nin the caller functions (`create_stash()` and `do_push_stash()`).\nThis way `check_changes()` and `get_untracked files()` are called\nonly one time.\n\nhttps://public-inbox.org/git/20180818223329.GJ11326@hank.intra.tgummerer.com/\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash.c | 51 ++++++++++++++++++++++++++++---------------------\n 1 file changed, 29 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex fc4a2050b1..43d0de1f13 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -875,19 +875,18 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,\n }\n \n /*\n- * The return value of `check_changes()` can be:\n+ * The return value of `check_changes_tracked_files()` can be:\n  *\n  * < 0 if there was an error\n  * = 0 if there are no changes.\n  * > 0 if there are changes.\n  */\n-static int check_changes(struct pathspec ps, int include_untracked)\n+\n+static int check_changes_tracked_files(struct pathspec ps)\n {\n \tint result;\n-\tint ret = 0;\n \tstruct rev_info rev;\n \tstruct object_id dummy;\n-\tstruct strbuf out = STRBUF_INIT;\n \n \t/* No initial commit. */\n \tif (get_oid(\"HEAD\", &dummy))\n@@ -915,14 +914,26 @@ static int check_changes(struct pathspec ps, int include_untracked)\n \tif (diff_result_code(&rev.diffopt, result))\n \t\treturn 1;\n \n+\treturn 0;\n+}\n+\n+/*\n+ * The function will fill `untracked_files` with the names of untracked files\n+ * It will return 1 if there were any changes and 0 if there were not.\n+ */\n+\n+static int check_changes(struct pathspec ps, int include_untracked,\n+\t\t\t struct strbuf *untracked_files)\n+{\n+\tint ret = 0;\n+\tif (check_changes_tracked_files(ps))\n+\t\tret = 1;\n+\n \tif (include_untracked && get_untracked_files(ps, include_untracked,\n-\t\t\t\t\t\t     &out)) {\n-\t\tstrbuf_release(&out);\n-\t\treturn 1;\n-\t}\n+\t\t\t\t\t\t     untracked_files))\n+\t\tret = 1;\n \n-\tstrbuf_release(&out);\n-\treturn 0;\n+\treturn ret;\n }\n \n static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n@@ -1131,7 +1142,7 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n \t}\n \n-\tif (!check_changes(ps, include_untracked)) {\n+\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n \t\tret = 1;\n \t\t*stash_msg = NULL;\n \t\tgoto done;\n@@ -1157,8 +1168,7 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\tgoto done;\n \t}\n \n-\tif (include_untracked && get_untracked_files(ps, include_untracked,\n-\t\t\t\t\t\t     &untracked_files)) {\n+\tif (include_untracked) {\n \t\tif (save_untracked_files(info, &msg, untracked_files)) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n@@ -1234,18 +1244,14 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \t\t\t\t\t     ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tret = do_create_stash(ps, &stash_msg, 0, 0, &info, NULL, 0);\n+\tif (!check_changes_tracked_files(ps))\n+\t\treturn 0;\n \n-\tif (!ret)\n+\tif (!(ret = do_create_stash(ps, &stash_msg, 0, 0, &info, NULL, 0)))\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n \tstrbuf_release(&stash_msg_buf);\n-\n-\t/*\n-\t * ret can be 1 if there were no changes. In this case, we should\n-\t * not error out.\n-\t */\n-\treturn ret < 0;\n+\treturn ret;\n }\n \n static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n@@ -1254,6 +1260,7 @@ static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n \tint ret = 0;\n \tstruct stash_info info;\n \tstruct strbuf patch = STRBUF_INIT;\n+\tstruct strbuf untracked_files = STRBUF_INIT;\n \n \tif (patch_mode && keep_index == -1)\n \t\tkeep_index = 1;\n@@ -1289,7 +1296,7 @@ static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n \t\tgoto done;\n \t}\n \n-\tif (!check_changes(ps, include_untracked)) {\n+\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n \t\tstash_msg = NULL;\n \t\tif (!quiet)\n \t\t\tprintf_ln(_(\"No local changes to save\"));\n-- \n2.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358910","messageId":"f7b639a7b4da943884f7a4960d06ec4d2344a119.1537913094.git.ungureanupaulsebastian@gmail.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v9 09/21] stash: convert branch to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-09-25T22:33:25Z","receivedAt":"2018-09-25T22:34:53Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd stash branch to the helper and delete the apply_to_branch\nfunction from the shell script.\n\nCheckout does not currently provide a function for checking out\na branch as cmd_checkout does a large amount of sanity checks\nfirst that we require here.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 46 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 17 ++-------------\n 2 files changed, 48 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 72472eaeb7..5841bd0e98 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -14,6 +14,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@@ -28,6 +29,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@@ -536,6 +542,44 @@ 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+\tint ret;\n+\tconst char *branch = NULL;\n+\tstruct stash_info info;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_branch_usage, 0);\n+\n+\tif (!argc) {\n+\t\tfprintf_ln(stderr, \"No branch name specified\");\n+\t\treturn -1;\n+\t}\n+\n+\tbranch = argv[0];\n+\n+\tif (get_stash_info(&info, argc - 1, argv + 1))\n+\t\treturn -1;\n+\n+\tcp.git_cmd = 1;\n+\targv_array_pushl(&cp.args, \"checkout\", \"-b\", NULL);\n+\targv_array_push(&cp.args, branch);\n+\targv_array_push(&cp.args, oid_to_hex(&info.b_commit));\n+\tret = run_command(&cp);\n+\tif (!ret)\n+\t\tret = do_apply_stash(prefix, &info, 1, 0);\n+\tif (!ret && info.is_stash_ref)\n+\t\tret = do_drop_stash(prefix, &info, 0);\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 \tpid_t pid = getpid();\n@@ -562,6 +606,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\treturn !!clear_stash(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"drop\"))\n \t\treturn !!drop_stash(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"branch\"))\n+\t\treturn !!branch_stash(argc, argv, prefix);\n \n \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n \t\t      git_stash_helper_usage, options);\ndiff --git a/git-stash.sh b/git-stash.sh\nindex a99d5dc9e5..29d9f44255 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@@ -673,7 +659,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.19.0.rc0.23.g1fb9f40d88\n\n"},{"id":"358973","messageId":"xmqq8t3oksve.fsf@gitster-ct.c.googlers.com","threadId":"48222","inReplyTo":"cover.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 00/21] Convert \"git stash\" to C builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-09-26T18:37:09Z","receivedAt":"2018-09-26T18:37:15Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n\n> Hello,\n>\n> This is a new iteration of `git stash`, based on the last review I got.\n> This new iteration brings mostly code styling fix issues in order to make\n> the code more readable. There is also a new patch \"strbuf.c: add\n> `strbuf_join_argv()`\". By making some small changes, the code is now a\n> little bit closer to be used as API.\n>\n> Joel 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> Paul-Sebastian Ungureanu (16):\n>   sha1-name.c: add `get_oidf()` which acts like `get_oid()`\n>   strbuf.c: add `strbuf_join_argv()`\n>   stash: update test cases conform to coding guidelines\n>   stash: rename test cases to be more descriptive\n>   stash: add tests for `git stash show` config\n>   stash: convert list to builtin\n>   stash: convert show to builtin\n>   stash: mention options in `show` synopsis.\n>   stash: convert store to builtin\n>   stash: convert create to builtin\n>   stash: convert push to builtin\n>   stash: make push -q quiet\n>   stash: convert save to builtin\n>   stash: convert `stash--helper.c` into `stash.c`\n>   stash: optimize `get_untracked_files()` and `check_changes()`\n>   stash: replace all `write-tree` child processes with API calls\n\nHere is how these appear in my MUA.  I am guessing that 03/21 is the\nright copy and 02/21 that appear after 21/21 is a duplicat that I\nshould not even look at (the same for 4 other ones with [GSoC]\nlabel)?\n\n   [  53: PSU] [PATCH v9 00/21] Convert \"git stash\" to C builtin\n    [  57: PSU] [PATCH v9 01/21] sha1-name.c: add `get_oidf()` which acts like `get_oid()`\n    [  56: PSU] [PATCH v9 02/21] strbuf.c: add `strbuf_join_argv()`\n    [  68: PSU] [PATCH v9 03/21] stash: improve option parsing test coverage\n    [ 378: PSU] [PATCH v9 04/21] stash: update test cases conform to coding guidelines\n    [  78: PSU] [PATCH v9 05/21] stash: rename test cases to be more descriptive\n    [ 102: PSU] [PATCH v9 06/21] stash: add tests for `git stash show` config\n    [ 632: PSU] [PATCH v9 07/21] stash: convert apply to builtin\n    [ 199: PSU] [PATCH v9 08/21] stash: convert drop and clear to builtin\n    [ 132: PSU] [PATCH v9 09/21] stash: convert branch to builtin\n    [ 151: PSU] [PATCH v9 10/21] stash: convert pop to builtin\n    [ 100: PSU] [PATCH v9 11/21] stash: convert list to builtin\n    [ 296: PSU] [PATCH v9 12/21] stash: convert show to builtin\n    [  56: PSU] [PATCH v9 13/21] stash: mention options in `show` synopsis.\n    [ 165: PSU] [PATCH v9 14/21] stash: convert store to builtin\n    [ 513: PSU] [PATCH v9 15/21] stash: convert create to builtin\n    [ 339: PSU] [PATCH v9 16/21] stash: convert push to builtin\n    [ 195: PSU] [PATCH v9 17/21] stash: make push -q quiet\n    [ 430: PSU] [PATCH v9 18/21] stash: convert save to builtin\n    [ 575: PSU] [PATCH v9 19/21] stash: convert `stash--helper.c` into `stash.c`\n    [ 156: PSU] [PATCH v9 20/21] stash: optimize `get_untracked_files()` and `check_changes()`\n    [ 130: PSU] [PATCH v9 21/21] stash: replace all `write-tree` child processes with API calls\n   [  68: PSU] [GSoC][PATCH v9 02/21] stash: improve option parsing test coverage\n   [ 378: PSU] [GSoC][PATCH v9 03/21] stash: update test cases conform to coding guidelines\n   [  78: PSU] [GSoC][PATCH v9 04/21] stash: rename test cases to be more descriptive\n   [ 102: PSU] [GSoC][PATCH v9 05/21] stash: add tests for `git stash show` config\n   [  55: PSU] [GSoC][PATCH v9 06/21] strbuf.c: add `strbuf_join_argv()`\n\nThanks.\n\n>\n>  Documentation/git-stash.txt  |    4 +-\n>  Makefile                     |    2 +-\n>  builtin.h                    |    1 +\n>  builtin/stash.c              | 1595 ++++++++++++++++++++++++++++++++++\n>  cache.h                      |    1 +\n>  git-stash.sh                 |  752 ----------------\n>  git.c                        |    1 +\n>  sha1-name.c                  |   19 +\n>  strbuf.c                     |   15 +\n>  strbuf.h                     |    7 +\n>  t/t3903-stash.sh             |  192 ++--\n>  t/t3907-stash-show-config.sh |   83 ++\n>  12 files changed, 1851 insertions(+), 821 deletions(-)\n>  create mode 100644 builtin/stash.c\n>  delete mode 100755 git-stash.sh\n>  create mode 100755 t/t3907-stash-show-config.sh\n"},{"id":"359344","messageId":"20180930164907.GC2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"1fb6d7a72c067fb594b04944dea4a5d223229d69.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 02/21] strbuf.c: add `strbuf_join_argv()`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-09-30T16:49:07Z","receivedAt":"2018-09-30T16:49:13Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 09/26, Paul-Sebastian Ungureanu wrote:\n> Implement `strbuf_join_argv()` to join arguments\n> into a strbuf.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  strbuf.c | 15 +++++++++++++++\n>  strbuf.h |  7 +++++++\n>  2 files changed, 22 insertions(+)\n> \n> diff --git a/strbuf.c b/strbuf.c\n> index 64041c3c24..3eb431b2b0 100644\n> --- a/strbuf.c\n> +++ b/strbuf.c\n> @@ -259,6 +259,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)\n>  \tstrbuf_setlen(sb, sb->len + sb2->len);\n>  }\n>  \n> +const char *strbuf_join_argv(struct strbuf *buf,\n> +\t\t\t     int argc, const char **argv, char delim)\n> +{\n> +\tif (!argc)\n> +\t\treturn buf->buf;\n> +\n> +\tstrbuf_addstr(buf, *argv);\n> +\twhile (--argc) {\n> +\t\tstrbuf_addch(buf, delim);\n> +\t\tstrbuf_addstr(buf, *(++argv));\n> +\t}\n> +\n> +\treturn buf->buf;\n\nWhy are we returning buf-buf here?  The strbuf is modified by the\nfunction, so the caller can just use buf->buf directly if they want\nto.  Is there something I'm missing?\n\n> +}\n> +\n>  void strbuf_addchars(struct strbuf *sb, int c, size_t n)\n>  {\n>  \tstrbuf_grow(sb, n);\n> diff --git a/strbuf.h b/strbuf.h\n> index 60a35aef16..7ed859bb8a 100644\n> --- a/strbuf.h\n> +++ b/strbuf.h\n> @@ -284,6 +284,13 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)\n>   */\n>  extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);\n>  \n> +\n\nstray newline? We usually only have one blank line between functions.\n\n> +/**\n> + *\n> + */\n\nForgot to write some documentation here? :)\n\n> +extern const char *strbuf_join_argv(struct strbuf *buf, int argc,\n> +\t\t\t\t    const char **argv, char delim);\n> +\n>  /**\n>   * This function can be used to expand a format string containing\n>   * placeholders. To that end, it parses the string and calls the specified\n> -- \n> 2.19.0.rc0.23.g1fb9f40d88\n> \n"},{"id":"359346","messageId":"20180930165921.GD2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"e2ced7320ffc205739133858a6b301c57893254f.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 04/21] stash: update test cases conform to coding guidelines","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-09-30T16:59:21Z","receivedAt":"2018-09-30T16:59:28Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"> Subject: stash: update test cases conform to coding guidelines\n\ns/stash/t3903/\ns/conform/to &/\n\nAlternatively the subject could also be \"t3903: modernize style\",\nwhich would be a bit shorter, and still convey the same information to\na reader of 'git log --oneline'.\n\nOn 09/26, Paul-Sebastian Ungureanu wrote:\n> Removed whitespaces after redirection operators.\n\ns/Removed/Remove/.  Commit messages should always use the imperative\nmood, as described in Documentation/SubmittingPatches.\n\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  t/t3903-stash.sh | 120 ++++++++++++++++++++++++-----------------------\n>  1 file changed, 61 insertions(+), 59 deletions(-)\n> \n> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh\n> index af7586d43d..de6cab1fe7 100755\n> --- a/t/t3903-stash.sh\n> +++ b/t/t3903-stash.sh\n> @@ -8,22 +8,22 @@ test_description='Test git stash'\n>  . ./test-lib.sh\n>  \n>  test_expect_success 'stash some dirty working directory' '\n> -\techo 1 > file &&\n> +\techo 1 >file &&\n>  \tgit add file &&\n>  \techo unrelated >other-file &&\n>  \tgit add other-file &&\n>  \ttest_tick &&\n>  \tgit commit -m initial &&\n> -\techo 2 > file &&\n> +\techo 2 >file &&\n>  \tgit add file &&\n> -\techo 3 > file &&\n> +\techo 3 >file &&\n>  \ttest_tick &&\n>  \tgit stash &&\n>  \tgit diff-files --quiet &&\n>  \tgit diff-index --cached --quiet HEAD\n>  '\n>  \n> -cat > expect << EOF\n> +cat >expect <<EOF\n>  diff --git a/file b/file\n>  index 0cfbf08..00750ed 100644\n>  --- a/file\n> @@ -35,7 +35,7 @@ EOF\n>  \n>  test_expect_success 'parents of stash' '\n>  \ttest $(git rev-parse stash^) = $(git rev-parse HEAD) &&\n> -\tgit diff stash^2..stash > output &&\n> +\tgit diff stash^2..stash >output &&\n>  \ttest_cmp output expect\n>  '\n>  \n> @@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' '\n>  \n>  test_expect_success 'apply stashed changes (including index)' '\n>  \tgit reset --hard HEAD^ &&\n> -\techo 6 > other-file &&\n> +\techo 6 >other-file &&\n>  \tgit add other-file &&\n>  \ttest_tick &&\n>  \tgit commit -m other-file &&\n> @@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' '\n>  \n>  test_expect_success 'drop top stash' '\n>  \tgit reset --hard &&\n> -\tgit stash list > stashlist1 &&\n> -\techo 7 > file &&\n> +\tgit stash list >expected &&\n> +\techo 7 >file &&\n>  \tgit stash &&\n>  \tgit stash drop &&\n> -\tgit stash list > stashlist2 &&\n> -\ttest_cmp stashlist1 stashlist2 &&\n> +\tgit stash list >actual &&\n> +\ttest_cmp expected actual &&\n>  \tgit stash apply &&\n>  \ttest 3 = $(cat file) &&\n>  \ttest 1 = $(git show :file) &&\n> @@ -113,9 +113,9 @@ test_expect_success 'drop top stash' '\n>  \n>  test_expect_success 'drop middle stash' '\n>  \tgit reset --hard &&\n> -\techo 8 > file &&\n> +\techo 8 >file &&\n>  \tgit stash &&\n> -\techo 9 > file &&\n> +\techo 9 >file &&\n>  \tgit stash &&\n>  \tgit stash drop stash@{1} &&\n>  \ttest 2 = $(git stash list | wc -l) &&\n> @@ -160,7 +160,7 @@ test_expect_success 'stash pop' '\n>  \ttest 0 = $(git stash list | wc -l)\n>  '\n>  \n> -cat > expect << EOF\n> +cat >expect <<EOF\n>  diff --git a/file2 b/file2\n>  new file mode 100644\n>  index 0000000..1fe912c\n> @@ -170,7 +170,7 @@ index 0000000..1fe912c\n>  +bar2\n>  EOF\n>  \n> -cat > expect1 << EOF\n> +cat >expect1 <<EOF\n>  diff --git a/file b/file\n>  index 257cc56..5716ca5 100644\n>  --- a/file\n> @@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644\n>  +bar\n>  EOF\n>  \n> -cat > expect2 << EOF\n> +cat >expect2 <<EOF\n>  diff --git a/file b/file\n>  index 7601807..5716ca5 100644\n>  --- a/file\n> @@ -198,79 +198,79 @@ index 0000000..1fe912c\n>  EOF\n>  \n>  test_expect_success 'stash branch' '\n> -\techo foo > file &&\n> +\techo foo >file &&\n>  \tgit commit file -m first &&\n> -\techo bar > file &&\n> -\techo bar2 > file2 &&\n> +\techo bar >file &&\n> +\techo bar2 >file2 &&\n>  \tgit add file2 &&\n>  \tgit stash &&\n> -\techo baz > file &&\n> +\techo baz >file &&\n>  \tgit commit file -m second &&\n>  \tgit stash branch stashbranch &&\n>  \ttest refs/heads/stashbranch = $(git symbolic-ref HEAD) &&\n>  \ttest $(git rev-parse HEAD) = $(git rev-parse master^) &&\n> -\tgit diff --cached > output &&\n> +\tgit diff --cached >output &&\n>  \ttest_cmp output expect &&\n> -\tgit diff > output &&\n> +\tgit diff >output &&\n>  \ttest_cmp output expect1 &&\n>  \tgit add file &&\n>  \tgit commit -m alternate\\ second &&\n> -\tgit diff master..stashbranch > output &&\n> +\tgit diff master..stashbranch >output &&\n>  \ttest_cmp output expect2 &&\n>  \ttest 0 = $(git stash list | wc -l)\n>  '\n>  \n>  test_expect_success 'apply -q is quiet' '\n> -\techo foo > file &&\n> +\techo foo >file &&\n>  \tgit stash &&\n> -\tgit stash apply -q > output.out 2>&1 &&\n> +\tgit stash apply -q >output.out 2>&1 &&\n>  \ttest_must_be_empty output.out\n>  '\n>  \n>  test_expect_success 'save -q is quiet' '\n> -\tgit stash save --quiet > output.out 2>&1 &&\n> +\tgit stash save --quiet >output.out 2>&1 &&\n>  \ttest_must_be_empty output.out\n>  '\n>  \n>  test_expect_success 'pop -q is quiet' '\n> -\tgit stash pop -q > output.out 2>&1 &&\n> +\tgit stash pop -q >output.out 2>&1 &&\n>  \ttest_must_be_empty output.out\n>  '\n>  \n>  test_expect_success 'pop -q --index works and is quiet' '\n> -\techo foo > file &&\n> +\techo foo >file &&\n>  \tgit add file &&\n>  \tgit stash save --quiet &&\n> -\tgit stash pop -q --index > output.out 2>&1 &&\n> +\tgit stash pop -q --index >output.out 2>&1 &&\n>  \ttest foo = \"$(git show :file)\" &&\n>  \ttest_must_be_empty output.out\n>  '\n>  \n>  test_expect_success 'drop -q is quiet' '\n>  \tgit stash &&\n> -\tgit stash drop -q > output.out 2>&1 &&\n> +\tgit stash drop -q >output.out 2>&1 &&\n>  \ttest_must_be_empty output.out\n>  '\n>  \n>  test_expect_success 'stash -k' '\n> -\techo bar3 > file &&\n> -\techo bar4 > file2 &&\n> +\techo bar3 >file &&\n> +\techo bar4 >file2 &&\n>  \tgit add file2 &&\n>  \tgit stash -k &&\n>  \ttest bar,bar4 = $(cat file),$(cat file2)\n>  '\n>  \n>  test_expect_success 'stash --no-keep-index' '\n> -\techo bar33 > file &&\n> -\techo bar44 > file2 &&\n> +\techo bar33 >file &&\n> +\techo bar44 >file2 &&\n>  \tgit add file2 &&\n>  \tgit stash --no-keep-index &&\n>  \ttest bar,bar2 = $(cat file),$(cat file2)\n>  '\n>  \n>  test_expect_success 'stash --invalid-option' '\n> -\techo bar5 > file &&\n> -\techo bar6 > file2 &&\n> +\techo bar5 >file &&\n> +\techo bar6 >file2 &&\n>  \tgit add file2 &&\n>  \ttest_must_fail git stash --invalid-option &&\n>  \ttest_must_fail git stash save --invalid-option &&\n> @@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD\" &&\n>  \tgit reset --hard &&\n> -\techo foo >> file &&\n> +\techo foo >>file &&\n>  \tSTASH_ID=$(git stash create) &&\n>  \tgit reset --hard &&\n>  \tgit stash branch stash-branch ${STASH_ID} &&\n> -\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n> +\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n> +\tgit branch -D stash-branch\" &&\n>  \ttest $(git ls-files --modified | wc -l) -eq 1\n>  '\n>  \n> @@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD\" &&\n>  \tgit reset --hard &&\n> -\techo foo >> file &&\n> +\techo foo >>file &&\n>  \tgit stash &&\n>  \ttest_when_finished \"git stash drop\" &&\n> -\techo bar >> file &&\n> +\techo bar >>file &&\n>  \tSTASH_ID=$(git stash create) &&\n>  \tgit reset --hard &&\n>  \tgit stash branch stash-branch ${STASH_ID} &&\n> -\ttest_when_finished \"git reset --hard HEAD && git checkout master && git branch -D stash-branch\" &&\n> +\ttest_when_finished \"git reset --hard HEAD && git checkout master &&\n> +\tgit branch -D stash-branch\" &&\n>  \ttest $(git ls-files --modified | wc -l) -eq 1\n>  '\n>  \n> @@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD\" &&\n>  \tgit reset --hard &&\n> -\techo foo >> file &&\n> +\techo foo >>file &&\n>  \tgit stash &&\n>  \ttest_when_finished \"git stash drop\" &&\n> -\techo bar >> file &&\n> +\techo bar >>file &&\n>  \tSTASH_ID=$(git stash create) &&\n>  \tgit reset --hard &&\n>  \tcat >expected <<-EOF &&\n> @@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD\" &&\n>  \tgit reset --hard &&\n> -\techo foo >> file &&\n> +\techo foo >>file &&\n>  \tgit stash &&\n>  \ttest_when_finished \"git stash drop\" &&\n> -\techo bar >> file &&\n> +\techo bar >>file &&\n>  \tSTASH_ID=$(git stash create) &&\n>  \tgit reset --hard &&\n>  \techo \"1\t0\tfile\" >expected &&\n> @@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD\" &&\n>  \tgit reset --hard &&\n> -\techo foo >> file &&\n> +\techo foo >>file &&\n>  \tgit stash &&\n>  \ttest_when_finished \"git stash drop\" &&\n> -\techo bar >> file &&\n> +\techo bar >>file &&\n>  \tSTASH_ID=$(git stash create) &&\n>  \tgit reset --hard &&\n>  \tcat >expected <<-EOF &&\n> @@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD\" &&\n>  \tgit reset --hard &&\n> -\techo foo >> file &&\n> +\techo foo >>file &&\n>  \tSTASH_ID=$(git stash create) &&\n>  \tgit reset --hard &&\n>  \techo \"1\t0\tfile\" >expected &&\n> @@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD\" &&\n>  \tgit reset --hard &&\n> -\techo foo >> file &&\n> +\techo foo >>file &&\n>  \tSTASH_ID=$(git stash create) &&\n>  \tgit reset --hard &&\n>  \tcat >expected <<-EOF &&\n> @@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n>  \tgit reset --hard &&\n> -\techo foo > file &&\n> +\techo foo >file &&\n>  \tgit stash &&\n> -\techo bar > file &&\n> +\techo bar >file &&\n>  \tgit stash &&\n>  \ttest_must_fail git stash drop $(git rev-parse stash@{0}) &&\n>  \tgit stash pop &&\n> @@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n>  \tgit stash clear &&\n>  \ttest_when_finished \"git reset --hard HEAD && git stash clear\" &&\n>  \tgit reset --hard &&\n> -\techo foo > file &&\n> +\techo foo >file &&\n>  \tgit stash &&\n> -\techo bar > file &&\n> +\techo bar >file &&\n>  \tgit stash &&\n>  \ttest_must_fail git stash pop $(git rev-parse stash@{0}) &&\n>  \tgit stash pop &&\n> @@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re\n>  \n>  test_expect_success 'ref with non-existent reflog' '\n>  \tgit stash clear &&\n> -\techo bar5 > file &&\n> -\techo bar6 > file2 &&\n> +\techo bar5 >file &&\n> +\techo bar6 >file2 &&\n>  \tgit add file2 &&\n>  \tgit stash &&\n>  \ttest_must_fail git rev-parse --quiet --verify does-not-exist &&\n> @@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' '\n>  test_expect_success 'invalid ref of the form stash@{n}, n >= N' '\n>  \tgit stash clear &&\n>  \ttest_must_fail git stash drop stash@{0} &&\n> -\techo bar5 > file &&\n> -\techo bar6 > file2 &&\n> +\techo bar5 >file &&\n> +\techo bar6 >file2 &&\n>  \tgit add file2 &&\n>  \tgit stash &&\n>  \ttest_must_fail git stash drop stash@{1} &&\n> @@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu\n>  \ttest_i18ncmp expect actual\n>  '\n>  \n> -cat > expect << EOF\n> +cat >expect <<EOF\n>  diff --git a/HEAD b/HEAD\n>  new file mode 100644\n>  index 0000000..fe0cbee\n> @@ -737,14 +739,14 @@ EOF\n>  test_expect_success 'stash where working directory contains \"HEAD\" file' '\n>  \tgit stash clear &&\n>  \tgit reset --hard &&\n> -\techo file-not-a-ref > HEAD &&\n> +\techo file-not-a-ref >HEAD &&\n>  \tgit add HEAD &&\n>  \ttest_tick &&\n>  \tgit stash &&\n>  \tgit diff-files --quiet &&\n>  \tgit diff-index --cached --quiet HEAD &&\n>  \ttest \"$(git rev-parse stash^)\" = \"$(git rev-parse HEAD)\" &&\n> -\tgit diff stash^..stash > output &&\n> +\tgit diff stash^..stash >output &&\n>  \ttest_cmp output expect\n>  '\n>  \n> -- \n> 2.19.0.rc0.23.g1fb9f40d88\n> \n"},{"id":"359347","messageId":"20180930174848.GE2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"fc0e991d5612f60b254ae07369dfda0939e256dd.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 07/21] stash: convert apply to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-09-30T17:48:48Z","receivedAt":"2018-09-30T17:48:56Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 09/26, Paul-Sebastian Ungureanu wrote:\n> From: Joel Teichroeb <joel@teichroeb.net>\n> \n> Add a builtin helper for performing stash commands. Converting\n> all at once proved hard to review, so starting with just apply\n> lets conversion get started without the other commands 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> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  .gitignore              |   1 +\n>  Makefile                |   1 +\n>  builtin.h               |   1 +\n>  builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++\n>  git-stash.sh            |  78 +------\n>  git.c                   |   1 +\n>  6 files changed, 463 insertions(+), 71 deletions(-)\n>  create mode 100644 builtin/stash--helper.c\n> \n> diff --git a/.gitignore b/.gitignore\n> index ffceea7d59..b59661cb88 100644\n> --- a/.gitignore\n> +++ b/.gitignore\n> @@ -157,6 +157,7 @@\n>  /git-show-ref\n>  /git-stage\n>  /git-stash\n> +/git-stash--helper\n>  /git-status\n>  /git-stripspace\n>  /git-submodule\n> diff --git a/Makefile b/Makefile\n> index d03df31c2a..f900c68e69 100644\n> --- a/Makefile\n> +++ b/Makefile\n> @@ -1093,6 +1093,7 @@ BUILTIN_OBJS += builtin/shortlog.o\n>  BUILTIN_OBJS += builtin/show-branch.o\n>  BUILTIN_OBJS += builtin/show-index.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\n> diff --git a/builtin.h b/builtin.h\n> index 99206df4bd..317bc338f7 100644\n> --- a/builtin.h\n> +++ b/builtin.h\n> @@ -223,6 +223,7 @@ 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_show_index(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);\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> new file mode 100644\n> index 0000000000..7819dae332\n> --- /dev/null\n> +++ b/builtin/stash--helper.c\n> @@ -0,0 +1,452 @@\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> +#include \"rerere.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 struct strbuf stash_index_path = STRBUF_INIT;\n> +\n> +/*\n> + * w_commit is set to the commit containing the working tree\n> + * b_commit is set to the base commit\n> + * i_commit is set to the commit containing the index tree\n> + * u_commit is set to the commit containing the untracked files tree\n> + * w_tree is set to the working tree\n> + * b_tree is set to the base tree\n> + * i_tree is set to the index tree\n> + * u_tree is set to the untracked files tree\n> + */\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 void free_stash_info(struct stash_info *info)\n> +{\n> +\tstrbuf_release(&info->revision);\n> +}\n> +\n> +static void assert_stash_like(struct stash_info *info, const char *revision)\n> +{\n> +\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n> +\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n> +\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n> +\t    get_oidf(&info->i_tree, \"%s^2:\", revision)) {\n> +\t\tfree_stash_info(info);\n> +\t\terror(_(\"'%s' is not a stash-like commit\"), revision);\n> +\t\texit(128);\n\nThis seems to just emulate 'die()'.  Can we just use that directly?\nThe only reason I could imagine for not doing that would be to keep\nthe same exit code, or the exact same message we had in the shell\nscript.  But we're doing neither here.  The shell script used to die\nwith exit code 1, while here we're exiting with 128, and the message\nis now prefixed with 'error: '.\n\nOne other thing is that we're free'ing 'info' before printing\nrevision.  As revision is basically 'info->revision.buf', and we're\nfreeing that we may end up printing garbage here.\n\n> +\t}\n> +}\n> +\n> +static int get_stash_info(struct stash_info *info, int argc, const char **argv)\n> +{\n> +\tint ret;\n> +\tchar *end_of_rev;\n> +\tchar *expanded_ref;\n> +\tconst char *revision;\n> +\tconst char *commit = NULL;\n> +\tstruct object_id dummy;\n> +\tstruct strbuf symbolic = STRBUF_INIT;\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\nThis should probably use 'strbuf_join_argv()' now. \n\n> +\n> +\t\tfprintf_ln(stderr, _(\"Too many revisions specified:%s\"),\n> +\t\t\t   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) {\n> +\t\tif (!ref_exists(ref_stash)) {\n> +\t\t\tfree_stash_info(info);\n> +\t\t\tfprintf_ln(stderr, _(\"No stash entries found.\"));\n> +\t\t\treturn -1;\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> +\tif (get_oid(revision, &info->w_commit)) {\n> +\t\terror(_(\"%s is not a valid reference\"), revision);\n> +\t\tfree_stash_info(info);\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tassert_stash_like(info, revision);\n> +\n> +\tinfo->has_u = !get_oidf(&info->u_tree, \"%s^3:\", revision);\n> +\n> +\tend_of_rev = strchrnul(revision, '@');\n> +\tstrbuf_add(&symbolic, revision, end_of_rev - revision);\n> +\n> +\tret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);\n> +\tstrbuf_release(&symbolic);\n> +\tswitch (ret) {\n> +\tcase 0: /* Not found, but valid ref */\n> +\t\tinfo->is_stash_ref = 0;\n> +\t\tbreak;\n> +\tcase 1:\n> +\t\tinfo->is_stash_ref = !strcmp(expanded_ref, ref_stash);\n> +\t\tbreak;\n> +\tdefault: /* Invalid or ambiguous */\n> +\t\tfree_stash_info(info);\n> +\t}\n> +\n> +\tfree(expanded_ref);\n> +\treturn !(ret == 0 || ret == 1);\n> +}\n> +\n> +static int reset_tree(struct object_id *i_tree, int update, int reset)\n> +{\n> +\tint nr_trees = 1;\n> +\tstruct unpack_trees_options opts;\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> +\t/*\n> +\t * Diff-tree would not be very hard to replace with a native function,\n> +\t * however it should be done together with apply_cached.\n> +\t */\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> +\t/*\n> +\t * Apply currently only reads either from stdin or a file, thus\n> +\t * apply_all_patches would have to be updated to optionally take a\n> +\t * buffer.\n> +\t */\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(void)\n> +{\n> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t/*\n> +\t * Reset is overall quite simple, however there is no current public\n> +\t * API for resetting.\n> +\t */\n> +\tcp.git_cmd = 1;\n> +\targv_array_push(&cp.args, \"reset\");\n> +\n> +\treturn run_command(&cp);\n> +}\n> +\n> +static int get_newly_staged(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> +\t/*\n> +\t * diff-index is very similar to diff-tree above, and should be\n> +\t * converted together with update_index.\n> +\t */\n> +\tcp.git_cmd = 1;\n> +\targv_array_pushl(&cp.args, \"diff-index\", \"--cached\", \"--name-only\",\n> +\t\t\t \"--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> +\t/*\n> +\t * Update-index is very complicated and may need to have a public\n> +\t * function exposed in order to remove this forking.\n> +\t */\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> +\tint res;\n> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t/*\n> +\t * We need to run restore files from a given index, but without\n> +\t * affecting the current index, so we use GIT_INDEX_FILE with\n> +\t * run_command to fork processes that will not interfere.\n> +\t */\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\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (run_command(&cp)) {\n> +\t\tremove_path(stash_index_path.buf);\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\",\n> +\t\t\t stash_index_path.buf);\n> +\n> +\tres = run_command(&cp);\n> +\tremove_path(stash_index_path.buf);\n> +\treturn res;\n> +}\n> +\n> +static int do_apply_stash(const char *prefix, struct stash_info *info,\n> +\t\t\t  int index, int quiet)\n> +{\n> +\tint ret;\n> +\tint has_index = index;\n> +\tstruct merge_options o;\n> +\tstruct object_id c_tree;\n> +\tstruct object_id index_tree;\n> +\tstruct commit *result;\n> +\tconst struct object_id *bases[1];\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) ||\n> +\t\t    !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();\n> +\t\t}\n> +\t}\n> +\n> +\tif (info->has_u && restore_untracked(&info->u_tree))\n> +\t\treturn error(_(\"could not restore untracked files from stash\"));\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, 1, bases,\n> +\t\t\t\t      &result);\n> +\tif (ret) {\n> +\t\trerere(0);\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 (get_newly_staged(&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\tif (refresh_cache(REFRESH_QUIET))\n> +\t\t\twarning(\"could not refresh index\");\n> +\t} else {\n> +\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\t/*\n> +\t\t * Status is quite simple and could be replaced with calls to\n> +\t\t * wt_status in the future, but it adds complexities which may\n> +\t\t * require more tests.\n> +\t\t */\n> +\t\tcp.git_cmd = 1;\n> +\t\tcp.dir = prefix;\n> +\t\targv_array_push(&cp.args, \"status\");\n> +\t\trun_command(&cp);\n> +\t}\n> +\n> +\treturn 0;\n> +}\n> +\n> +static int apply_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint ret;\n> +\tint quiet = 0;\n> +\tint index = 0;\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\t N_(\"attempt to recreate the index\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_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, quiet);\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> +\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\t\t     PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);\n> +\n> +\tindex_file = get_index_file();\n> +\tstrbuf_addf(&stash_index_path, \"%s.stash.%\" PRIuMAX, index_file,\n> +\t\t    (uintmax_t)pid);\n> +\n> +\tif (argc < 1)\n> +\t\tusage_with_options(git_stash_helper_usage, options);\n> +\tif (!strcmp(argv[0], \"apply\"))\n> +\t\treturn !!apply_stash(argc, argv, prefix);\n> +\n> +\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n> +\t\t      git_stash_helper_usage, options);\n> +}\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 94793c1a91..809b1c2d1d 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() {\n> @@ -713,7 +648,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> diff --git a/git.c b/git.c\n> index c27c38738b..3c0e762d7d 100644\n> --- a/git.c\n> +++ b/git.c\n> @@ -544,6 +544,7 @@ static struct cmd_struct commands[] = {\n>  \t{ \"show-index\", cmd_show_index },\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 | NO_PARSEOPT },\n> -- \n> 2.19.0.rc0.23.g1fb9f40d88\n> \n"},{"id":"359348","messageId":"20180930175714.GF2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"f7b639a7b4da943884f7a4960d06ec4d2344a119.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 09/21] stash: convert branch to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-09-30T17:57:14Z","receivedAt":"2018-09-30T17:57:20Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 09/26, Paul-Sebastian Ungureanu wrote:\n> From: Joel Teichroeb <joel@teichroeb.net>\n> \n> Add stash branch to the helper and delete the apply_to_branch\n> function from the shell script.\n> \n> Checkout does not currently provide a function for checking out\n> a branch as cmd_checkout does a large amount of sanity checks\n> first that we require here.\n> \n> Signed-off-by: Joel Teichroeb <joel@teichroeb.net>\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 46 +++++++++++++++++++++++++++++++++++++++++\n>  git-stash.sh            | 17 ++-------------\n>  2 files changed, 48 insertions(+), 15 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 72472eaeb7..5841bd0e98 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -14,6 +14,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> @@ -28,6 +29,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> @@ -536,6 +542,44 @@ 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> +\tint ret;\n> +\tconst char *branch = NULL;\n> +\tstruct stash_info info;\n> +\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\tstruct option options[] = {\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_branch_usage, 0);\n> +\n> +\tif (!argc) {\n> +\t\tfprintf_ln(stderr, \"No branch name specified\");\n\nThis should be marked for translation.\n\n> +\t\treturn -1;\n> +\t}\n> +\n> +\tbranch = argv[0];\n> +\n> +\tif (get_stash_info(&info, argc - 1, argv + 1))\n> +\t\treturn -1;\n> +\n> +\tcp.git_cmd = 1;\n> +\targv_array_pushl(&cp.args, \"checkout\", \"-b\", NULL);\n> +\targv_array_push(&cp.args, branch);\n> +\targv_array_push(&cp.args, oid_to_hex(&info.b_commit));\n> +\tret = run_command(&cp);\n> +\tif (!ret)\n> +\t\tret = do_apply_stash(prefix, &info, 1, 0);\n> +\tif (!ret && info.is_stash_ref)\n> +\t\tret = do_drop_stash(prefix, &info, 0);\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>  \tpid_t pid = getpid();\n> @@ -562,6 +606,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!clear_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"drop\"))\n>  \t\treturn !!drop_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"branch\"))\n> +\t\treturn !!branch_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index a99d5dc9e5..29d9f44255 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> @@ -673,7 +659,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> -- \n> 2.19.0.rc0.23.g1fb9f40d88\n> \n"},{"id":"359454","messageId":"20181002193658.GG2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"f4df1a271333bcf82c5ea1c5d9604ceb1c0374f8.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 13/21] stash: mention options in `show` synopsis.","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-02T19:36:58Z","receivedAt":"2018-10-02T19:37:02Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"> Subject: stash: mention options in `show` synopsis.\n\nReally minor point, but the '.' in the end should be dropped.\n\nAlso as this is fixing a pre-existing problem I would have put this\npatch near the beginning of the series, rather than in between\nconversions of functions, and just incorporated the bits that changed\nin the helper into the patch that converts 'git stash show'.\n\nThe rest of the patch looks good to me.\n\nOn 09/26, Paul-Sebastian Ungureanu wrote:\n> Mention in the usage text and in the documentation, that `show`\n> accepts any option known to `git diff`.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  Documentation/git-stash.txt | 4 ++--\n>  builtin/stash--helper.c     | 4 ++--\n>  2 files changed, 4 insertions(+), 4 deletions(-)\n> \n> diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt\n> index 7ef8c47911..e31ea7d303 100644\n> --- a/Documentation/git-stash.txt\n> +++ b/Documentation/git-stash.txt\n> @@ -9,7 +9,7 @@ SYNOPSIS\n>  --------\n>  [verse]\n>  'git stash' list [<options>]\n> -'git stash' show [<stash>]\n> +'git stash' show [<options>] [<stash>]\n>  'git stash' drop [-q|--quiet] [<stash>]\n>  'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]\n>  'git stash' branch <branchname> [<stash>]\n> @@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash\n>  The command takes options applicable to the 'git log'\n>  command to control what is shown and how. See linkgit:git-log[1].\n>  \n> -show [<stash>]::\n> +show [<options>] [<stash>]::\n>  \n>  \tShow the changes recorded in the stash entry as a diff between the\n>  \tstashed contents and the commit back when the stash entry was first\n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 1bc838ee6b..1f02f5f2e9 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -15,7 +15,7 @@\n>  \n>  static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper list [<options>]\"),\n> -\tN_(\"git stash--helper show [<stash>]\"),\n> +\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n>  \tN_(\"git stash--helper drop [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n> @@ -29,7 +29,7 @@ static const char * const git_stash_helper_list_usage[] = {\n>  };\n>  \n>  static const char * const git_stash_helper_show_usage[] = {\n> -\tN_(\"git stash--helper show [<stash>]\"),\n> +\tN_(\"git stash--helper show [<options>] [<stash>]\"),\n>  \tNULL\n>  };\n>  \n> -- \n> 2.19.0.rc0.23.g1fb9f40d88\n> \n"},{"id":"359462","messageId":"20181002201940.GH2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"2aa411b3c677f4a4cf0cba0850a96c1bd0ac2d40.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 15/21] stash: convert create to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-02T20:19:41Z","receivedAt":"2018-10-02T20:19:49Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 09/26, Paul-Sebastian Ungureanu wrote:\n> Add stash create to the helper.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 450 ++++++++++++++++++++++++++++++++++++++++\n>  git-stash.sh            |   2 +-\n>  2 files changed, 451 insertions(+), 1 deletion(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index b7421b68aa..49b05f2458 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -12,6 +12,9 @@\n>  #include \"rerere.h\"\n>  #include \"revision.h\"\n>  #include \"log-tree.h\"\n> +#include \"diffcore.h\"\n> +\n> +#define INCLUDE_ALL_FILES 2\n>  \n>  static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper list [<options>]\"),\n> @@ -63,6 +66,11 @@ static const char * const git_stash_helper_store_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_create_usage[] = {\n> +\tN_(\"git stash--helper create [<message>]\"),\n> +\tNULL\n> +};\n> +\n>  static const char *ref_stash = \"refs/stash\";\n>  static struct strbuf stash_index_path = STRBUF_INIT;\n>  \n> @@ -289,6 +297,24 @@ static int reset_head(void)\n>  \treturn run_command(&cp);\n>  }\n>  \n> +static void add_diff_to_buf(struct diff_queue_struct *q,\n> +\t\t\t    struct diff_options *options,\n> +\t\t\t    void *data)\n> +{\n> +\tint i;\n> +\n> +\tfor (i = 0; i < q->nr; i++) {\n> +\t\tstrbuf_addstr(data, q->queue[i]->one->path);\n> +\n> +\t\t/*\n> +\t\t * The reason we add \"0\" at the end of this strbuf\n> +\t\t * is because we will pass the output further to\n> +\t\t * \"git update-index -z ...\".\n> +\t\t */\n> +\t\tstrbuf_addch(data, 0);\n\nI'd find it slightly clearer to pass '\\0' instead of 0 here.  That\nmakes it immediately clear to the reader that we mean the NUL\ncharacter (even though it all ends up being the same in the end).\n\n> +\t}\n> +}\n> +\n>  static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)\n>  {\n>  \tstruct child_process cp = CHILD_PROCESS_INIT;\n> @@ -786,6 +812,428 @@ static int store_stash(int argc, const char **argv, const char *prefix)\n>  \treturn do_store_stash(&obj, stash_msg, quiet);\n>  }\n>  \n> +static void add_pathspecs(struct argv_array *args,\n> +\t\t\t\t       struct pathspec ps) {\n\nThis indentation looks a bit weird.\n\n> +\tint i;\n> +\n> +\tfor (i = 0; i < ps.nr; i++)\n> +\t\targv_array_push(args, ps.items[i].match);\n> +}\n> +\n> +/*\n> + * `untracked_files` will be filled with the names of untracked files.\n> + * The return value is:\n> + *\n> + * = 0 if there are not any untracked files\n> + * > 0 if there are untracked files\n> + */\n> +static int get_untracked_files(struct pathspec ps, int include_untracked,\n> +\t\t\t       struct strbuf *untracked_files)\n> +{\n> +\tint i;\n> +\tint max_len;\n> +\tint found = 0;\n> +\tchar *seen;\n> +\tstruct dir_struct dir;\n> +\n> +\tmemset(&dir, 0, sizeof(dir));\n> +\tif (include_untracked != INCLUDE_ALL_FILES)\n> +\t\tsetup_standard_excludes(&dir);\n> +\n> +\tseen = xcalloc(ps.nr, 1);\n> +\n> +\tmax_len = fill_directory(&dir, the_repository->index, &ps);\n> +\tfor (i = 0; i < dir.nr; i++) {\n> +\t\tstruct dir_entry *ent = dir.entries[i];\n> +\t\tif (dir_path_match(&the_index, ent, &ps, max_len, seen)) {\n> +\t\t\tfound++;\n> +\t\t\tstrbuf_addstr(untracked_files, ent->name);\n> +\t\t\t/* NUL-terminate: will be fed to update-index -z */\n> +\t\t\tstrbuf_addch(untracked_files, 0);\n> +\t\t}\n> +\t\tfree(ent);\n> +\t}\n> +\n> +\tfree(seen);\n> +\tfree(dir.entries);\n> +\tfree(dir.ignored);\n> +\tclear_directory(&dir);\n> +\treturn found;\n> +}\n> +\n> +/*\n> + * The return value of `check_changes()` can be:\n> + *\n> + * < 0 if there was an error\n> + * = 0 if there are no changes.\n> + * > 0 if there are changes.\n> + */\n> +static int check_changes(struct pathspec ps, int include_untracked)\n> +{\n> +\tint result;\n> +\tint ret = 0;\n\nThis variable is unused, so compilation with -Werror breaks at this\npatch.\n\n> +\tstruct rev_info rev;\n> +\tstruct object_id dummy;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t/* No initial commit. */\n> +\tif (get_oid(\"HEAD\", &dummy))\n> +\t\treturn -1;\n> +\n> +\tif (read_cache() < 0)\n> +\t\treturn -1;\n> +\n> +\tinit_revisions(&rev, NULL);\n> +\trev.prune_data = ps;\n> +\n> +\trev.diffopt.flags.quick = 1;\n> +\trev.diffopt.flags.ignore_submodules = 1;\n> +\trev.abbrev = 0;\n> +\n> +\tadd_head_to_pending(&rev);\n> +\tdiff_setup_done(&rev.diffopt);\n> +\n> +\tresult = run_diff_index(&rev, 1);\n> +\tif (diff_result_code(&rev.diffopt, result))\n> +\t\treturn 1;\n> +\n> +\tobject_array_clear(&rev.pending);\n> +\tresult = run_diff_files(&rev, 0);\n> +\tif (diff_result_code(&rev.diffopt, result))\n> +\t\treturn 1;\n> +\n> +\tif (include_untracked && get_untracked_files(ps, include_untracked,\n> +\t\t\t\t\t\t     &out)) {\n> +\t\tstrbuf_release(&out);\n> +\t\treturn 1;\n> +\t}\n> +\n> +\tstrbuf_release(&out);\n> +\treturn 0;\n> +}\n> +\n> +static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n> +\t\t\t\tstruct strbuf files)\n> +{\n> +\tint ret = 0;\n> +\tstruct strbuf untracked_msg = STRBUF_INIT;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> +\n> +\tcp_upd_index.git_cmd = 1;\n> +\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n> +\t\t\t \"--remove\", \"--stdin\", NULL);\n> +\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\n> +\tstrbuf_addf(&untracked_msg, \"untracked files on %s\\n\", msg->buf);\n> +\tif (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0, NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tcp_write_tree.git_cmd = 1;\n> +\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> +\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\tget_oid_hex(out.buf, &info->u_tree);\n> +\n> +\tif (commit_tree(untracked_msg.buf, untracked_msg.len,\n> +\t\t\t&info->u_tree, NULL, &info->u_commit, NULL, NULL)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +done:\n> +\tstrbuf_release(&untracked_msg);\n> +\tstrbuf_release(&out);\n> +\tremove_path(stash_index_path.buf);\n> +\treturn ret;\n> +}\n> +\n> +static int stash_patch(struct stash_info *info, struct pathspec ps,\n> +\t\t       struct strbuf *out_patch)\n> +{\n> +\tint ret = 0;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct child_process cp_read_tree = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_add_i = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n> +\n> +\tremove_path(stash_index_path.buf);\n> +\n> +\tcp_read_tree.git_cmd = 1;\n> +\targv_array_pushl(&cp_read_tree.args, \"read-tree\", \"HEAD\", NULL);\n> +\targv_array_pushf(&cp_read_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (run_command(&cp_read_tree)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\t/* Find out what the user wants. */\n> +\tcp_add_i.git_cmd = 1;\n> +\targv_array_pushl(&cp_add_i.args, \"add--interactive\", \"--patch=stash\",\n> +\t\t\t \"--\", NULL);\n> +\tadd_pathspecs(&cp_add_i.args, ps);\n> +\targv_array_pushf(&cp_add_i.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (run_command(&cp_add_i)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\t/* State of the working tree. */\n> +\tcp_write_tree.git_cmd = 1;\n> +\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> +\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tget_oid_hex(out.buf, &info->w_tree);\n> +\n> +\tcp_diff_tree.git_cmd = 1;\n> +\targv_array_pushl(&cp_diff_tree.args, \"diff-tree\", \"-p\", \"HEAD\",\n> +\t\t\t oid_to_hex(&info->w_tree), \"--\", NULL);\n> +\tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (!out_patch->len) {\n> +\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n> +\t\tret = 1;\n> +\t}\n> +\n> +done:\n> +\tstrbuf_release(&out);\n> +\tremove_path(stash_index_path.buf);\n> +\treturn ret;\n> +}\n> +\n> +static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n> +{\n> +\tint ret = 0;\n> +\tstruct rev_info rev;\n> +\tstruct child_process cp_upd_index = CHILD_PROCESS_INIT;\n> +\tstruct child_process cp_write_tree = CHILD_PROCESS_INIT;\n> +\tstruct strbuf out = STRBUF_INIT;\n> +\tstruct strbuf diff_output = STRBUF_INIT;\n> +\n> +\tset_alternate_index_output(stash_index_path.buf);\n> +\tif (reset_tree(&info->i_tree, 0, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\tset_alternate_index_output(NULL);\n> +\n> +\tgit_config(git_diff_basic_config, NULL);\n> +\tinit_revisions(&rev, NULL);\n> +\trev.prune_data = ps;\n> +\trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n> +\trev.diffopt.format_callback = add_diff_to_buf;\n> +\trev.diffopt.format_callback_data = &diff_output;\n> +\n> +\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit), \"\");\n> +\tif (run_diff_index(&rev, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tcp_upd_index.git_cmd = 1;\n> +\targv_array_pushl(&cp_upd_index.args, \"update-index\", \"-z\", \"--add\",\n> +\t\t\t \"--remove\", \"--stdin\", NULL);\n> +\targv_array_pushf(&cp_upd_index.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\n> +\tif (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,\n> +\t\t\t NULL, 0, NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tcp_write_tree.git_cmd = 1;\n> +\targv_array_push(&cp_write_tree.args, \"write-tree\");\n> +\targv_array_pushf(&cp_write_tree.env_array, \"GIT_INDEX_FILE=%s\",\n> +\t\t\t stash_index_path.buf);\n> +\tif (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tget_oid_hex(out.buf, &info->w_tree);\n> +\n> +done:\n> +\tUNLEAK(rev);\n> +\tstrbuf_release(&out);\n> +\tobject_array_clear(&rev.pending);\n> +\tstrbuf_release(&diff_output);\n> +\tremove_path(stash_index_path.buf);\n> +\treturn ret;\n> +}\n> +\n> +static int do_create_stash(struct pathspec ps, char **stash_msg,\n> +\t\t\t   int include_untracked, int patch_mode,\n> +\t\t\t   struct stash_info *info)\n> +{\n> +\tint ret = 0;\n> +\tint flags = 0;\n> +\tint untracked_commit_option = 0;\n> +\tconst char *head_short_sha1 = NULL;\n> +\tconst char *branch_ref = NULL;\n> +\tconst char *branch_name = \"(no branch)\";\n> +\tstruct commit *head_commit = NULL;\n> +\tstruct commit_list *parents = NULL;\n> +\tstruct strbuf msg = STRBUF_INIT;\n> +\tstruct strbuf commit_tree_label = STRBUF_INIT;\n> +\tstruct strbuf untracked_files = STRBUF_INIT;\n> +\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n> +\tstruct strbuf patch = STRBUF_INIT;\n> +\n> +\tread_cache_preload(NULL);\n> +\trefresh_cache(REFRESH_QUIET);\n> +\n> +\tif (get_oid(\"HEAD\", &info->b_commit)) {\n> +\t\tfprintf_ln(stderr, _(\"You do not have the initial commit yet\"));\n> +\t\tret = -1;\n> +\t\t*stash_msg = NULL;\n> +\t\tgoto done;\n> +\t} else {\n> +\t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n> +\t}\n> +\n> +\tif (!check_changes(ps, include_untracked)) {\n> +\t\tret = 1;\n> +\t\t*stash_msg = NULL;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tbranch_ref = resolve_ref_unsafe(\"HEAD\", 0, NULL, &flags);\n> +\tif (flags & REF_ISSYMREF)\n> +\t\tbranch_name = strrchr(branch_ref, '/') + 1;\n> +\thead_short_sha1 = find_unique_abbrev(&head_commit->object.oid,\n> +\t\t\t\t\t     DEFAULT_ABBREV);\n> +\tstrbuf_addf(&msg, \"%s: %s \", branch_name, head_short_sha1);\n> +\tpp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);\n> +\n> +\tstrbuf_addf(&commit_tree_label, \"index on %s\\n\", msg.buf);\n> +\tcommit_list_insert(head_commit, &parents);\n> +\tif (write_cache_as_tree(&info->i_tree, 0, NULL) ||\n> +\t    commit_tree(commit_tree_label.buf, commit_tree_label.len,\n> +\t\t\t&info->i_tree, parents, &info->i_commit, NULL, NULL)) {\n> +\t\tfprintf_ln(stderr, _(\"Cannot save the current index state\"));\n> +\t\tret = -1;\n> +\t\t*stash_msg = NULL;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (include_untracked && get_untracked_files(ps, include_untracked,\n> +\t\t\t\t\t\t     &untracked_files)) {\n> +\t\tif (save_untracked_files(info, &msg, untracked_files)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n> +\t\t\tret = -1;\n> +\t\t\t*stash_msg = NULL;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t\tuntracked_commit_option = 1;\n> +\t}\n> +\tif (patch_mode) {\n> +\t\tret = stash_patch(info, ps, &patch);\n> +\t\t*stash_msg = NULL;\n> +\t\tif (ret < 0) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n> +\t\t\tgoto done;\n> +\t\t} else if (ret > 0) {\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t} else {\n> +\t\tif (stash_working_tree(info, ps)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n> +\t\t\tret = -1;\n> +\t\t\t*stash_msg = NULL;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t}\n> +\n> +\tif (!*stash_msg || !strlen(*stash_msg))\n> +\t\tstrbuf_addf(&stash_msg_buf, \"WIP on %s\", msg.buf);\n> +\telse\n> +\t\tstrbuf_addf(&stash_msg_buf, \"On %s: %s\", branch_name,\n> +\t\t\t    *stash_msg);\n> +\t*stash_msg = strbuf_detach(&stash_msg_buf, NULL);\n> +\n> +\t/*\n> +\t * `parents` will be empty after calling `commit_tree()`, so there is\n> +\t * no need to call `free_commit_list()`\n> +\t */\n> +\tparents = NULL;\n> +\tif (untracked_commit_option)\n> +\t\tcommit_list_insert(lookup_commit(the_repository, &info->u_commit), &parents);\n> +\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit), &parents);\n> +\tcommit_list_insert(head_commit, &parents);\n> +\n> +\tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n> +\t\t\tparents, &info->w_commit, NULL, NULL)) {\n> +\t\tfprintf_ln(stderr, _(\"Cannot record working tree state\"));\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +done:\n> +\tstrbuf_release(&commit_tree_label);\n> +\tstrbuf_release(&msg);\n> +\tstrbuf_release(&untracked_files);\n> +\tstrbuf_release(&stash_msg_buf);\n> +\treturn ret;\n> +}\n> +\n> +static int create_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint include_untracked = 0;\n> +\tint ret = 0;\n> +\tchar *stash_msg = NULL;\n> +\tstruct stash_info info;\n> +\tstruct pathspec ps;\n> +\tstruct option options[] = {\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> +\t\t\t N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_create_usage,\n> +\t\t\t     0);\n> +\n> +\tmemset(&ps, 0, sizeof(ps));\n> +\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);\n> +\n> +\tif (!ret)\n> +\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n> +\n> +\t/*\n> +\t * ret can be 1 if there were no changes. In this case, we should\n> +\t * not error out.\n> +\t */\n> +\tfree((char *) stash_msg);\n\nstash_msg is already of type 'char *', so the cast here is unnecessary.\n\n> +\treturn ret < 0;\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -822,6 +1270,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!show_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"store\"))\n>  \t\treturn !!store_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"create\"))\n> +\t\treturn !!create_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index 5739c51527..ab06e4ffb8 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -425,7 +425,7 @@ clear)\n>  \t;;\n>  create)\n>  \tshift\n> -\tcreate_stash -m \"$*\" && echo \"$w_commit\"\n> +\tgit stash--helper create --message \"$*\"\n>  \t;;\n>  store)\n>  \tshift\n> -- \n> 2.19.0.rc0.23.g1fb9f40d88\n> \n"},{"id":"359465","messageId":"20181002203730.GI2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"6636ecc6b2a4da8dcde00d580455ea2911dbb218.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 16/21] stash: convert push to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-02T20:37:30Z","receivedAt":"2018-10-02T20:37:36Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 09/26, Paul-Sebastian Ungureanu wrote:\n> Add stash push to the helper.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  builtin/stash--helper.c | 244 +++++++++++++++++++++++++++++++++++++++-\n>  git-stash.sh            |   6 +-\n>  2 files changed, 244 insertions(+), 6 deletions(-)\n> \n> diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n> index 49b05f2458..d79233d7ec 100644\n> --- a/builtin/stash--helper.c\n> +++ b/builtin/stash--helper.c\n> @@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {\n>  \tN_(\"git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]\"),\n>  \tN_(\"git stash--helper branch <branchname> [<stash>]\"),\n>  \tN_(\"git stash--helper clear\"),\n> +\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n> +\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n> +\t   \"          [--] [<pathspec>...]]\"),\n>  \tNULL\n>  };\n>  \n> @@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {\n>  \tNULL\n>  };\n>  \n> +static const char * const git_stash_helper_push_usage[] = {\n> +\tN_(\"git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\\n\"\n> +\t   \"          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\\n\"\n> +\t   \"          [--] [<pathspec>...]]\"),\n> +\tNULL\n> +};\n> +\n>  static const char *ref_stash = \"refs/stash\";\n>  static struct strbuf stash_index_path = STRBUF_INIT;\n>  \n> @@ -1088,7 +1098,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n>  \n>  static int do_create_stash(struct pathspec ps, char **stash_msg,\n>  \t\t\t   int include_untracked, int patch_mode,\n> -\t\t\t   struct stash_info *info)\n> +\t\t\t   struct stash_info *info, struct strbuf *patch)\n>  {\n>  \tint ret = 0;\n>  \tint flags = 0;\n> @@ -1102,7 +1112,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n>  \tstruct strbuf commit_tree_label = STRBUF_INIT;\n>  \tstruct strbuf untracked_files = STRBUF_INIT;\n>  \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n> -\tstruct strbuf patch = STRBUF_INIT;\n>  \n>  \tread_cache_preload(NULL);\n>  \trefresh_cache(REFRESH_QUIET);\n> @@ -1152,7 +1161,7 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n>  \t\tuntracked_commit_option = 1;\n>  \t}\n>  \tif (patch_mode) {\n> -\t\tret = stash_patch(info, ps, &patch);\n> +\t\tret = stash_patch(info, ps, patch);\n>  \t\t*stash_msg = NULL;\n>  \t\tif (ret < 0) {\n>  \t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n> @@ -1221,7 +1230,8 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n>  \t\t\t     0);\n>  \n>  \tmemset(&ps, 0, sizeof(ps));\n> -\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info);\n> +\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info,\n> +\t\t\t      NULL);\n>  \n>  \tif (!ret)\n>  \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n> @@ -1234,6 +1244,230 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n>  \treturn ret < 0;\n>  }\n>  \n> +static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n> +\t\t\t int keep_index, int patch_mode, int include_untracked)\n> +{\n> +\tint ret = 0;\n> +\tstruct stash_info info;\n> +\tstruct strbuf patch = STRBUF_INIT;\n> +\n> +\tif (patch_mode && keep_index == -1)\n> +\t\tkeep_index = 1;\n> +\n> +\tif (patch_mode && include_untracked) {\n> +\t\tfprintf_ln(stderr, _(\"Can't use --patch and --include-untracked\"\n> +\t\t\t\t     \" or --all at the same time\"));\n> +\t\tret = -1;\n\nWe should set \"stash_msg\" to NULL here, otherwise we'll get an invalid\nfree if these options are used together and a message is given.\n\nIn general I find this API and the do_create_stash API a bit\ncumbersome to use.  Maybe it would help if we'd 'xstrdup' the string\nbefore passing it in, so we can free it unconditionally, without\nsetting it to NULL everywhere.  As it's only a single strdup for each\ncommand that's run that isn't very costly compared to what else we're\ndoing here, and I think the readability we're gaining would be worth\nit.\n\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tread_cache_preload(NULL);\n> +\tif (!include_untracked && ps.nr) {\n> +\t\tint i;\n> +\t\tchar *ps_matched = xcalloc(ps.nr, 1);\n> +\n> +\t\tfor (i = 0; i < active_nr; i++)\n> +\t\t\tce_path_match(&the_index, active_cache[i], &ps,\n> +\t\t\t\t      ps_matched);\n> +\n> +\t\tif (report_path_error(ps_matched, &ps, NULL)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Did you forget to 'git add'?\"));\n> +\t\t\tstash_msg = NULL;\n> +\t\t\tret = -1;\n\n'ps_matched' is not being free'd in this error case.\n\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t\tfree(ps_matched);\n> +\t}\n> +\n> +\tif (refresh_cache(REFRESH_QUIET)) {\n> +\t\tstash_msg = NULL;\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (!check_changes(ps, include_untracked)) {\n> +\t\tstash_msg = NULL;\n> +\t\tif (!quiet)\n> +\t\t\tprintf_ln(_(\"No local changes to save\"));\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (!reflog_exists(ref_stash) && do_clear_stash()) {\n> +\t\tstash_msg = NULL;\n> +\t\tret = -1;\n> +\t\tfprintf_ln(stderr, _(\"Cannot initialize stash\"));\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,\n> +\t\t\t    &info, &patch)) {\n> +\t\tret = -1;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tif (do_store_stash(&info.w_commit, stash_msg, 1)) {\n> +\t\tret = -1;\n> +\t\tfprintf_ln(stderr, _(\"Cannot save the current status\"));\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tprintf_ln(_(\"Saved working directory and index state %s\"), stash_msg);\n> +\n> +\tif (!patch_mode) {\n> +\t\tif (include_untracked && !ps.nr) {\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"clean\", \"--force\",\n> +\t\t\t\t\t \"--quiet\", \"-d\", NULL);\n> +\t\t\tif (include_untracked == INCLUDE_ALL_FILES)\n> +\t\t\t\targv_array_push(&cp.args, \"-x\");\n> +\t\t\tif (run_command(&cp)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\t\tif (ps.nr) {\n> +\t\t\tstruct child_process cp_add = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp_diff = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp_apply = CHILD_PROCESS_INIT;\n> +\t\t\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t\t\tcp_add.git_cmd = 1;\n> +\t\t\targv_array_push(&cp_add.args, \"add\");\n> +\t\t\tif (!include_untracked)\n> +\t\t\t\targv_array_push(&cp_add.args, \"-u\");\n> +\t\t\tif (include_untracked == INCLUDE_ALL_FILES)\n> +\t\t\t\targv_array_push(&cp_add.args, \"--force\");\n> +\t\t\targv_array_push(&cp_add.args, \"--\");\n> +\t\t\tadd_pathspecs(&cp_add.args, ps);\n> +\t\t\tif (run_command(&cp_add)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp_diff.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp_diff.args, \"diff-index\", \"-p\",\n> +\t\t\t\t\t \"--cached\", \"--binary\", \"HEAD\", \"--\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tadd_pathspecs(&cp_diff.args, ps);\n> +\t\t\tif (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp_apply.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp_apply.args, \"apply\", \"--index\",\n> +\t\t\t\t\t \"-R\", NULL);\n> +\t\t\tif (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,\n> +\t\t\t\t\t NULL, 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t} else {\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"reset\", \"--hard\", \"-q\",\n> +\t\t\t\t\t NULL);\n> +\t\t\tif (run_command(&cp)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\n> +\t\tif (keep_index == 1 && !is_null_oid(&info.i_tree)) {\n> +\t\t\tstruct child_process cp_ls = CHILD_PROCESS_INIT;\n> +\t\t\tstruct child_process cp_checkout = CHILD_PROCESS_INIT;\n> +\t\t\tstruct strbuf out = STRBUF_INIT;\n> +\n> +\t\t\tif (reset_tree(&info.i_tree, 0, 1)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp_ls.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp_ls.args, \"ls-files\", \"-z\",\n> +\t\t\t\t\t \"--modified\", \"--\", NULL);\n> +\n> +\t\t\tadd_pathspecs(&cp_ls.args, ps);\n> +\t\t\tif (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\n> +\t\t\tcp_checkout.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp_checkout.args, \"checkout-index\",\n> +\t\t\t\t\t \"-z\", \"--force\", \"--stdin\", NULL);\n> +\t\t\tif (pipe_command(&cp_checkout, out.buf, out.len, NULL,\n> +\t\t\t\t\t 0, NULL, 0)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\t\tgoto done;\n> +\t} else {\n> +\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\tcp.git_cmd = 1;\n> +\t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n> +\n> +\t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n> +\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n> +\t\t\tret = -1;\n> +\t\t\tgoto done;\n> +\t\t}\n> +\n> +\t\tif (keep_index < 1) {\n> +\t\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n> +\n> +\t\t\tcp.git_cmd = 1;\n> +\t\t\targv_array_pushl(&cp.args, \"reset\", \"-q\", \"--\", NULL);\n> +\t\t\tadd_pathspecs(&cp.args, ps);\n> +\t\t\tif (run_command(&cp)) {\n> +\t\t\t\tret = -1;\n> +\t\t\t\tgoto done;\n> +\t\t\t}\n> +\t\t}\n> +\t\tgoto done;\n> +\t}\n> +\n> +done:\n> +\tfree(stash_msg);\n> +\treturn ret;\n> +}\n> +\n> +static int push_stash(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint keep_index = -1;\n> +\tint patch_mode = 0;\n> +\tint include_untracked = 0;\n> +\tint quiet = 0;\n> +\tchar *stash_msg = NULL;\n> +\tstruct pathspec ps;\n> +\tstruct option options[] = {\n> +\t\tOPT_BOOL('k', \"keep-index\", &keep_index,\n> +\t\t\t N_(\"keep index\")),\n> +\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n> +\t\t\t N_(\"stash in patch mode\")),\n> +\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n> +\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n> +\t\t\t N_(\"include untracked files in stash\")),\n> +\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n> +\t\t\t    N_(\"include ignore files\"), 2),\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n> +\t\t\t   N_(\"stash message\")),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_stash_helper_push_usage,\n> +\t\t\t     0);\n> +\n> +\tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n> +\treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n> +\t\t\t     include_untracked);\n> +}\n> +\n>  int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  {\n>  \tpid_t pid = getpid();\n> @@ -1272,6 +1506,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!store_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"create\"))\n>  \t\treturn !!create_stash(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"push\"))\n> +\t\treturn !!push_stash(argc, argv, prefix);\n>  \n>  \tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n>  \t\t      git_stash_helper_usage, options);\n> diff --git a/git-stash.sh b/git-stash.sh\n> index ab06e4ffb8..c3146f62ab 100755\n> --- a/git-stash.sh\n> +++ b/git-stash.sh\n> @@ -412,7 +412,8 @@ save)\n>  \t;;\n>  push)\n>  \tshift\n> -\tpush_stash \"$@\"\n> +\tcd \"$START_DIR\"\n> +\tgit stash--helper push \"$@\"\n>  \t;;\n>  apply)\n>  \tshift\n> @@ -448,7 +449,8 @@ branch)\n>  *)\n>  \tcase $# in\n>  \t0)\n> -\t\tpush_stash &&\n> +\t\tcd \"$START_DIR\"\n> +\t\tgit stash--helper push &&\n>  \t\tsay \"$(gettext \"(To restore them type \\\"git stash apply\\\")\")\"\n>  \t\t;;\n>  \t*)\n> -- \n> 2.19.0.rc0.23.g1fb9f40d88\n> \n"},{"id":"359469","messageId":"20181002210424.GJ2253@hank.intra.tgummerer.com","threadId":"48222","inReplyTo":"2e6b0734c0185053733c0aa47d4a618cf054c67e.1537913094.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v9 19/21] stash: convert `stash--helper.c` into `stash.c`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-02T21:04:24Z","receivedAt":"2018-10-02T21:04:30Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 09/26, Paul-Sebastian Ungureanu wrote:\n> The old shell script `git-stash.sh`  was removed and replaced\n> entirely by `builtin/stash.c`. In order to do that, `create` and\n> `push` were adapted to work without `stash.sh`. For example, before\n> this commit, `git stash create` called `git stash--helper create\n> --message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\n> some of these changes wouldn't have been necessary.\n> \n> This commit also removes the word `helper` since now stash is\n> called directly and not by a shell script.\n> \n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  .gitignore                           |   1 -\n>  Makefile                             |   3 +-\n>  builtin.h                            |   2 +-\n>  builtin/{stash--helper.c => stash.c} | 162 ++++++++++++++++-----------\n>  git-stash.sh                         | 153 -------------------------\n>  git.c                                |   2 +-\n>  6 files changed, 98 insertions(+), 225 deletions(-)\n>  rename builtin/{stash--helper.c => stash.c} (90%)\n>  delete mode 100755 git-stash.sh\n>\n> [...]\n>\n> @@ -1571,7 +1562,44 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n>  \t\treturn !!push_stash(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"save\"))\n>  \t\treturn !!save_stash(argc, argv, prefix);\n> +\telse if (*argv[0] != '-')\n> +\t\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n> +\t\t\t      git_stash_usage, options);\n> +\n> +\tif (strcmp(argv[0], \"-p\")) {\n> +\t\twhile (++i < argc && strcmp(argv[i], \"--\")) {\n> +\t\t\t/*\n> +\t\t\t * `akpqu` is a string which contains all short options,\n> +\t\t\t * except `-m` which is verified separately.\n> +\t\t\t */\n> +\t\t\tif ((strlen(argv[i]) == 2) && *argv[i] == '-' &&\n> +\t\t\t    strchr(\"akpqu\", argv[i][1]))\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\tif (!strcmp(argv[i], \"--all\") ||\n> +\t\t\t    !strcmp(argv[i], \"--keep-index\") ||\n> +\t\t\t    !strcmp(argv[i], \"--no-keep-index\") ||\n> +\t\t\t    !strcmp(argv[i], \"--patch\") ||\n> +\t\t\t    !strcmp(argv[i], \"--quiet\") ||\n> +\t\t\t    !strcmp(argv[i], \"--include-untracked\"))\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\t/*\n> +\t\t\t * `-m` and `--message=` are verified separately because\n> +\t\t\t * they need to be immediately followed by a string\n> +\t\t\t * (i.e.`-m\"foobar\"` or `--message=\"foobar\"`).\n> +\t\t\t */\n> +\t\t\tif ((strlen(argv[i]) > 2 &&\n> +\t\t\t     !strncmp(argv[i], \"-m\", 2)) ||\n> +\t\t\t    (strlen(argv[i]) > 10 &&\n> +\t\t\t     !strncmp(argv[i], \"--message=\", 10)))\n\nThese 'strlen && !strncmp' calls could be replaced with\n'starts_with()'.\n\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\tusage_with_options(git_stash_usage, options);\n> +\t\t}\n> +\t}\n\nThis is a bit more complex than what we used to have, which was just\n\"if it starts with a \"-\" it's an option\", but I don't think it hurts\nbeing more explicit here either.\n\n>  \n> -\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n> -\t\t      git_stash_helper_usage, options);\n> +\targv_array_push(&args, \"push\");\n> +\targv_array_pushv(&args, argv);\n> +\treturn !!push_stash(args.argc, args.argv, prefix);\n>  }\n"},{"id":"362823","messageId":"nycvar.QRO.7.76.6.1811091324210.39@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"4de1b725-8687-590a-f93f-e80843e5bf8f@gmail.com","subject":"Re: [GSoC][PATCH v8 18/20] stash: convert `stash--helper.c` into `stash.c`","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-11-09T12:26:14Z","receivedAt":"2018-11-09T12:26:18Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Wed, 26 Sep 2018, Paul-Sebastian Ungureanu wrote:\n\n> Hi,\n> \n> > >   @@ -1443,9 +1448,10 @@ static int push_stash(int argc, const char\n> > > **argv, const char *prefix)\n> > >    \tOPT_END()\n> > >    };\n> > >   -\targc = parse_options(argc, argv, prefix, options,\n> > > -\t\t\t     git_stash_helper_push_usage,\n> > > -\t\t\t     0);\n> > > +\tif (argc)\n> > > +\t\targc = parse_options(argc, argv, prefix, options,\n> > > +\t\t\t\t     git_stash_push_usage,\n> > > +\t\t\t\t     0);\n> > \n> > Is this `if (argc)` guard necessary?\n> \n> Yes because without it, there would be a seg fault when\n> calling `git stash`. Why?\n> \n> After spawning `git stash`, in `push_stash()`: `argc` would be\n> 0 (and `argv` would be `NULL`).\n\nI *think* what you want to do, then, is to pass PARSE_OPT_KEEP_ARGV0 in\nthe first parse_options() call. In general, this needs to be done any time\nyou might want to call `parse_options()` on the remaining options.\n\nLooking forward to v10,\nDscho\n\n> `parse_options()` calls `parse_options_start()` which does the following:\n> \n> \tctx->argc = ctx->total = argc - 1;\n> \tctx->argv = argv + 1;\n> \n> So, `ctx->argc` would be `-1`. After we are back to `parse_options()`,\n> `parse_options_step()` would be called. In `parse_options_step()`:\n> \n> \tfor (; ctx->argc; ctx->argc--, ctx->argv++)\n> \n> Which is an infinite loop, because `ctx->argc` is already -1.\n> \n> This check is also done for `git notes list` (function `list()`\n> inside `builtin/notes.c`). Now, that I relook at it, it seems to me\n> that this is a bug. Probably a better solution would be to check at the\n> beginning of `parse_options()` and return early if `argc` is less then one.\n> \n> > > @@ -1536,7 +1544,44 @@ int cmd_stash__helper(int argc, const char **argv,\n> > > const char *prefix)\n> > >    \treturn !!push_stash(argc, argv, prefix);\n> > >    else if (!strcmp(argv[0], \"save\"))\n> > >   \t\treturn !!save_stash(argc, argv, prefix);\n> > > +\telse if (*argv[0] != '-')\n> > > +\t\tusage_msg_opt(xstrfmt(_(\"unknown subcommand: %s\"), argv[0]),\n> > > +\t\t\t      git_stash_usage, options);\n> > > +\n> > > +\tif (strcmp(argv[0], \"-p\")) {\n> > > +\t\twhile (++i < argc && strcmp(argv[i], \"--\")) {\n> > > +\t\t\t/*\n> > > +\t\t\t * `akpqu` is a string which contains all short\n> > > options,\n> > > +\t\t\t * except `-m` which is verified separately.\n> > > +\t\t\t */\n> > > +\t\t\tif ((strlen(argv[i]) == 2) && *argv[i] == '-' &&\n> > > +\t\t\t    strchr(\"akpqu\", argv[i][1]))\n> > \n> > I *think* this is missing the `n`.\n> \n> I guess that by `n` you are referring to the short option of\n> `--no-keep-index`, which is missing because it was also omitted\n> in `stash.sh`. I am not sure whether it is worth adding it. In\n> case `stash` will learn any other option starting with `n`, this\n> might create confusion amongst users.\n> \n> Best regards,\n> Paul\n> \n"},{"id":"362824","messageId":"nycvar.QRO.7.76.6.1811091326500.39@tvgsbejvaqbjf.bet","threadId":"48222","inReplyTo":"5ba50c37-2840-e1f9-4d35-877979018f1c@gmail.com","subject":"Re: [GSoC][PATCH v8 14/20] stash: convert create to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-11-09T12:27:53Z","receivedAt":"2018-11-09T12:27:57Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Wed, 26 Sep 2018, Paul-Sebastian Ungureanu wrote:\n\n> Sorry for the late reply. I had a lot on my plate for the last couple of\n> weeks.\n\nI understand. University can be busy times.\n\n> > > +\n> > > +\tgit_config(git_diff_basic_config, NULL);\n> > \n> > Is this not called in as part of `git_config(git_default_config, NULL);`\n> > in cmd_stash() already?\n> > \n> > *clicketyclick*\n> > \n> > I guess not. But then, maybe it would make sense to run with\n> > `git_diff_basic_config` from the get go, to avoid having to run\n> > `git_config()` twice.\n> \n> I am not sure I got it right, but if I did:\n> \n> Running `git_config` with `git_diff_basic_config` from the\n> beginning wouldn't be pointless when we would use any other\n> command than `create`, `push` and `save`? Although it might\n> confuse the reader a little bit, the stash should run without\n> problems.\n\nIn my mind, this would simplify the code. Which is always easier on the\nreader... ;-)\n\nCiao,\nDscho\n"}]}