-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitvb-commit
executable file
·112 lines (98 loc) · 2.84 KB
/
gitvb-commit
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
#!/usr/bin/env bash
set -e
GITVB_HOME="./.git/.gitvb-v0"
MAIN_BRANCH="${MAIN_BRANCH:-main}"
if [ "$1" == "" ]; then
echo "no branch provided so trying to determine it automatically"
# echo "usage: gitvb-commit <branch>"
commitBranch="$(gitvb-determine-stage-branch 2> /dev/null || {
echo "" >&2
echo "Cannot determine branch for your stage, please specify one manually." >&2
echo " $ gitvb-commit [branch]" >&2
echo "" >&2
echo "staged:" >&2
gitvb-list-staged >&2
echo "" >&2
echo "picked branches:" >&2
gitvb-ls | awk '{print " - " $1}' >&2
echo "" >&2
exit 1
})"
echo "determined branch is: $commitBranch"
# exit 1
else
commitBranch="$1"
fi
if [ -z "$(git diff --staged)" ]; then
echo "there are no staged changes. exiting."
echo "stage changes with $ git add"
exit 1
fi
if [ "$(git branch --show-current)" != "$MAIN_BRANCH" ]; then
echo "you are not on the main branch: $MAIN_BRANCH"
exit 1
fi
# REMOTE_COMMITS=$(git rev-list --count "origin/$MAIN_BRANCH" ^"$commitBranch")
# if [ "$REMOTE_COMMITS" -gt 0 ]; then
# echo ""
# echo "*** Branch '$BRANCH_NAME' look bad. ***"
# echo ""
# exit 1
# fi
vbFilePath="$GITVB_HOME/$commitBranch.gitvb"
if [ ! -f "$vbFilePath" ]; then
echo "not picked branch $commitBranch, so cannot commit there"
exit 2
fi
# determine if we're up-to-date before allowing a commit, otherwise when we repick we'll tell the user to do gitvb-sync-unpicked
echo "fetching..."
git fetch --quiet
echo "ok!"
nMainBehind=$(git rev-list --count $MAIN_BRANCH..origin/$MAIN_BRANCH)
if [ "$nMainBehind" != "0" ]; then
echo ""
echo "your main branch $MAIN_BRANCH is behind by $nMainBehind commits"
echo " before trying to commit, you probably want to run"
echo " $ gitvb-resync"
echo ""
echo "(no work was done)"
echo ""
exit 3
fi
if [ -z "$(git ls-remote --heads origin $commitBranch)" ]; then
echo ""
echo "the branch '$commitBranch' does not exist on the remote"
echo "(no work was done)"
echo ""
exit 4
fi
git commit -v
# then get previous/last commit id
addedCommitHash="$(git rev-parse "$MAIN_BRANCH")"
gitvb-wip commit
echo "1"
git checkout "$commitBranch"
echo "2"
git cherry-pick --ff "$addedCommitHash"
echo "3"
git checkout "$MAIN_BRANCH"
echo "3b"
git reset --hard HEAD~1
echo "4"
gitvb-unpick "$commitBranch"
echo "5"
gitvb-pick "$commitBranch"
echo "6"
gitvb-wip-undo commit
# 0. bail if branch not picked
# 1. git commit
# 2. write the commit hash to the gitvb file
# 3. wip commit if any unstaged files changed
# 4. checkout to target branch
# 5. cherry pick commit id over
# 6. checkout back to master
echo
echo "succesfully commited to $commitBranch!"
echo " you can upload your changes to the remote by running:"
echo " $ gitvb-publish $commitBranch"
echo