mailmapper 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python2
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright (C) 2014, Masahiro Yamada <yamada.m@jp.panasonic.com>
  5. '''
  6. A tool to create/update the mailmap file
  7. The command 'git shortlog' summarizes git log output in a format suitable
  8. for inclusion in release announcements. Each commit will be grouped by
  9. author and title.
  10. One problem is that the authors' name and/or email address is sometimes
  11. spelled differently. The .mailmap feature can be used to coalesce together
  12. commits by the same persion.
  13. (See 'man git-shortlog' for furthur information of this feature.)
  14. This tool helps to create/update the mailmap file.
  15. It runs 'git shortlog' internally and searches differently spelled author
  16. names which share the same email address. The author name with the most
  17. commits is asuumed to be a canonical real name. If the number of commits
  18. from the cananonical name is equal to or greater than 'MIN_COMMITS',
  19. the entry for the cananical name will be output. ('MIN_COMMITS' is used
  20. here because we do not want to create a fat mailmap by adding every author
  21. with only a few commits.)
  22. If there exists a mailmap file specified by the mailmap.file configuration
  23. options or '.mailmap' at the toplevel of the repository, it is used as
  24. a base file. (The mailmap.file configuration takes precedence over the
  25. '.mailmap' file if both exist.)
  26. The base file and the newly added entries are merged together and sorted
  27. alphabetically (but the comment block is kept untouched), and then printed
  28. to standard output.
  29. Usage
  30. -----
  31. scripts/mailmapper
  32. prints the mailmapping to standard output.
  33. scripts/mailmapper > tmp; mv tmp .mailmap
  34. will be useful for updating '.mailmap' file.
  35. '''
  36. import sys
  37. import os
  38. import subprocess
  39. # The entries only for the canonical names with MIN_COMMITS or more commits.
  40. # This limitation is used so as not to create a too big mailmap file.
  41. MIN_COMMITS = 50
  42. try:
  43. toplevel = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'])
  44. except subprocess.CalledProcessError:
  45. sys.exit('Please run in a git repository.')
  46. # strip '\n'
  47. toplevel = toplevel.rstrip()
  48. # Change the current working directory to the toplevel of the respository
  49. # for our easier life.
  50. os.chdir(toplevel)
  51. # First, create 'auther name' vs 'number of commits' database.
  52. # We assume the name with the most commits as the canonical real name.
  53. shortlog = subprocess.check_output(['git', 'shortlog', '-s', '-n'])
  54. commits_per_name = {}
  55. for line in shortlog.splitlines():
  56. try:
  57. commits, name = line.split(None, 1)
  58. except ValueError:
  59. # ignore lines with an empty author name
  60. pass
  61. commits_per_name[name] = int(commits)
  62. # Next, coalesce the auther names with the same email address
  63. shortlog = subprocess.check_output(['git', 'shortlog', '-s', '-n', '-e'])
  64. mail_vs_name = {}
  65. output = {}
  66. for line in shortlog.splitlines():
  67. # tmp, mail = line.rsplit(None, 1) is not safe
  68. # because weird email addresses might include whitespaces
  69. tmp, mail = line.split('<')
  70. mail = '<' + mail.rstrip()
  71. try:
  72. _, name = tmp.rstrip().split(None, 1)
  73. except ValueError:
  74. # author name is empty
  75. name = ''
  76. if mail in mail_vs_name:
  77. # another name for the same email address
  78. prev_name = mail_vs_name[mail]
  79. # Take the name with more commits
  80. major_name = sorted([prev_name, name],
  81. key=lambda x: commits_per_name[x] if x else 0)[1]
  82. mail_vs_name[mail] = major_name
  83. if commits_per_name[major_name] > MIN_COMMITS:
  84. output[mail] = major_name
  85. else:
  86. mail_vs_name[mail] = name
  87. # [1] If there exists a mailmap file at the location pointed to
  88. # by the mailmap.file configuration option, update it.
  89. # [2] If the file .mailmap exists at the toplevel of the repository, update it.
  90. # [3] Otherwise, create a new mailmap file.
  91. mailmap_files = []
  92. try:
  93. config_mailmap = subprocess.check_output(['git', 'config', 'mailmap.file'])
  94. except subprocess.CalledProcessError:
  95. config_mailmap = ''
  96. config_mailmap = config_mailmap.rstrip()
  97. if config_mailmap:
  98. mailmap_files.append(config_mailmap)
  99. mailmap_files.append('.mailmap')
  100. infile = None
  101. for map_file in mailmap_files:
  102. try:
  103. infile = open(map_file)
  104. except:
  105. # Failed to open. Try next.
  106. continue
  107. break
  108. comment_block = []
  109. output_lines = []
  110. if infile:
  111. for line in infile:
  112. if line[0] == '#' or line[0] == '\n':
  113. comment_block.append(line)
  114. else:
  115. output_lines.append(line)
  116. break
  117. for line in infile:
  118. output_lines.append(line)
  119. infile.close()
  120. for mail, name in output.items():
  121. output_lines.append(name + ' ' + mail + '\n')
  122. output_lines.sort()
  123. sys.stdout.write(''.join(comment_block + output_lines))