Skip to content

feat: support commit hash in --base flag#11

Merged
amaya382 merged 1 commit into
mainfrom
feat/base-commit-hash
Feb 16, 2026
Merged

feat: support commit hash in --base flag#11
amaya382 merged 1 commit into
mainfrom
feat/base-commit-hash

Conversation

@amaya382

Copy link
Copy Markdown
Owner

Summary

  • Allow bt add -b --base to accept commit hashes (full or short SHA) in addition to branch names
  • Add IsCommitHash method to git executor using git rev-parse --verify
  • When --base value doesn't match any local/remote branch, fall back to commit hash validation before erroring

Changes

  • internal/git/branch.go: Add IsCommitHash() method
  • cmd/bt/add.go: Add commit hash fallback in --base resolution, update help text
  • README.md: Add --base with commit example, update command reference
  • e2e/journey_remote_test.go: Add TestAddNewBranchWithCommitBase (full hash, short hash, invalid hash)
  • e2e/README.md: Document new test case

Test plan

  • task build passes
  • task test passes (unit + E2E)
  • golangci-lint run ./... passes
  • Manual test: bt add -b feat/test --base <commit-hash> creates worktree correctly
  • Manual test: bt add -b feat/test --base <invalid-hash> shows error

@amaya382 amaya382 self-assigned this Feb 16, 2026
The --base flag previously only accepted branch names (local or remote).
Now it also accepts commit hashes (full or short SHA), falling back to
git rev-parse verification when branch resolution fails.
@amaya382 amaya382 force-pushed the feat/base-commit-hash branch from ea8bc7d to cd61d96 Compare February 16, 2026 12:26
@amaya382 amaya382 marked this pull request as ready for review February 16, 2026 12:30
Copilot AI review requested due to automatic review settings February 16, 2026 12:30
@amaya382 amaya382 merged commit aba2e59 into main Feb 16, 2026
7 checks passed
@amaya382 amaya382 deleted the feat/base-commit-hash branch February 16, 2026 12:30

Copilot AI 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.

Pull request overview

This PR extends bt add -b --base to accept commit hashes (full or short SHA) in addition to branch names, updating the CLI help/docs and adding E2E coverage for the new behavior.

Changes:

  • Add git executor support for validating whether a ref resolves to a commit.
  • Update bt add base resolution to fall back to commit-hash validation when a branch isn’t found.
  • Add E2E coverage + documentation updates for --base <commit> usage.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/git/branch.go Adds a helper to validate that an input resolves to a commit object via git rev-parse.
cmd/bt/add.go Adds commit-hash fallback for --base and updates CLI help text.
e2e/journey_remote_test.go Adds E2E coverage for full hash, short hash, and invalid hash base cases.
e2e/README.md Documents the new E2E test case.
README.md Updates the command reference to mention --base supports branch/commit.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/git/branch.go
Comment on lines +172 to +173
// IsCommitHash checks if the given string resolves to a valid commit object
func (e *Executor) IsCommitHash(ref string) bool {

Copilot AI Feb 16, 2026

Copy link

Choose a reason for hiding this comment

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

IsCommitHash is named like it validates a hex SHA, but git rev-parse --verify <ref>^{commit} will also succeed for non-hash commit-ish values (e.g. HEAD, tags, or other refs). This makes the API misleading and can unintentionally broaden what --base accepts. Consider either renaming this to something like IsCommitRef/IsCommitish, or tightening the check (e.g., first require the input to match a hex SHA pattern of an acceptable length) so the behavior matches the name/help text.

Suggested change
// IsCommitHash checks if the given string resolves to a valid commit object
func (e *Executor) IsCommitHash(ref string) bool {
// IsCommitHash checks if the given string looks like a hex SHA and resolves to a valid commit object
func (e *Executor) IsCommitHash(ref string) bool {
ref = strings.TrimSpace(ref)
// Require a plausible abbreviated or full SHA-1 (Git allows abbreviations, typically >= 4 chars)
if len(ref) < 4 || len(ref) > 40 {
return false
}
for _, c := range ref {
if !((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F')) {
return false
}
}

Copilot uses AI. Check for mistakes.
Comment thread cmd/bt/add.go
}
baseDisplayInfo = shortHash + " (commit)"
} else {
return fmt.Errorf("base branch '%s' not found locally or on any remote", addBaseBranch)

Copilot AI Feb 16, 2026

Copy link

Choose a reason for hiding this comment

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

Now that --base supports commit hashes, this error message is misleading for invalid hashes because it only mentions branches/remotes. Consider updating it to indicate the value wasn’t found as either a branch or a commit, so users understand why a hash-like value failed.

Suggested change
return fmt.Errorf("base branch '%s' not found locally or on any remote", addBaseBranch)
return fmt.Errorf("base '%s' not found as a local/remote branch or commit", addBaseBranch)

Copilot uses AI. Check for mistakes.
Comment thread README.md
| Command | Description |
|---------|-------------|
| `bt add <branch>` | Add worktree (`-b` for new branch, auto-fetches remotes, `--no-fetch` to skip) |
| `bt add <branch>` | Add worktree (`-b` for new branch, `--base` for base branch/commit, auto-fetches remotes) |

Copilot AI Feb 16, 2026

Copy link

Choose a reason for hiding this comment

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

This command reference row previously documented --no-fetch but now omits it. If --no-fetch is still supported, consider keeping it in the short description while adding the new --base mention to avoid a docs regression.

Suggested change
| `bt add <branch>` | Add worktree (`-b` for new branch, `--base` for base branch/commit, auto-fetches remotes) |
| `bt add <branch>` | Add worktree (`-b` for new branch, `--base` for base branch/commit, auto-fetches remotes unless \`--no-fetch\`) |

Copilot uses AI. Check for mistakes.
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.

2 participants