Remove ChangeLog files from the repo
[mono.git] / scripts / commits-to-changelog.py
index fa2079fbfdd1ae9337973ef4fe9d14aee1127799..6181c751ca9c8d09b9d37024ab7ed9e1e8a48a61 100755 (executable)
@@ -1,11 +1,13 @@
 #!/usr/bin/python
 
+from __future__ import print_function
 from optparse import OptionParser
 import subprocess
 import re
 import os.path
 import fnmatch
 import os
+import sys
 
 # subtract 8 for the leading tabstop
 fill_column = 74 - 8
@@ -15,7 +17,12 @@ path_to_root = None
 all_changelogs = {}
 
 def git (command, *args):
-    return subprocess.Popen (["git", command] + list (args), stdout = subprocess.PIPE).communicate () [0]
+    popen = subprocess.Popen (["git", command] + list (args), stdout = subprocess.PIPE)
+    output = popen.communicate () [0]
+    if popen.returncode != 0:
+        print ("Error: git failed", file=sys.stderr)
+        exit (1)
+    return output
 
 def changelog_path (changelog):
     global path_to_root
@@ -46,6 +53,8 @@ def changelogs_for_file_pattern (pattern, changed_files):
 def format_paragraph (paragraph):
     lines = []
     words = paragraph.split ()
+    if len (words) == 0:
+        return lines
     current = words [0]
     for word in words [1:]:
         if len (current) + 1 + len (word) <= fill_column:
@@ -88,7 +97,7 @@ def format_changelog_entries (commit, changed_files, prefix, file_entries, all_p
         for (filename, entity) in files:
             entry_changelogs = changelogs_for_file_pattern (filename, changed_files)
             if len (entry_changelogs) == 0:
-                print "Warning: could not match file %s in commit %s" % (filename, commit)
+                print ("Warning: could not match file {0} in commit {1}".format (filename, commit))
             for changelog in entry_changelogs:
                 if changelog not in changelog_entries:
                     changelog_entries [changelog] = []
@@ -103,7 +112,7 @@ def format_changelog_entries (commit, changed_files, prefix, file_entries, all_p
     unmarked_changelogs = changelogs - marked_changelogs
     for changelog in unmarked_changelogs:
         if len (prefix) == 0:
-            print "Warning: empty entry in %s for commit %s" % (changelog_path (changelog), commit)
+            print ("Warning: empty entry in {0} for commit {1}".format (changelog_path (changelog), commit))
             insert_paragraphs = all_paragraphs
         else:
             insert_paragraphs = prefix
@@ -113,14 +122,14 @@ def format_changelog_entries (commit, changed_files, prefix, file_entries, all_p
     return paragraphs
 
 def debug_print_commit (commit, raw_message, prefix, file_entries, changed_files, changelog_entries):
-    print "===================== Commit"
-    print commit
-    print "--------------------- RAW"
-    print raw_message
-    print "--------------------- Prefix"
+    print ("===================== Commit")
+    print (commit)
+    print ("--------------------- RAW")
+    print (raw_message)
+    print ("--------------------- Prefix")
     for line in prefix:
-        print line
-    print "--------------------- File entries"
+        print (line)
+    print ("--------------------- File entries")
     for (files, comments) in file_entries:
         files_str = ""
         for (filename, entity) in files:
@@ -131,20 +140,20 @@ def debug_print_commit (commit, raw_message, prefix, file_entries, changed_files
                 files_str = files_str + " (" + entity + ")"
         print files_str
         for line in comments:
-            print "  " + line
-    print "--------------------- Files touched"
+            print ("  " + line)
+    print ("--------------------- Files touched")
     for f in changed_files:
-        print f
-    print "--------------------- ChangeLog entries"
+        print (f)
+    print ("--------------------- ChangeLog entries")
     for ((dirname, filename), lines) in changelog_entries.items ():
-        print "%s/%s:" % (dirname, filename)
+        print ("{0}/{1}:".format (dirname, filename))
         for line in lines:
-            print line
+            print (line)
 
 def process_commit (commit):
     changed_files = map (lambda l: l.split () [2], git ("diff-tree", "--numstat", commit).splitlines () [1:])
     if len (filter (lambda f: re.search ("(^|/)Change[Ll]og$", f), changed_files)):
-        return
+        return None
     raw_message = git ("log", "-n1", "--format=%B", commit)
     # filter SVN migration message
     message = re.sub ("(^|\n)svn path=[^\n]+revision=\d+(?=$|\n)", "", raw_message)
@@ -241,17 +250,33 @@ def main ():
         global path_to_root
         path_to_root = options.root + "/"
 
-    for filename in git ("ls-tree", "-r", "--name-only", "HEAD").splitlines ():
+    # MonkeyWrench uses a shared git repo but sets BUILD_REVISION,
+    # if present we use it instead of HEAD
+    HEAD = "HEAD"
+    if 'BUILD_REVISION' in os.environ:
+        HEAD = os.environ['BUILD_REVISION']
+
+    #see if git supports %B in --format
+    output = git ("log", "-n1", "--format=%B", HEAD)
+    if output.startswith ("%B"):
+        print ("Error: git doesn't support %B in --format - install version 1.7.2 or newer", file=sys.stderr)
+        exit (1)
+
+    for filename in git ("ls-tree", "-r", "--name-only", HEAD).splitlines ():
         if re.search ("(^|/)Change[Ll]og$", filename):
             (path, name) = os.path.split (filename)
             all_changelogs [path] = name
 
-    commits = git ("rev-list", "--no-merges", "HEAD", "^%s" % start_commit).splitlines ()
+    commits = git ("rev-list", "--no-merges", HEAD, "^{0}".format (start_commit)).splitlines ()
 
     touched_changelogs = {}
     for commit in commits:
         entries = process_commit (commit)
+        if entries == None:
+            continue
         for (changelog, lines) in entries.items ():
+            if not os.path.exists (changelog_path (changelog)):
+                continue
             if changelog not in touched_changelogs:
                 touched_changelogs [changelog] = start_changelog (changelog)
             append_lines (touched_changelogs [changelog], lines)