# 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 tags(*args):
#   return run('tag', *args).splitlines()
# 
# def config_list(option):
#   try:
#     return run('config', '--get-all', option).split()
#   except subprocess2.CalledProcessError:
#     return []
# 
# GIT_EXE = 'git.bat' if sys.platform.startswith('win') else 'git'
# 
# Path: git_common.py
# def get_or_create_merge_base(branch, parent=None):
#   """Finds the configured merge base for branch.
# 
#   If parent is supplied, it's used instead of calling upstream(branch).
#   """
#   base = branch_config(branch, 'base')
#   base_upstream = branch_config(branch, 'base-upstream')
#   parent = parent or upstream(branch)
#   if not parent:
#     return None
#   actual_merge_base = run('merge-base', parent, branch)
# 
#   if base_upstream != parent:
#     base = None
#     base_upstream = None
# 
#   def is_ancestor(a, b):
#     return run_with_retcode('merge-base', '--is-ancestor', a, b) == 0
# 
#   if base:
#     if not is_ancestor(base, branch):
#       logging.debug('Found WRONG pre-set merge-base for %s: %s', branch, base)
#       base = None
#     elif is_ancestor(base, actual_merge_base):
#       logging.debug('Found OLD pre-set merge-base for %s: %s', branch, base)
#       base = None
#     else:
#       logging.debug('Found pre-set merge-base for %s: %s', branch, base)
# 
#   if not base:
#     base = actual_merge_base
#     manual_merge_base(branch, base, parent)
# 
#   return base
# 
# def root():
#   return config('depot-tools.upstream', 'origin/master')


import sys
import subprocess2
from git_common import current_branch, branches, tags, config_list, GIT_EXE
from git_common import get_or_create_merge_base, root
from third_party import colorama




CYAN = colorama.Fore.CYAN
GREEN = colorama.Fore.GREEN
MAGENTA = colorama.Fore.MAGENTA
RED = colorama.Fore.RED
WHITE = colorama.Fore.WHITE

BLUEBAK = colorama.Back.BLUE

BRIGHT = colorama.Style.BRIGHT
RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL

# Git emits combined color
BRIGHT_RED = '\x1b[1;31m'

def main():
  map_extra = config_list('depot_tools.map_extra')
  fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s'
  log_proc = subprocess2.Popen(
    [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(),
     '--color=always', '--date=short', ('--pretty=format:' + fmt)
    ] + map_extra + sys.argv[1:],
    stdout=subprocess2.PIPE,
    shell=False)

  current = current_branch()
  all_branches = set(branches())

# 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 tags(*args):
#   return run('tag', *args).splitlines()
# 
# def config_list(option):
#   try:
#     return run('config', '--get-all', option).split()
#   except subprocess2.CalledProcessError:
#     return []
# 
# GIT_EXE = 'git.bat' if sys.platform.startswith('win') else 'git'
# 
# Path: git_common.py
# def get_or_create_merge_base(branch, parent=None):
#   """Finds the configured merge base for branch.
# 
#   If parent is supplied, it's used instead of calling upstream(branch).
#   """
#   base = branch_config(branch, 'base')
#   base_upstream = branch_config(branch, 'base-upstream')
#   parent = parent or upstream(branch)
#   if not parent:
#     return None
#   actual_merge_base = run('merge-base', parent, branch)
# 
#   if base_upstream != parent:
#     base = None
#     base_upstream = None
# 
#   def is_ancestor(a, b):
#     return run_with_retcode('merge-base', '--is-ancestor', a, b) == 0
# 
#   if base:
#     if not is_ancestor(base, branch):
#       logging.debug('Found WRONG pre-set merge-base for %s: %s', branch, base)
#       base = None
#     elif is_ancestor(base, actual_merge_base):
#       logging.debug('Found OLD pre-set merge-base for %s: %s', branch, base)
#       base = None
#     else:
#       logging.debug('Found pre-set merge-base for %s: %s', branch, base)
# 
#   if not base:
#     base = actual_merge_base
#     manual_merge_base(branch, base, parent)
# 
#   return base
# 
# def root():
#   return config('depot-tools.upstream', 'origin/master')


# Path: git_map.py
import sys
import subprocess2
from git_common import current_branch, branches, tags, config_list, GIT_EXE
from git_common import get_or_create_merge_base, root
from third_party import colorama





CYAN = colorama.Fore.CYAN
GREEN = colorama.Fore.GREEN
MAGENTA = colorama.Fore.MAGENTA
RED = colorama.Fore.RED
WHITE = colorama.Fore.WHITE

BLUEBAK = colorama.Back.BLUE

BRIGHT = colorama.Style.BRIGHT
RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL

# Git emits combined color
BRIGHT_RED = '\x1b[1;31m'

def main():
  map_extra = config_list('depot_tools.map_extra')
  fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s'
  log_proc = subprocess2.Popen(
    [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(),
     '--color=always', '--date=short', ('--pretty=format:' + fmt)
    ] + map_extra + sys.argv[1:],
    stdout=subprocess2.PIPE,
    shell=False)

  current = current_branch()
  all_branches = set(branches())

  merge_base_map = {b: get_or_create_merge_base(b) for b in all_branches}