git_map.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. # 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. """
  6. Enhances `git log --graph` view with information on commit branches + tags that
  7. point to them. Items are colorized as follows:
  8. * Cyan - Currently checked out branch
  9. * Green - Local branch
  10. * Red - Remote branches
  11. * Magenta - Tags
  12. * White - Merge Base Markers
  13. * Blue background - The currently checked out commit
  14. """
  15. import sys
  16. import subprocess2
  17. from git_common import current_branch, branches, tags, get_config_list, GIT_EXE
  18. from git_common import get_or_create_merge_base, root
  19. from third_party import colorama
  20. CYAN = colorama.Fore.CYAN
  21. GREEN = colorama.Fore.GREEN
  22. MAGENTA = colorama.Fore.MAGENTA
  23. RED = colorama.Fore.RED
  24. WHITE = colorama.Fore.WHITE
  25. BLUEBAK = colorama.Back.BLUE
  26. BRIGHT = colorama.Style.BRIGHT
  27. RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL
  28. # Git emits combined color
  29. BRIGHT_RED = '\x1b[1;31m'
  30. def print_help():
  31. names = {
  32. 'Cyan': CYAN,
  33. 'Green': GREEN,
  34. 'Magenta': MAGENTA,
  35. 'Red': RED,
  36. 'White': WHITE,
  37. 'Blue background': BLUEBAK,
  38. }
  39. msg = "usage: git map [-h] [<args>]\n"
  40. for line in __doc__.splitlines():
  41. for key in names.keys():
  42. if key in line:
  43. msg += line.replace('* ', '* ' + names[key])+RESET+'\n'
  44. break
  45. else:
  46. msg += line + '\n'
  47. sys.stdout.write(msg)
  48. def main(argv):
  49. if '-h' in argv:
  50. print_help()
  51. return 0
  52. map_extra = get_config_list('depot_tools.map_extra')
  53. fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%cd%Creset ~ %s'
  54. log_proc = subprocess2.Popen(
  55. [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(),
  56. '--color=always', '--date=short', ('--pretty=format:' + fmt)
  57. ] + map_extra + argv,
  58. stdout=subprocess2.PIPE,
  59. shell=False)
  60. current = current_branch()
  61. all_branches = set(branches())
  62. merge_base_map = {b: get_or_create_merge_base(b) for b in all_branches}
  63. merge_base_map = {b: v for b, v in merge_base_map.iteritems() if v}
  64. if current in all_branches:
  65. all_branches.remove(current)
  66. all_tags = set(tags())
  67. try:
  68. for line in log_proc.stdout.xreadlines():
  69. if merge_base_map:
  70. commit = line[line.find(BRIGHT_RED)+len(BRIGHT_RED):line.find('\t')]
  71. base_for_branches = set()
  72. for branch, sha in merge_base_map.iteritems():
  73. if sha.startswith(commit):
  74. base_for_branches.add(branch)
  75. if base_for_branches:
  76. newline = '\r\n' if line.endswith('\r\n') else '\n'
  77. line = line.rstrip(newline)
  78. line += ''.join(
  79. (BRIGHT, WHITE, ' <(%s)' % (', '.join(base_for_branches)),
  80. RESET, newline))
  81. for b in base_for_branches:
  82. del merge_base_map[b]
  83. start = line.find(GREEN+' (')
  84. end = line.find(')', start)
  85. if start != -1 and end != -1:
  86. start += len(GREEN) + 2
  87. branch_list = line[start:end].split(', ')
  88. branches_str = ''
  89. if branch_list:
  90. colored_branches = []
  91. head_marker = ''
  92. for b in branch_list:
  93. if b == "HEAD":
  94. head_marker = BLUEBAK+BRIGHT+'*'
  95. continue
  96. if b == current:
  97. colored_branches.append(CYAN+BRIGHT+b+RESET)
  98. current = None
  99. elif b in all_branches:
  100. colored_branches.append(GREEN+BRIGHT+b+RESET)
  101. all_branches.remove(b)
  102. elif b in all_tags:
  103. colored_branches.append(MAGENTA+BRIGHT+b+RESET)
  104. elif b.startswith('tag: '):
  105. colored_branches.append(MAGENTA+BRIGHT+b[5:]+RESET)
  106. else:
  107. colored_branches.append(RED+b)
  108. branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN)
  109. line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:])
  110. if head_marker:
  111. line = line.replace('*', head_marker, 1)
  112. sys.stdout.write(line)
  113. except (IOError, KeyboardInterrupt):
  114. pass
  115. finally:
  116. sys.stderr.close()
  117. sys.stdout.close()
  118. return 0
  119. if __name__ == '__main__':
  120. try:
  121. sys.exit(main(sys.argv[1:]))
  122. except KeyboardInterrupt:
  123. sys.stderr.write('interrupted\n')
  124. sys.exit(1)