Read about our upcoming Code of Conduct on this issue

test_bookmarks.py 8.74 KB
Newer Older
Georges Racinet's avatar
Georges Racinet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from .utils.hg import LocalRepo


def test_push_bookmarks_default_branch(test_project, tmpdir):
    repo_path = tmpdir.join('repo1')
    url = test_project.owner_basic_auth_url

    repo = LocalRepo.init(repo_path)
    # As usual, let's make a first public changeset
    repo_path.join('foo').write('foo0')
    repo.hg('commit', '-Am', "Commit 0")
    repo.hg('phase', '-p', ".")
    repo.hg('push', url)

    repo_path.join('foo').write('foo1')
    repo.hg('commit', '-m', "Commit 1")
    repo.hg('bookmark', '-r', '.', 'book1')
    print(repo.graphlog())

    code, out, err = repo.hg_unchecked('push', '-B', 'book1', url)
    assert code != 0, "Bookmarks should be forbidden by default"
    assert 'forbidden' in out.lower()
23
    assert 'book1' in out
Georges Racinet's avatar
Georges Racinet committed
24
25

    # now let's allow bookmarks on non-topic changesets
26
    test_project.owner_api_hgrc_set(inherit=True, allow_bookmarks=True)
Georges Racinet's avatar
Georges Racinet committed
27
28
29
30
31

    repo.hg('push', '-B', 'book1', url)
    branches = test_project.api_branches()
    # even though the 'default' named branch only has bookmarked heads,
    # the 'branch/default' GitLab branch should not be pruned, because it is
32
33
    # also GitLab's default branch (hence also the HEAD of the inner Git
    # repo)
Georges Racinet's avatar
Georges Racinet committed
34
    assert set(branches) == {'branch/default', 'book1'}
35
    assert branches['branch/default']['commit']['title'] == 'Commit 1'
Georges Racinet's avatar
Georges Racinet committed
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
    assert branches['book1']['commit']['title'] == 'Commit 1'

    # on the other hand, bookmarks on topics are still refused
    repo.hg('up', '0')
    repo_path.join('foo').write('foo3')
    repo.hg('topic', 'topbook')
    repo.hg('commit', '-m', "ontopic")
    repo.hg('bookmark', '-r', '.', 'book-on-top')

    code, out, err = repo.hg_unchecked('push', '-B', 'book-on-top', url)
    assert code != 0, "Bookmarks on topics should be forbidden"
    assert 'topbook' in out
    assert 'book-on-top' in out
    assert repo.hg('log', '-T', '{node}', '-r', 'book-on-top') in out

    # now let's try with a second bookmark on the same branch
    # (used to be a limitation of our modified hg-git)
    repo.hg('up', '0')
    repo_path.join('foo').write('foo2')
    repo.hg('commit', '-m', "Commit 2")
    repo.hg('bookmark', '-r', '.', 'book2')

    # it's also necessary to force-push
    repo.hg('push', '-f', '-B', 'book2', url)

    branches = test_project.api_branches()
    assert set(branches) == {'branch/default', 'book1', 'book2'}
63
    assert branches['branch/default']['commit']['title'] == 'Commit 2'
Georges Racinet's avatar
Georges Racinet committed
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
    assert branches['book1']['commit']['title'] == 'Commit 1'
    assert branches['book2']['commit']['title'] == 'Commit 2'


def test_push_bookmarks_non_default_branch(test_project, tmpdir):
    repo_path = tmpdir.join('repo1')
    url = test_project.owner_basic_auth_url

    repo = LocalRepo.init(repo_path)
    # As usual, let's make a first public changeset
    repo_path.join('foo').write('foo default')
    repo.hg('commit', '-Am', "Commit on default")
    repo.hg('phase', '-p', ".")
    repo.hg('push', url)

    repo.hg('branch', 'other')
    repo_path.join('foo').write('foo branch')
    repo.hg('commit', '-m', "Commit 0")
    repo.hg('push', '--new-branch', url)
    # Just to be sure
    branches = test_project.api_branches()
    assert set(branches) == {'branch/default', 'branch/other'}

    repo_path.join('foo').write('foo1')
    repo.hg('commit', '-m', "Commit 1")
    repo.hg('bookmark', '-r', '.', 'book1')
    print(repo.graphlog())

    code, out, err = repo.hg_unchecked('push', '-B', 'book1', url)
    assert code != 0, "Bookmarks should be forbidden by default"
    assert 'forbidden' in out.lower()

    # now let's allow bookmarks on non-topic changesets
97
    test_project.owner_api_hgrc_set(inherit=True, allow_bookmarks=True)
