Switch to unified view

a/ForgeHg/forgehg/model/hg.py b/ForgeHg/forgehg/model/hg.py
...
...
90
        self._repo.status = 'ready'
90
        self._repo.status = 'ready'
91
        log.info('... %s ready', self._repo)
91
        log.info('... %s ready', self._repo)
92
        session(self._repo).flush()
92
        session(self._repo).flush()
93
93
94
    def commit(self, rev):
94
    def commit(self, rev):
95
        result = M.Commit.query.get(object_id=rev)
95
        result = M.repo.Commit.query.get(_id=rev)
96
        if result is None:
96
        if result is None:
97
            try:
97
            try:
98
                impl = self._hg[str(rev)]
98
                impl = self._hg[str(rev)]
99
                result = M.Commit.query.get(object_id=impl.hex())
99
                result = M.repo.Commit.query.get(_id=impl.hex())
100
            except Exception, e:
100
            except Exception, e:
101
                log.exception(e)
101
                log.exception(e)
102
        if result is None: return None
102
        if result is None: return None
103
        result.set_context(self._repo)
103
        result.set_context(self._repo)
104
        return result
104
        return result
...
...
121
        while to_visit:
121
        while to_visit:
122
            obj = to_visit.pop()
122
            obj = to_visit.pop()
123
            if obj.hex() in graph: continue
123
            if obj.hex() in graph: continue
124
            if not all_commits:
124
            if not all_commits:
125
                # Look up the object
125
                # Look up the object
126
                if M.Commit.query.find(dict(object_id=obj.hex())).count():
126
                if M.repo.Commit.query.find(dict(_id=obj.hex())).count():
127
                    graph[obj.hex()] = set() # mark as parentless
127
                    graph[obj.hex()] = set() # mark as parentless
128
                    continue
128
                    continue
129
            graph[obj.hex()] = set(
129
            graph[obj.hex()] = set(
130
                p.hex() for p in obj.parents()
130
                p.hex() for p in obj.parents()
131
                if p.hex() != obj.hex())
131
                if p.hex() != obj.hex())
132
            to_visit += obj.parents()
132
            to_visit += obj.parents()
133
        return list(topological_sort(graph))
133
        return list(topological_sort(graph))
134
135
    def commit_context(self, commit):
136
        prev_ids = commit.parent_ids
137
        prev = M.Commit.query.find(dict(
138
                object_id={'$in':prev_ids})).all()
139
        next = M.Commit.query.find(dict(
140
                parent_ids=commit.object_id,
141
                repositories=self._repo._id)).all()
142
        for ci in prev + next:
143
            ci.set_context(self._repo)
144
        return dict(prev=prev, next=next)
145
134
146
    def refresh_heads(self):
135
    def refresh_heads(self):
147
        self._repo.heads = [
136
        self._repo.heads = [
148
            Object(name=None, object_id=self._hg[head].hex())
137
            Object(name=None, object_id=self._hg[head].hex())
149
            for head in self._hg.heads() ]
138
            for head in self._hg.heads() ]
...
...
155
            for name, tag in self._hg.tags().iteritems() ]
144
            for name, tag in self._hg.tags().iteritems() ]
156
        session(self._repo).flush()
145
        session(self._repo).flush()
157
146
158
    def refresh_commit(self, ci, seen_object_ids=None, lazy=True):
147
    def refresh_commit(self, ci, seen_object_ids=None, lazy=True):
159
        if seen_object_ids is None: seen_object_ids = set()
148
        if seen_object_ids is None: seen_object_ids = set()
160
        obj = self._hg[ci.object_id]
149
        obj = self._hg[ci._id]
161
        # Save commit metadata
150
        # Save commit metadata
162
        mo = self.re_hg_user.match(obj.user())
151
        mo = self.re_hg_user.match(obj.user())
163
        if mo:
152
        if mo:
164
            user_name, user_email = mo.groups()
153
            user_name, user_email = mo.groups()
165
        else:
154
        else:
...
...
174
            p.hex() for p in obj.parents()
163
            p.hex() for p in obj.parents()
175
            if p.hex() != obj.hex() ]
164
            if p.hex() != obj.hex() ]
176
        # Save commit tree (must build a fake git-like tree from the changectx)
165
        # Save commit tree (must build a fake git-like tree from the changectx)
177
        fake_tree = self._tree_from_changectx(obj)
166
        fake_tree = self._tree_from_changectx(obj)
178
        ci.tree_id = fake_tree.hex()
167
        ci.tree_id = fake_tree.hex()
179
        tree, isnew = M.Tree.upsert(fake_tree.hex())
168
        tree, isnew = M.repo.Tree.upsert(fake_tree.hex())
180
        if not lazy or isnew:
169
        if not lazy or isnew:
181
            tree.set_context(ci)
