blob: fc6c8cec46f907df097fa5437820a7f6c805c50b [file] [log] [blame]
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00001#!/usr/bin/env python
iannucci@chromium.orga112f032014-03-13 07:47:50 +00002# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00006"""
7Provides a short mapping of all the branches in your local repo, organized by
8their upstream ('tracking branch') layout. Example:
9
10origin/master
11 cool_feature
12 dependent_feature
13 other_dependent_feature
14 other_feature
15
16Branches are colorized as follows:
17 * Red - a remote branch (usually the root of all local branches)
18 * Cyan - a local branch which is the same as HEAD
19 * Note that multiple branches may be Cyan, if they are all on the same
20 commit, and you have that commit checked out.
21 * Green - a local branch
22 * Magenta - a placeholder for the '{NO UPSTREAM}' "branch". If you have
23 local branches which do not track any upstream, then you will see this.
24"""
25import collections
26import sys
27
28from third_party import colorama
29from third_party.colorama import Fore, Style
30
31from git_common import current_branch, branches, upstream, hash_one, hash_multi
32
33NO_UPSTREAM = '{NO UPSTREAM}'
34
35def print_branch(cur, cur_hash, branch, branch_hashes, par_map, branch_map,
36 depth=0):
37 branch_hash = branch_hashes[branch]
38 if branch.startswith('origin'):
39 color = Fore.RED
40 elif branch == NO_UPSTREAM:
41 color = Fore.MAGENTA
42 elif branch_hash == cur_hash:
43 color = Fore.CYAN
44 else:
45 color = Fore.GREEN
46
47 if branch_hash == cur_hash:
48 color += Style.BRIGHT
49 else:
50 color += Style.NORMAL
51
iannucci@chromium.orga112f032014-03-13 07:47:50 +000052 suffix = ''
53 if cur == 'HEAD':
54 if branch_hash == cur_hash:
55 suffix = ' *'
56 elif branch == cur:
57 suffix = ' *'
58
59 print color + " "*depth + branch + suffix
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000060 for child in par_map.pop(branch, ()):
61 print_branch(cur, cur_hash, child, branch_hashes, par_map, branch_map,
62 depth=depth+1)
63
64
65def main(argv):
66 colorama.init()
67 assert len(argv) == 1, "No arguments expected"
68 branch_map = {}
69 par_map = collections.defaultdict(list)
70 for branch in branches():
71 par = upstream(branch) or NO_UPSTREAM
72 branch_map[branch] = par
73 par_map[par].append(branch)
74
75 current = current_branch()
76 hashes = hash_multi(current, *branch_map.keys())
77 current_hash = hashes[0]
78 par_hashes = {k: hashes[i+1] for i, k in enumerate(branch_map.iterkeys())}
79 par_hashes[NO_UPSTREAM] = 0
80 while par_map:
81 for parent in par_map:
82 if parent not in branch_map:
83 if parent not in par_hashes:
84 par_hashes[parent] = hash_one(parent)
85 print_branch(current, current_hash, parent, par_hashes, par_map,
86 branch_map)
87 break
88
89
90if __name__ == '__main__':
91 sys.exit(main(sys.argv))
92