Skip to content

Commit

Permalink
a first sketch of access odb information using a sub-command (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 6, 2022
1 parent 6c10e09 commit 89b628a
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 28 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Please see _'Development Status'_ for a listing of all crates and their capabili
* **tree**
* [x] **entries** - list tree entries for a single tree or recursively
* [x] **info** - display tree statistics
* **odb**
* [x] **info** - display odb statistics
* [x] **entries** - display all object ids in the object database
* **index**
* [x] **entries** - show detailed entry information for human or machine consumption (via JSON)
* [x] **verify** - check the index for consistency
Expand Down
10 changes: 10 additions & 0 deletions git-odb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ impl Store {
pub fn path(&self) -> &std::path::Path {
&self.path
}

/// The kind of object hash to assume when dealing with pack indices and pack data files.
pub fn object_hash(&self) -> git_hash::Kind {
self.object_hash
}

/// Whether or not we are allowed to use multi-pack indices
pub fn use_multi_pack_index(&self) -> bool {
self.use_multi_pack_index
}
}

/// Create a new cached handle to the object store with support for additional options.
Expand Down
1 change: 1 addition & 0 deletions git-odb/src/store_impls/dynamic/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ pub(crate) struct MutableIndexAndPack {

/// A snapshot about resource usage.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Metrics {
/// The total amount of handles which can be used to access object information.
pub num_handles: usize,
Expand Down
64 changes: 64 additions & 0 deletions gitoxide-core/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,67 @@ pub fn init(directory: Option<PathBuf>) -> Result<git_repository::Path> {
pub mod tree;

pub mod verify;

pub mod odb {
use crate::OutputFormat;
use anyhow::bail;
use git_repository as git;
use std::io;
use std::path::PathBuf;

mod info {
use git_repository::odb::store;
use std::path::PathBuf;

#[cfg_attr(feature = "serde1", derive(serde::Serialize))]
pub struct Statistics {
pub path: PathBuf,
pub object_hash: String,
pub use_multi_pack_index: bool,
pub metrics: store::Metrics,
}
}

#[cfg_attr(not(feature = "serde1"), allow(unused_variables))]
pub fn info(
repository: PathBuf,
format: OutputFormat,
out: impl io::Write,
mut err: impl io::Write,
) -> anyhow::Result<()> {
if format == OutputFormat::Human {
writeln!(err, "Only JSON is implemented - using that instead")?;
}

let repo = git::open(repository)?.apply_environment();
let store = repo.objects.store_ref();
let stats = info::Statistics {
path: store.path().into(),
object_hash: store.object_hash().to_string(),
use_multi_pack_index: store.use_multi_pack_index(),
metrics: store.metrics(),
};

#[cfg(feature = "serde1")]
{
serde_json::to_writer_pretty(out, &stats)?;
}

Ok(())
}

pub fn entries(repository: PathBuf, format: OutputFormat, mut out: impl io::Write) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("Only human output format is supported at the moment");
}

let repo = git::open(repository)?.apply_environment();

for object in repo.objects.iter()? {
let object = object?;
writeln!(out, "{}", object)?;
}

Ok(())
}
}
78 changes: 50 additions & 28 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,35 +145,57 @@ pub fn main() -> Result<()> {
),
},
Subcommands::Repository(repo::Platform { repository, cmd }) => match cmd {
repo::Subcommands::Tree {
cmd:
repo::tree::Subcommands::Entries {
treeish,
recursive,
extended,
repo::Subcommands::Odb { cmd } => match cmd {
repo::odb::Subcommands::Entries => prepare_and_run(
"repository-odb-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::repository::odb::entries(repository, format, out),
),
repo::odb::Subcommands::Info => prepare_and_run(
"repository-odb-info",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| core::repository::odb::info(repository, format, out, err),
),
},
repo::Subcommands::Tree { cmd } => match cmd {
repo::tree::Subcommands::Entries {
treeish,
recursive,
extended,
} => prepare_and_run(
"repository-tree-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::tree::entries(
repository,
treeish.as_deref(),
recursive,
extended,
format,
out,
)
},
} => prepare_and_run(
"repository-tree-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::tree::entries(repository, treeish.as_deref(), recursive, extended, format, out)
},
),
repo::Subcommands::Tree {
cmd: repo::tree::Subcommands::Info { treeish, extended },
} => prepare_and_run(
"repository-tree-info",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::repository::tree::info(repository, treeish.as_deref(), extended, format, out, err)
},
),
),
repo::tree::Subcommands::Info { treeish, extended } => prepare_and_run(
"repository-tree-info",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::repository::tree::info(repository, treeish.as_deref(), extended, format, out, err)
},
),
},
repo::Subcommands::Verify {
args:
pack::VerifyOptions {
Expand Down
15 changes: 15 additions & 0 deletions src/plumbing/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ pub mod repo {
#[clap(subcommand)]
cmd: tree::Subcommands,
},
/// Interact with the object database.
Odb {
#[clap(subcommand)]
cmd: odb::Subcommands,
},
}

pub mod odb {
#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// Print all object names.
Entries,
/// Provide general information about the object database.
Info,
}
}

pub mod tree {
Expand Down

0 comments on commit 89b628a

Please sign in to comment.