-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Use pre-commit for Git hooks #3406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
06ebcae
0327d63
0834213
62651d1
880f51a
13d4ac5
e8b037e
497e0a6
770aad4
37a2576
7aec2fe
0a79495
63fcc66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,35 @@ | ||
| repos: | ||
| - repo: https://github.com/ambv/black | ||
| rev: 19.10b0 | ||
| hooks: | ||
| - id: black | ||
| language_version: python3 | ||
| - repo: https://gitlab.com/pycqa/flake8 | ||
| rev: master | ||
| hooks: | ||
| - id: flake8 | ||
| language_version: python3 | ||
| - repo: https://github.com/lovesegfault/beautysh | ||
| rev: master | ||
| hooks: | ||
| - id: beautysh | ||
| language_version: python3 | ||
| args: [-i, '2'] # 2-space indentaion | ||
| - hooks: | ||
| - id: black | ||
| language_version: python3 | ||
| repo: https://github.com/ambv/black | ||
| rev: 19.10b0 | ||
| - hooks: | ||
| - id: flake8 | ||
| language_version: python3 | ||
| repo: https://gitlab.com/pycqa/flake8 | ||
| rev: master | ||
| - hooks: | ||
| - args: | ||
| - -i | ||
| - '2' | ||
| id: beautysh | ||
| language_version: python3 | ||
| repo: https://github.com/lovesegfault/beautysh | ||
| rev: master | ||
| - hooks: | ||
| - id: dvc-pre-commit | ||
| language_version: python3 | ||
| stages: | ||
| - commit | ||
| - id: dvc-pre-push | ||
| language_version: python3 | ||
| stages: | ||
| - push | ||
| - always_run: true | ||
| id: dvc-post-checkout | ||
| language_version: python3 | ||
| stages: | ||
| - post-checkout | ||
| repo: https://github.com/andrewhare/dvc | ||
| rev: WIP-pre-commit-tool | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| - args: | ||
| - git-hook | ||
| - pre-commit | ||
| entry: dvc | ||
| id: dvc-pre-commit | ||
| language: python | ||
| language_version: python3 | ||
| name: DVC pre-commit | ||
| stages: | ||
| - commit | ||
| - args: | ||
| - git-hook | ||
| - pre-push | ||
| entry: dvc | ||
| id: dvc-pre-push | ||
| language: python | ||
| language_version: python3 | ||
| name: DVC pre-push | ||
| stages: | ||
| - push | ||
| - always_run: true | ||
| args: | ||
| - git-hook | ||
| - post-checkout | ||
| entry: dvc | ||
| id: dvc-post-checkout | ||
| language: python | ||
| language_version: python3 | ||
| minimum_pre_commit_version: 2.2.0 | ||
| name: DVC post-checkout | ||
| stages: | ||
| - post-checkout |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| from dvc.command.base import CmdBaseNoRepo, fix_subparsers | ||
| from dvc.exceptions import NotDvcRepoError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class CmdHookBase(CmdBaseNoRepo): | ||
| def run(self): | ||
| from dvc.repo import Repo | ||
|
|
||
| try: | ||
| repo = Repo() | ||
| repo.close() | ||
| except NotDvcRepoError: | ||
| return 0 | ||
|
|
||
| return self._run() | ||
|
|
||
|
|
||
| class CmdPreCommit(CmdHookBase): | ||
| def _run(self): | ||
| from dvc.main import main | ||
|
|
||
| return main(["status"]) | ||
|
|
||
|
|
||
| class CmdPostCheckout(CmdHookBase): | ||
| def run(self): | ||
| # when we are running from pre-commit tool, it doesn't provide CLI | ||
| # flags, but instead provides respective env vars that we could use. | ||
| flag = os.environ.get("PRE_COMMIT_CHECKOUT_TYPE") | ||
| if flag is None and len(self.args.args) >= 3: | ||
| # see https://git-scm.com/docs/githooks#_post_checkout | ||
| flag = self.args.args[2] | ||
|
|
||
| # checking out some reference and not specific file. | ||
| if flag != "1": | ||
| return 0 | ||
|
|
||
| # make sure we are not in the middle of a rebase/merge, so we | ||
| # don't accidentally break it with an unsuccessful checkout. | ||
| # Note that git hooks are always running in repo root. | ||
| if os.path.isdir(os.path.join(".git", "rebase-merge")): | ||
| return 0 | ||
|
|
||
| from dvc.main import main | ||
|
|
||
| return main(["checkout"]) | ||
|
|
||
|
|
||
| class CmdPrePush(CmdHookBase): | ||
| def run(self): | ||
| from dvc.main import main | ||
|
|
||
| return main(["push"]) | ||
|
|
||
|
|
||
| def add_parser(subparsers, parent_parser): | ||
| GIT_HOOK_HELP = "Run GIT hook." | ||
|
|
||
| git_hook_parser = subparsers.add_parser( | ||
| "git-hook", | ||
| parents=[parent_parser], | ||
| description=GIT_HOOK_HELP, | ||
| add_help=False, | ||
| ) | ||
|
|
||
| git_hook_subparsers = git_hook_parser.add_subparsers( | ||
| dest="cmd", | ||
| help="Use `dvc daemon CMD --help` for command-specific help.", | ||
| ) | ||
|
|
||
| fix_subparsers(git_hook_subparsers) | ||
|
|
||
| PRE_COMMIT_HELP = "Run pre-commit GIT hook." | ||
| pre_commit_parser = git_hook_subparsers.add_parser( | ||
| "pre-commit", | ||
| parents=[parent_parser], | ||
| description=PRE_COMMIT_HELP, | ||
| help=PRE_COMMIT_HELP, | ||
| ) | ||
| pre_commit_parser.add_argument( | ||
| "args", nargs="*", help="Arguments passed by GIT or pre-commit tool.", | ||
| ) | ||
| pre_commit_parser.set_defaults(func=CmdPreCommit) | ||
|
|
||
| POST_CHECKOUT_HELP = "Run post-checkout GIT hook." | ||
| post_checkout_parser = git_hook_subparsers.add_parser( | ||
| "post-checkout", | ||
| parents=[parent_parser], | ||
| description=POST_CHECKOUT_HELP, | ||
| help=POST_CHECKOUT_HELP, | ||
| ) | ||
| post_checkout_parser.add_argument( | ||
| "args", nargs="*", help="Arguments passed by GIT or pre-commit tool.", | ||
| ) | ||
| post_checkout_parser.set_defaults(func=CmdPostCheckout) | ||
|
|
||
| PRE_PUSH_HELP = "Run pre-push GIT hook." | ||
| pre_push_parser = git_hook_subparsers.add_parser( | ||
| "pre-push", | ||
| parents=[parent_parser], | ||
| description=PRE_PUSH_HELP, | ||
| help=PRE_PUSH_HELP, | ||
| ) | ||
| pre_push_parser.add_argument( | ||
| "args", nargs="*", help="Arguments passed by GIT or pre-commit tool.", | ||
| ) | ||
| pre_push_parser.set_defaults(func=CmdPrePush) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |
| class CmdInstall(CmdBase): | ||
| def run(self): | ||
| try: | ||
| self.repo.install() | ||
| self.repo.install(self.args.use_pre_commit_tool) | ||
| except Exception: | ||
| logger.exception("failed to install DVC Git hooks") | ||
| return 1 | ||
|
|
@@ -27,4 +27,11 @@ def add_parser(subparsers, parent_parser): | |
| help=INSTALL_HELP, | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| install_parser.add_argument( | ||
| "--use-pre-commit-tool", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be good to have
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @skshetry Im worried about it being confused with |
||
| action="store_true", | ||
| default=False, | ||
| help="Install DVC hooks using pre-commit " | ||
| "(https://pre-commit.com) if it is installed.", | ||
| ) | ||
| install_parser.set_defaults(func=CmdInstall) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| def install(self): | ||
| self.scm.install() | ||
| def install(self, use_pre_commit_tool): | ||
| self.scm.install(use_pre_commit_tool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be fixed after merge.