Skip to content

feat(buffetch): add merge_base option to buf breaking --against#4551

Open
jasondamour wants to merge 8 commits into
bufbuild:mainfrom
jasondamour:worktree-feat-merge-base
Open

feat(buffetch): add merge_base option to buf breaking --against#4551
jasondamour wants to merge 8 commits into
bufbuild:mainfrom
jasondamour:worktree-feat-merge-base

Conversation

@jasondamour
Copy link
Copy Markdown

Closes #4495

Summary

Adds a merge_base=<ref> option to the --against flag of buf breaking, so users can write:

buf breaking --against ".git#merge_base=main"

instead of the shell workaround:

buf breaking --against ".git#ref=$(git merge-base HEAD main)"

This is especially useful in pre-commit hook tools (e.g. prek) that don't expose the merge-base commit as an environment variable.

How it works

When merge_base=<ref> is specified:

  1. The option is parsed through the existing buffetch ref-parsing pipeline and stored on the GitRef interface as GitMergeBase() string.
  2. At clone time in getGitBucket(), buf runs git merge-base HEAD <ref> against the local repository to resolve the common ancestor commit hash.
  3. That commit hash is used as the checkout target (equivalent to ref=<hash>), so the comparison is against the exact point where the current branch diverged from <ref>.

merge_base is only supported for local git references (e.g. .git#merge_base=main). For remote repos, the existing shell workaround ref=$(git merge-base HEAD origin/main) remains the approach.

Constraints

  • Cannot be combined with branch, commit, tag, or ref (returns a clear error)
  • Defaults to depth=50 (same as ref=)
  • Only valid for local git schemes

Changes

File Change
private/pkg/git/git.go Add GetMergeBase() helper
private/buf/buffetch/internal/errors.go Add 2 error constructors
private/buf/buffetch/internal/internal.go Add GitMergeBase to RawRef and GitRef interface
private/buf/buffetch/internal/git_ref.go Add gitMergeBase field and accessor
private/buf/buffetch/internal/ref_parser.go Parse, validate, and thread merge_base through
private/buf/buffetch/internal/reader.go Resolve merge-base commit before cloning
private/buf/buffetch/internal/ref_parser_test.go Unit tests for parsing and validation

Test plan

  • Unit tests added in ref_parser_test.go covering valid parsing and all invalid combinations
  • buf breaking . --against ".git#merge_base=main" produces identical output to buf breaking . --against ".git#ref=$(git merge-base HEAD main)"
  • All existing buffetch and git package tests pass

🤖 Generated with Claude Code

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.

Add a merge-base option to the buf-breaking check

1 participant