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 (
27 from bzrlib
.plugins
.fastimport
.reftracker
import (
32 # A sample input stream that only adds files to a branch
38 commit refs/heads/master
40 committer a <b@c> 1234798653 +0000
43 M 644 :1 doc/README.txt
50 commit refs/heads/master
52 committer a <b@c> 1234798653 +0000
67 commit refs/heads/master
69 committer d <b@c> 1234798653 +0000
74 M 644 :3 doc/README.txt
75 M 644 :4 doc/index.txt
78 # A sample input stream that adds files to two branches
84 commit refs/heads/master
86 committer a <b@c> 1234798653 +0000
89 M 644 :1 doc/README.txt
96 commit refs/heads/mybranch
98 committer a <b@c> 1234798653 +0000
113 commit refs/heads/master
115 committer d <b@c> 1234798653 +0000
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
= \
130 commit refs/heads/master
132 committer a <b@c> 1234798653 +0000
135 M 644 :1 doc/README.txt
142 commit refs/heads/mybranch
144 committer a <b@c> 1234798653 +0000
159 commit refs/heads/master
161 committer d <b@c> 1234798653 +0000
166 M 644 :3 doc/README.txt
167 M 644 :4 doc/index.txt
168 commit refs/heads/master
170 committer d <b@c> 1234798653 +0000
179 # A sample input stream that contains a reset
185 commit refs/heads/master
187 committer a <b@c> 1234798653 +0000
190 M 644 :1 doc/README.txt
191 reset refs/remotes/origin/master
195 # A sample input stream that contains a reset and more commits
196 _SAMPLE_RESET_WITH_MORE_COMMITS
= \
201 commit refs/heads/master
203 committer a <b@c> 1234798653 +0000
206 M 644 :1 doc/README.txt
207 reset refs/remotes/origin/master
209 commit refs/remotes/origin/master
211 committer d <b@c> 1234798653 +0000
219 class TestHeadTracking(testtools
.TestCase
):
221 def assertHeads(self
, input, expected
):
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']),