-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathtest_refs.py
581 lines (468 loc) · 21.9 KB
/
test_refs.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# Copyright 2010-2021 The pygit2 contributors
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# In addition to the permissions in the GNU General Public License,
# the authors give you unlimited permission to link the compiled
# version of this file into combinations with other programs,
# and to distribute those combinations without any restriction
# coming from the use of this file. (The General Public License
# restrictions do apply in other respects; for example, they cover
# modification of the file, and distribution when not linked into
# a combined executable.)
#
# This file 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; see the file COPYING. If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""Tests for reference objects."""
import os
import pytest
from pygit2 import GIT_REF_OID, GIT_REF_SYMBOLIC, Signature
from pygit2 import Commit, Tree, reference_is_valid_name
from pygit2 import AlreadyExistsError, GitError, InvalidSpecError
LAST_COMMIT = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
def test_refs_list_objects(testrepo):
refs = [(ref.name, ref.target.hex) for ref in testrepo.references.objects]
assert sorted(refs) == [
('refs/heads/i18n', '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
('refs/heads/master', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98')]
def test_refs_list(testrepo):
# Without argument
assert sorted(testrepo.references) == ['refs/heads/i18n', 'refs/heads/master']
# We add a symbolic reference
testrepo.create_reference('refs/tags/version1', 'refs/heads/master')
assert sorted(testrepo.references) == ['refs/heads/i18n',
'refs/heads/master',
'refs/tags/version1']
def test_head(testrepo):
head = testrepo.head
assert LAST_COMMIT == testrepo[head.target].hex
assert LAST_COMMIT == testrepo[head.raw_target].hex
def test_refs_getitem(testrepo):
refname = 'refs/foo'
# Raise KeyError ?
with pytest.raises(KeyError): testrepo.references[refname]
# Return None ?
assert testrepo.references.get(refname) is None
# Test a lookup
reference = testrepo.references.get('refs/heads/master')
assert reference.name == 'refs/heads/master'
def test_refs_get_sha(testrepo):
reference = testrepo.references['refs/heads/master']
assert reference.target.hex == LAST_COMMIT
def test_refs_set_sha(testrepo):
NEW_COMMIT = '5ebeeebb320790caf276b9fc8b24546d63316533'
reference = testrepo.references.get('refs/heads/master')
reference.set_target(NEW_COMMIT)
assert reference.target.hex == NEW_COMMIT
def test_refs_set_sha_prefix(testrepo):
NEW_COMMIT = '5ebeeebb320790caf276b9fc8b24546d63316533'
reference = testrepo.references.get('refs/heads/master')
reference.set_target(NEW_COMMIT[0:6])
assert reference.target.hex == NEW_COMMIT
def test_refs_get_type(testrepo):
reference = testrepo.references.get('refs/heads/master')
assert reference.type == GIT_REF_OID
def test_refs_get_target(testrepo):
reference = testrepo.references.get('HEAD')
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
def test_refs_set_target(testrepo):
reference = testrepo.references.get('HEAD')
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
reference.set_target('refs/heads/i18n')
assert reference.target == 'refs/heads/i18n'
assert reference.raw_target == b'refs/heads/i18n'
def test_refs_get_shorthand(testrepo):
reference = testrepo.references.get('refs/heads/master')
assert reference.shorthand == 'master'
reference = testrepo.references.create('refs/remotes/origin/master', LAST_COMMIT)
assert reference.shorthand == 'origin/master'
def test_refs_set_target_with_message(testrepo):
reference = testrepo.references.get('HEAD')
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
sig = Signature('foo', 'bar')
testrepo.set_ident('foo', 'bar')
msg = 'Hello log'
reference.set_target('refs/heads/i18n', message=msg)
assert reference.target == 'refs/heads/i18n'
assert reference.raw_target == b'refs/heads/i18n'
first = list(reference.log())[0]
assert first.message == msg
assert first.committer == sig
def test_refs_delete(testrepo):
# We add a tag as a new reference that points to "origin/master"
reference = testrepo.references.create('refs/tags/version1', LAST_COMMIT)
assert 'refs/tags/version1' in testrepo.references
# And we delete it
reference.delete()
assert 'refs/tags/version1' not in testrepo.references
# Access the deleted reference
with pytest.raises(GitError): getattr(reference, 'name')
with pytest.raises(GitError): getattr(reference, 'type')
with pytest.raises(GitError): getattr(reference, 'target')
with pytest.raises(GitError): reference.delete()
with pytest.raises(GitError): reference.resolve()
with pytest.raises(GitError): reference.rename("refs/tags/version2")
def test_refs_rename(testrepo):
# We add a tag as a new reference that points to "origin/master"
reference = testrepo.references.create('refs/tags/version1',
LAST_COMMIT)
assert reference.name == 'refs/tags/version1'
reference.rename('refs/tags/version2')
assert reference.name == 'refs/tags/version2'
#def test_reload(testrepo):
# name = 'refs/tags/version1'
# ref = testrepo.create_reference(name, "refs/heads/master", symbolic=True)
# ref2 = testrepo.lookup_reference(name)
# ref.delete()
# assert ref2.name == name
# with pytest.raises(KeyError): ref2.reload()
# with pytest.raises(GitError): getattr(ref2, 'name')
def test_refs_resolve(testrepo):
reference = testrepo.references.get('HEAD')
assert reference.type == GIT_REF_SYMBOLIC
reference = reference.resolve()
assert reference.type == GIT_REF_OID
assert reference.target.hex == LAST_COMMIT
def test_refs_resolve_identity(testrepo):
head = testrepo.references.get('HEAD')
ref = head.resolve()
assert ref.resolve() is ref
def test_refs_create(testrepo):
# We add a tag as a new reference that points to "origin/master"
reference = testrepo.references.create('refs/tags/version1',
LAST_COMMIT)
refs = testrepo.references
assert 'refs/tags/version1' in refs
reference = testrepo.references.get('refs/tags/version1')
assert reference.target.hex == LAST_COMMIT
# try to create existing reference
with pytest.raises(ValueError):
testrepo.references.create('refs/tags/version1', LAST_COMMIT)
# try to create existing reference with force
reference = testrepo.references.create('refs/tags/version1',
LAST_COMMIT, force=True)
assert reference.target.hex == LAST_COMMIT
def test_refs_create_symbolic(testrepo):
# We add a tag as a new symbolic reference that always points to
# "refs/heads/master"
reference = testrepo.references.create('refs/tags/beta', 'refs/heads/master')
assert reference.type == GIT_REF_SYMBOLIC
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
# try to create existing symbolic reference
with pytest.raises(ValueError):
testrepo.references.create('refs/tags/beta', 'refs/heads/master')
# try to create existing symbolic reference with force
reference = testrepo.references.create('refs/tags/beta', 'refs/heads/master',
force=True)
assert reference.type == GIT_REF_SYMBOLIC
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
#def test_packall_references(testrepo):
# testrepo.packall_references()
def test_refs_peel(testrepo):
ref = testrepo.references.get('refs/heads/master')
assert testrepo[ref.target].id == ref.peel().id
assert testrepo[ref.raw_target].id == ref.peel().id
commit = ref.peel(Commit)
assert commit.tree.id == ref.peel(Tree).id
def test_refs_equality(testrepo):
ref1 = testrepo.references.get('refs/heads/master')
ref2 = testrepo.references.get('refs/heads/master')
ref3 = testrepo.references.get('refs/heads/i18n')
assert ref1 is not ref2
assert ref1 == ref2
assert not ref1 != ref2
assert ref1 != ref3
assert not ref1 == ref3
def test_refs_compress(testrepo):
packed_refs_file = os.path.join(testrepo.path, 'packed-refs')
assert not os.path.exists(packed_refs_file)
old_refs = [(ref.name, ref.target.hex)
for ref in testrepo.references.objects]
testrepo.references.compress()
assert os.path.exists(packed_refs_file)
new_refs = [(ref.name, ref.target.hex) for ref in testrepo.references.objects]
assert old_refs == new_refs
#
# Low level API written in C, repo.references call these.
#
def test_list_all_reference_objects(testrepo):
repo = testrepo
refs = [(ref.name, ref.target.hex)
for ref in repo.listall_reference_objects()]
assert sorted(refs) == [
('refs/heads/i18n', '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
('refs/heads/master', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98')]
def test_list_all_references(testrepo):
repo = testrepo
# Without argument
assert sorted(repo.listall_references()) == ['refs/heads/i18n', 'refs/heads/master']
assert sorted(repo.raw_listall_references()) == [b'refs/heads/i18n', b'refs/heads/master']
# We add a symbolic reference
repo.create_reference('refs/tags/version1', 'refs/heads/master')
assert sorted(repo.listall_references()) == ['refs/heads/i18n',
'refs/heads/master',
'refs/tags/version1']
assert sorted(repo.raw_listall_references()) == [b'refs/heads/i18n',
b'refs/heads/master',
b'refs/tags/version1']
def test_lookup_reference(testrepo):
repo = testrepo
# Raise KeyError ?
with pytest.raises(KeyError): repo.lookup_reference('refs/foo')
# Test a lookup
reference = repo.lookup_reference('refs/heads/master')
assert reference.name == 'refs/heads/master'
def test_lookup_reference_dwim(testrepo):
repo = testrepo
# remote ref
reference = testrepo.create_reference('refs/remotes/origin/master', LAST_COMMIT)
assert reference.shorthand == 'origin/master'
# tag
repo.create_reference('refs/tags/version1', LAST_COMMIT)
# Test dwim lookups
# Raise KeyError ?
with pytest.raises(KeyError): repo.lookup_reference_dwim('foo')
with pytest.raises(KeyError): repo.lookup_reference_dwim('refs/foo')
reference = repo.lookup_reference_dwim('refs/heads/master')
assert reference.name == 'refs/heads/master'
reference = repo.lookup_reference_dwim('master')
assert reference.name == 'refs/heads/master'
reference = repo.lookup_reference_dwim('origin/master')
assert reference.name == 'refs/remotes/origin/master'
reference = repo.lookup_reference_dwim('version1')
assert reference.name == 'refs/tags/version1'
def test_resolve_refish(testrepo):
repo = testrepo
# remote ref
reference = testrepo.create_reference('refs/remotes/origin/master', LAST_COMMIT)
assert reference.shorthand == 'origin/master'
# tag
repo.create_reference('refs/tags/version1', LAST_COMMIT)
# Test dwim lookups
# Raise KeyError ?
with pytest.raises(KeyError): repo.resolve_refish('foo')
with pytest.raises(KeyError): repo.resolve_refish('refs/foo')
commit, ref = repo.resolve_refish('refs/heads/i18n')
assert ref.name == 'refs/heads/i18n'
assert commit.hex == '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'
commit, ref = repo.resolve_refish('master')
assert ref.name == 'refs/heads/master'
assert commit.hex == LAST_COMMIT
commit, ref = repo.resolve_refish('origin/master')
assert ref.name == 'refs/remotes/origin/master'
assert commit.hex == LAST_COMMIT
commit, ref = repo.resolve_refish('version1')
assert ref.name == 'refs/tags/version1'
assert commit.hex == LAST_COMMIT
commit, ref = repo.resolve_refish(LAST_COMMIT)
assert ref is None
assert commit.hex == LAST_COMMIT
commit, ref = repo.resolve_refish('HEAD~1')
assert ref is None
assert commit.hex == '5ebeeebb320790caf276b9fc8b24546d63316533'
def test_reference_get_sha(testrepo):
reference = testrepo.lookup_reference('refs/heads/master')
assert reference.target.hex == LAST_COMMIT
def test_reference_set_sha(testrepo):
NEW_COMMIT = '5ebeeebb320790caf276b9fc8b24546d63316533'
reference = testrepo.lookup_reference('refs/heads/master')
reference.set_target(NEW_COMMIT)
assert reference.target.hex == NEW_COMMIT
def test_reference_set_sha_prefix(testrepo):
NEW_COMMIT = '5ebeeebb320790caf276b9fc8b24546d63316533'
reference = testrepo.lookup_reference('refs/heads/master')
reference.set_target(NEW_COMMIT[0:6])
assert reference.target.hex == NEW_COMMIT
def test_reference_get_type(testrepo):
reference = testrepo.lookup_reference('refs/heads/master')
assert reference.type == GIT_REF_OID
def test_get_target(testrepo):
reference = testrepo.lookup_reference('HEAD')
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
def test_set_target(testrepo):
reference = testrepo.lookup_reference('HEAD')
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
reference.set_target('refs/heads/i18n')
assert reference.target == 'refs/heads/i18n'
assert reference.raw_target == b'refs/heads/i18n'
def test_get_shorthand(testrepo):
reference = testrepo.lookup_reference('refs/heads/master')
assert reference.shorthand == 'master'
reference = testrepo.create_reference('refs/remotes/origin/master', LAST_COMMIT)
assert reference.shorthand == 'origin/master'
def test_set_target_with_message(testrepo):
reference = testrepo.lookup_reference('HEAD')
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
sig = Signature('foo', 'bar')
testrepo.set_ident('foo', 'bar')
msg = 'Hello log'
reference.set_target('refs/heads/i18n', message=msg)
assert reference.target == 'refs/heads/i18n'
assert reference.raw_target == b'refs/heads/i18n'
first = list(reference.log())[0]
assert first.message == msg
assert first.committer == sig
def test_delete(testrepo):
repo = testrepo
# We add a tag as a new reference that points to "origin/master"
reference = repo.create_reference('refs/tags/version1', LAST_COMMIT)
assert 'refs/tags/version1' in repo.listall_references()
assert b'refs/tags/version1' in repo.raw_listall_references()
# And we delete it
reference.delete()
assert 'refs/tags/version1' not in repo.listall_references()
assert b'refs/tags/version1' not in repo.raw_listall_references()
# Access the deleted reference
with pytest.raises(GitError): getattr(reference, 'name')
with pytest.raises(GitError): getattr(reference, 'type')
with pytest.raises(GitError): getattr(reference, 'target')
with pytest.raises(GitError): reference.delete()
with pytest.raises(GitError): reference.resolve()
with pytest.raises(GitError): reference.rename("refs/tags/version2")
def test_rename(testrepo):
# We add a tag as a new reference that points to "origin/master"
reference = testrepo.create_reference('refs/tags/version1',
LAST_COMMIT)
assert reference.name == 'refs/tags/version1'
reference.rename('refs/tags/version2')
assert reference.name == 'refs/tags/version2'
# def test_reload(testrepo):
# name = 'refs/tags/version1'
# repo = testrepo
# ref = repo.create_reference(name, "refs/heads/master", symbolic=True)
# ref2 = repo.lookup_reference(name)
# ref.delete()
# assert ref2.name == name
# with pytest.raises(KeyError): ref2.reload()
# with pytest.raises(GitError): getattr(ref2, 'name')
def test_reference_resolve(testrepo):
reference = testrepo.lookup_reference('HEAD')
assert reference.type == GIT_REF_SYMBOLIC
reference = reference.resolve()
assert reference.type == GIT_REF_OID
assert reference.target.hex == LAST_COMMIT
def test_reference_resolve_identity(testrepo):
head = testrepo.lookup_reference('HEAD')
ref = head.resolve()
assert ref.resolve() is ref
def test_create_reference(testrepo):
# We add a tag as a new reference that points to "origin/master"
reference = testrepo.create_reference('refs/tags/version1',
LAST_COMMIT)
assert 'refs/tags/version1' in testrepo.listall_references()
assert b'refs/tags/version1' in testrepo.raw_listall_references()
reference = testrepo.lookup_reference('refs/tags/version1')
assert reference.target.hex == LAST_COMMIT
# try to create existing reference
with pytest.raises(AlreadyExistsError) as error:
testrepo.create_reference('refs/tags/version1', LAST_COMMIT)
assert isinstance(error.value, ValueError)
# Clear error
del error
# try to create existing reference with force
reference = testrepo.create_reference('refs/tags/version1',
LAST_COMMIT, force=True)
assert reference.target.hex == LAST_COMMIT
def test_create_reference_with_message(testrepo):
sig = Signature('foo', 'bar')
testrepo.set_ident('foo', 'bar')
msg = 'Hello log'
reference = testrepo.create_reference('refs/heads/feature',
LAST_COMMIT,
message=msg)
first = list(reference.log())[0]
assert first.message == msg
assert first.committer == sig
def test_create_symbolic_reference(testrepo):
repo = testrepo
# We add a tag as a new symbolic reference that always points to
# "refs/heads/master"
reference = repo.create_reference('refs/tags/beta',
'refs/heads/master')
assert reference.type == GIT_REF_SYMBOLIC
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
# try to create existing symbolic reference
with pytest.raises(AlreadyExistsError) as error:
repo.create_reference('refs/tags/beta', 'refs/heads/master')
assert isinstance(error.value, ValueError)
# try to create existing symbolic reference with force
reference = repo.create_reference('refs/tags/beta',
'refs/heads/master', force=True)
assert reference.type == GIT_REF_SYMBOLIC
assert reference.target == 'refs/heads/master'
assert reference.raw_target == b'refs/heads/master'
def test_create_symbolic_reference_with_message(testrepo):
sig = Signature('foo', 'bar')
testrepo.set_ident('foo', 'bar')
msg = 'Hello log'
reference = testrepo.create_reference('HEAD',
'refs/heads/i18n',
force=True,
message=msg)
first = list(reference.log())[0]
assert first.message == msg
assert first.committer == sig
def test_create_invalid_reference(testrepo):
repo = testrepo
# try to create a reference with an invalid name
with pytest.raises(InvalidSpecError) as error:
repo.create_reference('refs/tags/in..valid', 'refs/heads/master')
assert isinstance(error.value, ValueError)
# def test_packall_references(testrepo):
# testrepo.packall_references()
def test_peel(testrepo):
repo = testrepo
ref = repo.lookup_reference('refs/heads/master')
assert repo[ref.target].id == ref.peel().id
assert repo[ref.raw_target].id == ref.peel().id
commit = ref.peel(Commit)
assert commit.tree.id == ref.peel(Tree).id
def test_valid_reference_names_ascii():
assert reference_is_valid_name('HEAD')
assert reference_is_valid_name('refs/heads/master')
assert reference_is_valid_name('refs/heads/perfectly/valid')
assert reference_is_valid_name('refs/tags/v1')
assert reference_is_valid_name('refs/special/ref')
def test_valid_reference_names_unicode():
assert reference_is_valid_name('refs/heads/ünicöde')
assert reference_is_valid_name('refs/tags/😀')
def test_invalid_reference_names():
assert not reference_is_valid_name('')
assert not reference_is_valid_name(' refs/heads/master')
assert not reference_is_valid_name('refs/heads/in..valid')
assert not reference_is_valid_name('refs/heads/invalid~')
assert not reference_is_valid_name('refs/heads/invalid^')
assert not reference_is_valid_name('refs/heads/invalid:')
assert not reference_is_valid_name('refs/heads/invalid\\')
assert not reference_is_valid_name('refs/heads/invalid?')
assert not reference_is_valid_name('refs/heads/invalid[')
assert not reference_is_valid_name('refs/heads/invalid*')
assert not reference_is_valid_name('refs/heads/@{no}')
assert not reference_is_valid_name('refs/heads/foo//bar')
def test_invalid_arguments():
with pytest.raises(TypeError):
reference_is_valid_name()
with pytest.raises(TypeError):
reference_is_valid_name(None)
with pytest.raises(TypeError):
reference_is_valid_name(1)
with pytest.raises(TypeError):
reference_is_valid_name('too', 'many')