-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
43 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters