2009-07-10 David Kilzer <ddkilzer@apple.com>
authorbfulgham@webkit.org <bfulgham@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Jul 2009 20:15:32 +0000 (20:15 +0000)
committerbfulgham@webkit.org <bfulgham@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Jul 2009 20:15:32 +0000 (20:15 +0000)
        bugzilla-tool: create CommitMessage class

        Reviewed by Eric Seidel.

        Create a CommitMessage class to encapsulate related code.

        * Scripts/bugzilla-tool:
         (bug_id_from_commit_message): Moved to
         CommitMessage.parse_bug_id().
         (commit_message_for_this_commit): Return a CommitMessage.
         (ApplyPatchesFromBug.apply_patches): Use CommitMessage.message().
         (LandPatchesFromBugs.build_and_commit): Ditto.
         (CommitMessageForCurrentDiff.execute): Ditto.
         (PostCommitsAsPatchesToBug.execute): Switched from
         Git.commit_message_for_commit() to
         Git.commit_message_for_local_commit().  Switched from
         bug_id_from_commit_message() to CommitMessage.parse_bug_id().

         * Scripts/modules/scm.py:
         (first_non_empty_line_after_index): Added.
         (CommitMessage.__init__): Added.
         (CommitMessage.body): Added.
         (CommitMessage.description): Added.
         (CommitMessage.message): Added.
         (CommitMessage.parse_bug_id): Added.  Moved from
         bug_id_from_commit_message() in bugzilla-tool.
         (Git.commit_message_for_local_commit): Renamed from
         commit_message_for_commit().  Return a CommitMessage.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@45940 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebKitTools/ChangeLog
WebKitTools/Scripts/bugzilla-tool
WebKitTools/Scripts/modules/scm.py

index 68787a7..463ff12 100644 (file)
@@ -1,3 +1,34 @@
+2009-07-10  David Kilzer  <ddkilzer@apple.com>
+
+        bugzilla-tool: create CommitMessage class
+
+        Reviewed by Eric Seidel.
+
+        Create a CommitMessage class to encapsulate related code.
+
+        * Scripts/bugzilla-tool:
+         (bug_id_from_commit_message): Moved to
+         CommitMessage.parse_bug_id().
+         (commit_message_for_this_commit): Return a CommitMessage.
+         (ApplyPatchesFromBug.apply_patches): Use CommitMessage.message().
+         (LandPatchesFromBugs.build_and_commit): Ditto.
+         (CommitMessageForCurrentDiff.execute): Ditto.
+         (PostCommitsAsPatchesToBug.execute): Switched from
+         Git.commit_message_for_commit() to
+         Git.commit_message_for_local_commit().  Switched from
+         bug_id_from_commit_message() to CommitMessage.parse_bug_id().
+
+         * Scripts/modules/scm.py:
+         (first_non_empty_line_after_index): Added.
+         (CommitMessage.__init__): Added.
+         (CommitMessage.body): Added.
+         (CommitMessage.description): Added.
+         (CommitMessage.message): Added.
+         (CommitMessage.parse_bug_id): Added.  Moved from
+         bug_id_from_commit_message() in bugzilla-tool.
+         (Git.commit_message_for_local_commit): Renamed from
+         commit_message_for_commit().  Return a CommitMessage.
+
 2009-07-15  Joseph Pecoraro  <joepeck02@gmail.com>
 
         Reviewed by David Kilzer.
index fd4df4d..b5e09ab 100755 (executable)
@@ -96,15 +96,6 @@ def modified_changelogs(scm):
             changelog_paths.append(path)
     return changelog_paths
 
-def bug_id_from_commit_message(commit_message):
-    match = re.search("http\://webkit\.org/b/(?P<bug_id>\d+)", commit_message, re.MULTILINE)
-    if match:
-        return match.group('bug_id')
-    match = re.search(Bugzilla.bug_server_regex + "show_bug\.cgi\?id=(?P<bug_id>\d+)", commit_message, re.MULTILINE)
-    if match:
-        return match.group('bug_id')
-    return None
-
 def commit_message_for_this_commit(scm):
     changelog_paths = modified_changelogs(scm)
     if not len(changelog_paths):
@@ -120,7 +111,7 @@ def commit_message_for_this_commit(scm):
         changelog_messages.append(changelog_entry)
     
     # FIXME: We should sort and label the ChangeLog messages like commit-log-editor does.
-    return ''.join(changelog_messages)
+    return CommitMessage(''.join(changelog_messages).splitlines())
 
 
 class Command:
@@ -194,7 +185,7 @@ class ApplyPatchesFromBug(Command):
             scm.apply_patch(patch)
             if commit_each:
                 commit_message = commit_message_for_this_commit(scm)
-                scm.commit_locally_with_message(commit_message or patch['name'])
+                scm.commit_locally_with_message(commit_message.message() or patch['name'])
 
     def execute(self, options, args, tool):
         bug_id = args[0]
@@ -317,7 +308,7 @@ class LandPatchesFromBugs(Command):
             if options.test:
                 cls.run_webkit_tests()
         commit_message = commit_message_for_this_commit(scm)
