annotate python/git_merge_master.py @ 898:e7ab4cf29f7b

py3
author Jeff Hammel <k0scist@gmail.com>
date Mon, 01 Aug 2022 16:42:02 -0700
parents 3059ee249888
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
639
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 merge master to branch
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 # TODO: combine with k0s.org/hg/gut
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import subprocess
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import sys
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import tempfile
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 from which import which
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 class Git(object):
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def branch(self):
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 """returns branch you are on"""
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 return self.branches()[0]
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 def branches(self):
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 """return all branches, active first"""
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 output = subprocess.check_output(['git', 'branch']).strip()
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 lines = sorted(output.splitlines(), key=lambda line: line.startswith('*'), reverse=True)
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 return [line.strip('*').strip() for line in lines]
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 def diff(self):
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """returns diff between active branch and master"""
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 branch = self.branch()
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 if branch == 'master':
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 raise AssertionError("Cannot be on the master branch")
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 merge_base = subprocess.check_output(['git', 'merge-base', 'HEAD', 'master']).strip()
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 return subprocess.check_output(['git', 'diff', merge_base])
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 def merge(self):
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 pass
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 def main(args=sys.argv[1:]):
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 parser = argparse.ArgumentParser(description=__doc__)
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 options = parser.parse_args(args)
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 # find branch
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 git = Git()
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 print (git.diff())
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 if __name__ == '__main__':
3be3b16aeda0 stub for merging from master
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 main()