Skip to content

Commit

Permalink
refactor (#301)
Browse files Browse the repository at this point in the history
Byron committed Apr 28, 2022

Unverified

This user has not yet uploaded their public signing key.
1 parent a331314 commit 3ff991d
Showing 8 changed files with 149 additions and 158 deletions.
5 changes: 1 addition & 4 deletions gitoxide-core/src/repository/commit.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::path::PathBuf;

use anyhow::{Context, Result};
use git_repository as git;

pub fn describe(
repo: impl Into<PathBuf>,
repo: git::Repository,
rev_spec: Option<&str>,
mut out: impl std::io::Write,
mut err: impl std::io::Write,
@@ -18,7 +16,6 @@ pub fn describe(
long_format,
}: describe::Options,
) -> Result<()> {
let repo = git::open(repo)?.apply_environment();
let commit = match rev_spec {
Some(spec) => repo.rev_parse(spec)?.object()?.try_into_commit()?,
None => repo.head_commit()?,
4 changes: 1 addition & 3 deletions gitoxide-core/src/repository/exclude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::bail;
use std::io;
use std::path::PathBuf;

use crate::OutputFormat;
use git_repository as git;
@@ -16,14 +15,13 @@ pub mod query {
}

pub fn query(
repository: PathBuf,
repository: git::Repository,
mut out: impl io::Write,
query::Options { format, pathspecs }: query::Options,
) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("JSON output isn't implemented yet");
}

let repo = git::open(repository)?.apply_environment();
todo!("impl");
}
5 changes: 2 additions & 3 deletions gitoxide-core/src/repository/mailmap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io, path::PathBuf};
use std::io;

use git_repository as git;
#[cfg(feature = "serde1")]
@@ -29,7 +29,7 @@ impl<'a> From<Entry<'a>> for JsonEntry {
}

