-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitvb-pick
executable file
·80 lines (65 loc) · 2.01 KB
/
gitvb-pick
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
#!/usr/bin/env bash
set -e
GITVB_HOME="./.git/.gitvb-v0"
MAIN_BRANCH="${MAIN_BRANCH:-main}"
if [ "$(git branch --show-current)" != "$MAIN_BRANCH" ]; then
echo "you are not on the main branch: $MAIN_BRANCH"
exit 1
fi
if [ ! -z "$(git diff --staged)" ]; then
echo "there are staged changes. exiting."
exit 1
fi
if [ "$1" == "" ]; then
echo "usage: gitvb-pick <branch>"
exit 1
fi
pickBranch="$1"
newVbFilePath="$GITVB_HOME/$pickBranch.gitvb"
if [ -f "$newVbFilePath" ]; then
echo "already picked $pickBranch"
exit 2
fi
mkdir -p "$(dirname "$newVbFilePath")"
# we can only pick branches that are up to date
echo "pick: fetch"
git fetch --progress
echo "pick: is pickable?"
gitvb-is-pickable $pickBranch
echo "pick: create wip stash"
gitvb-wip "pick-$pickBranch"
# 1. record our current hash
currentHash=$(git rev-parse "origin/$MAIN_BRANCH")
echo "pick: setting base as = $currentHash"
# 2. get each commit hash on the target branch
commitsToApply=$(git log --cherry $MAIN_BRANCH...$pickBranch --pretty=format:"%H" --reverse)
# 3. for each hash
# - cherry-pick the hash
# - record it on success
echo "base=$currentHash" > $newVbFilePath
# printf "applied=" >> $newVbFilePath
if [ -z "$commitsToApply" ]; then
echo 'pick: this branch does not yet differ, applied anyway'
echo "pick: undo wip stash"
gitvb-wip-undo "pick-$pickBranch"
echo ""
echo "succesfully picked $pickBranch"
echo ""
exit 0
fi
while IFS= read -r line; do
echo "pick: applying $line"
newCommitMessage="[gitvb:$pickBranch] $(git log -1 --format=%B $line)"
echo "$newCommitMessage" > /tmp/gitvb-commit
LASTEDITOR="$EDITOR"
EDITOR="gitvb-editor-commit" git cherry-pick -e $line
EDITOR="$LASTEDITOR"
appliedCommitHash="$(git log -1 --format="%H")"
echo "pick: applied as $appliedCommitHash"
# echo "$appliedCommitHash" >> $newVbFilePath
done <<< "$commitsToApply"
echo "pick: undo wip stash"
gitvb-wip-undo "pick-$pickBranch"
echo ""
echo "succesfully picked $pickBranch"
echo ""