Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

### Fixes

- 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)).
- 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)).

## 2.57.0

Expand Down
32 changes: 14 additions & 18 deletions src/utils/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,30 +272,26 @@ pub fn git_repo_head_ref(repo: &git2::Repository) -> Result<String> {
}

pub fn git_repo_base_ref(repo: &git2::Repository, remote_name: &str) -> Result<String> {
// Get the current HEAD commit
let head_commit = repo.head()?.peel_to_commit()?;

// Try to find the remote tracking branch
let remote_branch_name = format!("refs/remotes/{remote_name}/HEAD");
let remote_ref = repo.find_reference(&remote_branch_name).map_err(|e| {
anyhow::anyhow!("Could not find remote tracking branch for {remote_name}: {e}")
})?;

find_merge_base_ref(repo, &head_commit, &remote_ref)
}
let name = remote_ref
.resolve()?
.shorthand()
.ok_or(anyhow::anyhow!("Remote branch name is not valid UTF-8"))?
.to_owned();

fn find_merge_base_ref(
repo: &git2::Repository,
head_commit: &git2::Commit,
remote_ref: &git2::Reference,
) -> Result<String> {
let remote_commit = remote_ref.peel_to_commit()?;
let merge_base_oid = repo.merge_base(head_commit.id(), remote_commit.id())?;

// Return the merge-base commit SHA as the base reference
let merge_base_sha = merge_base_oid.to_string();
debug!("Found merge-base commit as base reference: {merge_base_sha}");
Ok(merge_base_sha)
let expected_prefix = format!("{remote_name}/");
name.strip_prefix(&expected_prefix)
.map(|s| s.to_owned())
.ok_or_else(|| {
anyhow::anyhow!(
"Remote branch name '{name}' does not start with expected prefix \
'{expected_prefix}'"
)
})
}

/// Like git_repo_base_repo_name but preserves the original case of the repository name.
Expand Down