Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67f2247

Browse files
committed
refactor
Move `index::from_tree` into `repository` module as it is consuming a repository and is not a free-standing command like the others.
1 parent 01ab5ca commit 67f2247

4 files changed

Lines changed: 43 additions & 43 deletions

File tree

gitoxide-core/src/index/mod.rs

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
use git::odb::FindExt;
21
use git_repository as git;
3-
use std::ffi::OsString;
4-
use std::{
5-
io::BufWriter,
6-
path::{Path, PathBuf},
7-
};
2+
use std::path::Path;
83

94
pub struct Options {
105
pub object_hash: git::hash::Kind,
@@ -90,39 +85,3 @@ pub fn information(
9085
}
9186
}
9287
}
93-
94-
pub fn from_tree(
95-
spec: OsString,
96-
index_path: Option<PathBuf>,
97-
force: bool,
98-
repo: git::Repository,
99-
mut out: impl std::io::Write,
100-
) -> anyhow::Result<()> {
101-
let spec = git::path::os_str_into_bstr(&spec)?;
102-
let tree = repo.rev_parse_single(spec)?;
103-
let state = git::index::State::from_tree(&tree, |oid, buf| repo.objects.find_tree_iter(oid, buf).ok())?;
104-
let options = git::index::write::Options {
105-
hash_kind: repo.object_hash(),
106-
..Default::default()
107-
};
108-
109-
match index_path {
110-
Some(index_path) => {
111-
if index_path.is_file() {
112-
if !force {
113-
anyhow::bail!(
114-
"File at \"{}\" already exists, to overwrite use the '-f' flag",
115-
index_path.display()
116-
);
117-
}
118-
}
119-
let writer = BufWriter::new(std::fs::File::create(&index_path)?);
120-
state.write_to(writer, options)?;
121-
}
122-
None => {
123-
state.write_to(&mut out, options)?;
124-
}
125-
}
126-
127-
Ok(())
128-
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use git::prelude::FindExt;
2+
use git_repository as git;
3+
use std::ffi::OsString;
4+
use std::{io::BufWriter, path::PathBuf};
5+
6+
pub fn from_tree(
7+
spec: OsString,
8+
index_path: Option<PathBuf>,
9+
force: bool,
10+
repo: git::Repository,
11+
mut out: impl std::io::Write,
12+
) -> anyhow::Result<()> {
13+
let spec = git::path::os_str_into_bstr(&spec)?;
14+
let tree = repo.rev_parse_single(spec)?;
15+
let state = git::index::State::from_tree(&tree, |oid, buf| repo.objects.find_tree_iter(oid, buf).ok())?;
16+
let options = git::index::write::Options {
17+
hash_kind: repo.object_hash(),
18+
..Default::default()
19+
};
20+
21+
match index_path {
22+
Some(index_path) => {
23+
if index_path.is_file() {
24+
if !force {
25+
anyhow::bail!(
26+
"File at \"{}\" already exists, to overwrite use the '-f' flag",
27+
index_path.display()
28+
);
29+
}
30+
}
31+
let writer = BufWriter::new(std::fs::File::create(&index_path)?);
32+
state.write_to(writer, options)?;
33+
}
34+
None => {
35+
state.write_to(&mut out, options)?;
36+
}
37+
}
38+
39+
Ok(())
40+
}

gitoxide-core/src/repository/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod config;
1919
mod credential;
2020
pub use credential::function as credential;
2121
pub mod exclude;
22+
pub mod index;
2223
pub mod mailmap;
2324
pub mod odb;
2425
pub mod remote;

src/plumbing/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ pub fn main() -> Result<()> {
767767
progress_keep_open,
768768
None,
769769
move |_progress, out, _err| {
770-
core::index::from_tree(spec, index_output_path, force, repository(Mode::Strict)?, out)
770+
core::repository::index::from_tree(spec, index_output_path, force, repository(Mode::Strict)?, out)
771771
},
772772
),
773773
},

0 commit comments

Comments
 (0)