Georges Racinet's avatar
Georges Racinet committed
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

    repo.hg('push', '-B', 'book1', url)

    # hg branch 'other' has no non bookmarked head, therefore the GitLab
    # branch 'branch/other' has been replaced by the bookmark.
    branches = test_project.api_branches()
    assert set(branches) == {'branch/default', 'book1'}
    assert branches['branch/default']['commit']['title'] == 'Commit on default'
    assert branches['book1']['commit']['title'] == 'Commit 1'

    # on the other hand, bookmarks on topics are still refused
    repo.hg('up', '1')
    repo_path.join('foo').write('foo3')
    repo.hg('topic', 'topbook')
    repo.hg('commit', '-m', "ontopic")
    repo.hg('bookmark', '-r', '.', 'book-on-top')

    code, out, err = repo.hg_unchecked('push', '-B', 'book-on-top', url)
    assert code != 0, "Bookmarks on topics should be forbidden"
    assert 'topbook' in out
    assert 'book-on-top' in out
    assert repo.hg('log', '-T', '{node}', '-r', 'book-on-top') in out

    # now let's try with a second bookmark on the same branch
    # (used to be a limitation of our modified hg-git)
    repo.hg('up', '1')
    repo_path.join('foo').write('foo2')
    repo.hg('commit', '-m', "Commit 2")
    repo.hg('bookmark', '-r', '.', 'book2')

    # it's also necessary to force-push
    repo.hg('push', '-f', '-B', 'book2', url)

    # New bookmark is listed in GitLab branch, 'branch/other' isn't
    branches = test_project.api_branches()
    assert set(branches) == {'branch/default', 'book1', 'book2'}
    assert branches['book1']['commit']['title'] == 'Commit 1'
    assert branches['book2']['commit']['title'] == 'Commit 2'

    # let's add a non bookmarked head and check that it becomes 'branch/other'
    # again
    repo.hg('up', '1')
    repo_path.join('foo').write('not a bookmark')
    repo.hg('commit', '-m', "Not bookmarked")
    # let's be sure 'book1' hasn't moved
    assert repo.hg('bookmarks', '-l', 'book1', '-T', '{rev}').strip() == '2'
    repo.hg('push', '-f', '-r', '.', url)

    branches = test_project.api_branches()
    assert set(branches) == {'branch/default', 'branch/other',
                             'book1', 'book2'}
    assert branches['book1']['commit']['title'] == 'Commit 1'
    assert branches['book2']['commit']['title'] == 'Commit 2'
    assert branches['branch/other']['commit']['title'] == 'Not bookmarked'
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


def test_implicit_move(test_project, tmpdir):
    repo_path = tmpdir.join('repo1')
    url = test_project.owner_basic_auth_url

    repo = LocalRepo.init(repo_path)
    # As usual, let's make a first public changeset
    repo_path.join('foo').write('foo default')
    repo.hg('commit', '-Am', "Commit on default")
    repo.hg('phase', '-p', ".")
    repo.hg('push', url)

    repo.hg('branch', 'other')
    repo_path.join('foo').write('foo branch')
    repo.hg('commit', '-m', "Commit 1")
    repo.hg('push', '--new-branch', url)
    # Just to be sure
    branches = test_project.api_branches()
    assert set(branches) == {'branch/default', 'branch/other'}

    repo_path.join('foo').write('foo1')
    repo.hg('commit', '-m', "Commit 1")
    repo.hg('bookmark', '-r', '.', 'book')
    print(repo.graphlog())

178
    test_project.owner_api_hgrc_set(allow_bookmarks=True)
179
180
    repo.hg('push', '-B', 'book', url)
    branches = test_project.api_branch_titles()
181
    test_project.owner_api_hgrc_set(allow_bookmarks=False)
182
183
184
185
186
187
188
189
190
191
192
193
194

    repo_path.join('foo').write('foo2')
    repo.hg('commit', '-m', "Commit 2")
    repo.hg('bookmark', '-r', '.', 'book')

    # notice how we dont explicitely push the bookmark
    repo.hg('push', url)

    # yet, it moved
    assert test_project.api_branch_titles()['book'] == 'Commit 2'

    # bookmark removal is possible and it restores the shadowed branch
    repo.hg('bookmark', '-d', 'book')
195
    out = repo.hg('push', '-B', 'book', url, expected_return_code=1)
196
197
198
199
    assert 'deleting remote bookmark book' in out
    assert test_project.api_branch_titles() == {
        'branch/default': 'Commit on default',
        'branch/other': 'Commit 2',
200
    }
201
202

    # now imagine a bookmark slides to the head of a branch
203
204
    # first, prepare it
    test_project.owner_api_hgrc_set(allow_bookmarks=True)
205
    repo.hg('bookmark', '-r', '1', 'sliding')
206
    repo.hg('push', '-B', 'sliding', url, expected_return_code=1)
207
    test_project.owner_api_hgrc_set(allow_bookmarks=False)
208
209
210

    # let's move it to the head of 'other'
    repo.hg('bookmark', '-r', 'other', 'sliding')
211
    repo.hg('push', '-B', 'sliding', url, expected_return_code=1)
212

213
    gl_branches = {
214
215
        'branch/default': 'Commit on default',
        'sliding': 'Commit 2',
216
        'branch/other': 'Commit 2',
217
    }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

    assert test_project.api_branch_titles() == gl_branches

    if test_project.hg_native:
        # additional test to check that sliding to the head of
        # the GitLab default branch has no ill effects

        # verifying our assumptions
        assert test_project.api_default_branch() == 'branch/default'
        repo.hg('bookmark', '--force', '-r', 'default', 'sliding')
        repo.hg('push', '-B', 'sliding', url, expected_return_code=1)
        assert test_project.api_branch_titles() == {
            'branch/default': 'Commit on default',
            'branch/other': 'Commit 2',
            'sliding': 'Commit on default',
        }