Merge John.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_repository.py
1 # test_repository.py -- tests for repository.py
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # of the License or (at your option) any later version of 
8 # the License.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 import os
21 import unittest
22
23 from dulwich import errors
24 from dulwich.repo import Repo
25
26 missing_sha = 'b91fa4d900g17e99b433218e988c4eb4a3e9a097'
27
28 class RepositoryTests(unittest.TestCase):
29
30   def open_repo(self, name):
31     return Repo(os.path.join(os.path.dirname(__file__),
32                       'data/repos', name, '.git'))
33
34   def test_simple_props(self):
35     r = self.open_repo('a')
36     basedir = os.path.join(os.path.dirname(__file__), 'data/repos/a/.git')
37     self.assertEqual(r.controldir(), basedir)
38     self.assertEqual(r.object_dir(), os.path.join(basedir, 'objects'))
39
40   def test_ref(self):
41     r = self.open_repo('a')
42     self.assertEqual(r.ref('master'),
43                      'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
44
45   def test_get_refs(self):
46     r = self.open_repo('a')
47     self.assertEquals({
48         'HEAD': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097', 
49         'refs/heads/master': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097'
50         }, r.get_refs())
51
52   def test_head(self):
53     r = self.open_repo('a')
54     self.assertEqual(r.head(), 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
55
56   def test_get_object(self):
57     r = self.open_repo('a')
58     obj = r.get_object(r.head())
59     self.assertEqual(obj._type, 'commit')
60
61   def test_get_object_non_existant(self):
62     r = self.open_repo('a')
63     self.assertRaises(KeyError, r.get_object, missing_sha)
64
65   def test_commit(self):
66     r = self.open_repo('a')
67     obj = r.commit(r.head())
68     self.assertEqual(obj._type, 'commit')
69
70   def test_commit_not_commit(self):
71     r = self.open_repo('a')
72     self.assertRaises(errors.NotCommitError,
73                       r.commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
74
75   def test_tree(self):
76     r = self.open_repo('a')
77     commit = r.commit(r.head())
78     tree = r.tree(commit.tree)
79     self.assertEqual(tree._type, 'tree')
80     self.assertEqual(tree.sha().hexdigest(), commit.tree)
81
82   def test_tree_not_tree(self):
83     r = self.open_repo('a')
84     self.assertRaises(errors.NotTreeError, r.tree, r.head())
85
86   def test_get_blob(self):
87     r = self.open_repo('a')
88     commit = r.commit(r.head())
89     tree = r.tree(commit.tree())
90     blob_sha = tree.entries()[0][2]
91     blob = r.get_blob(blob_sha)
92     self.assertEqual(blob._type, 'blob')
93     self.assertEqual(blob.sha().hexdigest(), blob_sha)
94
95   def test_get_blob(self):
96     r = self.open_repo('a')
97     self.assertRaises(errors.NotBlobError, r.get_blob, r.head())
98
99   def test_linear_history(self):
100     r = self.open_repo('a')
101     history = r.revision_history(r.head())
102     shas = [c.sha().hexdigest() for c in history]
103     self.assertEqual(shas, [r.head(),
104                             '2a72d929692c41d8554c07f6301757ba18a65d91'])
105
106   def test_merge_history(self):
107     r = self.open_repo('simple_merge')
108     history = r.revision_history(r.head())
109     shas = [c.sha().hexdigest() for c in history]
110     self.assertEqual(shas, ['5dac377bdded4c9aeb8dff595f0faeebcc8498cc',
111                             'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
112                             '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6',
113                             '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
114                             '0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
115
116   def test_revision_history_missing_commit(self):
117     r = self.open_repo('simple_merge')
118     self.assertRaises(errors.MissingCommitError, r.revision_history,
119                       missing_sha)
120
121   def test_out_of_order_merge(self):
122     """Test that revision history is ordered by date, not parent order."""
123     r = self.open_repo('ooo_merge')
124     history = r.revision_history(r.head())
125     shas = [c.sha().hexdigest() for c in history]
126     self.assertEqual(shas, ['7601d7f6231db6a57f7bbb79ee52e4d462fd44d1',
127                             'f507291b64138b875c28e03469025b1ea20bc614',
128                             'fb5b0425c7ce46959bec94d54b9a157645e114f5',
129                             'f9e39b120c68182a4ba35349f832d0e4e61f485c'])
130
131   def test_get_tags_empty(self):
132    r = self.open_repo('ooo_merge')
133    self.assertEquals({}, r.get_tags())