diff --git a/cli/src/commands/gitsync-settings/pull.ts b/cli/src/commands/gitsync-settings/pull.ts index 9777a27ce0..5c99a6264d 100644 --- a/cli/src/commands/gitsync-settings/pull.ts +++ b/cli/src/commands/gitsync-settings/pull.ts @@ -22,7 +22,8 @@ import { applyBackendSettingsToBranch, normalizeRepoPath, outputResult, - getEffectiveSettingsForWorkspace + getEffectiveSettingsForWorkspace, + resolveGitSyncWsName } from "./utils.ts"; export async function pullGitSyncSettings( @@ -441,12 +442,21 @@ export async function pullGitSyncSettings( } } - // For override mode, write to branch overrides - if (writeMode === "override" && isGitRepository()) { - const currentBranch = getCurrentGitBranch(); - if (currentBranch) { - log.info(`Detected Git repository, writing settings to branch: ${currentBranch}`); - updatedConfig = applyBackendSettingsToBranch(localConfig, currentBranch, backendSyncOptions); + // 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) { + overrideTarget = + findWorkspaceByGitBranch(localConfig.workspaces, currentBranch)?.[0] ?? currentBranch; + } + } + if (overrideTarget) { + log.info(`Writing settings to workspace entry: ${overrideTarget}`); + updatedConfig = applyBackendSettingsToBranch(localConfig, overrideTarget, backendSyncOptions); } } @@ -468,10 +478,9 @@ export async function pullGitSyncSettings( ), ); if (writeMode === "override") { - const currentBranch = getCurrentGitBranch(); - if (currentBranch) { + if (overrideTarget) { log.info( - colors.gray(`Settings written to branch '${currentBranch}' overrides`), + colors.gray(`Settings written to workspace '${overrideTarget}' overrides`), ); } } else if (writeMode === "replace") { diff --git a/cli/src/commands/gitsync-settings/utils.ts b/cli/src/commands/gitsync-settings/utils.ts index 8bc5e1be8d..358217a58e 100644 --- a/cli/src/commands/gitsync-settings/utils.ts +++ b/cli/src/commands/gitsync-settings/utils.ts @@ -29,20 +29,29 @@ export function outputResult(opts: { jsonOutput?: boolean }, result: { } /** - * The wmill.yaml settings that apply to the workspace being synced. Resolved by - * workspace first, 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. + * 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. */ +export function resolveGitSyncWsName( + config: SyncOptions, + opts: { workspace?: string }, + workspace: { remote: string; workspaceId: string }, +): string | undefined { + return ( + resolveWsNameForConfigFromFlags({ workspaces: config.workspaces, workspace: opts.workspace }) ?? + inferWsNameFromProfile(config, workspace) ?? + inferWsNameFromForkBranch(config, workspace.workspaceId) + ); +} + export async function getEffectiveSettingsForWorkspace( config: SyncOptions, opts: { workspace?: string; promotion?: string; jsonOutput?: boolean }, workspace: { remote: string; workspaceId: string }, ): Promise { - const wsName = - resolveWsNameForConfigFromFlags({ workspaces: config.workspaces, workspace: opts.workspace }) ?? - inferWsNameFromProfile(config, workspace) ?? - inferWsNameFromForkBranch(config, workspace.workspaceId); + const wsName = resolveGitSyncWsName(config, opts, workspace); return await getEffectiveSettings(config, opts.promotion, true, opts.jsonOutput, wsName); }