Switch to unified view

a/scripts/git-mr b/scripts/git-mr
1
#!/usr/bin/env python
1
#!/usr/bin/env python
2
2
3
import re
3
import shlex
4
import shlex
4
import subprocess
5
import subprocess
5
import sys
6
import sys
6
try:
7
try:
7
    import argparse
8
    import argparse
...
...
13
    from pygments.console import colorize
14
    from pygments.console import colorize
14
except ImportError:
15
except ImportError:
15
    def colorize(color, message):
16
    def colorize(color, message):
16
        return message
17
        return message
17
18
18
usage = "git mr [-a|-r] [upstream]"
19
usage = "git mr [-a|-r] [--grep PATTERN] [upstream]"
19
20
20
21
21
def main():
22
def main():
22
    parser = argparse.ArgumentParser(prog='git mr', usage=usage)
23
    parser = argparse.ArgumentParser(prog='git mr', usage=usage)
23
    parser.add_argument('-r', action='store_true', 
24
    parser.add_argument('-r', action='store_true', 
24
                        dest='examine_remote_branches', default=False,
25
                        dest='examine_remote_branches', default=False,
25
                        help='examine remote branches')
26
                        help='examine remote branches')
26
    parser.add_argument('-a', action='store_true',
27
    parser.add_argument('-a', action='store_true',
27
                        dest='examine_all_branches', default=False,
28
                        dest='examine_all_branches', default=False,
28
                        help='examine all branches')
29
                        help='examine all branches')
30
    parser.add_argument('--grep', nargs=1, metavar='PATTERN', default=None,
31
                        help='only examine branches matching PATTERN')
29
    parser.add_argument('upstream', nargs='?', default='HEAD',
32
    parser.add_argument('upstream', nargs='?', default='HEAD',
30
                        help='the branch to which everything else is compared')
33
                        help='the branch to which everything else is compared, defaults to HEAD')
31
    args = parser.parse_args()
34
    args = parser.parse_args()
32
35
33
    if args.examine_all_branches:
36
    if args.examine_all_branches:
34
        merged_branches = git('branch', '-a', '--merged', args.upstream)
37
        merged_branches = git('branch', '-a', '--merged', args.upstream)
35
        unmerged_branches = git('branch', '-a', '--no-merged', args.upstream)
38
        unmerged_branches = git('branch', '-a', '--no-merged', args.upstream)
...
...
41
        unmerged_branches = git('branch', '--no-merged', args.upstream)
44
        unmerged_branches = git('branch', '--no-merged', args.upstream)
42
45
43
    merged_branches = [ line[2:] for line in merged_branches ]
46
    merged_branches = [ line[2:] for line in merged_branches ]
44
    unmerged_branches = [ line[2:] for line in unmerged_branches ]
47
    unmerged_branches = [ line[2:] for line in unmerged_branches ]
45
    really_unmerged_branches = []
48
    really_unmerged_branches = []
49
50
    if args.grep:
51
        filter = re.compile(args.grep[0])
52
        merged_branches = [ b for b in merged_branches if filter.search(b) ]
53
        unmerged_branches = [ b for b in unmerged_branches if filter.search(b) ]
46
54
47
    if merged_branches:
55
    if merged_branches:
48
        print('Branches contained by %s:' % args.upstream)
56
        print('Branches contained by %s:' % args.upstream)
49
        for branch in merged_branches:
57
        for branch in merged_branches:
50
            print('  '+colorize('green', branch))
58
            print('  '+colorize('green', branch))