Skip to content

Commit c2def08

Browse files
authored
Merge pull request #3228 from efiop/3107
install: post-checkout: run only on branches/tags
2 parents 4457df3 + a11b5f4 commit c2def08

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

dvc/scm/git/__init__.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,12 @@ def list_tags(self):
247247
def list_all_commits(self):
248248
return [c.hexsha for c in self.repo.iter_commits("--all")]
249249

250-
def _install_hook(self, name, cmd):
251-
command = (
252-
'[ "$3" = "0" ]'
253-
' || [ -z "$(git ls-files --full-name .dvc)" ]'
254-
" || exec dvc {}".format(cmd)
250+
def _install_hook(self, name, preconditions, cmd):
251+
# only run in dvc repo
252+
in_dvc_repo = '[ -n "$(git ls-files --full-name .dvc)" ]'
253+
254+
command = "if {}; then exec dvc {}; fi".format(
255+
" && ".join([in_dvc_repo] + preconditions), cmd
255256
)
256257

257258
hook = self._hook_path(name)
@@ -269,9 +270,19 @@ def _install_hook(self, name, cmd):
269270
def install(self):
270271
self._verify_dvc_hooks()
271272

272-
self._install_hook("post-checkout", "checkout")
273-
self._install_hook("pre-commit", "status")
274-
self._install_hook("pre-push", "push")
273+
self._install_hook(
274+
"post-checkout",
275+
[
276+
# checking out some reference and not specific file.
277+
'[ "$3" = "1" ]',
278+
# check that we are on some branch/tag and not in detached HEAD
279+
# state, so we don't accidentally break a rebase.
280+
'[ "$(git rev-parse --abbrev-ref HEAD)" != "HEAD" ]',
281+
],
282+
"checkout",
283+
)
284+
self._install_hook("pre-commit", [], "status")
285+
self._install_hook("pre-push", [], "push")
275286

276287
def cleanup_ignores(self):
277288
for path in self.ignored_paths:

0 commit comments

Comments
 (0)