commit.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2011 The Chromium OS Authors.
  3. #
  4. import collections
  5. import re
  6. # Separates a tag: at the beginning of the subject from the rest of it
  7. re_subject_tag = re.compile('([^:\s]*):\s*(.*)')
  8. class Commit:
  9. """Holds information about a single commit/patch in the series.
  10. Args:
  11. hash: Commit hash (as a string)
  12. Variables:
  13. hash: Commit hash
  14. subject: Subject line
  15. tags: List of maintainer tag strings
  16. changes: Dict containing a list of changes (single line strings).
  17. The dict is indexed by change version (an integer)
  18. cc_list: List of people to aliases/emails to cc on this commit
  19. notes: List of lines in the commit (not series) notes
  20. change_id: the Change-Id: tag that was stripped from this commit
  21. and can be used to generate the Message-Id.
  22. rtags: Response tags (e.g. Reviewed-by) collected by the commit, dict:
  23. key: rtag type (e.g. 'Reviewed-by')
  24. value: Set of people who gave that rtag, each a name/email string
  25. warn: List of warnings for this commit, each a str
  26. """
  27. def __init__(self, hash):
  28. self.hash = hash
  29. self.subject = None
  30. self.tags = []
  31. self.changes = {}
  32. self.cc_list = []
  33. self.signoff_set = set()
  34. self.notes = []
  35. self.change_id = None
  36. self.rtags = collections.defaultdict(set)
  37. self.warn = []
  38. def __str__(self):
  39. return self.subject
  40. def AddChange(self, version, info):
  41. """Add a new change line to the change list for a version.
  42. Args:
  43. version: Patch set version (integer: 1, 2, 3)
  44. info: Description of change in this version
  45. """
  46. if not self.changes.get(version):
  47. self.changes[version] = []
  48. self.changes[version].append(info)
  49. def CheckTags(self):
  50. """Create a list of subject tags in the commit
  51. Subject tags look like this:
  52. propounder: fort: Change the widget to propound correctly
  53. Here the tags are propounder and fort. Multiple tags are supported.
  54. The list is updated in self.tag.
  55. Returns:
  56. None if ok, else the name of a tag with no email alias
  57. """
  58. str = self.subject
  59. m = True
  60. while m:
  61. m = re_subject_tag.match(str)
  62. if m:
  63. tag = m.group(1)
  64. self.tags.append(tag)
  65. str = m.group(2)
  66. return None
  67. def AddCc(self, cc_list):
  68. """Add a list of people to Cc when we send this patch.
  69. Args:
  70. cc_list: List of aliases or email addresses
  71. """
  72. self.cc_list += cc_list
  73. def CheckDuplicateSignoff(self, signoff):
  74. """Check a list of signoffs we have send for this patch
  75. Args:
  76. signoff: Signoff line
  77. Returns:
  78. True if this signoff is new, False if we have already seen it.
  79. """
  80. if signoff in self.signoff_set:
  81. return False
  82. self.signoff_set.add(signoff)
  83. return True
  84. def AddRtag(self, rtag_type, who):
  85. """Add a response tag to a commit
  86. Args:
  87. key: rtag type (e.g. 'Reviewed-by')
  88. who: Person who gave that rtag, e.g. 'Fred Bloggs <fred@bloggs.org>'
  89. """
  90. self.rtags[rtag_type].add(who)