Show More
@@ -1,193 +1,194 | |||||
1 | import time |
|
1 | import time | |
2 | import datetime |
|
2 | import datetime | |
3 | import posixpath |
|
3 | import posixpath | |
4 | from dulwich import objects |
|
4 | from dulwich import objects | |
5 | from dulwich.repo import Repo |
|
5 | from dulwich.repo import Repo | |
6 | from rhodecode.lib.vcs.backends.base import BaseInMemoryChangeset |
|
6 | from rhodecode.lib.vcs.backends.base import BaseInMemoryChangeset | |
7 | from rhodecode.lib.vcs.exceptions import RepositoryError |
|
7 | from rhodecode.lib.vcs.exceptions import RepositoryError | |
8 | from rhodecode.lib.vcs.utils import safe_str |
|
8 | from rhodecode.lib.vcs.utils import safe_str | |
9 |
|
9 | |||
10 |
|
10 | |||
11 | class GitInMemoryChangeset(BaseInMemoryChangeset): |
|
11 | class GitInMemoryChangeset(BaseInMemoryChangeset): | |
12 |
|
12 | |||
13 | def commit(self, message, author, parents=None, branch=None, date=None, |
|
13 | def commit(self, message, author, parents=None, branch=None, date=None, | |
14 | **kwargs): |
|
14 | **kwargs): | |
15 | """ |
|
15 | """ | |
16 | Performs in-memory commit (doesn't check workdir in any way) and |
|
16 | Performs in-memory commit (doesn't check workdir in any way) and | |
17 | returns newly created ``Changeset``. Updates repository's |
|
17 | returns newly created ``Changeset``. Updates repository's | |
18 | ``revisions``. |
|
18 | ``revisions``. | |
19 |
|
19 | |||
20 | :param message: message of the commit |
|
20 | :param message: message of the commit | |
21 | :param author: full username, i.e. "Joe Doe <joe.doe@example.com>" |
|
21 | :param author: full username, i.e. "Joe Doe <joe.doe@example.com>" | |
22 | :param parents: single parent or sequence of parents from which commit |
|
22 | :param parents: single parent or sequence of parents from which commit | |
23 | would be derieved |
|
23 | would be derieved | |
24 | :param date: ``datetime.datetime`` instance. Defaults to |
|
24 | :param date: ``datetime.datetime`` instance. Defaults to | |
25 | ``datetime.datetime.now()``. |
|
25 | ``datetime.datetime.now()``. | |
26 | :param branch: branch name, as string. If none given, default backend's |
|
26 | :param branch: branch name, as string. If none given, default backend's | |
27 | branch would be used. |
|
27 | branch would be used. | |
28 |
|
28 | |||
29 | :raises ``CommitError``: if any error occurs while committing |
|
29 | :raises ``CommitError``: if any error occurs while committing | |
30 | """ |
|
30 | """ | |
31 | self.check_integrity(parents) |
|
31 | self.check_integrity(parents) | |
32 |
|
32 | |||
33 | from .repository import GitRepository |
|
33 | from .repository import GitRepository | |
34 | if branch is None: |
|
34 | if branch is None: | |
35 | branch = GitRepository.DEFAULT_BRANCH_NAME |
|
35 | branch = GitRepository.DEFAULT_BRANCH_NAME | |
36 |
|
36 | |||
37 | repo = self.repository._repo |
|
37 | repo = self.repository._repo | |
38 | object_store = repo.object_store |
|
38 | object_store = repo.object_store | |
39 |
|
39 | |||
40 | ENCODING = "UTF-8" |
|
40 | ENCODING = "UTF-8" | |
41 | DIRMOD = 040000 |
|
41 | DIRMOD = 040000 | |
42 |
|
42 | |||
43 | # Create tree and populates it with blobs |
|
43 | # Create tree and populates it with blobs | |
44 | commit_tree = self.parents[0] and repo[self.parents[0]._commit.tree] or\ |
|
44 | commit_tree = self.parents[0] and repo[self.parents[0]._commit.tree] or\ | |
45 | objects.Tree() |
|
45 | objects.Tree() | |
46 | for node in self.added + self.changed: |
|
46 | for node in self.added + self.changed: | |
47 | # Compute subdirs if needed |
|
47 | # Compute subdirs if needed | |
48 | dirpath, nodename = posixpath.split(node.path) |
|
48 | dirpath, nodename = posixpath.split(node.path) | |
49 | dirnames = dirpath and dirpath.split('/') or [] |
|
49 | dirnames = dirpath and dirpath.split('/') or [] | |
50 | parent = commit_tree |
|
50 | parent = commit_tree | |
51 | ancestors = [('', parent)] |
|
51 | ancestors = [('', parent)] | |
52 |
|
52 | |||
53 | # Tries to dig for the deepest existing tree |
|
53 | # Tries to dig for the deepest existing tree | |
54 | while dirnames: |
|
54 | while dirnames: | |
55 | curdir = dirnames.pop(0) |
|
55 | curdir = dirnames.pop(0) | |
56 | try: |
|
56 | try: | |
57 | dir_id = parent[curdir][1] |
|
57 | dir_id = parent[curdir][1] | |
58 | except KeyError: |
|
58 | except KeyError: | |
59 | # put curdir back into dirnames and stops |
|
59 | # put curdir back into dirnames and stops | |
60 | dirnames.insert(0, curdir) |
|
60 | dirnames.insert(0, curdir) | |
61 | break |
|
61 | break | |
62 | else: |
|
62 | else: | |
63 | # If found, updates parent |
|
63 | # If found, updates parent | |
64 | parent = self.repository._repo[dir_id] |
|
64 | parent = self.repository._repo[dir_id] | |
65 | ancestors.append((curdir, parent)) |
|
65 | ancestors.append((curdir, parent)) | |
66 | # Now parent is deepest exising tree and we need to create subtrees |
|
66 | # Now parent is deepest exising tree and we need to create subtrees | |
67 | # for dirnames (in reverse order) [this only applies for nodes from added] |
|
67 | # for dirnames (in reverse order) [this only applies for nodes from added] | |
68 | new_trees = [] |
|
68 | new_trees = [] | |
69 | blob = objects.Blob.from_string(node.content.encode(ENCODING)) |
|
69 | blob = objects.Blob.from_string(node.content.encode(ENCODING)) | |
70 | node_path = node.name.encode(ENCODING) |
|
70 | node_path = node.name.encode(ENCODING) | |
71 | if dirnames: |
|
71 | if dirnames: | |
72 | # If there are trees which should be created we need to build |
|
72 | # If there are trees which should be created we need to build | |
73 | # them now (in reverse order) |
|
73 | # them now (in reverse order) | |
74 | reversed_dirnames = list(reversed(dirnames)) |
|
74 | reversed_dirnames = list(reversed(dirnames)) | |
75 | curtree = objects.Tree() |
|
75 | curtree = objects.Tree() | |
76 | curtree[node_path] = node.mode, blob.id |
|
76 | curtree[node_path] = node.mode, blob.id | |
77 | new_trees.append(curtree) |
|
77 | new_trees.append(curtree) | |
78 | for dirname in reversed_dirnames[:-1]: |
|
78 | for dirname in reversed_dirnames[:-1]: | |
79 | newtree = objects.Tree() |
|
79 | newtree = objects.Tree() | |
80 | #newtree.add(DIRMOD, dirname, curtree.id) |
|
80 | #newtree.add(DIRMOD, dirname, curtree.id) | |
81 | newtree[dirname] = DIRMOD, curtree.id |
|
81 | newtree[dirname] = DIRMOD, curtree.id | |
82 | new_trees.append(newtree) |
|
82 | new_trees.append(newtree) | |
83 | curtree = newtree |
|
83 | curtree = newtree | |
84 | parent[reversed_dirnames[-1]] = DIRMOD, curtree.id |
|
84 | parent[reversed_dirnames[-1]] = DIRMOD, curtree.id | |
85 | else: |
|
85 | else: | |
86 |
parent.add(node.mode, |
|
86 | parent.add(name=node_path, mode=node.mode, hexsha=blob.id) | |
|
87 | ||||
87 | new_trees.append(parent) |
|
88 | new_trees.append(parent) | |
88 | # Update ancestors |
|
89 | # Update ancestors | |
89 | for parent, tree, path in reversed([(a[1], b[1], b[0]) for a, b in |
|
90 | for parent, tree, path in reversed([(a[1], b[1], b[0]) for a, b in | |
90 | zip(ancestors, ancestors[1:])]): |
|
91 | zip(ancestors, ancestors[1:])]): | |
91 | parent[path] = DIRMOD, tree.id |
|
92 | parent[path] = DIRMOD, tree.id | |
92 | object_store.add_object(tree) |
|
93 | object_store.add_object(tree) | |
93 |
|
94 | |||
94 | object_store.add_object(blob) |
|
95 | object_store.add_object(blob) | |
95 | for tree in new_trees: |
|
96 | for tree in new_trees: | |
96 | object_store.add_object(tree) |
|
97 | object_store.add_object(tree) | |
97 | for node in self.removed: |
|
98 | for node in self.removed: | |
98 | paths = node.path.split('/') |
|
99 | paths = node.path.split('/') | |
99 | tree = commit_tree |
|
100 | tree = commit_tree | |
100 | trees = [tree] |
|
101 | trees = [tree] | |
101 | # Traverse deep into the forest... |
|
102 | # Traverse deep into the forest... | |
102 | for path in paths: |
|
103 | for path in paths: | |
103 | try: |
|
104 | try: | |
104 | obj = self.repository._repo[tree[path][1]] |
|
105 | obj = self.repository._repo[tree[path][1]] | |
105 | if isinstance(obj, objects.Tree): |
|
106 | if isinstance(obj, objects.Tree): | |
106 | trees.append(obj) |
|
107 | trees.append(obj) | |
107 | tree = obj |
|
108 | tree = obj | |
108 | except KeyError: |
|
109 | except KeyError: | |
109 | break |
|
110 | break | |
110 | # Cut down the blob and all rotten trees on the way back... |
|
111 | # Cut down the blob and all rotten trees on the way back... | |
111 | for path, tree in reversed(zip(paths, trees)): |
|
112 | for path, tree in reversed(zip(paths, trees)): | |
112 | del tree[path] |
|
113 | del tree[path] | |
113 | if tree: |
|
114 | if tree: | |
114 | # This tree still has elements - don't remove it or any |
|
115 | # This tree still has elements - don't remove it or any | |
115 | # of it's parents |
|
116 | # of it's parents | |
116 | break |
|
117 | break | |
117 |
|
118 | |||
118 | object_store.add_object(commit_tree) |
|
119 | object_store.add_object(commit_tree) | |
119 |
|
120 | |||
120 | # Create commit |
|
121 | # Create commit | |
121 | commit = objects.Commit() |
|
122 | commit = objects.Commit() | |
122 | commit.tree = commit_tree.id |
|
123 | commit.tree = commit_tree.id | |
123 | commit.parents = [p._commit.id for p in self.parents if p] |
|
124 | commit.parents = [p._commit.id for p in self.parents if p] | |
124 | commit.author = commit.committer = safe_str(author) |
|
125 | commit.author = commit.committer = safe_str(author) | |
125 | commit.encoding = ENCODING |
|
126 | commit.encoding = ENCODING | |
126 |
commit.message = safe_str(message) |
|
127 | commit.message = safe_str(message) | |
127 |
|
128 | |||
128 | # Compute date |
|
129 | # Compute date | |
129 | if date is None: |
|
130 | if date is None: | |
130 | date = time.time() |
|
131 | date = time.time() | |
131 | elif isinstance(date, datetime.datetime): |
|
132 | elif isinstance(date, datetime.datetime): | |
132 | date = time.mktime(date.timetuple()) |
|
133 | date = time.mktime(date.timetuple()) | |
133 |
|
134 | |||
134 | author_time = kwargs.pop('author_time', date) |
|
135 | author_time = kwargs.pop('author_time', date) | |
135 | commit.commit_time = int(date) |
|
136 | commit.commit_time = int(date) | |
136 | commit.author_time = int(author_time) |
|
137 | commit.author_time = int(author_time) | |
137 | tz = time.timezone |
|
138 | tz = time.timezone | |
138 | author_tz = kwargs.pop('author_timezone', tz) |
|
139 | author_tz = kwargs.pop('author_timezone', tz) | |
139 | commit.commit_timezone = tz |
|
140 | commit.commit_timezone = tz | |
140 | commit.author_timezone = author_tz |
|
141 | commit.author_timezone = author_tz | |
141 |
|
142 | |||
142 | object_store.add_object(commit) |
|
143 | object_store.add_object(commit) | |
143 |
|
144 | |||
144 | ref = 'refs/heads/%s' % branch |
|
145 | ref = 'refs/heads/%s' % branch | |
145 | repo.refs[ref] = commit.id |
|
146 | repo.refs[ref] = commit.id | |
146 | repo.refs.set_symbolic_ref('HEAD', ref) |
|
147 | repo.refs.set_symbolic_ref('HEAD', ref) | |
147 |
|
148 | |||
148 | # Update vcs repository object & recreate dulwich repo |
|
149 | # Update vcs repository object & recreate dulwich repo | |
149 | self.repository.revisions.append(commit.id) |
|
150 | self.repository.revisions.append(commit.id) | |
150 | self.repository._repo = Repo(self.repository.path) |
|
151 | self.repository._repo = Repo(self.repository.path) | |
151 | tip = self.repository.get_changeset() |
|
152 | tip = self.repository.get_changeset() | |
152 | self.reset() |
|
153 | self.reset() | |
153 | return tip |
|
154 | return tip | |
154 |
|
155 | |||
155 | def _get_missing_trees(self, path, root_tree): |
|
156 | def _get_missing_trees(self, path, root_tree): | |
156 | """ |
|
157 | """ | |
157 | Creates missing ``Tree`` objects for the given path. |
|
158 | Creates missing ``Tree`` objects for the given path. | |
158 |
|
159 | |||
159 | :param path: path given as a string. It may be a path to a file node |
|
160 | :param path: path given as a string. It may be a path to a file node | |
160 | (i.e. ``foo/bar/baz.txt``) or directory path - in that case it must |
|
161 | (i.e. ``foo/bar/baz.txt``) or directory path - in that case it must | |
161 | end with slash (i.e. ``foo/bar/``). |
|
162 | end with slash (i.e. ``foo/bar/``). | |
162 | :param root_tree: ``dulwich.objects.Tree`` object from which we start |
|
163 | :param root_tree: ``dulwich.objects.Tree`` object from which we start | |
163 | traversing (should be commit's root tree) |
|
164 | traversing (should be commit's root tree) | |
164 | """ |
|
165 | """ | |
165 | dirpath = posixpath.split(path)[0] |
|
166 | dirpath = posixpath.split(path)[0] | |
166 | dirs = dirpath.split('/') |
|
167 | dirs = dirpath.split('/') | |
167 | if not dirs or dirs == ['']: |
|
168 | if not dirs or dirs == ['']: | |
168 | return [] |
|
169 | return [] | |
169 |
|
170 | |||
170 | def get_tree_for_dir(tree, dirname): |
|
171 | def get_tree_for_dir(tree, dirname): | |
171 | for name, mode, id in tree.iteritems(): |
|
172 | for name, mode, id in tree.iteritems(): | |
172 | if name == dirname: |
|
173 | if name == dirname: | |
173 | obj = self.repository._repo[id] |
|
174 | obj = self.repository._repo[id] | |
174 | if isinstance(obj, objects.Tree): |
|
175 | if isinstance(obj, objects.Tree): | |
175 | return obj |
|
176 | return obj | |
176 | else: |
|
177 | else: | |
177 | raise RepositoryError("Cannot create directory %s " |
|
178 | raise RepositoryError("Cannot create directory %s " | |
178 | "at tree %s as path is occupied and is not a " |
|
179 | "at tree %s as path is occupied and is not a " | |
179 | "Tree" % (dirname, tree)) |
|
180 | "Tree" % (dirname, tree)) | |
180 | return None |
|
181 | return None | |
181 |
|
182 | |||
182 | trees = [] |
|
183 | trees = [] | |
183 | parent = root_tree |
|
184 | parent = root_tree | |
184 | for dirname in dirs: |
|
185 | for dirname in dirs: | |
185 | tree = get_tree_for_dir(parent, dirname) |
|
186 | tree = get_tree_for_dir(parent, dirname) | |
186 | if tree is None: |
|
187 | if tree is None: | |
187 | tree = objects.Tree() |
|
188 | tree = objects.Tree() | |
188 | dirmode = 040000 |
|
189 | dirmode = 040000 | |
189 | parent.add(dirmode, dirname, tree.id) |
|
190 | parent.add(dirmode, dirname, tree.id) | |
190 | parent = tree |
|
191 | parent = tree | |
191 | # Always append tree |
|
192 | # Always append tree | |
192 | trees.append(tree) |
|
193 | trees.append(tree) | |
193 | return trees |
|
194 | return trees |
General Comments 0
You need to be logged in to leave comments.
Login now