{"thread":{"id":"49563","subject":"[PATCH v10 00/21] Convert \"git stash\" to C builtin","startedAt":"2018-10-14T22:13:14Z","lastAt":"2018-10-16T19:59:55Z","messageCount":30,"participants":["Paul-Sebastian Ungureanu","Johannes Schindelin","Thomas Gummerer","Junio C Hamano"],"isPatch":true,"patchVersion":10,"patchTotal":21},"messages":[{"id":"360412","messageId":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"https://public-inbox.org/git/cover.1537913094.git.ungureanupaulsebastian@gmail.com/","subject":"[PATCH v10 00/21] Convert \"git stash\" to C builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:06Z","receivedAt":"2018-10-14T22:13:14Z","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.\nThis iteration fixes some code styling issues, bring some changes\nto `do_push_stash()` and `do_create_stash()` to be closer to API by\nfollowing Thomas Gummerer's review of last iteration [1]. Also, there\nwere some missing messages [2], which are now included.\n\n[1]\nhttps://public-inbox.org/git/20181002203730.GI2253@hank.intra.tgummerer.com/\n\n[2]\nhttps://github.com/git-for-windows/git/commit/2af24038a95a3879aa0c29d91a43180b9465247e\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  t3903: modernize style\n  stash: rename test cases to be more descriptive\n  stash: add tests for `git stash show` config\n  stash: mention options in `show` synopsis\n  stash: convert list to builtin\n  stash: convert show to builtin\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              | 1605 ++++++++++++++++++++++++++++++++++\n cache.h                      |    1 +\n git-stash.sh                 |  752 ----------------\n git.c                        |    1 +\n sha1-name.c                  |   19 +\n strbuf.c                     |   13 +\n strbuf.h                     |    7 +\n t/t3903-stash.sh             |  192 ++--\n t/t3907-stash-show-config.sh |   83 ++\n 12 files changed, 1859 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.g10a62394e7\n\n"},{"id":"360413","messageId":"b21cdeb1636b679d80ff554c48bc76516aed2cf6.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 01/21] sha1-name.c: add `get_oidf()` which acts like `get_oid()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:07Z","receivedAt":"2018-10-14T22:13:14Z","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.g10a62394e7\n\n"},{"id":"360414","messageId":"ed065178766fd1f1f5856422af0db785dde40f40.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 02/21] strbuf.c: add `strbuf_join_argv()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:08Z","receivedAt":"2018-10-14T22:13:15Z","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 | 13 +++++++++++++\n strbuf.h |  7 +++++++\n 2 files changed, 20 insertions(+)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex 64041c3c24..c8a104099a 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -259,6 +259,19 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)\n \tstrbuf_setlen(sb, sb->len + sb2->len);\n }\n \n+void strbuf_join_argv(struct strbuf *buf,\n+\t\t      int argc, const char **argv, char delim)\n+{\n+\tif (!argc)\n+\t\treturn;\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+\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..4ec912f4b7 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+ * Join the arguments into a buffer. `delim` is put between every\n+ * two arguments.\n+ */\n+extern void strbuf_join_argv(struct strbuf *buf, int argc,\n+\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.g10a62394e7\n\n"},{"id":"360416","messageId":"d48b36de7adca9f4e498a88a9909ab1dc2a3e7c5.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 03/21] stash: improve option parsing test coverage","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:09Z","receivedAt":"2018-10-14T22:13:17Z","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.g10a62394e7\n\n"},{"id":"360417","messageId":"c71313a697462a654a0a6bda6bda20b378ffdeda.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 04/21] t3903: modernize style","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:10Z","receivedAt":"2018-10-14T22:13:17Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Remove whitespaces after redirection operators and wrap\nlong lines.\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.g10a62394e7\n\n"},{"id":"360415","messageId":"ba1f9c1b1d36241047a915ef186e038d14128afb.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 05/21] stash: rename test cases to be more descriptive","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:11Z","receivedAt":"2018-10-14T22:13:18Z","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.g10a62394e7\n\n"},{"id":"360418","messageId":"60d86c0637da9e3fee99b56377a0f54712473d24.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 06/21] stash: add tests for `git stash show` config","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:12Z","receivedAt":"2018-10-14T22:13:19Z","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.g10a62394e7\n\n"},{"id":"360419","messageId":"97e3fef686fdd1659f193819ec8c14bf753aa7cb.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 09/21] stash: convert drop and clear to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:15Z","receivedAt":"2018-10-14T22:13:22Z","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 | 117 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   4 +-\n 2 files changed, 119 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex c43e0aacbb..aff6cda4c9 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,32 @@ 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 \"\n+\t\t\t       \"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@@ -426,6 +464,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@@ -448,6 +561,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.g10a62394e7\n\n"},{"id":"360420","messageId":"ac293e2a43d792447dd810f96614a0ba2ea5827f.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 07/21] stash: mention options in `show` synopsis","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:13Z","receivedAt":"2018-10-14T22:13:22Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Mention in the documentation, that `show` accepts any\noption known to `git diff`.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n Documentation/git-stash.txt | 4 ++--\n 1 file changed, 2 insertions(+), 2 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\n-- \n2.19.0.rc0.23.g10a62394e7\n\n"},{"id":"360421","messageId":"ab4ac449dd1b2722248d5b542c0b4cb1874306f6.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 10/21] stash: convert branch to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:16Z","receivedAt":"2018-10-14T22:13:23Z","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 aff6cda4c9..c41aad7036 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@@ -539,6 +545,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@@ -565,6 +609,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.g10a62394e7\n\n"},{"id":"360423","messageId":"192cba75acb1a65bf122ded65588a3a8e273be86.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 11/21] stash: convert pop to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:17Z","receivedAt":"2018-10-14T22:13: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 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 | 39 +++++++++++++++++++++++++++++++++-\n git-stash.sh            | 47 ++---------------------------------------\n 2 files changed, 40 insertions(+), 46 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex c41aad7036..33f4e83353 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@@ -545,6 +550,36 @@ 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 \"\n+\t\t\t    \"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@@ -609,6 +644,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.g10a62394e7\n\n"},{"id":"360422","messageId":"1b76e429041d7b05678f871607b86cb7a0a7d4df.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 12/21] stash: convert list to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:18Z","receivedAt":"2018-10-14T22:13:26Z","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 33f4e83353..a1249f6b76 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@@ -618,6 +624,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@@ -648,6 +677,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.g10a62394e7\n\n"},{"id":"360424","messageId":"e748881a4c9c1cebcd4034f6092b6b575d846c8b.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 13/21] stash: convert show to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:19Z","receivedAt":"2018-10-14T22:13:26Z","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 a1249f6b76..3f7ace92f5 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 [<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@@ -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 [<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 \tNULL\n@@ -647,6 +655,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@@ -679,6 +764,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.g10a62394e7\n\n"},{"id":"360425","messageId":"e8b49b0dc66380b047f06e41748cccfa136f8a18.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 14/21] stash: convert store to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:20Z","receivedAt":"2018-10-14T22:13:28Z","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 | 62 +++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            | 43 ++--------------------------\n 2 files changed, 64 insertions(+), 41 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 3f7ace92f5..758a32572d 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@@ -732,6 +737,61 @@ 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+\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_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 \"\n+\t\t\t\t\t     \"<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@@ -766,6 +826,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.g10a62394e7\n\n"},{"id":"360426","messageId":"b9c3f499ba9b9162477b04200f4a26bb1a7a3497.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 15/21] stash: convert create to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:21Z","receivedAt":"2018-10-14T22:13:29Z","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 | 458 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |   2 +-\n 2 files changed, 459 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 758a32572d..b8e69c1050 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@@ -290,6 +298,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@@ -792,6 +818,436 @@ 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  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+\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,\n+\t\t\t 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+\t\t\t   \"\");\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 \"\n+\t\t\t\t\t     \"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 \"\n+\t\t\t\t\t     \"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 \"\n+\t\t\t\t\t     \"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,\n+\t\t\t\t\t\t &info->u_commit),\n+\t\t\t\t   &parents);\n+\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit),\n+\t\t\t   &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+\tfree(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+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -828,6 +1284,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.g10a62394e7\n\n"},{"id":"360427","messageId":"efb1f1bba1aec287bf79452477611fa4b042b280.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 16/21] stash: convert push to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:22Z","receivedAt":"2018-10-14T22:13:32Z","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 | 241 +++++++++++++++++++++++++++++++++++++++-\n git-stash.sh            |   6 +-\n 2 files changed, 241 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex b8e69c1050..23f1329637 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@@ -1095,7 +1105,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@@ -1109,7 +1119,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@@ -1160,7 +1169,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 \"\n@@ -1234,7 +1243,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@@ -1248,6 +1258,227 @@ 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\tret = -1;\n+\t\t\tfree(ps_matched);\n+\t\t\tgoto done;\n+\t\t}\n+\t\tfree(ps_matched);\n+\t}\n+\n+\tif (refresh_cache(REFRESH_QUIET)) {\n+\t\tret = -1;\n+\t\tgoto done;\n+\t}\n+\n+\tif (!check_changes(ps, include_untracked)) {\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\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, xstrdup_or_null(stash_msg), quiet,\n+\t\t\t     keep_index, patch_mode, include_untracked);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1286,6 +1517,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.g10a62394e7\n\n"},{"id":"360428","messageId":"171db39a89b57a907b0baa3f6d783c6dab62806c.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 20/21] stash: optimize `get_untracked_files()` and `check_changes()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:26Z","receivedAt":"2018-10-14T22:13:33Z","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 | 50 ++++++++++++++++++++++++++++---------------------\n 1 file changed, 29 insertions(+), 21 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex e945c13c42..d2365ada2e 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -881,18 +881,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 \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@@ -920,14 +920,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@@ -1138,7 +1150,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\tgoto done;\n \t}\n@@ -1163,8 +1175,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 \"\n@@ -1244,19 +1255,15 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \tstash_msg = stash_msg_buf.buf;\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 \tfree(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 int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n@@ -1265,6 +1272,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@@ -1299,7 +1307,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\tif (!quiet)\n \t\t\tprintf_ln(_(\"No local changes to save\"));\n \t\tgoto done;\n-- \n2.19.0.rc0.23.g10a62394e7\n\n"},{"id":"360429","messageId":"10a62394e78991259425832e9af0656bbada4c45.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 21/21] stash: replace all `write-tree` child processes with API calls","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:27Z","receivedAt":"2018-10-14T22:13:35Z","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 d2365ada2e..651d05c820 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -947,9 +947,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@@ -964,15 +963,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@@ -981,8 +976,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@@ -991,11 +986,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@@ -1021,17 +1015,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@@ -1047,7 +1036,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@@ -1057,9 +1046,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@@ -1099,20 +1087,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.g10a62394e7\n\n"},{"id":"360430","messageId":"ea8814762c75a3387b9e697f62ea6122b5a1d014.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 17/21] stash: make push -q quiet","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:23Z","receivedAt":"2018-10-14T22:13:36Z","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. Add tests for `--quiet`.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 55 +++++++++++++++++++++++++++--------------\n t/t3903-stash.sh        | 23 +++++++++++++++++\n 2 files changed, 59 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 23f1329637..befb944871 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -973,7 +973,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@@ -1026,7 +1026,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@@ -1105,7 +1106,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@@ -1124,7 +1126,9 @@ 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 \"\n+\t\t\t\t\t     \"the initial commit yet\"));\n \t\tret = -1;\n \t\t*stash_msg = NULL;\n \t\tgoto done;\n@@ -1151,7 +1155,9 @@ 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 \"\n+\t\t\t\t\t     \"index state\"));\n \t\tret = -1;\n \t\t*stash_msg = NULL;\n \t\tgoto done;\n@@ -1160,8 +1166,9 @@ 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 \"\n-\t\t\t\t\t     \"the untracked files\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n+\t\t\t\t\t\t     \"the untracked files\"));\n \t\t\tret = -1;\n \t\t\t*stash_msg = NULL;\n \t\t\tgoto done;\n@@ -1169,19 +1176,21 @@ 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 \"\n-\t\t\t\t\t     \"worktree state\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n+\t\t\t\t\t\t     \"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 \"\n-\t\t\t\t\t     \"worktree state\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n+\t\t\t\t\t\t     \"worktree state\"));\n \t\t\tret = -1;\n \t\t\t*stash_msg = NULL;\n \t\t\tgoto done;\n@@ -1210,7 +1219,9 @@ 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 \"\n+\t\t\t\t\t     \"working tree state\"));\n \t\tret = -1;\n \t\tgoto done;\n \t}\n@@ -1244,7 +1255,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@@ -1306,23 +1317,27 @@ static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,\n \n \tif (!reflog_exists(ref_stash) && do_clear_stash()) {\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@@ -1423,7 +1438,9 @@ 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 \"\n+\t\t\t\t\t\t     \"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.g10a62394e7\n\n"},{"id":"360431","messageId":"004f2b2e3e8c4d1069a808034f3d6fe16853085c.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 19/21] stash: convert `stash--helper.c` into `stash.c`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:25Z","receivedAt":"2018-10-14T22:13:36Z","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} | 166 +++++++++++++++------------\n git-stash.sh                         | 153 ------------------------\n git.c                                |   2 +-\n 6 files changed, 96 insertions(+), 231 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 a0413f5d00..e945c13c42 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@@ -528,7 +523,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@@ -601,7 +596,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@@ -627,7 +622,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@@ -654,7 +649,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@@ -689,7 +684,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@@ -769,7 +764,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@@ -815,7 +810,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@@ -1138,7 +1133,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\t\tfprintf_ln(stderr, _(\"You do not have \"\n \t\t\t\t\t     \"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@@ -1146,7 +1140,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\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@@ -1167,7 +1160,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t     \"index state\"));\n \t\tret = -1;\n-\t\t*stash_msg = NULL;\n \t\tgoto done;\n \t}\n \n@@ -1178,14 +1170,12 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n \t\t\t\t\t\t     \"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, quiet);\n-\t\t*stash_msg = NULL;\n \t\tif (ret < 0) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n@@ -1200,7 +1190,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"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@@ -1210,7 +1199,7 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\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+\t*stash_msg = xstrdup(stash_msg_buf.buf);\n \n \t/*\n \t * `parents` will be empty after calling `commit_tree()`, so there is\n@@ -1244,30 +1233,23 @@ 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/* Starting with argv[1], since argv[0] is \"create\" */\n+\tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n+\tstash_msg = stash_msg_buf.buf;\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 \tfree(stash_msg);\n \n \t/*\n@@ -1495,9 +1477,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, xstrdup_or_null(stash_msg), quiet,\n@@ -1530,7 +1513,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@@ -1546,10 +1529,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@@ -1557,16 +1542,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@@ -1588,7 +1573,42 @@ 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 (starts_with(argv[i], \"-m\") ||\n+\t\t\t    starts_with(argv[i], \"--message=\"))\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.g10a62394e7\n\n"},{"id":"360432","messageId":"fbb4d34f26f6bd8946e3506e9e30a56d509dc1f8.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 18/21] stash: convert save to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:24Z","receivedAt":"2018-10-14T22:13:43Z","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 befb944871..a0413f5d00 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@@ -1496,6 +1504,48 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     keep_index, patch_mode, 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\tstrbuf_join_argv(&buf, argc, argv, ' ');\n+\t\tstash_msg = buf.buf;\n+\t}\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@@ -1536,6 +1586,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.g10a62394e7\n\n"},{"id":"360433","messageId":"fac126b6e4aebf8f3795a7a4f560cc1e67e95ff9.1539553398.git.ungureanupaulsebastian@gmail.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v10 08/21] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-10-14T22:11:14Z","receivedAt":"2018-10-14T22:13:44Z","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 | 454 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  78 +------\n git.c                   |   1 +\n 6 files changed, 465 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..c43e0aacbb\n--- /dev/null\n+++ b/builtin/stash--helper.c\n@@ -0,0 +1,454 @@\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\terror(_(\"'%s' is not a stash-like commit\"), revision);\n+\t\tfree_stash_info(info);\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 error(_(\"Could not generate diff %s^!.\"),\n+\t\t\t\t\t     oid_to_hex(&info->w_commit));\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 error(_(\"Conflicts in index.\"\n+\t\t\t\t\t       \"Try without --index.\"));\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 error(_(\"Could not save index tree\"));\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.g10a62394e7\n\n"},{"id":"360487","messageId":"nycvar.QRO.7.76.6.1810151108250.561@tvgsbejvaqbjf.bet","threadId":"49563","inReplyTo":"fac126b6e4aebf8f3795a7a4f560cc1e67e95ff9.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v10 08/21] stash: convert apply to builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-10-15T09:10:10Z","receivedAt":"2018-10-15T09:10:13Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Paul,\n\nOn Mon, 15 Oct 2018, Paul-Sebastian Ungureanu wrote:\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\terror(_(\"'%s' is not a stash-like commit\"), revision);\n> +\t\tfree_stash_info(info);\n> +\t\texit(128);\n\nThomas had mentioned earlier that this should probably be a die() (and\nthat the `free_stash_info()` should simply be dropped), see\nhttps://public-inbox.org/git/20180930174848.GE2253@hank.intra.tgummerer.com/\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\nThomas had also mentioned that this should be a `strbuf_join_argv()` call\nnow.\n\nMaybe v10 is an accidental re-send of v9?\n\nCiao,\nDscho\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 error(_(\"Could not generate diff %s^!.\"),\n> +\t\t\t\t\t     oid_to_hex(&info->w_commit));\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 error(_(\"Conflicts in index.\"\n> +\t\t\t\t\t       \"Try without --index.\"));\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 error(_(\"Could not save index tree\"));\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.g10a62394e7\n> \n> \n"},{"id":"360553","messageId":"20181015203207.GA4883@hank.intra.tgummerer.com","threadId":"49563","inReplyTo":"nycvar.QRO.7.76.6.1810151108250.561@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v10 08/21] stash: convert apply to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-15T20:32:07Z","receivedAt":"2018-10-15T20:32:15Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 10/15, Johannes Schindelin wrote:\n> Hi Paul,\n> \n> On Mon, 15 Oct 2018, Paul-Sebastian Ungureanu wrote:\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\terror(_(\"'%s' is not a stash-like commit\"), revision);\n> > +\t\tfree_stash_info(info);\n> > +\t\texit(128);\n> \n> Thomas had mentioned earlier that this should probably be a die() (and\n> that the `free_stash_info()` should simply be dropped), see\n> https://public-inbox.org/git/20180930174848.GE2253@hank.intra.tgummerer.com/\n\nI think the way this is now is fine by me.  Not sure how much we care\nabout freeing 'info' or not (do we care about leaks when we 'die()'\nanyway?), but this is done in the right order now, so we don't print\ngarbage in the error message anymore, and I'm happy with either this\nor replacing all this with 'die()'.  \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> \n> Thomas had also mentioned that this should be a `strbuf_join_argv()` call\n> now.\n\nRe-reading this we quote the individual args here, which is not\npossible with 'strbuf_join_argv()', which I failed to notice when\nreading this the other time.  We don't currently quote them, but I\nthink the quoting may actually be useful.\n\nIt would however have been nice if the reason why the suggestion was\nrejected would have been written down as a reply to my original\nreview, to avoid misunderstandings like this :)\n\n> Maybe v10 is an accidental re-send of v9?\n> \n> Ciao,\n> Dscho\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 error(_(\"Could not generate diff %s^!.\"),\n> > +\t\t\t\t\t     oid_to_hex(&info->w_commit));\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 error(_(\"Conflicts in index.\"\n> > +\t\t\t\t\t       \"Try without --index.\"));\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 error(_(\"Could not save index tree\"));\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.g10a62394e7\n> > \n> > \n"},{"id":"360556","messageId":"20181015220338.GB4883@hank.intra.tgummerer.com","threadId":"49563","inReplyTo":"004f2b2e3e8c4d1069a808034f3d6fe16853085c.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v10 19/21] stash: convert `stash--helper.c` into `stash.c`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-15T22:03:38Z","receivedAt":"2018-10-15T22:03:44Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 10/15, 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> @@ -1138,7 +1133,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n>  \t\t\tfprintf_ln(stderr, _(\"You do not have \"\n>  \t\t\t\t\t     \"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> @@ -1146,7 +1140,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\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> @@ -1167,7 +1160,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n>  \t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>  \t\t\t\t\t     \"index state\"));\n>  \t\tret = -1;\n> -\t\t*stash_msg = NULL;\n>  \t\tgoto done;\n>  \t}\n>  \n> @@ -1178,14 +1170,12 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n>  \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n>  \t\t\t\t\t\t     \"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, quiet);\n> -\t\t*stash_msg = NULL;\n>  \t\tif (ret < 0) {\n>  \t\t\tif (!quiet)\n>  \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n> @@ -1200,7 +1190,6 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n>  \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>  \t\t\t\t\t\t     \"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> @@ -1210,7 +1199,7 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\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> +\t*stash_msg = xstrdup(stash_msg_buf.buf);\n>  \n>  \t/*\n>  \t * `parents` will be empty after calling `commit_tree()`, so there is\n> @@ -1244,30 +1233,23 @@ 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/* Starting with argv[1], since argv[0] is \"create\" */\n> +\tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n> +\tstash_msg = stash_msg_buf.buf;\n\nstash_msg is just a pointer to stash_msg_buf.buf here..\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\nWe release the strbuf here, which means stash_msg_buf.buf is now\n'strbuf_slopbuf', which is a global variable and can't be free'd.  If\nstash_msg is not changed within do_create_stash, it is now pointing to\n'strbuf_slopbuf', and we try to free that below, which makes git\ncrash in t3903.44, which breaks bisection.\n\n>  \tfree(stash_msg);\n>  \n>  \t/*\n\nI think the following diff fixes memory management, by making\ndo_push_stash responsible for freeing stash_msg when it's done with\nit, while the callers of do_create_stash have to free the parameter\nthey pass in (although it may be pointing to something different than\nwhat was passed in).\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex e945c13c42..3b50f4bd53 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1199,6 +1199,7 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,\n \telse\n \t\tstrbuf_addf(&stash_msg_buf, \"On %s: %s\", branch_name,\n \t\t\t    *stash_msg);\n+\tfree(*stash_msg);\n \t*stash_msg = xstrdup(stash_msg_buf.buf);\n \n \t/*\n@@ -1241,7 +1242,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \n \t/* Starting with argv[1], since argv[0] is \"create\" */\n \tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n-\tstash_msg = stash_msg_buf.buf;\n+\tstash_msg = xstrdup(stash_msg_buf.buf);\n \n \tmemset(&ps, 0, sizeof(ps));\n \tret = do_create_stash(ps, &stash_msg, 0, 0, &info, NULL, 0);\n@@ -1522,7 +1523,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)\n \t}\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tret = do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n+\tret = do_push_stash(ps, xstrdup_or_null(stash_msg), quiet, keep_index, patch_mode,\n \t\t\t    include_untracked);\n \n \tstrbuf_release(&buf);\n\nThe above, as well as the some of the changes I quoted above should\nprobably be squashed in to the relevant patches, rather than being\npart of this patch (\"stash: convert create to builtin\", and \"stash:\nconvert save to builtin\").\n"},{"id":"360557","messageId":"20181015220553.GC4883@hank.intra.tgummerer.com","threadId":"49563","inReplyTo":"fbb4d34f26f6bd8946e3506e9e30a56d509dc1f8.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v10 18/21] stash: convert save to builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-15T22:05:53Z","receivedAt":"2018-10-15T22:05:58Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 10/15, Paul-Sebastian Ungureanu wrote:\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\n[...]\n\n> +\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\",\n> +\t\t\t   N_(\"stash message\")),\n> +\t\tOPT_END()\n\nWe do seem to support a '-m' option here though.  I'm happy not\nsupporting it, but the commit message seems to say otherwise.  I can't\nremember the discussion her, but either the commit message, or the\noption parsing should be updated here.\n"},{"id":"360558","messageId":"20181015221040.GD4883@hank.intra.tgummerer.com","threadId":"49563","inReplyTo":"cover.1539553398.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v10 00/21] Convert \"git stash\" to C builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-15T22:10:40Z","receivedAt":"2018-10-15T22:10:47Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 10/15, Paul-Sebastian Ungureanu wrote:\n> Hello,\n> \n> This is a new iteration of `git stash`, based on the last review.\n> This iteration fixes some code styling issues, bring some changes\n> to `do_push_stash()` and `do_create_stash()` to be closer to API by\n> following Thomas Gummerer's review of last iteration [1]. Also, there\n> were some missing messages [2], which are now included.\n\nThanks for your work!  I had two more comments (on the patches\ninline).  Once those are addressed, I'd be happy for this to be merged\nto 'next'.\n\nSince I applied this locally, and it may help someone else who may\nwant to have a look at this, the range-diff is below:\n\n 1:  b7224e494e =  1:  89142f99e7 sha1-name.c: add `get_oidf()` which acts like `get_oid()`\n 2:  63f2e0e6f9 !  2:  2d45985676 strbuf.c: add `strbuf_join_argv()`\n    @@ -14,19 +14,17 @@\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    ++void strbuf_join_argv(struct strbuf *buf,\n    ++\t\t      int argc, const char **argv, char delim)\n     +{\n     +\tif (!argc)\n    -+\t\treturn buf->buf;\n    ++\t\treturn;\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    @@ -40,12 +38,12 @@\n       */\n      extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);\n      \n    -+\n     +/**\n    -+ *\n    ++ * Join the arguments into a buffer. `delim` is put between every\n    ++ * two arguments.\n     + */\n    -+extern const char *strbuf_join_argv(struct strbuf *buf, int argc,\n    -+\t\t\t\t    const char **argv, char delim);\n    ++extern void strbuf_join_argv(struct strbuf *buf, int argc,\n    ++\t\t\t     const char **argv, char delim);\n     +\n      /**\n       * This function can be used to expand a format string containing\n 3:  9b9433781b =  3:  63d10ee599 stash: improve option parsing test coverage\n 4:  c1d38060c5 !  4:  a6953b57e5 stash: update test cases conform to coding guidelines\n    @@ -1,8 +1,9 @@\n     Author: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n     \n    -    stash: update test cases conform to coding guidelines\n    +    t3903: modernize style\n     \n    -    Removed whitespaces after redirection operators.\n    +    Remove whitespaces after redirection operators and wrap\n    +    long lines.\n     \n         Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n     \n 5:  ac7a8267e6 =  5:  9985d8650b stash: rename test cases to be more descriptive\n 6:  0e6458a280 =  6:  93d7e82b96 stash: add tests for `git stash show` config\n13:  6e04c948cf !  7:  e06aca5ff5 stash: mention options in `show` synopsis.\n    @@ -1,9 +1,9 @@\n     Author: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n     \n    -    stash: mention options in `show` synopsis.\n    +    stash: mention options in `show` synopsis\n     \n    -    Mention in the usage text and in the documentation, that `show`\n    -    accepts any option known to `git diff`.\n    +    Mention in the documentation, that `show` accepts any\n    +    option known to `git diff`.\n     \n         Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n     \n    @@ -28,25 +28,3 @@\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    -\n    - diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\n    - --- a/builtin/stash--helper.c\n    - +++ b/builtin/stash--helper.c\n    -@@\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    -@@\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 7:  a2abd1b4bd !  8:  974dbaa492 stash: convert apply to builtin\n    @@ -121,8 +121,8 @@\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\tfree_stash_info(info);\n     +\t\texit(128);\n     +\t}\n     +}\n    @@ -370,18 +370,20 @@\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\treturn error(_(\"Could not generate diff %s^!.\"),\n    ++\t\t\t\t\t     oid_to_hex(&info->w_commit));\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    ++\t\t\t\treturn error(_(\"Conflicts in index.\"\n    ++\t\t\t\t\t       \"Try without --index.\"));\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    ++\t\t\t\treturn error(_(\"Could not save index tree\"));\n     +\n     +\t\t\treset_head();\n     +\t\t}\n 8:  b4e019e15e !  9:  70b09f7dbb stash: convert drop and clear to builtin\n    @@ -68,7 +68,8 @@\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    ++\t\treturn error(_(\"git stash clear with parameters is \"\n    ++\t\t\t       \"unimplemented\"));\n     +\n     +\treturn do_clear_stash();\n     +}\n 9:  c097f4fb0c ! 10:  2408fb2f39 stash: convert branch to builtin\n    @@ -53,7 +53,7 @@\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\tfprintf_ln(stderr, _(\"No branch name specified\"));\n     +\t\treturn -1;\n     +\t}\n     +\n10:  ed3c5ccfe8 ! 11:  b9d2ed6394 stash: convert pop to builtin\n    @@ -58,7 +58,8 @@\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    ++\t\tprintf_ln(_(\"The stash entry is kept in case \"\n    ++\t\t\t    \"you need it again.\"));\n     +\telse\n     +\t\tret = do_drop_stash(prefix, &info, quiet);\n     +\n11:  85c0e246c6 = 12:  32375ffef9 stash: convert list to builtin\n12:  c7425be617 ! 13:  738b3e25fc stash: convert show to builtin\n    @@ -24,7 +24,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    @@ -33,7 +33,7 @@\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     +\n14:  b661dba53c ! 14:  89add3b71a stash: convert store to builtin\n    @@ -54,7 +54,8 @@\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_STRING('m', \"message\", &stash_msg, \"message\",\n    ++\t\t\t   N_(\"stash message\")),\n     +\t\tOPT_END()\n     +\t};\n     +\n    @@ -64,7 +65,8 @@\n     +\n     +\tif (argc != 1) {\n     +\t\tif (!quiet)\n    -+\t\t\tfprintf_ln(stderr, _(\"\\\"git stash store\\\" requires one <commit> argument\"));\n    ++\t\t\tfprintf_ln(stderr, _(\"\\\"git stash store\\\" requires one \"\n    ++\t\t\t\t\t     \"<commit> argument\"));\n     +\t\treturn -1;\n     +\t}\n     +\n15:  bd827be103 ! 15:  989db67e9a stash: convert create to builtin\n    @@ -49,7 +49,7 @@\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\tstrbuf_addch(data, '\\0');\n     +\t}\n     +}\n     +\n    @@ -61,7 +61,7 @@\n      }\n      \n     +static void add_pathspecs(struct argv_array *args,\n    -+\t\t\t\t       struct pathspec ps) {\n    ++\t\t\t  struct pathspec ps) {\n     +\tint i;\n     +\n     +\tfor (i = 0; i < ps.nr; i++)\n    @@ -119,7 +119,6 @@\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    @@ -176,7 +175,8 @@\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    ++\tif (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,\n    ++\t\t\t NULL, 0)) {\n     +\t\tret = -1;\n     +\t\tgoto done;\n     +\t}\n    @@ -296,7 +296,8 @@\n     +\t\tgoto done;\n     +\t}\n     +\n    -+\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit), \"\");\n    ++\tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n    ++\t\t\t   \"\");\n     +\tif (run_diff_index(&rev, 0)) {\n     +\t\tret = -1;\n     +\t\tgoto done;\n    @@ -392,7 +393,8 @@\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\tfprintf_ln(stderr, _(\"Cannot save \"\n    ++\t\t\t\t\t     \"the untracked files\"));\n     +\t\t\tret = -1;\n     +\t\t\t*stash_msg = NULL;\n     +\t\t\tgoto done;\n    @@ -403,14 +405,16 @@\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\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    ++\t\t\t\t\t     \"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\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    ++\t\t\t\t\t     \"worktree state\"));\n     +\t\t\tret = -1;\n     +\t\t\t*stash_msg = NULL;\n     +\t\t\tgoto done;\n    @@ -430,8 +434,11 @@\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    ++\t\tcommit_list_insert(lookup_commit(the_repository,\n    ++\t\t\t\t\t\t &info->u_commit),\n    ++\t\t\t\t   &parents);\n    ++\tcommit_list_insert(lookup_commit(the_repository, &info->i_commit),\n    ++\t\t\t   &parents);\n     +\tcommit_list_insert(head_commit, &parents);\n     +\n     +\tif (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,\n    @@ -474,11 +481,12 @@\n     +\tif (!ret)\n     +\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n     +\n    ++\tfree(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     +\n16:  e8434e36aa ! 16:  474e72d365 stash: convert push to builtin\n    @@ -58,7 +58,7 @@\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\tfprintf_ln(stderr, _(\"Cannot save the current \"\n     @@\n      \t\t\t     0);\n      \n    @@ -101,28 +101,25 @@\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\tfree(ps_matched);\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    @@ -293,8 +290,8 @@\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    ++\treturn do_push_stash(ps, xstrdup_or_null(stash_msg), quiet,\n    ++\t\t\t     keep_index, patch_mode, include_untracked);\n     +}\n     +\n      int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n17:  5a79bcaebf ! 17:  f6af86e944 stash: make push -q quiet\n    @@ -5,7 +5,7 @@\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    +    `--quiet` or `-q` is specified. Add tests for `--quiet`.\n     \n         Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n     \n    @@ -47,7 +47,8 @@\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\t\tfprintf_ln(stderr, _(\"You do not have \"\n    ++\t\t\t\t\t     \"the initial commit yet\"));\n      \t\tret = -1;\n      \t\t*stash_msg = NULL;\n      \t\tgoto done;\n    @@ -57,7 +58,8 @@\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\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    ++\t\t\t\t\t     \"index state\"));\n      \t\tret = -1;\n      \t\t*stash_msg = NULL;\n      \t\tgoto done;\n    @@ -65,9 +67,11 @@\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\tfprintf_ln(stderr, _(\"Cannot save \"\n    +-\t\t\t\t\t     \"the untracked files\"));\n     +\t\t\tif (!quiet)\n    -+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the untracked files\"));\n    ++\t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n    ++\t\t\t\t\t\t     \"the untracked files\"));\n      \t\t\tret = -1;\n      \t\t\t*stash_msg = NULL;\n      \t\t\tgoto done;\n    @@ -79,18 +83,22 @@\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\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    +-\t\t\t\t\t     \"worktree state\"));\n     +\t\t\tif (!quiet)\n    -+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n    ++\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    ++\t\t\t\t\t\t     \"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\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    +-\t\t\t\t\t     \"worktree state\"));\n     +\t\t\tif (!quiet)\n    -+\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current worktree state\"));\n    ++\t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    ++\t\t\t\t\t\t     \"worktree state\"));\n      \t\t\tret = -1;\n      \t\t\t*stash_msg = NULL;\n      \t\t\tgoto done;\n    @@ -100,7 +108,8 @@\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\t\tfprintf_ln(stderr, _(\"Cannot record \"\n    ++\t\t\t\t\t     \"working tree state\"));\n      \t\tret = -1;\n      \t\tgoto done;\n      \t}\n    @@ -114,8 +123,8 @@\n      \tif (!ret)\n      \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n     @@\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\tif (!quiet)\n    @@ -151,7 +160,8 @@\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\t\tfprintf_ln(stderr, _(\"Cannot remove \"\n    ++\t\t\t\t\t\t     \"worktree changes\"));\n      \t\t\tret = -1;\n      \t\t\tgoto done;\n      \t\t}\n18:  1c501ad666 ! 18:  c90e30173a stash: convert save to builtin\n    @@ -40,7 +40,7 @@\n      static struct strbuf stash_index_path = STRBUF_INIT;\n      \n     @@\n    - \t\t\t     include_untracked);\n    + \t\t\t     keep_index, patch_mode, include_untracked);\n      }\n      \n     +static int save_stash(int argc, const char **argv, const char *prefix)\n    @@ -72,8 +72,10 @@\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    ++\tif (argc) {\n    ++\t\tstrbuf_join_argv(&buf, argc, argv, ' ');\n    ++\t\tstash_msg = buf.buf;\n    ++\t}\n     +\n     +\tmemset(&ps, 0, sizeof(ps));\n     +\tret = do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n19:  c4401b21db ! 19:  4360ea875d stash: convert `stash--helper.c` into `stash.c`\n    @@ -215,7 +215,7 @@\n     +\t\t\t     git_stash_branch_usage, 0);\n      \n      \tif (!argc) {\n    - \t\tfprintf_ln(stderr, \"No branch name specified\");\n    + \t\tfprintf_ln(stderr, _(\"No branch name specified\"));\n     @@\n      \t};\n      \n    @@ -244,6 +244,62 @@\n      \n      \tif (argc != 1) {\n     @@\n    + \t\t\tfprintf_ln(stderr, _(\"You do not have \"\n    + \t\t\t\t\t     \"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    +@@\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    +@@\n    + \t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    + \t\t\t\t\t     \"index state\"));\n    + \t\tret = -1;\n    +-\t\t*stash_msg = NULL;\n    + \t\tgoto done;\n    + \t}\n    + \n    +@@\n    + \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n    + \t\t\t\t\t\t     \"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, quiet);\n    +-\t\t*stash_msg = NULL;\n    + \t\tif (ret < 0) {\n    + \t\t\tif (!quiet)\n    + \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    +@@\n    + \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n    + \t\t\t\t\t\t     \"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    + \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    ++\t*stash_msg = xstrdup(stash_msg_buf.buf);\n    + \n    + \t/*\n    + \t * `parents` will be empty after calling `commit_tree()`, so there is\n    +@@\n      \n      static int create_stash(int argc, const char **argv, const char *prefix)\n      {\n    @@ -264,9 +320,9 @@\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    ++\t/* Starting with argv[1], since argv[0] is \"create\" */\n    ++\tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n    ++\tstash_msg = stash_msg_buf.buf;\n      \n      \tmemset(&ps, 0, sizeof(ps));\n     -\tret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info,\n    @@ -277,15 +333,9 @@\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    + \tfree(stash_msg);\n      \n    + \t/*\n     @@\n      \t\tOPT_END()\n      \t};\n    @@ -299,7 +349,7 @@\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    + \treturn do_push_stash(ps, xstrdup_or_null(stash_msg), quiet,\n     @@\n      \t};\n      \n    @@ -308,7 +358,7 @@\n     +\t\t\t     git_stash_save_usage,\n      \t\t\t     PARSE_OPT_KEEP_DASHDASH);\n      \n    - \tif (argc)\n    + \tif (argc) {\n     @@\n      \treturn ret;\n      }\n    @@ -375,10 +425,8 @@\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\tif (starts_with(argv[i], \"-m\") ||\n    ++\t\t\t    starts_with(argv[i], \"--message=\"))\n     +\t\t\t\tcontinue;\n     +\n     +\t\t\tusage_with_options(git_stash_usage, options);\n20:  92dc11fd16 ! 20:  a384b05008 stash: optimize `get_untracked_files()` and `check_changes()`\n    @@ -52,7 +52,6 @@\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    @@ -99,8 +98,8 @@\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    + \t}\n     @@\n      \t\tgoto done;\n      \t}\n    @@ -110,9 +109,9 @@\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    + \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n     @@\n    - \t\t\t\t\t     ++argv, ' ');\n    + \tstash_msg = stash_msg_buf.buf;\n      \n      \tmemset(&ps, 0, sizeof(ps));\n     -\tret = do_create_stash(ps, &stash_msg, 0, 0, &info, NULL, 0);\n    @@ -124,6 +123,7 @@\n      \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n      \n      \tstrbuf_release(&stash_msg_buf);\n    + \tfree(stash_msg);\n     -\n     -\t/*\n     -\t * ret can be 1 if there were no changes. In this case, we should\n    @@ -148,6 +148,6 @@\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    + \t\tgoto done;\n21:  2c40c9232a = 21:  825d1e6ef1 stash: replace all `write-tree` child processes with API calls\n\n\n> [1]\n> https://public-inbox.org/git/20181002203730.GI2253@hank.intra.tgummerer.com/\n> \n> [2]\n> https://github.com/git-for-windows/git/commit/2af24038a95a3879aa0c29d91a43180b9465247e\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>   t3903: modernize style\n>   stash: rename test cases to be more descriptive\n>   stash: add tests for `git stash show` config\n>   stash: mention options in `show` synopsis\n>   stash: convert list to builtin\n>   stash: convert show to builtin\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              | 1605 ++++++++++++++++++++++++++++++++++\n>  cache.h                      |    1 +\n>  git-stash.sh                 |  752 ----------------\n>  git.c                        |    1 +\n>  sha1-name.c                  |   19 +\n>  strbuf.c                     |   13 +\n>  strbuf.h                     |    7 +\n>  t/t3903-stash.sh             |  192 ++--\n>  t/t3907-stash-show-config.sh |   83 ++\n>  12 files changed, 1859 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> -- \n> 2.19.0.rc0.23.g10a62394e7\n> \n"},{"id":"360571","messageId":"xmqqin2236e2.fsf@gitster-ct.c.googlers.com","threadId":"49563","inReplyTo":"20181015221040.GD4883@hank.intra.tgummerer.com","subject":"Re: [PATCH v10 00/21] Convert \"git stash\" to C builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-10-16T03:41:25Z","receivedAt":"2018-10-16T03:41:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Gummerer <t.gummerer@gmail.com> writes:\n\n> On 10/15, Paul-Sebastian Ungureanu wrote:\n>> Hello,\n>> \n>> This is a new iteration of `git stash`, based on the last review.\n>> This iteration fixes some code styling issues, bring some changes\n>> to `do_push_stash()` and `do_create_stash()` to be closer to API by\n>> following Thomas Gummerer's review of last iteration [1]. Also, there\n>> were some missing messages [2], which are now included.\n>\n> Thanks for your work!  I had two more comments (on the patches\n> inline).  Once those are addressed, I'd be happy for this to be merged\n> to 'next'.\n\nThank you for your tireless reviews.  They were quite helpful and\nreasonable.\n"},{"id":"360607","messageId":"nycvar.QRO.7.76.6.1810161205530.4546@tvgsbejvaqbjf.bet","threadId":"49563","inReplyTo":"20181015221040.GD4883@hank.intra.tgummerer.com","subject":"Re: [PATCH v10 00/21] Convert \"git stash\" to C builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-10-16T10:22:30Z","receivedAt":"2018-10-16T10:22:31Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Thomas,\n\nOn Mon, 15 Oct 2018, Thomas Gummerer wrote:\n\n>  2:  63f2e0e6f9 !  2:  2d45985676 strbuf.c: add `strbuf_join_argv()`\n>     @@ -14,19 +14,17 @@\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>     ++void strbuf_join_argv(struct strbuf *buf,\n>     ++\t\t      int argc, const char **argv, char delim)\n\nWhile the patch series does not use the return value, I have to ask\nwhether it would really be useful to change it to return `void`. I could\nimagine that there may already be quite a few code paths that would love\nto use strbuf_join_argv(), *and* would benefit from the `const char *`\nreturn value.\n\nIn other words: just because the *current* patches do not make use of that\nquite convenient return value does not mean that we should remove that\nconvenience.\n\n>  7:  a2abd1b4bd !  8:  974dbaa492 stash: convert apply to builtin\n>     @@ -370,18 +370,20 @@\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\treturn error(_(\"Could not generate diff %s^!.\"),\n>     ++\t\t\t\t\t     oid_to_hex(&info->w_commit));\n\nPlease start the argument of an `error()` call with a lower-case letter.\n\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>     ++\t\t\t\treturn error(_(\"Conflicts in index.\"\n>     ++\t\t\t\t\t       \"Try without --index.\"));\n\nSame here.\n\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>     ++\t\t\t\treturn error(_(\"Could not save index tree\"));\n\nAnd here.\n\n> 15:  bd827be103 ! 15:  989db67e9a stash: convert create to builtin\n>     @@ -119,7 +119,6 @@\n>      +static int check_changes(struct pathspec ps, int include_untracked)\n>      +{\n>      +\tint result;\n>     -+\tint ret = 0;\n\nI was curious about this change, and could not find it in the\ngit-stash-v10 tag of https://github.com/ungps/git...\n\n> 18:  1c501ad666 ! 18:  c90e30173a stash: convert save to builtin\n>     @@ -72,8 +72,10 @@\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>     ++\tif (argc) {\n>     ++\t\tstrbuf_join_argv(&buf, argc, argv, ' ');\n>     ++\t\tstash_msg = buf.buf;\n>     ++\t}\n\nAha! So there *was* a user of that return value. I really would prefer a\nnon-void return value here.\n\n> 19:  c4401b21db ! 19:  4360ea875d stash: convert `stash--helper.c` into `stash.c`\n>     @@ -264,9 +320,9 @@\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>     ++\t/* Starting with argv[1], since argv[0] is \"create\" */\n>     ++\tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n>     ++\tstash_msg = stash_msg_buf.buf;\n\nAgain, I would strongly prefer the convenience of assigning the return\nvalue directly, rather than having two lines.\n\n>     @@ -375,10 +425,8 @@\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\tif (starts_with(argv[i], \"-m\") ||\n>     ++\t\t\t    starts_with(argv[i], \"--message=\"))\n\nVery nice.\n\n> 20:  92dc11fd16 ! 20:  a384b05008 stash: optimize `get_untracked_files()` and `check_changes()`\n>     @@ -52,7 +52,6 @@\n>      +static int check_changes_tracked_files(struct pathspec ps)\n>       {\n>       \tint result;\n>     --\tint ret = 0;\n\nI also wonder about this change, in light of...\n\n>       \tstruct rev_info rev;\n>       \tstruct object_id dummy;\n>      -\tstruct strbuf out = STRBUF_INIT;\n>     @@ -99,8 +98,8 @@\n>      -\tif (!check_changes(ps, include_untracked)) {\n>      +\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n>       \t\tret = 1;\n\nthis here line. How does that work, if `ret` is removed? And why didn't\nthe `make DEVELOPER=1` complain about that unused `ret` variable before?\n\n>     - \t\t*stash_msg = NULL;\n>       \t\tgoto done;\n>     + \t}\n>      @@\n>       \t\tgoto done;\n\nThe rest of the changes looks pretty sensible to me (indentation/wrapping\nchanges, mostly, with a couple of commit message/typo fixes thrown in).\n\nMaybe you have a commit hash, or even better, a tag in a public Git\nrepository somewhere, so that Paul can pick it up more easily (and compare\nthe changes with his latest local branch)?\n\nThank you!\nDscho\n"},{"id":"360660","messageId":"20181016195949.GE4883@hank.intra.tgummerer.com","threadId":"49563","inReplyTo":"nycvar.QRO.7.76.6.1810161205530.4546@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v10 00/21] Convert \"git stash\" to C builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-10-16T19:59:49Z","receivedAt":"2018-10-16T19:59:55Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 10/16, Johannes Schindelin wrote:\n> Hi Thomas,\n> \n> On Mon, 15 Oct 2018, Thomas Gummerer wrote:\n> \n> >  2:  63f2e0e6f9 !  2:  2d45985676 strbuf.c: add `strbuf_join_argv()`\n> >     @@ -14,19 +14,17 @@\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> >     ++void strbuf_join_argv(struct strbuf *buf,\n> >     ++\t\t      int argc, const char **argv, char delim)\n> \n> While the patch series does not use the return value, I have to ask\n> whether it would really be useful to change it to return `void`. I could\n> imagine that there may already be quite a few code paths that would love\n> to use strbuf_join_argv(), *and* would benefit from the `const char *`\n> return value.\n\nFair enough.  I did suggest changing the return type to void here, as\nI found the API a bit odd compared to the rest of the strbuf API,\nhowever after looking at this again I agree with you, and returning a\nconst char * here does seem more helpful.  Sorry about the confusion\nPaul-Sebastian!\n\n> In other words: just because the *current* patches do not make use of that\n> quite convenient return value does not mean that we should remove that\n> convenience.\n>\n> >  7:  a2abd1b4bd !  8:  974dbaa492 stash: convert apply to builtin\n> >     @@ -370,18 +370,20 @@\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\treturn error(_(\"Could not generate diff %s^!.\"),\n> >     ++\t\t\t\t\t     oid_to_hex(&info->w_commit));\n> \n> Please start the argument of an `error()` call with a lower-case letter.\n\nI think this comes from your fixup! commit ;) But I do agree, these should be\nlower-case.\n\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> >     ++\t\t\t\treturn error(_(\"Conflicts in index.\"\n> >     ++\t\t\t\t\t       \"Try without --index.\"));\n> \n> Same here.\n> \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> >     ++\t\t\t\treturn error(_(\"Could not save index tree\"));\n> \n> And here.\n> \n> > 15:  bd827be103 ! 15:  989db67e9a stash: convert create to builtin\n> >     @@ -119,7 +119,6 @@\n> >      +static int check_changes(struct pathspec ps, int include_untracked)\n> >      +{\n> >      +\tint result;\n> >     -+\tint ret = 0;\n> \n> I was curious about this change, and could not find it in the\n> git-stash-v10 tag of https://github.com/ungps/git...\n\nThis line has been removed in v10, but did exist in v9, so\nthe git-stash-v10 should indeed not have this line.  I suggested\nremoving it in [*1*], because it breaks compilation with DEVELOPER=1\nat this step.\n\n> > 18:  1c501ad666 ! 18:  c90e30173a stash: convert save to builtin\n> >     @@ -72,8 +72,10 @@\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> >     ++\tif (argc) {\n> >     ++\t\tstrbuf_join_argv(&buf, argc, argv, ' ');\n> >     ++\t\tstash_msg = buf.buf;\n> >     ++\t}\n> \n> Aha! So there *was* a user of that return value. I really would prefer a\n> non-void return value here.\n\nRight, I'd argue we're mis-using the API here though.  do_push_stash\nwho we later pass stash_msg to takes ownership and later free's the\nmemory before returning.  This doesn't cause issues in the test suite\nat the moment, because do_create_stash() doesn't always free stash_msg\nbefore assigning a new value to the pointer, but would cause issues\nwhen do_create_stash exits early.\n\nRather than the solution I proposed in I think it would be nicer to\nuse 'stash_msg = strbuf_detach(...)' above.\n\nI'm still happy with the function returning buf->buf as const char *,\nbut I'm not sure we should use that return value here.\n\n> > 19:  c4401b21db ! 19:  4360ea875d stash: convert `stash--helper.c` into `stash.c`\n> >     @@ -264,9 +320,9 @@\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> >     ++\t/* Starting with argv[1], since argv[0] is \"create\" */\n> >     ++\tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n> >     ++\tstash_msg = stash_msg_buf.buf;\n> \n> Again, I would strongly prefer the convenience of assigning the return\n> value directly, rather than having two lines.\n\nThis is a similar case as above, where I think using strbuf_detach\nwould be best, again instead of the 'xstrdup()' I mentioned in [*2*].\n\n> >     @@ -375,10 +425,8 @@\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\tif (starts_with(argv[i], \"-m\") ||\n> >     ++\t\t\t    starts_with(argv[i], \"--message=\"))\n> \n> Very nice.\n> \n> > 20:  92dc11fd16 ! 20:  a384b05008 stash: optimize `get_untracked_files()` and `check_changes()`\n> >     @@ -52,7 +52,6 @@\n> >      +static int check_changes_tracked_files(struct pathspec ps)\n> >       {\n> >       \tint result;\n> >     --\tint ret = 0;\n> \n> I also wonder about this change, in light of...\n\nThe double - in the beginning of the range diff indicates that the\nremoval of this line was removed from this particular patch (the\nremoval is now done in patch 15 instead).  I think this is one of\nthose cases where the range-diff is a bit hard to interpret,\nespecially without the --dual-color mode :)\n\n> >       \tstruct rev_info rev;\n> >       \tstruct object_id dummy;\n> >      -\tstruct strbuf out = STRBUF_INIT;\n> >     @@ -99,8 +98,8 @@\n> >      -\tif (!check_changes(ps, include_untracked)) {\n> >      +\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n> >       \t\tret = 1;\n> \n> this here line. How does that work, if `ret` is removed? And why didn't\n> the `make DEVELOPER=1` complain about that unused `ret` variable before?\n\nThis is a different function, the above is in the\n'check_changes_tracked_files()' function, while we are in the\n'do_create_stash()' function, which has a local 'ret' variable.\n\n> \n> >     - \t\t*stash_msg = NULL;\n> >       \t\tgoto done;\n> >     + \t}\n> >      @@\n> >       \t\tgoto done;\n> \n> The rest of the changes looks pretty sensible to me (indentation/wrapping\n> changes, mostly, with a couple of commit message/typo fixes thrown in).\n> \n> Maybe you have a commit hash, or even better, a tag in a public Git\n> repository somewhere, so that Paul can pick it up more easily (and compare\n> the changes with his latest local branch)?\n\nThe range-diff here between Paul's v9 and v10 series.  I just applied\nboth series, and sent out the range-diff in case someone else prefered\nnot applying both series, or fetching both from Paul's repo, but\nreading the range-diff (and I got you to comment on it, which I\nalready found helpful :)).  There are no changes of my own included in\nthis.\n\n> Thank you!\n> Dscho\n\n[*1*]: https://public-inbox.org/git/20181002201940.GH2253@hank.intra.tgummerer.com/\n[*2*]: https://public-inbox.org/git/20181015220338.GB4883@hank.intra.tgummerer.com/\n"}]}