Skip to content

Commit acb069a

Browse files
committed
refactor squash me
1 parent e856ca8 commit acb069a

File tree

4 files changed

+48
-30
lines changed

4 files changed

+48
-30
lines changed
-365 KB
Binary file not shown.

gix-merge/tests/fixtures/tree-baseline.sh

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,6 @@ function baseline () (
6666
git -c merge.conflictStyle=$conflict_style merge-tree -z --write-tree --allow-unrelated-histories "$their_committish" "$our_committish" > "$merge_info" || :
6767
echo "$dir" "$conflict_style" "$their_commit_id" "$their_committish" "$our_commit_id" "$our_committish" "$merge_info" "$maybe_expected_tree" "$opt_deviation_message" >> ../baseline.cases
6868
fi
69-
70-
local index_path=.git/${conflict_style}-${our_committish}-${their_committish}.index
71-
if [ ! -e $index_path ]; then
72-
git checkout -f $our_committish
73-
git merge -m m $their_committish || :
74-
cp .git/index "$index_path"
75-
fi
76-
77-
local index_path=.git/${conflict_style}-${their_committish}-${our_committish}.index
78-
if [ ! -e $index_path ]; then
79-
git checkout -f $their_committish
80-
git merge -m m $our_committish || :
81-
cp .git/index "$index_path"
82-
fi
8369
)
8470

8571
git init simple

gix-merge/tests/merge/tree/baseline.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use gix_object::FindExt;
66
use std::path::{Path, PathBuf};
77

88
/// An entry in the conflict
9-
#[derive(Debug)]
9+
#[derive(Debug, Eq, PartialEq)]
1010
pub struct Entry {
1111
/// The relative path in the repository
1212
pub location: String,
@@ -17,7 +17,7 @@ pub struct Entry {
1717
}
1818

1919
/// Keep track of all the sides of a conflict. Some might not be set to indicate removal, including the ancestor.
20-
#[derive(Default, Debug)]
20+
#[derive(Default, Debug, Eq, PartialEq)]
2121
pub struct Conflict {
2222
pub ancestor: Option<Entry>,
2323
pub ours: Option<Entry>,
@@ -96,7 +96,6 @@ pub struct Expectation {
9696
pub their_side_name: String,
9797
pub merge_info: MergeInfo,
9898
pub case_name: String,
99-
pub index: gix_index::File,
10099
pub deviation: Option<Deviation>,
101100
}
102101

@@ -171,15 +170,6 @@ impl Iterator for Expectations<'_> {
171170
let our_commit_id = gix_hash::ObjectId::from_hex(our_commit_id.as_bytes()).unwrap();
172171
let their_commit_id = gix_hash::ObjectId::from_hex(their_commit_id.as_bytes()).unwrap();
173172
let merge_info = parse_merge_info(std::fs::read_to_string(subdir_path.join(merge_info_filename)).unwrap());
174-
let index = gix_index::File::at(
175-
subdir_path
176-
.join(".git")
177-
.join(format!("{conflict_style_name}-{our_side_name}-{their_side_name}.index")),
178-
gix_hash::Kind::Sha1,
179-
false, /* skip hash */
180-
Default::default(),
181-
)
182-
.expect("index should be present for each combination");
183173
Some(Expectation {
184174
root: subdir_path,
185175
conflict_style,
@@ -189,7 +179,6 @@ impl Iterator for Expectations<'_> {
189179
their_commit_id,
190180
their_side_name: their_side_name.to_owned(),
191181
merge_info,
192-
index,
193182
case_name: format!(
194183
"{subdir}-{}",
195184
merge_info_filename
@@ -232,6 +221,10 @@ fn parse_merge_info(content: String) -> MergeInfo {
232221
*field = Some(entry);
233222
}
234223

224+
if conflicts.last() != Some(&conflict) {
225+
conflicts.push(conflict);
226+
}
227+
235228
while lines.peek().is_some() {
236229
out.information
237230
.push(parse_info(&mut lines).expect("if there are lines, it should be valid info"));
@@ -377,3 +370,35 @@ pub fn show_diff_and_fail(
377370
expected.information
378371
);
379372
}
373+
374+
pub(crate) fn apply_git_index_entries(conflicts: Vec<Conflict>, state: &mut gix_index::State) {
375+
let len = state.entries().len();
376+
for Conflict { ours, theirs, ancestor } in conflicts {
377+
for (entry, stage) in [
378+
ancestor.map(|e| (e, gix_index::entry::Stage::Base)),
379+
ours.map(|e| (e, gix_index::entry::Stage::Ours)),
380+
theirs.map(|e| (e, gix_index::entry::Stage::Theirs)),
381+
]
382+
.into_iter()
383+
.filter_map(std::convert::identity)
384+
{
385+
if let Some(pos) = state.entry_index_by_path_and_stage_bounded(
386+
entry.location.as_str().into(),
387+
gix_index::entry::Stage::Unconflicted,
388+
len,
389+
) {
390+
state.entries_mut()[pos].flags.insert(gix_index::entry::Flags::REMOVE)
391+
}
392+
393+
state.dangerously_push_entry(
394+
Default::default(),
395+
entry.id,
396+
gix_index::entry::Flags::empty() | gix_index::entry::Flags::from_bits((stage as u32) << 12).unwrap(),
397+
entry.mode.into(),
398+
entry.location.as_str().into(),
399+
);
400+
}
401+
}
402+
state.sort_entries();
403+
state.remove_entries(|_, _, e| e.flags.contains(gix_index::entry::Flags::REMOVE));
404+
}

gix-merge/tests/merge/tree/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ fn run_baseline() -> crate::Result {
3232
their_side_name,
3333
merge_info,
3434
case_name,
35-
index,
3635
deviation,
3736
} in baseline::Expectations::new(&root, &cases)
3837
.filter(|case| new_test.map_or(true, |prefix: &str| case.case_name.starts_with(prefix)))
@@ -100,10 +99,18 @@ fn run_baseline() -> crate::Result {
10099
}
101100
}
102101

103-
let actual_index = gix_index::State::from_tree(&actual_id, &odb, Default::default())?;
102+
let mut actual_index = gix_index::State::from_tree(&actual_id, &odb, Default::default())?;
103+
let expected_index = {
104+
let mut index = actual_index.clone();
105+
if let Some(conflicts) = merge_info.conflicts {
106+
baseline::apply_git_index_entries(conflicts, &mut index)
107+
}
108+
index
109+
};
110+
actual.index_changed_after_applying_conflicts(&mut actual_index)?;
104111
pretty_assertions::assert_eq!(
105112
baseline::clear_entries(&actual_index),
106-
baseline::clear_entries(&index),
113+
baseline::clear_entries(&expected_index),
107114
"{case_name}: index mismatch"
108115
);
109116
}

0 commit comments

Comments
 (0)