-
Notifications
You must be signed in to change notification settings - Fork 28
/
git-flow
executable file
·263 lines (206 loc) · 8.58 KB
/
git-flow
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python3
"""
Git Flow Helper CLI 👾.
Version: 0.4.0
Author: Edgar Pogosyan <[email protected]> (https://github.com/Edgar-P-yan)
Repository: https://github.com/Edgar-P-yan/better-git-flow-cli
Licence: this is licensed under MIT License.
See file LICENSE (https://github.com/Edgar-P-yan/better-git-flow-cli/blob/main/LICENSE).
Automatically manages hotfix branches, increments tag versions, pushes to remote and etc.
The standard git-flow extension is not good enough 💩.
Available commands:
./git-flow fix start [TAG]
Creates new "hotfix/*" branch with next-patch-version name,
or with the specified name.
./git-flow fix finish
Merges current "hotfix/*" branch into "main" and "develop",
sets tags, and pushes to remote. If anything worked successfully, then
deletes the hotfix branch.
./git-flow feat start DESCRIPTION
Start feature branch. Works only on "develop" and "release/*" branches.
./git-flow feat finish [-mr]
Finishes feature. Merges into develop branch. If the "-mr" flag is specified
then it also merges into the latest "release/*" branch, and increments "-rc.*" version on it.
./git-flow help
Show this help message.
"""
import sys
import os
from typing import Union
RELEASE_BRANCH_PREFIX = "release/"
FEATURE_BRANCH_PREFIX = "feature/"
HOTFIX_BRANCH_PREFIX = "hotfix/"
DEVELOP_BRANCH_NAME = "develop"
MAIN_BRANCH_NAME = "master"
def main() -> None:
"""
Program entrypoint.
"""
current_branch_name = get_current_branch_name()
try:
if (len(sys.argv) <= 1 or sys.argv[1] == "help" or
sys.argv[1] == "--help" or sys.argv[1] == "-h"):
print(HELP_MESSAGE)
elif sys.argv[1] == "fix":
if sys.argv[2] == "start":
if current_branch_name != "main":
print("Should be on main branch to start a hotfix. Now on " +
current_branch_name)
sys.exit(1)
if len(sys.argv) >= 4:
branch_name = HOTFIX_BRANCH_PREFIX + sys.argv[3]
print("Using specified name. Creating branch " + branch_name)
create_branch(branch_name)
else:
latest_tag = get_latest_tag()
branch_name = HOTFIX_BRANCH_PREFIX + \
semver_incr_patch(latest_tag)
print("Latest tag was: " + latest_tag +
". Creating branch " + branch_name)
create_branch(branch_name)
elif sys.argv[2] == "finish":
if not current_branch_name.startswith(HOTFIX_BRANCH_PREFIX):
print("Should be on a hotfix branch. Now on " +
current_branch_name)
sys.exit(1)
tag_name = current_branch_name[len(HOTFIX_BRANCH_PREFIX):]
exec_cmd("git checkout " + MAIN_BRANCH_NAME)
exec_cmd("git merge " + current_branch_name + " --no-ff")
exec_cmd("git tag " + tag_name)
exec_cmd("git checkout " + DEVELOP_BRANCH_NAME)
exec_cmd("git merge " + current_branch_name + " --no-ff")
exec_cmd("git checkout " + MAIN_BRANCH_NAME)
exec_cmd("git push --tags origin " + DEVELOP_BRANCH_NAME +
" " + MAIN_BRANCH_NAME)
exec_cmd("git branch -D " + current_branch_name)
else:
print(
"Unknown subcommand '" +
sys.argv[1] + " " + sys.argv[2] +
"'. See './git-flow help' for available commands."
)
sys.exit(1)
elif sys.argv[1] == "feat":
if sys.argv[2] == "start":
if (current_branch_name != DEVELOP_BRANCH_NAME and
not current_branch_name.startswith(RELEASE_BRANCH_PREFIX)):
print("Should be on develop branch to start a feature. Now on " +
current_branch_name)
sys.exit(1)
if len(sys.argv) < 4:
print(
"You should specify a DESCRIPTION for you feature. See ./git-flow help")
sys.exit(1)
branch_name = FEATURE_BRANCH_PREFIX + sys.argv[3]
print("Creating branch " + branch_name)
create_branch(branch_name)
elif sys.argv[2] == "finish":
if not current_branch_name.startswith(FEATURE_BRANCH_PREFIX):
print("Should be on a feature branch. Now on " +
current_branch_name)
sys.exit(1)
exec_cmd("git checkout " + DEVELOP_BRANCH_NAME)
exec_cmd("git merge " + current_branch_name + " --no-ff")
exec_cmd("git push")
if "-mr" in sys.argv:
release_branch_name = get_latest_release_branch_name()
if release_branch_name is None:
print("No release branch is found.")
else:
exec_cmd("git checkout " + release_branch_name)
exec_cmd("git merge " +
current_branch_name + " --no-ff")
latest_tag = get_latest_tag()
next_rc_tag = semver_incr_release_candidate(latest_tag)
exec_cmd("git tag " + next_rc_tag)
exec_cmd("git push && git push --tag")
else:
print("Unknown subcommand '" +
sys.argv[1] + "'. See './git-flow help' for available commands.")
sys.exit(1)
except Exception as inst:
print(inst.args[0])
sys.exit(1)
def get_latest_release_branch_name() -> Union[str, None]:
"""
Compares all release branches names, and returns the one
that has newest semver version in it's name.
"""
branches = sorted(map(
lambda l: l.strip(),
exec_cmd("git branch --list '" +
RELEASE_BRANCH_PREFIX + "*'").split("\n")
))
if len(branches) == 0:
return None
return branches[:-1]
def get_current_branch_name() -> str:
"""
Gets the name of the current branch
"""
return exec_cmd("git rev-parse --abbrev-ref HEAD").strip()
def get_latest_tag() -> str:
"""
Gets latest tag on the current branch.
"""
return exec_cmd("git describe --tags --abbrev=0").strip()
def semver_incr_patch(ver: str) -> str:
"""
Adds 1 to patch version of semver annotation.
Example: 1.0.0 becomes 1.0.1
"""
parts = ver.split(".")
patch = str(int(parts[-1]) + 1)
parts = parts[:-1]
parts.append(patch)
return ".".join(parts)
def semver_incr_release_candidate(ver: str) -> str:
"""
Accepts only v1.0.0-rc.1 format.
"""
parts = ver.split(".")
patch = str(int(parts[-1]) + 1)
parts = parts[:-1]
parts.append(patch)
return ".".join(parts)
def create_branch(name: str) -> None:
"""
Creates git branch
"""
exec_cmd("git checkout -b " + name)
def exec_cmd(cmd: str) -> str:
"""
Executes provided command and returns the output as string.
"""
stream = os.popen(cmd)
output = stream.read()
code = stream.close()
if code and code != 0:
raise RuntimeError("Command \"" + cmd +
"\" returned non-0 status code: " + str(code))
return output
HELP_MESSAGE = r""" _____ _ _ ______ _
/ ____(_) | | ____| |
| | __ _| |_| |__ | | _____ __
| | |_ | | __| __| | |/ _ \ \ /\ / /
| |__| | | |_| | | | (_) \ V V /
\_____|_|\__|_| |_|\___/ \_/\_/
Git Flow Helper CLI 👾.
Version: v0.4.0
Automatically manages hotfix branches, increments tag versions, pushes to remote and etc.
The standard git-flow extension is not good enough 💩.
Available commands:
./git-flow fix start [TAG]
Creates new "hotfix/*" branch with next-patch-version name, or with the specified name.
./git-flow fix finish
Merges current "hotfix/*" branch into "main" and "develop", sets tags, and pushes to remote. If anything worked successfully, then deletes the hotfix branch.
./git-flow feat start DESCRIPTION
Start feature branch. Works only on "develop" and "release/*" branches.
./git-flow feat finish [-mr]
Finishes feature. Merges into develop branch. If the "-mr" flag is specified
then it also merges into the latest "release/*" branch, and increments "-rc.*" version on it.
./git-flow help
Show this help message.
"""
if __name__ == "__main__":
main()