]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/git-update-changelog.py
dist emacs patches too.
[lilypond.git] / buildscripts / git-update-changelog.py
index 2817ff31f7356da90b7e1c5fb61390c000b6ea9e..4b98c6c1994e0e3a8a2073f4144e7e8c91e53c99 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/python
 
+import sys
 import time
 import os
 import re
@@ -8,6 +9,7 @@ import optparse
 def read_pipe (x):
     print 'pipe', x
     return os.popen (x).read ()
+
 def system (x):
     print x
     return os.system (x)
@@ -15,6 +17,15 @@ def system (x):
 class PatchFailed(Exception):
     pass
 
+def sign (x):
+    if x < 0:
+        return -1
+    if x > 0:
+        return 1
+
+    return 0
+    
+
 class Commit:
     def __init__ (self, dict):
         for v in ('message',
@@ -22,8 +33,6 @@ class Commit:
                   'author',
                   'committish'):
             self.__dict__[v] = dict[v]
-
-        # Sat Oct 28 18:52:30 2006 +0200
         
         self.date = ' '.join  (self.date.split (' ')[:-1])
         self.date = time.strptime (self.date, '%a %b %d %H:%M:%S %Y')
@@ -32,8 +41,10 @@ class Commit:
         self.email = m.group (2).strip ()
         self.name = m.group (1).strip ()
         self.diff = read_pipe ('git show %s' % self.committish)
+    def compare (self, other):
+        return sign (time.mktime (self.date) - time.mktime (other.date))
+        
     def touched_files (self):
-
         files = []
         def note_file (x):
             files.append (x.group (1))
@@ -46,6 +57,9 @@ class Commit:
 
         return files
 
+    def has_patch (self):
+        return self.touched_files () <> []
+    
     def apply (self, add_del_files):
         def note_add_file (x):
             add_del_files.append (('add', x.group (1)))
@@ -55,7 +69,6 @@ class Commit:
             add_del_files.append (('del', x.group (1)))
             return ''
         
-        
         re.sub('\n--- /dev/null\n\\+\\+\\+ b/([^\n]+)',
                note_add_file, self.diff)
         
@@ -82,9 +95,17 @@ def parse_commit_log (log):
     c = Commit (locals ())
     return c
 
-def parse_add_changes (from_commit):
-    
-    log = read_pipe ('git log %(from_commit)s..' % locals ())
+def parse_add_changes (from_commit, max_count=0):
+    opt = ''
+    rest = '..'
+    if max_count:
+
+        # fixme.
+        assert max_count == 1
+        opt = '--max-count=%d' % max_count 
+        rest = ''
+        
+    log = read_pipe ('git log %(opt)s %(from_commit)s%(rest)s' % locals ())
 
     log = log[len ('commit '):]
     log = log.strip ()
@@ -102,39 +123,31 @@ def header (commit):
     return '%d-%02d-%02d  %s  <%s>\n' % (commit.date[:3] + (commit.name, commit.email))
 
 def changelog_body (commit):
-
     s = ''
-    s += "\ngit commit %s\n" % commit.committish    
     s += ''.join ('\n* %s: ' % f for f in commit.touched_files())
     s += '\n' + commit.message
     
     s = s.replace ('\n', '\n\t')
     s += '\n'
     return s
-        
-def find_last_checked_in_commit (log):
-    m = re.match ('^(\\d+-\\d+-\\d+)[^\n]+\n*\tgit commit ([a-f0-9]+)', log)
-    
-    if m:
-        return (m.group (1), m.group (2))
 
-    return None
+def main ():
+    p = optparse.OptionParser (usage="usage git-update-changelog.py [options] [commits]",
+                               description="""
+Apply GIT patches and update change log.
 
+Run this file from the CVS directory, with commits from the repository in --git-dir.
 
 
 
-def main ():
-    p = optparse.OptionParser (usage="usage git-update-changelog.py [options]",
-                               description="""
-Apply GIT patches and update change log.
 
-Run this file from the CVS directory, with --git-dir 
 """)
     p.add_option ("--start",
                   action='store',
                   default='',
+                  metavar="FIRST",
                   dest="start",
-                  help="start of log messages to merge.")
+                  help="all commits starting with FIRST.")
     
     p.add_option ("--git-dir",
                   action='store',
@@ -146,16 +159,21 @@ Run this file from the CVS directory, with --git-dir
     
     log = open ('ChangeLog').read ()
 
-    if not options.start:
-        (time, id) = find_last_checked_in_commit (log)
-        options.start = id
-
-        print 'processing commits from ', id, options.start
-
     if options.gitdir:
         os.environ['GIT_DIR'] = options.gitdir
-    
-    commits = parse_add_changes (options.start)
+
+
+    if not args:
+        if not options.start:
+            print 'Must set start committish.'  
+            sys.exit (1)
+
+        commits = parse_add_changes (options.start)
+    else:
+        commits = [] 
+        for a in args:
+            commits += parse_add_changes (a, max_count=1)
+
     if not commits:
         return
     
@@ -166,8 +184,30 @@ Run this file from the CVS directory, with --git-dir
     if first == log[:len (first)]:
         log = log[len (first):]
 
+    try:
+        previously_done = dict((c, 1) for c in open ('.git-commits-done').read ().split ('\n'))
+    except OSError:
+        previously_done = {}
+
+    commits = [c for c in commits if not previously_done.has_key (c.committish)]
+    commits = sorted (commits, cmp=Commit.compare)
+
+    
     file_adddel = []
-    for c in commits:
+    collated_log = ''
+    collated_message = ''
+
+    commits_done = []
+    while commits:
+        c = commits[0]
+        commits = commits[1:]
+        
+        commits_done.append (c) 
+
+        if not c.has_patch ():
+            print 'patchless commit (merge?)'
+            continue
+        
         print 'patch ', c.committish
         try:
             c.apply (file_adddel)
@@ -183,18 +223,23 @@ Run this file from the CVS directory, with --git-dir
 
             new_log += header (last_commit)
 
-        new_log += changelog_body (c)  
+        collated_log = changelog_body (c)  + collated_log
         last_commit = c
+
+        collated_message += c.message + '\n'
         
+
+
     for (op, f) in file_adddel:
         if op == 'del':
             system ('cvs remove %(f)s' % locals ())
         if op == 'add':
             system ('cvs add %(f)s' % locals ())
 
-    new_log = header (last_commit) + new_log + '\n'
+    if last_commit: 
+        collated_log = header (last_commit) + collated_log + '\n'
 
-    log = new_log + log
+    log = collated_log + log
 
     try:
         os.unlink ('ChangeLog~')
@@ -203,6 +248,17 @@ Run this file from the CVS directory, with --git-dir
     
     os.rename ('ChangeLog', 'ChangeLog~')
     open ('ChangeLog', 'w').write (log)
+
+    open ('.msg','w').write (collated_message)
+    print '\nCommit message\n**\n%s\n**\n' % collated_message
+    print '\nRun:\n\n\tcvs commit -F .msg\n\n'
+    print '\n\techo %s >> .git-commits-done\n\n' % ' '.join ([c.committish
+                                                              for c in commits_done]) 
+
+
+    if commits:
+        print 'Commits left to do:'
+        print ' '.join ([c.committish for c in commits])
     
 main ()