{"thread":{"id":"49883","subject":"[PATCH v11 00/22] Convert \"git stash\" to C builtin","startedAt":"2018-11-22T23:06:15Z","lastAt":"2018-11-29T14:06:20Z","messageCount":35,"participants":["Paul-Sebastian Ungureanu","Thomas Gummerer","Junio C Hamano","Johannes Schindelin","Ævar Arnfjörð Bjarmason"],"isPatch":true,"patchVersion":11,"patchTotal":22},"messages":[{"id":"363929","messageId":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"https://public-inbox.org/git/cover.1539553398.git.ungureanupaulsebastian@gmail.com/","subject":"[PATCH v11 00/22] Convert \"git stash\" to C builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:20Z","receivedAt":"2018-11-22T23:06:15Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Hello,\n\nThis is the 11th iteration of C git stash. Here are some of the changes,\nbased on Thomas's and dscho's suggestions (from mailing list / pull request\n#495):\n\n- improved memory management. Now, the callers of `do_create_stash()`\nare responsible of freeing the parameter they pass in. Moreover, the\nstash message is now a pointer to a buffer (in the previous iteration\nit was a pointer to a string). This should make it more clear who is\nresponsible of freeing the memory.\n\n- added `strbuf_insertf()` which inserts a format string at a given\nposition in the buffer.\n\n- some minor changes (changed \"!oidcmp\" to \"oideq\")\n\n- fixed merge conflicts\n\nBest regards,\nPaul\n\nJoel Teichroeb (5):\n  stash: improve option parsing test coverage\n  stash: convert apply to builtin\n  stash: convert drop and clear to builtin\n  stash: convert branch to builtin\n  stash: convert pop to builtin\n\nPaul-Sebastian Ungureanu (17):\n  sha1-name.c: add `get_oidf()` which acts like `get_oid()`\n  strbuf.c: add `strbuf_join_argv()`\n  strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`\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              | 1596 ++++++++++++++++++++++++++++++++++\n cache.h                      |    1 +\n git-stash.sh                 |  752 ----------------\n git.c                        |    1 +\n sha1-name.c                  |   19 +\n strbuf.c                     |   51 ++\n strbuf.h                     |   16 +\n t/t3903-stash.sh             |  192 ++--\n t/t3907-stash-show-config.sh |   83 ++\n 12 files changed, 1897 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.1.878.g0482332a22\n\n"},{"id":"363930","messageId":"bbd55849d41892a15021eb2fcfb2ea55efeecd95.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 01/22] sha1-name.c: add `get_oidf()` which acts like `get_oid()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:21Z","receivedAt":"2018-11-22T23:06:16Z","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 8b1ee42ae9..6f1a549489 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1334,6 +1334,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 faa60f69e3..cf0e8a3f85 100644\n--- a/sha1-name.c\n+++ b/sha1-name.c\n@@ -1542,6 +1542,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.1.878.g0482332a22\n\n"},{"id":"363931","messageId":"af23e1efdababcd981f814f20a1b44048509cd23.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 02/22] strbuf.c: add `strbuf_join_argv()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:22Z","receivedAt":"2018-11-22T23:06:18Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Implement `strbuf_join_argv()` to join arguments\ninto a strbuf.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n strbuf.c | 15 +++++++++++++++\n strbuf.h |  7 +++++++\n 2 files changed, 22 insertions(+)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex f6a6cf78b9..82e90f1dfe 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -268,6 +268,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)\n \tstrbuf_setlen(sb, sb->len + sb2->len);\n }\n \n+const char *strbuf_join_argv(struct strbuf *buf,\n+\t\t\t     int argc, const char **argv, char delim)\n+{\n+\tif (!argc)\n+\t\treturn buf->buf;\n+\n+\tstrbuf_addstr(buf, *argv);\n+\twhile (--argc) {\n+\t\tstrbuf_addch(buf, delim);\n+\t\tstrbuf_addstr(buf, *(++argv));\n+\t}\n+\n+\treturn buf->buf;\n+}\n+\n void strbuf_addchars(struct strbuf *sb, int c, size_t n)\n {\n \tstrbuf_grow(sb, n);\ndiff --git a/strbuf.h b/strbuf.h\nindex fc40873b65..be02150df3 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -288,6 +288,13 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)\n  */\n 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+const char *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.1.878.g0482332a22\n\n"},{"id":"363932","messageId":"e8d86fae660a79eabcf4764dfa9986282c097242.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 03/22] strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:23Z","receivedAt":"2018-11-22T23:06:18Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Implement `strbuf_insertf()` and `strbuf_vinsertf()` to\ninsert data using a printf format string.\n\nOriginal-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n strbuf.c | 36 ++++++++++++++++++++++++++++++++++++\n strbuf.h |  9 +++++++++\n 2 files changed, 45 insertions(+)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex 82e90f1dfe..bfbbdadbf3 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -249,6 +249,42 @@ void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)\n \tstrbuf_splice(sb, pos, 0, data, len);\n }\n \n+void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)\n+{\n+\tint len, len2;\n+\tchar save;\n+\tva_list cp;\n+\n+\tif (pos > sb->len)\n+\t\tdie(\"`pos' is too far after the end of the buffer\");\n+\tva_copy(cp, ap);\n+\tlen = vsnprintf(sb->buf + sb->len, 0, fmt, cp);\n+\tva_end(cp);\n+\tif (len < 0)\n+\t\tBUG(\"your vsnprintf is broken (returned %d)\", len);\n+\tif (!len)\n+\t\treturn; /* nothing to do */\n+\tif (unsigned_add_overflows(sb->len, len))\n+\t\tdie(\"you want to use way too much memory\");\n+\tstrbuf_grow(sb, len);\n+\tmemmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);\n+\t/* vsnprintf() will append a NUL, overwriting one of our characters */\n+\tsave = sb->buf[pos + len];\n+\tlen2 = vsnprintf(sb->buf + pos, sb->alloc - sb->len, fmt, ap);\n+\tsb->buf[pos + len] = save;\n+\tif (len2 != len)\n+\t\tBUG(\"your vsnprintf is broken (returns inconsistent lengths)\");\n+\tstrbuf_setlen(sb, sb->len + len);\n+}\n+\n+void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...)\n+{\n+\tva_list ap;\n+\tva_start(ap, fmt);\n+\tstrbuf_vinsertf(sb, pos, fmt, ap);\n+\tva_end(ap);\n+}\n+\n void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)\n {\n \tstrbuf_splice(sb, pos, len, \"\", 0);\ndiff --git a/strbuf.h b/strbuf.h\nindex be02150df3..8f8fe01e68 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -244,6 +244,15 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);\n  */\n void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);\n \n+/**\n+ * Insert data to the given position of the buffer giving a printf format\n+ * string. The contents will be shifted, not overwritten.\n+ */\n+void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt,\n+\t\t     va_list ap);\n+\n+void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...);\n+\n /**\n  * Remove given amount of data from a given position of the buffer.\n  */\n-- \n2.19.1.878.g0482332a22\n\n"},{"id":"363933","messageId":"b150a3abbfff1bcfebc272d2bea2f9ab947f68ed.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 04/22] stash: improve option parsing test coverage","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:24Z","receivedAt":"2018-11-22T23:06:24Z","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 cd216655b9..4f8aa56021 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.1.878.g0482332a22\n\n"},{"id":"363934","messageId":"7784ea24aa1ada5c5cbd9407a16c870b06a43d6c.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 05/22] t3903: modernize style","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:25Z","receivedAt":"2018-11-22T23:06:24Z","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 4f8aa56021..098a387a82 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 expect output\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 expect output &&\n-\tgit diff > output &&\n+\tgit diff >output &&\n \ttest_cmp expect1 output &&\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 expect output\n '\n \n-- \n2.19.1.878.g0482332a22\n\n"},{"id":"363935","messageId":"1b0d546c452872d45d8fc9e648855ef5c17eb7fb.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 07/22] stash: add tests for `git stash show` config","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:27Z","receivedAt":"2018-11-22T23:06:24Z","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.1.878.g0482332a22\n\n"},{"id":"363936","messageId":"52becd09368bec7e24df4d837f2687012f12f190.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 06/22] stash: rename test cases to be more descriptive","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:26Z","receivedAt":"2018-11-22T23:06:24Z","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 098a387a82..8b09a3d6cc 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.1.878.g0482332a22\n\n"},{"id":"363938","messageId":"4af795c07883b1e103a8999f4cbe32bd2db0cead.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 08/22] stash: mention options in `show` synopsis","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:28Z","receivedAt":"2018-11-22T23:06:25Z","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.1.878.g0482332a22\n\n"},{"id":"363937","messageId":"22ae2b44fc9c32f36050e5189f65307d66f31f96.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 10/22] stash: convert drop and clear to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:30Z","receivedAt":"2018-11-22T23:06:26Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd the drop and clear commands to the builtin helper. These two\nare each simple, but are being added together as they are quite\nrelated.\n\nWe have to unfortunately keep the drop and clear functions in the\nshell script as functions are called with parameters internally\nthat are not valid when the commands are called externally. Once\npop is converted they can both be removed.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 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 997b1c0ecf..07b8ec5bcb 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@@ -137,6 +149,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@@ -424,6 +462,81 @@ static int apply_stash(int argc, const char **argv, const char *prefix)\n \treturn ret;\n }\n \n+static int do_drop_stash(const char *prefix, struct stash_info *info, int quiet)\n+{\n+\tint ret;\n+\tstruct child_process cp_reflog = CHILD_PROCESS_INIT;\n+\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * reflog does not provide a simple function for deleting refs. One will\n+\t * need to be added to avoid implementing too much reflog code here\n+\t */\n+\n+\tcp_reflog.git_cmd = 1;\n+\targv_array_pushl(&cp_reflog.args, \"reflog\", \"delete\", \"--updateref\",\n+\t\t\t \"--rewrite\", NULL);\n+\targv_array_push(&cp_reflog.args, info->revision.buf);\n+\tret = run_command(&cp_reflog);\n+\tif (!ret) {\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"Dropped %s (%s)\"), info->revision.buf,\n+\t\t\t\t  oid_to_hex(&info->w_commit));\n+\t} else {\n+\t\treturn error(_(\"%s: Could not drop stash entry\"),\n+\t\t\t     info->revision.buf);\n+\t}\n+\n+\t/*\n+\t * This could easily be replaced by get_oid, but currently it will throw\n+\t * a fatal error when a reflog is empty, which we can not recover from.\n+\t */\n+\tcp.git_cmd = 1;\n+\t/* Even though --quiet is specified, rev-parse still outputs the hash */\n+\tcp.no_stdout = 1;\n+\targv_array_pushl(&cp.args, \"rev-parse\", \"--verify\", \"--quiet\", NULL);\n+\targv_array_pushf(&cp.args, \"%s@{0}\", ref_stash);\n+\tret = run_command(&cp);\n+\n+\t/* do_clear_stash if we just dropped the last stash entry */\n+\tif (ret)\n+\t\tdo_clear_stash();\n+\n+\treturn 0;\n+}\n+\n+static void assert_stash_ref(struct stash_info *info)\n+{\n+\tif (!info->is_stash_ref) {\n+\t\tfree_stash_info(info);\n+\t\terror(_(\"'%s' is not a stash reference\"), info->revision.buf);\n+\t\texit(128);\n+\t}\n+}\n+\n+static int drop_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint ret;\n+\tint quiet = 0;\n+\tstruct stash_info info;\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_drop_usage, 0);\n+\n+\tif (get_stash_info(&info, argc, argv))\n+\t\treturn -1;\n+\n+\tassert_stash_ref(&info);\n+\n+\tret = do_drop_stash(prefix, &info, quiet);\n+\tfree_stash_info(&info);\n+\treturn ret;\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -446,6 +559,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.1.878.g0482332a22\n\n"},{"id":"363939","messageId":"95eb58bf26aaadc418a30e488e5847b1843f3290.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 09/22] stash: convert apply to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:29Z","receivedAt":"2018-11-22T23:06:26Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"From: Joel Teichroeb <joel@teichroeb.net>\n\nAdd a builtin helper for performing stash commands. Converting\nall at once proved hard to review, so starting with just apply\nlets conversion get started without the other commands being\nfinished.\n\nThe helper is being implemented as a drop in replacement for\nstash so that when it is complete it can simply be renamed and\nthe shell script deleted.\n\nDelete the contents of the apply_stash shell function and replace\nit with a call to stash--helper apply until pop is also\nconverted.\n\nSigned-off-by: Joel Teichroeb <joel@teichroeb.net>\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n .gitignore              |   1 +\n Makefile                |   1 +\n builtin.h               |   1 +\n builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++\n git-stash.sh            |  78 +------\n git.c                   |   1 +\n 6 files changed, 463 insertions(+), 71 deletions(-)\n create mode 100644 builtin/stash--helper.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 0d77ea5894..6ecab90ab2 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -162,6 +162,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 bbfbb4292d..aa83545e94 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1115,6 +1115,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 6538932e99..ff4460aff7 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -225,6 +225,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..997b1c0ecf\n--- /dev/null\n+++ b/builtin/stash--helper.c\n@@ -0,0 +1,452 @@\n+#include \"builtin.h\"\n+#include \"config.h\"\n+#include \"parse-options.h\"\n+#include \"refs.h\"\n+#include \"lockfile.h\"\n+#include \"cache-tree.h\"\n+#include \"unpack-trees.h\"\n+#include \"merge-recursive.h\"\n+#include \"argv-array.h\"\n+#include \"run-command.h\"\n+#include \"dir.h\"\n+#include \"rerere.h\"\n+\n+static const char * const git_stash_helper_usage[] = {\n+\tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n+\tNULL\n+};\n+\n+static const char * const git_stash_helper_apply_usage[] = {\n+\tN_(\"git stash--helper apply [--index] [-q|--quiet] [<stash>]\"),\n+\tNULL\n+};\n+\n+static const char *ref_stash = \"refs/stash\";\n+static struct strbuf stash_index_path = STRBUF_INIT;\n+\n+/*\n+ * w_commit is set to the commit containing the working tree\n+ * b_commit is set to the base commit\n+ * i_commit is set to the commit containing the index tree\n+ * u_commit is set to the commit containing the untracked files tree\n+ * w_tree is set to the working tree\n+ * b_tree is set to the base tree\n+ * i_tree is set to the index tree\n+ * u_tree is set to the untracked files tree\n+ */\n+\n+struct stash_info {\n+\tstruct object_id w_commit;\n+\tstruct object_id b_commit;\n+\tstruct object_id i_commit;\n+\tstruct object_id u_commit;\n+\tstruct object_id w_tree;\n+\tstruct object_id b_tree;\n+\tstruct object_id i_tree;\n+\tstruct object_id u_tree;\n+\tstruct strbuf revision;\n+\tint is_stash_ref;\n+\tint has_u;\n+};\n+\n+static void free_stash_info(struct stash_info *info)\n+{\n+\tstrbuf_release(&info->revision);\n+}\n+\n+static void assert_stash_like(struct stash_info *info, const char *revision)\n+{\n+\tif (get_oidf(&info->b_commit, \"%s^1\", revision) ||\n+\t    get_oidf(&info->w_tree, \"%s:\", revision) ||\n+\t    get_oidf(&info->b_tree, \"%s^1:\", revision) ||\n+\t    get_oidf(&info->i_tree, \"%s^2:\", revision))\n+\t\tdie(_(\"'%s' is not a stash-like commit\"), revision);\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+\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 (oideq(&info->b_tree, &info->i_tree) ||\n+\t\t    oideq(&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 (oideq(&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 2f604a41ea..76ee02802e 100644\n--- a/git.c\n+++ b/git.c\n@@ -554,6 +554,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.1.878.g0482332a22\n\n"},{"id":"363940","messageId":"a91425232407f513901adf1711981b41e73bacf2.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 11/22] stash: convert branch to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:31Z","receivedAt":"2018-11-22T23:06:28Z","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 07b8ec5bcb..68b65165e4 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@@ -537,6 +543,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@@ -563,6 +607,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.1.878.g0482332a22\n\n"},{"id":"363942","messageId":"44347155280b80aa50599ba81fcadf9a25039fc5.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 15/22] stash: convert store to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:35Z","receivedAt":"2018-11-22T23:06:30Z","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 36651f745a..5dc6c068d7 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@@ -730,6 +735,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@@ -764,6 +824,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.1.878.g0482332a22\n\n"},{"id":"363941","messageId":"a77b1240e4d08964dc0c8e634282924a53871ea8.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 13/22] stash: convert list to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:33Z","receivedAt":"2018-11-22T23:06:31Z","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 d7ff78784b..d66a4589a5 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@@ -616,6 +622,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@@ -646,6 +675,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.1.878.g0482332a22\n\n"},{"id":"363943","messageId":"3fef00c4cb29a9096eefd0ee1512b58a5b658ccc.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 12/22] stash: convert pop to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:32Z","receivedAt":"2018-11-22T23:06:31Z","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 68b65165e4..d7ff78784b 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@@ -543,6 +548,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@@ -607,6 +642,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.1.878.g0482332a22\n\n"},{"id":"363944","messageId":"e6dadd8ace85f893041fc0b380c69b368e3490f0.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 16/22] stash: convert create to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:36Z","receivedAt":"2018-11-22T23:06:34Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Add stash create to the helper.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c | 451 +++++++++++++++++++++++++++++++++++++++-\n git-stash.sh            |   2 +-\n 2 files changed, 451 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 5dc6c068d7..cd769d87b3 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@@ -288,6 +296,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@@ -790,6 +816,427 @@ 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+\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, struct strbuf *stash_msg_buf,\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 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\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\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\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\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\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\tgoto done;\n+\t\t}\n+\t}\n+\n+\tif (!stash_msg_buf->len)\n+\t\tstrbuf_addf(stash_msg_buf, \"WIP on %s\", msg.buf);\n+\telse\n+\t\tstrbuf_insertf(stash_msg_buf, 0, \"On %s: \", branch_name);\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_buf->buf, stash_msg_buf->len, &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+\treturn ret;\n+}\n+\n+static int create_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint include_untracked = 0;\n+\tint ret = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\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+\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n+\tret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info);\n+\n+\tif (!ret)\n+\t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n+\n+\tstrbuf_release(&stash_msg_buf);\n+\n+\t/*\n+\t * ret can be 1 if there were no changes. In this case, we should\n+\t * not error out.\n+\t */\n+\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@@ -799,7 +1246,7 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\tgit_config(git_default_config, NULL);\n+\tgit_config(git_diff_basic_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@@ -826,6 +1273,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.1.878.g0482332a22\n\n"},{"id":"363945","messageId":"78914337d41163aa0ddb8014e9a8a259241f0d7d.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 18/22] stash: make push -q quiet","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:38Z","receivedAt":"2018-11-22T23:06:34Z","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 | 56 ++++++++++++++++++++++++++---------------\n t/t3903-stash.sh        | 23 +++++++++++++++++\n 2 files changed, 59 insertions(+), 20 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 8683c662fc..0dd5dbade6 100644\n--- a/builtin/stash--helper.c\n+++ b/builtin/stash--helper.c\n@@ -971,7 +971,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@@ -1024,7 +1024,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@@ -1102,7 +1103,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \n static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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@@ -1120,7 +1122,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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\tgoto done;\n \t} else {\n@@ -1145,7 +1149,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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\tgoto done;\n \t}\n@@ -1153,26 +1159,29 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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\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\tret = stash_patch(info, ps, patch, quiet);\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\tgoto done;\n \t\t}\n@@ -1198,7 +1207,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\n \n \tif (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &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@@ -1233,7 +1244,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \tmemset(&ps, 0, sizeof(ps));\n \tstrbuf_addstr(&stash_msg_buf, stash_msg);\n \tret = do_create_stash(ps, &stash_msg_buf, 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@@ -1296,26 +1307,29 @@ static int do_push_stash(struct pathspec ps, const 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 (stash_msg)\n \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n \tif (do_create_stash(ps, &stash_msg_buf, 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_buf.buf, 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\"),\n-\t\t  stash_msg_buf.buf);\n+\tif (!quiet)\n+\t\tprintf_ln(_(\"Saved working directory and index state %s\"),\n+\t\t\t  stash_msg_buf.buf);\n \n \tif (!patch_mode) {\n \t\tif (include_untracked && !ps.nr) {\n@@ -1416,7 +1430,9 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n \t\targv_array_pushl(&cp.args, \"apply\", \"-R\", NULL);\n \n \t\tif (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {\n-\t\t\tfprintf_ln(stderr, _(\"Cannot remove worktree changes\"));\n+\t\t\tif (!quiet)\n+\t\t\t\tfprintf_ln(stderr, _(\"Cannot remove \"\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 8b09a3d6cc..77e0b72035 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.1.878.g0482332a22\n\n"},{"id":"363946","messageId":"d57ef97e09dc038aeae1353684f32ce50e851ac1.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 17/22] stash: convert push to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:37Z","receivedAt":"2018-11-22T23:06:35Z","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 | 245 +++++++++++++++++++++++++++++++++++++++-\n git-stash.sh            |   6 +-\n 2 files changed, 245 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex cd769d87b3..8683c662fc 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@@ -1092,7 +1102,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)\n \n static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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@@ -1105,7 +1115,6 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\n \tstruct strbuf msg = STRBUF_INIT;\n \tstruct strbuf commit_tree_label = STRBUF_INIT;\n \tstruct strbuf untracked_files = STRBUF_INIT;\n-\tstruct strbuf patch = STRBUF_INIT;\n \n \tread_cache_preload(NULL);\n \trefresh_cache(REFRESH_QUIET);\n@@ -1152,7 +1161,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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\tif (ret < 0) {\n \t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t     \"worktree state\"));\n@@ -1223,7 +1232,8 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \n \tmemset(&ps, 0, sizeof(ps));\n \tstrbuf_addstr(&stash_msg_buf, stash_msg);\n-\tret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info);\n+\tret = do_create_stash(ps, &stash_msg_buf, 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@@ -1237,6 +1247,231 @@ 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, const char *stash_msg, int quiet,\n+\t\t\t int keep_index, int patch_mode, int include_untracked)\n+{\n+\tint ret = 0;\n+\tstruct stash_info info;\n+\tstruct strbuf patch = STRBUF_INIT;\n+\tstruct strbuf stash_msg_buf = 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 (stash_msg)\n+\t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n+\tif (do_create_stash(ps, &stash_msg_buf, 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_buf.buf, 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\"),\n+\t\t  stash_msg_buf.buf);\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+\tstrbuf_release(&stash_msg_buf);\n+\treturn ret;\n+}\n+\n+static int push_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct pathspec ps;\n+\tstruct option options[] = {\n+\t\tOPT_BOOL('k', \"keep-index\", &keep_index,\n+\t\t\t N_(\"keep index\")),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\t N_(\"stash in patch mode\")),\n+\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, N_(\"message\"),\n+\t\t\t   N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_push_usage,\n+\t\t\t     0);\n+\n+\tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n+\treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n+\t\t\t     include_untracked);\n+}\n+\n int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n {\n \tpid_t pid = getpid();\n@@ -1275,6 +1510,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.1.878.g0482332a22\n\n"},{"id":"363949","messageId":"4d33f8382f789501d7801ae46bd85e47b9503673.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 19/22] stash: convert save to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:39Z","receivedAt":"2018-11-22T23:06:36Z","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\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/stash--helper.c |  50 +++++++\n git-stash.sh            | 311 +---------------------------------------\n 2 files changed, 52 insertions(+), 309 deletions(-)\n\ndiff --git a/builtin/stash--helper.c b/builtin/stash--helper.c\nindex 0dd5dbade6..47a0ab6669 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@@ -1488,6 +1496,46 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\t\t     include_untracked);\n }\n \n+static int save_stash(int argc, const char **argv, const char *prefix)\n+{\n+\tint keep_index = -1;\n+\tint patch_mode = 0;\n+\tint include_untracked = 0;\n+\tint quiet = 0;\n+\tint ret = 0;\n+\tconst char *stash_msg = NULL;\n+\tstruct pathspec ps;\n+\tstruct strbuf stash_msg_buf = STRBUF_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_BOOL('k', \"keep-index\", &keep_index,\n+\t\t\t N_(\"keep index\")),\n+\t\tOPT_BOOL('p', \"patch\", &patch_mode,\n+\t\t\t N_(\"stash in patch mode\")),\n+\t\tOPT__QUIET(&quiet, N_(\"quiet mode\")),\n+\t\tOPT_BOOL('u', \"include-untracked\", &include_untracked,\n+\t\t\t N_(\"include untracked files in stash\")),\n+\t\tOPT_SET_INT('a', \"all\", &include_untracked,\n+\t\t\t    N_(\"include ignore files\"), 2),\n+\t\tOPT_STRING('m', \"message\", &stash_msg, \"message\",\n+\t\t\t   N_(\"stash message\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_stash_helper_save_usage,\n+\t\t\t     PARSE_OPT_KEEP_DASHDASH);\n+\n+\tif (argc)\n+\t\tstash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');\n+\n+\tmemset(&ps, 0, sizeof(ps));\n+\tret = do_push_stash(ps, stash_msg, quiet, keep_index,\n+\t\t\t    patch_mode, include_untracked);\n+\n+\tstrbuf_release(&stash_msg_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@@ -1528,6 +1576,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.1.878.g0482332a22\n\n"},{"id":"363947","messageId":"de2ebf96ee5f94a1cdadee67e436f5bd1b0fb6a5.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 20/22] stash: convert `stash--helper.c` into `stash.c`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:40Z","receivedAt":"2018-11-22T23:06:37Z","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} | 157 +++++++++++++++------------\n git-stash.sh                         | 153 --------------------------\n git.c                                |   2 +-\n 6 files changed, 92 insertions(+), 226 deletions(-)\n rename builtin/{stash--helper.c => stash.c} (91%)\n delete mode 100755 git-stash.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex 6ecab90ab2..0d77ea5894 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -162,7 +162,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 aa83545e94..450936fcaf 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -619,7 +619,6 @@ SCRIPT_SH += git-quiltimport.sh\n SCRIPT_SH += git-legacy-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@@ -1115,7 +1114,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 ff4460aff7..b78ab6e30b 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -225,7 +225,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);\n extern int cmd_show_branch(int argc, const char **argv, const char *prefix);\n extern int cmd_show_index(int argc, const char **argv, const char *prefix);\n extern int cmd_status(int argc, const char **argv, const char *prefix);\n-extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);\n+extern int cmd_stash(int argc, const char **argv, const char *prefix);\n extern int cmd_stripspace(int argc, const char **argv, const char *prefix);\n extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);\n extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);\ndiff --git a/builtin/stash--helper.c b/builtin/stash.c\nsimilarity index 91%\nrename from builtin/stash--helper.c\nrename to builtin/stash.c\nindex 47a0ab6669..c76a1936d5 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@@ -221,7 +216,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@@ -526,7 +521,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@@ -599,7 +594,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@@ -625,7 +620,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@@ -652,7 +647,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@@ -687,7 +682,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@@ -767,7 +762,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@@ -813,7 +808,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@@ -1231,28 +1226,16 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\n \n static int create_stash(int argc, const char **argv, const char *prefix)\n {\n-\tint include_untracked = 0;\n \tint ret = 0;\n-\tconst char *stash_msg = NULL;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\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+\t/* Starting with argv[1], since argv[0] is \"create\" */\n+\tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n-\tret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,\n-\t\t\t      NULL, 0);\n+\tret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0);\n \n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n@@ -1487,9 +1470,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\targc = parse_options(argc, argv, prefix, options,\n-\t\t\t     git_stash_helper_push_usage,\n-\t\t\t     0);\n+\tif (argc)\n+\t\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t\t     git_stash_push_usage,\n+\t\t\t\t     0);\n \n \tparse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);\n \treturn do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,\n@@ -1522,7 +1506,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@@ -1536,10 +1520,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@@ -1547,16 +1533,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)\n \n \tgit_config(git_diff_basic_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)\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@@ -1578,7 +1564,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 76ee02802e..49ab91b4ec 100644\n--- a/git.c\n+++ b/git.c\n@@ -554,7 +554,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.1.878.g0482332a22\n\n"},{"id":"363948","messageId":"7ca8408f1e8c192324ac276ec42559c3343e9eee.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 21/22] stash: optimize `get_untracked_files()` and `check_changes()`","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:41Z","receivedAt":"2018-11-22T23:06:38Z","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 c76a1936d5..5ad0f443ca 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -879,18 +879,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@@ -918,14 +918,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@@ -1134,7 +1146,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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@@ -1159,8 +1171,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,\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@@ -1235,18 +1246,14 @@ static int create_stash(int argc, const char **argv, const char *prefix)\n \tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tret = do_create_stash(ps, &stash_msg_buf, 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_buf, 0, 0, &info, NULL, 0)))\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n \tstrbuf_release(&stash_msg_buf);\n-\n-\t/*\n-\t * ret can be 1 if there were no changes. In this case, we should\n-\t * not error out.\n-\t */\n-\treturn ret < 0;\n+\treturn ret;\n }\n \n static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n@@ -1256,6 +1263,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,\n \tstruct stash_info info;\n \tstruct strbuf patch = STRBUF_INIT;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n+\tstruct strbuf untracked_files = STRBUF_INIT;\n \n \tif (patch_mode && keep_index == -1)\n \t\tkeep_index = 1;\n@@ -1290,7 +1298,7 @@ static int do_push_stash(struct pathspec ps, const 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.1.878.g0482332a22\n\n"},{"id":"363950","messageId":"0482332a22d527333b1b62a2ed293e6d92acd64e.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 22/22] stash: replace all `write-tree` child processes with API calls","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:42Z","receivedAt":"2018-11-22T23:06:38Z","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 5ad0f443ca..029e209176 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -945,9 +945,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@@ -962,15 +961,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@@ -979,8 +974,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@@ -989,11 +984,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@@ -1019,17 +1013,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@@ -1045,7 +1034,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@@ -1055,9 +1044,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@@ -1096,20 +1084,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.1.878.g0482332a22\n\n"},{"id":"363951","messageId":"3e141cf7471ea0db65a5308105d549cd59388a6a.1542925164.git.ungureanupaulsebastian@gmail.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"[PATCH v11 14/22] stash: convert show to builtin","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-11-22T23:05:34Z","receivedAt":"2018-11-22T23:06:43Z","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 d66a4589a5..36651f745a 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@@ -645,6 +653,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@@ -677,6 +762,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.1.878.g0482332a22\n\n"},{"id":"364039","messageId":"20181125214353.GI4883@hank.intra.tgummerer.com","threadId":"49883","inReplyTo":"e8d86fae660a79eabcf4764dfa9986282c097242.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v11 03/22] strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-11-25T21:43:53Z","receivedAt":"2018-11-25T21:44:00Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 11/23, Paul-Sebastian Ungureanu wrote:\n> Implement `strbuf_insertf()` and `strbuf_vinsertf()` to\n> insert data using a printf format string.\n> \n> Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  strbuf.c | 36 ++++++++++++++++++++++++++++++++++++\n>  strbuf.h |  9 +++++++++\n>  2 files changed, 45 insertions(+)\n> \n> diff --git a/strbuf.c b/strbuf.c\n> index 82e90f1dfe..bfbbdadbf3 100644\n> --- a/strbuf.c\n> +++ b/strbuf.c\n> @@ -249,6 +249,42 @@ void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)\n>  \tstrbuf_splice(sb, pos, 0, data, len);\n>  }\n>  \n> +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)\n> +{\n> +\tint len, len2;\n> +\tchar save;\n> +\tva_list cp;\n> +\n> +\tif (pos > sb->len)\n> +\t\tdie(\"`pos' is too far after the end of the buffer\");\n\nI was going to ask about translation of this and other messages in\n'die()' calls, but I see other messages in strbuf.c are not marked for\ntranslation either.  It may make sense to mark them all for\ntranslation at some point in the future, but having them all\nuntranslated for now makes sense.\n\nIn the long run it may even be better to return an error here rather\nthan 'die()'ing, but again this is consistent with the rest of the\nAPI, so this wouldn't be a good time to take that on.\n\n> +\tva_copy(cp, ap);\n> +\tlen = vsnprintf(sb->buf + sb->len, 0, fmt, cp);\n\nHere we're just getting the length of what we're trying to format\n(excluding the final NUL).  As the second argument is 0, we do not\nmodify the strbuf at this point...\n\n> +\tva_end(cp);\n> +\tif (len < 0)\n> +\t\tBUG(\"your vsnprintf is broken (returned %d)\", len);\n> +\tif (!len)\n> +\t\treturn; /* nothing to do */\n> +\tif (unsigned_add_overflows(sb->len, len))\n> +\t\tdie(\"you want to use way too much memory\");\n> +\tstrbuf_grow(sb, len);\n\n... and then we grow the strbuf by the length we previously, which\nexcludes the NUL character, plus one extra character, so even if pos\n== len we are sure to have enough space in the strbuf ...\n\n> +\tmemmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);\n> +\t/* vsnprintf() will append a NUL, overwriting one of our characters */\n> +\tsave = sb->buf[pos + len];\n> +\tlen2 = vsnprintf(sb->buf + pos, sb->alloc - sb->len, fmt, ap);\n\n... and we use vsnprintf to write the formatted string to the\nbeginning of the buffer.  sb->alloc - sb->len can be larger than\n'len', which is fine as vsnprintf doesn't write anything after the NUL\ncharacter.  And as 'strbuf_grow' adds len + 1 bytes to the strbuf\nwe'll always have enough space for adding the formatted string ...\n\n> +\tsb->buf[pos + len] = save;\n> +\tif (len2 != len)\n> +\t\tBUG(\"your vsnprintf is broken (returns inconsistent lengths)\");\n> +\tstrbuf_setlen(sb, sb->len + len);\n\nAnd finally we set the strbuf to the new length.  So all this is just\na very roundabout way to say that this function does the right thing\naccording to my reading (and tests).\n\n> +}\n> +\n> +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...)\n> +{\n> +\tva_list ap;\n> +\tva_start(ap, fmt);\n> +\tstrbuf_vinsertf(sb, pos, fmt, ap);\n> +\tva_end(ap);\n> +}\n> +\n>  void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)\n>  {\n>  \tstrbuf_splice(sb, pos, len, \"\", 0);\n> diff --git a/strbuf.h b/strbuf.h\n> index be02150df3..8f8fe01e68 100644\n> --- a/strbuf.h\n> +++ b/strbuf.h\n> @@ -244,6 +244,15 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);\n>   */\n>  void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);\n>  \n> +/**\n> + * Insert data to the given position of the buffer giving a printf format\n> + * string. The contents will be shifted, not overwritten.\n> + */\n> +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt,\n> +\t\t     va_list ap);\n> +\n> +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...);\n> +\n>  /**\n>   * Remove given amount of data from a given position of the buffer.\n>   */\n> -- \n> 2.19.1.878.g0482332a22\n> \n"},{"id":"364040","messageId":"20181125215504.GJ4883@hank.intra.tgummerer.com","threadId":"49883","inReplyTo":"cover.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v11 00/22] Convert \"git stash\" to C builtin","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-11-25T21:55:04Z","receivedAt":"2018-11-25T21:55:10Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 11/23, Paul-Sebastian Ungureanu wrote:\n> Hello,\n> \n> This is the 11th iteration of C git stash. Here are some of the changes,\n> based on Thomas's and dscho's suggestions (from mailing list / pull request\n> #495):\n\nThanks for your work on this!  I have read through the range-diff and\nthe new patch of this last round, and this addresses all the comments\nI had on v10 (and some more :)).  I consider it\nReviewed-by: Thomas Gummerer <t.gummerer@gmail.com>\n\n> - improved memory management. Now, the callers of `do_create_stash()`\n> are responsible of freeing the parameter they pass in. Moreover, the\n> stash message is now a pointer to a buffer (in the previous iteration\n> it was a pointer to a string). This should make it more clear who is\n> responsible of freeing the memory.\n> \n> - added `strbuf_insertf()` which inserts a format string at a given\n> position in the buffer.\n> \n> - some minor changes (changed \"!oidcmp\" to \"oideq\")\n> \n> - fixed merge conflicts\n> \n> Best regards,\n> Paul\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 (17):\n>   sha1-name.c: add `get_oidf()` which acts like `get_oid()`\n>   strbuf.c: add `strbuf_join_argv()`\n>   strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`\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              | 1596 ++++++++++++++++++++++++++++++++++\n>  cache.h                      |    1 +\n>  git-stash.sh                 |  752 ----------------\n>  git.c                        |    1 +\n>  sha1-name.c                  |   19 +\n>  strbuf.c                     |   51 ++\n>  strbuf.h                     |   16 +\n>  t/t3903-stash.sh             |  192 ++--\n>  t/t3907-stash-show-config.sh |   83 ++\n>  12 files changed, 1897 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.1.878.g0482332a22\n> \n"},{"id":"364053","messageId":"xmqqefb8z9qh.fsf@gitster-ct.c.googlers.com","threadId":"49883","inReplyTo":"de2ebf96ee5f94a1cdadee67e436f5bd1b0fb6a5.1542925164.git.ungureanupaulsebastian@gmail.com","subject":"Re: [PATCH v11 20/22] stash: convert `stash--helper.c` into `stash.c`","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-11-26T05:30:30Z","receivedAt":"2018-11-26T05:30:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n\n> The old shell script `git-stash.sh`  was removed and replaced\n> entirely by `builtin/stash.c`. In order to do that, `create` and\n> `push` were adapted to work without `stash.sh`. For example, before\n> this commit, `git stash create` called `git stash--helper create\n> --message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\n> some of these changes wouldn't have been necessary.\n>\n> This commit also removes the word `helper` since now stash is\n> called directly and not by a shell script.\n>\n> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> ---\n>  .gitignore                           |   1 -\n>  Makefile                             |   3 +-\n>  builtin.h                            |   2 +-\n>  builtin/{stash--helper.c => stash.c} | 157 +++++++++++++++------------\n>  git-stash.sh                         | 153 --------------------------\n>  git.c                                |   2 +-\n>  6 files changed, 92 insertions(+), 226 deletions(-)\n>  rename builtin/{stash--helper.c => stash.c} (91%)\n>  delete mode 100755 git-stash.sh\n\nSeeing the recent trouble in \"rebase in C\" and how keeping the\nscripted version as \"git legacy-rebase\" helped us postpone the\nrewritten version without ripping the whole thing out, I wonder if\nwe can do the same here.\n\nAlso, the remaining two patches should be done _before_ this step, I\nwould think.  I can understand it if the reason you have those two\nafter this step is because you found the opportunity for these\nimprovements after you wrote this step, but in the larger picture\nseen by the end users of the \"stash in C\" and those developers who\nfollow the evolution of the code, the logical place for this \"now we\nhave everything in C, we retire the scripted version\" step to happen\nis at the very end.\n\nThanks.\n"},{"id":"364054","messageId":"xmqqa7lwz8xm.fsf@gitster-ct.c.googlers.com","threadId":"49883","inReplyTo":"20181125215504.GJ4883@hank.intra.tgummerer.com","subject":"Re: [PATCH v11 00/22] Convert \"git stash\" to C builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-11-26T05:47:49Z","receivedAt":"2018-11-26T05:48:00Z","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> Thanks for your work on this!  I have read through the range-diff and\n> the new patch of this last round, and this addresses all the comments\n> I had on v10 (and some more :)).  I consider it\n> Reviewed-by: Thomas Gummerer <t.gummerer@gmail.com>\n\nThanks.\n\nOne thing that bothers me is that this seems to have been rebased on\n'master', but as long as we are rebasing, the updated series must\nalso take into account of the sd/stash-wo-user-name topic, i.e. if\nwe are rebasing it, it should be rebased on top of the result of\n\n\tgit checkout -B ps/rebase-in-c master\n\tgit merge --no-ff sd/stash-wo-user-name\n\nI think.\n"},{"id":"364066","messageId":"xmqq8t1gwano.fsf@gitster-ct.c.googlers.com","threadId":"49883","inReplyTo":"xmqqa7lwz8xm.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v11 00/22] Convert \"git stash\" to C builtin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-11-26T07:38:51Z","receivedAt":"2018-11-26T07:38:57Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Thomas Gummerer <t.gummerer@gmail.com> writes:\n>\n>> Thanks for your work on this!  I have read through the range-diff and\n>> the new patch of this last round, and this addresses all the comments\n>> I had on v10 (and some more :)).  I consider it\n>> Reviewed-by: Thomas Gummerer <t.gummerer@gmail.com>\n>\n> Thanks.\n>\n> One thing that bothers me is that this seems to have been rebased on\n> 'master', but as long as we are rebasing, the updated series must\n> also take into account of the sd/stash-wo-user-name topic, i.e. if\n> we are rebasing it, it should be rebased on top of the result of\n>\n> \tgit checkout -B ps/rebase-in-c master\n> \tgit merge --no-ff sd/stash-wo-user-name\n>\n> I think.\n\nhttps://travis-ci.org/git/git/builds/459619672 would show that this\nC reimplementation now regresses from the scripted version due to\nlack of such rebasing (i.e. porting a correction from scripted one).\n\n"},{"id":"364131","messageId":"nycvar.QRO.7.76.6.1811271428560.41@tvgsbejvaqbjf.bet","threadId":"49883","inReplyTo":"20181125214353.GI4883@hank.intra.tgummerer.com","subject":"Re: [PATCH v11 03/22] strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-11-27T13:35:30Z","receivedAt":"2018-11-27T13:35:50Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 25 Nov 2018, Thomas Gummerer wrote:\n\n> On 11/23, Paul-Sebastian Ungureanu wrote:\n> > Implement `strbuf_insertf()` and `strbuf_vinsertf()` to\n> > insert data using a printf format string.\n> > \n> > Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n> > Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> > ---\n> >  strbuf.c | 36 ++++++++++++++++++++++++++++++++++++\n> >  strbuf.h |  9 +++++++++\n> >  2 files changed, 45 insertions(+)\n> > \n> > diff --git a/strbuf.c b/strbuf.c\n> > index 82e90f1dfe..bfbbdadbf3 100644\n> > --- a/strbuf.c\n> > +++ b/strbuf.c\n> > @@ -249,6 +249,42 @@ void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)\n> >  \tstrbuf_splice(sb, pos, 0, data, len);\n> >  }\n> >  \n> > +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)\n> > +{\n> > +\tint len, len2;\n> > +\tchar save;\n> > +\tva_list cp;\n> > +\n> > +\tif (pos > sb->len)\n> > +\t\tdie(\"`pos' is too far after the end of the buffer\");\n> \n> I was going to ask about translation of this and other messages in\n> 'die()' calls, but I see other messages in strbuf.c are not marked for\n> translation either.  It may make sense to mark them all for\n> translation at some point in the future, but having them all\n> untranslated for now makes sense.\n> \n> In the long run it may even be better to return an error here rather\n> than 'die()'ing, but again this is consistent with the rest of the\n> API, so this wouldn't be a good time to take that on.\n\nI guess I was too overzealous in my copying. These conditions really\nindicate bugs in the caller... So I'd actually rather change that die() to\nBUG().\n\nBut then, the original code in strbuf_vaddf() calls die() and would have\nto be changed, too.\n\n> > +\tva_copy(cp, ap);\n> > +\tlen = vsnprintf(sb->buf + sb->len, 0, fmt, cp);\n> \n> Here we're just getting the length of what we're trying to format\n> (excluding the final NUL).  As the second argument is 0, we do not\n> modify the strbuf at this point...\n> \n> > +\tva_end(cp);\n> > +\tif (len < 0)\n> > +\t\tBUG(\"your vsnprintf is broken (returned %d)\", len);\n> > +\tif (!len)\n> > +\t\treturn; /* nothing to do */\n> > +\tif (unsigned_add_overflows(sb->len, len))\n> > +\t\tdie(\"you want to use way too much memory\");\n> > +\tstrbuf_grow(sb, len);\n> \n> ... and then we grow the strbuf by the length we previously, which\n> excludes the NUL character, plus one extra character, so even if pos\n> == len we are sure to have enough space in the strbuf ...\n> \n> > +\tmemmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);\n> > +\t/* vsnprintf() will append a NUL, overwriting one of our characters */\n> > +\tsave = sb->buf[pos + len];\n> > +\tlen2 = vsnprintf(sb->buf + pos, sb->alloc - sb->len, fmt, ap);\n> \n> ... and we use vsnprintf to write the formatted string to the\n> beginning of the buffer.\n\nIt is not actually the beginning of the buffer, but possibly the middle of\nthe buffer ;-)\n\n> sb->alloc - sb->len can be larger than 'len', which is fine as vsnprintf\n> doesn't write anything after the NUL character.  And as 'strbuf_grow'\n> adds len + 1 bytes to the strbuf we'll always have enough space for\n> adding the formatted string ...\n> \n> > +\tsb->buf[pos + len] = save;\n> > +\tif (len2 != len)\n> > +\t\tBUG(\"your vsnprintf is broken (returns inconsistent lengths)\");\n> > +\tstrbuf_setlen(sb, sb->len + len);\n> \n> And finally we set the strbuf to the new length.  So all this is just\n> a very roundabout way to say that this function does the right thing\n> according to my reading (and tests).\n\n:-)\n\nIt seems that Junio likes this way of reviewing, giving him confidence\nthat the review was thorough.\n\nThanks!\nDscho\n\n> > +}\n> > +\n> > +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...)\n> > +{\n> > +\tva_list ap;\n> > +\tva_start(ap, fmt);\n> > +\tstrbuf_vinsertf(sb, pos, fmt, ap);\n> > +\tva_end(ap);\n> > +}\n> > +\n> >  void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)\n> >  {\n> >  \tstrbuf_splice(sb, pos, len, \"\", 0);\n> > diff --git a/strbuf.h b/strbuf.h\n> > index be02150df3..8f8fe01e68 100644\n> > --- a/strbuf.h\n> > +++ b/strbuf.h\n> > @@ -244,6 +244,15 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);\n> >   */\n> >  void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);\n> >  \n> > +/**\n> > + * Insert data to the given position of the buffer giving a printf format\n> > + * string. The contents will be shifted, not overwritten.\n> > + */\n> > +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt,\n> > +\t\t     va_list ap);\n> > +\n> > +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...);\n> > +\n> >  /**\n> >   * Remove given amount of data from a given position of the buffer.\n> >   */\n> > -- \n> > 2.19.1.878.g0482332a22\n> > \n> \n"},{"id":"364132","messageId":"nycvar.QRO.7.76.6.1811271438310.41@tvgsbejvaqbjf.bet","threadId":"49883","inReplyTo":"xmqqefb8z9qh.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v11 20/22] stash: convert `stash--helper.c` into `stash.c`","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-11-27T13:46:49Z","receivedAt":"2018-11-27T13:47:12Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Junio & Paul,\n\nOn Mon, 26 Nov 2018, Junio C Hamano wrote:\n\n> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n> \n> > The old shell script `git-stash.sh`  was removed and replaced\n> > entirely by `builtin/stash.c`. In order to do that, `create` and\n> > `push` were adapted to work without `stash.sh`. For example, before\n> > this commit, `git stash create` called `git stash--helper create\n> > --message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\n> > some of these changes wouldn't have been necessary.\n> >\n> > This commit also removes the word `helper` since now stash is\n> > called directly and not by a shell script.\n> >\n> > Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> > ---\n> >  .gitignore                           |   1 -\n> >  Makefile                             |   3 +-\n> >  builtin.h                            |   2 +-\n> >  builtin/{stash--helper.c => stash.c} | 157 +++++++++++++++------------\n> >  git-stash.sh                         | 153 --------------------------\n> >  git.c                                |   2 +-\n> >  6 files changed, 92 insertions(+), 226 deletions(-)\n> >  rename builtin/{stash--helper.c => stash.c} (91%)\n> >  delete mode 100755 git-stash.sh\n> \n> Seeing the recent trouble in \"rebase in C\" and how keeping the\n> scripted version as \"git legacy-rebase\" helped us postpone the\n> rewritten version without ripping the whole thing out, I wonder if\n> we can do the same here.\n\nFeel very free to cherry-pick\nhttps://github.com/git-for-windows/git/commit/004da7e7faa36c872868ae938e06594ea1c2f01c\nand\nhttps://github.com/git-for-windows/git/commit/cedfcd39f5a4e4beb33e16fa67c4659fd4bdabf6\nwhich is what we carry in Git for Windows.\n\nCiao,\nDscho\n\n> Also, the remaining two patches should be done _before_ this step, I\n> would think.  I can understand it if the reason you have those two\n> after this step is because you found the opportunity for these\n> improvements after you wrote this step, but in the larger picture\n> seen by the end users of the \"stash in C\" and those developers who\n> follow the evolution of the code, the logical place for this \"now we\n> have everything in C, we retire the scripted version\" step to happen\n> is at the very end.\n> \n> Thanks.\n> \n"},{"id":"364166","messageId":"20181127223256.GM4883@hank.intra.tgummerer.com","threadId":"49883","inReplyTo":"nycvar.QRO.7.76.6.1811271428560.41@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v11 03/22] strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`","fromName":"Thomas Gummerer","fromEmail":"t.gummerer@gmail.com","sentAt":"2018-11-27T22:32:56Z","receivedAt":"2018-11-27T22:33:03Z","isPatch":true,"sender":{"key":"t.gummerer@gmail.com","avatar":"https://avatars.githubusercontent.com/u/191004?v=4"},"body":"On 11/27, Johannes Schindelin wrote:\n> Hi,\n> \n> On Sun, 25 Nov 2018, Thomas Gummerer wrote:\n> \n> > On 11/23, Paul-Sebastian Ungureanu wrote:\n> > > Implement `strbuf_insertf()` and `strbuf_vinsertf()` to\n> > > insert data using a printf format string.\n> > > \n> > > Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n> > > Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n> > > ---\n> > >  strbuf.c | 36 ++++++++++++++++++++++++++++++++++++\n> > >  strbuf.h |  9 +++++++++\n> > >  2 files changed, 45 insertions(+)\n> > > \n> > > diff --git a/strbuf.c b/strbuf.c\n> > > index 82e90f1dfe..bfbbdadbf3 100644\n> > > --- a/strbuf.c\n> > > +++ b/strbuf.c\n> > > @@ -249,6 +249,42 @@ void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)\n> > >  \tstrbuf_splice(sb, pos, 0, data, len);\n> > >  }\n> > >  \n> > > +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)\n> > > +{\n> > > +\tint len, len2;\n> > > +\tchar save;\n> > > +\tva_list cp;\n> > > +\n> > > +\tif (pos > sb->len)\n> > > +\t\tdie(\"`pos' is too far after the end of the buffer\");\n> > \n> > I was going to ask about translation of this and other messages in\n> > 'die()' calls, but I see other messages in strbuf.c are not marked for\n> > translation either.  It may make sense to mark them all for\n> > translation at some point in the future, but having them all\n> > untranslated for now makes sense.\n> > \n> > In the long run it may even be better to return an error here rather\n> > than 'die()'ing, but again this is consistent with the rest of the\n> > API, so this wouldn't be a good time to take that on.\n> \n> I guess I was too overzealous in my copying. These conditions really\n> indicate bugs in the caller... So I'd actually rather change that die() to\n> BUG().\n> \n> But then, the original code in strbuf_vaddf() calls die() and would have\n> to be changed, too.\n\nRight, making these 'BUG()' makes sense to me.  But at this stage of\nthe series it's probably better to just aim for consistency with the\nsurrounding code without starting to do more cleanups that were not\nincluded in earlier iterations.  I think that's best left for patches\non top.\n\n> > > +\tva_copy(cp, ap);\n> > > +\tlen = vsnprintf(sb->buf + sb->len, 0, fmt, cp);\n> > \n> > Here we're just getting the length of what we're trying to format\n> > (excluding the final NUL).  As the second argument is 0, we do not\n> > modify the strbuf at this point...\n> > \n> > > +\tva_end(cp);\n> > > +\tif (len < 0)\n> > > +\t\tBUG(\"your vsnprintf is broken (returned %d)\", len);\n> > > +\tif (!len)\n> > > +\t\treturn; /* nothing to do */\n> > > +\tif (unsigned_add_overflows(sb->len, len))\n> > > +\t\tdie(\"you want to use way too much memory\");\n> > > +\tstrbuf_grow(sb, len);\n> > \n> > ... and then we grow the strbuf by the length we previously, which\n> > excludes the NUL character, plus one extra character, so even if pos\n> > == len we are sure to have enough space in the strbuf ...\n> > \n> > > +\tmemmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);\n> > > +\t/* vsnprintf() will append a NUL, overwriting one of our characters */\n> > > +\tsave = sb->buf[pos + len];\n> > > +\tlen2 = vsnprintf(sb->buf + pos, sb->alloc - sb->len, fmt, ap);\n> > \n> > ... and we use vsnprintf to write the formatted string to the\n> > beginning of the buffer.\n> \n> It is not actually the beginning of the buffer, but possibly the middle of\n> the buffer ;-)\n\nOops, you're right of course :) \n\n> > sb->alloc - sb->len can be larger than 'len', which is fine as vsnprintf\n> > doesn't write anything after the NUL character.  And as 'strbuf_grow'\n> > adds len + 1 bytes to the strbuf we'll always have enough space for\n> > adding the formatted string ...\n> > \n> > > +\tsb->buf[pos + len] = save;\n> > > +\tif (len2 != len)\n> > > +\t\tBUG(\"your vsnprintf is broken (returns inconsistent lengths)\");\n> > > +\tstrbuf_setlen(sb, sb->len + len);\n> > \n> > And finally we set the strbuf to the new length.  So all this is just\n> > a very roundabout way to say that this function does the right thing\n> > according to my reading (and tests).\n> \n> :-)\n> \n> It seems that Junio likes this way of reviewing, giving him confidence\n> that the review was thorough.\n>\n> Thanks!\n> Dscho\n> \n> > > +}\n> > > +\n> > > +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...)\n> > > +{\n> > > +\tva_list ap;\n> > > +\tva_start(ap, fmt);\n> > > +\tstrbuf_vinsertf(sb, pos, fmt, ap);\n> > > +\tva_end(ap);\n> > > +}\n> > > +\n> > >  void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)\n> > >  {\n> > >  \tstrbuf_splice(sb, pos, len, \"\", 0);\n> > > diff --git a/strbuf.h b/strbuf.h\n> > > index be02150df3..8f8fe01e68 100644\n> > > --- a/strbuf.h\n> > > +++ b/strbuf.h\n> > > @@ -244,6 +244,15 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);\n> > >   */\n> > >  void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);\n> > >  \n> > > +/**\n> > > + * Insert data to the given position of the buffer giving a printf format\n> > > + * string. The contents will be shifted, not overwritten.\n> > > + */\n> > > +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt,\n> > > +\t\t     va_list ap);\n> > > +\n> > > +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...);\n> > > +\n> > >  /**\n> > >   * Remove given amount of data from a given position of the buffer.\n> > >   */\n> > > -- \n> > > 2.19.1.878.g0482332a22\n> > > \n> > \n"},{"id":"364174","messageId":"87ftvmytqj.fsf@evledraar.gmail.com","threadId":"49883","inReplyTo":"nycvar.QRO.7.76.6.1811271438310.41@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v11 20/22] stash: convert `stash--helper.c` into `stash.c`","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2018-11-27T23:40:36Z","receivedAt":"2018-11-27T23:40:41Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Nov 27 2018, Johannes Schindelin wrote:\n\n> Hi Junio & Paul,\n>\n> On Mon, 26 Nov 2018, Junio C Hamano wrote:\n>\n>> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n>>\n>> > The old shell script `git-stash.sh`  was removed and replaced\n>> > entirely by `builtin/stash.c`. In order to do that, `create` and\n>> > `push` were adapted to work without `stash.sh`. For example, before\n>> > this commit, `git stash create` called `git stash--helper create\n>> > --message \"$*\"`. If it called `git stash--helper create \"$@\"`, then\n>> > some of these changes wouldn't have been necessary.\n>> >\n>> > This commit also removes the word `helper` since now stash is\n>> > called directly and not by a shell script.\n>> >\n>> > Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n>> > ---\n>> >  .gitignore                           |   1 -\n>> >  Makefile                             |   3 +-\n>> >  builtin.h                            |   2 +-\n>> >  builtin/{stash--helper.c => stash.c} | 157 +++++++++++++++------------\n>> >  git-stash.sh                         | 153 --------------------------\n>> >  git.c                                |   2 +-\n>> >  6 files changed, 92 insertions(+), 226 deletions(-)\n>> >  rename builtin/{stash--helper.c => stash.c} (91%)\n>> >  delete mode 100755 git-stash.sh\n>>\n>> Seeing the recent trouble in \"rebase in C\" and how keeping the\n>> scripted version as \"git legacy-rebase\" helped us postpone the\n>> rewritten version without ripping the whole thing out, I wonder if\n>> we can do the same here.\n>\n> Feel very free to cherry-pick\n> https://github.com/git-for-windows/git/commit/004da7e7faa36c872868ae938e06594ea1c2f01c\n> and\n> https://github.com/git-for-windows/git/commit/cedfcd39f5a4e4beb33e16fa67c4659fd4bdabf6\n> which is what we carry in Git for Windows.\n\n...and then something similar to 62c23938fa (\"tests: add a special setup\nwhere rebase.useBuiltin is off\", 2018-11-14) so those of us who're\nsmoking next for bugs can test both and report if some of the test\nsetups (odd OS's etc) show a difference in behavior.\n\nI did some of this the last time around, but then I had to e.g. smoke\nnext against pu, and look at the general fallout there and see what was\ndue to stash-in-C, it would be much better to have a\nGIT_TEST_STASH_USE_BUILTIN.\n\n\n>> Also, the remaining two patches should be done _before_ this step, I\n>> would think.  I can understand it if the reason you have those two\n>> after this step is because you found the opportunity for these\n>> improvements after you wrote this step, but in the larger picture\n>> seen by the end users of the \"stash in C\" and those developers who\n>> follow the evolution of the code, the logical place for this \"now we\n>> have everything in C, we retire the scripted version\" step to happen\n>> is at the very end.\n>>\n>> Thanks.\n>>\n"},{"id":"364304","messageId":"nycvar.QRO.7.76.6.1811291155570.41@tvgsbejvaqbjf.bet","threadId":"49883","inReplyTo":"87ftvmytqj.fsf@evledraar.gmail.com","subject":"Re: [PATCH v11 20/22] stash: convert `stash--helper.c` into `stash.c`","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-11-29T10:58:41Z","receivedAt":"2018-11-29T10:58:46Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Wed, 28 Nov 2018, Ævar Arnfjörð Bjarmason wrote:\n\n> On Tue, Nov 27 2018, Johannes Schindelin wrote:\n> \n> > On Mon, 26 Nov 2018, Junio C Hamano wrote:\n> >\n> >> Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> writes:\n> >>\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> >> Seeing the recent trouble in \"rebase in C\" and how keeping the\n> >> scripted version as \"git legacy-rebase\" helped us postpone the\n> >> rewritten version without ripping the whole thing out, I wonder if\n> >> we can do the same here.\n> >\n> > Feel very free to cherry-pick\n> > https://github.com/git-for-windows/git/commit/004da7e7faa36c872868ae938e06594ea1c2f01c\n> > and\n> > https://github.com/git-for-windows/git/commit/cedfcd39f5a4e4beb33e16fa67c4659fd4bdabf6\n> > which is what we carry in Git for Windows.\n> \n> ...and then something similar to 62c23938fa (\"tests: add a special setup\n> where rebase.useBuiltin is off\", 2018-11-14) so those of us who're\n> smoking next for bugs can test both and report if some of the test\n> setups (odd OS's etc) show a difference in behavior.\n\nI allowed myself to make those changes, and to reorder the last three\npatches as asked by Junio. Paul, please find the result at\nhttps://github.com/dscho/git as `git-stash`. If you agree with it, I would\nbe delighted if you resubmitted it directly after Git v2.20.0 is released\n(Junio, it seemed that -rc1 slipped a couple of days, and now -rc2, too,\nany word when you think the final v2.20.0 is due?).\n\nThanks,\nDscho"},{"id":"364309","messageId":"nycvar.QRO.7.76.6.1811291352480.41@tvgsbejvaqbjf.bet","threadId":"49883","inReplyTo":"xmqq8t1gwano.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v11 00/22] Convert \"git stash\" to C builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-11-29T12:54:18Z","receivedAt":"2018-11-29T12:54:23Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Junio,\n\nOn Mon, 26 Nov 2018, Junio C Hamano wrote:\n\n> Junio C Hamano <gitster@pobox.com> writes:\n> \n> > Thomas Gummerer <t.gummerer@gmail.com> writes:\n> >\n> >> Thanks for your work on this!  I have read through the range-diff and\n> >> the new patch of this last round, and this addresses all the comments\n> >> I had on v10 (and some more :)).  I consider it\n> >> Reviewed-by: Thomas Gummerer <t.gummerer@gmail.com>\n> >\n> > Thanks.\n> >\n> > One thing that bothers me is that this seems to have been rebased on\n> > 'master', but as long as we are rebasing, the updated series must\n> > also take into account of the sd/stash-wo-user-name topic, i.e. if\n> > we are rebasing it, it should be rebased on top of the result of\n> >\n> > \tgit checkout -B ps/rebase-in-c master\n> > \tgit merge --no-ff sd/stash-wo-user-name\n> >\n> > I think.\n> \n> https://travis-ci.org/git/git/builds/459619672 would show that this\n> C reimplementation now regresses from the scripted version due to\n> lack of such rebasing (i.e. porting a correction from scripted one).\n\nOh, you know, at first I *mis-read* your mail to mean \"don't you rebase\nall the time!\", but in this case (in contrast to earlier statements about\nrebasing between iterations of patch series), you *do* want Paul to\nrebase.\n\nLet me see what I can come up with in my `git-stash` branch on\nhttps://github.com/dscho/git\n\nCiao,\nDscho\n"},{"id":"364315","messageId":"nycvar.QRO.7.76.6.1811291501100.41@tvgsbejvaqbjf.bet","threadId":"49883","inReplyTo":"nycvar.QRO.7.76.6.1811291352480.41@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v11 00/22] Convert \"git stash\" to C builtin","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-11-29T14:06:13Z","receivedAt":"2018-11-29T14:06:20Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Junio,\n\nOn Thu, 29 Nov 2018, Johannes Schindelin wrote:\n\n> On Mon, 26 Nov 2018, Junio C Hamano wrote:\n> \n> > Junio C Hamano <gitster@pobox.com> writes:\n> > \n> > > Thomas Gummerer <t.gummerer@gmail.com> writes:\n> > >\n> > >> Thanks for your work on this!  I have read through the range-diff and\n> > >> the new patch of this last round, and this addresses all the comments\n> > >> I had on v10 (and some more :)).  I consider it\n> > >> Reviewed-by: Thomas Gummerer <t.gummerer@gmail.com>\n> > >\n> > > Thanks.\n> > >\n> > > One thing that bothers me is that this seems to have been rebased on\n> > > 'master', but as long as we are rebasing, the updated series must\n> > > also take into account of the sd/stash-wo-user-name topic, i.e. if\n> > > we are rebasing it, it should be rebased on top of the result of\n> > >\n> > > \tgit checkout -B ps/rebase-in-c master\n> > > \tgit merge --no-ff sd/stash-wo-user-name\n> > >\n> > > I think.\n> > \n> > https://travis-ci.org/git/git/builds/459619672 would show that this\n> > C reimplementation now regresses from the scripted version due to\n> > lack of such rebasing (i.e. porting a correction from scripted one).\n> \n> Oh, you know, at first I *mis-read* your mail to mean \"don't you rebase\n> all the time!\", but in this case (in contrast to earlier statements about\n> rebasing between iterations of patch series), you *do* want Paul to\n> rebase.\n> \n> Let me see what I can come up with in my `git-stash` branch on\n> https://github.com/dscho/git\n\nThere. I force-pushed an update that is based on sd/stash-wo-user-name and\nadds a `prepare_fallback_ident(name, email)` to `ident.c` for use in the\nbuilt-in stash:\n\nhttps://github.com/dscho/git/commit/d37ce623fbd32e4345c701dea822e56de1a5417f\n\nIt passes t3903 in a little over a minute with\nGIT_TEST_STASH_USE_BUILTIN=true and in a little less than seven minutes\nwith GIT_TEST_STASH_USE_BUILTIN=false.\n\nCiao,\nDscho\n\n"}]}