pre-commit-hooks icon indicating copy to clipboard operation
pre-commit-hooks copied to clipboard

check for long file paths

Open guykisel opened this issue 3 years ago • 2 comments
trafficstars

Depending on what you're doing, Windows can have a path length limit of 260 characters. It's kind of complicated and there are a number of workarounds but this limit is true in many cases.

In editions of Windows before Windows 10 version 1607, the maximum length for a path is MAX_PATH, which is defined as 260 characters. In later versions of Windows, changing a registry key or using the Group Policy tool is required to remove the limit. See Maximum Path Length Limitation for full details.

My proposal is to write a simple hook that checks each path for its length as an absolute path and errors if the path exceeds a configurable value that defaults to 260 chars.

This might make the most sense as part of the Windows illegal filenames check - if so I'm happy to just copy this request into that issue. https://github.com/pre-commit/pre-commit-hooks/issues/589

References

  • https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
  • https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd

(I did search for dupes and didn't find any similar proposals or requests, but of course if this has come up before and I missed it, please forgive me :) )

guykisel avatar Apr 25 '22 23:04 guykisel

yeah this might be an interesting idea -- I'm not sure how we would pick a good lower bound though (since it kind of depends on where the repository gets cloned to)

it might also be possible just with language: fail and and entry like .{260,} or something? it wouldn't be args configurable but that's perhaps an idea for a repo: local hook?

#589 kinda stalled, not really sure what's up with that and I wouldn't want to tie the two together

asottile avatar Apr 26 '22 00:04 asottile

Yeah, for some of the projects I've worked on there's a standardized path recommended for checkouts, like C:/git/your_project, but that is a good point about clone location mattering. In that case maybe if this hook was implemented it would be better to just track path length relative to the repo root, even if that's not a perfect measure of absolute path length.

Using language: fail is a neat option, I didn't even realize it existed! https://pre-commit.com/#fail - link to the docs for anyone else reading this. Something super simple like that does seem like it should work generally.

Somehow in my thinking about this, the clone location issue slipped my mind. Makes me think this might not be quite as generally valuable as I originally thought.

I suppose maybe it could be paired with a hook to warn you if your repo clone location is invalid according to some ruleset, based on length or maybe other rules.

guykisel avatar Apr 26 '22 01:04 guykisel

I'm going out on a limb here, but os.getcwd() should return the git repo directory, and then this is a pretty simple python hook. Are PRs welcome for this?

AFulgens avatar Feb 28 '24 15:02 AFulgens

yeah this might be an interesting idea -- I'm not sure how we would pick a good lower bound though (since it kind of depends on where the repository gets cloned to)

probably not really doable I think

asottile avatar Feb 28 '24 16:02 asottile