170
            tree.set_context(ci)
182
            self._refresh_tree(tree, fake_tree, lazy)
171
            self._refresh_tree(tree, fake_tree, lazy)
183
172
184
    def refresh_commit_info(self, oid, seen, lazy=True):
173
    def refresh_commit_info(self, oid, seen, lazy=True):
...
...
252
                skip -= 1
241
                skip -= 1
253
            candidates += obj.parents()
242
            candidates += obj.parents()
254
        return result, [ p.hex() for p in candidates ]
243
        return result, [ p.hex() for p in candidates ]
255
244
256
    def open_blob(self, blob):
245
    def open_blob(self, blob):
257
        fctx = self._hg[blob.commit.object_id][h.really_unicode(blob.path()).encode('utf-8')[1:]]
246
        fctx = self._hg[blob.commit._id][h.really_unicode(blob.path()).encode('utf-8')[1:]]
258
        return StringIO(fctx.data())
247
        return StringIO(fctx.data())
259
248
260
    def _setup_hooks(self):
249
    def _setup_hooks(self):
261
        'Set up the hg changegroup hook'
250
        'Set up the hg changegroup hook'
262
        cp = ConfigParser()
251
        cp = ConfigParser()
...
...
280
            oid = b2a_hex(fctx.filenode())
269
            oid = b2a_hex(fctx.filenode())
281
            root.set_blob(filepath, oid)
270
            root.set_blob(filepath, oid)
282
        return root
271
        return root
283
272
284
    def _refresh_tree(self, tree, obj, lazy=True):
273
    def _refresh_tree(self, tree, obj, lazy=True):
285
        tree.object_ids=[
274
        tree.tree_ids = [
286
            Object(object_id=o.hex(), name=name)
275
            Object(object_id=o.hex(), name=name)
287
            for name, o in obj.trees.iteritems() ]
276
            for name, o in obj.trees.iteritems() ]
288
        tree.object_ids += [
277
        tree.blob_ids = [
289
            Object(object_id=oid, name=name)
278
            Object(object_id=oid, name=name)
290
            for name, oid in obj.blobs.iteritems() ]
279
            for name, oid in obj.blobs.iteritems() ]
291
        for name, o in obj.trees.iteritems():
280
        for name, o in obj.trees.iteritems():
292
            subtree, isnew = M.Tree.upsert(o.hex())
281
            subtree, isnew = M.repo.Tree.upsert(o.hex())
293
            if not lazy or isnew:
282
            if not lazy or isnew:
294
                subtree.set_context(tree, name)
283
                subtree.set_context(tree, name)
295
                self._refresh_tree(subtree, o)
284
                self._refresh_tree(subtree, o)
296
        for name, oid in obj.blobs.iteritems():
285
        for name, oid in obj.blobs.iteritems():
297
            blob, isnew = M.Blob.upsert(oid)
286
            blob, isnew = M.Blob.upsert(oid)
298
287
299
    def symbolics_for_commit(self, commit):
288
    def symbolics_for_commit(self, commit):
300
        branch_heads, tags = super(self.__class__, self).symbolics_for_commit(commit)
289
        branch_heads, tags = super(self.__class__, self).symbolics_for_commit(commit)
301
        ctx = self._hg[commit.object_id]
290
        ctx = self._hg[commit._id]
302
        return [ctx.branch()], tags
291
        return [ctx.branch()], tags
303
292
304
    def compute_tree(self, commit, tree_path='/'):
293
    def compute_tree(self, commit, tree_path='/'):
305
        ctx = self._hg[commit.object_id]
294
        ctx = self._hg[commit._id]
306
        fake_tree = self._tree_from_changectx(ctx)
295
        fake_tree = self._tree_from_changectx(ctx)
307
        fake_tree = fake_tree.get_tree(tree_path)
296
        fake_tree = fake_tree.get_tree(tree_path)
308
        tree, isnew = M.Tree.upsert(fake_tree.hex())
297
        tree, isnew = M.repo.Tree.upsert(fake_tree.hex())
309
        if isnew:
298
        if isnew:
310
            tree.set_context(commit)
299
            tree.set_context(commit)
311
            self._refresh_tree(tree, fake_tree)
300
            self._refresh_tree(tree, fake_tree)
312
        return tree.object_id
301
        return tree._id
313
302
314
    def compute_tree_new(self, commit, tree_path='/'):
303
    def compute_tree_new(self, commit, tree_path='/'):
315
        ctx = self._hg[commit._id]
304
        ctx = self._hg[commit._id]
316
        fake_tree = self._tree_from_changectx(ctx)
305
        fake_tree = self._tree_from_changectx(ctx)
317
        fake_tree = fake_tree.get_tree(tree_path)
306
        fake_tree = fake_tree.get_tree(tree_path)