Skip to content

Commit

Permalink
'index' with its own sub-commands (#279)
Browse files Browse the repository at this point in the history
Cleanup to prepare for multi-index
  • Loading branch information
Byron committed Jan 1, 2022
1 parent fb64af4 commit c4c5678
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 120 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ 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_
* [x] [index from data](https://asciinema.org/a/352941) - create an index file by streaming a pack file as done during clone
* [ ] support for thin packs (as needed for fetch/pull)
* **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)
* **commitgraph**
* [x] **verify** - assure that a commitgraph is consistent
* **remote**
Expand Down
74 changes: 38 additions & 36 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,42 +177,6 @@ pub fn main() -> Result<()> {
)
},
),
pack::Subcommands::IndexFromData {
iteration_mode,
pack_path,
directory,
} => prepare_and_run(
"pack-index-from-data",
verbose,
progress,
progress_keep_open,
core::pack::index::PROGRESS_RANGE,
move |progress, out, _err| {
use gitoxide_core::pack::index::PathOrRead;
let input = if let Some(path) = pack_path {
PathOrRead::Path(path)
} else {
if atty::is(atty::Stream::Stdin) {
anyhow::bail!(
"Refusing to read from standard input as no path is given, but it's a terminal."
)
}
PathOrRead::Read(Box::new(std::io::stdin()))
};
core::pack::index::from_pack(
input,
directory,
progress,
core::pack::index::Context {
thread_limit,
iteration_mode,
format,
out,
should_interrupt: &git_repository::interrupt::IS_INTERRUPTED,
},
)
},
),
pack::Subcommands::Explode {
check,
sink_compress,
Expand Down Expand Up @@ -277,6 +241,44 @@ pub fn main() -> Result<()> {
},
)
.map(|_| ()),
pack::Subcommands::Index(subcommands) => match subcommands {
pack::index::Subcommands::Create {
iteration_mode,
pack_path,
directory,
} => prepare_and_run(
"pack-index-from-data",
verbose,
progress,
progress_keep_open,
core::pack::index::PROGRESS_RANGE,
move |progress, out, _err| {
use gitoxide_core::pack::index::PathOrRead;
let input = if let Some(path) = pack_path {
PathOrRead::Path(path)
} else {
if atty::is(atty::Stream::Stdin) {
anyhow::bail!(
"Refusing to read from standard input as no path is given, but it's a terminal."
)
}
PathOrRead::Read(Box::new(std::io::stdin()))
};
core::pack::index::from_pack(
input,
directory,
progress,
core::pack::index::Context {
thread_limit,
iteration_mode,
format,
out,
should_interrupt: &git_repository::interrupt::IS_INTERRUPTED,
},
)
},
),
},
},
#[cfg(any(feature = "gitoxide-core-async-client", feature = "gitoxide-core-blocking-client"))]
Subcommands::Remote(subcommands) => match subcommands {
Expand Down
76 changes: 45 additions & 31 deletions src/plumbing/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,37 +153,9 @@ pub mod pack {
/// If unset, they will be discarded.
directory: Option<PathBuf>,
},
/// create a pack index from a pack data file.
#[clap(setting = AppSettings::DisableVersionFlag)]
IndexFromData {
/// Specify how to iterate the pack, defaults to 'verify'
///
/// Valid values are
///
/// **as-is** do not do anything and expect the pack file to be valid as per the trailing hash,
/// **verify** the input ourselves and validate that it matches with the hash provided in the pack,
/// **restore** hash the input ourselves and ignore failing entries, instead finish the pack with the hash we computed
/// to keep as many objects as possible.
#[clap(
long,
short = 'i',
default_value = "verify",
possible_values(core::pack::index::IterationMode::variants())
)]
iteration_mode: core::pack::index::IterationMode,

/// Path to the pack file to read (with .pack extension).
///
/// If unset, the pack file is expected on stdin.
#[clap(long, short = 'p')]
pack_path: Option<PathBuf>,

/// The folder into which to place the pack and the generated index file
///
/// If unset, only informational output will be provided to standard output.
#[clap(parse(from_os_str))]
directory: Option<PathBuf>,
},
/// Subcommands for interacting with pack indices (.idx)
#[clap(subcommand)]
Index(index::Subcommands),
/// Dissolve a pack into its loose objects.
///
/// Note that this effectively removes delta compression for an average compression of 2x, creating one file per object in the process.
Expand Down Expand Up @@ -261,6 +233,48 @@ pub mod pack {
path: PathBuf,
},
}

///
pub mod index {
use clap::AppSettings;
use gitoxide_core as core;
use std::path::PathBuf;

#[derive(Debug, clap::Parser)]
pub enum Subcommands {
/// create a pack index from a pack data file.
#[clap(setting = AppSettings::DisableVersionFlag)]
Create {
/// Specify how to iterate the pack, defaults to 'verify'
///
/// Valid values are
///
/// **as-is** do not do anything and expect the pack file to be valid as per the trailing hash,
/// **verify** the input ourselves and validate that it matches with the hash provided in the pack,
/// **restore** hash the input ourselves and ignore failing entries, instead finish the pack with the hash we computed
/// to keep as many objects as possible.
#[clap(
long,
short = 'i',
default_value = "verify",
possible_values(core::pack::index::IterationMode::variants())
)]
iteration_mode: core::pack::index::IterationMode,

/// Path to the pack file to read (with .pack extension).
///
/// If unset, the pack file is expected on stdin.
#[clap(long, short = 'p')]
pack_path: Option<PathBuf>,

/// The folder into which to place the pack and the generated index file
///
/// If unset, only informational output will be provided to standard output.
#[clap(parse(from_os_str))]
directory: Option<PathBuf>,
},
}
}
}

