diff --git a/cli/src/commands/gitsync-settings/pull.ts b/cli/src/commands/gitsync-settings/pull.ts index c46bf7bfe2..f6f5531a56 100644 --- a/cli/src/commands/gitsync-settings/pull.ts +++ b/cli/src/commands/gitsync-settings/pull.ts @@ -6,7 +6,7 @@ import { GlobalOptions } from "../../types.ts"; import { requireLogin } from "../../core/auth.ts"; import { resolveWorkspace } from "../../core/context.ts"; import * as wmill from "../../../gen/services.gen.ts"; -import { SyncOptions, readConfigFile, DEFAULT_SYNC_OPTIONS, getWmillYamlPath, findWorkspaceByGitBranch, WorkspacesConfig, WorkspaceEntryConfig, getWorkspaceNames, getEffectiveGitBranch } from "../../core/conf.ts"; +import { SyncOptions, readConfigFile, DEFAULT_SYNC_OPTIONS, getWmillYamlPath, findWorkspaceByGitBranch, WorkspacesConfig } from "../../core/conf.ts"; import { yamlOptions } from "../sync/sync.ts"; import { deepEqual } from "../../utils/utils.ts"; import { getCurrentGitBranch, isGitRepository } from "../../utils/git.ts"; @@ -23,7 +23,7 @@ import { normalizeRepoPath, outputResult, getEffectiveSettingsForWorkspace, - resolveGitSyncWsName + resolveOverrideTarget } from "./utils.ts"; export async function pullGitSyncSettings( @@ -442,30 +442,14 @@ export async function pullGitSyncSettings( } } - // Override mode writes to the entry the diff was computed against, so a - // second pull finds no conflict let overrideTarget: string | undefined; if (writeMode === "override") { - overrideTarget = resolveGitSyncWsName(localConfig, opts, workspace); - if (!overrideTarget && isGitRepository()) { - const currentBranch = getCurrentGitBranch(); - if (currentBranch) { - // The branch does not identify a workspace, so an unresolved workspace - // never writes into an existing entry: it could be another workspace's. - const entries = (localConfig.workspaces ?? {}) as Record; - const taken = getWorkspaceNames(localConfig.workspaces).filter( - (name) => - name === currentBranch || getEffectiveGitBranch(name, entries[name]) === currentBranch, - ); - if (taken.length > 0) { - throw new Error( - `No workspace entry in wmill.yaml matches workspace '${workspace.workspaceId}', and git branch '${currentBranch}' is already used by ${taken.join(", ")}. ` + - `Name the entry for this workspace '${workspace.workspaceId}' and keep 'gitBranch: ${currentBranch}' on it so it can be resolved.`, - ); - } - overrideTarget = currentBranch; - } - } + overrideTarget = resolveOverrideTarget( + localConfig, + opts, + workspace, + isGitRepository() ? getCurrentGitBranch() : undefined, + ); if (overrideTarget) { log.info(`Writing settings to workspace entry: ${overrideTarget}`); updatedConfig = applyBackendSettingsToBranch(localConfig, overrideTarget, backendSyncOptions); diff --git a/cli/src/commands/gitsync-settings/utils.ts b/cli/src/commands/gitsync-settings/utils.ts index 358217a58e..cb57c5e488 100644 --- a/cli/src/commands/gitsync-settings/utils.ts +++ b/cli/src/commands/gitsync-settings/utils.ts @@ -8,6 +8,9 @@ import { resolveWsNameForConfigFromFlags, inferWsNameFromProfile, inferWsNameFromForkBranch, + getWorkspaceNames, + getEffectiveGitBranch, + type WorkspaceEntryConfig, } from "../../core/conf.ts"; import { GitSyncRepository, GIT_SYNC_FIELDS } from "./types.ts"; import { GitSyncSettingsConverter } from "./converter.ts"; @@ -29,10 +32,10 @@ export function outputResult(opts: { jsonOutput?: boolean }, result: { } /** - * The wmill.yaml entry of the workspace being synced, resolved by workspace as - * `sync pull/push` do: several entries can share one `gitBranch`, so matching on - * the checked-out branch alone picks whichever of them comes first in the file. - * Undefined when no entry names this workspace. + * The wmill.yaml entry whose settings apply to the workspace being synced, + * resolved by workspace as `sync pull/push` do: several entries can share one + * `gitBranch`, so matching on the checked-out branch alone picks whichever of + * them comes first in the file. A fork resolves to its parent's entry. */ export function resolveGitSyncWsName( config: SyncOptions, @@ -46,6 +49,38 @@ export function resolveGitSyncWsName( ); } +/** + * The entry `pull --override` writes into: the one its diff was read from. + * + * A fork writes into its parent's entry on purpose. A fork never has an entry + * of its own, so an entry beside the parent's would never be read back; and the + * write lands on the fork branch, so it reaches the parent only through the + * fork's pull request, which is how a settings change made in a fork is promoted. + * + * With no resolved entry the branch does not say whose an existing entry is, so + * it refuses when the branch is in use and otherwise creates a branch-named entry. + */ +export function resolveOverrideTarget( + config: SyncOptions, + opts: { workspace?: string }, + workspace: { remote: string; workspaceId: string }, + currentBranch: string | null | undefined, +): string | undefined { + const resolved = resolveGitSyncWsName(config, opts, workspace); + if (resolved || !currentBranch) return resolved; + const entries = (config.workspaces ?? {}) as Record; + const taken = getWorkspaceNames(config.workspaces).filter( + (name) => name === currentBranch || getEffectiveGitBranch(name, entries[name]) === currentBranch, + ); + if (taken.length > 0) { + throw new Error( + `No workspace entry in wmill.yaml matches workspace '${workspace.workspaceId}', and git branch '${currentBranch}' is already used by ${taken.join(", ")}. ` + + `Name the entry for this workspace '${workspace.workspaceId}' and keep 'gitBranch: ${currentBranch}' on it so it can be resolved.`, + ); + } + return currentBranch; +} + export async function getEffectiveSettingsForWorkspace( config: SyncOptions, opts: { workspace?: string; promotion?: string; jsonOutput?: boolean }, diff --git a/cli/test/gitsync_settings_workspace_resolution_unit.test.ts b/cli/test/gitsync_settings_workspace_resolution_unit.test.ts index 9e0d52262c..0408f9f0c0 100644 --- a/cli/test/gitsync_settings_workspace_resolution_unit.test.ts +++ b/cli/test/gitsync_settings_workspace_resolution_unit.test.ts @@ -1,6 +1,13 @@ import { expect, test } from "bun:test"; +import { execSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import type { SyncOptions } from "../src/core/conf.ts"; -import { getEffectiveSettingsForWorkspace } from "../src/commands/gitsync-settings/utils.ts"; +import { + getEffectiveSettingsForWorkspace, + resolveOverrideTarget, +} from "../src/commands/gitsync-settings/utils.ts"; // Entries sharing a gitBranch must resolve by workspace, not by the first one // bound to the branch. @@ -36,3 +43,45 @@ test("gitsync-settings: entries sharing a gitBranch resolve by workspace", async ); expect(byProfile.includes).toEqual(["f/team_b/**"]); }); + +// `pull --override` writes into the entry its diff reads, and never into an +// entry the branch alone attributes to this workspace. +test("gitsync-settings: override target", () => { + const remote = "http://windmill_server:8000/"; + const target = (workspaces: SyncOptions["workspaces"], workspaceId: string, branch: string) => + resolveOverrideTarget({ workspaces }, { workspace: workspaceId }, { remote, workspaceId }, branch); + + const shared = { + "team-a": { gitBranch: "staging" }, + "team-b": { gitBranch: "staging" }, + }; + expect(target(shared, "team-b", "staging")).toEqual("team-b"); + expect(() => target(shared, "team-c", "staging")).toThrow("already used by team-a, team-b"); + expect(() => target({ staging: {} }, "team-c", "staging")).toThrow("already used by staging"); + expect(() => target({ team: { workspaceId: "other", gitBranch: "staging" } }, "team-c", "staging")).toThrow(); + expect(target({ prod: { gitBranch: "main" } }, "team-c", "staging")).toEqual("staging"); +}); + +// A fork has no entry of its own: an entry beside its parent's would never be +// read back, so its override goes into the parent's entry on the fork branch. +test("gitsync-settings: a fork's override targets its parent's entry", () => { + const dir = mkdtempSync(join(tmpdir(), "wmill-fork-")); + const cwd = process.cwd(); + try { + execSync( + "git init -q -b staging && git -c user.email=t@t -c user.name=t commit -q --allow-empty -m i && git switch -q -c wm-fork/staging/abc", + { cwd: dir, stdio: "pipe" }, + ); + process.chdir(dir); + const target = resolveOverrideTarget( + { workspaces: { staging: {}, other: { gitBranch: "main" } } }, + { workspace: "wm-fork-abc" }, + { remote: "http://windmill_server:8000/", workspaceId: "wm-fork-abc" }, + "wm-fork/staging/abc", + ); + expect(target).toEqual("staging"); + } finally { + process.chdir(cwd); + rmSync(dir, { recursive: true, force: true }); + } +});