fn find_default_sha(repo: &git2::Repository) -> Result<git2::Oid, Box<dyn Error>> {
    // go through all the remotes, and find which has a HEAD branch
    // then resolve that to the local branch
    let mut head_ref: Option<git2::Reference> = None;
    if let Ok(maybe_refs) = repo.references_glob("refs/remotes/*/HEAD") {
        for maybe_ref in maybe_refs {
            let r = maybe_ref?;
            if !r.is_remote() {
                continue;
            }

            let is_origin = r.name().unwrap().starts_with("refs/remotes/origin");

            // this ensures that we prefer "origin"; otherwise, we just fallback to whatever
            // the last remote with a HEAD is (normally, only one remote --the one used to
            // clone-- has it)
            head_ref = Some(r);
            if is_origin {
                break;
            }
        }
    }

    if let Some(hr) = head_ref {
        let r = hr.resolve()?;

        let name = r.name().unwrap();
        let remote_and_ref = &name["refs/remotes/".len()..];
        let v: Vec<&str> = remote_and_ref.splitn(2, '/').collect();
        assert!(v.len() == 2);
        let branch = v[1];

        // now find the local branch of the same name
        if let Ok(b) = repo.find_branch(branch, git2::BranchType::Local) {
            return Ok(b.get().peel_to_commit()?.id());
        }
    }

    // no HEAD remote ref, or not connected to a local branch, just guess "master" or "main", and
    // if that's not it, throw
    if let Ok(b) = repo.find_branch("master", git2::BranchType::Local) {
        return Ok(b.get().peel_to_commit()?.id());
    } else if let Ok(b) = repo.find_branch("main", git2::BranchType::Local) {
        return Ok(b.get().peel_to_commit()?.id());
    }

    Err(Box::new(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "Couldn't find default branch",
    )))
}
/// Get the default SHA against which comparisons should be made to determine +ahead number.
/// This is usually "master", or the default branch to check out after cloning.
Get the default SHA against which comparisons should be made to determine +ahead number.
This is usually "master", or the default branch to check out after cloning.
Get the default SHA against which comparisons should be made to determine + ahead number . This is usually " master " or the default branch to check out after cloning .
fn find_default_sha(repo: &git2::Repository) -> Result<git2::Oid, Box<dyn Error>> {
    // go through all the remotes, and find which has a HEAD branch
    // then resolve that to the local branch
    let mut head_ref: Option<git2::Reference> = None;
    if let Ok(maybe_refs) = repo.references_glob("refs/remotes/*/HEAD") {
        for maybe_ref in maybe_refs {
            let r = maybe_ref?;
            if !r.is_remote() {
                continue;
            }
            let is_origin = r.name().unwrap().starts_with("refs/remotes/origin");
            // this ensures that we prefer "origin"; otherwise, we just fallback to whatever
            // the last remote with a HEAD is (normally, only one remote --the one used to
            // clone-- has it)
            head_ref = Some(r);
            if is_origin {
                break;
            }
        }
    }
    if let Some(hr) = head_ref {
        let r = hr.resolve()?;
        let name = r.name().unwrap();
        let remote_and_ref = &name["refs/remotes/".len()..];
        let v: Vec<&str> = remote_and_ref.splitn(2, '/').collect();
        assert!(v.len() == 2);
        let branch = v[1];
        // now find the local branch of the same name
        if let Ok(b) = repo.find_branch(branch, git2::BranchType::Local) {
            return Ok(b.get().peel_to_commit()?.id());
        }
    }
    // no HEAD remote ref, or not connected to a local branch, just guess "master" or "main", and
    // if that's not it, throw
    if let Ok(b) = repo.find_branch("master", git2::BranchType::Local) {
        return Ok(b.get().peel_to_commit()?.id());
    } else if let Ok(b) = repo.find_branch("main", git2::BranchType::Local) {
        return Ok(b.get().peel_to_commit()?.id());
    }
    Err(Box::new(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "Couldn't find default branch",
    )))
}
fn find_default_sha ( repo : & git2 :: Repository ) -> Result < git2 :: Oid , Box < dyn Error > > { // go through all the remotes, and find which has a HEAD branch // then resolve that to the local branch let mut head_ref : Option < git2 :: Reference > = None ; if let Ok ( maybe_refs ) = repo . references_glob ( " " ) { for maybe_ref in maybe_refs { let r = maybe_ref ? ; if ! r . is_remote ( ) { continue ; } let is_origin = r . name ( ) . unwrap ( ) . starts_with ( " " ) ; // this ensures that we prefer "origin"; otherwise, we just fallback to whatever // the last remote with a HEAD is (normally, only one remote --the one used to // clone-- has it) head_ref = Some ( r ) ; if is_origin { break ; } } } if let Some ( hr ) = head_ref { let r = hr . resolve ( ) ? ; let name = r . name ( ) . unwrap ( ) ; let remote_and_ref = & name [ " " . len ( ) .. ] ; let v : Vec < & str > = remote_and_ref . splitn ( 2 , '/' ) . collect ( ) ; assert ! ( v len ( ) 2 ) ; let branch = v [ 1 ] ; // now find the local branch of the same name if let Ok ( b ) = repo . find_branch ( branch , git2 :: BranchType :: Local ) { return Ok ( b . get ( ) . peel_to_commit ( ) ? . id ( ) ) ; } } // no HEAD remote ref, or not connected to a local branch, just guess "master" or "main", and // if that's not it, throw if let Ok ( b ) = repo . find_branch ( " " , git2 :: BranchType :: Local ) { return Ok ( b . get ( ) . peel_to_commit ( ) ? . id ( ) ) ; } else if let Ok ( b ) = repo . find_branch ( " " , git2 :: BranchType :: Local ) { return Ok ( b . get ( ) . peel_to_commit ( ) ? . id ( ) ) ; } Err ( Box :: new ( std :: io :: Error :: new ( std :: io :: ErrorKind :: NotFound , " " , ) ) ) }
Get the default SHA against which comparisons should be made to determine +ahead number.
Get the default SHA against which comparisons should be made to determine + ahead number .
repo
&git2::Repository