{"thread":{"id":"37675","subject":"[bug] [UX] `stash save --untracked` produces a stash that *looks* empty","startedAt":"2014-10-05T17:28:45Z","lastAt":"2014-10-09T02:51:12Z","messageCount":3,"participants":["Alberto Scotto","Tanay Abhra","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"250258","messageId":"CACsY31pSeijqGC4Rw=q+qAGHJQ+bNibindVReYoZZACvYL7O+A@mail.gmail.com","threadId":"37675","inReplyTo":null,"subject":"[bug] [UX] `stash save --untracked` produces a stash that *looks* empty","fromName":"Alberto Scotto","fromEmail":"scotto.alberto.86@gmail.com","sentAt":"2014-10-05T17:28:45Z","receivedAt":"2014-10-05T17:28:45Z","isPatch":false,"sender":{"key":"scotto.alberto.86@gmail.com","avatar":null},"body":"Hi all,\n\nI've just found that:\n- given you have an empty staging area\n- and you have only untracked files in your working dir\n- when you do `git stash --untracked`\n- then `git stash show` gives you an empty output => stash looks empty\n\nMy first thought was \"oh god, my files are lost!\"\nSecond thought: \"Jeez I found a bug in git! cool!\"\nThen I found that actually `git stash apply` restores the apparently lost\nfiles\nSo I think it's a UX issue.\nIt cost me a few lost files already, as I thought \"an empty stash? uhm..\ncan't remember what/when I stashed.. whatever.. let's just delete it and\nclean up a little bit this mess of stashes\".\n\n\nHere are the reproducible steps:\n\n   1. create new fresh git repo in $REPO_DIR\n   2. create a couple of files/dirs and commit\n   3. edit src/MyClass.java and commit\n   4. create dir src/new-dir with one file inside\n   5. edit file.txt and stage it\n   6. stash => stashes staged changes; only untracked files are left\n   7. stash -u => stashes untracked changes => working dir is clean\n   8. stash list\n   9. git stash show -p => empty output\n   10. git stash apply (restore stashed untracked files)\n\n\nI made a bash script that runs through those steps.\nPlease check it out.\nhttps://gist.github.com/alb-i986/a4002f1ac50ce355278e\n\nEnvs:\n\n   - Mac OSX 10.9.5\n   - Darwin 13.4.0 Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT\n      2014; root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64\n      - git version 1.9.3 (Apple Git-50)\n      - GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)\n   - Ubuntu precise <-\n   https://github.com/alb-i986/vagrantfiles/tree/master/basic\n      - Linux precise64 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51\n      UTC 2012 x86_64 x86_64 x86_64 GNU/Linux\n      - git version 1.7.9.5\n      - GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu)\n\n\nThank you\n\n\nAlberto Scotto\nhttp://alb-i986.me\n"},{"id":"250345","messageId":"543501CE.5040705@gmail.com","threadId":"37675","inReplyTo":"CACsY31pSeijqGC4Rw=q+qAGHJQ+bNibindVReYoZZACvYL7O+A@mail.gmail.com","subject":"Re: [bug] [UX] `stash save --untracked` produces a stash that *looks* empty","fromName":"Tanay Abhra","fromEmail":"tanayabh@gmail.com","sentAt":"2014-10-08T09:20:14Z","receivedAt":"2014-10-08T09:20:14Z","isPatch":false,"sender":{"key":"tanayabh@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2097229?v=4"},"body":"On 10/5/2014 10:58 PM, Alberto Scotto wrote:\n> Hi all,\n> \n> I've just found that:\n> - given you have an empty staging area\n> - and you have only untracked files in your working dir\n> - when you do `git stash --untracked`\n> - then `git stash show` gives you an empty output => stash looks empty\n> \n> My first thought was \"oh god, my files are lost!\"\n> Second thought: \"Jeez I found a bug in git! cool!\"\n> Then I found that actually `git stash apply` restores the apparently lost\n> files\n> So I think it's a UX issue.\n> It cost me a few lost files already, as I thought \"an empty stash? uhm..\n> can't remember what/when I stashed.. whatever.. let's just delete it and\n> clean up a little bit this mess of stashes\".\n> \n> \n> Here are the reproducible steps:\n> \n>    1. create new fresh git repo in $REPO_DIR\n>    2. create a couple of files/dirs and commit\n>    3. edit src/MyClass.java and commit\n>    4. create dir src/new-dir with one file inside\n>    5. edit file.txt and stage it\n>    6. stash => stashes staged changes; only untracked files are left\n>    7. stash -u => stashes untracked changes => working dir is clean\n>    8. stash list\n>    9. git stash show -p => empty output\n>    10. git stash apply (restore stashed untracked files)\n\nHi,\n\nI think problem lies with  show_stash() which just shows the\ndiff between working tree and the base tree, it ignores the\nuntracked files. A quick and dirty fix can be to just show\nthe diff between the untracked files and a NULL commit.\nHere's the patch, it works all right but can be implemented\nmuch better. I will try to find a better approach tomorrow.\n\ndiff --git a/git-stash.sh b/git-stash.sh\nindex d4cf818..7088584 100755\n--- a/git-stash.sh\n+++ b/git-stash.sh\n@@ -304,6 +304,8 @@ show_stash () {\n        assert_stash_like \"$@\"\n\n        git diff ${FLAGS:---stat} $b_commit $w_commit\n+       empty_tree=$(git hash-object -t tree /dev/null)\n+       git diff ${FLAGS:---stat} ${empty_tree} $u_commit\n }\n\n #\n\nCheers,\nTanay\n"},{"id":"250406","messageId":"20141009025112.GA19897@peff.net","threadId":"37675","inReplyTo":"543501CE.5040705@gmail.com","subject":"Re: [bug] [UX] `stash save --untracked` produces a stash that *looks* empty","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-10-09T02:51:12Z","receivedAt":"2014-10-09T02:51:12Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Oct 08, 2014 at 02:50:14PM +0530, Tanay Abhra wrote:\n\n> I think problem lies with  show_stash() which just shows the\n> diff between working tree and the base tree, it ignores the\n> untracked files. A quick and dirty fix can be to just show\n> the diff between the untracked files and a NULL commit.\n> Here's the patch, it works all right but can be implemented\n> much better. I will try to find a better approach tomorrow.\n\nYou may want to read through another recent attempt at the same thing\nhere:\n\n  http://thread.gmane.org/gmane.comp.version-control.git/254420\n\nIt ended with us just making \"git stash list\" behave like \"git stash\nshow\" (and show only the working tree); that ended up as 288c67c (stash:\ndefault listing to working-tree diff, 2014-08-06).\n\nWhich isn't to say there isn't room for more improvement. But you may\nfind the backstory and pitfalls helpful (and I think Junio explicitly\nsaid he prefers the current behavior in that thread).\n\n-Peff\n"}]}