Skip to content

Commit

Permalink
support for JSON format output
Browse files Browse the repository at this point in the history
Not super pretty, but good enough
  • Loading branch information
Byron committed Aug 7, 2020
1 parent 181d69c commit 1931575
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 30 deletions.
18 changes: 8 additions & 10 deletions gitoxide-core/src/pack/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
};
let out = ctx.out;
let format = ctx.format;
match pack {
let res = match pack {
Some(pack) => {
let pack_len = pack.metadata()?.len();
let pack_file = fs::File::open(pack)?;
Expand All @@ -94,15 +94,13 @@ where
pack::Bundle::write_to_directory(stdin.lock(), None, directory, progress, options)
}
}
.with_context(|| "Failed to write pack and index")
.map(|res| {
match format {
OutputFormat::Human => drop(human_output(out, res)),
#[cfg(feature = "serde1")]
OutputFormat::Json => serde_json::to_writer_pretty(out, &res)?,
};
()
})
.with_context(|| "Failed to write pack and index")?;
match format {
OutputFormat::Human => drop(human_output(out, res)),
#[cfg(feature = "serde1")]
OutputFormat::Json => serde_json::to_writer_pretty(out, &res)?,
};
Ok(())
}

fn human_output(mut out: impl io::Write, res: pack::bundle::write::Outcome) -> io::Result<()> {
Expand Down
38 changes: 21 additions & 17 deletions src/plumbing/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ mod options {
#[clap(long, conflicts_with("verbose"), requires("progress"))]
pub progress_keep_open: bool,

/// Determine the format to use when outputting statistics.
#[clap(
long,
short = "f",
default_value = "human",
possible_values(core::OutputFormat::variants())
)]
pub format: core::OutputFormat,

#[clap(subcommand)]
pub cmd: Subcommands,
}
Expand Down Expand Up @@ -121,15 +130,6 @@ mod options {
/// output statistical information about the pack
#[clap(long, short = "s")]
statistics: bool,
/// Determine the format to use when outputting statistics.
#[clap(
long,
short = "f",
default_value = "human",
possible_values(core::OutputFormat::variants())
)]
format: core::OutputFormat,

/// The algorithm used to verify the pack. They differ in costs.
#[clap(
long,
Expand Down Expand Up @@ -264,13 +264,16 @@ fn prepare_and_run<T: Send + 'static>(
}

pub fn main() -> Result<()> {
let args = Args::parse();
let thread_limit = args.threads;
let verbose = args.verbose;
let progress = args.progress;
let progress_keep_open = args.progress_keep_open;
let Args {
threads: thread_limit,
verbose,
progress,
progress_keep_open,
format,
cmd,
} = Args::parse();

match args.cmd {
match cmd {
Subcommands::IndexFromPack {
iteration_mode,
pack_path,
Expand All @@ -280,14 +283,16 @@ pub fn main() -> Result<()> {
verbose,
progress,
progress_keep_open,
move |progress, _out, _err| {
move |progress, out, _err| {
core::pack::index::from_pack(
pack_path,
directory,
git_features::progress::DoOrDiscard::from(progress),
core::pack::index::Context {
thread_limit,
iteration_mode,
format,
out,
},
)
},
Expand Down Expand Up @@ -322,7 +327,6 @@ pub fn main() -> Result<()> {
Subcommands::PackVerify {
path,
algorithm,
format,
decode,
re_encode,
statistics,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"index": {
"index_kind": "V2",
"index_hash": [
86,
14,
186,
102,
230,
179,
145,
235,
131,
239,
195,
236,
159,
200,
163,
8,
119,
136,
145,
28
],
"pack_hash": [
241,
205,
60,
199,
188,
99,
164,
162,
179,
87,
164,
117,
165,
138,
212,
155,
64,
53,
84,
112
],
"num_objects": 30
},
"pack_kind": "V2"
}
14 changes: 11 additions & 3 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,22 @@ title plumbing
(with "pack file passed as file"
it "generates an index and outputs pack and index information" && {
WITH_SNAPSHOT="$snapshot/plumbing-index-from-pack-no-output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" index-from-pack -p "${PACK_FILE}"
expect_run $SUCCESSFULLY "$exe_plumbing" index-from-pack -p "$PACK_FILE"
}
)
(with "pack file passed from stdin"
it "generates an index and outputs pack and index information" && {
WITH_SNAPSHOT="$snapshot/plumbing-index-from-pack-no-output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" index-from-pack < "${PACK_FILE}"
expect_run $SUCCESSFULLY "$exe_plumbing" index-from-pack < "$PACK_FILE"
}
if test "$kind" = "max"; then
(with "--format json"
it "generates the index and outputs information as JSON" && {
WITH_SNAPSHOT="$snapshot/plumbing-index-from-pack-no-output-dir-as-json-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" --format json index-from-pack < "$PACK_FILE"
}
)
fi
)
)
(sandbox
Expand Down Expand Up @@ -194,7 +202,7 @@ title plumbing
(with "statistics (JSON)"
it "verifies the pack index successfully and with desired output" && {
WITH_SNAPSHOT="$snapshot/plumbing-pack-verify-index-with-statistics-json-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" --threads 1 pack-verify --statistics --format json "$PACK_INDEX_FILE"
expect_run $SUCCESSFULLY "$exe_plumbing" --format json --threads 1 pack-verify --statistics "$PACK_INDEX_FILE"
}
)
fi
Expand Down

0 comments on commit 1931575

Please sign in to comment.