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