Skip to content

Commit

Permalink
Make stashing verbose for start-work command
Browse files Browse the repository at this point in the history
The introduced `pipe` plugin gives ability to execute a command through
a wrapper function which runs pre- and/or post-conditions. For now, there
is an ability to make automatic stash if there are some uncommitted
changes, and apply them back after the wrapped command is executed.

Use `git rev-parse --abbrev-ref HEAD` to display a name of the current
branch. As the result:
1. it will be executed faster
2. removes `*` prefix from the branch name

Use `$@` always in double-quotes. This makes correct parameters
propagation for all arguments.
  • Loading branch information
extsoft committed Sep 29, 2019
1 parent 0cd61e0 commit eb004cc
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 34 deletions.
10 changes: 10 additions & 0 deletions .wf/tests-execution.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
# Runs bats tests
# usage: ./script [command name]

if [[ -n "${1}" ]]; then
bats --tap $(find tests -type f -name "*${1}*")
else
bats --tap tests
fi
7 changes: 1 addition & 6 deletions libexec/git-elegant
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -e
# It registers all "libexec" scripts.
BINS=$(dirname ${0})
export PATH=${BINS}:${PATH}
source ${BINS}/plugins/pipe
source ${BINS}/plugins/text

__site="https://elegant-git.bees-hive.org"
Expand All @@ -28,12 +29,6 @@ _error-if-empty() {
fi
}

__branches() {
local branch_command="$1"; shift
local b=$(eval "$branch_command" | sed -e 's|[* ]||g')
echo ${b[@]}
}

__loop_ask() {
local c="$1"; shift
local m="$1"; shift
Expand Down
2 changes: 1 addition & 1 deletion libexec/git-elegant-amend-work
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MESSAGE
}

default(){
local BRANCH=$(__branches 'git branch | grep \*')
local BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH" == "master" ]]; then
error-box "No commits to 'master' branch. Please read more on ${__site}"
error-box "Try 'git elegant start-work' prior to retrying this command."
Expand Down
2 changes: 1 addition & 1 deletion libexec/git-elegant-deliver-work
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ MESSAGE
}

default() {
local BRANCH=$(__branches 'git branch | grep \*')
local BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH" == "master" ]]; then
error-box "No pushes to 'master' branch. Please read more on ${__site}"
exit 42
Expand Down
2 changes: 1 addition & 1 deletion libexec/git-elegant-save-work
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ MESSAGE
}

default(){
local BRANCH=$(__branches 'git branch | grep \*')
local BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH" == "master" ]]; then
error-box "No commits to 'master' branch. Please read more on ${__site}"
error-box "Try 'git elegant start-work' prior to retrying this command."
Expand Down
12 changes: 5 additions & 7 deletions libexec/git-elegant-start-work
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ git stash drop stash@{0}
MESSAGE
}

