|
| 1 | +use crate::{Entry, EntryRef}; |
| 2 | + |
| 3 | +/// The kind of the entry. |
| 4 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] |
| 5 | +pub enum Kind { |
| 6 | + /// The entry is a blob, executable or not. |
| 7 | + File, |
| 8 | + /// The entry is a symlink. |
| 9 | + Symlink, |
| 10 | + /// The entry is an ordinary directory. |
| 11 | + Directory, |
| 12 | + /// The entry is a directory which *contains* a `.git` folder. |
| 13 | + Repository, |
| 14 | +} |
| 15 | + |
| 16 | +/// The kind of entry as obtained from a directory. |
| 17 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] |
| 18 | +pub enum Status { |
| 19 | + /// The filename of an entry was `.git`, which is generally pruned. |
| 20 | + DotGit, |
| 21 | + /// The provided pathspec prevented further processing as the path didn't match, or it is a `.git` directory. |
| 22 | + /// 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`). |
| 23 | + Pruned, |
| 24 | + /// The entry is ignored as per `.gitignore` files and their rules. |
| 25 | + /// |
| 26 | + /// If this is a directory, then its entire contents is ignored. Otherwise, possibly due to configuration, individual ignored files are listed. |
| 27 | + Ignored(gix_ignore::Kind), |
| 28 | + /// The entry is not tracked by git yet, it was not found in the [index](gix_index::State). |
| 29 | + /// |
| 30 | + /// If it's a directory, the entire directory contents is untracked. |
| 31 | + Untracked, |
| 32 | + /// The entry is tracked in git. |
| 33 | + Tracked, |
| 34 | +} |
| 35 | + |
| 36 | +impl Status { |
| 37 | + /// Returns `true` if this entry will generally not be traversed into. |
| 38 | + pub fn is_pruned(&self) -> bool { |
| 39 | + matches!(self, Status::DotGit | Status::Pruned) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl EntryRef<'_> { |
| 44 | + /// Strip the lifetime to obtain a fully owned copy. |
| 45 | + pub fn to_owned(&self) -> Entry { |
| 46 | + Entry { |
| 47 | + rela_path: self.rela_path.to_owned(), |
| 48 | + status: self.status, |
| 49 | + kind: self.kind, |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl Entry { |
| 55 | + /// Obtain an [`EntryRef`] from this instance. |
| 56 | + pub fn to_ref(&self) -> EntryRef<'_> { |
| 57 | + EntryRef { |
| 58 | + rela_path: self.rela_path.as_ref(), |
| 59 | + status: self.status, |
| 60 | + kind: self.kind, |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl From<std::fs::FileType> for Kind { |
| 66 | + fn from(value: std::fs::FileType) -> Self { |
| 67 | + if value.is_dir() { |
| 68 | + Kind::Directory |
| 69 | + } else if value.is_symlink() { |
| 70 | + Kind::Symlink |
| 71 | + } else { |
| 72 | + Kind::File |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl Status { |
| 78 | + /// Return `true` if this directory isn't ignored, and is not a repository. |
| 79 | + /// This implements the default rules of `git status`, which is good for a minimal traversal through |
| 80 | + /// tracked and non-ignored portions of a worktree. |
| 81 | + pub fn can_recurse(&self, file_type: Option<Kind>) -> bool { |
| 82 | + if file_type.map_or(true, |ft| !ft.is_recursable_dir()) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + match self { |
| 86 | + Status::DotGit | Status::Pruned => false, |
| 87 | + Status::Ignored(_) => false, // TODO: needs option to allow recursing into ignored directories |
| 88 | + Status::Untracked | Status::Tracked => true, |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl Kind { |
| 94 | + fn is_recursable_dir(&self) -> bool { |
| 95 | + matches!(self, Kind::Directory) |
| 96 | + } |
| 97 | + |
| 98 | + /// Return `true` if this is a directory on disk. Note that this is true for repositories as well. |
| 99 | + pub fn is_dir(&self) -> bool { |
| 100 | + matches!(self, Kind::Directory | Kind::Repository) |
| 101 | + } |
| 102 | +} |
0 commit comments