Skip to content

Commit c61ce10

Browse files
committed
Skip workflows if needed
Sometimes, a user may want to ignore defined workflows and just execute a pure command. And `--no-workflows` option is designed to implement this behavior. This option can be put prior or after the command. This gives more flexibility in usage. However, `git-elegan --no-workflows <command>` is preffered as it encourages to split Elegant Git options and command arguments. Bash completion is updated appropriately. Also, two improvements: 1. The `version` file will be removed prior to actual test execution. This exposes the mentioned behavior for any place where `.workflows/bats-pipeline.bash` is used. 2. Remove `-v` option (was equal to `--version`) as, in Git, `-v` stands for `--verbose`. #192
1 parent e2be239 commit c61ce10

7 files changed

Lines changed: 127 additions & 47 deletions

File tree

.workflows/bats-pipeline.bash

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
set -e
33
# Runs bats tests
44
# usage: ./script [command name]
5-
5+
if [[ -f version ]]; then
6+
rm -v version
7+
fi
68
if [[ -n "${1}" ]]; then
79
bats --tap $(find tests -type f -name "*${1}*")
810
else

completions/git-elegant.bash

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,63 @@
33
_git_elegant() {
44
COMPREPLY=()
55
local cursor="${COMP_WORDS[COMP_CWORD]}"
6-
6+
local geops="--help --version --no-workflows"
7+
local gecops="--help --no-workflows"
8+
local offset=0
9+
if [[ ${COMP_WORDS[*]} =~ (--no-workflows)|(-nw) ]]; then
10+
geops=""
11+
gecops=""
12+
if [[ ${COMP_WORDS[COMP_CWORD-1]} =~ (--no-workflows)|(-nw) ]]; then
13+
offset=1
14+
fi
15+
fi
716
# the first word prior to the ${cursor}
8-
case ""${COMP_WORDS[COMP_CWORD-1]}"" in
9-
elegant|git-elegant)
10-
local opts=($(git elegant commands))
11-
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
12-
return 0 ;;
13-
accept-work|obtain-work)
14-
COMPREPLY=(
15-
$(compgen -W "$(git branch --remotes --list)" -- ${cursor})
16-
)
17-
return 0 ;;
18-
show-release-notes)
19-
COMPREPLY=( $(compgen -W "simple smart" ${cursor}) )
20-
return 0 ;;
21-
*) ;;
22-
esac
17+
if [[ ${#COMP_WORDS[*]} > $(( 1 + ${offset} )) ]]; then
18+
case "${COMP_WORDS[COMP_CWORD-$(( 1 + ${offset} ))]}" in
19+
elegant|git-elegant)
20+
local opts=(
21+
${geops}
22+
$(git elegant commands)
23+
)
24+
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
25+
return 0 ;;
26+
accept-work|obtain-work)
27+
local opts=(
28+
${gecops}
29+
$(git branch --remotes --list)
30+
)
31+
COMPREPLY=(
32+
$(compgen -W "${opts[*]}" -- ${cursor})
33+
)
34+
return 0 ;;
35+
show-release-notes)
36+
COMPREPLY=( $(compgen -W "simple smart ${gecops[*]}" -- ${cursor}) )
37+
return 0 ;;
38+
*) ;;
39+
esac
40+
fi
2341

