Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
- use revspec as OsString - they can contain paths after all which
  aren't necessarly unicode safe.
  Maybe one day we can run tests to validate this is the right choice,
  as I have a feeling gitoxide will panic in other places if it would
  actually encounter illformed UTF-8 (so why bother trying to accept
  it).
- change variable names
- reduce subcommand verbosity to be in line with the other subcommands
  • Loading branch information
Byron committed Oct 11, 2022
1 parent c5c0ac5 commit 01ab5ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
36 changes: 20 additions & 16 deletions gitoxide-core/src/index/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use git::odb::FindExt;
use git_repository as git;
use std::ffi::OsString;
use std::{
io::BufWriter,
path::{Path, PathBuf},
Expand Down Expand Up @@ -91,32 +92,35 @@ pub fn information(
}

pub fn from_tree(
id: String,
index: Option<PathBuf>,
spec: OsString,
index_path: Option<PathBuf>,
force: bool,
repo: git::Repository,
mut out: impl std::io::Write,
mut err: impl std::io::Write,
) -> anyhow::Result<()> {
let tree = repo.rev_parse_single(id.as_str())?;
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 {
Some(index) => {
if index.is_file() {
writeln!(err, "File {:?} already exists", index).ok();
if force {
writeln!(err, "overwriting").ok();
} else {
anyhow::bail!("exiting, to overwrite use the '-f' flag");
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)?);
state.write_to(writer, git::index::write::Options::default())?;
writeln!(err, "Successfully wrote file {:?}", index).ok();
let writer = BufWriter::new(std::fs::File::create(&index_path)?);
state.write_to(writer, options)?;
}
None => {
state.write_to(&mut out, git::index::write::Options::default())?;
state.write_to(&mut out, options)?;
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,14 +756,18 @@ pub fn main() -> Result<()> {
),
},
Subcommands::Index(cmd) => match cmd {
index::Subcommands::FromTree { force, id, index } => prepare_and_run(
"index-read-tree",
index::Subcommands::FromTree {
force,
index_output_path,
spec,
} => prepare_and_run(
"index-from-tree",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::index::from_tree(id, index, force, repository(Mode::Strict)?, out, err)
move |_progress, out, _err| {
core::index::from_tree(spec, index_output_path, force, repository(Mode::Strict)?, out)
},
),
},
Expand Down
27 changes: 14 additions & 13 deletions src/plumbing/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,36 @@ pub struct Args {

#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// Interact with the object database.
/// Interact with the object database
#[clap(subcommand)]
Odb(odb::Subcommands),
/// Interact with tree objects.
/// Interact with tree objects
#[clap(subcommand)]
Tree(tree::Subcommands),
/// Interact with commit objects.
/// Interact with commit objects
#[clap(subcommand)]
Commit(commit::Subcommands),
/// Verify the integrity of the entire repository
Verify {
#[clap(flatten)]
args: free::pack::VerifyOptions,
},
/// Query and obtain information about revisions.
/// Query and obtain information about revisions
#[clap(subcommand)]
Revision(revision::Subcommands),
/// A program just like `git credential`.
/// A program just like `git credential`
#[clap(subcommand)]
Credential(credential::Subcommands),
/// Interact with the mailmap.
/// Interact with the mailmap
#[clap(subcommand)]
Mailmap(mailmap::Subcommands),
/// Interact with the remote hosts.
Remote(remote::Platform),
/// Interact with the exclude files like .gitignore.
/// Interact with the exclude files like .gitignore
#[clap(subcommand)]
Exclude(exclude::Subcommands),
Config(config::Platform),
/// Subcommands that need no git repository to run.
/// Subcommands that need no git repository to run
#[clap(subcommand)]
Free(free::Subcommands),
/// Interact with index files
Expand Down Expand Up @@ -673,17 +673,18 @@ pub mod index {

#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// Create an index from a tree-ish.
#[clap(visible_alias = "read-tree")]
FromTree {
/// Overwrite the specified file if it already exists
/// Overwrite the specified index file if it already exists.
#[clap(long, short = 'f')]
force: bool,
/// RevSpec that points to a tree to generate the index from
id: String,
/// Path to the index file to be written.
/// If none is given output will be written to stdout.
/// If none is given it will be written to stdout, avoiding to overwrite the repository index just yet.
#[clap(long, short = 'i')]
index: Option<PathBuf>,
index_output_path: Option<PathBuf>,
/// A revspec that points to the to generate the index from.
spec: std::ffi::OsString,
},
}
}

0 comments on commit 01ab5ca

Please sign in to comment.