git_map.py 5.0 KB

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