git_map.py 4.6 KB

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