series.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2011 The Chromium OS Authors.
  3. #
  4. from __future__ import print_function
  5. import collections
  6. import itertools
  7. import os
  8. from patman import get_maintainer
  9. from patman import gitutil
  10. from patman import settings
  11. from patman import terminal
  12. from patman import tools
  13. # Series-xxx tags that we understand
  14. valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
  15. 'cover_cc', 'process_log', 'links', 'patchwork_url']
  16. class Series(dict):
  17. """Holds information about a patch series, including all tags.
  18. Vars:
  19. cc: List of aliases/emails to Cc all patches to
  20. commits: List of Commit objects, one for each patch
  21. cover: List of lines in the cover letter
  22. notes: List of lines in the notes
  23. changes: (dict) List of changes for each version, The key is
  24. the integer version number
  25. allow_overwrite: Allow tags to overwrite an existing tag
  26. """
  27. def __init__(self):
  28. self.cc = []
  29. self.to = []
  30. self.cover_cc = []
  31. self.commits = []
  32. self.cover = None
  33. self.notes = []
  34. self.changes = {}
  35. self.allow_overwrite = False
  36. # Written in MakeCcFile()
  37. # key: name of patch file
  38. # value: list of email addresses
  39. self._generated_cc = {}
  40. # These make us more like a dictionary
  41. def __setattr__(self, name, value):
  42. self[name] = value
  43. def __getattr__(self, name):
  44. return self[name]
  45. def AddTag(self, commit, line, name, value):
  46. """Add a new Series-xxx tag along with its value.
  47. Args:
  48. line: Source line containing tag (useful for debug/error messages)
  49. name: Tag name (part after 'Series-')
  50. value: Tag value (part after 'Series-xxx: ')
  51. Returns:
  52. String warning if something went wrong, else None
  53. """
  54. # If we already have it, then add to our list
  55. name = name.replace('-', '_')
  56. if name in self and not self.allow_overwrite:
  57. values = value.split(',')
  58. values = [str.strip() for str in values]
  59. if type(self[name]) != type([]):
  60. raise ValueError("In %s: line '%s': Cannot add another value "
  61. "'%s' to series '%s'" %
  62. (commit.hash, line, values, self[name]))
  63. self[name] += values
  64. # Otherwise just set the value
  65. elif name in valid_series:
  66. if name=="notes":
  67. self[name] = [value]
  68. else:
  69. self[name] = value
  70. else:
  71. return ("In %s: line '%s': Unknown 'Series-%s': valid "
  72. "options are %s" % (commit.hash, line, name,
  73. ', '.join(valid_series)))
  74. return None
  75. def AddCommit(self, commit):
  76. """Add a commit into our list of commits
  77. We create a list of tags in the commit subject also.
  78. Args:
  79. commit: Commit object to add
  80. """
  81. commit.CheckTags()
  82. self.commits.append(commit)
  83. def ShowActions(self, args, cmd, process_tags):
  84. """Show what actions we will/would perform
  85. Args:
  86. args: List of patch files we created
  87. cmd: The git command we would have run
  88. process_tags: Process tags as if they were aliases
  89. """
  90. to_set = set(gitutil.BuildEmailList(self.to));
  91. cc_set = set(gitutil.BuildEmailList(self.cc));
  92. col = terminal.Color()
  93. print('Dry run, so not doing much. But I would do this:')
  94. print()
  95. print('Send a total of %d patch%s with %scover letter.' % (
  96. len(args), '' if len(args) == 1 else 'es',
  97. self.get('cover') and 'a ' or 'no '))
  98. # TODO: Colour the patches according to whether they passed checks
  99. for upto in range(len(args)):
  100. commit = self.commits[upto]
  101. print(col.Color(col.GREEN, ' %s' % args[upto]))
  102. cc_list = list(self._generated_cc[commit.patch])
  103. for email in sorted(set(cc_list) - to_set - cc_set):
  104. if email == None:
  105. email = col.Color(col.YELLOW, "<alias '%s' not found>"
  106. % tag)
  107. if email:
  108. print(' Cc: ', email)
  109. print
  110. for item in sorted(to_set):
  111. print('To:\t ', item)
  112. for item in sorted(cc_set - to_set):
  113. print('Cc:\t ', item)
  114. print('Version: ', self.get('version'))
  115. print('Prefix:\t ', self.get('prefix'))
  116. if self.cover:
  117. print('Cover: %d lines' % len(self.cover))
  118. cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
  119. all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
  120. for email in sorted(set(all_ccs) - to_set - cc_set):
  121. print(' Cc: ', email)
  122. if cmd:
  123. print('Git command: %s' % cmd)
  124. def MakeChangeLog(self, commit):
  125. """Create a list of changes for each version.
  126. Return:
  127. The change log as a list of strings, one per line
  128. Changes in v4:
  129. - Jog the dial back closer to the widget
  130. Changes in v2:
  131. - Fix the widget
  132. - Jog the dial
  133. If there are no new changes in a patch, a note will be added
  134. (no changes since v2)
  135. Changes in v2:
  136. - Fix the widget
  137. - Jog the dial
  138. """
  139. # Collect changes from the series and this commit
  140. changes = collections.defaultdict(list)
  141. for version, changelist in self.changes.items():
  142. changes[version] += changelist
  143. if commit:
  144. for version, changelist in commit.changes.items():
  145. changes[version] += [[commit, text] for text in changelist]
  146. versions = sorted(changes, reverse=True)
  147. newest_version = 1
  148. if 'version' in self:
  149. newest_version = max(newest_version, int(self.version))
  150. if versions:
  151. newest_version = max(newest_version, versions[0])
  152. final = []
  153. process_it = self.get('process_log', '').split(',')
  154. process_it = [item.strip() for item in process_it]
  155. need_blank = False
  156. for version in versions:
  157. out = []
  158. for this_commit, text in changes[version]:
  159. if commit and this_commit != commit:
  160. continue
  161. if 'uniq' not in process_it or text not in out:
  162. out.append(text)
  163. if 'sort' in process_it:
  164. out = sorted(out)
  165. have_changes = len(out) > 0
  166. line = 'Changes in v%d:' % version
  167. if have_changes:
  168. out.insert(0, line)
  169. if version < newest_version and len(final) == 0:
  170. out.insert(0, '')
  171. out.insert(0, '(no changes since v%d)' % version)
  172. newest_version = 0
  173. # Only add a new line if we output something
  174. if need_blank:
  175. out.insert(0, '')
  176. need_blank = False
  177. final += out
  178. need_blank = need_blank or have_changes
  179. if len(final) > 0:
  180. final.append('')
  181. elif newest_version != 1:
  182. final = ['(no changes since v1)', '']
  183. return final
  184. def DoChecks(self):
  185. """Check that each version has a change log
  186. Print an error if something is wrong.
  187. """
  188. col = terminal.Color()
  189. if self.get('version'):
  190. changes_copy = dict(self.changes)
  191. for version in range(1, int(self.version) + 1):
  192. if self.changes.get(version):
  193. del changes_copy[version]
  194. else:
  195. if version > 1:
  196. str = 'Change log missing for v%d' % version
  197. print(col.Color(col.RED, str))
  198. for version in changes_copy:
  199. str = 'Change log for unknown version v%d' % version
  200. print(col.Color(col.RED, str))
  201. elif self.changes:
  202. str = 'Change log exists, but no version is set'
  203. print(col.Color(col.RED, str))
  204. def MakeCcFile(self, process_tags, cover_fname, warn_on_error,
  205. add_maintainers, limit):
  206. """Make a cc file for us to use for per-commit Cc automation
  207. Also stores in self._generated_cc to make ShowActions() faster.
  208. Args:
  209. process_tags: Process tags as if they were aliases
  210. cover_fname: If non-None the name of the cover letter.
  211. warn_on_error: True to print a warning when an alias fails to match,
  212. False to ignore it.
  213. add_maintainers: Either:
  214. True/False to call the get_maintainers to CC maintainers
  215. List of maintainers to include (for testing)
  216. limit: Limit the length of the Cc list (None if no limit)
  217. Return:
  218. Filename of temp file created
  219. """
  220. col = terminal.Color()
  221. # Look for commit tags (of the form 'xxx:' at the start of the subject)
  222. fname = '/tmp/patman.%d' % os.getpid()
  223. fd = open(fname, 'w', encoding='utf-8')
  224. all_ccs = []
  225. for commit in self.commits:
  226. cc = []
  227. if process_tags:
  228. cc += gitutil.BuildEmailList(commit.tags,
  229. warn_on_error=warn_on_error)
  230. cc += gitutil.BuildEmailList(commit.cc_list,
  231. warn_on_error=warn_on_error)
  232. if type(add_maintainers) == type(cc):
  233. cc += add_maintainers
  234. elif add_maintainers:
  235. dir_list = [os.path.join(gitutil.GetTopLevel(), 'scripts')]
  236. cc += get_maintainer.GetMaintainer(dir_list, commit.patch)
  237. for x in set(cc) & set(settings.bounces):
  238. print(col.Color(col.YELLOW, 'Skipping "%s"' % x))
  239. cc = list(set(cc) - set(settings.bounces))
  240. if limit is not None:
  241. cc = cc[:limit]
  242. all_ccs += cc
  243. print(commit.patch, '\0'.join(sorted(set(cc))), file=fd)
  244. self._generated_cc[commit.patch] = cc
  245. if cover_fname:
  246. cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
  247. cover_cc = list(set(cover_cc + all_ccs))
  248. if limit is not None:
  249. cover_cc = cover_cc[:limit]
  250. cc_list = '\0'.join([x for x in sorted(cover_cc)])
  251. print(cover_fname, cc_list, file=fd)
  252. fd.close()
  253. return fname
  254. def AddChange(self, version, commit, info):
  255. """Add a new change line to a version.
  256. This will later appear in the change log.
  257. Args:
  258. version: version number to add change list to
  259. info: change line for this version
  260. """
  261. if not self.changes.get(version):
  262. self.changes[version] = []
  263. self.changes[version].append([commit, info])
  264. def GetPatchPrefix(self):
  265. """Get the patch version string
  266. Return:
  267. Patch string, like 'RFC PATCH v5' or just 'PATCH'
  268. """
  269. git_prefix = gitutil.GetDefaultSubjectPrefix()
  270. if git_prefix:
  271. git_prefix = '%s][' % git_prefix
  272. else:
  273. git_prefix = ''
  274. version = ''
  275. if self.get('version'):
  276. version = ' v%s' % self['version']
  277. # Get patch name prefix
  278. prefix = ''
  279. if self.get('prefix'):
  280. prefix = '%s ' % self['prefix']
  281. return '%s%sPATCH%s' % (git_prefix, prefix, version)