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

Question: Why does husky findUp git? #502

Closed
G-Rath opened this issue Jun 11, 2019 · 4 comments
Closed

Question: Why does husky findUp git? #502

G-Rath opened this issue Jun 11, 2019 · 4 comments

Comments

@G-Rath
Copy link
Contributor

G-Rath commented Jun 11, 2019

I'm breaking this out from #499

I've got a project whose nature requires having multiple mini-projects ("templates") in a directory of the project, all of which are committed to git.

When husky installs, it uses find-up to look up recursively until it finds a parent folder w/ a .git file or folder:

const gitDirOrFile = findUp.sync('.git', { cwd: userPkgDir })

This means that if you install husky in root/a/b/c, it'll set root/.git to call husky in root/a/b/c.

My question is: Why?

Personally, I'd think you'd want to just check if the package.json root had a .git file or folder (so /c in this example); otherwise you're running scripts that are hidden away in nested folders, and also need to make sure the packages are actually installed if they're critical to your project (or it's flow).

It could be I've completely missed a valid use case, hence the question :)

@typicode
Copy link
Owner

Hi @G-Rath,

It's because some users have their package.json in nested directory and can't move it to Git root. That seems to happen mostly with enterprise projects.

.git
front/package.json
back/ # May not be in Node but in some other tech

@G-Rath
Copy link
Contributor Author

G-Rath commented Jun 12, 2019

Hi @typicode - thanks for the response.

That seems a bit weird to me, as why would not be able to move husky to the git root?

As I've said, IMO if you're going to use husky to manage your git hooks, theres no reason why you can' stick it at the same level as the .git, since it'll override any other hooks anyway?

So in your example:

.git
front/package.json # this is fine - contains things for front
back/ # also fine
package.json # this is where husky should be IMO

Of course, this is my opinion, and I'm sure there are some valid reasons for why that won't work. Either way; that's how it is, and clearly some people have use for it 😄

In saying that, it does make me a little uncomfortable as there's no bail out condition - if I install husky by mistake in a folder w/o git, it'll just keep going up until it reaches the top of the drive (or of course finds a git repo).

(Ironically, I found the other day I had actually managed to create a git repo of my C:\ drive by mistake, w/o realising it, which is part of the reason why this makes me a little paranoid)


Originally, I was going to suggest passing a predicate function to find-up, but then after looking at the code some more, I realised you actually get the config before searching for the nearest .git, and bail out if you can't find it in the same directory as the node_modules the husky package is installed in.

That makes the whole thing a lot simpler: would you be open to having a root config property, like eslints root, that if true means husky only looks for .git in the same directory as it's package.json?

It should just be a matter of the following:

const gitDirOrFile = conf.root
    ? fs.existsSync(path.join(userPkgDir, '.git'))
    : findUp.sync('.git', { cwd: userPkgDir })

Let me know if you'd be interested in supporting that, and I'll break it out into a new issue + PR (w/ tests 😬). I think this might solve #499, but suspect I might be not be understanding what the issue is actually about.

Alternatively, it could work the other way: change the default behaviour to be only search for .git in the same directory as the package.json, unless nested is true. That'd be a "safe" breaking change since it'd just mean you need to add that property to your config if you wanted the original behaviour. Eithers fine w/ me 😄

@abbottdev
Copy link

Just for reference - I am here trying to find a config setting to specify the .git dir because my org does have a front/back type scenario. (We also are using NPM utilities within different language repos, as the tooling for NPM is better 🙉) As I've now discovered, if husky walks up that actually is exactly what I need! ❤️

@typicode
Copy link
Owner

typicode commented Jan 7, 2021

Behavior has been changed in v5. Git dir needs to be specified if it's at a different level than .package.json. Thanks for the feedbacks 👍

@typicode typicode closed this as completed Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants