11 return os.popen (x).read ()
17 class PatchFailed(Exception):
30 def __init__ (self, dict):
35 self.__dict__[v] = dict[v]
37 self.date = ' '.join (self.date.split (' ')[:-1])
38 self.date = time.strptime (self.date, '%a %b %d %H:%M:%S %Y')
40 m = re.search ('(.*)<(.*)>', self.author)
41 self.email = m.group (2).strip ()
42 self.name = m.group (1).strip ()
43 self.diff = read_pipe ('git show %s' % self.committish)
44 def compare (self, other):
45 return sign (time.mktime (self.date) - time.mktime (other.date))
47 def check_diff_chunk (self, filename, chunk):
50 removals.append (m.group (1))
52 re.sub ('\n-([^\n]+)', note_removal, chunk)
57 if not os.path.exists (filename):
60 contents = open (filename).read ()
67 def check_diff (self):
68 chunks = re.split ('\ndiff --git ', self.diff)
72 m = re.search ('^a/([^ ]+)', c)
78 c = re.sub('\n--- [^\n]+', '', c)
79 ok = ok and self.check_diff_chunk (file, c)
85 def touched_files (self):
88 files.append (x.group (1))
91 re.sub ('\n--- a/([^\n]+)\n',
93 re.sub('\n--- /dev/null\n\\+\\+\\+ b/([^\n]+)',
99 return self.touched_files () <> []
101 def apply (self, add_del_files):
102 def note_add_file (x):
103 add_del_files.append (('add', x.group (1)))
106 def note_del_file (x):
107 add_del_files.append (('del', x.group (1)))
110 re.sub('\n--- /dev/null\n\\+\\+\\+ b/([^\n]+)',
111 note_add_file, self.diff)
113 re.sub('\n--- a/([^\n]+)\n\\+\\+\\+ /dev/null',
114 note_del_file, self.diff)
116 p = os.popen ('patch -f -p1 ', 'w')
120 raise PatchFailed, self.committish
123 def parse_commit_log (log):
124 committish = re.search ('^([^\n]+)', log).group (1)
125 author = re.search ('\nAuthor:\s+([^\n]+)', log).group (1)
126 date_match = re.search ('\nDate:\s+([^\n]+)', log)
127 date = date_match.group (1)
128 log = log[date_match.end (1):]
130 message = re.sub ("\n *", '', log)
131 message = message.strip ()
133 c = Commit (locals ())
136 def parse_add_changes (from_commit, max_count=0):
142 assert max_count == 1
143 opt = '--max-count=%d' % max_count
146 log = read_pipe ('git log %(opt)s %(from_commit)s%(rest)s' % locals ())
148 log = log[len ('commit '):]
154 commits = map (parse_commit_log, re.split ('\ncommit ', log))
161 return '%d-%02d-%02d %s <%s>\n' % (commit.date[:3] + (commit.name, commit.email))
163 def changelog_body (commit):
165 s += ''.join ('\n* %s: ' % f for f in commit.touched_files())
166 s += '\n' + commit.message
168 s = s.replace ('\n', '\n\t')
173 p = optparse.OptionParser (usage="usage git-update-changelog.py [options] [commits]",
175 Apply GIT patches and update change log.
177 Run this file from the CVS directory, with commits from the repository in --git-dir.
183 p.add_option ("--start",
188 help="all commits starting with FIRST.")
190 p.add_option ("--git-dir",
194 help="the GIT directory to merge.")
196 (options, args) = p.parse_args ()
198 log = open ('ChangeLog').read ()
201 os.environ['GIT_DIR'] = options.gitdir
205 if not options.start:
206 print 'Must set start committish.'
209 commits = parse_add_changes (options.start)
213 commits += parse_add_changes (a, max_count=1)
221 first = header (commits[0]) + '\n'
222 if first == log[:len (first)]:
223 log = log[len (first):]
226 previously_done = dict((c, 1) for c in open ('.git-commits-done').read ().split ('\n'))
230 commits = [c for c in commits if not previously_done.has_key (c.committish)]
231 commits = [c for c in commits if not previously_done.has_key (c.committish)]
232 commits = sorted (commits, cmp=Commit.compare)
237 collated_message = ''
242 if not c.has_patch ():
243 print 'patchless commit (merge?)'
249 print "Patch doesn't seem to apply"
250 print 'skipping', c.committish
251 print 'message:', c.message
256 commits = commits[1:]
257 commits_done.append (c)
259 print 'patch ', c.committish
261 c.apply (file_adddel)
265 if c.touched_files () == ['ChangeLog']:
269 and c.author != last_commit.author
270 and c.date[:3] != last_commit.date[:3]):
272 new_log += header (last_commit)
274 collated_log = changelog_body (c) + collated_log
277 collated_message += c.message + '\n'
281 for (op, f) in file_adddel:
283 system ('cvs remove %(f)s' % locals ())
285 system ('cvs add %(f)s' % locals ())
288 collated_log = header (last_commit) + collated_log + '\n'
290 log = collated_log + log
293 os.unlink ('ChangeLog~')
297 os.rename ('ChangeLog', 'ChangeLog~')
298 open ('ChangeLog', 'w').write (log)
300 open ('.msg','w').write (collated_message)
301 print '\nCommit message\n**\n%s\n**\n' % collated_message
302 print '\nRun:\n\n\tcvs commit -F .msg\n\n'
303 print '\n\techo %s >> .git-commits-done\n\n' % ' '.join ([c.committish
304 for c in commits_done])
308 print 'Commits left to do:'
309 print ' '.join ([c.committish for c in commits])