Skip to content

Commit

Permalink
frmae for dir walking and handling of untracked files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 25, 2024
1 parent 74704c7 commit 8bb7d66
Show file tree
Hide file tree
Showing 11 changed files with 792 additions and 27 deletions.
62 changes: 35 additions & 27 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions gix-dir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ rust-version = "1.65"
doctest = false

[dependencies]
gix-index = { version = "^0.29.0", path = "../gix-index" }
gix-path = { version = "^0.10.4", path = "../gix-path" }
gix-pathspec = { version = "^0.6.0", path = "../gix-pathspec" }

bstr = { version = "1.5.0", default-features = false }
thiserror = "1.0.56"

[dev-dependencies]
gix-testtools = { path = "../tests/tools" }
50 changes: 50 additions & 0 deletions gix-dir/src/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::{Entry, EntryRef};

/// The git-style filesystem mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Mode {
/// The entry is a blob, executable or not.
Blob,
/// The entry is a symlink.
Symlink,
/// The entry is an ordinary directory, which is either untracked or ignored along with all its contents.
Directory,
/// The entry is a directory which *contains* a `.git` folder.
///
/// Note that we don't know if it's a submodule as we don't have `.gitmodules` information.
Repository,
}

/// The kind of entry as obtained from a directory.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Kind {
/// The provided pathspec prevented further processing as the path didn't match, or it is a `.git` directory.
/// If this happens, no further checks are done so we wouldn't know if the path is also ignored for example (by mention in `.gitignore`).
Excluded,
/// The entry is not tracked by git yet, it was not found in the [index](gix_index::State).
Untracked,
/// The entry is tracked in git.
Tracked,
}

impl EntryRef<'_> {
/// Strip the lifetime to obtain a fully owned copy.
pub fn to_owned(&self) -> Entry {
Entry {
rela_path: self.rela_path.to_owned(),
kind: self.kind,
mode: self.mode,
}
}
}

impl Entry {
/// Obtain an [`EntryRef`] from this instance.
pub fn to_ref(&self) -> EntryRef<'_> {
EntryRef {
rela_path: self.rela_path.as_ref(),
kind: self.kind,
mode: self.mode,
}
}
}
37 changes: 37 additions & 0 deletions gix-dir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
//! A crate for handling a git-style directory walk.
#![deny(rust_2018_idioms)]
#![forbid(unsafe_code)]
use bstr::{BStr, BString};

/// A directory entry, typically obtained using [`walk()`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct EntryRef<'a> {
/// The repository-relative path at which the file or directory could be found, with unix-style component separators.
///
/// To obtain the respective file, join it with the `worktree_root` passed to [`walk()`].
/// The rationale here is that this is a compressed and normalized version compared to the paths we would otherwise get,
/// which is preferable especially when converted to [`Entry`] due to lower memory requirements.
///
/// This also means that the original path to be presented to the user needs to be computed separately.
pub rela_path: &'a BStr,
/// The kind of entry.
pub kind: entry::Kind,
/// Further specify the what the entry is, similar to a file mode.
pub mode: entry::Mode,
}

/// Just like [`EntryRef`], but with all fields owned (and thus without a lifetime to consider).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct Entry {
/// The path at which the file or directory could be found, always with `root` as prefix,
/// the first parameter of [`walk()`].
pub rela_path: BString,
/// The kind of entry.
pub kind: entry::Kind,
/// Further specify the what the entry is, similar to a file mode.
pub mode: entry::Mode,
}

///
pub mod entry;

///
pub mod walk;
pub use walk::function::walk;
Loading

0 comments on commit 8bb7d66

Please sign in to comment.