Skip to content

Commit f9d9de0

Browse files
committed
Use stupid temporary indexes instead of git2
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.
1 parent 0cb47b3 commit f9d9de0

File tree

8 files changed

+38
-126
lines changed

8 files changed

+38
-126
lines changed

src/cmd/refresh.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{
1515
color::get_color_stdout,
1616
commit::{CommitMessage, RepositoryCommitExtended},
1717
hook::run_pre_commit_hook,
18-
index::TemporaryIndex,
1918
patchedit,
2019
patchname::PatchName,
2120
signature::SignatureExtended,
@@ -280,9 +279,7 @@ fn run(matches: &ArgMatches) -> Result<()> {
280279
let ours = patch_commit.tree_id();
281280
let theirs = temp_commit.tree_id();
282281

283-
if let Some(tree_id) = repo.with_temp_index_file(|temp_index| {
284-
let stupid = repo.stupid();
285-
let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());
282+
if let Some(tree_id) = repo.stupid().with_temp_index(|stupid_temp| {
286283
stupid_temp.read_tree(ours)?;
287284
if stupid_temp.apply_treediff_to_index(base, theirs)? {
288285
let tree_id = stupid_temp.write_tree()?;
@@ -412,13 +409,11 @@ fn write_tree(
412409
// may be formed into a coherent tree while leaving the default index as-is.
413410
let stupid = stack.repo.stupid();
414411
if is_path_limiting {
415-
let stupid_temp = stupid.get_temp_index_context();
416-
let stupid_temp = stupid_temp.context();
417-
let tree_id_result = {
412+
let tree_id_result = stupid.with_temp_index(|stupid_temp| {
418413
stupid_temp.read_tree(stack.branch_head.tree_id())?;
419414
stupid_temp.update_index(Some(refresh_paths))?;
420415
stupid_temp.write_tree()
421-
};
416+
});
422417
stupid.update_index(Some(refresh_paths))?;
423418
tree_id_result
424419
} else {

src/cmd/spill.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use clap::{Arg, ArgMatches};
1010
use crate::{
1111
color::get_color_stdout,
1212
commit::{CommitExtended, RepositoryCommitExtended},
13-
index::TemporaryIndex,
1413
repo::RepositoryExtended,
1514
stack::{Error, Stack, StackStateAccess},
1615
stupid::Stupid,
@@ -86,8 +85,7 @@ fn run(matches: &ArgMatches) -> Result<()> {
8685
let mut index = repo.index()?;
8786

8887
let tree_id = if let Some(pathspecs) = matches.get_many::<PathBuf>("pathspecs") {
89-
stack.repo.with_temp_index_file(|temp_index| {
90-
let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());
88+
stupid.with_temp_index(|stupid_temp| {
9189
stupid_temp.read_tree(patch_commit.tree_id())?;
9290
stupid_temp.apply_pathlimited_treediff_to_index(
9391
patch_commit.tree_id(),

src/cmd/squash.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use clap::{Arg, ArgMatches};
1111
use crate::{
1212
color::get_color_stdout,
1313
commit::CommitExtended,
14-
index::TemporaryIndex,
1514
patchedit,
1615
patchname::PatchName,
1716
patchrange, print_info_message,
@@ -234,20 +233,18 @@ fn try_squash(
234233
) -> Result<Option<(PatchName, git2::Oid)>> {
235234
let repo = trans.repo();
236235
let base_commit = trans.get_patch_commit(&patchnames[0]);
237-
if let Some(tree_id) = repo.with_temp_index_file(|temp_index| {
238-
let stupid = repo.stupid();
239-
let stupid = stupid.with_index_path(temp_index.path().unwrap());
240-
stupid.read_tree(base_commit.tree_id())?;
236+
if let Some(tree_id) = repo.stupid().with_temp_index(|stupid_temp| {
237+
stupid_temp.read_tree(base_commit.tree_id())?;
241238
for commit in patchnames[1..].iter().map(|pn| trans.get_patch_commit(pn)) {
242239
let parent = commit.parent(0)?;
243240
if parent.tree_id() != commit.tree_id()
244-
&& !stupid.apply_treediff_to_index(parent.tree_id(), commit.tree_id())?
241+
&& !stupid_temp.apply_treediff_to_index(parent.tree_id(), commit.tree_id())?
245242
{
246243
return Ok(None);
247244
}
248245
}
249246

250-
let tree_id = stupid.write_tree()?;
247+
let tree_id = stupid_temp.write_tree()?;
251248
Ok(Some(tree_id))
252249
})? {
253250
if let patchedit::EditOutcome::Committed {

src/index.rs

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ mod cmd;
1616
mod color;
1717
mod commit;
1818
mod hook;
19-
mod index;
2019
mod patchedit;
2120
mod patchname;
2221
mod patchrange;

src/patchedit/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use clap::{
2525

2626
use crate::{
2727
commit::{CommitExtended, CommitMessage, RepositoryCommitExtended},
28-
index::TemporaryIndex,
2928
patchname::PatchName,
3029
signature::{self, SignatureExtended},
3130
stack::StackStateAccess,
@@ -461,6 +460,7 @@ impl<'a, 'repo> EditBuilder<'a, 'repo> {
461460
},
462461
} = self;
463462

463+
let stupid = repo.stupid();
464464
let config = repo.config()?;
465465
let default_committer = git2::Signature::default_committer(Some(&config))?;
466466
let committer = patch_commit
@@ -600,7 +600,6 @@ impl<'a, 'repo> EditBuilder<'a, 'repo> {
600600
{
601601
let old_tree = repo.find_commit(parent_id)?.tree()?;
602602
let new_tree = repo.find_tree(tree_id)?;
603-
let stupid = repo.stupid();
604603
let diff_buf = if patch_commit.is_some() || old_tree.id() != new_tree.id() {
605604
stupid.diff_tree_patch(
606605
old_tree.id(),
@@ -722,9 +721,7 @@ impl<'a, 'repo> EditBuilder<'a, 'repo> {
722721
let tree_id = if need_to_apply_diff {
723722
let diff = diff.unwrap().0;
724723

725-
match repo.with_temp_index_file(|temp_index| {
726-
let stupid = repo.stupid();
727-
let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());
724+
match stupid.with_temp_index(|stupid_temp| {
728725
stupid_temp.read_tree(parent_id)?;
729726
stupid_temp.apply_to_index(&diff)?;
730727
stupid_temp.write_tree()

src/stack/transaction.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ use termcolor::WriteColor;
3636

3737
use crate::{
3838
commit::{CommitExtended, RepositoryCommitExtended},
39-
index::TemporaryIndex,
4039
patchname::PatchName,
4140
signature::SignatureExtended,
4241
stack::{PatchState, Stack, StackStateAccess},
43-
stupid::Stupid,
42+
stupid::{Stupid, StupidContext},
4443
};
4544

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

10401040
let merged = if check_merged {
1041-
Some(self.check_merged(patchnames, temp_index, &mut temp_index_tree_id)?)
1041+
Some(self.check_merged(patchnames, stupid_temp, &mut temp_index_tree_id)?)
10421042
} else {
10431043
None
10441044
};
@@ -1054,10 +1054,11 @@ impl<'repo> StackTransaction<'repo> {
10541054
patchname,
10551055
already_merged,
10561056
is_last,
1057-
temp_index,
1057+
stupid_temp,
10581058
&mut temp_index_tree_id,
10591059
)?;
10601060
}
1061+
10611062
Ok(())
10621063
})
10631064
}
@@ -1067,7 +1068,7 @@ impl<'repo> StackTransaction<'repo> {
10671068
patchname: &PatchName,
10681069
already_merged: bool,
10691070
is_last: bool,
1070-
temp_index: &mut git2::Index,
1071+
stupid_temp: &StupidContext,
10711072
temp_index_tree_id: &mut Option<git2::Oid>,
10721073
) -> Result<()> {
10731074
let repo = self.stack.repo;
@@ -1096,10 +1097,6 @@ impl<'repo> StackTransaction<'repo> {
10961097
(new_parent.tree_id(), patch_commit.tree_id())
10971098
};
10981099
let base = old_parent.tree_id();
1099-
// let ours = new_parent.tree_id();
1100-
// let theirs = patch_commit.tree_id();
1101-
1102-
let stupid_temp = stupid.with_index_path(temp_index.path().unwrap());
11031100

11041101
if temp_index_tree_id != &Some(ours) {
11051102
stupid_temp.read_tree(ours)?;
@@ -1218,20 +1215,17 @@ impl<'repo> StackTransaction<'repo> {
12181215
fn check_merged<'a, P>(
12191216
&self,
12201217
patchnames: &'a [P],
1221-
temp_index: &mut git2::Index,
1218+
stupid_temp: &StupidContext,
12221219
temp_index_tree_id: &mut Option<git2::Oid>,
12231220
) -> Result<Vec<&'a PatchName>>
12241221
where
12251222
P: AsRef<PatchName>,
12261223
{
1227-
let repo = self.stack.repo;
1228-
let stupid = repo.stupid();
1229-
let stupid = stupid.with_index_path(temp_index.path().unwrap());
12301224
let head_tree_id = self.stack.branch_head.tree_id();
12311225
let mut merged: Vec<&PatchName> = vec![];
12321226

12331227
if temp_index_tree_id != &Some(head_tree_id) {
1234-
stupid.read_tree(head_tree_id)?;
1228+
stupid_temp.read_tree(head_tree_id)?;
12351229
*temp_index_tree_id = Some(head_tree_id);
12361230
}
12371231

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

12461240
let parent_commit = patch_commit.parent(0)?;
12471241

1248-
if stupid.apply_treediff_to_index(patch_commit.tree_id(), parent_commit.tree_id())? {
1242+
if stupid_temp
1243+
.apply_treediff_to_index(patch_commit.tree_id(), parent_commit.tree_id())?
1244+
{
12491245
merged.push(patchname);
12501246
*temp_index_tree_id = None;
12511247
}

src/stupid/mod.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) mod status;
2424
use std::{
2525
ffi::{OsStr, OsString},
2626
io::Write,
27-
path::{Path, PathBuf},
27+
path::Path,
2828
process::{Command, Stdio},
2929
};
3030

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

65-
pub(crate) struct StupidTempIndexContext<'repo> {
66-
git_dir: Option<&'repo Path>,
67-
temp_index_path: PathBuf,
68-
work_dir: Option<&'repo Path>,
69-
}
70-
71-
impl<'repo> StupidTempIndexContext<'repo> {
72-
pub(crate) fn context(&self) -> StupidContext<'repo, '_> {
73-
StupidContext {
74-
git_dir: self.git_dir,
75-
index_path: Some(self.temp_index_path.as_path()),
76-
work_dir: self.work_dir,
77-
}
78-
}
79-
}
80-
8165
impl<'repo, 'index> StupidContext<'repo, 'index> {
82-
pub(crate) fn with_index_path(&self, index_path: &'index Path) -> StupidContext {
83-
StupidContext {
84-
git_dir: self.git_dir,
85-
index_path: Some(index_path),
86-
work_dir: self.work_dir,
87-
}
88-
}
89-
90-
pub(crate) fn get_temp_index_context(&self) -> StupidTempIndexContext {
66+
/// Perform actions with a temporary index file.
67+
///
68+
/// The temporary index file is automatically deleted when this call returns.
69+
pub(crate) fn with_temp_index<F, T>(&self, f: F) -> Result<T>
70+
where
71+
F: FnOnce(&StupidContext) -> Result<T>,
72+
{
9173
let temp_index_root = if let Some(git_dir) = self.git_dir {
9274
git_dir
9375
} else {
@@ -96,12 +78,16 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
9678
.parent()
9779
.expect("git index path has parent")
9880
};
99-
100-
StupidTempIndexContext {
81+
let index_tempfile = tempfile::Builder::new()
82+
.prefix("index-temp-stg")
83+
.tempfile_in(temp_index_root)?;
84+
let stupid_temp = StupidContext {
10185
git_dir: self.git_dir,
102-
temp_index_path: temp_index_root.join("index-temp-stgit"),
86+
index_path: Some(index_tempfile.path()),
10387
work_dir: self.work_dir,
104-
}
88+
};
89+
90+
f(&stupid_temp)
10591
}
10692

10793
fn git(&self) -> Command {

0 commit comments

Comments
 (0)