Re: Feature request: different exit codes for git stash depending on whether stash was created or not
- From
brian m. carlson <sandals@crustytoothpaste.net>
- Date
- Sep 27, 2019, 23:07 UTC
- Message-ID
- <20190927230747.yjdeaen34nnpcchy@camp.crustytoothpaste.net>
- In-Reply-To
- <CADZUK1cjvifbFYk9w6+vE95jgEBuwjoOi_1QfZHoPFBXuXHCCg@mail.gmail.com>
On 2019-09-27 at 12:55:27, Ian Kemp wrote:
Show 14 quoted lines
> Hi, > > Currently, git stash's exit code is 0 regardless of whether it > performed a stash operation or not. Third parties invoking git stash > are therefore unable to determine whether a stash was actually made or > not. > > It would be helpful if there were different exit codes for the case > when a stash was created (working directory dirty) vs when a stash was > not (working dir clean). git stash create suffers from the same issue. > > There are various workarounds for this e.g. > https://stackoverflow.com/a/34116244/70345 but they aren't > particularly pretty or reliable, hence this request.
Folks have already given you a link to previous discussion, but if you want to know if there's anything to stash couldn't you use something like this:
#!/bin/sh
if [ -z "$(git status --porcelain)" ]
then
CLEAN=1
fi[ -n "$CLEAN" ] || git stash
# your operation here
[ -n "$CLEAN" ] || git stash pop
An empty output from git status --porcelain is the definitive way to know if the working tree is clean, which is what you care about in this case. If you want to ignore untracked files, a suitable grep invocation can do that for you as well.
-- brian m. carlson: Houston, Texas, US OpenPGP: https://keybase.io/bk2204