Martin Roth | 34e4a1c | 2016-08-03 22:50:46 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # This file is part of the coreboot project. |
| 4 | # |
| 5 | # Copyright (C) 2016 Google, Inc. |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; version 2 of the License. |
| 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 | import sys |
| 17 | import re |
| 18 | import subprocess |
| 19 | |
| 20 | # Regular expression patterns |
| 21 | pat_change_id = re.compile('^Change-Id: (.*)$') |
| 22 | pat_orig_lines = re.compile('^(Signed-off-by|Reviewed-on|Reviewed-by)') |
| 23 | pat_uninteresting = re.compile('^Tested-by') |
| 24 | pat_quotes = re.compile('"') |
| 25 | pat_leading_space = re.compile('^ ') |
| 26 | pat_bug_line = re.compile('^bug\s*=', re.IGNORECASE) |
| 27 | pat_branch_line = re.compile('branch\s*=', re.IGNORECASE) |
| 28 | pat_test_line = re.compile('test\s*=', re.IGNORECASE) |
| 29 | |
| 30 | def main(): |
| 31 | branch = "" |
| 32 | new_commit_message = "" |
| 33 | change_id = "" |
| 34 | commit_id = "" |
| 35 | bug_line = "BUG=None\n" |
| 36 | branch_line = "BRANCH=None\n" |
| 37 | test_line = "TEST=Build tested at coreboot.org\n" |
| 38 | |
| 39 | # Check command line arguments |
| 40 | if len(sys.argv) > 1: |
| 41 | if sys.argv[1] == "-h" or sys.argv[1] == "--help": |
| 42 | print "Update the commit message to submit to the Chrome OS tree." |
| 43 | print "Usage: " + sys.argv[1] + " [Remote branch]\n" |
| 44 | else: |
| 45 | branch = sys.argv[1] |
| 46 | else: |
| 47 | branch = "cborg/master" |
| 48 | |
| 49 | # Get the last commit message, then loop through looking at each line |
| 50 | commit_message = subprocess.check_output(["git", "log", "-n1"]).split("\n") |
| 51 | for line in commit_message: |
| 52 | |
| 53 | # Skip the initial few lines of the commit message |
| 54 | m = pat_leading_space.match(line) |
| 55 | if not m: |
| 56 | continue |
| 57 | |
| 58 | # Remove initial whitespace |
| 59 | line = line.lstrip(' ') |
| 60 | |
| 61 | # Add the 'UPSTREAM' comment to the subject line |
| 62 | if len(new_commit_message) == 0: |
| 63 | new_commit_message += "UPSTREAM: " + line + "\n" |
| 64 | continue |
| 65 | |
| 66 | # If we've found a TEST, BRANCH, or BUG line, mark it as found |
| 67 | if pat_test_line.match(line): |
| 68 | test_line = "" |
| 69 | if pat_bug_line.match(line): |
| 70 | bug_line = "" |
| 71 | if pat_branch_line.match(line): |
| 72 | branch_line = "" |
| 73 | |
| 74 | # Grab the Change-Id |
| 75 | chid = pat_change_id.match(line) |
| 76 | if chid: |
| 77 | change_id = chid.group(1) |
| 78 | |
| 79 | # Add 'Original-' to all of the coreboot.org gerrit messages |
| 80 | grrt = pat_orig_lines.match(line) |
| 81 | if grrt: |
| 82 | line = "Original-" + line |
| 83 | |
| 84 | # if we've reached the end of the real commit message text and we don't |
| 85 | # have the required TEST= BUG= and BRANCH= lines, add them. |
| 86 | if (chid or grrt) and (bug_line or branch_line or test_line): |
| 87 | new_commit_message += bug_line + branch_line + test_line + "\n" |
| 88 | bug_line = branch_line = test_line = "" |
| 89 | |
| 90 | # Remove uninteresting gerrit messages |
| 91 | if pat_uninteresting.match(line): |
| 92 | continue |
| 93 | |
| 94 | # Add the current line to the updated commit message |
| 95 | new_commit_message += line + "\n" |
| 96 | |
| 97 | # Error out if no Change-Id was located |
| 98 | if not change_id: |
| 99 | print "Error: No Change-Id found" |
| 100 | sys.exit(1) |
| 101 | |
| 102 | # Get the Commit ID based on the Change-Id and the branch. |
| 103 | # Error out if git returns an error |
| 104 | try: |
| 105 | commit_id = subprocess.check_output(["git", "log", "-n1", "--grep", change_id, '--pretty=%H', branch, "--"]).rstrip('\n') |
| 106 | except: |
| 107 | print "Error: invalid branch - " + branch + ".\n" |
| 108 | sys.exit(1) |
| 109 | |
| 110 | # To find the Commit-Id, we've looked through the git log for a particular Change-Id |
| 111 | # Error out if the Commit-Id wasn't found, with the message that we couldn't find the Change-Id |
| 112 | if not commit_id: |
| 113 | print "Error: Could not find Change-Id: " + change_id + " in branch " + branch + ".\n" |
| 114 | sys.exit(1) |
| 115 | |
| 116 | # Add the Commit-Id that this change came from to the new commit message. |
| 117 | new_commit_message += "(cherry-picked from commit " + commit_id + ")\n" |
| 118 | |
| 119 | # Update the commit message |
| 120 | amend_output = subprocess.check_output(["git", "commit", "-s", "--amend", "-m", new_commit_message ]) |
| 121 | |
| 122 | print "----------\n" |
| 123 | print amend_output |
| 124 | |
| 125 | if __name__ == "__main__": |
| 126 | main() |