///
Expand Down
110 changes: 59 additions & 51 deletions tests/journey/gix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,68 +196,76 @@ title "gix pack"
fi
)
)
title "gix pack index-from-data"
(with "the 'index-from-data' sub-command"
snapshot="$snapshot/index-from-data"
PACK_FILE="$fixtures/packs/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack"
(with "a valid and complete pack file"
(with "NO output directory specified"
(with "pack file passed as file"
it "generates an index into a sink and outputs pack and index information" && {
WITH_SNAPSHOT="$snapshot/no-output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index-from-data -p "$PACK_FILE"
}
(with "the 'index' sub-command"
snapshot="$snapshot/index"
title "gix pack index create"
(with "the 'create' sub-command"
snapshot="$snapshot/create"
PACK_FILE="$fixtures/packs/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack"
(with "a valid and complete pack file"
(with "NO output directory specified"
(with "pack file passed as file"
it "generates an index into a sink and outputs pack and index information" && {
WITH_SNAPSHOT="$snapshot/no-output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index create -p "$PACK_FILE"
}
)
(with "pack file passed from stdin"
it "generates an index into a sink and outputs pack and index information" && {
WITH_SNAPSHOT="$snapshot/no-output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index create < "$PACK_FILE"
}
if test "$kind" = "max"; then
(with "--format json"
it "generates the index into a sink and outputs information as JSON" && {
WITH_SNAPSHOT="$snapshot/no-output-dir-as-json-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" --format json pack index create < "$PACK_FILE"
}
)
fi
)
)
(with "pack file passed from stdin"
it "generates an index into a sink and outputs pack and index information" && {
WITH_SNAPSHOT="$snapshot/no-output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index-from-data < "$PACK_FILE"
}
if test "$kind" = "max"; then
(with "--format json"
it "generates the index into a sink and outputs information as JSON" && {
WITH_SNAPSHOT="$snapshot/no-output-dir-as-json-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" --format json pack index-from-data < "$PACK_FILE"
(sandbox
(with "with an output directory specified"
it "generates an index and outputs information" && {
WITH_SNAPSHOT="$snapshot/output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index create -p "$PACK_FILE" "$PWD"
}
it "writes the index and pack into the directory (they have the same names, different suffixes)" && {
WITH_SNAPSHOT="$snapshot/output-dir-content" \
expect_run $SUCCESSFULLY ls
}
)
fi
)
)
(sandbox
(with "with an output directory specified"
it "generates an index and outputs information" && {
WITH_SNAPSHOT="$snapshot/output-dir-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index-from-data -p "$PACK_FILE" "$PWD"
}
it "writes the index and pack into the directory (they have the same names, different suffixes)" && {
WITH_SNAPSHOT="$snapshot/output-dir-content" \
expect_run $SUCCESSFULLY ls
(with "'restore' iteration mode"
(sandbox
cp "${PACK_FILE}" .
PACK_FILE="${PACK_FILE##*/}"
"$jtt" mess-in-the-middle "${PACK_FILE}"

it "generates an index and outputs information (instead of failing)" && {
WITH_SNAPSHOT="$snapshot/output-dir-restore-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index create -i restore -p "$PACK_FILE" "$PWD"
}

if test "$kind" = "max"; then
(with "--format json and the very same output directory"
it "generates the index, overwriting existing files, and outputs information as JSON" && {
WITH_SNAPSHOT="$snapshot/output-dir-restore-as-json-success" \
SNAPSHOT_FILTER=remove-paths \
expect_run $SUCCESSFULLY "$exe_plumbing" --format json pack index create -i restore $PWD < "$PACK_FILE"
}
)
fi
)
)
)
(with "'restore' iteration mode"
(sandbox
cp "${PACK_FILE}" .
PACK_FILE="${PACK_FILE##*/}"
"$jtt" mess-in-the-middle "${PACK_FILE}"
)

it "generates an index and outputs information (instead of failing)" && {
WITH_SNAPSHOT="$snapshot/output-dir-restore-success" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack index-from-data -i restore -p "$PACK_FILE" "$PWD"
}
title "gix pack multi-index"
(with "the 'multi-index' sub-command"

if test "$kind" = "max"; then
(with "--format json and the very same output directory"
it "generates the index, overwriting existing files, and outputs information as JSON" && {
WITH_SNAPSHOT="$snapshot/output-dir-restore-as-json-success" \
SNAPSHOT_FILTER=remove-paths \
expect_run $SUCCESSFULLY "$exe_plumbing" --format json pack index-from-data -i restore $PWD < "$PACK_FILE"
}
)
fi
)
)
)

title "gix pack explode"
Expand Down

0 comments on commit c4c5678

Please sign in to comment.