-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Check if file is ignored according to git #98
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use { | ||
anyhow::{Context, Result}, | ||
git_repository::{ | ||
self as git, | ||
prelude::FindExt, | ||
Repository, | ||
}, | ||
std::path::{Path, PathBuf}, | ||
}; | ||
|
||
/// An object able to tell whether a file is excluded | ||
/// by gitignore rules | ||
pub struct Ignorer { | ||
repo: Repository, | ||
} | ||
|
||
impl Ignorer { | ||
|
||
/// Create an Ignorer from any directory path: the closest | ||
/// surrounding git repository will be found (if there's one) | ||
/// and its gitignore rules used. | ||
/// | ||
/// root_path is assumed to exist and be a directory | ||
pub(crate) fn new(root_path: &Path) -> Result<Self> { | ||
let repo = git::discover(root_path)?; | ||
Ok(Self { repo }) | ||
} | ||
|
||
/// Tell whether the given path is excluded according to | ||
/// either the global gitignore rules or the ones of the repository | ||
pub fn excludes(&mut self, file_path: &Path) -> Result<bool> { | ||
let worktree = self.repo.worktree().context("a worktree should exist")?; | ||
let index = worktree.index()?; | ||
|
||
// the "Cache" is the structure allowing checking exclusion | ||
let mut cache = worktree.excludes(&index, None)?; | ||
|
||
// cache.at_path panics if not provided a path relative | ||
// to the work directory, so we compute the relative path | ||
let Some(work_dir) = self.repo.work_dir() else { | ||
return Ok(false); | ||
}; | ||
let Ok(relative_path) = file_path.strip_prefix(work_dir) else { | ||
return Ok(false); | ||
}; | ||
|
||
// cache.at_path panics if the relative path is empty, so | ||
// we must check that | ||
if relative_path.as_os_str().is_empty() { | ||
return Ok(true); | ||
}; | ||
|
||
let platform = cache.at_path(relative_path, Some(file_path.is_dir()), |oid, buf| { | ||
self.repo.objects.find_blob(oid, buf) | ||
})?; | ||
|
||
Ok(platform.is_excluded()) | ||
} | ||
|
||
/// Return Ok(false) when at least one file is included (i.e. we should | ||
/// execute the job) | ||
pub fn excludes_all(&mut self, paths: &[PathBuf]) -> Result<bool> { | ||
for path in paths { | ||
if !self.excludes(path)? { | ||
return Ok(false); | ||
} | ||
} | ||
Ok(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think as it stands, each path will trigger reading all gitignore files. They are indeed held in the
excludes()
data structure and ideally it is kept. I see how this isn't possible right now, and believe that the current reference is likely a premature optimization rather than a necessity. This will change for sure - and it's done in the latest main, which should greatly improve performance as the cache can actually reuse state if it's kept around.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel you. I fell in the same trap in my first libs too, and it's a pain to fix.
TBH checking whether a file is excluded takes less than 1 ms right now and that's fine for bacon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will think about this - the number one usage of lifetimes in structs is platforms, they add some data around an operation and perform it, keeping a reference to their originating
Repository
. As long as these are basically free, I think they can be created on demand with theRepository
being cloned to where it is needed - that's the intent.If they aren't free though, like the
Cache
here, I think it's good advice to rather clone theRepository
into it to make it standalone, or do whatever else it takes. I think some useful rule emerges from this experience and I will put it into words inDEVELOPMENT.md
to make it official.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW @Byron I don't know if you may find this interesting, but I've also implemented a gitignoring stack (stacking the parsed gitignore files as I imagine you do): https://github.com/Canop/broot/blob/main/src/git/ignore.rs#L155
This was done for broot with very specific performance concerns (breathfirst tree diving).
The reasons I didn't take that for bacon were
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sharing! I love the perceived simplicity of the git-ignore implementation, it fits in 240 lines after all :)!
With the
Cache
type unchained from the lifetime inmain
you would now be able to reuse it for each lookup and that should yield much better performance.From a correctness point of view, it's probably (hopefully) a good idea to use
gitoxide
even if the performance is just similar, as I tried my best to validate the implementation against git with many many test cases. Of course I hope you won't take my word for it and validate it yourself,gitoxide
strives to yield the same results asgit
.Thus, I hope you will end up using
gitoxide
in more of your projects and if there is anything preventing that, I'd love to know to get a chance to fix it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gitignoring is only part of using git, and I definitely don't want to expand into building a general git crate while there's already an ambitious project, so it's my clear intention to try and use gitoxide ;)