Checkout the configured branch/tag fetch_on_fail If checkout fails perform a fetch then try to checkout again.
(self, fetch_on_fail=True)
| 1827 | return obj.get_object() |
| 1828 | |
| 1829 | def checkout(self, fetch_on_fail=True): |
| 1830 | """ |
| 1831 | Checkout the configured branch/tag |
| 1832 | |
| 1833 | fetch_on_fail |
| 1834 | If checkout fails perform a fetch then try to checkout again. |
| 1835 | """ |
| 1836 | self.fetch_request_check() |
| 1837 | tgt_ref = self.get_checkout_target() |
| 1838 | local_ref = "refs/heads/" + tgt_ref |
| 1839 | remote_ref = "refs/remotes/origin/" + tgt_ref |
| 1840 | tag_ref = "refs/tags/" + tgt_ref |
| 1841 | |
| 1842 | try: |
| 1843 | local_head = self.repo.lookup_reference("HEAD") |
| 1844 | except KeyError: |
| 1845 | log.warning("HEAD not present in %s remote '%s'", self.role, self.id) |
| 1846 | return None |
| 1847 | |
| 1848 | try: |
| 1849 | head_sha = str(self.peel(local_head).id) |
| 1850 | except AttributeError: |
| 1851 | # Shouldn't happen, but just in case a future pygit2 API change |
| 1852 | # breaks things, avoid a traceback and log an error. |
| 1853 | log.error( |
| 1854 | "Unable to get SHA of HEAD for %s remote '%s'", self.role, self.id |
| 1855 | ) |
| 1856 | return None |
| 1857 | except KeyError: |
| 1858 | head_sha = None |
| 1859 | |
| 1860 | refs = self.repo.listall_references() |
| 1861 | |
| 1862 | def _perform_checkout(checkout_ref, branch=True): |
| 1863 | """ |
| 1864 | DRY function for checking out either a branch or a tag |
| 1865 | """ |
| 1866 | try: |
| 1867 | with self.gen_lock(lock_type="checkout"): |
| 1868 | # Checkout the local branch corresponding to the |
| 1869 | # remote ref. |
| 1870 | self.repo.checkout(checkout_ref) |
| 1871 | if branch: |
| 1872 | self.repo.reset(pygit2_id, pygit2.GIT_RESET_HARD) |
| 1873 | return True |
| 1874 | except GitLockError as exc: |
| 1875 | if exc.errno == errno.EEXIST: |
| 1876 | # Re-raise with a different strerror containing a |
| 1877 | # more meaningful error message for the calling |
| 1878 | # function. |
| 1879 | raise GitLockError( |
| 1880 | exc.errno, |
| 1881 | f"Checkout lock exists for {self.role} remote '{self.id}'", |
| 1882 | ) |
| 1883 | else: |
| 1884 | log.error( |
| 1885 | "Error %d encountered obtaining checkout lock " |
| 1886 | "for %s remote '%s'", |