Skip to content

Commit cd5c20a

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

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-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: 13 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-
}
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();
286285

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())?;
294-
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.

0 commit comments

Comments
 (0)