Mercurial > hg > config
annotate python/git_merge_master.py @ 732:fc81284013bf
shebang
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 03 Feb 2015 14:48:40 -0800 |
parents | 3059ee249888 |
children |
rev | line source |
---|---|
639 | 1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 merge master to branch | |
6 """ | |
7 # TODO: combine with k0s.org/hg/gut | |
8 | |
9 import argparse | |
10 import os | |
11 import subprocess | |
12 import sys | |
13 import tempfile | |
14 from which import which | |
15 | |
16 class Git(object): | |
17 def branch(self): | |
18 """returns branch you are on""" | |
19 return self.branches()[0] | |
20 def branches(self): | |
21 """return all branches, active first""" | |
22 output = subprocess.check_output(['git', 'branch']).strip() | |
23 lines = sorted(output.splitlines(), key=lambda line: line.startswith('*'), reverse=True) | |
24 return [line.strip('*').strip() for line in lines] | |
25 | |
26 def diff(self): | |
27 """returns diff between active branch and master""" | |
28 branch = self.branch() | |
29 if branch == 'master': | |
30 raise AssertionError("Cannot be on the master branch") | |
31 merge_base = subprocess.check_output(['git', 'merge-base', 'HEAD', 'master']).strip() | |
32 return subprocess.check_output(['git', 'diff', merge_base]) | |
33 | |
640
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
34 def checkout(self, branch): |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
35 subprocess.check_output(['git', 'checkout', branch]) |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
36 |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
37 def pull(self, branch='master'): |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
38 current_branch = self.branch() |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
39 if current_branch != branch: |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
40 self.checkout(branch) |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
41 subprocess.check_output(['git', 'pull', 'origin', branch]) |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
42 if current_branch != branch: |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
43 self.checkout(current_branch) |
3059ee249888
STUB: python/git_merge_master.py
Jeff Hammel <k0scist@gmail.com>
parents:
639
diff
changeset
|
44 |
639 | 45 def merge(self): |
46 pass | |
47 | |
48 def main(args=sys.argv[1:]): | |
49 | |
50 parser = argparse.ArgumentParser(description=__doc__) | |
51 options = parser.parse_args(args) | |
52 | |
53 # find branch | |
54 git = Git() | |
55 print (git.diff()) | |
56 | |
57 | |
58 if __name__ == '__main__': | |
59 main() |