1
 
# Copyright (C) 2007 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, write to the Free Software
 
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
"""Test our ability to build up test repositories"""
 
19
 
from cStringIO import StringIO
 
21
 
from bzrlib.plugins.git import tests
 
24
 
class TestGitBranchBuilder(tests.TestCase):
 
26
 
    def test__create_blob(self):
 
28
 
        builder = tests.GitBranchBuilder(stream)
 
29
 
        self.assertEqual(1, builder._create_blob('foo\nbar\n'))
 
30
 
        self.assertEqualDiff('blob\nmark :1\ndata 8\nfoo\nbar\n\n',
 
33
 
    def test_set_file(self):
 
35
 
        builder = tests.GitBranchBuilder(stream)
 
36
 
        builder.set_file('foobar', 'foo\nbar\n', False)
 
37
 
        self.assertEqualDiff('blob\nmark :1\ndata 8\nfoo\nbar\n\n',
 
39
 
        self.assertEqual(['M 100644 :1 foobar\n'], builder.commit_info)
 
41
 
    def test_set_file_unicode(self):
 
43
 
        builder = tests.GitBranchBuilder(stream)
 
44
 
        builder.set_file(u'f\xb5/bar', 'contents\nbar\n', False)
 
45
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n',
 
47
 
        self.assertEqual(['M 100644 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
49
 
    def test_set_file_newline(self):
 
51
 
        builder = tests.GitBranchBuilder(stream)
 
52
 
        builder.set_file(u'foo\nbar', 'contents\nbar\n', False)
 
53
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n',
 
55
 
        self.assertEqual(['M 100644 :1 "foo\\nbar"\n'], builder.commit_info)
 
57
 
    def test_set_file_executable(self):
 
59
 
        builder = tests.GitBranchBuilder(stream)
 
60
 
        builder.set_file(u'f\xb5/bar', 'contents\nbar\n', True)
 
61
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n',
 
63
 
        self.assertEqual(['M 100755 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
65
 
    def test_set_link(self):
 
67
 
        builder = tests.GitBranchBuilder(stream)
 
68
 
        builder.set_link(u'f\xb5/bar', 'link/contents')
 
69
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\nlink/contents\n',
 
71
 
        self.assertEqual(['M 120000 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
73
 
    def test_set_link_newline(self):
 
75
 
        builder = tests.GitBranchBuilder(stream)
 
76
 
        builder.set_link(u'foo\nbar', 'link/contents')
 
77
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\nlink/contents\n',
 
79
 
        self.assertEqual(['M 120000 :1 "foo\\nbar"\n'], builder.commit_info)
 
81
 
    def test_delete_entry(self):
 
83
 
        builder = tests.GitBranchBuilder(stream)
 
84
 
        builder.delete_entry(u'path/to/f\xb5')
 
85
 
        self.assertEqual(['D path/to/f\xc2\xb5\n'], builder.commit_info)
 
87
 
    def test_delete_entry_newline(self):
 
89
 
        builder = tests.GitBranchBuilder(stream)
 
90
 
        builder.delete_entry(u'path/to/foo\nbar')
 
91
 
        self.assertEqual(['D "path/to/foo\\nbar"\n'], builder.commit_info)
 
93
 
    def test_encode_path(self):
 
94
 
        encode = tests.GitBranchBuilder._encode_path
 
95
 
        # Unicode is encoded to utf-8
 
96
 
        self.assertEqual(encode(u'f\xb5'), 'f\xc2\xb5')
 
97
 
        # The name must be quoted if it starts by a double quote or contains a
 
99
 
        self.assertEqual(encode(u'"foo'), '"\\"foo"')
 
100
 
        self.assertEqual(encode(u'fo\no'), '"fo\\no"')
 
101
 
        # When the name is quoted, all backslash and quote chars must be
 
103
 
        self.assertEqual(encode(u'fo\\o\nbar'), '"fo\\\\o\\nbar"')
 
104
 
        self.assertEqual(encode(u'fo"o"\nbar'), '"fo\\"o\\"\\nbar"')
 
105
 
        # Other control chars, such as \r, need not be escaped.
 
106
 
        self.assertEqual(encode(u'foo\r\nbar'), '"foo\r\\nbar"')
 
108
 
    def test_add_and_commit(self):
 
110
 
        builder = tests.GitBranchBuilder(stream)
 
112
 
        builder.set_file(u'f\xb5/bar', 'contents\nbar\n', False)
 
113
 
        self.assertEqual(2, builder.commit('Joe Foo <joe@foo.com>',
 
114
 
                                           u'committing f\xb5/bar',
 
115
 
                                           timestamp=1194586400,
 
117
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n'
 
118
 
                             'commit refs/heads/master\n'
 
120
 
                             'committer Joe Foo <joe@foo.com> 1194586400 +0100\n'
 
122
 
                             'committing f\xc2\xb5/bar'
 
124
 
                             'M 100644 :1 f\xc2\xb5/bar\n'
 
128
 
    def test_commit_base(self):
 
130
 
        builder = tests.GitBranchBuilder(stream)
 
132
 
        builder.set_file(u'foo', 'contents\nfoo\n', False)
 
133
 
        r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
 
134
 
                            timestamp=1194586400)
 
135
 
        r2 = builder.commit('Joe Foo <joe@foo.com>', u'second',
 
136
 
                            timestamp=1194586405)
 
137
 
        r3 = builder.commit('Joe Foo <joe@foo.com>', u'third',
 
138
 
                            timestamp=1194586410,
 
141
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
142
 
                             'commit refs/heads/master\n'
 
144
 
                             'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
150
 
                             'commit refs/heads/master\n'
 
152
 
                             'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
157
 
                             'commit refs/heads/master\n'
 
159
 
                             'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
164
 
                             '\n', stream.getvalue())
 
166
 
    def test_commit_merge(self):
 
168
 
        builder = tests.GitBranchBuilder(stream)
 
170
 
        builder.set_file(u'foo', 'contents\nfoo\n', False)
 
171
 
        r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
 
172
 
                            timestamp=1194586400)
 
173
 
        r2 = builder.commit('Joe Foo <joe@foo.com>', u'second',
 
174
 
                            timestamp=1194586405)
 
175
 
        r3 = builder.commit('Joe Foo <joe@foo.com>', u'third',
 
176
 
                            timestamp=1194586410,
 
178
 
        r4 = builder.commit('Joe Foo <joe@foo.com>', u'Merge',
 
179
 
                            timestamp=1194586415,
 
182
 
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
183
 
                             'commit refs/heads/master\n'
 
185
 
                             'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
191
 
                             'commit refs/heads/master\n'
 
193
 
                             'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
198
 
                             'commit refs/heads/master\n'
 
200
 
                             'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
206
 
                             'commit refs/heads/master\n'
 
208
 
                             'committer Joe Foo <joe@foo.com> 1194586415 +0000\n'
 
213
 
                             '\n', stream.getvalue())
 
215
 
    def test_auto_timestamp(self):
 
217
 
        builder = tests.GitBranchBuilder(stream)
 
218
 
        builder.commit('Joe Foo <joe@foo.com>', u'message')
 
219
 
        self.assertContainsRe(stream.getvalue(),
 
220
 
                              r'committer Joe Foo <joe@foo\.com> \d+ \+0000')
 
222
 
    def test_reset(self):
 
224
 
        builder = tests.GitBranchBuilder(stream)
 
226
 
        self.assertEqualDiff('reset refs/heads/master\n\n', stream.getvalue())
 
228
 
    def test_reset_named_ref(self):
 
230
 
        builder = tests.GitBranchBuilder(stream)
 
231
 
        builder.reset('refs/heads/branch')
 
232
 
        self.assertEqualDiff('reset refs/heads/branch\n\n', stream.getvalue())
 
234
 
    def test_reset_revision(self):
 
236
 
        builder = tests.GitBranchBuilder(stream)
 
237
 
        builder.reset(mark=123)
 
238
 
        self.assertEqualDiff(
 
239
 
            'reset refs/heads/master\n'
 
241
 
            '\n', stream.getvalue())
 
244
 
class TestGitBranchBuilderReal(tests.TestCaseInTempDir):
 
246
 
    def test_create_real_branch(self):
 
247
 
        tests.run_git('init')
 
249
 
        builder = tests.GitBranchBuilder()
 
250
 
        builder.set_file(u'foo', 'contents\nfoo\n', False)
 
251
 
        r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
 
252
 
                            timestamp=1194586400)
 
253
 
        mapping = builder.finish()
 
254
 
        self.assertEqual({1:'44411e8e9202177dd19b6599d7a7991059fa3cb4',
 
255
 
                          2: 'b0b62e674f67306fddcf72fa888c3b56df100d64',