From: David Caldwell <hidden> Date: 2016-06-15 22:51:30
The --clean option acts like the normal "git stash save" but also adds all
untracked files in the working directory to the stash and then calls "git
clean --force --quiet" to restore the working directory to a pristine
state.
I find this useful for certain projects that need to run release
scripts. With this option I can run the release scripts from my main working
directory and not have to maintain a "clean" directory in parallel just for
releasing. Basically the work-flow becomes:
$ git tag release-1.0
$ git stash --clean
$ make release
$ git stash pop
"git stash" alone is not enough in this case--it leaves untracked files
lying around (configure and automake droppings, for instance) that might
mess up a release process that expects everything to be very clean.
Signed-off-by: David Caldwell <redacted>
Hi,
This is my first patch to git so I have a couple questions:
* I used 'find . -name ".git" -prune -o -print' to get a list of all the
files in the working directory. That assumes ".git" is the name
of the repo--is that assumption valid?
* Also, that find command does not respect .gitignore. Should it? If it
does then I think it would need another option to also clean up stuff
that is normally ignored (similar to the way "git clean" works with the
-x option). My thoughts were that if we were stashing everything, being
overzealous with the cleanup wouldn't hurt (and would generally be
exactly what you want).
Thanks,
David
---
Documentation/git-stash.txt | 8 ++-
git-stash.sh | 20 +++++++-
t/t3905-stash-clean.sh | 111 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 134 insertions(+), 5 deletions(-)
create mode 100755 t/t3905-stash-clean.sh
@@ -42,7 +42,7 @@ is also possible). OPTIONS --------save [-p|--patch] [--[no-]keep-index] [-q|--quiet] [<message>]::+save [-p|--patch] [--[no-]keep-index] [-c|--clean] [-q|--quiet] [<message>]:: Save your local modifications to a new 'stash', and run `git reset --hard` to revert them. The <message> part is optional and gives
@@ -54,6 +54,10 @@ save [-p|--patch] [--[no-]keep-index] [-q|--quiet] [<message>]:: If the `--keep-index` option is used, all changes already added to the index are left intact. ++If the `--clean` option is used, all non-tracked files are also stashed and+then cleaned up with `git clean`, leaving the working directory in a very+clean state.++ With `--patch`, you can interactively select hunks from the diff between HEAD and the working tree to be stashed. The stash entry is constructed such that its index state is the same as the index state
@@ -162,6 +167,11 @@ save_stash () {shiftdone+iftest-n"$patch_mode"&&test-n"$clean"+then+die"Can't stash --patch and --clean at the same time"+fi+stash_msg="$*"gitupdate-index-q--refresh
@@ -173,7 +183,7 @@ save_stash () {test-f"$GIT_DIR/logs/$ref_stash"||clear_stash||die"Cannot initialize stash"-create_stash"$stash_msg"+create_stash"$stash_msg"$clean# Make sure the reflog for stash is kept.:>>"$GIT_DIR/logs/$ref_stash"
@@ -0,0 +1,111 @@+#!/bin/sh+#+# Copyright (c) 2011 David Caldwell+#++test_description='Test git stash --clean'++../test-lib.sh++# Test code doesn't seem to clean the test dir out between runs.+#git clean --force --quiet++test_expect_success'stash save --clean some dirty working directory''+echo1>file&&+gitaddfile&&+test_tick&&+gitcommit-minitial&&+echo2>file&&+gitaddfile&&+echo3>file&&+test_tick&&+echo1>file2&&+gitstash--clean&&+gitdiff-files--quiet&&+gitdiff-index--cached--quietHEAD+'++cat>expect<<EOF+??expect+??output+EOF++test_expect_success'stash save --clean cleaned the untracked files''+gitstatus--porcelain>output+test_cmpoutputexpect+'++cat>expect<<EOF+diff--gita/fileb/file+index0cfbf08..00750ed100644+---a/file++++b/file+@@-1+1@@+-2++3+diff--gita/file2b/file2+newfilemode100644+index0000000..d00491f+---/dev/null++++b/file2+@@-0,0+1@@++1+EOF++test_expect_success'stash save --clean stashed the untracked files''+gitdiffstash^2..stash>output&&+test_cmpoutputexpect+'++test_expect_success'stash save --patch --clean fails''+test_must_failgitstash--patch--clean+'++gitclean--force--quiet++test_expect_success'stash save -c dirty index''+echo4>file3&&+gitaddfile3&&+test_tick&&+gitstash-c+'++cat>expect<<EOF+diff--gita/file3b/file3+newfilemode100644+index0000000..b8626c4+---/dev/null++++b/file3+@@-0,0+1@@++4+EOF++test_expect_success'stash save --clean dirty index got stashed''+gitstashpop--index&&+gitdiff--cached>output&&+test_cmpoutputexpect+'++gitreset++test_expect_success'stash save --clean -q is quiet''+echo1>file5+gitstashsave--clean--quiet>output.out2>&1&&+test!-soutput.out+'++test_expect_success'stash save --clean removed files''+rm-ffile&&+gitstashsave--clean&&+echo1>expect&&+test_cmpfileexpect+'++rm-fexpect++test_expect_success'stash save --clean removed files got stashed''+gitstashpop&&+test!-ffile+'++test_done
From: Jeff King <hidden> Date: 2016-06-15 22:51:30
On Mon, Jun 20, 2011 at 04:36:26PM -0700, David Caldwell wrote:
The --clean option acts like the normal "git stash save" but also adds all
untracked files in the working directory to the stash and then calls "git
clean --force --quiet" to restore the working directory to a pristine
state.
Hmm. I think I would call this something like "--untracked", as to me
the main function is saving those files, not cleaning them afterwards
(the fact that they are cleaned is really just making the untracked-file
handling in line with what we do for tracked files; we put the changes
in the stash and remove them from the working tree).
But that might just be me.
I find this useful for certain projects that need to run release
scripts. With this option I can run the release scripts from my main working
directory and not have to maintain a "clean" directory in parallel just for
releasing. Basically the work-flow becomes:
$ git tag release-1.0
$ git stash --clean
$ make release
$ git stash pop
"git stash" alone is not enough in this case--it leaves untracked files
lying around (configure and automake droppings, for instance) that might
mess up a release process that expects everything to be very clean.
For that workflow, do you actually want the files saved and restored
via "stash pop"? That is, aren't those untracked files just useless
cruft that could be regenerated, and you would be just as happy to do:
$ git tag release-1.0
$ git stash
$ git clean
$ make release
$ git stash pop
and have a pristine state after your pop? You end up regenerating them
during the next build, which could be costly. But you don't have to pay
the price of stashing potentially large files that are just going to end
up regenerated anyway. For that matter, what should this do with
gitignored files, like generated object files?
Also, wouldn't you want to "git clean" after your "make release" but
before your "git stash pop" in case the build creates cruft that is not
overwritten by your stash pop?
Signed-off-by: David Caldwell <redacted>
Hi,
This is my first patch to git so I have a couple questions:
Hi David. Welcome to the list. :)
Please put your comments on the patch (i.e., anything not destined to go
into the commit message) below the "---" marker; that helps "git am"
know which part is which.
* I used 'find . -name ".git" -prune -o -print' to get a list of all the
files in the working directory. That assumes ".git" is the name
of the repo--is that assumption valid?
Generally yes, but somebody could do something tricky with GIT_DIR. You
should be using "git ls-files -o" instead.
* Also, that find command does not respect .gitignore. Should it? If it
does then I think it would need another option to also clean up stuff
that is normally ignored (similar to the way "git clean" works with the
-x option). My thoughts were that if we were stashing everything, being
overzealous with the cleanup wouldn't hurt (and would generally be
exactly what you want).
I'm not sure of the answer to this. I think it would depend on your
workflow and your project (i.e., is your build system fragile enough
that you need to get rid of ignored build products between builds, or is
it OK to leave them, which is more efficient). I would think respecting
ignore would be a sane default, but I don't know if it should be
configurable, or have an extra command line option to stash everything.
If you do want to respect .gitignore, then you can add
"--exclude-standard" to the "ls-files" command I mentioned above.
When you apply this stash, what does the resulting index look like? Do
the untracked files remain properly untracked? That might be a good
thing to double check in the test script.
-Peff
From: David Caldwell <hidden> Date: 2016-06-15 22:51:30
On 6/20/11 5:38 PM, Jeff King wrote:
On Mon, Jun 20, 2011 at 04:36:26PM -0700, David Caldwell wrote:
quoted
The --clean option acts like the normal "git stash save" but also adds all
untracked files in the working directory to the stash and then calls "git
clean --force --quiet" to restore the working directory to a pristine
state.
Hmm. I think I would call this something like "--untracked", as to me
the main function is saving those files, not cleaning them afterwards
(the fact that they are cleaned is really just making the untracked-file
handling in line with what we do for tracked files; we put the changes
in the stash and remove them from the working tree).
I see your point but I thought "--clean" was pretty descriptive of how
the working dir ended up afterward. Maybe "git stash --everything" (or
"--all")?
quoted
"git stash" alone is not enough in this case--it leaves untracked files
lying around (configure and automake droppings, for instance) that might
mess up a release process that expects everything to be very clean.
For that workflow, do you actually want the files saved and restored
via "stash pop"? That is, aren't those untracked files just useless
cruft that could be regenerated, and you would be just as happy to do:
$ git tag release-1.0
$ git stash
$ git clean
$ make release
$ git stash pop
and have a pristine state after your pop?
Yes, in that case you are right. My example was poor. I'm more worried
about junk that might actually affect the release but isn't
auto-generated. I'm usually too wary to run "git clean" because of the
random files I have sitting around--todo lists, random patches, new
files I haven't added but are sitting around possibly affecting tests,
things like that. I want them back at some point, but I want the
directory very clean when doing the release just to make sure I have
everything properly committed (so I could, for example, detect the new
source file that I forgot to add and commit).
Also, wouldn't you want to "git clean" after your "make release" but
before your "git stash pop" in case the build creates cruft that is not
overwritten by your stash pop?
Yes, you'd definitely want the git clean before the git stash pop. I
forgot that step.
Please put your comments on the patch (i.e., anything not destined to go
into the commit message) below the "---" marker; that helps "git am"
know which part is which.
Whoops. That was a copy and paste error. Took multiple tries to get git
send-email to work ;-).
quoted
* I used 'find . -name ".git" -prune -o -print' to get a list of all the
files in the working directory. That assumes ".git" is the name
of the repo--is that assumption valid?
Generally yes, but somebody could do something tricky with GIT_DIR. You
should be using "git ls-files -o" instead.
Ah, thanks--I didn't know about that command! I was considering using
"git status --porcelain", but ls-files looks exactly like what I want.
For that matter, what should this do with gitignored files, like
generated object files?
quoted
* Also, that find command does not respect .gitignore. Should it?
I'm not sure of the answer to this. I think it would depend on your
workflow and your project (i.e., is your build system fragile enough
that you need to get rid of ignored build products between builds, or is
it OK to leave them, which is more efficient). I would think respecting
ignore would be a sane default, but I don't know if it should be
configurable, or have an extra command line option to stash everything.
If you do want to respect .gitignore, then you can add
"--exclude-standard" to the "ls-files" command I mentioned above.
The more I think about it the more I think you're right that it should
respect .gitignore on the default case and have another option to be
really thorough (even if my build system isn't always fragile, it's
sometimes nice to be overly cautious just for my own paranoid peace of
mind).
Perhaps --clean (or --untracked) respects .gitignore and --all just flat
out does everything?
When you apply this stash, what does the resulting index look like? Do
the untracked files remain properly untracked? That might be a good
thing to double check in the test script.
They do in indeed, but you're right, that would be a good automated test
to add.
Thanks, I greatly appreciate the help.
-David
From: Andrew Wong <hidden> Date: 2016-06-15 22:51:30
Ah, this patch will be very useful. I actually ran into several
scenarios before where I wished stash could do exactly this.
On 11-06-20 9:36 PM, David Caldwell wrote:
On 6/20/11 5:38 PM, Jeff King wrote:
quoted
Hmm. I think I would call this something like "--untracked", as to me
the main function is saving those files, not cleaning them afterwards
(the fact that they are cleaned is really just making the untracked-file
handling in line with what we do for tracked files; we put the changes
in the stash and remove them from the working tree).
I see your point but I thought "--clean" was pretty descriptive of how
the working dir ended up afterward. Maybe "git stash --everything" (or
"--all")?
I personally think "--untracked" (and -u) is more intuitive too, since
it tells you what "git stash" is about to do. i.e. "git stash" is about
to do the usual stash operation *and* also stash the "untracked" files.
It seems more clear and precise than saying "git will stash my tree to a
state that is clean", which doesn't exactly tell me how is that
different from the usual stash.To me, "--clean" might even sounds more
like "do a stash, and then do a clean", which doesn't make sense.
I would've liked "--all" too, except it reminds me too much of "git
commit --all", which is just committing all tracked files.
Again, that's just my personal preference.
From: Johannes Sixt <hidden> Date: 2016-06-15 22:51:30
Am 6/21/2011 7:08, schrieb Andrew Wong:
Ah, this patch will be very useful. I actually ran into several scenarios
before where I wished stash could do exactly this.
On 11-06-20 9:36 PM, David Caldwell wrote:
quoted
On 6/20/11 5:38 PM, Jeff King wrote:
quoted
Hmm. I think I would call this something like "--untracked", as to me
the main function is saving those files, not cleaning them afterwards
(the fact that they are cleaned is really just making the untracked-file
handling in line with what we do for tracked files; we put the changes
in the stash and remove them from the working tree).
I see your point but I thought "--clean" was pretty descriptive of how
the working dir ended up afterward. Maybe "git stash --everything" (or
"--all")?
I personally think "--untracked" (and -u) is more intuitive too, since it
tells you what "git stash" is about to do. i.e. "git stash" is about to do
the usual stash operation *and* also stash the "untracked" files.
Really?
$ git stash --untracked
sound like it stashes *only* untracked files. (That by itself may be a
feature that some people want; so far, I'm not among them.)
-- Hannes
From: Jeff King <hidden> Date: 2016-06-15 22:51:30
On Tue, Jun 21, 2011 at 08:28:31AM +0200, Johannes Sixt wrote:
quoted
I personally think "--untracked" (and -u) is more intuitive too, since it
tells you what "git stash" is about to do. i.e. "git stash" is about to do
the usual stash operation *and* also stash the "untracked" files.
Really?
$ git stash --untracked
sound like it stashes *only* untracked files. (That by itself may be a
feature that some people want; so far, I'm not among them.)
I would be happy with something that indicated "untracked files in
addition to the regular stash". I just think it should be about "add
these other files into the stash", not "end up in this directory state".
Something like "--untracked-too" fits that, but is horribly ugly. I also
think it makes sense to have some way of stashing everything, including
excluded files. That could just be "-x" in conjunction with whatever
this option is (which matches "git clean"), or it could be a separate
option name (like "--all" or "--ignored").
Things like "git stash --all" or "git stash --thorough" indicate that
you are stashing more, but it's hard to remember what the "more" is.
So I don't have any brilliant suggestions. Doing:
$ git stash --untracked-too --ignored-too
is fairly clear, but somehow strikes me as unnecessarily ugly and
verbose.
-Peff
From: Paul Ebermann <hidden> Date: 2016-06-15 22:51:30
Jeff King schrieb:
I would be happy with something that indicated "untracked files in
addition to the regular stash". I just think it should be about "add
these other files into the stash", not "end up in this directory state".
Something like "--untracked-too" fits that, but is horribly ugly. I also
think it makes sense to have some way of stashing everything, including
excluded files. That could just be "-x" in conjunction with whatever
this option is (which matches "git clean"), or it could be a separate
option name (like "--all" or "--ignored").
Things like "git stash --all" or "git stash --thorough" indicate that
you are stashing more, but it's hard to remember what the "more" is.
So I don't have any brilliant suggestions. Doing:
$ git stash --untracked-too --ignored-too
is fairly clear, but somehow strikes me as unnecessarily ugly and
verbose.
I think `--also-untracked` sounds better. It is even longer, though.
There could also be `--only-untracked`, which would stack only the
untracked files (and let changes of tracked files there).
Paŭlo
From: Marc Branchaud <hidden> Date: 2016-06-15 22:51:30
On 11-06-21 12:11 PM, Paul Ebermann wrote:
I think `--also-untracked` sounds better. It is even longer, though.
There could also be `--only-untracked`, which would stack only the
untracked files (and let changes of tracked files there).
Perhaps -uall to match git-status's -u[<mode>] option (and also the long
version thereof)?
M.
From: David Caldwell <hidden> Date: 2016-06-15 22:51:31
The --include-untracked option acts like the normal "git stash save" but
also adds all untracked files in the working directory to the stash and then
calls "git clean --force --quiet" to restore the working directory to a
pristine state.
This is useful for projects that need to run release scripts. With this
option, the release scripts can be from the main working directory so one
does not have to maintain a "clean" directory in parallel just for
releasing. Basically the work-flow becomes:
$ git tag release-1.0
$ git stash --include-untracked
$ make release
$ git clean -f
$ git stash pop
"git stash" alone is not enough in this case--it leaves untracked files
lying around that might mess up a release process that expects everything to
be very clean or might let a release succeed that should actually fail (due
to a new source file being created that hasn't been committed yet).
Signed-off-by: David Caldwell <redacted>
---
Hi,
This is a new version of the "git-stash --clean patch" I submitted earlier
this week. The changes in this patch with respect to the old patch are:
* changed --clean to --include-untracked
* --include-untracked now respects .gitignore
* added --all that does not respect .gitignore
* changed the way untracked files get stashed so that when you "git
stash apply" they remain untracked. I did this by creating a 3rd
parent to the stash commit that contains only the unstashed files.
* Added more tests to check --all and also whether untracked files
remain untracked after apply.
-David
Documentation/git-stash.txt | 10 ++-
git-stash.sh | 68 +++++++++++++++-
t/t3905-stash-include-untracked.sh | 155 ++++++++++++++++++++++++++++++++++++
3 files changed, 227 insertions(+), 6 deletions(-)
create mode 100755 t/t3905-stash-include-untracked.sh
@@ -42,7 +43,7 @@ is also possible). OPTIONS --------save [-p|--patch] [--[no-]keep-index] [-q|--quiet] [<message>]::+save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [<message>]:: Save your local modifications to a new 'stash', and run `git reset --hard` to revert them. The <message> part is optional and gives
@@ -54,6 +55,11 @@ save [-p|--patch] [--[no-]keep-index] [-q|--quiet] [<message>]:: If the `--keep-index` option is used, all changes already added to the index are left intact. ++If the `--include-untracked` option is used, all untracked files are also+stashed and then cleaned up with `git clean`, leaving the working directory+in a very clean state. If the `--all` option is used instead then the+ignored files are stashed and cleaned in addition to the untracked files.++ With `--patch`, you can interactively select hunks from the diff between HEAD and the working tree to be stashed. The stash entry is constructed such that its index state is the same as the index state
@@ -78,6 +87,25 @@ create_stash () {gitcommit-tree$i_tree-p$b_commit)||die"Cannot save the current index state"+iftest-n"$untracked"+then+# Untracked files are stored by themselves in a parentless commit, for+# ease of unpacking later.+u_commit=$(+untracked_files|(+exportGIT_INDEX_FILE="$TMPindex"+rm-f"$TMPindex"&&+gitupdate-index-z--add--remove--stdin&&+u_tree=$(gitwrite-tree)&&+printf'untracked files on %s\n'"$msg"|gitcommit-tree$u_tree&&+rm-f"$TMPindex"+))||die"Cannot save the untracked files"++untracked_commit_option="-p $u_commit";+else+untracked_commit_option=+fi+iftest-z"$patch_mode"then
@@ -122,13 +150,14 @@ create_stash () {stash_msg=$(printf'On %s: %s'"$branch""$stash_msg")fiw_commit=$(printf'%s\n'"$stash_msg"|-gitcommit-tree$w_tree-p$b_commit-p$i_commit)||+gitcommit-tree$w_tree-p$b_commit-p$i_commit$untracked_commit_option)||die"Cannot record working tree state"} save_stash(){keep_index=patch_mode=+untracked=whiletest$#!=0docase"$1"in
@@ -162,6 +197,11 @@ save_stash () {shiftdone+iftest-n"$patch_mode"&&test-n"$untracked"+then+die"Can't use --patch and ---include-untracked or --all at the same time"+fi+stash_msg="$*"gitupdate-index-q--refresh
@@ -173,7 +213,7 @@ save_stash () {test-f"$GIT_DIR/logs/$ref_stash"||clear_stash||die"Cannot initialize stash"-create_stash"$stash_msg"+create_stash"$stash_msg"$untracked# Make sure the reflog for stash is kept.:>>"$GIT_DIR/logs/$ref_stash"
@@ -234,9 +279,11 @@ show_stash () {# w_commit is set to the commit containing the working tree# b_commit is set to the base commit# i_commit is set to the commit containing the index tree+# u_commit is set to the commit containing the untracked files tree# w_tree is set to the working tree# b_tree is set to the base tree# i_tree is set to the index tree+# u_tree is set to the untracked files tree## GIT_QUIET is set to t if -q is specified# INDEX_OPTION is set to --index if --index is specified.
@@ -0,0 +1,155 @@+#!/bin/sh+#+# Copyright (c) 2011 David Caldwell+#++test_description='Test git stash --include-untracked'++../test-lib.sh++test_expect_success'stash save --include-untracked some dirty working directory''+echo1>file&&+gitaddfile&&+test_tick&&+gitcommit-minitial&&+echo2>file&&+gitaddfile&&+echo3>file&&+test_tick&&+echo1>file2&&+gitstash--include-untracked&&+gitdiff-files--quiet&&+gitdiff-index--cached--quietHEAD+'++cat>expect<<EOF+??expect+??output+EOF++test_expect_success'stash save --include-untracked cleaned the untracked files''+gitstatus--porcelain>output+test_cmpoutputexpect+'++cat>expect.diff<<EOF+diff--gita/file2b/file2+newfilemode100644+index0000000..d00491f+---/dev/null++++b/file2+@@-0,0+1@@++1+EOF+cat>expect.lstree<<EOF+file2+EOF++test_expect_success'stash save --include-untracked stashed the untracked files''+test"!"-ffile2&&+gitdiffHEAD..stash^3--file2>output&&+test_cmpoutputexpect.diff&&+gitls-tree--name-onlystash^3:>output&&+test_cmpoutputexpect.lstree+'+test_expect_success'stash save --patch --include-untracked fails''+test_must_failgitstash--patch--include-untracked+'++test_expect_success'stash save --patch --all fails''+test_must_failgitstash--patch--all+'++gitclean--force--quiet++cat>expect<<EOF+Mfile+??expect+??file2+??output+EOF++test_expect_success'stash pop after save --include-untracked leaves files untracked again''+gitstashpop&&+gitstatus--porcelain>output+test_cmpoutputexpect+'++gitclean--force--quiet++test_expect_success'stash save -u dirty index''+echo4>file3&&+gitaddfile3&&+test_tick&&+gitstash-u+'++cat>expect<<EOF+diff--gita/file3b/file3+newfilemode100644+index0000000..b8626c4+---/dev/null++++b/file3+@@-0,0+1@@++4+EOF++test_expect_success'stash save --include-untracked dirty index got stashed''+gitstashpop--index&&+gitdiff--cached>output&&+test_cmpoutputexpect+'++gitreset>/dev/null++test_expect_success'stash save --include-untracked -q is quiet''+echo1>file5&&+gitstashsave--include-untracked--quiet>output.out2>&1&&+test!-soutput.out+'++test_expect_success'stash save --include-untracked removed files''+rm-ffile&&+gitstashsave--include-untracked&&+echo1>expect&&+test_cmpfileexpect+'++rm-fexpect++test_expect_success'stash save --include-untracked removed files got stashed''+gitstashpop&&+test!-ffile+'++cat>.gitignore<<EOF+.gitignore+ignored+EOF++test_expect_success'stash save --include-untracked respects .gitignore''+echoignored>ignored&&+gitstash-u&&+test-signored&&+test-s.gitignore+'++test_expect_success'stash save -u can stash with only untracked files different''+echo4>file4&&+gitstash-u+test"!"-ffile4+'++test_expect_success'stash save --all does not respect .gitignore''+gitstash-a&&+test"!"-fignored&&+test"!"-f.gitignore+'++test_expect_success'stash save --all is stash poppable''+gitstashpop&&+test-signored&&+test-s.gitignore+'++test_done