Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion script/upstream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ The only remaining conflicts are files with **actual code differences** - files
Options:
--version <version> Target upstream version (e.g., v1.1.49)
--commit <hash> Target upstream commit hash
--base-branch <name> Base branch to merge into (default: main)
--base-branch <name> Base branch to merge into; use HEAD for current branch (default: main)
--dry-run Preview changes without applying them
--no-push Don't push branches to remote
--no-worktrees Don't create reference worktrees
Expand Down Expand Up @@ -305,6 +305,8 @@ Tighten the blast radius with `--review-limit 0` (only `markers-only` and `cosme

By default, upstream merges start from the `main` branch. However, you can use `--base-branch` to start from a different branch. This is useful for:

Passing `--base-branch HEAD` targets the currently checked-out branch without typing its full name.

### Incremental Merges

When working on multiple upstream versions, you can create a chain of merge PRs:
Expand Down
11 changes: 8 additions & 3 deletions script/upstream/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Options:
* --version <version> Target upstream version (e.g., v1.1.49)
* --commit <hash> Target upstream commit hash
* --base-branch <name> Base branch to merge into (default: main)
* --base-branch <name> Base branch to merge into, or HEAD for current branch (default: main)
* --dry-run Preview changes without applying them
* --no-push Don't push branches to remote
* --no-worktrees Don't create reference worktrees for manual resolution
Expand All @@ -25,7 +25,7 @@ import * as logger from "./utils/logger"
import * as version from "./utils/version"
import * as report from "./utils/report"
import * as worktree from "./utils/worktree"
import { loadConfig } from "./utils/config"
import { loadConfig, resolveBaseBranch } from "./utils/config"
import { transformAll as transformPackageNames } from "./transforms/package-names"
import { preserveAllVersions } from "./transforms/preserve-versions"
import { keepOursFiles, resetToOurs } from "./transforms/keep-ours"
Expand Down Expand Up @@ -219,7 +219,6 @@ async function main() {
process.chdir((await $`git rev-parse --show-toplevel`.text()).trim())

const options = parseArgs()
const config = loadConfig(options.baseBranch ? { baseBranch: options.baseBranch } : undefined)

if (options.verbose) {
logger.setVerbose(true)
Expand Down Expand Up @@ -248,6 +247,12 @@ async function main() {
const currentBranch = await git.getCurrentBranch()
logger.info(`Current branch: ${currentBranch}`)

const base = resolveBaseBranch(options.baseBranch, currentBranch)
const config = loadConfig(base ? { baseBranch: base } : undefined)
if (options.baseBranch === "HEAD") {
logger.info(`Resolved --base-branch HEAD to current branch: ${config.baseBranch}`)
}

// Enable git rerere so conflict resolutions are recorded and reused across merges
if (!options.dryRun) {
await git.ensureRerere()
Expand Down
15 changes: 15 additions & 0 deletions script/upstream/utils/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from "bun:test"
import { resolveBaseBranch } from "./config"

test("resolves HEAD to the current branch", () => {
expect(resolveBaseBranch("HEAD", "session/agent-123")).toBe("session/agent-123")
})

test("keeps explicit base branch names", () => {
expect(resolveBaseBranch("main", "session/agent-123")).toBe("main")
expect(resolveBaseBranch(undefined, "session/agent-123")).toBeUndefined()
})

test("rejects HEAD when detached", () => {
expect(() => resolveBaseBranch("HEAD", "HEAD")).toThrow("--base-branch HEAD requires a named branch")
})
6 changes: 6 additions & 0 deletions script/upstream/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,9 @@ export const defaultConfig: MergeConfig = {
export function loadConfig(overrides?: Partial<MergeConfig>): MergeConfig {
return { ...defaultConfig, ...overrides }
}

export function resolveBaseBranch(base: string | undefined, current: string): string | undefined {
if (base !== "HEAD") return base
if (current === "HEAD") throw new Error("--base-branch HEAD requires a named branch, but git is in detached HEAD")
return current
}
Loading