diff options
| author | Max Audron <audron@cocaine.farm> | 2025-04-08 16:48:57 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2025-04-08 16:48:57 +0200 |
| commit | 9be68804bae9833a4749377e05bfaebf7d64f2b1 (patch) | |
| tree | 5231887860d2cded23a931ea8d76a6c07d54c72c /src/repo/git | |
| parent | delete files removed from index during checkout (diff) | |
only checkout repo if clean
Diffstat (limited to 'src/repo/git')
| -rw-r--r-- | src/repo/git/mod.rs | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/repo/git/mod.rs b/src/repo/git/mod.rs index b88a13f..24b1e1a 100644 --- a/src/repo/git/mod.rs +++ b/src/repo/git/mod.rs @@ -2,12 +2,12 @@ use super::{LocalRepoState, Repo, RepoError}; use anyhow::Context; use gix::{ - bstr::BString, + bstr::{BString, ByteSlice}, refs::{ transaction::{LogChange, PreviousValue, RefEdit}, FullName, }, - remote, Id, ObjectId, Remote, + remote, Id, ObjectId, Reference, Remote, }; use tracing::debug; @@ -33,6 +33,34 @@ impl Repo { return Ok(LocalRepoState::UnbornHead); } + let head = self.repo()?.head().unwrap(); + let branch = head.referent_name().unwrap(); + let default_branch = self.default_branch()?; + + if !branch.as_bstr().contains_str(default_branch) { + return Ok(LocalRepoState::NonDefaultBranch); + } + + let default_ref = self.default_remote_ref()?.into_fully_peeled_id().unwrap(); + + let head_ref = repo + .head_ref() + .map_err(|_| RepoError::NoHead)? + .ok_or(RepoError::NoHead)? + .into_fully_peeled_id() + .unwrap(); + + let unpushed_commits = head_ref + .ancestors() + .with_boundary([default_ref]) + .all() + .unwrap() + .count(); + + if default_ref != head_ref && unpushed_commits > 0 { + return Ok(LocalRepoState::UnpushedCommits(unpushed_commits)); + } + Ok(LocalRepoState::Clean) } } @@ -45,7 +73,7 @@ impl Repo { .context("fetch: failed to find default remote")?) } - pub fn default_branch(&self) -> Result<BString, RepoError> { + pub fn default_remote_ref(&self) -> Result<Reference, RepoError> { let repo = self.repo()?; let remote = self.default_remote()?; let remote_name = remote.name().context("remote does not have name")?; @@ -56,6 +84,14 @@ impl Repo { debug!("got ref to origin: {:?}", origin_ref); + Ok(origin_ref) + } + + pub fn default_branch(&self) -> Result<BString, RepoError> { + let remote = self.default_remote()?; + let remote_name = remote.name().context("remote does not have name")?; + let origin_ref = self.default_remote_ref()?; + if let Some(origin_ref) = origin_ref.target().try_name() { let shortened = origin_ref.shorten().to_string(); |