pub fn entries(
repository: PathBuf,
repo: git::Repository,
format: OutputFormat,
#[cfg_attr(not(feature = "serde1"), allow(unused_variables))] out: impl io::Write,
mut err: impl io::Write,
@@ -38,7 +38,6 @@ pub fn entries(
writeln!(err, "Defaulting to JSON as human format isn't implemented").ok();
}

let repo = git::open(repository)?.apply_environment();
let mut mailmap = git::mailmap::Snapshot::default();
if let Err(e) = repo.load_mailmap_into(&mut mailmap) {
writeln!(err, "Error while loading mailmap, the first error is: {}", e).ok();
9 changes: 3 additions & 6 deletions gitoxide-core/src/repository/odb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io, path::PathBuf};
use std::io;

use anyhow::bail;
use git_repository as git;
@@ -22,7 +22,7 @@ mod info {

#[cfg_attr(not(feature = "serde1"), allow(unused_variables))]
pub fn info(
repository: PathBuf,
repo: git::Repository,
format: OutputFormat,
out: impl io::Write,
mut err: impl io::Write,
@@ -31,7 +31,6 @@ pub fn info(
writeln!(err, "Only JSON is implemented - using that instead")?;
}

let repo = git::open(repository)?.apply_environment();
let store = repo.objects.store_ref();
let stats = info::Statistics {
path: store.path().into(),
@@ -49,13 +48,11 @@ pub fn info(
Ok(())
}

pub fn entries(repository: PathBuf, format: OutputFormat, mut out: impl io::Write) -> anyhow::Result<()> {
pub fn entries(repo: git::Repository, format: OutputFormat, mut out: impl io::Write) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("Only human output format is supported at the moment");
}

let repo = git::open(repository)?.apply_environment();

for object in repo.objects.iter()? {
let object = object?;
writeln!(out, "{}", object)?;
8 changes: 3 additions & 5 deletions gitoxide-core/src/repository/tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, io, path::PathBuf};
use std::{borrow::Cow, io};

use anyhow::bail;
use git_repository as git;
@@ -117,7 +117,7 @@ mod entries {

#[cfg_attr(not(feature = "serde1"), allow(unused_variables))]
pub fn info(
repository: PathBuf,
repo: git::Repository,
treeish: Option<&str>,
extended: bool,
format: OutputFormat,
@@ -128,7 +128,6 @@ pub fn info(
writeln!(err, "Only JSON is implemented - using that instead")?;
}

let repo = git::open(repository)?.apply_environment();
let tree = treeish_to_tree(treeish, &repo)?;

let mut delegate = entries::Traverse::new(extended.then(|| &repo), None);
@@ -144,7 +143,7 @@ pub fn info(
}

pub fn entries(
repository: PathBuf,
repo: git::Repository,
treeish: Option<&str>,
recursive: bool,
extended: bool,
@@ -155,7 +154,6 @@ pub fn entries(
bail!("Only human output format is supported at the moment");
}

let repo = git::open(repository)?.apply_environment();
let tree = treeish_to_tree(treeish, &repo)?;

if recursive {
5 changes: 2 additions & 3 deletions gitoxide-core/src/repository/verify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::atomic::AtomicBool};
use std::sync::atomic::AtomicBool;

use git_repository as git;
use git_repository::Progress;
@@ -20,7 +20,7 @@ pub struct Context {
pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3;

pub fn integrity(
repo: PathBuf,
repo: git::Repository,
mut out: impl std::io::Write,
progress: impl Progress,
should_interrupt: &AtomicBool,
@@ -31,7 +31,6 @@ pub fn integrity(
algorithm,
}: Context,
) -> anyhow::Result<()> {
let repo = git_repository::open(repo)?;
#[cfg_attr(not(feature = "serde1"), allow(unused))]
let mut outcome = repo.objects.store_ref().verify_integrity(
progress,
270 changes: 137 additions & 133 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
@@ -155,149 +155,153 @@ pub fn main() -> Result<()> {
},
),
},
Subcommands::Repository(repo::Platform { repository, cmd }) => match cmd {
repo::Subcommands::Commit { cmd } => match cmd {
repo::commit::Subcommands::Describe {
annotated_tags,
all_refs,
first_parent,
always,
long,
statistics,
max_candidates,
rev_spec,
Subcommands::Repository(repo::Platform { repository, cmd }) => {
use git_repository as git;
let repository = git::open(repository)?.apply_environment();
match cmd {
repo::Subcommands::Commit { cmd } => match cmd {
repo::commit::Subcommands::Describe {
annotated_tags,
all_refs,
first_parent,
always,
long,
statistics,
max_candidates,
rev_spec,
} => prepare_and_run(
"repository-commit-describe",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::repository::commit::describe(
repository,
rev_spec.as_deref(),
out,
err,
core::repository::commit::describe::Options {
all_tags: !annotated_tags,
all_refs,
long_format: long,
first_parent,
statistics,
max_candidates,
always,
},
)
},
),
},
repo::Subcommands::Exclude { cmd } => match cmd {
repo::exclude::Subcommands::Query { pathspecs } => prepare_and_run(
"repository-exclude-query",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::exclude::query(
repository,
out,
core::repository::exclude::query::Options { format, pathspecs },
)
},
),
},
repo::Subcommands::Mailmap { cmd } => match cmd {
repo::mailmap::Subcommands::Entries => prepare_and_run(
"repository-mailmap-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| core::repository::mailmap::entries(repository, format, out, err),
),
},
repo::Subcommands::Odb { cmd } => match cmd {
repo::odb::Subcommands::Entries => prepare_and_run(
"repository-odb-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::repository::odb::entries(repository, format, out),
),
repo::odb::Subcommands::Info => prepare_and_run(
"repository-odb-info",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| core::repository::odb::info(repository, format, out, err),
),
},
repo::Subcommands::Tree { cmd } => match cmd {
repo::tree::Subcommands::Entries {
treeish,
recursive,
extended,
} => prepare_and_run(
"repository-tree-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::tree::entries(
repository,
treeish.as_deref(),
recursive,
extended,
format,
out,
)
},
),
repo::tree::Subcommands::Info { treeish, extended } => prepare_and_run(
"repository-tree-info",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::repository::tree::info(repository, treeish.as_deref(), extended, format, out, err)
},
),
},
repo::Subcommands::Verify {
args:
pack::VerifyOptions {
statistics,
algorithm,
decode,
re_encode,
},
} => prepare_and_run(
"repository-commit-describe",
"repository-verify",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::repository::commit::describe(
core::repository::verify::PROGRESS_RANGE,
move |progress, out, _err| {
core::repository::verify::integrity(
repository,
rev_spec.as_deref(),
out,
err,
core::repository::commit::describe::Options {
all_tags: !annotated_tags,
all_refs,
long_format: long,
first_parent,
statistics,
max_candidates,
always,
progress,
&should_interrupt,
core::repository::verify::Context {
output_statistics: statistics.then(|| format),
algorithm,
verify_mode: verify_mode(decode, re_encode),
thread_limit,
},
)
},
),
},
repo::Subcommands::Exclude { cmd } => match cmd {
repo::exclude::Subcommands::Query { pathspecs } => prepare_and_run(
"repository-exclude-query",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::exclude::query(
repository,
out,
core::repository::exclude::query::Options { format, pathspecs },
)
},
),
},
repo::Subcommands::Mailmap { cmd } => match cmd {
repo::mailmap::Subcommands::Entries => prepare_and_run(
"repository-mailmap-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| core::repository::mailmap::entries(repository, format, out, err),
),
},
repo::Subcommands::Odb { cmd } => match cmd {
repo::odb::Subcommands::Entries => prepare_and_run(
"repository-odb-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::repository::odb::entries(repository, format, out),
),
repo::odb::Subcommands::Info => prepare_and_run(
"repository-odb-info",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| core::repository::odb::info(repository, format, out, err),
),
},
repo::Subcommands::Tree { cmd } => match cmd {
repo::tree::Subcommands::Entries {
treeish,
recursive,
extended,
} => prepare_and_run(
"repository-tree-entries",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::tree::entries(
repository,
treeish.as_deref(),
recursive,
extended,
format,
out,
)
},
),
repo::tree::Subcommands::Info { treeish, extended } => prepare_and_run(
"repository-tree-info",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::repository::tree::info(repository, treeish.as_deref(), extended, format, out, err)
},
),
},
repo::Subcommands::Verify {
args:
pack::VerifyOptions {
statistics,
algorithm,
decode,
re_encode,
},
} => prepare_and_run(
"repository-verify",
verbose,
progress,
progress_keep_open,
core::repository::verify::PROGRESS_RANGE,
move |progress, out, _err| {
core::repository::verify::integrity(
repository,
out,
progress,
&should_interrupt,
core::repository::verify::Context {
output_statistics: statistics.then(|| format),
algorithm,
verify_mode: verify_mode(decode, re_encode),
thread_limit,
},
)
},
),
},
}
}
Subcommands::Pack(subcommands) => match subcommands {
pack::Subcommands::Create {
repository,
1 change: 0 additions & 1 deletion src/shared.rs
Original file line number Diff line number Diff line change
@@ -64,7 +64,6 @@ pub mod pretty {
&mut dyn std::io::Write,
) -> Result<T>
+ Send
+ std::panic::UnwindSafe
+ 'static,
) -> Result<T> {
crate::shared::init_env_logger();

0 comments on commit 3ff991d

Please sign in to comment.