Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
asks once per branch, with the same answer. On a kinisi_ros clone with 36 branches
that check fell from 146 `git` spawns and 20 s to 8 spawns and 0.5 s
([#720](https://github.com/blooop/devlaunch/pull/720)).
- **The squash rule no longer merges pairs that cannot pass.** It merged every
point of a branch into every remote ref it compares with, and on a kinisi_ros clone
one `dl --ls` row made 824 merges, none of which could pass. One `git rev-parse` of
the merge bases and one `git diff-tree` of the changed paths now rule those pairs out
first, and any failure of either merges every pair as before. On that clone the rule
fell from 18–20 s to 4–6 s, with the same answer
([#721](https://github.com/blooop/devlaunch/pull/721)).

## [0.64.3] - 2026-11-18

Expand Down
162 changes: 162 additions & 0 deletions rust/devlaunch-core/src/clients/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,78 @@
})
}

/// The merge bases of each of *pairs*, with the commit its remote ref
/// names, in the order of *pairs*; `None` when the output is not one
/// answer per pair.
///
/// One `rev-parse <ref>...<point>` per pair, all in one spawn. For a
/// symmetric range `rev-parse` prints the right side, the left side and then
/// each merge base with a `^` before it. Those are the bases `merge-base
/// --all` gives, the ones [`Git::holds_the_change_of`] asks for, so the
/// squash rule can tell before any merge that a pair with two bases, or a
/// ref with no change of its own since its base, cannot pass.
pub(crate) fn merge_bases_of(
&self,
clone: &Path,
pairs: &[(&str, &RemoteRef)],
) -> GitAnswer<Option<Vec<PairBases>>> {
if pairs.is_empty() {
return GitAnswer::Said(Some(Vec::new()));
}
let ranges: Vec<String> = pairs
.iter()
.map(|(point, other)| format!("{}...{point}", other.as_str()))
.collect();
let mut args = vec!["rev-parse"];
args.extend(ranges.iter().map(String::as_str));
self.about(clone, &args)
.map(|stdout| pair_bases_in(&stdout, pairs))
}

/// The paths each of *diffs* changes, `(commit, base)` read as the change
/// from *base* to *commit*, in the order of *diffs*; `None` when the output
/// is not one answer per diff.
///
/// One `diff-tree --stdin` for all of them: a line `<commit> <base>` names
/// *base* as the commit's one parent, whatever its real parents are. `-r`
/// lists files, not directories, and `--no-renames` lists a rename as the
/// path it left and the path it took. `--always` prints the commit's line
/// even for an empty diff, so every diff is accounted for, and `-z` ends
/// each field with a NUL, so no path is quoted.
pub(crate) fn paths_changed(
&self,
clone: &Path,
diffs: &[(&str, &str)],
) -> GitAnswer<Option<Vec<Vec<PathChange>>>> {
if diffs.is_empty() {
return GitAnswer::Said(Some(Vec::new()));
}
let lines: String = diffs
.iter()
.map(|(commit, base)| format!("{commit} {base}\n"))
.collect();
let Some(input) = tempfile::NamedTempFile::new()
.ok()
.filter(|file| std::fs::write(file.path(), &lines).is_ok())
else {
return GitAnswer::Said(None);

Check warning on line 1190 in rust/devlaunch-core/src/clients/git.rs

View check run for this annotation

Codecov / codecov/patch

rust/devlaunch-core/src/clients/git.rs#L1190

Added line #L1190 was not covered by tests
};
let args = [
"diff-tree",
"--stdin",
"-r",
"--no-renames",
"--name-status",
"-z",
"--always",
];
let spec = SpawnSpec::new(pinned(clone, &args))
.with_timeout(ABOUT_ONE_REPO)
.with_stdin_file(input.path());
self.captured(&args.join(" "), &spec)
.map(|stdout| paths_changed_in(&stdout, diffs))
}

/// How *clone* merges as text, or `None` when it cannot: an
/// `info/attributes` that could name a merge driver, or objects named in a
/// format [`empty_tree_of`] does not know.
Expand Down Expand Up @@ -2354,6 +2426,96 @@
Some(graph)
}

/// What [`Git::merge_bases_of`] found for one pair: the commit its remote ref
/// names, and its merge bases, none, one or more.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PairBases {
pub(crate) other: String,
pub(crate) bases: Vec<String>,
}

