install: add pre-checkout hook

Fixes #1248

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2018-10-22 01:18:55 +03:00
parent 94cf83c1cd
commit 437c2e7cbe
2 changed files with 12 additions and 4 deletions
+7 -3
View File
@@ -240,18 +240,22 @@ class Git(Base):
def list_tags(self):
return [t.name for t in self.repo.tags]
def install(self):
def _install_hook(self, name, cmd):
hook = os.path.join(self.root_dir,
self.GIT_DIR,
'hooks',
'post-checkout')
name)
if os.path.isfile(hook):
msg = 'Git hook \'{}\' already exists.'
raise SCMError(msg.format(os.path.relpath(hook)))
with open(hook, 'w+') as fd:
fd.write('#!/bin/sh\nexec dvc checkout\n')
fd.write('#!/bin/sh\nexec dvc {}\n'.format(cmd))
os.chmod(hook, 0o777)
def install(self):
self._install_hook('post-checkout', 'checkout')
self._install_hook('pre-commit', 'status')
def SCM(root_dir, no_scm=False, project=None):
if Git.is_repo(root_dir) or Git.is_submodule(root_dir):
+5 -1
View File
@@ -13,7 +13,11 @@ class TestInstall(TestDvc):
ret = main(['install'])
self.assertNotEqual(ret, 0)
self.assertTrue(os.path.isfile('.git/hooks/post-checkout'))
def hook(name):
return os.path.join('.git', 'hooks', name)
self.assertTrue(os.path.isfile(hook('post-checkout')))
self.assertTrue(os.path.isfile(hook('pre-commit')))
self.dvc.add(self.FOO)
os.unlink(self.FOO)