Skip to content

Commit

Permalink
docs for git-glob (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 13, 2022
1 parent d3a7349 commit 8f4969f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion git-glob/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms)]
#![deny(rust_2018_idioms, missing_docs)]
//! Provide glob [`Patterns`][Pattern] for matching against paths or anything else.

use bstr::BString;

Expand All @@ -20,8 +21,10 @@ pub struct Pattern {
pub base_path: Option<BString>,
}

///
pub mod pattern;

///
pub mod wildmatch;
pub use wildmatch::function::wildmatch;

Expand Down
18 changes: 18 additions & 0 deletions git-glob/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use bstr::{BStr, BString, ByteSlice};
use crate::{pattern, wildmatch, Pattern};

bitflags! {
/// Information about a [`Pattern`].
///
/// Its main purpose is to accelerate pattern matching, or to negate the match result or to
/// keep special rules only applicable when matching paths.
///
/// The mode is typically created when parsing the pattern by inspecting it and isn't typically handled by the user.
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Mode: u32 {
/// The pattern does not contain a sub-directory and - it doesn't contain slashes after removing the trailing one.
Expand All @@ -19,6 +25,9 @@ bitflags! {
}
}

/// Describes whether to match a path case sensitively or not.
///
/// Used in [Pattern::matches_repo_relative_path()].
pub enum Case {
/// The case affects the match
Sensitive,
Expand All @@ -27,6 +36,7 @@ pub enum Case {
}

impl Pattern {
/// Parse the given `text` as pattern, or return `None` if `text` was empty.
pub fn from_bytes(text: &[u8]) -> Option<Self> {
crate::parse::pattern(text).map(|(text, mode, first_wildcard_pos)| Pattern {
text,
Expand Down Expand Up @@ -61,6 +71,8 @@ impl Pattern {
/// `basename_start_pos` is the index at which the `path`'s basename starts.
///
/// Lastly, `case` folding can be configured as well.
///
/// Note that this method uses shortcuts to accelerate simple patterns.
pub fn matches_repo_relative_path<'a>(
&self,
path: impl Into<&'a BStr>,
Expand Down Expand Up @@ -113,6 +125,12 @@ impl Pattern {
}
}

/// See if `value` matches this pattern in the given `mode`.
///
/// `mode` can identify `value` as path which won't match the slash character, and can match
/// strings with cases ignored as well. Note that the case folding performed here is ASCII only.
///
/// Note that this method uses some shortcuts to accelerate simple patterns.
pub fn matches<'a>(&self, value: impl Into<&'a BStr>, mode: wildmatch::Mode) -> bool {
let value = value.into();
match self.first_wildcard_pos {
Expand Down
4 changes: 4 additions & 0 deletions git-glob/src/wildmatch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bitflags::bitflags;
bitflags! {
/// The match mode employed in [`Pattern::matches()`][crate::Pattern::matches()].
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Mode: u8 {
/// Let globs not match the slash `/` literal.
Expand Down Expand Up @@ -334,6 +335,9 @@ pub(crate) mod function {
t.next().map(|_| NoMatch).unwrap_or(Match)
}

/// Employ pattern matching to see if `value` matches `pattern`.
///
/// `mode` can be used to adjust the way the matching is performed.
pub fn wildmatch(pattern: &BStr, value: &BStr, mode: Mode) -> bool {
match_recursive(pattern, value, mode) == Result::Match
}
Expand Down
3 changes: 2 additions & 1 deletion git-repository/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ max-performance = ["git-features/parallel", "git-features/zlib-ng-compat", "git-
local-time-support = ["git-actor/local-time-support"]
## Re-export stability tier 2 crates for convenience and make `Repository` struct fields with types from these crates publicly accessible.
## Doing so is less stable than the stability tier 1 that `git-repository` is a member of.
unstable = ["git-index", "git-worktree", "git-mailmap"]
unstable = ["git-index", "git-worktree", "git-mailmap", "git-glob"]
## Print debugging information about usage of object database caches, useful for tuning cache sizes.
cache-efficiency-debug = ["git-features/cache-efficiency-debug"]

Expand Down Expand Up @@ -78,6 +78,7 @@ git-mailmap = { version = "^0.1.0", path = "../git-mailmap", optional = true }
git-features = { version = "^0.20.0", path = "../git-features", features = ["progress"] }

# unstable only
git-glob = { version = "^0.1.0", path = "../git-glob", optional = true }
git-index = { version = "^0.2.0", path = "../git-index", optional = true }
git-worktree = { version = "^0.1.0", path = "../git-worktree", optional = true }

Expand Down
3 changes: 3 additions & 0 deletions git-repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
//! * [`actor`]
//! * [`bstr`][bstr]
//! * [`index`]
//! * [`glob`]
//! * [`worktree`]
//! * [`mailmap`]
//! * [`objs`]
Expand Down Expand Up @@ -126,6 +127,8 @@ pub use git_diff as diff;
use git_features::threading::OwnShared;
#[cfg(feature = "unstable")]
pub use git_features::{parallel, progress, progress::Progress, threading};
#[cfg(all(feature = "unstable", feature = "git-glob"))]
pub use git_glob as glob;
pub use git_hash as hash;
#[doc(inline)]
#[cfg(all(feature = "unstable", feature = "git-index"))]
Expand Down

0 comments on commit 8f4969f

Please sign in to comment.