Skip to content

Commit

Permalink
pack multi-index info subcommand (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 6, 2022
1 parent e6a3d43 commit 21c2dd5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ Please see _'Development Status'_ for a listing of all crates and their capabili
* [x] **create** - create a pack from given objects or tips of the commit graph.
* [ ] **send** - create a pack and send it using the pack protocol to stdout, similar to 'git-upload-pack',
for consumption by **pack-receive** or _git-receive-pack_
* **index**
* [x] [create](https://asciinema.org/a/352941) - create an index file by streaming a pack file as done during clone
* [x] support for thin packs (as needed for fetch/pull)
- **multi-index**
* [x] **info** - print information about the file
* [x] **create** - create a multi-index from pack indices
* [x] **verify** - check the file for consistency
- **index**
* [x] [create](https://asciinema.org/a/352941) - create an index file by streaming a pack file as done during clone
* [x] support for thin packs (as needed for fetch/pull)
* **commit-graph**
* [x] **verify** - assure that a commit-graph is consistent
* **repository**
Expand Down
36 changes: 36 additions & 0 deletions gitoxide-core/src/pack/multi_index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{io::BufWriter, path::PathBuf, sync::atomic::AtomicBool};

use crate::OutputFormat;
use git_repository as git;
use git_repository::Progress;

Expand Down Expand Up @@ -32,3 +33,38 @@ pub fn create(
out.into_inner()?.commit()?;
Ok(())
}

mod info {
use std::path::PathBuf;

#[cfg_attr(feature = "serde1", derive(serde::Serialize))]
pub struct Statistics {
pub path: PathBuf,
pub num_objects: u32,
pub index_names: Vec<PathBuf>,
pub object_hash: String,
}
}

pub fn info(
multi_index_path: PathBuf,
format: OutputFormat,
out: impl std::io::Write,
mut err: impl std::io::Write,
) -> anyhow::Result<()> {
let file = git::odb::pack::multi_index::File::at(&multi_index_path)?;
if format == OutputFormat::Human {
writeln!(err, "Defaulting to JSON as human format isn't implemented").ok();
}
#[cfg(feature = "serde1")]
serde_json::to_writer_pretty(
out,
&info::Statistics {
path: multi_index_path,
num_objects: file.num_objects(),
index_names: file.index_names().to_vec(),
object_hash: file.object_hash().to_string(),
},
)?;
Ok(())
}
10 changes: 5 additions & 5 deletions gitoxide-core/src/repository/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ pub fn info(
treeish: Option<&str>,
extended: bool,
format: OutputFormat,
out: &mut dyn io::Write,
err: &mut dyn io::Write,
out: impl io::Write,
mut err: impl io::Write,
) -> anyhow::Result<()> {
if format == OutputFormat::Human {
writeln!(err, "Only JSON is implemented - using that instead")?;
Expand All @@ -148,7 +148,7 @@ pub fn entries(
recursive: bool,
extended: bool,
format: OutputFormat,
out: &mut dyn io::Write,
mut out: impl io::Write,
) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("Only human output format is supported at the moment");
Expand All @@ -158,13 +158,13 @@ pub fn entries(
let tree = treeish_to_tree(treeish, &repo)?;

if recursive {
let mut delegate = entries::Traverse::new(extended.then(|| &repo), out.into());
let mut delegate = entries::Traverse::new(extended.then(|| &repo), Some(&mut out));
tree.traverse().breadthfirst(&mut delegate)?;
} else {
for entry in tree.iter() {
let entry = entry?;
format_entry(
&mut *out,
&mut out,
&entry.inner,
entry.inner.filename,
extended
Expand Down
8 changes: 8 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ pub fn main() -> Result<()> {
)
.map(|_| ()),
pack::Subcommands::MultiIndex(multi_index::Platform { multi_index_path, cmd }) => match cmd {
pack::multi_index::Subcommands::Info => prepare_and_run(
"pack-multi-index-info",
verbose,
progress,
progress_keep_open,
core::pack::multi_index::PROGRESS_RANGE,
move |_progress, out, err| core::pack::multi_index::info(multi_index_path, format, out, err),
),
pack::multi_index::Subcommands::Verify => prepare_and_run(
"pack-multi-index-verify",
verbose,
Expand Down
4 changes: 3 additions & 1 deletion src/plumbing/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub mod pack {
#[derive(Debug, clap::Parser)]
pub struct Platform {
/// The path to the index file.
#[clap(short = 'i', long, default_value = ".git/objects/packs/multi-pack-index")]
#[clap(short = 'i', long, default_value = ".git/objects/pack/multi-pack-index")]
pub multi_index_path: PathBuf,

/// Subcommands
Expand All @@ -267,6 +267,8 @@ pub mod pack {

#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// Print general information about a multi-index file
Info,
/// Verify a multi-index quickly without inspecting objects themselves
Verify,
/// Create a multi-pack index from one or more pack index files, overwriting possibloy existing files.
Expand Down

0 comments on commit 21c2dd5

Please sign in to comment.