From a11b5f43774c2213476877ad415fe9f49192e0cc Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 23 Jan 2020 22:51:13 +0200 Subject: [PATCH] install: post-checkout: run only on branches/tags We shouldn't try to automatically checkout in detached HEAD state, as we might break the rebase process. Fixes #3107 --- dvc/scm/git/__init__.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index 2d334a6493..ad1fcb2da2 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -247,11 +247,12 @@ def list_tags(self): def list_all_commits(self): return [c.hexsha for c in self.repo.iter_commits("--all")] - def _install_hook(self, name, cmd): - command = ( - '[ "$3" = "0" ]' - ' || [ -z "$(git ls-files --full-name .dvc)" ]' - " || exec dvc {}".format(cmd) + def _install_hook(self, name, preconditions, cmd): + # only run in dvc repo + in_dvc_repo = '[ -n "$(git ls-files --full-name .dvc)" ]' + + command = "if {}; then exec dvc {}; fi".format( + " && ".join([in_dvc_repo] + preconditions), cmd ) hook = self._hook_path(name) @@ -269,9 +270,19 @@ def _install_hook(self, name, cmd): def install(self): self._verify_dvc_hooks() - self._install_hook("post-checkout", "checkout") - self._install_hook("pre-commit", "status") - self._install_hook("pre-push", "push") + self._install_hook( + "post-checkout", + [ + # checking out some reference and not specific file. + '[ "$3" = "1" ]', + # check that we are on some branch/tag and not in detached HEAD + # state, so we don't accidentally break a rebase. + '[ "$(git rev-parse --abbrev-ref HEAD)" != "HEAD" ]', + ], + "checkout", + ) + self._install_hook("pre-commit", [], "status") + self._install_hook("pre-push", [], "push") def cleanup_ignores(self): for path in self.ignored_paths: