Skip to content

Commit

Permalink
[ref-ls] Frame for remote-ref-ls command in gitoxide-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 4, 2020
1 parent 4cf3643 commit 161e7df
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 151 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Please see _'Development Status'_ for a listing of all crates and their capabili
* **pack-index**
* [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)
* **remote-ref-ls**
* [ ] list all (or given) references from a remote at the given URL

### git-object
* *decode (zero-copy)* borrowed objects
Expand Down Expand Up @@ -105,6 +107,7 @@ Please see _'Development Status'_ for a listing of all crates and their capabili
* [x] via git-credentials
* [ ] via pure Rust implementation if no git is installed
* [ ] fetch & clone
* [x] detailed progress
* [x] control credentials provider to fill, approve and reject
* [x] command: ls-ref
* [x] parse V1 refs as provided during handshake
Expand Down
4 changes: 4 additions & 0 deletions gitoxide-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ impl FromStr for OutputFormat {
}
}

mod protocol;
pub use protocol::Protocol;

pub mod pack;
pub mod remote;
pub mod repository;
22 changes: 2 additions & 20 deletions gitoxide-core/src/pack/receive.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
use crate::OutputFormat;
use crate::{OutputFormat, Protocol};
use git_features::progress::Progress;
use std::{io, path::PathBuf, str::FromStr};

#[derive(PartialEq, Debug)]
pub enum Protocol {
V1,
V2,
}

impl FromStr for Protocol {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"1" => Protocol::V1,
"2" => Protocol::V2,
_ => return Err(format!("Unsupported protocol version '{}', choose '1' or '2'", s)),
})
}
}
use std::{io, path::PathBuf};

pub struct Context<W: io::Write> {
pub thread_limit: Option<usize>,
Expand Down
19 changes: 19 additions & 0 deletions gitoxide-core/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::str::FromStr;

#[derive(PartialEq, Debug)]
pub enum Protocol {
V1,
V2,
}

impl FromStr for Protocol {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"1" => Protocol::V1,
"2" => Protocol::V2,
_ => return Err(format!("Unsupported protocol version '{}', choose '1' or '2'", s)),
})
}
}
25 changes: 25 additions & 0 deletions gitoxide-core/src/remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub mod refs {
use crate::{OutputFormat, Protocol};
use git_features::progress::Progress;
use std::io;

pub struct Context<W: io::Write> {
pub thread_limit: Option<usize>,
pub format: OutputFormat,
pub out: W,
}

pub fn list<P, W: io::Write>(
_protocol: Option<Protocol>,
_url: &str,
_progress: P,
_ctx: Context<W>,
) -> anyhow::Result<()>
where
P: Progress,
<P as Progress>::SubProgress: Send + 'static,
<<P as Progress>::SubProgress as Progress>::SubProgress: Send,
{
unimplemented!("remote ref list")
}
}
142 changes: 142 additions & 0 deletions src/plumbing/lean/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use crate::shared::ProgressRange;
use anyhow::Result;
use git_features::progress;
use gitoxide_core::{self as core, OutputFormat};
use std::io::{self, stderr, stdout};

#[cfg(not(any(feature = "prodash-render-line-crossterm", feature = "prodash-render-line-termion")))]
fn prepare(verbose: bool, name: &str, _: impl Into<Option<ProgressRange>>) -> ((), Option<prodash::progress::Log>) {
super::init_env_logger(verbose);
((), Some(prodash::progress::Log::new(name, Some(1))))
}

#[cfg(any(feature = "prodash-render-line-crossterm", feature = "prodash-render-line-termion"))]
fn prepare(
verbose: bool,
name: &str,
range: impl Into<Option<ProgressRange>>,
) -> (Option<prodash::render::line::JoinHandle>, Option<prodash::tree::Item>) {
use crate::shared::{self, STANDARD_RANGE};
crate::plumbing::init_env_logger(false);

if verbose {
let progress = prodash::Tree::new();
let sub_progress = progress.add_child(name);
let handle = shared::setup_line_renderer_range(progress, range.into().unwrap_or(STANDARD_RANGE), true);
(Some(handle), Some(sub_progress))
} else {
(None, None)
}
}

pub fn main() -> Result<()> {
pub use crate::plumbing::lean::options::*;
let cli: Args = crate::shared::from_env();
git_features::interrupt::init_handler(std::io::stderr());
let thread_limit = cli.threads;
let verbose = cli.verbose;
match cli.subcommand {
SubCommands::RemoteRefList(RemoteRefList { protocol, url }) => {
let (_handle, progress) = prepare(verbose, "remote-ref-list", None);
core::remote::refs::list(
protocol,
&url,
progress::DoOrDiscard::from(progress),
core::remote::refs::Context {
thread_limit,
format: OutputFormat::Human,
out: io::stdout(),
},
)
}
SubCommands::PackReceive(PackReceive {
protocol,
url,
directory,
}) => {
let (_handle, progress) = prepare(verbose, "pack-receive", None);
core::pack::receive(
protocol,
&url,
directory,
progress::DoOrDiscard::from(progress),
core::pack::receive::Context {
thread_limit,
format: OutputFormat::Human,
out: io::stdout(),
},
)
}
SubCommands::IndexFromPack(IndexFromPack {
iteration_mode,
pack_path,
directory,
}) => {
let (_handle, progress) = prepare(verbose, "pack-explode", core::pack::index::PROGRESS_RANGE);
core::pack::index::from_pack(
pack_path,
directory,
progress::DoOrDiscard::from(progress),
core::pack::index::Context {
thread_limit,
iteration_mode: iteration_mode.unwrap_or_default(),
format: OutputFormat::Human,
out: io::stdout(),
},
)
}
SubCommands::PackExplode(PackExplode {
pack_path,
sink_compress,
object_path,
verify,
check,
delete_pack,
}) => {
let (_handle, progress) = prepare(verbose, "pack-explode", None);
core::pack::explode::pack_or_pack_index(
pack_path,
object_path,
check.unwrap_or_default(),
progress,
core::pack::explode::Context {
thread_limit,
delete_pack,
sink_compress,
verify,
},
)
}
SubCommands::PackVerify(PackVerify {
path,
statistics,
algorithm,
decode,
re_encode,
}) => {
use self::core::pack::verify;
let (_handle, progress) = prepare(verbose, "pack-verify", None);
core::pack::verify::pack_or_pack_index(
path,
progress,
core::pack::verify::Context {
output_statistics: if statistics {
Some(core::OutputFormat::Human)
} else {
None
},
algorithm: algorithm.unwrap_or(verify::Algorithm::LessTime),
thread_limit,
mode: match (decode, re_encode) {
(true, false) => verify::Mode::Sha1CRC32Decode,
(true, true) | (false, true) => verify::Mode::Sha1CRC32DecodeEncode,
(false, false) => verify::Mode::Sha1CRC32,
},
out: stdout(),
err: stderr(),
},
)
.map(|_| ())
}
}
}
132 changes: 2 additions & 130 deletions src/plumbing/lean/mod.rs
Original file line number Diff line number Diff line change
@@ -1,131 +1,3 @@
mod main;
pub use main::main;
mod options;

use crate::shared::ProgressRange;
use anyhow::Result;
use git_features::progress;
use gitoxide_core::{self as core, OutputFormat};
use std::io::{self, stderr, stdout};

#[cfg(not(any(feature = "prodash-render-line-crossterm", feature = "prodash-render-line-termion")))]
fn prepare(verbose: bool, name: &str, _: impl Into<Option<ProgressRange>>) -> ((), Option<prodash::progress::Log>) {
super::init_env_logger(verbose);
((), Some(prodash::progress::Log::new(name, Some(1))))
}

#[cfg(any(feature = "prodash-render-line-crossterm", feature = "prodash-render-line-termion"))]
fn prepare(
verbose: bool,
name: &str,
range: impl Into<Option<ProgressRange>>,
) -> (Option<prodash::render::line::JoinHandle>, Option<prodash::tree::Item>) {
use crate::shared::{self, STANDARD_RANGE};
super::init_env_logger(false);

if verbose {
let progress = prodash::Tree::new();
let sub_progress = progress.add_child(name);
let handle = shared::setup_line_renderer_range(progress, range.into().unwrap_or(STANDARD_RANGE), true);
(Some(handle), Some(sub_progress))
} else {
(None, None)
}
}

pub fn main() -> Result<()> {
pub use options::*;
let cli: Args = crate::shared::from_env();
git_features::interrupt::init_handler(std::io::stderr());
let thread_limit = cli.threads;
let verbose = cli.verbose;
match cli.subcommand {
SubCommands::PackReceive(PackReceive {
protocol,
url,
directory,
}) => {
let (_handle, progress) = prepare(verbose, "pack-receive", None);
core::pack::receive(
protocol,
&url,
directory,
progress::DoOrDiscard::from(progress),
core::pack::receive::Context {
thread_limit,
format: OutputFormat::Human,
out: io::stdout(),
},
)
}
SubCommands::IndexFromPack(IndexFromPack {
iteration_mode,
pack_path,
directory,
}) => {
let (_handle, progress) = prepare(verbose, "pack-explode", core::pack::index::PROGRESS_RANGE);
core::pack::index::from_pack(
pack_path,
directory,
progress::DoOrDiscard::from(progress),
core::pack::index::Context {
thread_limit,
iteration_mode: iteration_mode.unwrap_or_default(),
format: OutputFormat::Human,
out: io::stdout(),
},
)
}
SubCommands::PackExplode(PackExplode {
pack_path,
sink_compress,
object_path,
verify,
check,
delete_pack,
}) => {
let (_handle, progress) = prepare(verbose, "pack-explode", None);
core::pack::explode::pack_or_pack_index(
pack_path,
object_path,
check.unwrap_or_default(),
progress,
core::pack::explode::Context {
thread_limit,
delete_pack,
sink_compress,
verify,
},
)
}
SubCommands::PackVerify(PackVerify {
path,
statistics,
algorithm,
decode,
re_encode,
}) => {
use self::core::pack::verify;
let (_handle, progress) = prepare(verbose, "pack-verify", None);
core::pack::verify::pack_or_pack_index(
path,
progress,
core::pack::verify::Context {
output_statistics: if statistics {
Some(core::OutputFormat::Human)
} else {
None
},
algorithm: algorithm.unwrap_or(verify::Algorithm::LessTime),
thread_limit,
mode: match (decode, re_encode) {
(true, false) => verify::Mode::Sha1CRC32Decode,
(true, true) | (false, true) => verify::Mode::Sha1CRC32DecodeEncode,
(false, false) => verify::Mode::Sha1CRC32,
},
out: stdout(),
err: stderr(),
},
)
.map(|_| ())
}
}
}
Loading

0 comments on commit 161e7df

Please sign in to comment.