@@ -278,6 +278,73 @@ def test_lookup_reference(self):
278
278
reference = repo .lookup_reference ('refs/heads/master' )
279
279
assert reference .name == 'refs/heads/master'
280
280
281
+ def test_lookup_reference_dwim (self ):
282
+ repo = self .repo
283
+
284
+ # remote ref
285
+ reference = self .repo .create_reference ('refs/remotes/origin/master' , LAST_COMMIT )
286
+ assert reference .shorthand == 'origin/master'
287
+ # tag
288
+ repo .create_reference ('refs/tags/version1' , LAST_COMMIT )
289
+
290
+ # Test dwim lookups
291
+
292
+ # Raise KeyError ?
293
+ with pytest .raises (KeyError ): repo .lookup_reference_dwim ('foo' )
294
+ with pytest .raises (KeyError ): repo .lookup_reference_dwim ('refs/foo' )
295
+
296
+ reference = repo .lookup_reference_dwim ('refs/heads/master' )
297
+ assert reference .name == 'refs/heads/master'
298
+
299
+ reference = repo .lookup_reference_dwim ('master' )
300
+ assert reference .name == 'refs/heads/master'
301
+
302
+ reference = repo .lookup_reference_dwim ('origin/master' )
303
+ assert reference .name == 'refs/remotes/origin/master'
304
+
305
+ reference = repo .lookup_reference_dwim ('version1' )
306
+ assert reference .name == 'refs/tags/version1'
307
+
308
+ def test_resolve_refish (self ):
309
+ repo = self .repo
310
+
311
+ # remote ref
312
+ reference = self .repo .create_reference ('refs/remotes/origin/master' , LAST_COMMIT )
313
+ assert reference .shorthand == 'origin/master'
314
+ # tag
315
+ repo .create_reference ('refs/tags/version1' , LAST_COMMIT )
316
+
317
+ # Test dwim lookups
318
+
319
+ # Raise KeyError ?
320
+ with pytest .raises (KeyError ): repo .resolve_refish ('foo' )
321
+ with pytest .raises (KeyError ): repo .resolve_refish ('refs/foo' )
322
+
323
+ commit , ref = repo .resolve_refish ('refs/heads/i18n' )
324
+ assert ref .name == 'refs/heads/i18n'
325
+ assert commit .hex == '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'
326
+
327
+ commit , ref = repo .resolve_refish ('master' )
328
+ assert ref .name == 'refs/heads/master'
329
+ assert commit .hex == LAST_COMMIT
330
+
331
+ commit , ref = repo .resolve_refish ('origin/master' )
332
+ assert ref .name == 'refs/remotes/origin/master'
333
+ assert commit .hex == LAST_COMMIT
334
+
335
+ commit , ref = repo .resolve_refish ('version1' )
336
+ assert ref .name == 'refs/tags/version1'
337
+ assert commit .hex == LAST_COMMIT
338
+
339
+ commit , ref = repo .resolve_refish (LAST_COMMIT )
340
+ assert ref is None
341
+ assert commit .hex == LAST_COMMIT
342
+
343
+ commit , ref = repo .resolve_refish ('HEAD~1' )
344
+ assert ref is None
345
+ assert commit .hex == '5ebeeebb320790caf276b9fc8b24546d63316533'
346
+
347
+
281
348
def test_reference_get_sha (self ):
282
349
reference = self .repo .lookup_reference ('refs/heads/master' )
283
350
assert reference .target .hex == LAST_COMMIT
@@ -390,7 +457,7 @@ def test_create_reference(self):
390
457
with pytest .raises (AlreadyExistsError ) as error :
391
458
self .repo .create_reference ('refs/tags/version1' , LAST_COMMIT )
392
459
assert isinstance (error .value , ValueError )
393
-
460
+
394
461
# Clear error
395
462
del error
396
463
0 commit comments