1
1
#!usr/bin/env python
2
2
3
- import sys
3
+ import argparse
4
4
5
5
from git import Repo
6
6
from termcolor import cprint
7
7
8
- args = sys .argv [1 :]
9
8
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 ('\n START MERGE FLOW...' )
9
+ def mergeFlow (branches , currentBranchName ):
10
+ print ('\n ' + '=' * 40 + '\n ' )
11
+ print ('START MERGE GIT BRANCHES...' + '\n ' )
37
12
38
13
for idx , branch in enumerate (branches ):
39
14
git .checkout (branch )
@@ -43,11 +18,45 @@ def mergeFlow(branches):
43
18
continue
44
19
git .merge ('-' )
45
20
git .push ()
46
- print (' ✅ {} -> {}' .format (branches [idx - 1 ], branch ))
21
+ print (' ✅ {} -> {}' .format (branches [idx - 1 ], branch ))
47
22
48
23
git .checkout (currentBranchName )
49
24
print ()
50
25
cprint ('🎉 COMPLETED: {}' .format (' -> ' .join (branches )), 'green' )
51
26
52
27
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