The get_ancestry() method was removed from bzr sometime in the past.
[bzr-fastimport.git] / tests / test_head_tracking.py
blob19f6c6819a9b38a0f38cbfb84d69956714850c22
1 # Copyright (C) 2009 Canonical Ltd
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.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Test tracking of heads"""
18 from cStringIO import StringIO
20 from fastimport import (
21 commands,
22 parser,
25 import testtools
27 from bzrlib.plugins.fastimport.reftracker import (
28 RefTracker,
32 # A sample input stream that only adds files to a branch
33 _SAMPLE_MAINLINE = \
34 """blob
35 mark :1
36 data 9
37 Welcome!
38 commit refs/heads/master
39 mark :100
40 committer a <b@c> 1234798653 +0000
41 data 4
42 test
43 M 644 :1 doc/README.txt
44 blob
45 mark :2
46 data 17
47 Life
49 good ...
50 commit refs/heads/master
51 mark :101
52 committer a <b@c> 1234798653 +0000
53 data 8
54 test
55 ing
56 from :100
57 M 644 :2 NEWS
58 blob
59 mark :3
60 data 19
61 Welcome!
62 my friend
63 blob
64 mark :4
65 data 11
66 == Docs ==
67 commit refs/heads/master
68 mark :102
69 committer d <b@c> 1234798653 +0000
70 data 8
71 test
72 ing
73 from :101
74 M 644 :3 doc/README.txt
75 M 644 :4 doc/index.txt
76 """
78 # A sample input stream that adds files to two branches
79 _SAMPLE_TWO_HEADS = \
80 """blob
81 mark :1
82 data 9
83 Welcome!
84 commit refs/heads/master
85 mark :100
86 committer a <b@c> 1234798653 +0000
87 data 4
88 test
89 M 644 :1 doc/README.txt
90 blob
91 mark :2
92 data 17
93 Life
95 good ...
96 commit refs/heads/mybranch
97 mark :101
98 committer a <b@c> 1234798653 +0000
99 data 8
100 test
102 from :100
103 M 644 :2 NEWS
104 blob
105 mark :3
106 data 19
107 Welcome!
108 my friend
109 blob
110 mark :4
111 data 11
112 == Docs ==
113 commit refs/heads/master
114 mark :102
115 committer d <b@c> 1234798653 +0000
116 data 8
117 test
119 from :100
120 M 644 :3 doc/README.txt
121 M 644 :4 doc/index.txt
124 # A sample input stream that adds files to two branches
125 _SAMPLE_TWO_BRANCHES_MERGED = \
126 """blob
127 mark :1
128 data 9
129 Welcome!
130 commit refs/heads/master
131 mark :100
132 committer a <b@c> 1234798653 +0000
133 data 4
134 test
135 M 644 :1 doc/README.txt
136 blob
137 mark :2
138 data 17
139 Life
141 good ...
142 commit refs/heads/mybranch
143 mark :101
144 committer a <b@c> 1234798653 +0000
145 data 8
146 test
148 from :100
149 M 644 :2 NEWS
150 blob
151 mark :3
152 data 19
153 Welcome!
154 my friend
155 blob
156 mark :4
157 data 11
158 == Docs ==
159 commit refs/heads/master
160 mark :102
161 committer d <b@c> 1234798653 +0000
162 data 8
163 test
165 from :100
166 M 644 :3 doc/README.txt
167 M 644 :4 doc/index.txt
168 commit refs/heads/master
169 mark :103
170 committer d <b@c> 1234798653 +0000
171 data 8
172 test
174 from :102
175 merge :101
176 D doc/index.txt
179 # A sample input stream that contains a reset
180 _SAMPLE_RESET = \
181 """blob
182 mark :1
183 data 9
184 Welcome!
185 commit refs/heads/master
186 mark :100
187 committer a <b@c> 1234798653 +0000
188 data 4
189 test
190 M 644 :1 doc/README.txt
191 reset refs/remotes/origin/master
192 from :100
195 # A sample input stream that contains a reset and more commits
196 _SAMPLE_RESET_WITH_MORE_COMMITS = \
197 """blob
198 mark :1
199 data 9
200 Welcome!
201 commit refs/heads/master
202 mark :100
203 committer a <b@c> 1234798653 +0000
204 data 4
205 test
206 M 644 :1 doc/README.txt
207 reset refs/remotes/origin/master
208 from :100
209 commit refs/remotes/origin/master
210 mark :101
211 committer d <b@c> 1234798653 +0000
212 data 8
213 test
215 from :100
216 D doc/README.txt
219 class TestHeadTracking(testtools.TestCase):
221 def assertHeads(self, input, expected):
222 s = StringIO(input)
223 p = parser.ImportParser(s)
224 reftracker = RefTracker()
225 for cmd in p.iter_commands():
226 if isinstance(cmd, commands.CommitCommand):
227 reftracker.track_heads(cmd)
228 # eat the file commands
229 list(cmd.iter_files())
230 elif isinstance(cmd, commands.ResetCommand):
231 if cmd.from_ is not None:
232 reftracker.track_heads_for_ref(cmd.ref, cmd.from_)
233 self.assertEqual(reftracker.heads, expected)
235 def test_mainline(self):
236 self.assertHeads(_SAMPLE_MAINLINE, {
237 ':102': set(['refs/heads/master']),
240 def test_two_heads(self):
241 self.assertHeads(_SAMPLE_TWO_HEADS, {
242 ':101': set(['refs/heads/mybranch']),
243 ':102': set(['refs/heads/master']),
246 def test_two_branches_merged(self):
247 self.assertHeads(_SAMPLE_TWO_BRANCHES_MERGED, {
248 ':103': set(['refs/heads/master']),
251 def test_reset(self):
252 self.assertHeads(_SAMPLE_RESET, {
253 ':100': set(['refs/heads/master', 'refs/remotes/origin/master']),
256 def test_reset_with_more_commits(self):
257 self.assertHeads(_SAMPLE_RESET_WITH_MORE_COMMITS, {
258 ':101': set(['refs/remotes/origin/master']),