-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Print basic index information, including the tree extension (#293)
The latter is the one we have to maintain/create when creating and index from a tree.
- Loading branch information
Showing
8 changed files
with
185 additions
and
106 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use git_repository as git; | ||
use std::convert::TryFrom; | ||
|
||
mod ext { | ||
#[cfg_attr(feature = "serde1", derive(serde::Serialize))] | ||
pub(crate) struct Tree { | ||
name: String, | ||
/// Only set if there are any entries in the index we are associated with. | ||
id: Option<tree::NodeId>, | ||
children: Vec<Tree>, | ||
} | ||
|
||
mod tree { | ||
use git_repository as git; | ||
use git_repository::bstr::ByteSlice; | ||
|
||
impl<'a> From<&'a git::index::extension::Tree> for super::Tree { | ||
fn from(t: &'a git_repository::index::extension::Tree) -> Self { | ||
super::Tree { | ||
name: t.name.as_bstr().to_string(), | ||
id: t.id.as_ref().map(|id| NodeId { | ||
entry_count: id.entry_count, | ||
id: id.id.to_hex().to_string(), | ||
}), | ||
children: t.children.iter().map(|t| t.into()).collect(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct NodeId { | ||
/// The id of the directory tree of the associated tree object. | ||
id: String, | ||
/// The amount of non-tree entries contained within, and definitely not zero. | ||
entry_count: u32, | ||
} | ||
} | ||
} | ||
|
||
#[cfg_attr(feature = "serde1", derive(serde::Serialize))] | ||
pub(crate) struct EntryKind { | ||
dir: usize, | ||
file: usize, | ||
executable: usize, | ||
symlink: usize, | ||
submodule: usize, | ||
other: usize, | ||
} | ||
|
||
#[cfg_attr(feature = "serde1", derive(serde::Serialize))] | ||
pub(crate) struct EntryFlag { | ||
intent_to_add: usize, | ||
skip_worktree: usize, | ||
} | ||
|
||
#[cfg_attr(feature = "serde1", derive(serde::Serialize))] | ||
pub(crate) struct Entries { | ||
stage_0: usize, | ||
stage_1: usize, | ||
stage_2: usize, | ||
kind: EntryKind, | ||
flags: EntryFlag, | ||
} | ||
|
||
#[cfg_attr(feature = "serde1", derive(serde::Serialize))] | ||
pub(crate) struct Extensions { | ||
count: usize, | ||
tree: Option<ext::Tree>, | ||
} | ||
|
||
#[cfg_attr(feature = "serde1", derive(serde::Serialize))] | ||
pub(crate) struct Collection { | ||
version: u8, | ||
checksum: String, | ||
entries: Entries, | ||
extensions: Extensions, | ||
} | ||
|
||
impl TryFrom<git::index::File> for Collection { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(f: git::index::File) -> Result<Self, Self::Error> { | ||
Ok(Collection { | ||
version: f.version() as u8, | ||
checksum: f.checksum.to_hex().to_string(), | ||
extensions: { | ||
let mut count = 0; | ||
let tree = f.tree().map(|tree| { | ||
count += 1; | ||
tree.into() | ||
}); | ||
if let Some(_) = f.link() { | ||
count += 1 | ||
}; | ||
if let Some(_) = f.resolve_undo() { | ||
count += 1 | ||
}; | ||
if let Some(_) = f.untracked() { | ||
count += 1 | ||
}; | ||
if let Some(_) = f.fs_monitor() { | ||
count += 1 | ||
}; | ||
Extensions { count, tree } | ||
}, | ||
entries: { | ||
let (mut stage_0, mut stage_1, mut stage_2) = (0, 0, 0); | ||
let (mut dir, mut file, mut executable, mut symlink, mut submodule, mut other) = (0, 0, 0, 0, 0, 0); | ||
let (mut intent_to_add, mut skip_worktree) = (0, 0); | ||
for entry in f.entries() { | ||
match entry.flags.stage() { | ||
0 => stage_0 += 1, | ||
1 => stage_1 += 1, | ||
2 => stage_2 += 1, | ||
invalid => anyhow::bail!("Invalid stage {} encountered", invalid), | ||
} | ||
match entry.mode { | ||
git::index::entry::Mode::DIR => dir += 1, | ||
git::index::entry::Mode::FILE => file += 1, | ||
git::index::entry::Mode::FILE_EXECUTABLE => executable += 1, | ||
git::index::entry::Mode::SYMLINK => symlink += 1, | ||
git::index::entry::Mode::COMMIT => submodule += 1, | ||
_ => other += 1, | ||
} | ||
if entry.flags.contains(git::index::entry::Flags::INTENT_TO_ADD) { | ||
intent_to_add += 1; | ||
} | ||
if entry.flags.contains(git::index::entry::Flags::SKIP_WORKTREE) { | ||
skip_worktree += 1; | ||
} | ||
} | ||
Entries { | ||
stage_0, | ||
stage_1, | ||
stage_2, | ||
kind: EntryKind { | ||
dir, | ||
file, | ||
executable, | ||
symlink, | ||
submodule, | ||
other, | ||
}, | ||
flags: EntryFlag { | ||
intent_to_add, | ||
skip_worktree, | ||
}, | ||
} | ||
}, | ||
}) | ||
} | ||
} |
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