patchstream.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # Copyright (c) 2011 The Chromium OS Authors.
  2. #
  3. # See file CREDITS for list of people who contributed to this
  4. # project.
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. # MA 02111-1307 USA
  20. #
  21. import os
  22. import re
  23. import shutil
  24. import tempfile
  25. import command
  26. import commit
  27. import gitutil
  28. from series import Series
  29. # Tags that we detect and remove
  30. re_remove = re.compile('^BUG=|^TEST=|^Change-Id:|^Review URL:'
  31. '|Reviewed-on:|Reviewed-by:')
  32. # Lines which are allowed after a TEST= line
  33. re_allowed_after_test = re.compile('^Signed-off-by:')
  34. # The start of the cover letter
  35. re_cover = re.compile('^Cover-letter:')
  36. # Patch series tag
  37. re_series = re.compile('^Series-(\w*): *(.*)')
  38. # Commit tags that we want to collect and keep
  39. re_tag = re.compile('^(Tested-by|Acked-by|Signed-off-by|Cc): (.*)')
  40. # The start of a new commit in the git log
  41. re_commit = re.compile('^commit (.*)')
  42. # We detect these since checkpatch doesn't always do it
  43. re_space_before_tab = re.compile('^[+].* \t')
  44. # States we can be in - can we use range() and still have comments?
  45. STATE_MSG_HEADER = 0 # Still in the message header
  46. STATE_PATCH_SUBJECT = 1 # In patch subject (first line of log for a commit)
  47. STATE_PATCH_HEADER = 2 # In patch header (after the subject)
  48. STATE_DIFFS = 3 # In the diff part (past --- line)
  49. class PatchStream:
  50. """Class for detecting/injecting tags in a patch or series of patches
  51. We support processing the output of 'git log' to read out the tags we
  52. are interested in. We can also process a patch file in order to remove
  53. unwanted tags or inject additional ones. These correspond to the two
  54. phases of processing.
  55. """
  56. def __init__(self, series, name=None, is_log=False):
  57. self.skip_blank = False # True to skip a single blank line
  58. self.found_test = False # Found a TEST= line
  59. self.lines_after_test = 0 # MNumber of lines found after TEST=
  60. self.warn = [] # List of warnings we have collected
  61. self.linenum = 1 # Output line number we are up to
  62. self.in_section = None # Name of start...END section we are in
  63. self.notes = [] # Series notes
  64. self.section = [] # The current section...END section
  65. self.series = series # Info about the patch series
  66. self.is_log = is_log # True if indent like git log
  67. self.in_change = 0 # Non-zero if we are in a change list
  68. self.blank_count = 0 # Number of blank lines stored up
  69. self.state = STATE_MSG_HEADER # What state are we in?
  70. self.tags = [] # Tags collected, like Tested-by...
  71. self.signoff = [] # Contents of signoff line
  72. self.commit = None # Current commit
  73. def AddToSeries(self, line, name, value):
  74. """Add a new Series-xxx tag.
  75. When a Series-xxx tag is detected, we come here to record it, if we
  76. are scanning a 'git log'.
  77. Args:
  78. line: Source line containing tag (useful for debug/error messages)
  79. name: Tag name (part after 'Series-')
  80. value: Tag value (part after 'Series-xxx: ')
  81. """
  82. if name == 'notes':
  83. self.in_section = name
  84. self.skip_blank = False
  85. if self.is_log:
  86. self.series.AddTag(self.commit, line, name, value)
  87. def CloseCommit(self):
  88. """Save the current commit into our commit list, and reset our state"""
  89. if self.commit and self.is_log:
  90. self.series.AddCommit(self.commit)
  91. self.commit = None
  92. def FormatTags(self, tags):
  93. out_list = []
  94. for tag in sorted(tags):
  95. if tag.startswith('Cc:'):
  96. tag_list = tag[4:].split(',')
  97. out_list += gitutil.BuildEmailList(tag_list, 'Cc:')
  98. else:
  99. out_list.append(tag)
  100. return out_list
  101. def ProcessLine(self, line):
  102. """Process a single line of a patch file or commit log
  103. This process a line and returns a list of lines to output. The list
  104. may be empty or may contain multiple output lines.
  105. This is where all the complicated logic is located. The class's
  106. state is used to move between different states and detect things
  107. properly.
  108. We can be in one of two modes:
  109. self.is_log == True: This is 'git log' mode, where most output is
  110. indented by 4 characters and we are scanning for tags
  111. self.is_log == False: This is 'patch' mode, where we already have
  112. all the tags, and are processing patches to remove junk we
  113. don't want, and add things we think are required.
  114. Args:
  115. line: text line to process
  116. Returns:
  117. list of output lines, or [] if nothing should be output
  118. """
  119. # Initially we have no output. Prepare the input line string
  120. out = []
  121. line = line.rstrip('\n')
  122. if self.is_log:
  123. if line[:4] == ' ':
  124. line = line[4:]
  125. # Handle state transition and skipping blank lines
  126. series_match = re_series.match(line)
  127. commit_match = re_commit.match(line) if self.is_log else None
  128. tag_match = None
  129. if self.state == STATE_PATCH_HEADER:
  130. tag_match = re_tag.match(line)
  131. is_blank = not line.strip()
  132. if is_blank:
  133. if (self.state == STATE_MSG_HEADER
  134. or self.state == STATE_PATCH_SUBJECT):
  135. self.state += 1
  136. # We don't have a subject in the text stream of patch files
  137. # It has its own line with a Subject: tag
  138. if not self.is_log and self.state == STATE_PATCH_SUBJECT:
  139. self.state += 1
  140. elif commit_match:
  141. self.state = STATE_MSG_HEADER
  142. # If we are in a section, keep collecting lines until we see END
  143. if self.in_section:
  144. if line == 'END':
  145. if self.in_section == 'cover':
  146. self.series.cover = self.section
  147. elif self.in_section == 'notes':
  148. if self.is_log:
  149. self.series.notes += self.section
  150. else:
  151. self.warn.append("Unknown section '%s'" % self.in_section)
  152. self.in_section = None
  153. self.skip_blank = True
  154. self.section = []
  155. else:
  156. self.section.append(line)
  157. # Detect the commit subject
  158. elif not is_blank and self.state == STATE_PATCH_SUBJECT:
  159. self.commit.subject = line
  160. # Detect the tags we want to remove, and skip blank lines
  161. elif re_remove.match(line):
  162. self.skip_blank = True
  163. # TEST= should be the last thing in the commit, so remove
  164. # everything after it
  165. if line.startswith('TEST='):
  166. self.found_test = True
  167. elif self.skip_blank and is_blank:
  168. self.skip_blank = False
  169. # Detect the start of a cover letter section
  170. elif re_cover.match(line):
  171. self.in_section = 'cover'
  172. self.skip_blank = False
  173. # If we are in a change list, key collected lines until a blank one
  174. elif self.in_change:
  175. if is_blank:
  176. # Blank line ends this change list
  177. self.in_change = 0
  178. else:
  179. self.series.AddChange(self.in_change, self.commit, line)
  180. self.skip_blank = False
  181. # Detect Series-xxx tags
  182. elif series_match:
  183. name = series_match.group(1)
  184. value = series_match.group(2)
  185. if name == 'changes':
  186. # value is the version number: e.g. 1, or 2
  187. try:
  188. value = int(value)
  189. except ValueError as str:
  190. raise ValueError("%s: Cannot decode version info '%s'" %
  191. (self.commit.hash, line))
  192. self.in_change = int(value)
  193. else:
  194. self.AddToSeries(line, name, value)
  195. self.skip_blank = True
  196. # Detect the start of a new commit
  197. elif commit_match:
  198. self.CloseCommit()
  199. self.commit = commit.Commit(commit_match.group(1)[:7])
  200. # Detect tags in the commit message
  201. elif tag_match:
  202. # Onlly allow a single signoff tag
  203. if tag_match.group(1) == 'Signed-off-by':
  204. if self.signoff:
  205. self.warn.append('Patch has more than one Signed-off-by '
  206. 'tag')
  207. self.signoff += [line]
  208. # Remove Tested-by self, since few will take much notice
  209. elif (tag_match.group(1) == 'Tested-by' and
  210. tag_match.group(2).find(os.getenv('USER') + '@') != -1):
  211. self.warn.append("Ignoring %s" % line)
  212. elif tag_match.group(1) == 'Cc':
  213. self.commit.AddCc(tag_match.group(2).split(','))
  214. else:
  215. self.tags.append(line);
  216. # Well that means this is an ordinary line
  217. else:
  218. pos = 1
  219. # Look for ugly ASCII characters
  220. for ch in line:
  221. # TODO: Would be nicer to report source filename and line
  222. if ord(ch) > 0x80:
  223. self.warn.append("Line %d/%d ('%s') has funny ascii char" %
  224. (self.linenum, pos, line))
  225. pos += 1
  226. # Look for space before tab
  227. m = re_space_before_tab.match(line)
  228. if m:
  229. self.warn.append('Line %d/%d has space before tab' %
  230. (self.linenum, m.start()))
  231. # OK, we have a valid non-blank line
  232. out = [line]
  233. self.linenum += 1
  234. self.skip_blank = False
  235. if self.state == STATE_DIFFS:
  236. pass
  237. # If this is the start of the diffs section, emit our tags and
  238. # change log
  239. elif line == '---':
  240. self.state = STATE_DIFFS
  241. # Output the tags (signeoff first), then change list
  242. out = []
  243. if self.signoff:
  244. out += self.signoff
  245. log = self.series.MakeChangeLog(self.commit)
  246. out += self.FormatTags(self.tags)
  247. out += [line] + log
  248. elif self.found_test:
  249. if not re_allowed_after_test.match(line):
  250. self.lines_after_test += 1
  251. return out
  252. def Finalize(self):
  253. """Close out processing of this patch stream"""
  254. self.CloseCommit()
  255. if self.lines_after_test:
  256. self.warn.append('Found %d lines after TEST=' %
  257. self.lines_after_test)
  258. def ProcessStream(self, infd, outfd):
  259. """Copy a stream from infd to outfd, filtering out unwanting things.
  260. This is used to process patch files one at a time.
  261. Args:
  262. infd: Input stream file object
  263. outfd: Output stream file object
  264. """
  265. # Extract the filename from each diff, for nice warnings
  266. fname = None
  267. last_fname = None
  268. re_fname = re.compile('diff --git a/(.*) b/.*')
  269. while True:
  270. line = infd.readline()
  271. if not line:
  272. break
  273. out = self.ProcessLine(line)
  274. # Try to detect blank lines at EOF
  275. for line in out:
  276. match = re_fname.match(line)
  277. if match:
  278. last_fname = fname
  279. fname = match.group(1)
  280. if line == '+':
  281. self.blank_count += 1
  282. else:
  283. if self.blank_count and (line == '-- ' or match):
  284. self.warn.append("Found possible blank line(s) at "
  285. "end of file '%s'" % last_fname)
  286. outfd.write('+\n' * self.blank_count)
  287. outfd.write(line + '\n')
  288. self.blank_count = 0
  289. self.Finalize()
  290. def GetMetaData(start, count):
  291. """Reads out patch series metadata from the commits
  292. This does a 'git log' on the relevant commits and pulls out the tags we
  293. are interested in.
  294. Args:
  295. start: Commit to start from: 0=HEAD, 1=next one, etc.
  296. count: Number of commits to list
  297. """
  298. pipe = [['git', 'log', '--reverse', 'HEAD~%d' % start, '-n%d' % count]]
  299. stdout = command.RunPipe(pipe, capture=True)
  300. series = Series()
  301. ps = PatchStream(series, is_log=True)
  302. for line in stdout.splitlines():
  303. ps.ProcessLine(line)
  304. ps.Finalize()
  305. return series
  306. def FixPatch(backup_dir, fname, series, commit):
  307. """Fix up a patch file, by adding/removing as required.
  308. We remove our tags from the patch file, insert changes lists, etc.
  309. The patch file is processed in place, and overwritten.
  310. A backup file is put into backup_dir (if not None).
  311. Args:
  312. fname: Filename to patch file to process
  313. series: Series information about this patch set
  314. commit: Commit object for this patch file
  315. Return:
  316. A list of errors, or [] if all ok.
  317. """
  318. handle, tmpname = tempfile.mkstemp()
  319. outfd = os.fdopen(handle, 'w')
  320. infd = open(fname, 'r')
  321. ps = PatchStream(series)
  322. ps.commit = commit
  323. ps.ProcessStream(infd, outfd)
  324. infd.close()
  325. outfd.close()
  326. # Create a backup file if required
  327. if backup_dir:
  328. shutil.copy(fname, os.path.join(backup_dir, os.path.basename(fname)))
  329. shutil.move(tmpname, fname)
  330. return ps.warn
  331. def FixPatches(series, fnames):
  332. """Fix up a list of patches identified by filenames
  333. The patch files are processed in place, and overwritten.
  334. Args:
  335. series: The series object
  336. fnames: List of patch files to process
  337. """
  338. # Current workflow creates patches, so we shouldn't need a backup
  339. backup_dir = None #tempfile.mkdtemp('clean-patch')
  340. count = 0
  341. for fname in fnames:
  342. commit = series.commits[count]
  343. commit.patch = fname
  344. result = FixPatch(backup_dir, fname, series, commit)
  345. if result:
  346. print '%d warnings for %s:' % (len(result), fname)
  347. for warn in result:
  348. print '\t', warn
  349. print
  350. count += 1
  351. print 'Cleaned %d patches' % count
  352. return series
  353. def InsertCoverLetter(fname, series, count):
  354. """Inserts a cover letter with the required info into patch 0
  355. Args:
  356. fname: Input / output filename of the cover letter file
  357. series: Series object
  358. count: Number of patches in the series
  359. """
  360. fd = open(fname, 'r')
  361. lines = fd.readlines()
  362. fd.close()
  363. fd = open(fname, 'w')
  364. text = series.cover
  365. prefix = series.GetPatchPrefix()
  366. for line in lines:
  367. if line.startswith('Subject:'):
  368. # TODO: if more than 10 patches this should save 00/xx, not 0/xx
  369. line = 'Subject: [%s 0/%d] %s\n' % (prefix, count, text[0])
  370. # Insert our cover letter
  371. elif line.startswith('*** BLURB HERE ***'):
  372. # First the blurb test
  373. line = '\n'.join(text[1:]) + '\n'
  374. if series.get('notes'):
  375. line += '\n'.join(series.notes) + '\n'
  376. # Now the change list
  377. out = series.MakeChangeLog(None)
  378. line += '\n' + '\n'.join(out)
  379. fd.write(line)
  380. fd.close()