-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbash_limbo
107 lines (81 loc) · 2.8 KB
/
bash_limbo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
tcrSync() (
local TEST_COMMAND=${1}
runTests() {
eval "${TEST_COMMAND}" 2>/dev/null
}
revert() {
git reset --hard
}
tcr() {
# Either test, commmit, and return true, or revert and return false.
runTests
local TESTS_PASS=$?
if [ $TESTS_PASS -ne 0 ]; then
reset
echo "Reverting:"
eval ${TEST_COMMAND}
revert >/dev/null
return $TESTS_PASS
fi
echo
git add . && git commit -m "Limbo-ing! See tags/releases for what you'd normally expect in a commit message."
local COMMIT_RET=$?
if [ $COMMIT_RET -ne 0 ] && [ "`git rev-parse '@'`" != "`git rev-parse '@{upstream}'`" ];
then
git push
fi
}
clear
tcr
local TCR_RET=$?
# Update the remote. This works even if there is no remote.
git remote update
local REACHED_UPSTREAM=$?
# Try to find the upstream remote hash
#
# If an upstream exists, this prints the hash and returns true.
# If no upstream exists, this errors to /dev/null and returns false.
local REMOTE_REV
REMOTE_REV=$(git rev-parse '@{upstream}' 2>/dev/null)
local HAS_UPSTREAM=$?
local LOCAL_REV
LOCAL_REV=$(git rev-parse '@')
[ $HAS_UPSTREAM -eq 0 ] && [ $REACHED_UPSTREAM -eq 0 ] && [ "$LOCAL_REV" != "$REMOTE_REV" ]
local NEEDS_PULL=$?
while ( [ $NEEDS_PULL -eq 0 ] && ! git pull -q --rebase --ff-only );
do
read -r -p "Press <enter> to re-try the pull."
done
if [ $TCR_RET -eq 0 ] && [ $NEEDS_PULL -eq 0 ]; then
# If the tests passed the first time, and a push can be performed, test to ensure the
# rebase didn't break enything.
#
# Pulls are assumed to always be tested and working, so there's no need to run the tests
# if we only pull.
#
# Try running the tests silently, then run them loudly if they fail.
runTests &>/dev/null || ( clear; runTests ; false; ) && git push
fi
)
limbo() {
local DOTLIMBO_COMMAND
if [ ! -d ".git" ]; then
echo "You must be in the top level of a git repository to use limbo"
return 1
fi
[ -e ".limbo" ] && DOTLIMBO_COMMAND=$(cat .limbo)
# Prefer the expansion of the $1 positional parameter to the
# DOTLIMBO_COMMAND when the former is not unset (see BASH :- operator)
local TEST_COMMAND=${1-"${DOTLIMBO_COMMAND}"}
if [ -z "$TEST_COMMAND" ]; then
echo "Usage: limbo \"<command to run tests>\""
echo -e "\nAlternatively, you can put your test command in \".limbo\"."
return 1
fi
while : ; do
tcrSync "${TEST_COMMAND}"
echo
read -rs -p "<enter> to test && commit || revert "
done
}