Skip to content
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

optional feature for auto installing the hooks on cloned repos #1084

Closed
ssbarnea opened this issue Jul 11, 2019 · 6 comments · Fixed by #1090
Closed

optional feature for auto installing the hooks on cloned repos #1084

ssbarnea opened this issue Jul 11, 2019 · 6 comments · Fixed by #1090

Comments

@ssbarnea
Copy link

As pre-commit becomes more and more popular I often face myself the problem that I forget to install the hooks on newly cloned repositories.

What it would be cook is if (at clone time?) I could get a prompt that would ask me if I want to enable the hooks or not for that repository.

The idea is to find a way to detect that a repository has pre-commit but the hooks ar ere not installed and allow user to install them, with 1-2 key strokes.

@asottile
Copy link
Member

I use this: https://github.com/asottile/scratch/blob/357638a6f88261ad3715dfe2e4bbdea653f756e3/.bashrc#L21

there isn't really a way to hook into clone or other things so I'm not sure what can be done here from a framework perspective beyond documenting something like this

though I guess maybe it could be provided as eval "$(pre-commit autoinstall)" or something and have support for several shells

@asottile
Copy link
Member

@ssbarnea thought on that?

@asottile
Copy link
Member

Actually, seems now that I've fixed up some things since the original implementation, pre-commit is compatible with init.templatedir

I think it might make sense to have a pre-commit init-templatedir which ~essentially does the following:

  • mkdir -p "$templatedir/hooks"
  • write out $templatedir/hooks/pre-commit using the template but with --allow-missing-config
  • (maybe?) set git config --global init.templatedir ...

@ssbarnea
Copy link
Author

Your prompt hack seems to do exactly that but I do not find it as a scalable solution, especially as it adds extra time for each chdir and that it does not work with all shells.

On the other hand, the templates approachs seems super nice. It would call pre-commit on each clones git repo but silently doing nothign if the repository is not pre-commit enable. No problems with shells or performanc impact on chdir.

I really like it, going to start testing it right away.

@ssbarnea
Copy link
Author

@asottile One first problem that I spotted is that I had to manually mention the template directory, when this should have being the implicit location

# required now
pre-commit init-templatedir ~/.git-template
# expected
pre-commit init-templatedir 

I also hope this would not remove other templates from there.

Another issue is that I got an unexpected warning:

$ pre-commit init-templatedir ~/.git-template                                                                                                                                                                                                pre-commit installed at /Users/ssbarnea/.git-template/hooks/pre-commit
[WARNING] `init.templateDir` not set to the target directory
[WARNING] maybe `git config --global init.templateDir /Users/ssbarnea/.git-template`?

$ git config --global init.templateDir                                                                                                                                                                                                       [11:54:39]
~/.git-template

I guess that while testing the current location of git-template-dir, pre-commit is missing to resolve their paths. The folder happened to be the same but the code complains about it.

IMHO, just using what is returned from git config --global init.templateDir should be the default location.

@asottile
Copy link
Member

Ah it probably needs os.path.expanduser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants