diff --git a/CHANGELOG.md b/CHANGELOG.md index ab6497cb..72a40a78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- **The check for unpushed work no longer asks git once per branch and remote ref.** + The check for unpushed work compared each local branch with each remote ref in a + separate `git rev-list`, so its cost grew with the square of the branches. It now + 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)). + ## [0.64.3] - 2026-11-18 ### Fixed diff --git a/rust/devlaunch-core/src/clients/git.rs b/rust/devlaunch-core/src/clients/git.rs index 1dd01aeb..af70694c 100644 --- a/rust/devlaunch-core/src/clients/git.rs +++ b/rust/devlaunch-core/src/clients/git.rs @@ -791,8 +791,8 @@ impl<'r> Git<'r> { .map(|stdout| local_branches_in(&stdout)) } - /// The commits on *tip* that the remote ref *other* does not contain but - /// holds a copy of, as full hashes. + /// The commits on *tip* that a remote ref of *others* holds a copy of, as + /// full hashes. /// /// A copy is a commit that makes the same change in the same place: put the /// local commit on the copy's parent and git gets the copy's tree. A rebase, @@ -815,6 +815,25 @@ impl<'r> Git<'r> { /// `--left-right` marks a match `=` on either side, and a match between two /// left commits is two local copies, not one on the remote. /// + /// **All of *others* are asked about in one question**, one symmetric range + /// `tip...other` per ref. A walk over all of them leaves out what *tip* + /// shares with any of them, so its left side is the commits on *tip* that + /// none of them reach, and its right side is the commits that one of them + /// reaches and *tip* does not. For a commit no remote ref reaches, which is + /// the only kind this is asked to clear, that is the same answer as one + /// question per ref: a copy on any one ref is on the right side here, and a + /// copy on no ref is on neither. A commit that one of *others* already + /// reaches drops off the left side, and nothing asks about it, because no + /// count holds it. It used to be one question per ref, and a kinisi_ros + /// clone with 36 branches and 18 refs to compare each with spent 20 s of + /// its `dl --ls` row in 146 `rev-list` spawns. + /// + /// `--ignore-missing` because a ref here need not exist: `origin/` + /// for a branch never pushed, or an upstream deleted on the remote. git then + /// drops that range alone and answers about the rest, where one question per + /// ref refused that one ref. Each name is a full `refs/remotes/…` ref, so + /// none can be read as an option. + /// /// `--no-merges` because a merge's patch id is not defined. Merges are /// [`Git::merges_with_nothing_of_their_own`]'s question. `--binary` so a /// binary change is compared by its content and not by "Binary files @@ -827,15 +846,26 @@ impl<'r> Git<'r> { &self, clone: &Path, tip: &str, - other: &RemoteRef, + others: &[RemoteRef], ) -> GitAnswer> { - let range = format!("{tip}...{}", other.as_str()); + if others.is_empty() { + return GitAnswer::Said(Vec::new()); + } + let ranges: Vec = others + .iter() + .map(|other| format!("{tip}...{}", other.as_str())) + .collect(); let copies_on = |side: &str| { - self.about( - clone, - &["rev-list", "--cherry-mark", side, "--no-merges", &range], - ) - .map(|stdout| cherry_marked_in(&stdout)) + let mut args = vec![ + "rev-list", + "--ignore-missing", + "--cherry-mark", + side, + "--no-merges", + ]; + args.extend(ranges.iter().map(String::as_str)); + self.about(clone, &args) + .map(|stdout| cherry_marked_in(&stdout)) }; let left = match copies_on("--left-only") { GitAnswer::Said(left) if left.is_empty() => return GitAnswer::Said(left), diff --git a/rust/devlaunch-core/src/clients/git/tests.rs b/rust/devlaunch-core/src/clients/git/tests.rs index f890186f..6e089d4d 100644 --- a/rust/devlaunch-core/src/clients/git/tests.rs +++ b/rust/devlaunch-core/src/clients/git/tests.rs @@ -1366,11 +1366,21 @@ fn a_patch_match(root: &Path, replayed: Response, copy_tree: Response) -> Script let patch = "\ndiff --git a/f b/f\n--- a/f\n+++ b/f\n@@ -1 +1 @@\n-a\n+b\n"; ScriptedRunner::new() .with_script( - pinned(&["rev-list", "--cherry-mark", "--left-only"]), + pinned(&[ + "rev-list", + "--ignore-missing", + "--cherry-mark", + "--left-only", + ]), Response::stdout(format!("={LOCAL}\n")), ) .with_script( - pinned(&["rev-list", "--cherry-mark", "--right-only"]), + pinned(&[ + "rev-list", + "--ignore-missing", + "--cherry-mark", + "--right-only", + ]), Response::stdout(format!("={COPY}\n")), ) .with_script( @@ -1399,7 +1409,7 @@ fn a_patch_match_is_a_copy_only_when_git_replays_it_as_the_copy() { .patches_already_on( dir.path(), "refs/heads/feature", - &RemoteRef::of("origin", "feature") + &[RemoteRef::of("origin", "feature")] ) .said(), Some(vec![LOCAL.to_owned()]) @@ -1472,7 +1482,7 @@ fn a_patch_match_is_a_copy_only_when_git_replays_it_as_the_copy() { .patches_already_on( dir.path(), "refs/heads/feature", - &RemoteRef::of("origin", "feature") + &[RemoteRef::of("origin", "feature")] ) .said(), Some(vec![]), diff --git a/rust/devlaunch-core/src/domain/workspace_state.rs b/rust/devlaunch-core/src/domain/workspace_state.rs index 0c7eed04..fbc67428 100644 --- a/rust/devlaunch-core/src/domain/workspace_state.rs +++ b/rust/devlaunch-core/src/domain/workspace_state.rs @@ -860,8 +860,13 @@ pub(crate) fn cleared_unpushed(git: &Git<'_>, clone: &Path, bare: BareCache<'_>) /// of every workspace, so a copy on a remote branch nothing local tracks is not /// found, and its commit stays counted, which is the safe side. /// -/// A branch stops being compared once every one of its unpushed commits is -/// accounted for, so the usual clone pays one comparison per unpushed branch. +/// **A branch is compared with all of its remote refs in one question**, not +/// one per ref, so a clone pays one comparison per branch that still holds a +/// commit nothing has accounted for, however many refs it is compared with. A +/// branch whose unpushed commits are all accounted for is not compared at all. +/// One question per ref made this quadratic, branches times every local +/// branch's upstream: on a kinisi_ros clone of 36 branches it was 146 `git` +/// spawns and 20 s of that clone's `dl --ls` row. /// /// **Every failure here counts less as copied, never more.** A question git /// refuses adds nothing to the answer, so the commits it was about stay @@ -885,13 +890,12 @@ fn already_on_a_remote(git: &Git<'_>, clone: &Path, local_tags: &[String]) -> Ve let Some(unpushed) = git.unpushed_commits_from(clone, &branch.name).said() else { continue; }; - for other in remote_refs_to_compare(branch, &upstreams) { - if unpushed.lines().all(|line| is_among(line, &copied)) { - break; - } - if let Some(hashes) = git.patches_already_on(clone, &branch.name, &other).said() { - copied.extend(hashes); - } + if unpushed.lines().all(|line| is_among(line, &copied)) { + continue; + } + let others = remote_refs_to_compare(branch, &upstreams); + if let Some(hashes) = git.patches_already_on(clone, &branch.name, &others).said() { + copied.extend(hashes); } } copied diff --git a/rust/devlaunch-core/src/domain/workspace_state/tests.rs b/rust/devlaunch-core/src/domain/workspace_state/tests.rs index 3d99ac60..ceb77d57 100644 --- a/rust/devlaunch-core/src/domain/workspace_state/tests.rs +++ b/rust/devlaunch-core/src/domain/workspace_state/tests.rs @@ -719,6 +719,8 @@ fn a_branch_the_remote_rebased_holds_nothing_unsaved_in_colour_too() { fn a_backup_made_before_the_remote_rebase_holds_nothing_unsaved() { // The shape that kept most of the old commits on a real host: a backup branch // with no upstream, whose copies are on *another* local branch's upstream. + // `origin/backup/before-rebase` is not there, so one of the ranges the copy + // rule asks about names no ref, and git has to drop that range alone. let fixture = Fixture::new(); let clone = fixture.clone(); git(&clone, &["branch", "backup/before-rebase"]); @@ -2423,6 +2425,65 @@ fn a_copy_that_git_will_not_look_for_leaves_every_commit_counted() { ); } +#[test] +fn each_branch_is_compared_with_all_its_remote_refs_in_one_question() { + // Every branch is compared with every branch's upstream, and one question + // per branch and per ref was quadratic in the branches: a kinisi_ros clone + // of 36 branches spent 20 s of `dl --ls` in 146 `rev-list --cherry-mark` + // spawns. One question per branch is linear in the branches. + let branches = 12; + let listing: String = (0..branches) + .map(|n| format!("refs/heads/b{n}\0refs/remotes/origin/b{n}\n")) + .collect(); + let pinned = |verb: &[&str]| -> Vec { + ["git", "--git-dir=/ws/.git", "--work-tree=/ws"] + .iter() + .chain(verb) + .map(|arg| (*arg).to_owned()) + .collect() + }; + let fake = ScriptedRunner::new() + .with_script(pinned(&["for-each-ref"]), Response::stdout(listing)) + .with_script( + pinned(&["log", "--oneline"]), + Response::stdout("1111111 unpushed\n"), + ) + .with_script(pinned(&["rev-list"]), Response::stdout("+1111111\n")); + + assert_eq!( + already_on_a_remote(&Git::new(&fake), Path::new("/ws"), &[]), + Vec::::new() + ); + let questions: Vec> = fake + .argvs() + .into_iter() + .filter(|argv| argv.get(3).map(String::as_str) == Some("rev-list")) + .collect(); + assert_eq!(questions.len(), branches, "one question per branch"); + let first = &questions[0]; + assert_eq!( + first[3..8], + [ + "rev-list", + "--ignore-missing", + "--cherry-mark", + "--left-only", + "--no-merges" + ], + "a ref that is not there drops its own range and not the question" + ); + assert_eq!( + first.len() - 8, + branches + 1, + "every upstream and `origin/HEAD`, each one range" + ); + assert!( + first[8..] + .iter() + .all(|range| range.starts_with("refs/heads/b0...refs/remotes/origin/")) + ); +} + #[test] fn a_commit_on_a_branch_that_is_not_checked_out_is_unsaved() { // The whole of #471, and it is a live data-loss path in shipped code: commit