rycus86 / githooks

Githooks: per-repo and global Git hooks with version control
MIT License
380 stars 19 forks source link

How do I disable automatic setup on new repos? #154

Closed henderea closed 3 years ago

henderea commented 3 years ago

I want to use githooks only for specific repos, but it seems that the setup automatically adds githooks to all new repos. Is there a way I can make githooks only install on demand?

To be specific, I want to manually turn on githooks for the Java repositories I have for work on my computer, but leave it off for everything else. And most new repos I create/clone are not Java. I tried removing the init.templateDir property from my global git config, but then githooks started telling me the setup was corrupted. And I wasn't able to find any way to make git hooks install work on repos but not have the hooks automatically installed on repos or applied globally.

rycus86 commented 3 years ago

Hey @henderea

We used to have a "single-repo" install mode for Githooks but that's been deprecated and (mostly) removed now. I'd have expected removing the init.templateDir would work. :thinking: Can you please copy the output here on what exactly are you getting?

henderea commented 3 years ago
^ Checking for updates ...
Updating Githooks installation ...
Fetching Githooks updates ...
Githooks clone at version: 2102.092216-637be3
! Your installation is corrupt.
  The global value `githooks.useCoreHooksPath = false`
  is set but the corresponding hook templates directory
  is not found.
! Git hook templates directory not found
! Failed to execute the installation

as a note, if I set the useCoreHooksPath to true, it still gives me the error, but with it saying = true.

rycus86 commented 3 years ago

@henderea I brought back the --single option partially in https://github.com/rycus86/githooks/commit/3618f03cf852602d363cca28a49430c0ae37f01c , and I think it should work for use case, can you give it a go?

You can either try git hooks install if you already have the CLI from githooks, or do it with:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/rycus86/githooks/master/install.sh)" -- --single

Let me know how it goes!

henderea commented 3 years ago

@rycus86 so how do I use single mode if I already have the CLI installed? I tried running git hooks install, and this was my output:

^ Checking for updates ...
Updating Githooks installation ...
Fetching Githooks updates ...
Githooks clone at version: 2102.112033-3618f0
cp: /applypatch-msg: Read-only file system
! Failed to setup the applypatch-msg template at /applypatch-msg
! Failed to execute the installation

I tried this both with and without init.templateDir set to the githooks template dir. Githooks is installed at ~/.githooks, and I'm not seeing any issues with the ownership of the directory.

I'm on macOS Catalina 10.15.7. Git is installed with homebrew in /usr/local/.... I think Catalina locked down some areas of the filesystem, so it's possible githooks decided to try to use a directory that isn't available to write to instead of the install location it already has in my home directory.

henderea commented 3 years ago

Actually, after looking at some of the code, it seems as though it is somehow no longer detecting my templates directory. I even tried running export GIT_TEMPLATE_DIR="$HOME/.githooks/templates" in my shell before running git hooks install, and I got the same result. Based on the code, it looks like where it says /applypatch-msg, it is missing the path to my templates directory, but I can't see any clear reason why it would be missing that when looking at the code. The error is because the root of the filesystem on Catalina is read-only.

Also, if I run git config init.templateDir in the git repo directory I'm running the install command in, it prints out the right value.

henderea commented 3 years ago

I looked at the code a bit more, and I'm wondering if somehow, githooks thinks it is supposed to be in single mode. Looking at the code you changed, I think it might be running the code in setup_hook_templates() in install.sh even when $TARGET_TEMPLATE_DIR is blank. If you're going to bring back single mode partially, you should probably add some extra checks to make sure it doesn't run template-related stuff in single mode.

That said, I'm not entirely sure why it would be using single mode in the first place. Here are the relevant parts of my global .gitconfig

[githooks]
    installDir = /Users/erichenderson/.githooks
    runner = /Users/erichenderson/.githooks/release/base-template.sh
    cloneUrl = https://github.com/rycus86/githooks.git
    cloneBranch = master
    useCoreHooksPath = false
[init]
    templateDir = /Users/erichenderson/.githooks/templates
    defaultBranch = master

And here is from the repo I'm running stuff in:

[githooks]
    registered = true

And the command I'm running is just git hooks install. My githooks setup is standard as far as I know.

rycus86 commented 3 years ago

Thanks for testing @henderea ! It looks like I missed some checks here and there based on the above. Out of curiosity, have you tried single install through the curl/install script instead or the git hooks install wrapper?

That said, I'm not entirely sure why it would be using single mode in the first place.

The git hooks install command without any flags should pass a --single to the install script internally, perhaps that's broken.

I'll try to add more tests later to see if I can reproduce and fix this.

rycus86 commented 3 years ago

Oh, looks like it's already failing tests in Travis where it runs as a user rather than root, so hopefully it's an easy fix. 🤞

rycus86 commented 3 years ago

@henderea can you please try again? I'm hoping https://github.com/rycus86/githooks/commit/4cbad0a748365de2ced658e9a818bf48089cece3 will fix the problem as https://travis-ci.org/github/rycus86/githooks/jobs/758506188 had the exact same error message, and I think you were hitting the same problem. With that change it should be now fixed.

henderea commented 3 years ago

@rycus86 looks like it's working again. And even if I take out the init.templateDir stuff, it still works fine.

Now all I need is to be able to disable the README offer globally, because I always say no 😅

rycus86 commented 3 years ago

@henderea great to hear that its working now. 🙂 Can you please open a separate issue for disabling the README offer?

rycus86 commented 3 years ago

@henderea on the latest version, when you skip adding READMEs (the s choice) it will ask if you always want to skip adding one, just say yes and it will never ask again.

(alternative: git config --global githooks.noReadme "yes")

henderea commented 3 years ago

@rycus86 works great. thanks!