default() {
_error-if-empty "$1" "Please give a name for the new branch."
status=$(git-verbose stash save elegant-git)
--start-work-logic(){
git-verbose checkout ${MASTER}
git-verbose pull
git-verbose checkout -b "$1"
}

if [[ "$status" =~ "Saved working directory" ]]; then
git-verbose stash apply stash^{/elegant-git}
git-verbose stash drop stash@{0}
fi
default() {
_error-if-empty "$1" "Please give a name for the new branch."
stash-pipe --start-work-logic "${@}"
}
23 changes: 23 additions & 0 deletions libexec/plugins/pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# The plugin which allows running a given functions through different types
# (aka pre- and post-conditions).

stash-pipe() {
# Makes automatic stash and unstash if possible
# usage: stash-pipe <command> [args]...

git update-index -q --refresh
if ! git diff-index --quiet HEAD --; then
local message="Elegant Git auto-stash: "
message+="WIP in '$(git rev-parse --abbrev-ref HEAD)' branch "
message+="on $(date "+%Y-%m-%dT%H:%M:%S")"
git-verbose stash push --message "${message}"
fi

"${@}"

if [[ -n "${message}" ]]; then
git update-index -q --refresh
git-verbose stash pop $(git stash list --grep="${message}" --format="%gd")
fi
}
2 changes: 1 addition & 1 deletion tests/addons-fake.bash
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if [[ -e "\${FIXTURE_HOME}" ]]; then
cat "\${FIXTURE_HOME}/stderr" >&2
exit \$(cat "\${FIXTURE_HOME}/exit_code")
else
${ORIGIN_BINARY} \$@
${ORIGIN_BINARY} "\$@"
fi
MOCK
fi
Expand Down
2 changes: 1 addition & 1 deletion tests/addons-git.bash
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ gitrepo() {
# execute given arguments on real git repo
# usage: gitrepo <command and arguments>
testtee cd ${GIT_REPO_DIR}
testtee $@
testtee "${@}"
testtee cd -
}

Expand Down
6 changes: 3 additions & 3 deletions tests/git-elegant-deliver-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ teardown() {
}

@test "'deliver-work': by default, a name of remote branch is equal to local branch" {
fake-pass git branch *feature1
fake-pass git "rev-parse --abbrev-ref HEAD" feature1
fake-pass git "fetch"
fake-pass git "rebase origin/master"
fake-pass git "push --set-upstream --force origin feature1:feature1"
Expand All @@ -19,7 +19,7 @@ teardown() {
}

@test "'deliver-work': if branch name passed, a name of remote branch is different to local branch" {
fake-pass git branch *feature1
fake-pass git "rev-parse --abbrev-ref HEAD" feature1
fake-pass git "fetch"
fake-pass git "rebase origin/master"
fake-pass git "push --set-upstream --force origin feature1:feature2"
Expand All @@ -29,7 +29,7 @@ teardown() {
}

@test "'deliver-work': exit code is 42 when current local branch is master" {
fake-pass git branch *master
fake-pass git "rev-parse --abbrev-ref HEAD" master
fake-pass git "fetch"
fake-pass git "rebase origin/master"
fake-pass git "push --set-upstream --force origin master:master"
Expand Down
25 changes: 12 additions & 13 deletions tests/git-elegant-start-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,36 @@ teardown() {

@test "'start-work': branch with given name is created successfully" {
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
[[ "$status" -eq 0 ]]
}

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

@test "'start-work': print error message when branch name isn't set" {
check git-elegant start-work
[[ "${lines[0]}" =~ "Please give a name for the new branch." ]]
}

@test "'start-work': use stash for available changes" {
fake-pass git "stash save elegant-git" "Saved working directory"
fake-pass git "stash apply stash^{/elegant-git}"
fake-pass git "stash drop stash@{0}"
@test "'start-work': use stash pipe if there are uncommitted changes" {
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "stash push" ]]
[[ "${lines[@]}" =~ "stash pop" ]]
}

@test "'start-work': ignore stash if there are no changes" {
@test "'start-work': ignore stash pipe if there are uncommitted changes" {
fake-pass git "stash save elegant-git" "No local changes to save"
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
}

@test "'start-work': exit code is 100 when stash wasn't applied" {
fake-pass git "stash save elegant-git" "Saved working directory"
fake-pass git "stash apply stash^{/elegant-git}"
fake-fail git "stash drop stash@{0}"
@test "'start-work': exit code is 100 when stash pipe wasn't applied" {
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
fake-fail git "stash pop stash@{0}"
check git-elegant start-work test-feature
[ "$status" -eq 100 ]
[[ "$status" -eq 100 ]]
}
32 changes: 32 additions & 0 deletions workflows
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -ex

testing() {
docker run -it --rm -v $PWD:/eg beeshive/elegant-git-ci:3 ./.wf/tests-execution.bash $@
}

repository() {
docker run -idt --rm --name repository --workdir /tmp/elegant-git-repo -v $PWD:/eg beeshive/elegant-git-ci:3 bash
docker exec -it repository bash -c "source /eg/tests/addons-git.bash;source /eg/tests/addons-common.bash; init-repo"
docker attach repository
}

commands=(
testing
repository
)

main() {
local command=${1}
if [[ -z "${command}" ]]; then
select any in ${commands[@]}; do
command=${any}
break
done
else
shift
fi
${command} ${@}
}

main ${@}

0 comments on commit eb004cc

Please sign in to comment.