Skip to content

Commit 948c09c

Browse files
committed
fix(vcs): Make git_repo_base_ref return ref
1 parent 1c7fcdc commit 948c09c

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
### Fixes
1111

12-
- Fix autofilled git base metadata (`--base-ref`, `--base-sha`) when using the `build upload` subcommand in git repos. Previously this worked only in the contexts of GitHub workflows ([#2897](https://github.com/getsentry/sentry-cli/pull/2897)).
12+
- Fix autofilled git base metadata (`--base-ref`, `--base-sha`) when using the `build upload` subcommand in git repos. Previously this worked only in the contexts of GitHub workflows ([#2897](https://github.com/getsentry/sentry-cli/pull/2897), [#2898](https://github.com/getsentry/sentry-cli/pull/2898)).
1313

1414
## 2.57.0
1515

src/utils/vcs.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -272,30 +272,25 @@ pub fn git_repo_head_ref(repo: &git2::Repository) -> Result<String> {
272272
}
273273

274274
pub fn git_repo_base_ref(repo: &git2::Repository, remote_name: &str) -> Result<String> {
275-
// Get the current HEAD commit
276-
let head_commit = repo.head()?.peel_to_commit()?;
277-
278-
// Try to find the remote tracking branch
279275
let remote_branch_name = format!("refs/remotes/{remote_name}/HEAD");
280276
let remote_ref = repo.find_reference(&remote_branch_name).map_err(|e| {
281277
anyhow::anyhow!("Could not find remote tracking branch for {remote_name}: {e}")
282278
})?;
283279

284-
find_merge_base_ref(repo, &head_commit, &remote_ref)
285-
}
286-
287-
fn find_merge_base_ref(
288-
repo: &git2::Repository,
289-
head_commit: &git2::Commit,
290-
remote_ref: &git2::Reference,
291-
) -> Result<String> {
292-
let remote_commit = remote_ref.peel_to_commit()?;
293-
let merge_base_oid = repo.merge_base(head_commit.id(), remote_commit.id())?;
280+
let name = remote_ref
281+
.resolve()?
282+
.shorthand()
283+
.ok_or(anyhow::anyhow!("Remote branch name is not valid UTF-8"))?
284+
.to_owned();
294285

295-
// Return the merge-base commit SHA as the base reference
296-
let merge_base_sha = merge_base_oid.to_string();
297-
debug!("Found merge-base commit as base reference: {merge_base_sha}");
298-
Ok(merge_base_sha)
286+
let expected_prefix = format!("{remote_name}/");
287+
if let Some(branch_name) = name.strip_prefix(&expected_prefix) {
288+
Ok(branch_name.to_owned())
289+
} else {
290+
Err(anyhow::anyhow!(
291+
"Remote branch name '{name}' does not start with expected prefix '{expected_prefix}'"
292+
))
293+
}
299294
}
300295

301296
/// Like git_repo_base_repo_name but preserves the original case of the repository name.
@@ -594,6 +589,13 @@ pub fn find_base_sha(remote_name: &str) -> Result<Option<String>> {
594589
.map(|oid| oid.to_string())
595590
.ok()
596591
.inspect(|sha| debug!("Found merge-base commit as base reference: {sha}")))
592+
.map_err(|e| anyhow::anyhow!("Could default branch for {remote_name}: {e}"))?;
593+
594+
let remote_commit = remote_ref.peel_to_commit()?;
595+
let merge_base_oid = repo.merge_base(head_commit.id(), remote_commit.id())?;
596+
let merge_base_sha = merge_base_oid.to_string();
597+
debug!("Found merge-base commit as base reference: {merge_base_sha}");
598+
Ok(Some(merge_base_sha))
597599
}
598600

599601
/// Extracts the PR head SHA from GitHub Actions event payload JSON.
@@ -1684,6 +1686,7 @@ mod tests {
16841686
fn test_extract_pr_base_sha_from_event_invalid() {
16851687
extract_pr_base_sha_from_event("invalid json {");
16861688
}
1689+
16871690
#[test]
16881691
#[serial(github_env)]
16891692
#[should_panic]

0 commit comments

Comments
 (0)