swellaby / rusty-hook

git hook manager, geared toward Rust projects
MIT License
206 stars 11 forks source link

Option to skip/control which hook files are created during init #119

Closed calebcartwright closed 3 years ago

calebcartwright commented 3 years ago

Description

Add a feature that allows for controlling which hook files are instantiated

Value

Currently, the repo initialization process (triggered with either rusty-hook init or using compiling with rusty-hook as a dev dep) adds a hook file for all the supportable client side hooks to easily support user configurability. However, there are probably some advanced user scenarios where they'd like to control which hook files are/aren't created.

For example, there are other Rust tools that support creating their own hook files (for example git-journal which supports commit message hooks) and we should support users that may want to use rusty-hook in conjunction with those others. There's also the potential for some users to want really fine grained control (create the pre-commit file but none of the others)

Implementation Thoughts

I think the most common usage of this feature would be a skipping of one or two hook files (like commit-msg and perhaps prepare-commit-msg) so my initial inclination would be to support this via a new config option skip list, for example:

hook_file_skip_list = [
  "commit-msg"
]

If there's sufficient demand down the road to support an opt-in type feature over an opt out, we could add an additional config at that time for an include/allow list and figure out precedence if and when we get there (implementing this as a skip/opt-out won't preclude doing more work in the future).

Currently, the init process that sets up the hook files in the repo just iterates through the full list of hooks: https://github.com/swellaby/rusty-hook/blob/39fbca3b90c6f37bfdacd23bc4d25b9a0da554f0/src/hooks.rs#L75-L80

(side note create_hook_files has a lot of duplication that could be consolidated into a separate closure or function)

That could be updated pretty easily to accept a list of hook files to skip, and then use the subset of HOOK_NAMES that weren't skipped (I suppose technically the disjunctive union provided the skip list doesn't have any junk entries)

Would then need to shuffle up the init order a bit to run the config creation check first, and then check for the hook_file_skip_list presence/value to pass along to setup_hooks function flow https://github.com/swellaby/rusty-hook/blob/39fbca3b90c6f37bfdacd23bc4d25b9a0da554f0/src/rusty_hook.rs#L10-L36

wpater commented 2 years ago

Is there any possibility to create a list of hooks to create? I would like to use only pre-commit and commit-msg but for now rusty-hooks creates all hooks, what is causing confusion.

calebcartwright commented 2 years ago

what is causing confusion.

Sorry, I don't understand what you mean. Could you rephrase this question?

Is there any possibility to create a list of hooks to create?

It would be possible with this feature although it hasn't been released. Even if it were released, you'd have to use the skip option (or manually created config file) to enumerate all the other hooks that you didn't want; it's more of an opt-out than an opt-in.

rusty-hook's primary goal is to make git hooks easy so the users don't have to do or think about anything other than adding the commands to a config file, without having to run extra commands (or having to ask all your contributors to run commands) nor having to deal with the underlying git hooks yourselves. In order to accomplish this, yes, rust-hook establishes hook files, but it doesn't actually do any work unless you configure it as such.

For example, if you only want to run something on pre-commit and commit-msg, then you should only add those entries to your config file, just like the example in our readme; that example config file will result in hook commands only being run when the pre-commit, pre-push, and post-push git hooks are invoked, all other hooks are no-ops.

wpater commented 2 years ago

Yes, indeed. My problem is that I have created this file first with configuration for only two hooks but rusty-hook created all possible hooks in .git/hooks - and in my opinion it's not right.

In default configuration (without file - file created by rusty-hooks) it's ok, we don't have information which hooks should be created and that's why rusty-hook creates all of them. But in case when file exists, and I explicitly configured that I want only 2 hooks - pre-commit and commit-msg - rusty-hook should create only those two hooks.

Did I miss something in configuration, or it is not possible? Do you plan to release a new version with this skip option?

calebcartwright commented 2 years ago

Thank you for responding @wpater, however, I have to say that I don't feel like you really addressed the question. You'd already indicated that you don't want the hook files to be created, but you haven't explained why that's the case beyond sharing your subjective opinion that it shouldn't.

There's a couple different styles of client side git hook tooling, and rusty-hook is absolutely in the style where creating all hook files is common and expected. This is done so that a single user can add a new hook command simply by checking in an update to the config file, and having that cascade to all users automatically (i.e. if you decide you want to introduce a new project-wide pre-push hook).

This is not without tradeoffs (like most choices in tech), but is an intentional decision to achieve the desired goals, and is always going to be our default behavior.

Did I miss something in configuration, or it is not possible? Do you plan to release a new version with this skip option?

As I stated in my previous comment this feature hasn't been released. It's currently backed up behind other changes in source that would break backwards compatibility and thus the release is blocked.

We will likely release it at some point once some of those blocking issues have been resolved, but it's not going to happen any time soon. For now, if you feel that strongly that the hook files shouldn't exist, then I'd suggest you consider alternative strategies/tooling, such as including hook file scripts in your repository with your own automation to copy or symlink them, or take a look at some of the alternative libs/tools we enumerate in our own Readme

wpater commented 2 years ago

Ok, I understand you, thank you for your help.

calebcartwright commented 2 years ago

Absolutely! Thanks for giving rusty-hook a try and sharing your perspective. Will be sure to drop a note here whenever we manage to get the new feature deployed so feel free to subscribe to this issue if that's something you're interested in.