-        commit_log = scm.commit_with_message(commit_message)
+        commit_log = scm.commit_with_message(commit_message.message())
         return bug_comment_from_commit_text(scm, commit_log)
 
     @classmethod
@@ -368,7 +359,7 @@ class CommitMessageForCurrentDiff(Command):
 
     def execute(self, options, args, tool):
         os.chdir(tool.scm().checkout_root)
-        print "%s" % commit_message_for_this_commit(tool.scm())
+        print "%s" % commit_message_for_this_commit(tool.scm()).message()
 
 
 class ObsoleteAttachmentsOnBug(Command):
@@ -432,10 +423,9 @@ class PostCommitsAsPatchesToBug(Command):
 
         have_obsoleted_patches = set()
         for commit_id in commit_ids:
-            commit_message = tool.scm().commit_message_for_commit(commit_id)
-            commit_lines = commit_message.splitlines()
+            commit_message = tool.scm().commit_message_for_local_commit(commit_id)
 
-            bug_id = options.bug_id or bug_id_from_commit_message(commit_message)
+            bug_id = options.bug_id or commit_message.parse_bug_id()
             if not bug_id:
                 log("Skipping %s: No bug id found in commit log or specified with --bug-id." % commit_id)
                 continue
@@ -444,11 +434,11 @@ class PostCommitsAsPatchesToBug(Command):
                 PostDiffAsPatchToBug.obsolete_patches_on_bug(bug_id, tool.bugs)
                 have_obsoleted_patches.update(bug_id)
 
-            description = commit_lines[0]
+            description = commit_message.description(lstrip=True, strip_url=True)
             comment_text = None
             if (options.comment):
-                comment_text = "\n".join(commit_lines[1:])
-                comment_text += "\n---\n"
+                comment_text = commit_message.body(lstrip=True)
+                comment_text += "---\n"
                 comment_text += tool.scm().files_changed_summary_for_commit(commit_id)
 
             diff = tool.scm().create_patch_from_local_commit(commit_id)
index 5a706b1..bfe792e 100644 (file)
@@ -45,9 +45,52 @@ def detect_scm_system(path):
     
     return None
 
+def first_non_empty_line_after_index(lines, index=0):
+    first_non_empty_line = index
+    for line in lines[index:]:
+        if re.match("^\s*$", line):
+            first_non_empty_line += 1
+        else:
+            break
+    return first_non_empty_line
+
+
+class CommitMessage:
+    def __init__(self, message):
+        self.message = message[first_non_empty_line_after_index(message, 0):]
+
+    def body(self, lstrip=False):
+        lines = self.message[first_non_empty_line_after_index(self.message, 1):]
+        if lstrip:
+            lines = [line.lstrip() for line in lines]
+        return "\n".join(lines) + "\n"
+
+    def description(self, lstrip=False, strip_url=False):
+        line = self.message[0]
+        if lstrip:
+            line = line.lstrip()
+        if strip_url:
+            line = re.sub("^(\s*)<.+> ", "\1", line)
+        return line
+
+    def message(self):
+        return "\n".join(self.message) + "\n"
+
+    def parse_bug_id(self):
+        for line in self.message:
+            match = re.search("http\://webkit\.org/b/(?P<bug_id>\d+)", line)
+            if match:
+                return match.group('bug_id')
+            match = re.search(Bugzilla.bug_server_regex + "show_bug\.cgi\?id=(?P<bug_id>\d+)", line)
+            if match:
+                return match.group('bug_id')
+        return None
+
+
 class ScriptError(Exception):
     pass
 
+
 class SCM:
     def __init__(self, cwd, dryrun=False):
         self.cwd = cwd
@@ -162,6 +205,7 @@ class SCM:
     def local_commits(self):
         return []
 
+
 class SVN(SCM):
     def __init__(self, cwd, dryrun=False):
         SCM.__init__(self, cwd, dryrun)
@@ -226,6 +270,7 @@ class SVN(SCM):
             return "Dry run, no remote commit."
         return self.run_command(['svn', 'commit', '-m', message])
 
+
 # All git-specific logic should go here.
 class Git(SCM):
     def __init__(self, cwd, dryrun=False):
@@ -315,7 +360,7 @@ class Git(SCM):
 
         return self.run_command(['git', 'rev-list'] + revisions).splitlines()
 
-    def commit_message_for_commit(self, commit_id):
+    def commit_message_for_local_commit(self, commit_id):
         commit_lines = self.run_command(['git', 'cat-file', 'commit', commit_id]).splitlines()
 
         # Skip the git headers.
@@ -324,7 +369,7 @@ class Git(SCM):
             first_line_after_headers += 1
             if line == "":
                 break
-        return "\n".join(commit_lines[first_line_after_headers:])
+        return CommitMessage(commit_lines[first_line_after_headers:])
 
     def files_changed_summary_for_commit(self, commit_id):
         return self.run_command(['git', 'diff-tree', '--shortstat', '--no-commit-id', commit_id])