git_map.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python3
  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. usage: git map [-h] [--help] [<args>]
  7. Enhances `git log --graph` view with information on commit branches + tags that
  8. point to them. Items are colorized as follows:
  9. * Cyan - Currently checked out branch
  10. * Green - Local branch
  11. * Red - Remote branches
  12. * Magenta - Tags
  13. * White - Merge Base Markers
  14. * Blue background - The currently checked out commit
  15. """
  16. import os
  17. import sys
  18. import git_common
  19. import setup_color
  20. import subprocess2
  21. from third_party import colorama
  22. RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL
  23. BRIGHT = colorama.Style.BRIGHT
  24. BLUE_BACK = colorama.Back.BLUE + BRIGHT
  25. BRIGHT_RED = colorama.Fore.RED + BRIGHT
  26. CYAN = colorama.Fore.CYAN + BRIGHT
  27. GREEN = colorama.Fore.GREEN + BRIGHT
  28. MAGENTA = colorama.Fore.MAGENTA + BRIGHT
  29. RED = colorama.Fore.RED
  30. WHITE = colorama.Fore.WHITE + BRIGHT
  31. YELLOW = colorama.Fore.YELLOW
  32. def _print_help(outbuf):
  33. names = {
  34. 'Cyan': CYAN,
  35. 'Green': GREEN,
  36. 'Magenta': MAGENTA,
  37. 'Red': RED,
  38. 'White': WHITE,
  39. 'Blue background': BLUE_BACK,
  40. }
  41. msg = ''
  42. for line in __doc__.splitlines():
  43. for name, color in names.items():
  44. if name in line:
  45. msg += line.replace('* ' + name,
  46. color + '* ' + name + RESET) + '\n'
  47. break
  48. else:
  49. msg += line + '\n'
  50. outbuf.write(msg.encode('utf-8', 'replace'))
  51. def _color_branch(branch, all_branches, all_tags, current):
  52. if branch in (current, 'HEAD -> ' + current):
  53. color = CYAN
  54. current = None
  55. elif branch in all_branches:
  56. color = GREEN
  57. all_branches.remove(branch)
  58. elif branch in all_tags:
  59. color = MAGENTA
  60. elif branch.startswith('tag: '):
  61. color = MAGENTA
  62. branch = branch[len('tag: '):]
  63. else:
  64. color = RED
  65. return color + branch + RESET
  66. def _color_branch_list(branch_list, all_branches, all_tags, current):
  67. if not branch_list:
  68. return ''
  69. colored_branches = (GREEN + ', ').join(
  70. _color_branch(branch, all_branches, all_tags, current)
  71. for branch in branch_list if branch != 'HEAD')
  72. return (GREEN + '(' + colored_branches + GREEN + ') ' + RESET)
  73. def _parse_log_line(line):
  74. graph, branch_list, commit_date, subject = (line.decode(
  75. 'utf-8', 'replace').strip().split('\x00'))
  76. branch_list = [] if not branch_list else branch_list.split(', ')
  77. commit = graph.split()[-1]
  78. graph = graph[:-len(commit)]
  79. return graph, commit, branch_list, commit_date, subject
  80. def main(argv, outbuf):
  81. if '-h' in argv or '--help' in argv:
  82. _print_help(outbuf)
  83. return 0
  84. map_extra = git_common.get_config_list('depot_tools.map_extra')
  85. cmd = [
  86. git_common.GIT_EXE, 'log',
  87. git_common.root(), '--graph', '--branches', '--tags', '--color=always',
  88. '--date=short', '--pretty=format:%H%x00%D%x00%cd%x00%s'
  89. ] + map_extra + argv
  90. log_proc = subprocess2.Popen(cmd, stdout=subprocess2.PIPE, shell=False)
  91. current = git_common.current_branch()
  92. all_tags = set(git_common.tags())
  93. all_branches = set(git_common.branches())
  94. if current in all_branches:
  95. all_branches.remove(current)
  96. merge_base_map = {}
  97. for branch in all_branches:
  98. merge_base = git_common.get_or_create_merge_base(branch)
  99. if merge_base:
  100. merge_base_map.setdefault(merge_base, set()).add(branch)
  101. for merge_base, branches in merge_base_map.items():
  102. merge_base_map[merge_base] = ', '.join(branches)
  103. try:
  104. for line in log_proc.stdout:
  105. if b'\x00' not in line:
  106. outbuf.write(line)
  107. continue
  108. graph, commit, branch_list, commit_date, subject = _parse_log_line(
  109. line)
  110. if 'HEAD' in branch_list:
  111. graph = graph.replace('*', BLUE_BACK + '*')
  112. line = '{graph}{commit}\t{branches}{date} ~ {subject}'.format(
  113. graph=graph,
  114. commit=BRIGHT_RED + commit[:10] + RESET,
  115. branches=_color_branch_list(branch_list, all_branches, all_tags,
  116. current),
  117. date=YELLOW + commit_date + RESET,
  118. subject=subject)
  119. if commit in merge_base_map:
  120. line += ' <({})'.format(WHITE + merge_base_map[commit] +
  121. RESET)
  122. line += os.linesep
  123. outbuf.write(line.encode('utf-8', 'replace'))
  124. except (BrokenPipeError, KeyboardInterrupt):
  125. pass
  126. return 0
  127. if __name__ == '__main__':
  128. setup_color.init()
  129. with git_common.less() as less_input:
  130. sys.exit(main(sys.argv[1:], less_input))