Skip to content

Add subcommand branch show-base#118

Open
guettli wants to merge 3 commits into
git-pkgs:mainfrom
guettli:add-show-base-branch
Open

Add subcommand branch show-base#118
guettli wants to merge 3 commits into
git-pkgs:mainfrom
guettli:add-show-base-branch

Conversation

@guettli

@guettli guettli commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Description

Resolves #115.

Introduces a local caching layer for the resolved base (target) branch of local feature branches, avoiding redundant Forge API network calls.

Changes

  1. Caching Logic: Added the git package with GetOrFetchBaseBranch to check for and set branch.<branch>.forge-merge-base in .git/config.
  2. Subcommand: Added forge branch show-base [branch] (with -r/--refresh flag to bypass/update the cache).
  3. Automatic Cache Updates:
    • forge pr checkout: Caches base branch for the checked-out branch.
    • forge pr create: Caches base branch for the source branch.
    • forge pr view: Caches base branch if the source branch exists locally.
  4. Tests: Added unit tests in git_test.go and CLI command validation.

Introduces a local caching layer for the resolved
base (target) branch of local feature branches,
avoiding redundant Forge API network calls.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for picking this up. The design follows #115 closely and the user-facing surface is small. A few changes to request before merge:

1. Move git/ under internal/. Every other helper package here (internal/resolve, internal/output, internal/config) is internal/. A top-level git/ commits GetOrFetchBaseBranch as a public API. Unless that's intentional, please move it to internal/git/.

2. Deduplicate the cache-write sites. The new git package owns the branch.<name>.forge-merge-base key format, but internal/cli/pr.go reformats it inline in three places (prViewCmd, prCreateCmd, the checkout block). Please add an exported helper in the git package (e.g. SetBaseBranch(ctx, branch, base)) and route all three sites through it.

3. pr view can mis-cache against an unrelated local branch. In prViewCmd the cache write fires whenever a local branch happens to share a name with pr.Head.Ref, even for fork PRs where the local branch tracks something else. Please skip the cache write when pr.Head.Fork != nil, or check branch.<name>.remote/merge to confirm the local branch actually corresponds to the PR head.

4. Head-matching loop in GetOrFetchBaseBranch. ListPROpts.Head is already passed as a filter, so the per-PR pr.Head.Ref == branch || strings.HasSuffix(pr.Head.Ref, ":"+branch) check is either redundant or compensating for a backend that ignores the filter. If the latter, the suffix branch can match an unrelated fork PR with the same branch name. Please either drop the client-side check or add a comment explaining which forge needs it.

5. Test uses os.Chdir. git/git_test.go mutates process-wide cwd, which precludes t.Parallel() and is fragile if other tests in the package ever run alongside it. Prefer running git with -C tmpDir (you'd need to thread a working dir into the production function), or document the constraint.

Minor:

  • git/git.go has no package doc comment.
  • Mock List ignores opts — a small opts.Head == "feature-xyz" assertion would catch a future refactor that drops the API-side filter.
  • -r collides conceptually with "recursive" elsewhere; --refresh only might be clearer, but not blocking.

- Move git package to internal/git
- Deduplicate cache-write sites using git.SetBaseBranch helper
- Skip cache-write in pr view if PR is from a fork
- Refactor git_test to avoid os.Chdir by passing/setting directory context
- Clean up client-side Head filtering in GetOrFetchBaseBranch with comment
@guettli

guettli commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

I have addressed the PR review feedback:

  1. Moved the git package under internal/git.
  2. Deduplicated cache-write sites using a new git.SetBaseBranch helper.
  3. Skipped the cache-write in pr view for fork PRs.
  4. Cleaned up the head-matching logic and documented the client-side filtering constraint.
  5. Refactored the unit tests to set target directories directly on commands, avoiding the use of os.Chdir.
  6. Removed the -r shorthand from the --refresh flag as it was conceptually colliding with recursive flags.

@guettli guettli requested a review from andrew June 9, 2026 13:10
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.

Feature Request: Local caching for base/target branch resolution

2 participants