/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to commit.py

  • Committer: Robert Collins
  • Date: 2007-07-15 15:40:37 UTC
  • mto: (2592.3.33 repository)
  • mto: This revision was merged to the branch mainline in revision 2624.
  • Revision ID: robertc@robertcollins.net-20070715154037-3ar8g89decddc9su
Make GraphIndex accept nodes as key, value, references, so that the method
signature is closer to what a simple key->value index delivers. Also
change the behaviour when the reference list count is zero to accept
key, value as nodes, and emit key, value to make it identical in that case
to a simple key->value index. This may not be a good idea, but for now it
seems ok.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
 
18
 
"""Support for committing in native Git working trees."""
19
 
 
20
 
 
21
 
import stat
22
 
 
23
 
from bzrlib import (
24
 
    osutils,
25
 
    )
26
 
from bzrlib.repository import (
27
 
    CommitBuilder,
28
 
    )
29
 
 
30
 
from dulwich.objects import (
31
 
    Commit,
32
 
    Tree,
33
 
    )
34
 
 
35
 
 
36
 
class GitCommitBuilder(CommitBuilder):
37
 
 
38
 
    def __init__(self, *args, **kwargs):
39
 
        super(GitCommitBuilder, self).__init__(*args, **kwargs)
40
 
        self._trees = {}
41
 
 
42
 
    def _new_tree(self, path):
43
 
        newtree = Tree()
44
 
        # FIXME: Inherit children from the base revision
45
 
        self._trees[path] = newtree
46
 
        return newtree
47
 
 
48
 
    def _add_tree(self, path):
49
 
        if path in self._trees:
50
 
            return self._trees[path]
51
 
        if path == "":
52
 
            return self._new_tree("")
53
 
        dirname, basename = osutils.split(path)
54
 
        t = self._add_tree(dirname)
55
 
        assert isinstance(basename, str)
56
 
        if not basename in t:
57
 
            newtree = self._new_tree(path)
58
 
            t[basename] = (stat.S_IFDIR, newtree.id)
59
 
            return newtree
60
 
        else:
61
 
            return self.repository._git.object_store[t[basename][1]]
62
 
 
63
 
    def _change_blob(self, path, value):
64
 
        assert isinstance(path, str)
65
 
        dirname, basename = osutils.split(path)
66
 
        t = self._add_tree(dirname)
67
 
        t[basename] = value
68
 
 
69
 
    def record_delete(self, path, file_id):
70
 
        dirname, basename = osutils.split(path)
71
 
        t = self._add_tree(dirname)
72
 
        del t[basename]
73
 
 
74
 
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
75
 
        for (file_id, path, changed_content, versioned, parent, name, kind, 
76
 
             executable) in iter_changes:
77
 
            if kind[1] in ("directory",):
78
 
                if kind[0] in ("file", "symlink"):
79
 
                    self.record_delete(path[0], file_id)
80
 
                continue
81
 
            if kind == "file":
82
 
                mode = stat.S_IFREG
83
 
            else:
84
 
                mode = stat.S_IFLNK
85
 
            if executable:
86
 
                mode |= 0111
87
 
            self._change_blob(path[1].encode("utf-8"), (mode, workingtree.index.get_sha1(path[1].encode("utf-8"))))
88
 
            yield file_id, path, (None, None)
89
 
 
90
 
    def commit(self, message):
91
 
        # FIXME: Eliminate any empty trees recursively
92
 
        # Write any tree objects to disk
93
 
        for path in sorted(self._trees.keys(), reverse=True):
94
 
            self.repository._git.object_store.add_object(self._trees[path])
95
 
        c = Commit()
96
 
        root_tree = self._add_tree("")
97
 
        c.tree = root_tree.id 
98
 
        c.committer = self._committer
99
 
        c.author = self._revprops.get('author', self._committer)
100
 
        c.commit_timestamp = self._timestamp
101
 
        c.author_timestamp = self._timestamp
102
 
        c.commit_timezone = self._timezone
103
 
        c.author_timezone = self._timezone
104
 
        c.message = message.encode("utf-8")
105
 
        self.repository._git.object_store.add_object(c)