Skip to content

Commit

Permalink
implement gix index from-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidney Douw committed Sep 15, 2022
1 parent 87f6db7 commit 2fbd3df
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
30 changes: 28 additions & 2 deletions gitoxide-core/src/index/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

use git::odb::FindExt;
use git_repository as git;
use std::path::{Path, PathBuf};

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

pub fn from_tree(
id: git::hash::ObjectId,
path: PathBuf,
force: bool,
repo: git::Repository,
mut err: impl std::io::Write,
) -> anyhow::Result<()> {
let state = git::index::State::from_tree(&id, |oid, buf| repo.objects.find_tree_iter(oid, buf).ok())?;

if path.is_file() {
writeln!(err, "File {:?} already exists", path).ok();
if force {
writeln!(err, "overwriting").ok();
} else {
anyhow::bail!("exiting");
}
}

let mut file = std::fs::File::create(&path)?;
state.write_to(&mut file, git::index::write::Options::default())?;

writeln!(err, "Successfully wrote file {:?}", path).ok();

Ok(())
}
12 changes: 11 additions & 1 deletion src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use gitoxide_core::pack::verify;

use crate::{
plumbing::options::{
commit, config, credential, exclude, free, mailmap, odb, remote, revision, tree, Args, Subcommands,
commit, config, credential, exclude, free, index, mailmap, odb, remote, revision, tree, Args, Subcommands,
},
shared::pretty::prepare_and_run,
};
Expand Down Expand Up @@ -755,6 +755,16 @@ pub fn main() -> Result<()> {
},
),
},
Subcommands::Index(cmd) => match cmd {
index::Subcommands::FromTree { force, id, path } => prepare_and_run(
"index-read-tree",
verbose,
progress,
progress_keep_open,
None,
move |_progress, _out, err| core::index::from_tree(id, path, force, repository(Mode::Strict)?, err),
),
},
}?;
Ok(())
}
Expand Down
25 changes: 23 additions & 2 deletions src/plumbing/options.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::path::PathBuf;

use git_repository as git;
use git_repository::bstr::BString;
use gitoxide_core as core;
use std::path::PathBuf;

#[derive(Debug, clap::Parser)]
#[clap(name = "gix-plumbing", about = "The git underworld", version = clap::crate_version!())]
Expand Down Expand Up @@ -93,6 +92,9 @@ pub enum Subcommands {
/// Subcommands that need no git repository to run.
#[clap(subcommand)]
Free(free::Subcommands),
/// Interact with index files
#[clap(subcommand)]
Index(index::Subcommands),
}

pub mod config {
Expand Down Expand Up @@ -304,6 +306,7 @@ pub mod free {
}
}

///
pub mod index {
use std::path::PathBuf;

Expand Down Expand Up @@ -664,3 +667,21 @@ pub mod exclude {
},
}
}

pub mod index {
use std::path::PathBuf;

#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
#[clap(visible_alias = "read-tree")]
FromTree {
/// Overwrite the specified file if it already exists
#[clap(long, short = 'f')]
force: bool,
/// Hash of the tree object to generate the index from
id: git_repository::hash::ObjectId,
/// Path to the index file to be written
path: PathBuf,
},
}
}

0 comments on commit 2fbd3df

Please sign in to comment.