Skip to content

Commit

Permalink
Save and restore WIP state while executing accept-work
Browse files Browse the repository at this point in the history
As `accept-work` manipulates with branches, all uncommitted changes have
to be stashed prior to its execution. Also, the stash has to be applied
back to the correct branch. That's why the branch, which was a current one
before the command execution, has to be activated.
  • Loading branch information
extsoft committed Sep 29, 2019
1 parent 344fd7e commit 6dc3330
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ current changes. The final index merges using fast-forward strategy into the
default local branch and pushes into the default upstream branch. After a
successful push, the given and temporary branches are removed.

Prior to the execution, a current state is saved (a branch with modifications).
After the successful accepting a work, the state will be restored. In the case
of a failure, you need to go to the desired branch and apply a stash if needed.

Approximate commands flow is
```bash
==>> git elegant accept-work task-123
Expand Down
10 changes: 9 additions & 1 deletion libexec/git-elegant-accept-work
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ current changes. The final index merges using fast-forward strategy into the
default local branch and pushes into the default upstream branch. After a
successful push, the given and temporary branches are removed.
Prior to the execution, a current state is saved (a branch with modifications).
After the successful accepting a work, the state will be restored. In the case
of a failure, you need to go to the desired branch and apply a stash if needed.
Approximate commands flow is
\`\`\`bash
==>> git elegant accept-work task-123
Expand All @@ -37,7 +41,7 @@ git push origin --delete task-123
MESSAGE
}

default() {
--accept-work-logic() {
local WORK_BRANCH="__eg"
git elegant obtain-work "${1}" "${WORK_BRANCH}"
git-verbose status
Expand All @@ -55,3 +59,7 @@ default() {
git-verbose branch --delete --force ${WORK_BRANCH}
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${ACTUAL_REMOTE})
}

default() {
stash-pipe branch-pipe --accept-work-logic "${@}"
}
11 changes: 11 additions & 0 deletions libexec/plugins/pipe
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ stash-pipe() {
git-verbose stash pop $(git stash list --grep="${message}" --format="%gd")
fi
}

branch-pipe() {
# Moves to the current branch after a command execution
# usage: branch-pipe <command> [args]...
local to=$(git rev-parse --abbrev-ref HEAD)
"${@}"
local now=$(git rev-parse --abbrev-ref HEAD)
if ! ${to} == ${now}; then
git-verbose checkout ${to}
fi
}
40 changes: 27 additions & 13 deletions tests/git-elegant-accept-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@

load addons-common
load addons-fake
load addons-git

teardown() {
clean-fake
}

@test "'accept-work': a work is accepted successfully for given remote branch" {
fake-pass git "elegant obtain-work some __eg"
fake-pass git "status"
setup() {
init-repo
fake-pass git "elegant obtain-work test-feature __eg"
fake-pass git "rebase origin/master"
fake-pass git "checkout master"
fake-pass git "fetch --all"
fake-pass git "merge --ff-only __eg"
fake-pass git "push origin master:master"
fake-pass git "branch --delete --force __eg"
fake-pass git "for-each-ref --format='%(upstream:short)' refs/heads/_eg}" "origin/some-work"
fake-pass git "push origin --delete some-work"
fake-pass git "for-each-ref --format='%(upstream:short)' refs/heads/_eg}" "origin/test-feature"
fake-pass git "push origin --delete test-feature"
}

check git-elegant accept-work some
[[ "$status" -eq 0 ]]
teardown() {
clean-git
clean-fake
}

@test "'accept-work': a work is accepted successfully for given remote branch" {
check git-elegant accept-work test-feature
[[ "${status}" -eq 0 ]]
}

@test "'accept-work': exit code is 45 when remote branch name isn't set" {
check git-elegant accept-work
[[ "$status" -eq 45 ]]
[[ "${status}" -eq 45 ]]
}

@test "'accept-work': save WIP prior accepting and restore after it" {
gitrepo "git checkout -b other"
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
check git-elegant accept-work test-feature
[[ "${status}" -eq 0 ]]
[[ "${lines[@]}" =~ "git stash push" ]]
[[ "${lines[@]}" =~ "git stash pop" ]]
[[ "${lines[@]}" =~ "git checkout other" ]]
}

0 comments on commit 6dc3330

Please sign in to comment.