repo: Fall back from stale remote trunk

82295dab08fe602145826cfcca11b66524457b2e
Validate each default-branch candidate against local branches before
choosing it. This lets status use local main when origin/HEAD names a
deleted remote branch.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent bcb3dd01
crates/repo/src/git.rs +7 -16
417 417
        Ok((!value.is_empty()).then_some(value))
418 418
    }
419 419
420 420
    /// Returns the repository trunk branch among `branches`.
421 421
    fn trunk(&self, branches: &[Branch]) -> Result<Option<Branch>, RepoError> {
422 -
        let default = self.default_branch()?;
423 -
        Ok(default
422 +
        let remote = self.remote_default_branch()?;
423 +
        let configured = self.config("init.defaultBranch")?;
424 +
        Ok(remote
424 425
            .as_deref()
425 -
            .and_then(|name| branches.iter().find(|branch| branch.name == name))
426 -
            .or_else(|| {
427 -
                branches
428 -
                    .iter()
429 -
                    .find(|branch| branch.name == refs::name::str::MASTER)
430 -
            })
426 +
            .into_iter()
427 +
            .chain(configured.as_deref())
428 +
            .chain([refs::name::str::MAIN, refs::name::str::MASTER])
429 +
            .find_map(|name| branches.iter().find(|branch| branch.name == name))
431 430
            .cloned())
432 431
    }
433 432
434 433
    /// Returns the best common ancestor of two revisions.
435 434
    fn merge_base(&self, left: &Rev, right: &Rev) -> Result<Oid, RepoError> {
868 867
        Ok(())
869 868
    }
870 869
}
871 870
872 871
impl Repository {
873 -
    /// Returns the repository default branch name, if one is configured.
874 -
    fn default_branch(&self) -> Result<Option<String>, RepoError> {
875 -
        if let Some(name) = self.remote_default_branch()? {
876 -
            return Ok(Some(name));
877 -
        }
878 -
        self.config("init.defaultBranch")
879 -
    }
880 -
881 872
    /// Returns the default branch reported by `origin/HEAD`, if present.
882 873
    fn remote_default_branch(&self) -> Result<Option<String>, RepoError> {
883 874
        let output = self.run(
884 875
            "symbolic-ref",
885 876
            &["--quiet", "--short", "refs/remotes/origin/HEAD"],
file\n")); +0 -0
file\n"), +0 -0
file\n"), +0 -0
a\n--- a/a\n+++ b/a\n@@ -1 +1 @@\n-old\n+new\n".to_owned(), +0 -0
b\n--- a/b\n+++ b/b\n@@ -0,0 +1 @@\n+line\n".to_owned(), +0 -0