summaryrefslogtreecommitdiff
path: root/tests/test_head_tracking.py
blob: 19f6c6819a9b38a0f38cbfb84d69956714850c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# Copyright (C) 2009 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Test tracking of heads"""

from cStringIO import StringIO

from fastimport import (
    commands,
    parser,
    )

import testtools

from bzrlib.plugins.fastimport.reftracker import (
    RefTracker,
    )


# A sample input stream that only adds files to a branch
_SAMPLE_MAINLINE = \
"""blob
mark :1
data 9
Welcome!
commit refs/heads/master
mark :100
committer a <b@c> 1234798653 +0000
data 4
test
M 644 :1 doc/README.txt
blob
mark :2
data 17
Life
is
good ...
commit refs/heads/master
mark :101
committer a <b@c> 1234798653 +0000
data 8
test
ing
from :100
M 644 :2 NEWS
blob
mark :3
data 19
Welcome!
my friend
blob
mark :4
data 11
== Docs ==
commit refs/heads/master
mark :102
committer d <b@c> 1234798653 +0000
data 8
test
ing
from :101
M 644 :3 doc/README.txt
M 644 :4 doc/index.txt
"""

# A sample input stream that adds files to two branches
_SAMPLE_TWO_HEADS = \
"""blob
mark :1
data 9
Welcome!
commit refs/heads/master
mark :100
committer a <b@c> 1234798653 +0000
data 4
test
M 644 :1 doc/README.txt
blob
mark :2
data 17
Life
is
good ...
commit refs/heads/mybranch
mark :101
committer a <b@c> 1234798653 +0000
data 8
test
ing
from :100
M 644 :2 NEWS
blob
mark :3
data 19
Welcome!
my friend
blob
mark :4
data 11
== Docs ==
commit refs/heads/master
mark :102
committer d <b@c> 1234798653 +0000
data 8
test
ing
from :100
M 644 :3 doc/README.txt
M 644 :4 doc/index.txt
"""

# A sample input stream that adds files to two branches
_SAMPLE_TWO_BRANCHES_MERGED = \
"""blob
mark :1
data 9
Welcome!
commit refs/heads/master
mark :100
committer a <b@c> 1234798653 +0000
data 4
test
M 644 :1 doc/README.txt
blob
mark :2
data 17
Life
is
good ...
commit refs/heads/mybranch
mark :101
committer a <b@c> 1234798653 +0000
data 8
test
ing
from :100
M 644 :2 NEWS
blob
mark :3
data 19
Welcome!
my friend
blob
mark :4
data 11
== Docs ==
commit refs/heads/master
mark :102
committer d <b@c> 1234798653 +0000
data 8
test
ing
from :100
M 644 :3 doc/README.txt
M 644 :4 doc/index.txt
commit refs/heads/master
mark :103
committer d <b@c> 1234798653 +0000
data 8
test
ing
from :102
merge :101
D doc/index.txt
"""

# A sample input stream that contains a reset
_SAMPLE_RESET = \
"""blob
mark :1
data 9
Welcome!
commit refs/heads/master
mark :100
committer a <b@c> 1234798653 +0000
data 4
test
M 644 :1 doc/README.txt
reset refs/remotes/origin/master
from :100
"""

# A sample input stream that contains a reset and more commits
_SAMPLE_RESET_WITH_MORE_COMMITS = \
"""blob
mark :1
data 9
Welcome!
commit refs/heads/master
mark :100
committer a <b@c> 1234798653 +0000
data 4
test
M 644 :1 doc/README.txt
reset refs/remotes/origin/master
from :100
commit refs/remotes/origin/master
mark :101
committer d <b@c> 1234798653 +0000
data 8
test
ing
from :100
D doc/README.txt
"""

class TestHeadTracking(testtools.TestCase):

    def assertHeads(self, input, expected):
        s = StringIO(input)
        p = parser.ImportParser(s)
        reftracker = RefTracker()
        for cmd in p.iter_commands():
            if isinstance(cmd, commands.CommitCommand):
                reftracker.track_heads(cmd)
                # eat the file commands
                list(cmd.iter_files())
            elif isinstance(cmd, commands.ResetCommand):
                if cmd.from_ is not None:
                    reftracker.track_heads_for_ref(cmd.ref, cmd.from_)
        self.assertEqual(reftracker.heads, expected)

    def test_mainline(self):
        self.assertHeads(_SAMPLE_MAINLINE, {
            ':102': set(['refs/heads/master']),
            })

    def test_two_heads(self):
        self.assertHeads(_SAMPLE_TWO_HEADS, {
            ':101': set(['refs/heads/mybranch']),
            ':102': set(['refs/heads/master']),
            })

    def test_two_branches_merged(self):
        self.assertHeads(_SAMPLE_TWO_BRANCHES_MERGED, {
            ':103': set(['refs/heads/master']),
            })

    def test_reset(self):
        self.assertHeads(_SAMPLE_RESET, {
            ':100': set(['refs/heads/master', 'refs/remotes/origin/master']),
            })

    def test_reset_with_more_commits(self):
        self.assertHeads(_SAMPLE_RESET_WITH_MORE_COMMITS, {
            ':101': set(['refs/remotes/origin/master']),
            })