TypeScript
fix(git): preserve whitespace in file paths (#12464) (#12465)
Defmon3aef0e9ca401a
diff --git a/packages/contracts/src/git.test.ts b/packages/contracts/src/git.test.tsindex 5dda491b009..2b48000cebd 100644--- a/packages/contracts/src/git.test.ts+++ b/packages/contracts/src/git.test.ts@@ -8,6 +8,7 @@ import {GitRunStackedActionResult,GitRunStackedActionInput,GitResolvePullRequestResult,+ VcsStatusLocalResult,} from "./git.ts";const decodeCreateWorktreeInput = Schema.decodeUnknownSync(VcsCreateWorktreeInput);@@ -20,6 +21,7 @@ const decodePreparePullRequestThreadResult = Schema.decodeUnknownSync(const decodeRunStackedActionInput = Schema.decodeUnknownSync(GitRunStackedActionInput);const decodeRunStackedActionResult = Schema.decodeUnknownSync(GitRunStackedActionResult);const decodeResolvePullRequestResult = Schema.decodeUnknownSync(GitResolvePullRequestResult);+const decodeVcsStatusLocalResult = Schema.decodeUnknownSync(VcsStatusLocalResult);describe("VcsCreateWorktreeInput", () => {it("accepts omitted newRefName for existing-refName worktrees", () => {@@ -125,6 +127,70 @@ describe("GitRunStackedActionInput", () => {expect(parsed.actionId).toBe("action-1");expect(parsed.action).toBe("create_pr");});++ it("preserves leading and trailing whitespace in file paths", () => {+ const parsed = decodeRunStackedActionInput({+ actionId: "action-1",+ cwd: "/repo",+ action: "commit",+ filePaths: [" leading.txt", "trailing.txt "],+ });++ expect(parsed.filePaths).toEqual([" leading.txt", "trailing.txt "]);+ });++ it("rejects an empty file path", () => {+ expect(() =>+ decodeRunStackedActionInput({+ actionId: "action-1",+ cwd: "/repo",+ action: "commit",+ filePaths: [""],+ }),+ ).toThrow();+ });+});++describe("VcsStatusLocalResult", () => {+ it("preserves leading and trailing whitespace in working-tree paths", () => {+ const parsed = decodeVcsStatusLocalResult({+ isRepo: true,+ hasPrimaryRemote: true,+ isDefaultRef: false,+ refName: "main",+ hasWorkingTreeChanges: true,+ workingTree: {+ files: [+ { path: " leading.txt", insertions: 1, deletions: 0 },+ { path: "trailing.txt ", insertions: 0, deletions: 1 },+ ],+ insertions: 1,+ deletions: 1,+ },+ });++ expect(parsed.workingTree.files.map((file) => file.path)).toEqual([+ " leading.txt",+ "trailing.txt ",+ ]);+ });++ it("rejects an empty working-tree path", () => {+ expect(() =>+ decodeVcsStatusLocalResult({+ isRepo: true,+ hasPrimaryRemote: true,+ isDefaultRef: false,+ refName: "main",+ hasWorkingTreeChanges: true,+ workingTree: {+ files: [{ path: "", insertions: 0, deletions: 0 }],+ insertions: 0,+ deletions: 0,+ },+ }),+ ).toThrow();+ });});describe("GitRunStackedActionResult", () => {diff --git a/packages/contracts/src/git.ts b/packages/contracts/src/git.tsindex b2cb2e902d8..2c97a62ab5e 100644--- a/packages/contracts/src/git.ts+++ b/packages/contracts/src/git.ts@@ -11,6 +11,7 @@ import { SourceControlProviderError, SourceControlProviderInfo } from "./sourceCimport { VcsDriverKind } from "./vcs.ts";const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;+const GitFilePath = Schema.String.check(Schema.isNonEmpty());const GIT_LIST_BRANCHES_MAX_LIMIT = 200;// Domain Types@@ -129,9 +130,7 @@ export const GitRunStackedActionInput = Schema.Struct({action: GitStackedAction,commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),featureBranch: Schema.optional(Schema.Boolean),- filePaths: Schema.optional(- Schema.Array(TrimmedNonEmptyStringSchema).check(Schema.isMinLength(1)),- ),+ filePaths: Schema.optional(Schema.Array(GitFilePath).check(Schema.isMinLength(1))),/** The thread the action runs beside; a pull request it creates is linked to it. */threadId: Schema.optional(ThreadId),projectId: Schema.optional(ProjectId),@@ -234,7 +233,7 @@ const VcsStatusLocalShape = {workingTree: Schema.Struct({files: Schema.Array(Schema.Struct({- path: TrimmedNonEmptyStringSchema,+ path: GitFilePath,insertions: NonNegativeInt,deletions: NonNegativeInt,}),