Skip to content

Conversation

runningcode
Copy link
Contributor

@runningcode runningcode commented Aug 28, 2025

Add automatic detection of base reference for build uploads by finding
the merge-base between current HEAD and remote tracking branch.

This works by:

  • Finding the remote tracking branch (origin/HEAD or default_branch)
  • Calculating git merge-base between HEAD and remote branch
  • Returning the branch name pointing to merge-base

This mirrors the existing head_ref auto-detection behavior.

@runningcode runningcode force-pushed the feat/build-upload-auto-base-ref branch from e3c6ea9 to cca503f Compare August 28, 2025 17:18
Add automatic detection of base reference for build uploads by finding
the merge-base between current HEAD and remote tracking branch.

This works by:

- Finding the remote tracking branch (origin/HEAD, main, master, develop)
- Calculating git merge-base between HEAD and remote branch
- Returning the branch name pointing to merge-base or commit SHA

This mirrors the existing head_ref auto-detection behavior.
@runningcode runningcode force-pushed the feat/build-upload-auto-base-ref branch from cca503f to 1a0e73b Compare August 28, 2025 17:23
@runningcode runningcode requested a review from rbro112 August 28, 2025 17:25
@runningcode runningcode marked this pull request as ready for review August 29, 2025 10:15
@runningcode runningcode requested review from szokeasaurusrex and a team as code owners August 29, 2025 10:15
Comment on lines 189 to 190
debug!("Error getting base branch reference: {}", e);
None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we swallow this error without propagating it to the user? Seems like potentially undesired behavior, as if something is broken, the user will not be made aware, unless they have debug logging turned on (which is unlikely in most use cases).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good question. I would expect this to fail if the user isn't using git or has some non default branching strategy.
The question is how do we want to handle this.

  1. Throw an error at the CLI level.
  2. Show some error message after they've uploaded in the UI if the git information is missing.

I don't have a strong preference for 1 or 2, it just how we want the user experience to be. Throwing an error here might be frustrating if trying to get this working initially but once the integration is working throwing an error would be the easiest way to know that a change broke the integration.

Copy link
Member

@szokeasaurusrex szokeasaurusrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a couple questions about the implementation, plus a suggestion for how to simplify the code a bit

cursor[bot]

This comment was marked as outdated.

@runningcode runningcode force-pushed the feat/build-upload-auto-base-ref branch from 357b35f to 1f1d297 Compare August 29, 2025 12:07
cursor[bot]

This comment was marked as outdated.

let mut remote = repo.find_remote(remote_name)?;
remote.connect(git2::Direction::Fetch)?;
let default_branch_buf = remote.default_branch()?;
let default_branch = default_branch_buf.as_str().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChatGPT seems to think not, but the fact that the value returned by the git2 library (which is just Rust bindings for libgit2) is not a string and only performs a fallible conversion to str makes me think that we should perhaps be cautious. There is probably a reason why git2 is implemented this way; perhaps in some cases on some platforms a non-UTF-8 value is possible. It does not really cost anything to handle the error gracefully, without panicking, so let's do that!

let mut remote = repo.find_remote(remote_name)?;
remote.connect(git2::Direction::Fetch)?;
let default_branch_buf = remote.default_branch()?;
let default_branch = default_branch_buf.as_str().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how you can handle the error gracefully

Suggested change
let default_branch = default_branch_buf.as_str().unwrap();
let default_branch = default_branch_buf
.as_str()
.ok_or_else(|| anyhow::anyhow!("Default branch contains invalid UTF-8"))?;

.strip_prefix("refs/heads/")
.unwrap_or(default_branch);
let remote_branch = format!("refs/remotes/{remote_name}/{branch_name}");
repo.find_reference(&remote_branch)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the return type of this closure will now be an anyhow::Error, we need a small refactor here, to convert to the new error type

Suggested change
repo.find_reference(&remote_branch)
Ok(repo.find_reference(&remote_branch)?)

let remote_branch = format!("refs/remotes/{remote_name}/{branch_name}");
repo.find_reference(&remote_branch)
})
.map_err(|e| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, we also need a type annotation here, as the return value of the previous closure can no longer be inferred

Suggested change
.map_err(|e| {
.map_err(|e: Error| {

- Add debug logging when base_ref is found
- Use info level logging when base_ref cannot be detected for CI visibility

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants