Skip to content

Commit

Permalink
feat: gix free index from-list and gix index from-tree gain `--sk…
Browse files Browse the repository at this point in the history
…ip-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.
  • Loading branch information
Byron committed Aug 27, 2023
1 parent 2f42132 commit 3ff5ac0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
27 changes: 19 additions & 8 deletions gitoxide-core/src/repository/index/mod.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,
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) => {
Expand All @@ -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)?;
}
Expand All @@ -36,7 +39,12 @@ pub fn from_tree(
Ok(())
}

pub fn from_list(entries_file: PathBuf, index_path: Option<PathBuf>, force: bool) -> anyhow::Result<()> {
pub fn from_list(
entries_file: PathBuf,
index_path: Option<PathBuf>,
force: bool,
skip_hash: bool,
) -> anyhow::Result<()> {
use std::io::BufRead;
let object_hash = gix::hash::Kind::Sha1;

Expand All @@ -57,7 +65,10 @@ pub fn from_list(entries_file: PathBuf, index_path: Option<PathBuf>, 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 {
Expand Down
14 changes: 12 additions & 2 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -1164,6 +1167,7 @@ pub fn main() -> Result<()> {
index::Subcommands::FromTree {
force,
index_output_path,
skip_hash,
spec,
} => prepare_and_run(
"index-from-tree",
Expand All @@ -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,
)
},
),
},
Expand Down
3 changes: 3 additions & 0 deletions src/plumbing/options/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// 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,
},
Expand Down
3 changes: 3 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// 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,
},
Expand Down

0 comments on commit 3ff5ac0

Please sign in to comment.