fix: improve cross-platform compatibility (#2064)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable max-lines -- Why: runtime git dispatch stays in one boundary so local, SSH, and runtime-environment behavior remains comparable. */
|
||||
import type {
|
||||
GitBranchCompareResult,
|
||||
GitConflictOperation,
|
||||
@@ -6,10 +7,13 @@ import type {
|
||||
GitStatusResult,
|
||||
GitUpstreamStatus,
|
||||
GitWorktreeInfo,
|
||||
GlobalSettings,
|
||||
Worktree
|
||||
} from '../../shared/types'
|
||||
import type { CommitMessageDraftContext } from '../../shared/commit-message-generation'
|
||||
import { getRemoteFileUrl } from '../git/repo'
|
||||
import {
|
||||
bulkDiscardChanges,
|
||||
bulkStageFiles,
|
||||
bulkUnstageFiles,
|
||||
commitChanges,
|
||||
@@ -18,6 +22,7 @@ import {
|
||||
getBranchCompare,
|
||||
getBranchDiff,
|
||||
getDiff,
|
||||
getStagedCommitContext,
|
||||
getStatus as getGitStatus,
|
||||
stageFile,
|
||||
unstageFile
|
||||
@@ -25,14 +30,37 @@ import {
|
||||
import { getUpstreamStatus } from '../git/upstream'
|
||||
import { gitFetch, gitPull, gitPush } from '../git/remote'
|
||||
import { getSshGitProvider } from '../providers/ssh-git-dispatch'
|
||||
import {
|
||||
cancelGenerateCommitMessageLocal,
|
||||
generateCommitMessageFromContext,
|
||||
resolveCommitMessageSettings,
|
||||
type GenerateCommitMessageResult
|
||||
} from '../text-generation/commit-message-text-generation'
|
||||
import type { CommitMessageAgentEnvironmentResolvers } from '../text-generation/commit-message-agent-environment'
|
||||
import { prepareLocalCommitMessageAgentEnv } from '../text-generation/commit-message-agent-environment'
|
||||
import { normalizeRuntimeRelativePath } from './runtime-relative-paths'
|
||||
|
||||
export type ResolvedRuntimeGitWorktree = Worktree & { git: GitWorktreeInfo }
|
||||
type RuntimeCommitMessageSettingsOverride = Partial<
|
||||
Pick<GlobalSettings, 'commitMessageAi' | 'agentCmdOverrides' | 'enableGitHubAttribution'>
|
||||
>
|
||||
|
||||
function normalizeRuntimeGitRelativePath(filePath: string): string {
|
||||
const relativePath = normalizeRuntimeRelativePath(filePath)
|
||||
if (relativePath === '') {
|
||||
// Why: git mutation APIs treat an empty pathspec as the worktree root;
|
||||
// runtime RPC must never let malformed file paths discard whole worktrees.
|
||||
throw new Error('invalid_relative_path')
|
||||
}
|
||||
return relativePath
|
||||
}
|
||||
|
||||
export type RuntimeGitCommandHost = {
|
||||
resolveRuntimeGitTarget(
|
||||
selector: string
|
||||
): Promise<{ worktree: ResolvedRuntimeGitWorktree; connectionId?: string }>
|
||||
getRuntimeSettings(): GlobalSettings
|
||||
getCommitMessageAgentEnvironment?(): CommitMessageAgentEnvironmentResolvers | undefined
|
||||
}
|
||||
|
||||
export class RuntimeGitCommands {
|
||||
@@ -52,7 +80,9 @@ export class RuntimeGitCommands {
|
||||
? provider.getStatus(target.worktree.path, options)
|
||||
: provider.getStatus(target.worktree.path)
|
||||
}
|
||||
return options ? getGitStatus(target.worktree.path, options) : getGitStatus(target.worktree.path)
|
||||
return options
|
||||
? getGitStatus(target.worktree.path, options)
|
||||
: getGitStatus(target.worktree.path)
|
||||
}
|
||||
|
||||
async getRuntimeGitConflictOperation(worktreeSelector: string): Promise<GitConflictOperation> {
|
||||
@@ -74,7 +104,7 @@ export class RuntimeGitCommands {
|
||||
compareAgainstHead?: boolean
|
||||
): Promise<GitDiffResult> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePath = normalizeRuntimeRelativePath(filePath)
|
||||
const relativePath = normalizeRuntimeGitRelativePath(filePath)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
@@ -165,8 +195,8 @@ export class RuntimeGitCommands {
|
||||
oldPath?: string
|
||||
): Promise<GitDiffResult> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePath = normalizeRuntimeRelativePath(filePath)
|
||||
const oldRelativePath = oldPath ? normalizeRuntimeRelativePath(oldPath) : undefined
|
||||
const relativePath = normalizeRuntimeGitRelativePath(filePath)
|
||||
const oldRelativePath = oldPath ? normalizeRuntimeGitRelativePath(oldPath) : undefined
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
@@ -213,9 +243,83 @@ export class RuntimeGitCommands {
|
||||
return commitChanges(target.worktree.path, message)
|
||||
}
|
||||
|
||||
async generateRuntimeCommitMessage(
|
||||
worktreeSelector: string,
|
||||
settingsOverride?: RuntimeCommitMessageSettingsOverride
|
||||
): Promise<GenerateCommitMessageResult> {
|
||||
const resolvedSettings = resolveCommitMessageSettings({
|
||||
...this.host.getRuntimeSettings(),
|
||||
...settingsOverride
|
||||
})
|
||||
if (!resolvedSettings.ok) {
|
||||
return { success: false, error: resolvedSettings.error }
|
||||
}
|
||||
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
return {
|
||||
success: false,
|
||||
error: `No git provider for connection "${target.connectionId}"`
|
||||
}
|
||||
}
|
||||
let context: CommitMessageDraftContext | null
|
||||
try {
|
||||
context = await provider.getStagedCommitContext(target.worktree.path)
|
||||
} catch (error) {
|
||||
console.error('[runtime-git] Failed to read remote staged commit context:', error)
|
||||
return { success: false, error: 'Failed to read staged changes.' }
|
||||
}
|
||||
if (!context) {
|
||||
return { success: false, error: 'No staged changes to summarize.' }
|
||||
}
|
||||
return generateCommitMessageFromContext(context, resolvedSettings.params, {
|
||||
kind: 'remote',
|
||||
cwd: target.worktree.path,
|
||||
execute: (plan, cwd, timeoutMs) => provider.executeCommitMessagePlan(plan, cwd, timeoutMs),
|
||||
missingBinaryLocation: 'remote PATH'
|
||||
})
|
||||
}
|
||||
|
||||
let context: CommitMessageDraftContext | null
|
||||
try {
|
||||
context = await getStagedCommitContext(target.worktree.path)
|
||||
} catch (error) {
|
||||
console.error('[runtime-git] Failed to read staged commit context:', error)
|
||||
return { success: false, error: 'Failed to read staged changes.' }
|
||||
}
|
||||
if (!context) {
|
||||
return { success: false, error: 'No staged changes to summarize.' }
|
||||
}
|
||||
const localEnv = await prepareLocalCommitMessageAgentEnv(
|
||||
resolvedSettings.params.agentId,
|
||||
this.host.getCommitMessageAgentEnvironment?.()
|
||||
)
|
||||
if (!localEnv.ok) {
|
||||
return { success: false, error: localEnv.error }
|
||||
}
|
||||
return generateCommitMessageFromContext(context, resolvedSettings.params, {
|
||||
kind: 'local',
|
||||
cwd: target.worktree.path,
|
||||
...(localEnv.env ? { env: localEnv.env } : {})
|
||||
})
|
||||
}
|
||||
|
||||
async cancelRuntimeGenerateCommitMessage(worktreeSelector: string): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
await provider?.cancelGenerateCommitMessage(target.worktree.path)
|
||||
return { ok: true }
|
||||
}
|
||||
cancelGenerateCommitMessageLocal(target.worktree.path)
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async stageRuntimeGitPath(worktreeSelector: string, filePath: string): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePath = normalizeRuntimeRelativePath(filePath)
|
||||
const relativePath = normalizeRuntimeGitRelativePath(filePath)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
@@ -230,7 +334,7 @@ export class RuntimeGitCommands {
|
||||
|
||||
async unstageRuntimeGitPath(worktreeSelector: string, filePath: string): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePath = normalizeRuntimeRelativePath(filePath)
|
||||
const relativePath = normalizeRuntimeGitRelativePath(filePath)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
@@ -248,7 +352,7 @@ export class RuntimeGitCommands {
|
||||
filePaths: string[]
|
||||
): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePaths = filePaths.map((path) => normalizeRuntimeRelativePath(path))
|
||||
const relativePaths = filePaths.map((path) => normalizeRuntimeGitRelativePath(path))
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
@@ -266,7 +370,7 @@ export class RuntimeGitCommands {
|
||||
filePaths: string[]
|
||||
): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePaths = filePaths.map((path) => normalizeRuntimeRelativePath(path))
|
||||
const relativePaths = filePaths.map((path) => normalizeRuntimeGitRelativePath(path))
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
@@ -279,9 +383,27 @@ export class RuntimeGitCommands {
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async bulkDiscardRuntimeGitPaths(
|
||||
worktreeSelector: string,
|
||||
filePaths: string[]
|
||||
): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePaths = filePaths.map((path) => normalizeRuntimeGitRelativePath(path))
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
throw new Error('remote_git_unavailable')
|
||||
}
|
||||
await provider.bulkDiscardChanges(target.worktree.path, relativePaths)
|
||||
return { ok: true }
|
||||
}
|
||||
await bulkDiscardChanges(target.worktree.path, relativePaths)
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async discardRuntimeGitPath(worktreeSelector: string, filePath: string): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const relativePath = normalizeRuntimeRelativePath(filePath)
|
||||
const relativePath = normalizeRuntimeGitRelativePath(filePath)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
@@ -300,7 +422,7 @@ export class RuntimeGitCommands {
|
||||
line: number
|
||||
): Promise<string | null> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const normalizedRelativePath = normalizeRuntimeRelativePath(relativePath)
|
||||
const normalizedRelativePath = normalizeRuntimeGitRelativePath(relativePath)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
|
||||
Reference in New Issue
Block a user