/// What a diff did to one path, as far as the squash rule cares: whether the
/// path is new, gone, or there on both sides.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Change {
Added,
Deleted,
/// Modified, or its type changed.
Altered,
}

/// One path a diff changed, and how.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PathChange {
pub(crate) change: Change,
pub(crate) path: String,
}

/// Whether *word* is a full object name, SHA-1 or SHA-256.
fn is_full_hash(word: &str) -> bool {
matches!(word.len(), 40 | 64) && word.bytes().all(|byte| byte.is_ascii_hexdigit())
}

/// The bases of each of *pairs* in `rev-parse <ref>...<point>` output: the
/// point, the ref's commit and a `^<base>` line per base, pair after pair.
/// `None` for output of any other shape, a point that is not the one asked
/// for included.
fn pair_bases_in(output: &str, pairs: &[(&str, &RemoteRef)]) -> Option<Vec<PairBases>> {
let mut lines = output.lines().peekable();
let mut found = Vec::with_capacity(pairs.len());
for (point, _) in pairs {
if lines.next()? != *point {
return None;
}
let other = lines.next().filter(|other| is_full_hash(other))?;
let mut bases = Vec::new();
while let Some(base) = lines.peek().and_then(|line| line.strip_prefix('^')) {
if !is_full_hash(base) {
return None;
}
bases.push(base.to_owned());
lines.next();
}
found.push(PairBases {
other: other.to_owned(),
bases,
});
}
lines.next().is_none().then_some(found)
}

/// The paths each of *diffs* changes in `diff-tree --stdin -z --always
/// --name-status` output: each diff's commit, then a status and a path per
/// changed path, every field ended by a NUL. `None` for output of any other
/// shape.
fn paths_changed_in(output: &str, diffs: &[(&str, &str)]) -> Option<Vec<Vec<PathChange>>> {
let mut fields = output.split('\0');
let mut found: Vec<Vec<PathChange>> = Vec::with_capacity(diffs.len());
let mut field = fields.next()?;
for (commit, _) in diffs {
if field != *commit {
return None;
}
let mut changed = Vec::new();
loop {
field = fields.next()?;
let change = match field {
"A" => Change::Added,
"D" => Change::Deleted,
"M" | "T" => Change::Altered,
_ => break,
};
let path = fields.next().filter(|path| !path.is_empty())?;
changed.push(PathChange {
change,
path: path.to_owned(),
});
}
found.push(changed);
}
(field.is_empty() && fields.next().is_none()).then_some(found)
}

/// What [`Git::text_merge`] found a clone can merge with: the `--attr-source`
/// that names the empty tree in the clone's object format.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
172 changes: 172 additions & 0 deletions rust/devlaunch-core/src/clients/git/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,3 +1724,175 @@ fn clean_merge_trees_are_read_from_merge_tree_stdin_output() {
assert_eq!(clean_merge_trees_in("1\0aaa\0", 1), None);
assert_eq!(clean_merge_trees_in("", 1), None);
}

const POINT: &str = "1111111111111111111111111111111111111111";
const REF_AT: &str = "2222222222222222222222222222222222222222";
const BASE: &str = "3333333333333333333333333333333333333333";
const OTHER_BASE: &str = "4444444444444444444444444444444444444444";

#[test]
fn the_squash_rule_asks_every_pair_s_bases_in_one_rev_parse() {
// `<ref>...<point>`, the ref on the left, so `rev-parse` prints the point
// first: that is the order `pair_bases_in` reads.
let (dir, _root) = a_clone();
let fake = ScriptedRunner::new();
let feature = RemoteRef::of("origin", "feature");
let main = RemoteRef::of("origin", "main");

Git::new(&fake).merge_bases_of(dir.path(), &[(POINT, &feature), (BASE, &main)]);

assert_eq!(
strs(&argv(&fake))[3..],
[
"rev-parse",
&format!("refs/remotes/origin/feature...{POINT}"),
&format!("refs/remotes/origin/main...{BASE}"),
]
);
}

#[test]
fn the_squash_rule_asks_every_change_in_one_diff_tree() {
// `--no-renames`: a rename is the path it left and the path it took, both
// of which the path check has to see. `--always`: an empty diff still
// prints its commit, so every diff is accounted for.
let (dir, _root) = a_clone();
let fake = ScriptedRunner::new();

Git::new(&fake).paths_changed(dir.path(), &[(POINT, BASE), (REF_AT, BASE)]);

let call = fake.only_call();
assert_eq!(
strs(&call.argv())[3..],
[
"diff-tree",
"--stdin",
"-r",
"--no-renames",
"--name-status",
"-z",
"--always",
]
);
assert!(
matches!(
call.spec().map(|spec| &spec.stdin),
Some(crate::runner::StdinPlan::File(_))
),
"the `<commit> <base>` lines go in on stdin"
);
}

