Skip to content

Commit

Permalink
Apply only created stashes automatically
Browse files Browse the repository at this point in the history
There are cases when Git says about modification, although they are
absent in a working tree. And Elegant Git, as it sees the changes, will
try to stash them. But the stash won't be created (Git just says: "No
local changes to save"), and, as the exit code is 0, the next command
will be executed. At this point, Elegant Git will assume, that created
stash (which is false) must be uncovered after commands execution. And
it will apply the last available stash instead of doing nothing. That's
why a check is added that sees if there is a real created stash
that must be uncovered.
  • Loading branch information
extsoft committed Nov 21, 2019
1 parent 5d9fb81 commit f8b9b97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 8 additions & 2 deletions libexec/plugins/pipe
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ has-changes() {
stash-pipe() {
# Makes automatic stash and unstash if possible
# usage: stash-pipe <command> [args]...
# The pipe behavior is tested based on `start-work` command. That's why you
# have to add new tests in `git-elegant-start-work.bats` if you want to
# check some specific cases.

git update-index -q --really-refresh
if has-changes; then
Expand All @@ -24,9 +27,12 @@ stash-pipe() {

"${@}"

if [[ -n "${message}" ]]; then
if [[ -n ${message} ]]; then
git update-index -q --really-refresh
git-verbose stash pop $(git stash list --grep="${message}" --format="%gd")
local stash=$(git stash list --grep="${message}" --format="%gd")
if [[ -n ${stash} ]]; then
git-verbose stash pop ${stash}
fi
fi
}

Expand Down
18 changes: 10 additions & 8 deletions tests/git-elegant-start-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@ teardown() {
[[ "${lines[0]}" =~ "Please give a name for the new branch." ]]
}

@test "'start-work': use stash pipe if there are uncommitted changes" {
@test "'start-work': tracked changes move to a new branch when they are available" {
repo-non-staged-change "A new line..."
check git-elegant start-work test-feature
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "stash push" ]]
[[ "${lines[@]}" =~ "stash pop" ]]
}

@test "'start-work': ignore stash pipe if there are uncommitted changes" {
fake-pass "git stash save elegant-git" "No local changes to save"
@test "'start-work': stash commands don't run when there are no tracked changes" {
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
[[ "$status" -eq 0 ]]
[[ ! "${lines[@]}" =~ "stash push" ]]
[[ ! "${lines[@]}" =~ "stash pop" ]]
}

@test "'start-work': exit code is 100 when stash pipe wasn't applied" {
repo-non-staged-change "A new line..."
fake-fail "git stash pop stash@{0}"
@test "'start-work': stash is not applied when it is not found for a given message" {
fake-fail "git diff-index --quiet HEAD"
check git-elegant start-work test-feature
[[ "$status" -eq 100 ]]
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "stash push" ]]
[[ ! "${lines[@]}" =~ "stash pop" ]]
}

0 comments on commit f8b9b97

Please sign in to comment.