Skip to content

Commit 843fe1d

Browse files
committed
refactor: refactoring gitMergeFlow
1 parent 4d49842 commit 843fe1d

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

gitMergeFlow.py

+40-31
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
11
#!usr/bin/env python
22

3-
import sys
3+
import argparse
44

55
from git import Repo
66
from termcolor import cprint
77

8-
args = sys.argv[1:]
98

10-
repo = Repo()
11-
git = repo.git
12-
# cache current branch
13-
currentBranchName = repo.head.ref.name
14-
15-
# check if working tree is clean
16-
isDirty = repo.is_dirty(untracked_files=True)
17-
if (isDirty):
18-
cprint('Unclean working tree. Commit or stash changes first', 'red')
19-
exit(1)
20-
21-
22-
if len(args) == 0:
23-
cprint('No branch name provided', 'red')
24-
print(' - Usage: python3 <path_to_this_file> <branch1_name> <branch2_name> ...')
25-
exit(1)
26-
27-
# check if all branches exist
28-
branchNames = [x.name for x in repo.branches]
29-
invalidBranches = [x for x in args if x not in branchNames]
30-
if len(invalidBranches) > 0:
31-
cprint('Branch does not exist: {}'.format(invalidBranches), 'red')
32-
exit(1)
33-
34-
35-
def mergeFlow(branches):
36-
print('\nSTART MERGE FLOW...')
9+
def mergeFlow(branches, currentBranchName):
10+
print('\n' + '=' * 40 + '\n')
11+
print('START MERGE GIT BRANCHES...' + '\n')
3712

3813
for idx, branch in enumerate(branches):
3914
git.checkout(branch)
@@ -43,11 +18,45 @@ def mergeFlow(branches):
4318
continue
4419
git.merge('-')
4520
git.push()
46-
print(' ✅ {} -> {}'.format(branches[idx - 1], branch))
21+
print(' ✅ {} -> {}'.format(branches[idx - 1], branch))
4722

4823
git.checkout(currentBranchName)
4924
print()
5025
cprint('🎉 COMPLETED: {}'.format(' -> '.join(branches)), 'green')
5126

5227

53-
mergeFlow(args)
28+
def checkWorkingTree(repo):
29+
# check if working tree is clean
30+
isDirty = repo.is_dirty(untracked_files=True)
31+
if (isDirty):
32+
cprint('Unclean working tree. Commit or stash changes first', 'red')
33+
exit(1)
34+
35+
36+
def validateBranches(repo, targetBranches):
37+
# check if all branches exist
38+
branches = repo.branches
39+
branchNames = list(map(lambda x: x.name, branches))
40+
invalidBranches = list(
41+
filter(lambda x: x not in branchNames, targetBranches))
42+
if len(invalidBranches) > 0:
43+
cprint('Branch does not exist: {}'.format(invalidBranches), 'red')
44+
exit(1)
45+
46+
47+
if __name__ == "__main__":
48+
parser = argparse.ArgumentParser(description="Merge git branches")
49+
parser.add_argument(
50+
'targetBranches', help="branches to be merged", nargs='+')
51+
args = parser.parse_args()
52+
targetBranches = args.targetBranches
53+
54+
repo = Repo()
55+
git = repo.git
56+
# cache current branch
57+
currentBranchName = repo.head.ref.name
58+
59+
# check if working tree is clean
60+
checkWorkingTree(repo)
61+
validateBranches(repo, targetBranches)
62+
mergeFlow(targetBranches, currentBranchName)

0 commit comments

Comments
 (0)