Fix use of bare repositories.
[jelmer/gitpython.git] / test / git / test_repo.py
1 # test_repo.py
2 # Copyright (C) 2008 Michael Trier (mtrier@gmail.com) and contributors
3 #
4 # This module is part of GitPython and is released under
5 # the BSD License: http://www.opensource.org/licenses/bsd-license.php
6
7 import os
8 import time
9 from test.testlib import *
10 from git import *
11
12 class TestRepo(object):
13     def setup(self):
14         self.repo = Repo(GIT_REPO)
15     
16     @raises(InvalidGitRepositoryError)
17     def test_new_should_raise_on_invalid_repo_location(self):
18         Repo("/tmp")
19
20     @raises(NoSuchPathError)
21     def test_new_should_raise_on_non_existant_path(self):
22         Repo("repos/foobar")
23
24     def test_description(self):
25         assert_equal("Unnamed repository; edit this file to name it for gitweb.", self.repo.description)
26
27     def test_heads_should_return_array_of_head_objects(self):
28         for head in self.repo.heads:
29             assert_equal(Head, head.__class__)
30
31     @patch(Git, '_call_process')
32     def test_heads_should_populate_head_data(self, git):
33         git.return_value = fixture('for_each_ref')
34
35         head = self.repo.heads[0]
36         assert_equal('master', head.name)
37         assert_equal('634396b2f541a9f2d58b00be1a07f0c358b999b3', head.commit.id)
38
39         assert_true(git.called)
40         assert_equal(git.call_args, (('for_each_ref', 'refs/heads'), {'sort': 'committerdate', 'format': '%(refname)%00%(objectname)'}))
41
42     @patch(Git, '_call_process')
43     def test_commits(self, git):
44         git.return_value = fixture('rev_list')
45
46         commits = self.repo.commits('master', 10)
47
48         c = commits[0]
49         assert_equal('4c8124ffcf4039d292442eeccabdeca5af5c5017', c.id)
50         assert_equal(["634396b2f541a9f2d58b00be1a07f0c358b999b3"], [p.id for p in c.parents])
51         assert_equal("672eca9b7f9e09c22dcb128c283e8c3c8d7697a4", c.tree.id)
52         assert_equal("Tom Preston-Werner", c.author.name)
53         assert_equal("tom@mojombo.com", c.author.email)
54         assert_equal(time.gmtime(1191999972), c.authored_date)
55         assert_equal("Tom Preston-Werner", c.committer.name)
56         assert_equal("tom@mojombo.com", c.committer.email)
57         assert_equal(time.gmtime(1191999972), c.committed_date)
58         assert_equal("implement Grit#heads", c.message)
59
60         c = commits[1]
61         assert_equal([], c.parents)
62
63         c = commits[2]
64         assert_equal(["6e64c55896aabb9a7d8e9f8f296f426d21a78c2c", "7f874954efb9ba35210445be456c74e037ba6af2"], map(lambda p: p.id, c.parents))
65         assert_equal("Merge branch 'site'", c.message)
66
67         assert_true(git.called)
68         assert_equal(git.call_args, (('rev_list', 'master'), {'skip': 0, 'pretty': 'raw', 'max_count': 10}))
69
70     @patch(Git, '_call_process')
71     def test_commit_count(self, git):
72         git.return_value = fixture('rev_list_count')
73
74         assert_equal(655, self.repo.commit_count('master'))
75
76         assert_true(git.called)
77         assert_equal(git.call_args, (('rev_list', 'master'), {}))
78
79     @patch(Git, '_call_process')
80     def test_commit(self, git):
81         git.return_value = fixture('rev_list_single')
82
83         commit = self.repo.commit('4c8124ffcf4039d292442eeccabdeca5af5c5017')
84
85         assert_equal("4c8124ffcf4039d292442eeccabdeca5af5c5017", commit.id)
86
87         assert_true(git.called)
88         assert_equal(git.call_args, (('rev_list', '4c8124ffcf4039d292442eeccabdeca5af5c5017'), {'pretty': 'raw', 'max_count': 1}))
89
90     @patch(Git, '_call_process')
91     def test_tree(self, git):
92         git.return_value = fixture('ls_tree_a')
93
94         tree = self.repo.tree('master')
95
96         assert_equal(4, len([c for c in tree.contents if isinstance(c, Blob)]))
97         assert_equal(3, len([c for c in tree.contents if isinstance(c, Tree)]))
98
99         assert_true(git.called)
100         assert_equal(git.call_args, (('ls_tree', 'master'), {}))
101
102     @patch(Git, '_call_process')
103     def test_blob(self, git):
104         git.return_value = fixture('cat_file_blob')
105
106         blob = self.repo.blob("abc")
107         assert_equal("Hello world", blob.data)
108
109         assert_true(git.called)
110         assert_equal(git.call_args, (('cat_file', 'abc'), {'p': True, 'with_raw_output': True}))
111
112     @patch(Repo, '__init__')
113     @patch(Git, '_call_process')
114     def test_init_bare(self, repo, git):
115         git.return_value = True
116
117         Repo.init_bare("repos/foo/bar.git")
118
119         assert_true(git.called)
120         assert_equal(git.call_args, (('init',), {}))
121         assert_true(repo.called)
122         assert_equal(repo.call_args, (('repos/foo/bar.git',), {}))
123
124     @patch(Repo, '__init__')
125     @patch(Git, '_call_process')
126     def test_init_bare_with_options(self, repo, git):
127         git.return_value = True
128
129         Repo.init_bare("repos/foo/bar.git", **{'template': "/baz/sweet"})
130
131         assert_true(git.called)
132         assert_equal(git.call_args, (('init',), {'template': '/baz/sweet'}))
133         assert_true(repo.called)
134         assert_equal(repo.call_args, (('repos/foo/bar.git',), {}))
135
136     @patch(Repo, '__init__')
137     @patch(Git, '_call_process')
138     def test_fork_bare(self, repo, git):
139         git.return_value = None
140
141         self.repo.fork_bare("repos/foo/bar.git")
142
143         assert_true(git.called)
144         assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), 'repos/foo/bar.git'), {'bare': True}))
145         assert_true(repo.called)
146
147     @patch(Repo, '__init__')
148     @patch(Git, '_call_process')
149     def test_fork_bare_with_options(self, repo, git):
150         git.return_value = None
151
152         self.repo.fork_bare("repos/foo/bar.git", **{'template': '/awesome'})
153
154         assert_true(git.called)
155         assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), 'repos/foo/bar.git'), 
156                                       {'bare': True, 'template': '/awesome'}))
157         assert_true(repo.called)
158
159     @patch(Git, '_call_process')
160     def test_diff(self, git):
161         self.repo.diff('master^', 'master')
162
163         assert_true(git.called)
164         assert_equal(git.call_args, (('diff', 'master^', 'master', '--'), {}))
165
166         self.repo.diff('master^', 'master', 'foo/bar')
167
168         assert_true(git.called)
169         assert_equal(git.call_args, (('diff', 'master^', 'master', '--', 'foo/bar'), {}))
170
171         self.repo.diff('master^', 'master', 'foo/bar', 'foo/baz')
172
173         assert_true(git.called)
174         assert_equal(git.call_args, (('diff', 'master^', 'master', '--', 'foo/bar', 'foo/baz'), {}))
175
176     @patch(Git, '_call_process')
177     def test_diff(self, git):
178         git.return_value = fixture('diff_p')
179
180         diffs = self.repo.commit_diff('master')
181         assert_equal(15, len(diffs))
182         assert_true(git.called)
183
184     def test_archive_tar(self):
185         self.repo.archive_tar
186
187     def test_archive_tar_gz(self):
188         self.repo.archive_tar_gz
189
190     @patch('git.utils', 'touch')
191     def test_enable_daemon_serve(self, touch):
192         self.repo.enable_daemon_serve
193
194     def test_disable_daemon_serve(self):
195         self.repo.disable_daemon_serve  
196   
197     # @patch(os.path, 'exists')
198     # @patch('__builtin__', 'open')
199     # def test_alternates_with_two_alternates(self, exists, read):
200     #     # File.expects(:exist?).with("#{absolute_project_path}/.git/objects/info/alternates").returns(true)
201     #     # File.expects(:read).returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")        
202     #     exists.return_value = True
203     #     read.return_value = ("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")
204     #     
205     #     assert_equal(["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], self.repo.alternates)
206     #     
207     #     assert_true(exists.called)
208     #     assert_true(read.called)
209     # 
210     @patch(os.path, 'exists')
211     def test_alternates_no_file(self, os):
212         os.return_value = False
213         assert_equal([], self.repo.alternates)
214
215         assert_true(os.called)
216
217     # @patch(os.path, 'exists')
218     # def test_alternates_setter_ok(self, os):
219     #     os.return_value = True
220     #     alts = ['/path/to/repo.git/objects', '/path/to/repo2.git/objects']
221     #     
222     #     # File.any_instance.expects(:write).with(alts.join("\n"))
223     #     
224     #     self.repo.alternates = alts
225     #     
226     #     assert_true(os.called)
227     #     # assert_equal(os.call_args, ((alts,), {}))
228     #     # for alt in alts:
229     #         
230     # @patch(os.path, 'exists')
231     # @raises(NoSuchPathError)
232     # def test_alternates_setter_bad(self, os):
233     #     os.return_value = False
234     #     
235     #     alts = ['/path/to/repo.git/objects']
236     #     # File.any_instance.expects(:write).never
237     #     self.repo.alternates = alts
238     #     
239     #     for alt in alts:
240     #         assert_true(os.called)
241     #         assert_equal(os.call_args, (alt, {}))
242
243     @patch(os, 'remove')
244     def test_alternates_setter_empty(self, os):
245         self.repo.alternates = []
246         assert_true(os.called)
247
248     def test_repr(self):
249         assert_equal('<GitPython.Repo "%s/.git">' % os.path.abspath(GIT_REPO), repr(self.repo))
250
251     @patch(Git, '_call_process')
252     def test_log(self, git):
253         git.return_value = fixture('rev_list')
254         assert_equal('4c8124ffcf4039d292442eeccabdeca5af5c5017', self.repo.log()[0].id)
255         assert_equal('ab25fd8483882c3bda8a458ad2965d2248654335', self.repo.log()[-1].id)
256         assert_true(git.called)
257         assert_equal(git.call_count, 2)
258         assert_equal(git.call_args, (('log', 'master'), {'pretty': 'raw'}))
259
260     @patch(Git, '_call_process')
261     def test_log_with_path_and_options(self, git):
262         git.return_value = fixture('rev_list')
263         self.repo.log('master', 'file.rb', **{'max_count': 1})
264         assert_true(git.called)
265         assert_equal(git.call_args, (('log', 'master', '--', 'file.rb'), {'pretty': 'raw', 'max_count': 1}))
266
267     # @patch(Git, '_call_process')
268     # @patch(Git, '_call_process')
269     # def test_commit_deltas_from_nothing_new(self, gitb, gita):
270     #     gitb.return_value = fixture("rev_list_delta_b")
271     #     gita.return_value = fixture("rev_list_delta_a")
272     #     other_repo = Repo(GIT_REPO)
273     #     # self.repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
274     #     # other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))
275     #     
276     #     delta_commits = self.repo.commit_deltas_from(other_repo)
277     #     assert_equal(0, len(delta_commits))
278     #     assert_true(gitb.called)
279     #     assert_equal(gitb.call_args, (('rev_list', 'master'), {}))
280     #     assert_true(gita.called)
281     #     assert_equal(gita.call_args, (('rev_list', 'master'), {}))
282     #   
283     # def test_commit_deltas_from_when_other_has_new(self):
284     #     other_repo = Repo(GIT_REPO)
285     #     # self.repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))
286     #     # other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
287     #     # for ref in ['4c8124ffcf4039d292442eeccabdeca5af5c5017',
288     #     #             '634396b2f541a9f2d58b00be1a07f0c358b999b3',
289     #     #             'ab25fd8483882c3bda8a458ad2965d2248654335']:
290     #     #     Commit.expects(:find_all).with(other_repo, ref, :max_count => 1).returns([stub()])
291     #     delta_commits = self.repo.commit_deltas_from(other_repo)
292     #     assert_equal(3, len(delta_commits))