-
Notifications
You must be signed in to change notification settings - Fork 0
/
shipgit-feature
151 lines (122 loc) · 4.52 KB
/
shipgit-feature
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
#!/usr/bin/env bash
usage() {
echo "usage: shipgit feature [action] [-options]"
echo " shipgit feature start <name>"
echo " shipgit feature finish"
echo " shipgit feature sync"
}
cmd_help() {
usage
exit 0
}
cmd_default() {
usage
exit 0
}
handle_feature_finish_conflict() {
MAIN_BRANCH=$(git config --get shipgit.branch.main)
FEATURE_BRANCH=$1
echo "++++++++++++++++++++++++++ REBASE CONFLICT +++++++++++++++++++++++++++"
echo ""
echo "There are conflicts between $FEATURE_BRANCH and $MAIN_BRANCH"
echo ""
echo "Please fix the conflicts and when you are done, run:"
echo " $ git rebase --continue"
echo ""
echo "After that you can checkout back to your feature branch and run:"
echo " $ shipgit feature finish"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
}
handle_feature_sync_conflict() {
MAIN_BRANCH=$(git config --get shipgit.branch.main)
FEATURE_BRANCH=$1
echo "++++++++++++++++++++++++++ REBASE CONFLICT +++++++++++++++++++++++++++"
echo ""
echo "There are conflicts between $FEATURE_BRANCH and $MAIN_BRANCH"
echo ""
echo "Please fix the conflicts and when you are done, run:"
echo " $ git rebase --continue"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
}
handle_not_in_feature_branch() {
ACTION = $1
echo "+++++++++++++++++++++++ ATTENTION +++++++++++++++++++++++"
echo ""
echo "You are not in a feature branch."
echo "shipgit feature $ACTION must be called in a feature branch."
echo ""
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
}
cmd_start() {
# Creating the a new feature branch
PREFIX=$(git config --get shipgit.prefix.feature)
MAIN_BRANCH=$(git config --get shipgit.branch.main)
NAME=$3
FEATURE_BRANCH=$PREFIX$NAME
echo "=========================== Starting feature branch ==========================="
echo ""
echo "Steps:"
git checkout -q "$MAIN_BRANCH" &> /dev/null
git pull origin "$MAIN_BRANCH" &> /dev/null
echo " 1. Pulled latest changes from remote $MAIN_BRANCH."
git branch --no-track "$FEATURE_BRANCH" "$MAIN_BRANCH" &> /dev/null
echo " 2. Created new $FEATURE_BRANCH branch based on $MAIN_BRANCH."
git checkout -q "$FEATURE_BRANCH" &> /dev/null
git push -u origin "$FEATURE_BRANCH" &> /dev/null
echo " 3. Pushed $FEATURE_BRANCH to remote and added upstream reference."
echo ""
echo "==============================================================================="
}
cmd_sync() {
MAIN_BRANCH=$(git config --get shipgit.branch.main)
FEATURE_BRANCH=$(git branch --show-current)
PREFIX=$(git config --get shipgit.prefix.feature)
if ! [[ "$FEATURE_BRANCH" =~ ^$PREFIX.* ]]; then
handle_not_in_feature_branch "sync"
fi
echo "======================= Syncing feature branch ======================="
echo ""
echo "Steps:"
git checkout -q "$MAIN_BRANCH" &> /dev/null
git pull origin $MAIN_BRANCH &> /dev/null
git fetch --tags &> /dev/null
echo " 1. Pulled latest changes from $MAIN_BRANCH."
git checkout -q "$FEATURE_BRANCH" &> /dev/null
git rebase "$MAIN_BRANCH" &> /dev/null || handle_feature_sync_conflict "$FEATURE_BRANCH"
echo " 2. Rebased $FEATURE_BRANCH with $MAIN_BRANCH."
echo ""
echo "======================================================================"
}
cmd_finish() {
MAIN_BRANCH=$(git config --get shipgit.branch.main)
FEATURE_BRANCH=$(git branch --show-current)
PREFIX=$(git config --get shipgit.prefix.feature)
if ! [[ "$FEATURE_BRANCH" =~ ^$PREFIX.* ]]; then
handle_not_in_feature_branch "finish"
fi
echo "=========================== Finishing feature branch ==========================="
echo ""
echo "Steps:"
git checkout -q "$MAIN_BRANCH" &> /dev/null
git pull origin $MAIN_BRANCH &> /dev/null
git fetch --tags &> /dev/null
echo " 1. Pulled latest changes from remote $MAIN_BRANCH."
git checkout -q "$FEATURE_BRANCH" &> /dev/null
git rebase "$MAIN_BRANCH" &> /dev/null || handle_feature_finish_conflict "$FEATURE_BRANCH"
echo " 2. Rebased $FEATURE_BRANCH with $MAIN_BRANCH."
git checkout -q "$MAIN_BRANCH" &> /dev/null
git merge --log --ff-only "$FEATURE_BRANCH" &> /dev/null
echo " 3. Merged $FEATURE_BRANCH into $MAIN_BRANCH."
git push origin "$MAIN_BRANCH" &> /dev/null
echo " 4. Pushed $MAIN_BRANCH to remote."
git branch -D "$FEATURE_BRANCH" &> /dev/null
git push -d origin "$FEATURE_BRANCH" &> /dev/null
echo " 5. Deleted $FEATURE_BRANCH locally and remotely."
echo ""
echo "================================================================================"
}