git_map.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, color + '* ' + name + RESET) + '\n'
  46. break
  47. else:
  48. msg += line + '\n'
  49. outbuf.write(msg.encode('utf-8', 'replace'))
  50. def _color_branch(branch, all_branches, all_tags, current):
  51. if branch in (current, 'HEAD -> ' + current):
  52. color = CYAN
  53. current = None
  54. elif branch in all_branches:
  55. color = GREEN
  56. all_branches.remove(branch)
  57. elif branch in all_tags:
  58. color = MAGENTA
  59. elif branch.startswith('tag: '):
  60. color = MAGENTA
  61. branch = branch[len('tag: '):]
  62. else:
  63. color = RED
  64. return color + branch + RESET
  65. def _color_branch_list(branch_list, all_branches, all_tags, current):
  66. if not branch_list:
  67. return ''
  68. colored_branches = (GREEN + ', ').join(
  69. _color_branch(branch, all_branches, all_tags, current)
  70. for branch in branch_list if branch != 'HEAD')
  71. return (GREEN + '(' + colored_branches + GREEN + ') ' + RESET)
  72. def _parse_log_line(line):
  73. graph, branch_list, commit_date, subject = (
  74. line.decode('utf-8', 'replace').strip().split('\x00'))
  75. branch_list = [] if not branch_list else branch_list.split(', ')
  76. commit = graph.split()[-1]
  77. graph = graph[:-len(commit)]
  78. return graph, commit, branch_list, commit_date, subject
  79. def main(argv, outbuf):
  80. if '-h' in argv or '--help' in argv:
  81. _print_help(outbuf)
  82. return 0
  83. map_extra = git_common.get_config_list('depot_tools.map_extra')
  84. cmd = [
  85. git_common.GIT_EXE, 'log', git_common.root(),
  86. '--graph', '--branches', '--tags', '--color=always', '--date=short',
  87. '--pretty=format:%H%x00%D%x00%cd%x00%s'
  88. ] + map_extra + argv
  89. log_proc = subprocess2.Popen(cmd, stdout=subprocess2.PIPE, shell=False)
  90. current = git_common.current_branch()
  91. all_tags = set(git_common.tags())
  92. all_branches = set(git_common.branches())
  93. if current in all_branches:
  94. all_branches.remove(current)
  95. merge_base_map = {}
  96. for branch in all_branches:
  97. merge_base = git_common.get_or_create_merge_base(branch)
  98. if merge_base:
  99. merge_base_map.setdefault(merge_base, set()).add(branch)
  100. for merge_base, branches in merge_base_map.items():
  101. merge_base_map[merge_base] = ', '.join(branches)
  102. try:
  103. for line in log_proc.stdout:
  104. if b'\x00' not in line:
  105. outbuf.write(line)
  106. continue
  107. graph, commit, branch_list, commit_date, subject = _parse_log_line(line)
  108. if 'HEAD' in branch_list:
  109. graph = graph.replace('*', BLUE_BACK + '*')
  110. line = '{graph}{commit}\t{branches}{date} ~ {subject}'.format(
  111. graph=graph,
  112. commit=BRIGHT_RED + commit[:10] + RESET,
  113. branches=_color_branch_list(
  114. branch_list, all_branches, all_tags, current),
  115. date=YELLOW + commit_date + RESET,
  116. subject=subject)
  117. if commit in merge_base_map:
  118. line += ' <({})'.format(WHITE + merge_base_map[commit] + RESET)
  119. line += os.linesep
  120. outbuf.write(line.encode('utf-8', 'replace'))
  121. except (BrokenPipeError, KeyboardInterrupt):
  122. pass
  123. return 0
  124. if __name__ == '__main__':
  125. setup_color.init()
  126. with git_common.less() as less_input:
  127. sys.exit(main(sys.argv[1:], less_input))