From 3ff5ac0cda9e3e089dc79fdfbff5ff619ee60b1f Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 27 Aug 2023 17:03:47 +0200 Subject: [PATCH] feat: `gix free index from-list` and `gix index from-tree` gain `--skip-hash`. This flag can be derived from options, but thus far we have no higher-level writing of the index so this has to do to see the difference in performance. --- gitoxide-core/src/repository/index/mod.rs | 27 ++++++++++++++++------- src/plumbing/main.rs | 14 ++++++++++-- src/plumbing/options/free.rs | 3 +++ src/plumbing/options/mod.rs | 3 +++ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/gitoxide-core/src/repository/index/mod.rs b/gitoxide-core/src/repository/index/mod.rs index 17ba656b16f..63b359e331a 100644 --- a/gitoxide-core/src/repository/index/mod.rs +++ b/gitoxide-core/src/repository/index/mod.rs @@ -1,19 +1,23 @@ use std::{ffi::OsString, path::PathBuf}; use anyhow::bail; -use gix::prelude::FindExt; pub fn from_tree( + repo: gix::Repository, mut spec: OsString, index_path: Option, force: bool, - repo: gix::Repository, + skip_hash: bool, ) -> anyhow::Result<()> { spec.push("^{tree}"); let spec = gix::path::os_str_into_bstr(&spec)?; let tree = repo.rev_parse_single(spec)?; - let index = gix::index::State::from_tree(&tree, |oid, buf| repo.objects.find_tree_iter(oid, buf).ok())?; - let options = gix::index::write::Options::default(); + + let mut index = repo.index_from_tree(&tree)?; + let options = gix::index::write::Options { + skip_hash, + ..Default::default() + }; match index_path { Some(index_path) => { @@ -23,11 +27,10 @@ pub fn from_tree( index_path.display() ); } - let mut index = gix::index::File::from_state(index, index_path); + index.set_path(index_path); index.write(options)?; } None => { - let index = gix::index::File::from_state(index, std::path::PathBuf::new()); let mut out = Vec::with_capacity(512 * 1024); index.write_to(&mut out, options)?; } @@ -36,7 +39,12 @@ pub fn from_tree( Ok(()) } -pub fn from_list(entries_file: PathBuf, index_path: Option, force: bool) -> anyhow::Result<()> { +pub fn from_list( + entries_file: PathBuf, + index_path: Option, + force: bool, + skip_hash: bool, +) -> anyhow::Result<()> { use std::io::BufRead; let object_hash = gix::hash::Kind::Sha1; @@ -57,7 +65,10 @@ pub fn from_list(entries_file: PathBuf, index_path: Option, force: bool } index.sort_entries(); - let options = gix::index::write::Options::default(); + let options = gix::index::write::Options { + skip_hash, + ..Default::default() + }; match index_path { Some(index_path) => { if index_path.is_file() && !force { diff --git a/src/plumbing/main.rs b/src/plumbing/main.rs index 6383bc195bc..542fb402b4f 100644 --- a/src/plumbing/main.rs +++ b/src/plumbing/main.rs @@ -447,6 +447,7 @@ pub fn main() -> Result<()> { free::index::Subcommands::FromList { force, index_output_path, + skip_hash, file, } => prepare_and_run( "index-from-list", @@ -455,7 +456,9 @@ pub fn main() -> Result<()> { progress, progress_keep_open, None, - move |_progress, _out, _err| core::repository::index::from_list(file, index_output_path, force), + move |_progress, _out, _err| { + core::repository::index::from_list(file, index_output_path, force, skip_hash) + }, ), free::index::Subcommands::CheckoutExclusive { directory, @@ -1164,6 +1167,7 @@ pub fn main() -> Result<()> { index::Subcommands::FromTree { force, index_output_path, + skip_hash, spec, } => prepare_and_run( "index-from-tree", @@ -1173,7 +1177,13 @@ pub fn main() -> Result<()> { progress_keep_open, None, move |_progress, _out, _err| { - core::repository::index::from_tree(spec, index_output_path, force, repository(Mode::Strict)?) + core::repository::index::from_tree( + repository(Mode::Strict)?, + spec, + index_output_path, + force, + skip_hash, + ) }, ), }, diff --git a/src/plumbing/options/free.rs b/src/plumbing/options/free.rs index 625204bcf80..64aa443d4de 100644 --- a/src/plumbing/options/free.rs +++ b/src/plumbing/options/free.rs @@ -63,6 +63,9 @@ pub mod index { /// back by default, but that requires us to write more of the index to work. #[clap(long, short = 'i')] index_output_path: Option, + /// Don't write the trailing hash for a performance gain. + #[clap(long, short = 's')] + skip_hash: bool, /// The file to read the index entries from, one path per line. file: PathBuf, }, diff --git a/src/plumbing/options/mod.rs b/src/plumbing/options/mod.rs index 0ff3c1980d3..54a6d8db3c8 100644 --- a/src/plumbing/options/mod.rs +++ b/src/plumbing/options/mod.rs @@ -705,6 +705,9 @@ pub mod index { /// back by default, but that requires us to write more of the index to work. #[clap(long, short = 'i')] index_output_path: Option, + /// Don't write the trailing hash for a performance gain. + #[clap(long, short = 's')] + skip_hash: bool, /// A revspec that points to the to generate the index from. spec: std::ffi::OsString, },