-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmob.sh
executable file
·164 lines (135 loc) · 4.25 KB
/
mob.sh
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
readonly PROGNAME=$(basename "$0")
readonly ARGS=( "$@" )
readonly WHOAMI=$(whoami)
readonly LOGFILE=$(mktemp /tmp/wip-mob-logs.XXXXXX)
readonly BRANCH=$(git branch --show-current)
readonly WIPPREFIX="wip-mob-"
readonly ORIGINALBRANCH=${BRANCH/$WIPPREFIX}
if [[ "$ORIGINALBRANCH" = "$BRANCH" ]] ; then
readonly WIPBRANCH="$WIPPREFIX$BRANCH"
readonly CURRENT_BRANCH_IS_WIP=0
else
readonly WIPBRANCH="$BRANCH"
readonly CURRENT_BRANCH_IS_WIP=1
fi
trap 'mob-error $?' EXIT
usage() {
cat <<- EOF
Usage: $PROGNAME subcommand
$PROGNAME takes the current branch you're on as the "main branch".
When you want to switch drivers, you just do "mob switch" which
creates a wip commit and pushes it to a wip branch. It then
switches you back to the main branch.
On another computer, you can use "mob continue" to continue where you
left off with the previous driver. It will take the wip commits from
the wip branch and apply them on the "main branch".
This essentially works like a "git stash" and "git stash pop" where
the stash is a remote branch with just one commit.
SUBCOMMANDS:
switch Commit and push everything that changed on the wip branch.
continue Continue working on the wip branch after somebody switched.
EOF
}
mob-switch() {
if [ $CURRENT_BRANCH_IS_WIP == 0 ]; then
printf 'log: %s\n' "$LOGFILE"
colorline "🕵️♂️ Making sure there are changes..."
{
if [ -z "$(git status --porcelain)" ]; then
echo "Working directory is clean, aborting."
return 1;
fi
} >> "$LOGFILE" 2>&1
colorline "🔥 Deleting local & remote wip branch..."
(
git branch -D "$WIPBRANCH" || true
git push origin ":$WIPBRANCH" || true
) >> "$LOGFILE" 2>&1
colorline "📦 Stashing changes..."
(
git stash push --include-untracked .
git stash apply
) >> "$LOGFILE" 2>&1
colorline "🌍 Making sure we are up-to-date with remote..."
(
git pull origin "$BRANCH"
git push origin "$BRANCH"
git checkout -b "$WIPBRANCH"
) >> "$LOGFILE" 2>&1
colorline "🚧 Creating wip commit..."
(
git add .
git commit -m "wip $WHOAMI" || true
) >> "$LOGFILE" 2>&1
colorline "🚀 Pushing changes to '$WIPBRANCH'..."
(
git push origin "$WIPBRANCH"
git checkout "$ORIGINALBRANCH"
) >> "$LOGFILE" 2>&1
colorline "🏁 Done"
fi
}
mob-continue() {
if [ $CURRENT_BRANCH_IS_WIP == 0 ]; then
printf 'log: %s\n' "$LOGFILE"
colorline "🛁 Making sure working directory is clean..."
{
if [ -n "$(git status --porcelain)" ]; then
echo "Working directory is not clean, aborting."
return 1;
fi
} >> "$LOGFILE" 2>&1
colorline "🌍 Making sure we are up-to-date with remote..."
(
git remote update
git pull --rebase origin "$BRANCH"
git checkout "$WIPBRANCH"
git pull --rebase origin "$WIPBRANCH"
) >> "$LOGFILE" 2>&1
colorline "🔨 Applying changes to our local branch..."
(
git reset --soft "$ORIGINALBRANCH"
git restore --staged .
git stash push --include-untracked .
git pull --rebase origin "$WIPBRANCH"
git checkout "$ORIGINALBRANCH"
git stash pop
) >> "$LOGFILE" 2>&1
colorline "🔥 Deleting local & remote wip branch..."
(
git branch -D "$WIPBRANCH"
git push origin ":$WIPBRANCH"
) >> "$LOGFILE" 2>&1
colorline "🏁 Done"
fi
}
colorline() {
printf -- '\033[37m-\033[36m %s\033[0m\n' "$1"
}
mob-error() {
if [ "$1" != "0" ]; then
printf -- '\033[31m %s\033[0m\n' \
"⚠️ An error occured. Check the logs to see what happened."
fi
}
main() {
if [ $# -eq 0 ]; then
usage
exit 1
fi
case $1 in
switch)
mob-switch
;;
continue)
mob-continue
;;
*)
usage
;;
esac
}
main "${ARGS[@]}"