#[test]
fn nothing_to_ask_spawns_nothing() {
let (dir, _root) = a_clone();
let fake = ScriptedRunner::new();
let git = Git::new(&fake);

assert_eq!(
git.merge_bases_of(dir.path(), &[]).said(),
Some(Some(vec![]))
);
assert_eq!(
git.paths_changed(dir.path(), &[]).said(),
Some(Some(vec![]))
);
assert!(fake.calls().is_empty());
}

#[test]
fn merge_bases_are_read_from_rev_parse_symmetric_range_output() {
let feature = RemoteRef::of("origin", "feature");
let pairs = [(POINT, &feature), (POINT, &feature), (POINT, &feature)];
// One base, two bases, and none (histories with no common commit).
let output = format!(
"{POINT}\n{REF_AT}\n^{BASE}\n\
{POINT}\n{REF_AT}\n^{BASE}\n^{OTHER_BASE}\n\
{POINT}\n{REF_AT}\n"
);
let bases = |found: &[&str]| PairBases {
other: REF_AT.to_owned(),
bases: found.iter().map(|base| (*base).to_owned()).collect(),
};
assert_eq!(
pair_bases_in(&output, &pairs),
Some(vec![bases(&[BASE]), bases(&[BASE, OTHER_BASE]), bases(&[])])
);

// A SHA-256 clone names its objects in 64 digits.
let long = "a".repeat(64);
let base = "b".repeat(64);
assert_eq!(
pair_bases_in(
&format!("{long}\n{long}\n^{base}\n"),
&[(long.as_str(), &feature)]
),
Some(vec![PairBases {
other: long.clone(),
bases: vec![base]
}])
);

// Fewer answers than pairs, more, a point that is not the one asked for, a
// ref or a base that is not a full hash: no answer at all.
let one = [(POINT, &feature)];
for (output, why) in [
(String::new(), "nothing"),
(format!("{POINT}\n"), "no ref"),
(
format!("{POINT}\n{REF_AT}\n{POINT}\n{REF_AT}\n"),
"a pair too many",
),
(format!("{BASE}\n{REF_AT}\n"), "another point"),
(
format!("{POINT}\nrefs/remotes/origin/feature\n"),
"a ref by name",
),
(format!("{POINT}\n{REF_AT}\n^abc\n"), "a short base"),
] {
assert_eq!(pair_bases_in(&output, &one), None, "{why}");
}
}

#[test]
fn changed_paths_are_read_from_diff_tree_stdin_output() {
let diffs = [(POINT, BASE), (REF_AT, BASE), (BASE, BASE)];
// Each kind of change, a path with a space and a newline (`-z` quotes
// none), and an empty diff that `--always` still names.
let output = format!(
"{POINT}\0A\0new dir/a.txt\0D\0gone.txt\0{REF_AT}\0M\0line\nbreak.txt\0T\0link\0{BASE}\0"
);
let change = |change: Change, path: &str| PathChange {
change,
path: path.to_owned(),
};
assert_eq!(
paths_changed_in(&output, &diffs),
Some(vec![
vec![
change(Change::Added, "new dir/a.txt"),
change(Change::Deleted, "gone.txt")
],
vec![
change(Change::Altered, "line\nbreak.txt"),
change(Change::Altered, "link")
],
vec![],
])
);

// A diff missing, a diff too many, another commit, a status this does not
// know (a rename `--no-renames` should have split) and a path cut short:
// no answer at all.
let one = [(POINT, BASE)];
for (output, why) in [
(String::new(), "nothing"),
(format!("{POINT}\0{REF_AT}\0"), "a diff too many"),
(format!("{REF_AT}\0"), "another commit"),
(format!("{POINT}\0R100\0a\0b\0"), "a rename"),
(format!("{POINT}\0A\0"), "no path"),
(format!("{POINT}\0A\0a"), "no NUL at the end"),
] {
assert_eq!(paths_changed_in(&output, &one), None, "{why}");
}
}
Loading
Loading