Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 181d69c

Browse files
committed
first pieces of the index-from-pack journey tests
1 parent 81dfb05 commit 181d69c

6 files changed

Lines changed: 70 additions & 34 deletions

File tree

gitoxide-core/src/pack/index.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::OutputFormat;
12
use git_features::progress::Progress;
23
use git_odb::pack;
34
use std::{fs, io, path::PathBuf, str::FromStr};
@@ -47,24 +48,11 @@ impl From<IterationMode> for pack::data::iter::Mode {
4748
}
4849
}
4950

50-
pub struct Context {
51+
pub struct Context<W: io::Write> {
5152
pub thread_limit: Option<usize>,
5253
pub iteration_mode: IterationMode,
53-
}
54-
55-
impl From<Context> for pack::bundle::write::Options {
56-
fn from(
57-
Context {
58-
thread_limit,
59-
iteration_mode,
60-
}: Context,
61-
) -> Self {
62-
pack::bundle::write::Options {
63-
thread_limit,
64-
iteration_mode: iteration_mode.into(),
65-
index_kind: pack::index::Kind::default(),
66-
}
67-
}
54+
pub format: OutputFormat,
55+
pub out: W,
6856
}
6957

7058
pub fn stream_len(mut s: impl io::Seek) -> io::Result<u64> {
@@ -77,28 +65,47 @@ pub fn stream_len(mut s: impl io::Seek) -> io::Result<u64> {
7765
Ok(len)
7866
}
7967

80-
pub fn from_pack<P>(
68+
pub fn from_pack<P, W: io::Write>(
8169
pack: Option<PathBuf>,
8270
directory: Option<PathBuf>,
8371
progress: P,
84-
context: Context,
72+
ctx: Context<W>,
8573
) -> anyhow::Result<()>
8674
where
8775
P: Progress,
8876
<<P as Progress>::SubProgress as Progress>::SubProgress: Send,
8977
{
9078
use anyhow::Context;
79+
let options = pack::bundle::write::Options {
80+
thread_limit: ctx.thread_limit,
81+
iteration_mode: ctx.iteration_mode.into(),
82+
index_kind: pack::index::Kind::default(),
83+
};
84+
let out = ctx.out;
85+
let format = ctx.format;
9186
match pack {
9287
Some(pack) => {
9388
let pack_len = pack.metadata()?.len();
9489
let pack_file = fs::File::open(pack)?;
95-
pack::Bundle::write_to_directory(pack_file, Some(pack_len), directory, progress, context.into())
90+
pack::Bundle::write_to_directory(pack_file, Some(pack_len), directory, progress, options)
9691
}
9792
None => {
9893
let stdin = io::stdin();
99-
pack::Bundle::write_to_directory(stdin.lock(), None, directory, progress, context.into())
94+
pack::Bundle::write_to_directory(stdin.lock(), None, directory, progress, options)
10095
}
10196
}
10297
.with_context(|| "Failed to write pack and index")
103-
.map(|_| ())
98+
.map(|res| {
99+
match format {
100+
OutputFormat::Human => drop(human_output(out, res)),
101+
#[cfg(feature = "serde1")]
102+
OutputFormat::Json => serde_json::to_writer_pretty(out, &res)?,
103+
};
104+
()
105+
})
106+
}
107+
108+
fn human_output(mut out: impl io::Write, res: pack::bundle::write::Outcome) -> io::Result<()> {
109+
writeln!(&mut out, "index: {}", res.index.index_hash)?;
110+
writeln!(&mut out, "pack: {}", res.index.pack_hash)
104111
}

gitoxide-core/src/pack/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ where
163163
match output_statistics {
164164
Some(OutputFormat::Human) => drop(print_statistics(&mut out, stats)),
165165
#[cfg(feature = "serde1")]
166-
Some(OutputFormat::Json) => drop(serde_json::to_writer_pretty(out, stats)),
166+
Some(OutputFormat::Json) => serde_json::to_writer_pretty(out, stats)?,
167167
_ => {}
168168
};
169169
}

src/plumbing/lean.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ mod options {
4141
/// specify how to iterate the pack, defaults to 'verify'
4242
///
4343
/// Valid values are
44-
/// - as-is
45-
/// * do not do anything and expect the pack file to be valid as per the trailing hash
46-
/// - verify
47-
/// * the input ourselves and validate that it matches with the hash provided in the pack
48-
/// - restore
49-
/// * hash the input ourselves and ignore failing entries, instead finish the pack with the hash we computed
44+
///
45+
/// **as-is** do not do anything and expect the pack file to be valid as per the trailing hash,
46+
/// **verify** the input ourselves and validate that it matches with the hash provided in the pack,
47+
/// **restore** hash the input ourselves and ignore failing entries, instead finish the pack with the hash we computed
5048
#[argh(option, short = 'i')]
5149
pub iteration_mode: Option<core::pack::index::IterationMode>,
5250

@@ -144,6 +142,8 @@ mod options {
144142
use anyhow::Result;
145143
use git_features::progress;
146144
use gitoxide_core as core;
145+
use gitoxide_core::OutputFormat;
146+
use std::io;
147147
use std::io::{stderr, stdout};
148148

149149
#[cfg(not(any(
@@ -196,6 +196,8 @@ pub fn main() -> Result<()> {
196196
core::pack::index::Context {
197197
thread_limit,
198198
iteration_mode: iteration_mode.unwrap_or_default(),
199+
format: OutputFormat::Human,
200+
out: io::stdout(),
199201
},
200202
)
201203
}

src/plumbing/pretty.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,10 @@ mod options {
5151
/// Specify how to iterate the pack, defaults to 'verify'
5252
///
5353
/// Valid values are
54-
/// - as-is
55-
/// * do not do anything and expect the pack file to be valid as per the trailing hash
56-
/// - verify
57-
/// * the input ourselves and validate that it matches with the hash provided in the pack
58-
/// - restore
59-
/// * hash the input ourselves and ignore failing entries, instead finish the pack with the hash we computed
54+
///
55+
/// **as-is** do not do anything and expect the pack file to be valid as per the trailing hash,
56+
/// **verify** the input ourselves and validate that it matches with the hash provided in the pack,
57+
/// **restore** hash the input ourselves and ignore failing entries, instead finish the pack with the hash we computed
6058
#[clap(
6159
long,
6260
short = "i",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
index: 560eba66e6b391eb83efc3ec9fc8a3087788911c
2+
pack: f1cd3cc7bc63a4a2b357a475a58ad49b40355470

tests/stateless-journey.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ title "CLI ${kind}"
4343
)
4444

4545
title plumbing
46+
(when "running 'index-from-pack"
47+
PACK_FILE="$fixtures/packs/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack"
48+
(with "a valid and complete pack file"
49+
(with "NO output directory specified"
50+
(with "pack file passed as file"
51+
it "generates an index and outputs pack and index information" && {
52+
WITH_SNAPSHOT="$snapshot/plumbing-index-from-pack-no-output-dir-success" \
53+
expect_run $SUCCESSFULLY "$exe_plumbing" index-from-pack -p "${PACK_FILE}"
54+
}
55+
)
56+
(with "pack file passed from stdin"
57+
it "generates an index and outputs pack and index information" && {
58+
WITH_SNAPSHOT="$snapshot/plumbing-index-from-pack-no-output-dir-success" \
59+
expect_run $SUCCESSFULLY "$exe_plumbing" index-from-pack < "${PACK_FILE}"
60+
}
61+
)
62+
)
63+
(sandbox
64+
(with "with an output directory specified"
65+
66+
)
67+
)
68+
)
69+
(with "'recover' iteration mode"
70+
71+
)
72+
)
4673
(when "running 'pack-explode"
4774
PACK_FILE="$fixtures/packs/pack-11fdfa9e156ab73caae3b6da867192221f2089c2"
4875
(with "no objects directory specified"

0 commit comments

Comments
 (0)