Skip to content

Commit 3032862

Browse files
committed
fix(vcs): Handle non-github workflow in find_base_sha
`find_head_sha` supported both Github workflow contexts and 'raw git' contexts however the matching function `find_base_sha` did not handle raw git contexts. Thi updates `find_base_sha` to return the equivalent of: `git merge-base HEAD origin/HEAD`. For: ``` o---o---o---origin/main / ---1---o---o---o---foo ``` We return the SHA of 1.
1 parent db7606a commit 3032862

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

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

14+
### Fixes
15+
16+
- Fix auto filled git metadata when using the `build upload` subcommand in non-github workflow contexts ([#2897](https://github.com/getsentry/sentry-cli/pull/2897))
17+
1418
## 2.57.0
1519

1620
### New Features

src/utils/vcs.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fmt;
44
use std::path::PathBuf;
55

6-
use anyhow::{bail, format_err, Context as _, Error, Result};
6+
use anyhow::{bail, format_err, Error, Result};
77
use chrono::{DateTime, FixedOffset, TimeZone as _};
88
use git2::{Commit, Repository, Time};
99
use if_chain::if_chain;
@@ -579,13 +579,18 @@ pub fn find_base_sha(remote_name: &str) -> Result<Option<String>> {
579579
return Ok(Some(pr_base_sha));
580580
}
581581

582+
<<<<<<< HEAD
582583
let repo = git2::Repository::open_from_env().context("Could not open repository")?;
584+
=======
585+
let repo = git2::Repository::open_from_env()?;
586+
>>>>>>> 4f1cba4a (fix(vcs): Handle non-github workflow in find_base_sha)
583587

584588
let head_commit = repo.head()?.peel_to_commit()?;
585589

586590
let remote_branch_name = format!("refs/remotes/{remote_name}/HEAD");
587591
let remote_ref = repo
588592
.find_reference(&remote_branch_name)
593+
<<<<<<< HEAD
589594
.with_context(|| "Could not find default branch for {remote_name}")?;
590595

591596
Ok(remote_ref
@@ -594,6 +599,15 @@ pub fn find_base_sha(remote_name: &str) -> Result<Option<String>> {
594599
.map(|oid| oid.to_string())
595600
.ok()
596601
.inspect(|sha| debug!("Found merge-base commit as base reference: {sha}")))
602+
=======
603+
.map_err(|e| anyhow::anyhow!("Could default branch for {remote_name}: {e}"))?;
604+
605+
let remote_commit = remote_ref.peel_to_commit()?;
606+
let merge_base_oid = repo.merge_base(head_commit.id(), remote_commit.id())?;
607+
let merge_base_sha = merge_base_oid.to_string();
608+
debug!("Found merge-base commit as base reference: {merge_base_sha}");
609+
Ok(Some(merge_base_sha))
610+
>>>>>>> 4f1cba4a (fix(vcs): Handle non-github workflow in find_base_sha)
597611
}
598612

599613
/// Extracts the PR head SHA from GitHub Actions event payload JSON.

0 commit comments

Comments
 (0)