@@ -4340,6 +4340,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
43404340 it . effect . each ( [
43414341 { localBranch : "ahead" , expectedHead : "local" , isOnPullRequestHead : false } ,
43424342 { localBranch : "behind" , expectedHead : "pull request" , isOnPullRequestHead : true } ,
4343+ { localBranch : "rewritten" , expectedHead : "pull request" , isOnPullRequestHead : true } ,
43434344 ] as const ) (
43444345 "moves an existing PR branch into a new worktree only when nothing is lost ($localBranch)" ,
43454346 ( { localBranch, expectedHead, isOnPullRequestHead } ) =>
@@ -4358,21 +4359,39 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
43584359 NodeFS . writeFileSync ( NodePath . join ( repoDir , "mine.txt" ) , "mine\n" ) ;
43594360 yield * runGit ( repoDir , [ "add" , "mine.txt" ] ) ;
43604361 yield * runGit ( repoDir , [ "commit" , "-m" , "My unpublished commit" ] ) ;
4361- } else {
4362+ } else if ( localBranch === "behind" ) {
43624363 yield * runGit ( repoDir , [ "checkout" , "-b" , "author-push" ] ) ;
43634364 NodeFS . writeFileSync ( NodePath . join ( repoDir , "authored.txt" ) , "authored\n" ) ;
43644365 yield * runGit ( repoDir , [ "add" , "authored.txt" ] ) ;
43654366 yield * runGit ( repoDir , [ "commit" , "-m" , "New PR head commit" ] ) ;
43664367 yield * runGit ( repoDir , [ "push" , "origin" , "author-push:feature/pr-existing" ] ) ;
4368+ } else {
4369+ const staleHead = ( yield * runGit ( repoDir , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ;
4370+ yield * runGit ( repoDir , [ "checkout" , "-b" , "author-rewrite" ] ) ;
4371+ NodeFS . writeFileSync ( NodePath . join ( repoDir , "existing.txt" ) , "rewritten\n" ) ;
4372+ yield * runGit ( repoDir , [ "add" , "existing.txt" ] ) ;
4373+ yield * runGit ( repoDir , [ "commit" , "--amend" , "-m" , "Rewritten PR head" ] ) ;
4374+ yield * runGit ( repoDir , [
4375+ "push" ,
4376+ "--force" ,
4377+ "origin" ,
4378+ "author-rewrite:feature/pr-existing" ,
4379+ ] ) ;
4380+ yield * runGit ( repoDir , [
4381+ "update-ref" ,
4382+ "refs/remotes/origin/feature/pr-existing" ,
4383+ staleHead ,
4384+ ] ) ;
43674385 }
43684386 const localHead = ( yield * runGit ( repoDir , [
43694387 "rev-parse" ,
43704388 "feature/pr-existing" ,
43714389 ] ) ) . stdout . trim ( ) ;
43724390 const pullHead = ( yield * runGit ( repoDir , [
4373- "rev-parse" ,
4374- "origin/feature/pr-existing" ,
4375- ] ) ) . stdout . trim ( ) ;
4391+ "ls-remote" ,
4392+ "origin" ,
4393+ "refs/heads/feature/pr-existing" ,
4394+ ] ) ) . stdout . split ( "\t" ) [ 0 ] ;
43764395 yield * runGit ( repoDir , [ "checkout" , "main" ] ) ;
43774396
43784397 const { manager } = yield * makeManager ( {
@@ -4411,68 +4430,82 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
44114430 } ) ,
44124431 ) ;
44134432
4414- it . effect ( "checks out the submodules of the head an existing PR branch moves onto" , ( ) =>
4415- Effect . gen ( function * ( ) {
4416- // Git refuses `file:` submodule transports by default (CVE-2022-39253) and ignores
4417- // repo-level config for it, so a local fixture needs the env allowance.
4418- const previousAllowedProtocol = process . env . GIT_ALLOW_PROTOCOL ;
4419- process . env . GIT_ALLOW_PROTOCOL = "file" ;
4420- yield * Effect . addFinalizer ( ( ) =>
4421- Effect . sync ( ( ) => {
4422- if ( previousAllowedProtocol === undefined ) {
4423- delete process . env . GIT_ALLOW_PROTOCOL ;
4424- } else {
4425- process . env . GIT_ALLOW_PROTOCOL = previousAllowedProtocol ;
4426- }
4427- } ) ,
4428- ) ;
4433+ it . effect . each ( [ "adds" , "removes" ] as const ) (
4434+ "checks out the submodules of the head an existing PR branch moves onto (head %s one)" ,
4435+ ( headChange ) =>
4436+ Effect . gen ( function * ( ) {
4437+ // Git refuses `file:` submodule transports by default (CVE-2022-39253) and ignores
4438+ // repo-level config for it, so a local fixture needs the env allowance.
4439+ const previousAllowedProtocol = process . env . GIT_ALLOW_PROTOCOL ;
4440+ process . env . GIT_ALLOW_PROTOCOL = "file" ;
4441+ yield * Effect . addFinalizer ( ( ) =>
4442+ Effect . sync ( ( ) => {
4443+ if ( previousAllowedProtocol === undefined ) {
4444+ delete process . env . GIT_ALLOW_PROTOCOL ;
4445+ } else {
4446+ process . env . GIT_ALLOW_PROTOCOL = previousAllowedProtocol ;
4447+ }
4448+ } ) ,
4449+ ) ;
44294450
4430- const submoduleDir = yield * makeTempDir ( "t3code-git-submodule-" ) ;
4431- yield * initRepo ( submoduleDir ) ;
4432- const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
4433- yield * initRepo ( repoDir ) ;
4434- const remoteDir = yield * createBareRemote ( ) ;
4435- yield * runGit ( repoDir , [ "remote" , "add" , "origin" , remoteDir ] ) ;
4436- yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "main" ] ) ;
4437- yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/pr-existing" ] ) ;
4438- yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "feature/pr-existing" ] ) ;
4439- yield * runGit ( repoDir , [ "checkout" , "-b" , "author-push" ] ) ;
4440- yield * runGit ( repoDir , [ "submodule" , "add" , submoduleDir , "shared" ] ) ;
4441- yield * runGit ( repoDir , [ "commit" , "-m" , "Add shared submodule" ] ) ;
4442- yield * runGit ( repoDir , [ "push" , "origin" , "author-push:feature/pr-existing" ] ) ;
4443- const pullHead = ( yield * runGit ( repoDir , [ "rev-parse" , "author-push" ] ) ) . stdout . trim ( ) ;
4444- yield * runGit ( repoDir , [ "checkout" , "main" ] ) ;
4451+ const submoduleDir = yield * makeTempDir ( "t3code-git-submodule-" ) ;
4452+ yield * initRepo ( submoduleDir ) ;
4453+ const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
4454+ yield * initRepo ( repoDir ) ;
4455+ const remoteDir = yield * createBareRemote ( ) ;
4456+ yield * runGit ( repoDir , [ "remote" , "add" , "origin" , remoteDir ] ) ;
4457+ yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "main" ] ) ;
4458+ yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/pr-existing" ] ) ;
4459+ if ( headChange === "removes" ) {
4460+ yield * runGit ( repoDir , [ "submodule" , "add" , submoduleDir , "shared" ] ) ;
4461+ yield * runGit ( repoDir , [ "commit" , "-m" , "Add shared submodule" ] ) ;
4462+ }
4463+ yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "feature/pr-existing" ] ) ;
4464+ yield * runGit ( repoDir , [ "checkout" , "-b" , "author-push" ] ) ;
4465+ if ( headChange === "adds" ) {
4466+ yield * runGit ( repoDir , [ "submodule" , "add" , submoduleDir , "shared" ] ) ;
4467+ yield * runGit ( repoDir , [ "commit" , "-m" , "Add shared submodule" ] ) ;
4468+ } else {
4469+ yield * runGit ( repoDir , [ "rm" , "shared" ] ) ;
4470+ yield * runGit ( repoDir , [ "commit" , "-m" , "Remove shared submodule" ] ) ;
4471+ }
4472+ yield * runGit ( repoDir , [ "push" , "origin" , "author-push:feature/pr-existing" ] ) ;
4473+ const pullHead = ( yield * runGit ( repoDir , [ "rev-parse" , "author-push" ] ) ) . stdout . trim ( ) ;
4474+ yield * runGit ( repoDir , [ "checkout" , "main" ] ) ;
44454475
4446- const { manager } = yield * makeManager ( {
4447- providerScenario : {
4448- pullRequest : {
4449- number : 89 ,
4450- title : "Submodule PR" ,
4451- url : "https://github.com/pingdotgg/codething-mvp/pull/89" ,
4452- baseRefName : "main" ,
4453- headRefName : "feature/pr-existing" ,
4454- state : "open" ,
4455- isCrossRepository : false ,
4456- headRepositoryNameWithOwner : "pingdotgg/codething-mvp" ,
4457- headRepositoryOwnerLogin : "pingdotgg" ,
4458- } ,
4459- repositoryCloneUrls : {
4460- "pingdotgg/codething-mvp" : { url : remoteDir , sshUrl : remoteDir } ,
4476+ const { manager } = yield * makeManager ( {
4477+ providerScenario : {
4478+ pullRequest : {
4479+ number : 89 ,
4480+ title : "Submodule PR" ,
4481+ url : "https://github.com/pingdotgg/codething-mvp/pull/89" ,
4482+ baseRefName : "main" ,
4483+ headRefName : "feature/pr-existing" ,
4484+ state : "open" ,
4485+ isCrossRepository : false ,
4486+ headRepositoryNameWithOwner : "pingdotgg/codething-mvp" ,
4487+ headRepositoryOwnerLogin : "pingdotgg" ,
4488+ } ,
4489+ repositoryCloneUrls : {
4490+ "pingdotgg/codething-mvp" : { url : remoteDir , sshUrl : remoteDir } ,
4491+ } ,
44614492 } ,
4462- } ,
4463- } ) ;
4493+ } ) ;
44644494
4465- const result = yield * preparePullRequestThread ( manager , {
4466- cwd : repoDir ,
4467- reference : "89" ,
4468- mode : "worktree" ,
4469- } ) ;
4495+ const result = yield * preparePullRequestThread ( manager , {
4496+ cwd : repoDir ,
4497+ reference : "89" ,
4498+ mode : "worktree" ,
4499+ } ) ;
44704500
4471- const worktreePath = result . worktreePath as string ;
4472- expect ( ( yield * runGit ( worktreePath , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ) . toBe ( pullHead ) ;
4473- expect ( NodeFS . existsSync ( NodePath . join ( worktreePath , "shared" , "README.md" ) ) ) . toBe ( true ) ;
4474- expect ( result ) . toMatchObject ( { isOnPullRequestHead : true } ) ;
4475- } ) ,
4501+ const worktreePath = result . worktreePath as string ;
4502+ expect ( ( yield * runGit ( worktreePath , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ) . toBe ( pullHead ) ;
4503+ expect ( NodeFS . existsSync ( NodePath . join ( worktreePath , "shared" , "README.md" ) ) ) . toBe (
4504+ headChange === "adds" ,
4505+ ) ;
4506+ expect ( ( yield * runGit ( worktreePath , [ "status" , "--porcelain" ] ) ) . stdout ) . toBe ( "" ) ;
4507+ expect ( result ) . toMatchObject ( { isOnPullRequestHead : true } ) ;
4508+ } ) ,
44764509 ) ;
44774510
44784511 it . effect ( "refreshes a reused PR worktree onto the updated pull request head" , ( ) =>
0 commit comments