allura
リビジョン | 0cb43092bd74695e7320cce8ae7147c1412b3d3d (tree) |
---|---|
日時 | 2011-10-06 02:14:54 |
作者 | Rick Copeland <rcopeland@geek...> |
コミッター | Rick Copeland |
[#1540] Add last commit info to new model
Signed-off-by: Rick Copeland <rcopeland@geek.net>
@@ -3,7 +3,7 @@ from datetime import datetime | ||
3 | 3 | from ming import Document, Field |
4 | 4 | from ming import schema as S |
5 | 5 | |
6 | -from .session import main_doc_session | |
6 | +from .session import main_doc_session, project_doc_session | |
7 | 7 | |
8 | 8 | class Commit(Document): |
9 | 9 | class __mongometa__: |
@@ -39,6 +39,16 @@ class Commit(Document): | ||
39 | 39 | return ' '.join(summary) |
40 | 40 | return '' |
41 | 41 | |
42 | + def url(self): | |
43 | + return '' | |
44 | + | |
45 | + def shorthand_id(self): | |
46 | + return '' | |
47 | + | |
48 | + @property | |
49 | + def author_url(self): | |
50 | + return '' | |
51 | + | |
42 | 52 | class Tree(Document): |
43 | 53 | class __mongometa__: |
44 | 54 | name = 'repo_tree' |
@@ -50,6 +60,45 @@ class Tree(Document): | ||
50 | 60 | blob_ids = Field([dict(name=str, id=str)]) |
51 | 61 | other_ids = Field([dict(name=str, id=str, type=ObjType)]) |
52 | 62 | |
63 | +class LastCommit(Document): | |
64 | + class __mongometa__: | |
65 | + name = 'repo_last_commit' | |
66 | + session = project_doc_session | |
67 | + indexes = [ | |
68 | + ( 'repo_id', 'object_id'), | |
69 | + ] | |
70 | + | |
71 | + _id = Field(str) | |
72 | + repo_id=Field(S.ObjectId()) | |
73 | + object_id=Field(str) | |
74 | + commit_info = Field(dict( | |
75 | + id=str, | |
76 | + date=datetime, | |
77 | + author=str, | |
78 | + author_email=str, | |
79 | + author_url=str, | |
80 | + href=str, | |
81 | + shortlink=str, | |
82 | + summary=str)) | |
83 | + | |
84 | + @classmethod | |
85 | + def set_last_commit(cls, repo_id, oid, commit): | |
86 | + lc = cls(dict( | |
87 | + _id='%s:%s' % (repo_id, oid), | |
88 | + repo_id=repo_id, | |
89 | + object_id=oid, | |
90 | + commit_info=dict( | |
91 | + id=commit._id, | |
92 | + author=commit.authored.name, | |
93 | + author_email=commit.authored.email, | |
94 | + author_url=commit.author_url, | |
95 | + date=commit.authored.date, | |
96 | + href=commit.url(), | |
97 | + shortlink=commit.shorthand_id(), | |
98 | + summary=commit.summary))) | |
99 | + lc.m.save(safe=False) | |
100 | + return lc | |
101 | + | |
53 | 102 | class Trees(Document): |
54 | 103 | class __mongometa__: |
55 | 104 | name = 'repo_trees' |
@@ -35,6 +35,7 @@ def main(): | ||
35 | 35 | M.repo.Tree.m.remove({}) |
36 | 36 | M.repo.Trees.m.remove({}) |
37 | 37 | M.repo.DiffInfo.m.remove({}) |
38 | + M.repo.LastCommit.m.remove({}) | |
38 | 39 | M.repo.BasicBlock.m.remove({}) |
39 | 40 | repo = c.app.repo._impl._git |
40 | 41 |
@@ -70,15 +71,17 @@ def main(): | ||
70 | 71 | parents = set() |
71 | 72 | |
72 | 73 | for i, oid in enumerate(commit_ids): |
73 | - ci = M.repo.Commit.m.get(_id=oid) | |
74 | + ci = M.repo.Commit.m.find(dict(_id=oid), validate=False).next() | |
74 | 75 | refresh_children(ci) |
75 | 76 | seen.add(ci._id) |
76 | 77 | parents.update(ci.parent_ids) |
77 | 78 | if (i+1) % 100 == 0: |
78 | 79 | log.info('Refresh child (a) info %d: %s', (i+1), ci._id) |
79 | 80 | for j, oid in enumerate(parents-seen): |
80 | - ci = M.repo.Commit.m.get(_id=oid) | |
81 | - if ci is None: continue | |
81 | + try: | |
82 | + ci = M.repo.Commit.m.find(dict(_id=oid), validate=False).next() | |
83 | + except StopIteration: | |
84 | + continue | |
82 | 85 | refresh_children(ci) |
83 | 86 | if (i + j + 1) % 100 == 0: |
84 | 87 | log.info('Refresh child (b) info %d: %s', (i + j + 1), ci._id) |
@@ -90,14 +93,14 @@ def main(): | ||
90 | 93 | |
91 | 94 | # Verify the log |
92 | 95 | log.info('Logging via basic blocks') |
93 | - for i, ci in enumerate(commitlog(commit_ids[0], skip=2000, limit=50)): | |
96 | + for i, ci in enumerate(commitlog(commit_ids[0])): | |
94 | 97 | pass |
95 | 98 | log.info('... done (%d commits from %s)', i+1, commit_ids[0]) |
96 | 99 | |
97 | 100 | # Refresh trees |
98 | 101 | cache = {} |
99 | 102 | for i, oid in enumerate(commit_ids): |
100 | - ci = M.repo.Commit.m.get(_id=oid) | |
103 | + ci = M.repo.Commit.m.find(dict(_id=oid), validate=False).next() | |
101 | 104 | cache = refresh_commit_trees(ci, cache) |
102 | 105 | if (i+1) % 100 == 0: |
103 | 106 | log.info('Refresh commit trees %d: %s', (i+1), ci._id) |
@@ -105,8 +108,8 @@ def main(): | ||
105 | 108 | # Compute diffs |
106 | 109 | cache = {} |
107 | 110 | for i, oid in enumerate(commit_ids): |
108 | - ci = M.repo.Commit.m.get(_id=oid) | |
109 | - compute_diffs(cache, ci) | |
111 | + ci = M.repo.Commit.m.find(dict(_id=oid), validate=False).next() | |
112 | + compute_diffs(c.app.repo._id, cache, ci) | |
110 | 113 | if (i+1) % 100 == 0: |
111 | 114 | log.info('Compute diffs %d: %s', (i+1), ci._id) |
112 | 115 |
@@ -122,7 +125,7 @@ def refresh_commit_trees(ci, cache): | ||
122 | 125 | |
123 | 126 | def refresh_commit_info(ci, seen): |
124 | 127 | if M.repo.Commit.m.find(dict(_id=ci.hexsha)).count() != 0: |
125 | - return | |
128 | + return False | |
126 | 129 | try: |
127 | 130 | ci_doc = M.repo.Commit(dict( |
128 | 131 | _id=ci.hexsha, |
@@ -142,8 +145,9 @@ def refresh_commit_info(ci, seen): | ||
142 | 145 | parent_ids = [ p.hexsha for p in ci.parents ])) |
143 | 146 | ci_doc.m.insert(safe=True) |
144 | 147 | except DuplicateKeyError: |
145 | - return | |
148 | + return False | |
146 | 149 | refresh_tree(ci.tree, seen) |
150 | + return True | |
147 | 151 | |
148 | 152 | def refresh_repo(commit_ids, repo_id): |
149 | 153 | for oids in utils.chunked_iter(commit_ids, QSIZE): |
@@ -285,10 +289,20 @@ def unknown_commit_ids(all_commit_ids): | ||
285 | 289 | result += [ oid for oid in chunk if oid not in known_commit_ids ] |
286 | 290 | return result |
287 | 291 | |
288 | -def compute_diffs(tree_cache, rhs_ci, lhs_ci=None): | |
292 | +def compute_diffs(repo_id, tree_cache, rhs_ci): | |
293 | + def _walk_tree(tree, tree_index): | |
294 | + for x in tree.blob_ids: yield x.id | |
295 | + for x in tree.other_ids: yield x.id | |
296 | + for x in tree.tree_ids: | |
297 | + yield x.id | |
298 | + for xx in _walk_tree(tree_index[x.id], tree_index): | |
299 | + yield xx | |
300 | + | |
289 | 301 | rhs_tree_ids = M.repo.Trees.m.get(_id=rhs_ci._id).tree_ids |
290 | - if lhs_ci is None and rhs_ci.parent_ids: | |
302 | + if rhs_ci.parent_ids: | |
291 | 303 | lhs_ci = M.repo.Commit.m.get(_id=rhs_ci.parent_ids[0]) |
304 | + else: | |
305 | + lhs_ci = None | |
292 | 306 | if lhs_ci is not None: |
293 | 307 | lhs_tree_ids = M.repo.Trees.m.get(_id=lhs_ci._id).tree_ids |
294 | 308 | else: |
@@ -308,9 +322,17 @@ def compute_diffs(tree_cache, rhs_ci, lhs_ci=None): | ||
308 | 322 | lhs_tree = Object(_id=None, tree_ids=[], blob_ids=[], other_ids=[]) |
309 | 323 | else: |
310 | 324 | lhs_tree = tree_index[lhs_ci.tree_id] |
311 | - differences = [ | |
312 | - dict(name=name, lhs_id=lhs_id, rhs_id=rhs_id) | |
313 | - for name, lhs_id, rhs_id in _diff_trees(lhs_tree, rhs_tree, tree_index) ] | |
325 | + differences = [] | |
326 | + for name, lhs_id, rhs_id in _diff_trees(lhs_tree, rhs_tree, tree_index): | |
327 | + differences.append( | |
328 | + dict(name=name, lhs_id=lhs_id, rhs_id=rhs_id)) | |
329 | + # Set last commit info | |
330 | + if rhs_id is not None: | |
331 | + M.repo.LastCommit.set_last_commit(repo_id, rhs_id, rhs_ci) | |
332 | + rhs_tree = tree_index.get(rhs_id, None) | |
333 | + if rhs_tree is not None: | |
334 | + for oid in _walk_tree(rhs_tree, tree_index): | |
335 | + M.repo.LastCommit.set_last_commit(repo_id, oid, rhs_ci) | |
314 | 336 | di = M.repo.DiffInfo(dict( |
315 | 337 | _id=rhs_ci._id, |
316 | 338 | differences=differences)) |