Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Move `index::from_tree` into `repository` module as it is
consuming a repository and is not a free-standing command like the
others.
  • Loading branch information
Byron committed Oct 11, 2022
1 parent 01ab5ca commit 67f2247
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
43 changes: 1 addition & 42 deletions gitoxide-core/src/index/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use git::odb::FindExt;
use git_repository as git;
use std::ffi::OsString;
use std::{
io::BufWriter,
path::{Path, PathBuf},
};
use std::path::Path;

pub struct Options {
pub object_hash: git::hash::Kind,
Expand Down Expand Up @@ -90,39 +85,3 @@ pub fn information(
}
}
}

pub fn from_tree(
spec: OsString,
index_path: Option<PathBuf>,
force: bool,
repo: git::Repository,
mut out: impl std::io::Write,
) -> anyhow::Result<()> {
let spec = git::path::os_str_into_bstr(&spec)?;
let tree = repo.rev_parse_single(spec)?;
let state = git::index::State::from_tree(&tree, |oid, buf| repo.objects.find_tree_iter(oid, buf).ok())?;
let options = git::index::write::Options {
hash_kind: repo.object_hash(),
..Default::default()
};

match index_path {
Some(index_path) => {
if index_path.is_file() {
if !force {
anyhow::bail!(
"File at \"{}\" already exists, to overwrite use the '-f' flag",
index_path.display()
);
}
}
let writer = BufWriter::new(std::fs::File::create(&index_path)?);
state.write_to(writer, options)?;
}
None => {
state.write_to(&mut out, options)?;
}
}

Ok(())
}
40 changes: 40 additions & 0 deletions gitoxide-core/src/repository/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use git::prelude::FindExt;
use git_repository as git;
use std::ffi::OsString;
use std::{io::BufWriter, path::PathBuf};

pub fn from_tree(
spec: OsString,
index_path: Option<PathBuf>,
force: bool,
repo: git::Repository,
mut out: impl std::io::Write,
) -> anyhow::Result<()> {
let spec = git::path::os_str_into_bstr(&spec)?;
let tree = repo.rev_parse_single(spec)?;
let state = git::index::State::from_tree(&tree, |oid, buf| repo.objects.find_tree_iter(oid, buf).ok())?;
let options = git::index::write::Options {
hash_kind: repo.object_hash(),
..Default::default()
};

match index_path {
Some(index_path) => {
if index_path.is_file() {
if !force {
anyhow::bail!(
"File at \"{}\" already exists, to overwrite use the '-f' flag",
index_path.display()
);
}
}
let writer = BufWriter::new(std::fs::File::create(&index_path)?);
state.write_to(writer, options)?;
}
None => {
state.write_to(&mut out, options)?;
}
}

Ok(())
}
1 change: 1 addition & 0 deletions gitoxide-core/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod config;
mod credential;
pub use credential::function as credential;
pub mod exclude;
pub mod index;
pub mod mailmap;
pub mod odb;
pub mod remote;
Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ pub fn main() -> Result<()> {
progress_keep_open,
None,
move |_progress, out, _err| {
core::index::from_tree(spec, index_output_path, force, repository(Mode::Strict)?, out)
core::repository::index::from_tree(spec, index_output_path, force, repository(Mode::Strict)?, out)
},
),
},
Expand Down

0 comments on commit 67f2247

Please sign in to comment.