goelhardik / ignore

.gitignore based parser implemented in C# according to the .gitignore spec 2.29.2.
MIT License
50 stars 5 forks source link

Make it more clear that we should only manipulate relative path #26

Closed jairbubbles closed 3 years ago

jairbubbles commented 3 years ago

If you pass an absolute path, dependind on the rule it will work correctly or not. It might give the impression that it's ok to use it like that.

Maybe it should throw when the path is rooted?

ignore.IsIgnored("C:\repos\foo.txt"); // => throw
ignore.IsIgnored("\foo.txt"); // => throw
ignore.IsIgnored("foo.txt"); // => OK
goelhardik commented 3 years ago

Looks like Path.IsPathRooted() doesn't handle Windows style paths on Linux/OSX. So it returns false for C:\foo on Linux/OSX. Is there any other alternative here that you are aware of @jairbubbles ?

jairbubbles commented 3 years ago

Looks like Path.IsPathRooted() doesn't handle Windows style paths on Linux/OSX

Is it a valid use case, I mean on Linux we would use Linux paths.

The validation could slow down the evaluation, so I'm wondering if making it more explicit in the documentation and function parameter would be enough.

On a different subject but related, I was wondering if you had plans to support multiple .gitignore files in a folder hierarchy. This is not widely used but supported by git. I feel like a class like RepositoryIgnore(string repoistoryPath).

goelhardik commented 3 years ago

Good point about the performance. Will make it more explicit in the documentation.

Regarding the multiple .gitignore files in hierarchy - are there use cases for this for apps who want to do it in-memory? Libgit2sharp can already do it for file systems by using native git. For Ignore, the use case I had in mind was for apps who have a file path in-memory and just want to do git style path matching. See the corresponding nodejs library, they have a similar approach.

jairbubbles commented 3 years ago

Well in my case I can't rely on libGit2sharp as I don't want to keep an handle on the repository and still I'm not sure you can ask it directly if a file is ignored or not (in a fast way). Also worth mentioning, in my case it's plugged on a file system watcher and I need to quickly now if it's an ignored file or not. The application also handles multiple repositories.

I would love to show you more about the application but it's not open sourced (yet!).

goelhardik commented 3 years ago

libGit2Sharp does have the interface to detect if the file is ignored or not. I have used this for non-huge repos and the performance seems okay so far to me. Your case could have more changes/big repos, but maybe give it a try? That's true though that the repo handles would have to be kept open.

using var repo = new Repository(RepoPath);
repo.Ignore.IsPathIgnored(file);

I still feel like encapsulating handling of hierarchical .gitignore files is somewhat out of scope for this library. If we make it deal with file systems (reading .gitignore files from the file system by traversing hierarchy), it would essentially be doing what libGit2Sharp is already doing. Then there is also the global .gitignore file to complicate things further. Alternatively, we could pass in a list of paths and contents of .gitignore files to the RepositoryIgnore ctor, but it sounds like a complex interface to expose.

Could your app keep track of the paths of .gitignores and then call Ignore multiple times to figure out if the file is ignored?

jairbubbles commented 3 years ago

I'm fine with the fact that it's out of scope for this library.

The other thing I'm doing in the app is to keep a big static dictionary with all the rules to reduce memory usage. Repositories are often sharing the same rules.