Compare commits
2 Commits
ee4ff6c752
...
afb1bbeb71
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afb1bbeb71 | ||
|
|
a4dc507b66 |
@ -15,10 +15,8 @@ impl GitDomain {
|
||||
};
|
||||
|
||||
let mut branches = Vec::with_capacity(16);
|
||||
let head_name = self.repo.head().ok().and_then(|r| {
|
||||
r.name()
|
||||
.map(|name| name.strip_prefix("refs/heads/").unwrap_or(name).to_string())
|
||||
});
|
||||
// Keep head_name as full ref for comparison with branch names
|
||||
let head_name = self.repo.head().ok().and_then(|r| r.name().map(String::from));
|
||||
|
||||
for branch_result in self
|
||||
.repo()
|
||||
@ -32,6 +30,7 @@ impl GitDomain {
|
||||
};
|
||||
let name = name.to_string();
|
||||
let oid = CommitOid::from_git2(target);
|
||||
// Compare full ref names (e.g., "refs/heads/main" == "refs/heads/main")
|
||||
let is_head = head_name.as_ref().map_or(false, |h| h == &name);
|
||||
let is_current = branch.is_head();
|
||||
|
||||
@ -78,24 +77,28 @@ impl GitDomain {
|
||||
}
|
||||
|
||||
pub fn branch_get(&self, name: &str) -> GitResult<BranchInfo> {
|
||||
// Determine full ref name and branch type
|
||||
let full_name = if name.starts_with("refs/heads/") {
|
||||
name.to_string()
|
||||
} else if name.starts_with("refs/remotes/") {
|
||||
name.to_string()
|
||||
} else if name.contains('/') {
|
||||
// e.g. "origin/main" → remote branch
|
||||
format!("refs/remotes/{}", name)
|
||||
// Determine candidates: try full refs as-is, then construct refs/heads/ and refs/remotes/
|
||||
let candidates: Vec<String> = if name.starts_with("refs/") {
|
||||
vec![name.to_string()]
|
||||
} else {
|
||||
format!("refs/heads/{}", name)
|
||||
vec![
|
||||
format!("refs/heads/{}", name),
|
||||
format!("refs/remotes/{}", name),
|
||||
]
|
||||
};
|
||||
|
||||
let branch = self
|
||||
.repo()
|
||||
.find_branch(&full_name, git2::BranchType::Local)
|
||||
.or_else(|_| self.repo.find_branch(&full_name, git2::BranchType::Remote))
|
||||
.map_err(|_e| GitError::RefNotFound(name.to_string()))?;
|
||||
// Try local branch first, then remote
|
||||
let branch = candidates
|
||||
.iter()
|
||||
.find_map(|full_name| {
|
||||
self.repo
|
||||
.find_branch(full_name, git2::BranchType::Local)
|
||||
.ok()
|
||||
.or_else(|| self.repo.find_branch(full_name, git2::BranchType::Remote).ok())
|
||||
})
|
||||
.ok_or_else(|| GitError::RefNotFound(name.to_string()))?;
|
||||
|
||||
let full_name = branch.name().ok().flatten().unwrap_or_default().to_string();
|
||||
let target = branch
|
||||
.get()
|
||||
.target()
|
||||
@ -125,19 +128,25 @@ impl GitDomain {
|
||||
}
|
||||
|
||||
pub fn branch_exists(&self, name: &str) -> bool {
|
||||
let full_name = if name.starts_with("refs/heads/") || name.starts_with("refs/remotes/") {
|
||||
name.to_string()
|
||||
} else if name.contains('/') {
|
||||
format!("refs/remotes/{}", name)
|
||||
// Same candidate logic as branch_get
|
||||
let candidates: Vec<String> = if name.starts_with("refs/") {
|
||||
vec![name.to_string()]
|
||||
} else {
|
||||
format!("refs/heads/{}", name)
|
||||
vec![
|
||||
format!("refs/heads/{}", name),
|
||||
format!("refs/remotes/{}", name),
|
||||
]
|
||||
};
|
||||
|
||||
self.repo.find_branch(&full_name, BranchType::Local).is_ok()
|
||||
|| self
|
||||
.repo()
|
||||
.find_branch(&full_name, BranchType::Remote)
|
||||
candidates.iter().any(|full_name| {
|
||||
self.repo
|
||||
.find_branch(full_name, BranchType::Local)
|
||||
.is_ok()
|
||||
|| self
|
||||
.repo()
|
||||
.find_branch(full_name, BranchType::Remote)
|
||||
.is_ok()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn branch_is_head(&self, name: &str) -> GitResult<bool> {
|
||||
|
||||
@ -89,9 +89,24 @@ impl HookMetaDataSync {
|
||||
tagger_email: String::new(),
|
||||
});
|
||||
} else if is_branch && !is_remote {
|
||||
let upstream = reference
|
||||
.shorthand()
|
||||
.map(|short| format!("refs/remotes/{{}}/{}", short));
|
||||
// Try to get upstream branch name from the reference's upstream target
|
||||
let upstream: Option<String> = if reference.target().is_some() {
|
||||
if let Ok(branch) = self.domain.repo().find_branch(&name, git2::BranchType::Local) {
|
||||
if let Ok(upstream_ref) = branch.upstream() {
|
||||
if let Some(upstream_name) = upstream_ref.name().ok().flatten() {
|
||||
Some(upstream_name.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
branches.push(BranchTip {
|
||||
name,
|
||||
|
||||
@ -1031,6 +1031,15 @@ impl AppService {
|
||||
) -> Result<Vec<CommitReflogEntryResponse>, AppError> {
|
||||
let repo = self.utils_find_repo(namespace, repo_name, ctx).await?;
|
||||
|
||||
// If refname is None and HEAD is detached, fall back to default_branch
|
||||
let refname = refname.or_else(|| {
|
||||
if repo.default_branch.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(repo.default_branch.clone())
|
||||
}
|
||||
});
|
||||
|
||||
let entries = git_spawn!(repo, domain -> {
|
||||
domain.reflog_entries(refname.as_deref())
|
||||
})?;
|
||||
@ -1064,7 +1073,13 @@ impl AppService {
|
||||
}
|
||||
|
||||
let repo = self.utils_find_repo(namespace, repo_name, ctx).await?;
|
||||
let rev_clone = query.rev.clone();
|
||||
let rev_clone = query.rev.clone().or_else(|| {
|
||||
if repo.default_branch.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(format!("refs/heads/{}", repo.default_branch))
|
||||
}
|
||||
});
|
||||
let limit = query.limit.unwrap_or(0);
|
||||
|
||||
let graph = git_spawn!(repo, domain -> {
|
||||
@ -1112,7 +1127,13 @@ impl AppService {
|
||||
}
|
||||
|
||||
let repo = self.utils_find_repo(namespace, repo_name, ctx).await?;
|
||||
let rev_clone = query.rev.clone();
|
||||
let rev_clone = query.rev.clone().or_else(|| {
|
||||
if repo.default_branch.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(format!("refs/heads/{}", repo.default_branch))
|
||||
}
|
||||
});
|
||||
let limit = query.limit.unwrap_or(0);
|
||||
|
||||
let (graph, refs_grouped) = git_spawn!(repo, domain -> {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user