Checkout the configured branch/tag. We catch an "Exception" class here instead of a specific exception class because the exceptions raised by GitPython when running these functions vary in different versions of GitPython. fetch_on_fail If checkout
(self, fetch_on_fail=True)
| 1459 | failhard(self.role) |
| 1460 | |
| 1461 | def checkout(self, fetch_on_fail=True): |
| 1462 | """ |
| 1463 | Checkout the configured branch/tag. We catch an "Exception" class here |
| 1464 | instead of a specific exception class because the exceptions raised by |
| 1465 | GitPython when running these functions vary in different versions of |
| 1466 | GitPython. |
| 1467 | |
| 1468 | fetch_on_fail |
| 1469 | If checkout fails perform a fetch then try to checkout again. |
| 1470 | """ |
| 1471 | self.fetch_request_check() |
| 1472 | tgt_ref = self.get_checkout_target() |
| 1473 | try: |
| 1474 | head_sha = self.repo.rev_parse("HEAD").hexsha |
| 1475 | except Exception: # pylint: disable=broad-except |
| 1476 | # Should only happen the first time we are checking out, since |
| 1477 | # we fetch first before ever checking anything out. |
| 1478 | head_sha = None |
| 1479 | |
| 1480 | # 'origin/' + tgt_ref ==> matches a branch head |
| 1481 | # 'tags/' + tgt_ref + '@{commit}' ==> matches tag's commit |
| 1482 | checkout_refs = [ |
| 1483 | ("origin/" + tgt_ref, False), |
| 1484 | ("tags/" + tgt_ref, False), |
| 1485 | ] |
| 1486 | if self.fallback: |
| 1487 | checkout_refs += [ |
| 1488 | ("origin/" + self.fallback, True), |
| 1489 | ("tags/" + self.fallback, True), |
| 1490 | ] |
| 1491 | for checkout_ref, fallback in checkout_refs: |
| 1492 | try: |
| 1493 | target_sha = self.repo.rev_parse(checkout_ref).hexsha |
| 1494 | except Exception: # pylint: disable=broad-except |
| 1495 | # ref does not exist |
| 1496 | continue |
| 1497 | else: |
| 1498 | if head_sha == target_sha: |
| 1499 | # No need to checkout, we're already up-to-date |
| 1500 | return self.check_root() |
| 1501 | |
| 1502 | try: |
| 1503 | with self.gen_lock(lock_type="checkout"): |
| 1504 | self.repo.git.checkout(checkout_ref) |
| 1505 | log.debug( |
| 1506 | "%s remote '%s' has been checked out to %s%s", |
| 1507 | self.role, |
| 1508 | self.id, |
| 1509 | checkout_ref, |
| 1510 | " as fallback" if fallback else "", |
| 1511 | ) |
| 1512 | except GitLockError as exc: |
| 1513 | if exc.errno == errno.EEXIST: |
| 1514 | # Re-raise with a different strerror containing a |
| 1515 | # more meaningful error message for the calling |
| 1516 | # function. |
| 1517 | raise GitLockError( |
| 1518 | exc.errno, |
nothing calls this directly
no test coverage detected