@@ -4,8 +4,9 @@ import * as NodeChildProcess from "node:child_process";
44import * as NodeFS from "node:fs" ;
55import * as NodeOS from "node:os" ;
66
7- import { assert , it } from "@effect/vitest" ;
7+ import { assert , it , vi } from "@effect/vitest" ;
88
9+ import * as ForkCommand from "./lib/fork-command.ts" ;
910import { fixupFoldFailures } from "./fork-fixup-fold.ts" ;
1011
1112const git = ( root : string , args : ReadonlyArray < string > ) : string =>
@@ -48,6 +49,85 @@ it("passes a stack whose fixups fold at their owners, squash suffix included", (
4849 }
4950} ) ;
5051
52+ it ( "folds without configured Git identity and leaves the live repository unchanged" , ( ) => {
53+ const root = NodeFS . mkdtempSync ( NodeOS . tmpdir ( ) + "/fork-fixup-fold-" ) ;
54+ try {
55+ git ( root , [ "init" , "--quiet" , "--initial-branch" , "main" ] ) ;
56+ identity ( root ) ;
57+ commit ( root , "upstream base" , { "file.txt" : "one\ntwo\n" } ) ;
58+ const base = git ( root , [ "rev-parse" , "HEAD" ] ) ;
59+ commit ( root , "owner edit" , { "file.txt" : "ONE\ntwo\n" } ) ;
60+ commit ( root , "fixup! owner edit" , { "file.txt" : "ONE!\ntwo\n" } ) ;
61+ const tip = git ( root , [ "rev-parse" , "HEAD" ] ) ;
62+ // Empty local values mask any ambient global identity, like a clean CI runner.
63+ git ( root , [ "config" , "user.name" , "" ] ) ;
64+ git ( root , [ "config" , "user.email" , "" ] ) ;
65+ assert . deepStrictEqual ( fixupFoldFailures ( root , base , "HEAD" ) , [ ] ) ;
66+ assert . strictEqual ( git ( root , [ "rev-parse" , "HEAD" ] ) , tip ) ;
67+ assert . strictEqual ( git ( root , [ "config" , "user.name" ] ) , "" ) ;
68+ assert . strictEqual ( git ( root , [ "config" , "user.email" ] ) , "" ) ;
69+ assert . strictEqual ( worktreeCount ( root ) , 1 ) ;
70+ } finally {
71+ NodeFS . rmSync ( root , { recursive : true , force : true } ) ;
72+ }
73+ } ) ;
74+
75+ it ( "reports Git's error when the replay fails before applying a commit" , ( ) => {
76+ const root = NodeFS . mkdtempSync ( NodeOS . tmpdir ( ) + "/fork-fixup-fold-" ) ;
77+ try {
78+ git ( root , [ "init" , "--quiet" , "--initial-branch" , "main" ] ) ;
79+ identity ( root ) ;
80+ commit ( root , "upstream base" , { "file.txt" : "one\n" } ) ;
81+ const base = git ( root , [ "rev-parse" , "HEAD" ] ) ;
82+ commit ( root , "owner edit" , { "file.txt" : "ONE\n" } ) ;
83+ commit ( root , "fixup! owner edit" , { "file.txt" : "ONE!\n" } ) ;
84+ const hooks = `${ root } /hooks` ;
85+ NodeFS . mkdirSync ( hooks ) ;
86+ NodeFS . writeFileSync (
87+ `${ hooks } /pre-rebase` ,
88+ "#!/bin/sh\necho 'fixture refuses replay' >&2\nexit 1\n" ,
89+ { mode : 0o755 } ,
90+ ) ;
91+ git ( root , [ "config" , "core.hooksPath" , hooks ] ) ;
92+ const [ failure ] = fixupFoldFailures ( root , base , "HEAD" ) ;
93+ assert . ok ( failure ?. includes ( "the replay failed before applying a commit" ) , failure ) ;
94+ assert . ok ( failure ?. includes ( "fixture refuses replay" ) , failure ) ;
95+ assert . strictEqual ( worktreeCount ( root ) , 1 ) ;
96+ } finally {
97+ NodeFS . rmSync ( root , { recursive : true , force : true } ) ;
98+ }
99+ } ) ;
100+
101+ it ( "keeps both Git stderr and a subprocess timeout error" , ( ) => {
102+ const root = NodeFS . mkdtempSync ( NodeOS . tmpdir ( ) + "/fork-fixup-fold-" ) ;
103+ try {
104+ git ( root , [ "init" , "--quiet" , "--initial-branch" , "main" ] ) ;
105+ identity ( root ) ;
106+ commit ( root , "upstream base" , { "file.txt" : "one\n" } ) ;
107+ const base = git ( root , [ "rev-parse" , "HEAD" ] ) ;
108+ commit ( root , "owner edit" , { "file.txt" : "ONE\n" } ) ;
109+ commit ( root , "fixup! owner edit" , { "file.txt" : "ONE!\n" } ) ;
110+ const runCommand = ForkCommand . runCommand ;
111+ vi . spyOn ( ForkCommand , "runCommand" ) . mockImplementation ( ( command , args , options ) =>
112+ command === "git" && args . includes ( "rebase" ) && args . includes ( "-i" )
113+ ? {
114+ status : 1 ,
115+ stdout : "" ,
116+ stderr : "replay progress" ,
117+ error : new Error ( "spawnSync git ETIMEDOUT" ) ,
118+ }
119+ : runCommand ( command , args , options ) ,
120+ ) ;
121+ const [ failure ] = fixupFoldFailures ( root , base , "HEAD" ) ;
122+ assert . ok ( failure ?. includes ( "replay progress" ) , failure ) ;
123+ assert . ok ( failure ?. includes ( "spawnSync git ETIMEDOUT" ) , failure ) ;
124+ assert . strictEqual ( worktreeCount ( root ) , 1 ) ;
125+ } finally {
126+ vi . restoreAllMocks ( ) ;
127+ NodeFS . rmSync ( root , { recursive : true , force : true } ) ;
128+ }
129+ } ) ;
130+
51131it ( "refuses a fixup that only folds at the tip, naming fixup and owner" , ( ) => {
52132 const root = NodeFS . mkdtempSync ( NodeOS . tmpdir ( ) + "/fork-fixup-fold-" ) ;
53133 try {
0 commit comments