2442
# the second word prior to the ${cursor}
25-
case "${COMP_WORDS[COMP_CWORD-2]}" in
26-
show-release-notes)
27-
local opts=($(git for-each-ref --sort "-version:refname" --format "%(refname:short)" refs/**))
28-
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
29-
return 0 ;;
30-
*) ;;
31-
esac
43+
if [[ ${#COMP_WORDS[*]} > $(( 2 + ${offset} )) ]]; then
44+
case "${COMP_WORDS[COMP_CWORD-$(( 2 + ${offset} ))]}" in
45+
show-release-notes)
46+
local opts=($(git for-each-ref --sort "-version:refname" --format "%(refname:short)" refs/**))
47+
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
48+
return 0 ;;
49+
*) ;;
50+
esac
51+
fi
3252

3353
# the third word prior to the ${cursor}
34-
case "${COMP_WORDS[COMP_CWORD-3]}" in
35-
show-release-notes)
36-
local opts=($(git for-each-ref --sort "-version:refname" --format "%(refname:short)" refs/**))
37-
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
38-
return 0 ;;
39-
*) ;;
40-
esac
54+
if [[ ${#COMP_WORDS[*]} > $(( 3 + ${offset} )) ]]; then
55+
case "${COMP_WORDS[COMP_CWORD-$(( 3 + ${offset} ))]}" in
56+
show-release-notes)
57+
local opts=($(git for-each-ref --sort "-version:refname" --format "%(refname:short)" refs/**))
58+
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
59+
return 0 ;;
60+
*) ;;
61+
esac
62+
fi
4163
}
4264

4365
complete -F _git_elegant git-elegant

docs/commands.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
An assistant who carefully automates routine work with Git.
55

6-
usage: git elegant [-h | --help | help]
7-
or: git elegant [-v | --version | version]
8-
or: git elegant <command> [args]
6+
usage: git elegant [-h | --help | help | --version | version]
97
or: git elegant <command> [-h | --help | help]
8+
or: git elegant <command> [--no-workflows] [args]
9+
or: git elegant [--no-workflows] <command> [args]
1010

11+
-h, --help, help displays help
12+
--version, version displays program version
13+
--no-workflows disables available workflows
1114

1215
There are commands used in various situations such as
1316

docs/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ A sample workflow execution:
4949
❗Please take into that if an action returns a non-zero exit code, the execution of the workflow
5050
will be interrupted.
5151

52+
If you want to skip workflows for the current command execution, just use `--no-workflows` option
53+
like `git elegant --no-workflows save-work`.
54+
5255
# Known limitations
5356
Support only one default remote - `origin`.
5457

libexec/git-elegant

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ remove-file() {
9494
9595
An assistant who carefully automates routine work with Git.
9696
97-
usage: git elegant [-h | --help | help]
98-
or: git elegant [-v | --version | version]
99-
or: git elegant <command> [args]
97+
usage: git elegant [-h | --help | help | --version | version]
10098
or: git elegant <command> [-h | --help | help]
99+
or: git elegant <command> [--no-workflows] [args]
100+
or: git elegant [--no-workflows] <command> [args]
101101
102+
-h, --help, help displays help
103+
--version, version displays program version
104+
--no-workflows disables available workflows
102105
103106
There are commands used in various situations such as
104107
@@ -145,12 +148,21 @@ MESSAGE
145148
--run-file ".workflows/${command}-${type}"
146149
}
147150

151+
--load-command(){
152+
local commandfile="${BINS}/git-elegant-${1}"
153+
if [[ -f ${commandfile} ]]; then
154+
source ${commandfile}
155+
else
156+
echo "Unknown command: git elegant ${1}"
157+
--usage
158+
exit 46
159+
fi
160+
}
161+
148162
--run-command() {
149163
# usage: <command name> [arg]...
150164
local COMMAND=${1}; shift
151-
. "${BINS}/git-elegant-${COMMAND}" 2>/dev/null || {
152-
echo "Unknown command: git elegant $COMMAND" && --usage && exit 46
153-
}
165+
--load-command ${COMMAND}
154166
case "${1}" in
155167
-h|--help|help)
156168
echo ""
@@ -159,6 +171,10 @@ MESSAGE
159171
command-description
160172
echo ""
161173
;;
174+
--no-workflows)
175+
shift
176+
default "$@"
177+
;;
162178
*)
163179
--run-workflow ahead ${COMMAND}
164180
default "$@"
@@ -171,9 +187,19 @@ main() {
171187
local COMMAND="none"
172188
[[ -n "$1" ]] && COMMAND="$1" && shift
173189
case "${COMMAND}" in
174-
none|-h|--help|help) --usage ;;
175-
-v|--version|version) cat "${BINS}/../version" ;;
176-
*) --run-command ${COMMAND} "$@" ;;
190+
none|-h|--help|help)
191+
--usage
192+
;;
193+
--version|version)
194+
cat "${BINS}/../version"
195+
;;
196+
--no-workflows)
197+
COMMAND=${1}
198+
shift
199+
--run-command ${COMMAND} --no-workflows "$@"
200+
;;
201+
*) --run-command ${COMMAND} "$@"
202+
;;
177203
esac
178204
}
179205

tests/git-elegant.bats

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,35 @@ load addons-common
5656
[[ "${lines[-1]}" == "after no" ]]
5757
}
5858

59+
@test "'git elegant': workflows are ignored if --no-workflows is set before a command" {
60+
perform-verbose mkdir -p .workflows
61+
echo "echo ahead no" | tee -i .workflows/commands-ahead
62+
echo "echo after no" | tee -i .workflows/commands-after
63+
perform-verbose chmod +x .workflows/*
64+
perform-verbose ls -lah .workflows/*
65+
check git-elegant --no-workflows commands
66+
[[ "$status" -eq 0 ]]
67+
[[ ! "${lines[@]}" =~ ".workflows/commands-ahead" ]]
68+
[[ ! "${lines[@]}" =~ ".workflows/commands-after" ]]
69+
}
70+
71+
@test "'git elegant': workflows are ignored if --no-workflows is set after a command" {
72+
perform-verbose mkdir -p .workflows
73+
echo "echo ahead no" | tee -i .workflows/commands-ahead
74+
echo "echo after no" | tee -i .workflows/commands-after
75+
perform-verbose chmod +x .workflows/*
76+
perform-verbose ls -lah .workflows/*
77+
check git-elegant commands --no-workflows
78+
[[ "$status" -eq 0 ]]
79+
[[ ! "${lines[@]}" =~ ".workflows/commands-ahead" ]]
80+
[[ ! "${lines[@]}" =~ ".workflows/commands-after" ]]
81+
}
82+
5983
teardown(){
6084
if [[ -d ".workflows" ]]; then
61-
perform-verbose rm -rv .git/.workflows .workflows
85+
perform-verbose rm -rv .workflows
86+
fi
87+
if [[ -d ".git/.workflows" ]]; then
88+
perform-verbose rm -rv .git/.workflows
6289
fi
6390
}

workflows

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ repository() {
3535
}
3636

3737
ci() {
38-
if [[ -f version ]]; then
39-
rm -v version
40-
fi
4138
docker run --rm -v $PWD:/eg ${WORKER_IMAGE} .workflows/ci-pipeline.bash testing
4239
}
4340

0 commit comments

Comments
 (0)