-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitvb-resync
executable file
·126 lines (104 loc) · 3.05 KB
/
gitvb-resync
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
#!/usr/bin/env bash
set -e
GITVB_HOME="./.git/.gitvb-v0"
MAIN_BRANCH="${MAIN_BRANCH:-main}"
STAGE_STASH_MSG="STAGED CHANGES FROM GITVB-RESYNC"
stashedStage="0"
if [ ! -z "$(git diff --staged)" ]; then
# echo "there are staged changes. exiting."
echo "WARNING: stashing the stage!"
git stash --staged -m "$STAGE_STASH_MSG"
stashedStage="1"
# exit 1
fi
if [ "$(git branch --show-current)" != "$MAIN_BRANCH" ]; then
echo "you are not on the main branch: $MAIN_BRANCH"
exit 1
fi
# 1. fetch
# echo a
# git fetch --progress
echo "fetching..."
git fetch --quiet
echo "ok!"
# nBehind=$(git rev-list --count $MAIN_BRANCH..origin/$MAIN_BRANCH)
# if [ "$nBehind" == "0" ]; then
# echo
# echo "you are up to date already. no work needs to be done."
# echo
# exit 18
# fi
echo b...
# 2. wip commit if any unstaged files changed
gitvb-wip resync
echo c...
pickedBranches="$(gitvb-ls)"
# 3. unpick every picked branch
if [ ! -z "$pickedBranches" ]; then
while IFS= read -r line; do
echo i will now unpick $line
gitvb-unpick $line
done <<< "$pickedBranches"
fi
# 5. rebase master on origin/master
echo updating $MAIN_BRANCH
git pull --rebase
repickBranches=""
# 4. checkout each picked branch, and with it rebase on origin/master
if [ ! -z "$pickedBranches" ]; then
echo d...
while IFS= read -r line; do
echo i will now checkout $line
git checkout "$line"
# determine if the branch was merged into main with the original branch deleted
# in this case, we should cleanup our local branch and not repick it
if [ ! -z "$(git ls-remote --heads origin $line)" ]; then
echo "Ref $line exists on the remote."
echo i will now pull upstream changes to $line
git pull --rebase
# git pull --ff-only || echo "nothing to fast-forward"
echo i will now updated changes to $line with changes from origin/$MAIN_BRANCH
git rebase "origin/$MAIN_BRANCH"
repickBranches="$repickBranches
$line"
else
echo
echo "!! REMOVE REFERENCE IS GONE !!"
echo
git checkout "$MAIN_BRANCH"
echo "removing branch since the remote is gone"
git branch -D $line
fi
done <<< "$pickedBranches"
fi
# 6. (re)pick each branch
echo e...
git checkout $MAIN_BRANCH
if [ ! -z "$repickBranches" ]; then
echo f
while IFS= read -r line; do
if [ ! -z "$line" ]; then
echo i will now re-pick $line
gitvb-pick $line
fi
done <<< "$repickBranches"
fi
echo g...
# 7. undo wip
gitvb-wip-undo resync
# undo the stage stash
if [ "$stashedStage" == "1" ]; then
if [[ "$(git stash list)" == *$STAGE_STASH_MSG* ]]; then
echo "Reapplying stash onto stage!"
git stash pop --index # index pops onto the stage
else
echo "oops, don't have expected stash for the stage!"
exit 8
fi
else
echo "no stash to reapply back to stage"
fi
echo ""
echo "everything is now up to date"
echo ""
gitvb-status