Skip to content

Commit

Permalink
Use stupid temporary indexes instead of git2
Browse files Browse the repository at this point in the history
The new Stupid::with_temp_index() method replaces the
Index::with_temp_index() and Index::with_temp_index_file() extension
methods. This is motivated by sparse checkouts (#195) since git2::Index
does not comprehend sparse checkouts.
  • Loading branch information
jpgrayson committed Aug 24, 2022
1 parent 0cb47b3 commit f9d9de0
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 126 deletions.
11 changes: 3 additions & 8 deletions src/cmd/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
color::get_color_stdout,
commit::{CommitMessage, RepositoryCommitExtended},
hook::run_pre_commit_hook,
index::TemporaryIndex,
patchedit,
patchname::PatchName,
signature::SignatureExtended,
Expand Down Expand Up @@ -280,9 +279,7 @@ fn run(matches: &ArgMatches) -> Result<()> {
let ours = patch_commit.tree_id();
let theirs = temp_commit.tree_id();

if let Some(tree_id) = repo.with_temp_index_file(|temp_index| {
let stupid = repo.stupid();
let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());
if let Some(tree_id) = repo.stupid().with_temp_index(|stupid_temp| {
stupid_temp.read_tree(ours)?;
if stupid_temp.apply_treediff_to_index(base, theirs)? {
let tree_id = stupid_temp.write_tree()?;
Expand Down Expand Up @@ -412,13 +409,11 @@ fn write_tree(
// may be formed into a coherent tree while leaving the default index as-is.
let stupid = stack.repo.stupid();
if is_path_limiting {
let stupid_temp = stupid.get_temp_index_context();
let stupid_temp = stupid_temp.context();
let tree_id_result = {
let tree_id_result = stupid.with_temp_index(|stupid_temp| {
stupid_temp.read_tree(stack.branch_head.tree_id())?;
stupid_temp.update_index(Some(refresh_paths))?;
stupid_temp.write_tree()
};
});
stupid.update_index(Some(refresh_paths))?;
tree_id_result
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/cmd/spill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use clap::{Arg, ArgMatches};
use crate::{
color::get_color_stdout,
commit::{CommitExtended, RepositoryCommitExtended},
index::TemporaryIndex,
repo::RepositoryExtended,
stack::{Error, Stack, StackStateAccess},
stupid::Stupid,
Expand Down Expand Up @@ -86,8 +85,7 @@ fn run(matches: &ArgMatches) -> Result<()> {
let mut index = repo.index()?;

let tree_id = if let Some(pathspecs) = matches.get_many::<PathBuf>("pathspecs") {
stack.repo.with_temp_index_file(|temp_index| {
let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());
stupid.with_temp_index(|stupid_temp| {
stupid_temp.read_tree(patch_commit.tree_id())?;
stupid_temp.apply_pathlimited_treediff_to_index(
patch_commit.tree_id(),
Expand Down
11 changes: 4 additions & 7 deletions src/cmd/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use clap::{Arg, ArgMatches};
use crate::{
color::get_color_stdout,
commit::CommitExtended,
index::TemporaryIndex,
patchedit,
patchname::PatchName,
patchrange, print_info_message,
Expand Down Expand Up @@ -234,20 +233,18 @@ fn try_squash(
) -> Result<Option<(PatchName, git2::Oid)>> {
let repo = trans.repo();
let base_commit = trans.get_patch_commit(&patchnames[0]);
if let Some(tree_id) = repo.with_temp_index_file(|temp_index| {
let stupid = repo.stupid();
let stupid = stupid.with_index_path(temp_index.path().unwrap());
stupid.read_tree(base_commit.tree_id())?;
if let Some(tree_id) = repo.stupid().with_temp_index(|stupid_temp| {
stupid_temp.read_tree(base_commit.tree_id())?;
for commit in patchnames[1..].iter().map(|pn| trans.get_patch_commit(pn)) {
let parent = commit.parent(0)?;
if parent.tree_id() != commit.tree_id()
&& !stupid.apply_treediff_to_index(parent.tree_id(), commit.tree_id())?
&& !stupid_temp.apply_treediff_to_index(parent.tree_id(), commit.tree_id())?
{
return Ok(None);
}
}

let tree_id = stupid.write_tree()?;
let tree_id = stupid_temp.write_tree()?;
Ok(Some(tree_id))
})? {
if let patchedit::EditOutcome::Committed {
Expand Down
56 changes: 0 additions & 56 deletions src/index.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ mod cmd;
mod color;
mod commit;
mod hook;
mod index;
mod patchedit;
mod patchname;
mod patchrange;
Expand Down
7 changes: 2 additions & 5 deletions src/patchedit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use clap::{

use crate::{
commit::{CommitExtended, CommitMessage, RepositoryCommitExtended},
index::TemporaryIndex,
patchname::PatchName,
signature::{self, SignatureExtended},
stack::StackStateAccess,
Expand Down Expand Up @@ -461,6 +460,7 @@ impl<'a, 'repo> EditBuilder<'a, 'repo> {
},
} = self;

let stupid = repo.stupid();
let config = repo.config()?;
let default_committer = git2::Signature::default_committer(Some(&config))?;
let committer = patch_commit
Expand Down Expand Up @@ -600,7 +600,6 @@ impl<'a, 'repo> EditBuilder<'a, 'repo> {
{
let old_tree = repo.find_commit(parent_id)?.tree()?;
let new_tree = repo.find_tree(tree_id)?;
let stupid = repo.stupid();
let diff_buf = if patch_commit.is_some() || old_tree.id() != new_tree.id() {
stupid.diff_tree_patch(
old_tree.id(),
Expand Down Expand Up @@ -722,9 +721,7 @@ impl<'a, 'repo> EditBuilder<'a, 'repo> {
let tree_id = if need_to_apply_diff {
let diff = diff.unwrap().0;

match repo.with_temp_index_file(|temp_index| {
let stupid = repo.stupid();
let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());
match stupid.with_temp_index(|stupid_temp| {
stupid_temp.read_tree(parent_id)?;
stupid_temp.apply_to_index(&diff)?;
stupid_temp.write_tree()
Expand Down
28 changes: 12 additions & 16 deletions src/stack/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ use termcolor::WriteColor;

use crate::{
commit::{CommitExtended, RepositoryCommitExtended},
index::TemporaryIndex,
patchname::PatchName,
signature::SignatureExtended,
stack::{PatchState, Stack, StackStateAccess},
stupid::Stupid,
stupid::{Stupid, StupidContext},
};

use super::{error::Error, state::StackState};
Expand Down Expand Up @@ -1034,11 +1033,12 @@ impl<'repo> StackTransaction<'repo> {
where
P: AsRef<PatchName>,
{
self.stack.repo.with_temp_index_file(|temp_index| {
let stupid = self.stack.repo.stupid();
stupid.with_temp_index(|stupid_temp| {
let mut temp_index_tree_id: Option<git2::Oid> = None;

let merged = if check_merged {
Some(self.check_merged(patchnames, temp_index, &mut temp_index_tree_id)?)
Some(self.check_merged(patchnames, stupid_temp, &mut temp_index_tree_id)?)
} else {
None
};
Expand All @@ -1054,10 +1054,11 @@ impl<'repo> StackTransaction<'repo> {
patchname,
already_merged,
is_last,
temp_index,
stupid_temp,
&mut temp_index_tree_id,
)?;
}

Ok(())
})
}
Expand All @@ -1067,7 +1068,7 @@ impl<'repo> StackTransaction<'repo> {
patchname: &PatchName,
already_merged: bool,
is_last: bool,
temp_index: &mut git2::Index,
stupid_temp: &StupidContext,
temp_index_tree_id: &mut Option<git2::Oid>,
) -> Result<()> {
let repo = self.stack.repo;
Expand Down Expand Up @@ -1096,10 +1097,6 @@ impl<'repo> StackTransaction<'repo> {
(new_parent.tree_id(), patch_commit.tree_id())
};
let base = old_parent.tree_id();
// let ours = new_parent.tree_id();
// let theirs = patch_commit.tree_id();

let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());

if temp_index_tree_id != &Some(ours) {
stupid_temp.read_tree(ours)?;
Expand Down Expand Up @@ -1218,20 +1215,17 @@ impl<'repo> StackTransaction<'repo> {
fn check_merged<'a, P>(
&self,
patchnames: &'a [P],
temp_index: &mut git2::Index,
stupid_temp: &StupidContext,
temp_index_tree_id: &mut Option<git2::Oid>,
) -> Result<Vec<&'a PatchName>>
where
P: AsRef<PatchName>,
{
let repo = self.stack.repo;
let stupid = repo.stupid();
let stupid = stupid.with_index_path(temp_index.path().unwrap());
let head_tree_id = self.stack.branch_head.tree_id();
let mut merged: Vec<&PatchName> = vec![];

if temp_index_tree_id != &Some(head_tree_id) {
stupid.read_tree(head_tree_id)?;
stupid_temp.read_tree(head_tree_id)?;
*temp_index_tree_id = Some(head_tree_id);
}

Expand All @@ -1245,7 +1239,9 @@ impl<'repo> StackTransaction<'repo> {

let parent_commit = patch_commit.parent(0)?;

if stupid.apply_treediff_to_index(patch_commit.tree_id(), parent_commit.tree_id())? {
if stupid_temp
.apply_treediff_to_index(patch_commit.tree_id(), parent_commit.tree_id())?
{
merged.push(patchname);
*temp_index_tree_id = None;
}
Expand Down
46 changes: 16 additions & 30 deletions src/stupid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) mod status;
use std::{
ffi::{OsStr, OsString},
io::Write,
path::{Path, PathBuf},
path::Path,
process::{Command, Stdio},
};

Expand Down Expand Up @@ -62,32 +62,14 @@ pub(crate) struct StupidContext<'repo, 'index> {
pub work_dir: Option<&'repo Path>,
}

pub(crate) struct StupidTempIndexContext<'repo> {
git_dir: Option<&'repo Path>,
temp_index_path: PathBuf,
work_dir: Option<&'repo Path>,
}

impl<'repo> StupidTempIndexContext<'repo> {
pub(crate) fn context(&self) -> StupidContext<'repo, '_> {
StupidContext {
git_dir: self.git_dir,
index_path: Some(self.temp_index_path.as_path()),
work_dir: self.work_dir,
}
}
}

impl<'repo, 'index> StupidContext<'repo, 'index> {
pub(crate) fn with_index_path(&self, index_path: &'index Path) -> StupidContext {
StupidContext {
git_dir: self.git_dir,
index_path: Some(index_path),
work_dir: self.work_dir,
}
}

pub(crate) fn get_temp_index_context(&self) -> StupidTempIndexContext {
/// Perform actions with a temporary index file.
///
/// The temporary index file is automatically deleted when this call returns.
pub(crate) fn with_temp_index<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(&StupidContext) -> Result<T>,
{
let temp_index_root = if let Some(git_dir) = self.git_dir {
git_dir
} else {
Expand All @@ -96,12 +78,16 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
.parent()
.expect("git index path has parent")
};

StupidTempIndexContext {
let index_tempfile = tempfile::Builder::new()
.prefix("index-temp-stg")
.tempfile_in(temp_index_root)?;
let stupid_temp = StupidContext {
git_dir: self.git_dir,
temp_index_path: temp_index_root.join("index-temp-stgit"),
index_path: Some(index_tempfile.path()),
work_dir: self.work_dir,
}
};

f(&stupid_temp)
}

fn git(&self) -> Command {
Expand Down

0 comments on commit f9d9de0

Please sign in to comment.