Drop foreign key from 'References:' relation
[trackgit.git] / notes.py
blob4afc467545ba92483dc398c2bd89675cb8155358
1 import sys
2 import time
3 from collections import defaultdict
5 import db
6 from git import git
8 NOTES_INDEX = '.git/git-notes-index'
9 notes_env = {
10 'GIT_INDEX_FILE': NOTES_INDEX,
12 NOTES_REF = 'refs/heads/mailnotes'
14 def write_notes(commit_sha1, notes):
15 blob_sha1 = git('hash-object', '-w', '--stdin', input=notes)[0]
16 git('update-index', '--add', '--cacheinfo', '0644', blob_sha1, commit_sha1, env=notes_env)
18 def finalize_notes():
19 previous, ret = git('rev-parse', NOTES_REF)
20 if ret != 0:
21 args = []
22 previous_arg = []
23 else:
24 args = ['-p', previous.strip()]
25 previous_arg = [previous.strip()]
26 tree_sha1 = git('write-tree', env=notes_env)[0].strip()
27 head_sha1 = git('commit-tree', tree_sha1, *args,
28 input='Mass annotation by notes.py')[0].strip()
29 git('update-ref', '-m', 'Mass annotation by notes.py',
30 NOTES_REF, head_sha1, *previous_arg)
32 def compute_notes(session, commit, mail):
33 notes = []
34 notes.append('Mail-From: %s\n' % mail.author)
35 notes.append('Posted-Date: %s\n'
36 % time.strftime("%c", time.localtime(mail.post_date)))
37 if mail.in_reply_to:
38 notes.append('In-Reply-To: %s\n' % mail.in_reply_to)
39 if mail.references:
40 notes.append('References:\n')
41 for r in mail.references:
42 notes.append('\t%s\n' % r.message_id)
43 if len(mail.patch)>0 and mail.patch[0].extra_notes:
44 notes.append('Extra-notes:\n')
45 for line in str(mail.patch[0].extra_notes).splitlines():
46 notes.append('\t%s\n' % line)
47 return notes
49 def _redo_all():
50 session = db.Session()
51 count = 0
52 notes = defaultdict(list)
53 for cmt, mail in (session.query(db.Commit, db.Mail)
54 .filter(db.Commit.upstream==True)
55 .filter(db.Mail.has_patch==True)
56 .filter(db.Mail.patch_id==db.Commit.patch_id)):
57 nts = compute_notes(session, cmt, mail)
58 if not nts:
59 continue
60 if notes[cmt]:
61 notes[cmt].append('\n')
62 notes[cmt].extend(nts)
63 sys.stdout.write('\r%6d' % count)
64 sys.stdout.flush()
65 count = count + 1
66 count = 0
67 sys.stdout.write('\n')
68 for cmt, nts in notes.iteritems():
69 sys.stdout.write('\r%6d' % count)
70 sys.stdout.flush()
71 write_notes(cmt.sha1, ''.join(nts))
72 count = count + 1
73 sys.stdout.write('\n')
74 finalize_notes()
76 if __name__ == '__main__':
77 if len(sys.argv) > 1:
78 session = db.Session()
79 cmt = session.query(db.Commit).filter(db.Commit.sha1==sys.argv[1]).one()
80 print compute_notes(session, cmt)
81 else:
82 _redo_all()