# Path: git_common.py
# def current_branch():
#   try:
#     return run('rev-parse', '--abbrev-ref', 'HEAD')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def branches(*args):
#   NO_BRANCH = ('* (no branch', '* (detached from ')
# 
#   key = 'depot-tools.branch-limit'
#   limit = 20
#   try:
#     limit = int(config(key, limit))
#   except ValueError:
#     pass
# 
#   raw_branches = run('branch', *args).splitlines()
# 
#   num = len(raw_branches)
#   if num > limit:
#     print >> sys.stderr, textwrap.dedent("""\
#     Your git repo has too many branches (%d/%d) for this tool to work well.
# 
#     You may adjust this limit by running:
#       git config %s <new_limit>
#     """ % (num, limit, key))
#     sys.exit(1)
# 
#   for line in raw_branches:
#     if line.startswith(NO_BRANCH):
#       continue
#     yield line.split()[-1]
# 
# def upstream(branch):
#   try:
#     return run('rev-parse', '--abbrev-ref', '--symbolic-full-name',
#                branch+'@{upstream}')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def hash_one(reflike):
#   return run('rev-parse', reflike)
# 
# def hash_multi(*reflike):
#   return run('rev-parse', *reflike).splitlines()
# 
# Path: git_common.py
# def tags(*args):
#   return run('tag', *args).splitlines()


import collections
import sys
from third_party import colorama
from third_party.colorama import Fore, Style
from git_common import current_branch, branches, upstream, hash_one, hash_multi
from git_common import tags
def print_branch(cur, cur_hash, branch, branch_hashes, par_map, branch_map,
                 tag_set, depth=0):
  branch_hash = branch_hashes[branch]

  color = color_for_branch(branch, branch_hash, cur_hash, tag_set)

  suffix = ''
  if cur == 'HEAD':
    if branch_hash == cur_hash:
      suffix = ' *'
  elif branch == cur:
    suffix = ' *'

  print color + "  "*depth + branch + suffix
  for child in par_map.pop(branch, ()):
    print_branch(cur, cur_hash, child, branch_hashes, par_map, branch_map,
                 tag_set, depth=depth+1)


def main(argv):
  colorama.init()
  assert len(argv) == 1, "No arguments expected"
  branch_map = {}
  par_map = collections.defaultdict(list)
  for branch in branches():
    par = upstream(branch) or NO_UPSTREAM
    branch_map[branch] = par
    par_map[par].append(branch)

  current = current_branch()

# Path: git_common.py
# def current_branch():
#   try:
#     return run('rev-parse', '--abbrev-ref', 'HEAD')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def branches(*args):
#   NO_BRANCH = ('* (no branch', '* (detached from ')
# 
#   key = 'depot-tools.branch-limit'
#   limit = 20
#   try:
#     limit = int(config(key, limit))
#   except ValueError:
#     pass
# 
#   raw_branches = run('branch', *args).splitlines()
# 
#   num = len(raw_branches)
#   if num > limit:
#     print >> sys.stderr, textwrap.dedent("""\
#     Your git repo has too many branches (%d/%d) for this tool to work well.
# 
#     You may adjust this limit by running:
#       git config %s <new_limit>
#     """ % (num, limit, key))
#     sys.exit(1)
# 
#   for line in raw_branches:
#     if line.startswith(NO_BRANCH):
#       continue
#     yield line.split()[-1]
# 
# def upstream(branch):
#   try:
#     return run('rev-parse', '--abbrev-ref', '--symbolic-full-name',
#                branch+'@{upstream}')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def hash_one(reflike):
#   return run('rev-parse', reflike)
# 
# def hash_multi(*reflike):
#   return run('rev-parse', *reflike).splitlines()
# 
# Path: git_common.py
# def tags(*args):
#   return run('tag', *args).splitlines()


# Path: git_map_branches.py
import collections
import sys
from third_party import colorama
from third_party.colorama import Fore, Style
from git_common import current_branch, branches, upstream, hash_one, hash_multi
from git_common import tags

def print_branch(cur, cur_hash, branch, branch_hashes, par_map, branch_map,
                 tag_set, depth=0):
  branch_hash = branch_hashes[branch]

  color = color_for_branch(branch, branch_hash, cur_hash, tag_set)

  suffix = ''
  if cur == 'HEAD':
    if branch_hash == cur_hash:
      suffix = ' *'
  elif branch == cur:
    suffix = ' *'

  print color + "  "*depth + branch + suffix
  for child in par_map.pop(branch, ()):
    print_branch(cur, cur_hash, child, branch_hashes, par_map, branch_map,
                 tag_set, depth=depth+1)


def main(argv):
  colorama.init()
  assert len(argv) == 1, "No arguments expected"
  branch_map = {}
  par_map = collections.defaultdict(list)
  for branch in branches():
    par = upstream(branch) or NO_UPSTREAM
    branch_map[branch] = par
    par_map[par].append(branch)

  current = current_branch()

  hashes = hash_multi(current, *branch_map.keys())