@@ -57,15 +57,22 @@ function writeTextFile(
5757function git (
5858 cwd : string ,
5959 args : ReadonlyArray < string > ,
60+ options : { readonly committedAt ?: string ; readonly allowNonZeroExit ?: boolean } = { } ,
6061) : Effect . Effect < string , VcsError , VcsProcess . VcsProcess > {
6162 return Effect . gen ( function * ( ) {
62- const process = yield * VcsProcess . VcsProcess ;
63- const result = yield * process . run ( {
63+ const vcsProcess = yield * VcsProcess . VcsProcess ;
64+ const result = yield * vcsProcess . run ( {
6465 operation : "CheckpointStore.test.git" ,
6566 command : "git" ,
6667 cwd,
6768 args,
6869 timeoutMs : 10_000 ,
70+ ...( options . committedAt === undefined
71+ ? { }
72+ : { env : { ...process . env , GIT_COMMITTER_DATE : options . committedAt } } ) ,
73+ ...( options . allowNonZeroExit === undefined
74+ ? { }
75+ : { allowNonZeroExit : options . allowNonZeroExit } ) ,
6976 } ) ;
7077 return result . stdout . trim ( ) ;
7178 } ) ;
@@ -371,12 +378,12 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => {
371378 ) ;
372379 const expectedFiles = [
373380 { path : "binary.bin" , additions : 0 , deletions : 0 } ,
374- { path : "copied.txt" , additions : 0 , deletions : 0 } ,
381+ { path : "copied.txt" , previousPath : "copy-source.txt" , additions : 0 , deletions : 0 } ,
375382 { path : "copy-source.txt" , additions : 1 , deletions : 0 } ,
376383 { path : "deleted.txt" , additions : 0 , deletions : 1 } ,
377384 { path : "empty.txt" , additions : 0 , deletions : 0 } ,
378385 { path : addedPath , additions : 2 , deletions : 0 } ,
379- { path : renamedPath , additions : 1 , deletions : 1 } ,
386+ { path : renamedPath , previousPath : "rename-old.txt" , additions : 1 , deletions : 1 } ,
380387 ] . toSorted ( ( left , right ) => left . path . localeCompare ( right . path ) ) ;
381388 expect ( firstSummary ) . toEqual ( expectedFiles ) ;
382389
@@ -441,5 +448,122 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => {
441448 ] ) ;
442449 } ) ,
443450 ) ;
451+ it . effect ( "limits a diff to repository paths from a subdirectory workspace" , ( ) =>
452+ Effect . gen ( function * ( ) {
453+ const tmp = yield * makeTmpDir ( ) ;
454+ yield * initRepoWithCommit ( tmp ) ;
455+ const fileSystem = yield * FileSystem . FileSystem ;
456+ const cwd = NodePath . join ( tmp , "app" ) ;
457+ yield * fileSystem . makeDirectory ( cwd ) ;
458+ const checkpointStore = yield * CheckpointStore . CheckpointStore ;
459+ const threadId = ThreadId . make ( "checkpoint-file-paths-subdirectory" ) ;
460+ const refs = {
461+ cwd,
462+ fromCheckpointRef : checkpointRefForThreadTurn ( threadId , 0 ) ,
463+ toCheckpointRef : checkpointRefForThreadTurn ( threadId , 1 ) ,
464+ } ;
465+ yield * checkpointStore . captureCheckpoint ( { cwd, checkpointRef : refs . fromCheckpointRef } ) ;
466+ yield * writeTextFile ( NodePath . join ( cwd , "kept.txt" ) , "kept\n" ) ;
467+ yield * writeTextFile ( NodePath . join ( tmp , "README.md" ) , "# hidden\n" ) ;
468+ yield * checkpointStore . captureCheckpoint ( { cwd, checkpointRef : refs . toCheckpointRef } ) ;
469+
470+ const patch = yield * checkpointStore . diffCheckpoints ( {
471+ ...refs ,
472+ ignoreWhitespace : false ,
473+ filePaths : [ "app/kept.txt" ] ,
474+ } ) ;
475+ expect ( patch ) . toContain ( "+kept" ) ;
476+ expect ( patch ) . not . toContain ( "hidden" ) ;
477+ } ) ,
478+ ) ;
479+ } ) ;
480+
481+ describe ( "listAuthoredPaths" , ( ) => {
482+ it . effect ( "leaves out paths that only a merged upstream changed" , ( ) =>
483+ Effect . gen ( function * ( ) {
484+ const tmp = yield * makeTmpDir ( ) ;
485+ yield * initRepoWithCommit ( tmp ) ;
486+ const checkpointStore = yield * CheckpointStore . CheckpointStore ;
487+ const threadId = ThreadId . make ( "checkpoint-authored-paths" ) ;
488+ const refs = {
489+ cwd : tmp ,
490+ fromCheckpointRef : checkpointRefForThreadTurn ( threadId , 0 ) ,
491+ toCheckpointRef : checkpointRefForThreadTurn ( threadId , 1 ) ,
492+ } ;
493+ const commitBeforeTurn = ( file : string , contents : string ) =>
494+ Effect . gen ( function * ( ) {
495+ yield * writeTextFile ( NodePath . join ( tmp , file ) , contents ) ;
496+ yield * git ( tmp , [ "add" , "." ] ) ;
497+ yield * git ( tmp , [ "commit" , "-m" , file ] , { committedAt : "2020-01-01T00:00:00Z" } ) ;
498+ } ) ;
499+
500+ yield * git ( tmp , [ "checkout" , "-b" , "upstream" ] ) ;
501+ yield * commitBeforeTurn ( "upstream.txt" , "upstream\n" ) ;
502+ yield * commitBeforeTurn ( "shared.txt" , "upstream\n" ) ;
503+ yield * git ( tmp , [ "checkout" , "-" ] ) ;
504+ yield * commitBeforeTurn ( "shared.txt" , "ours\n" ) ;
505+ yield * checkpointStore . captureCheckpoint ( {
506+ cwd : tmp ,
507+ checkpointRef : refs . fromCheckpointRef ,
508+ } ) ;
509+
510+ yield * checkpointStore . captureCheckpoint ( { cwd : tmp , checkpointRef : refs . toCheckpointRef } ) ;
511+ expect ( yield * checkpointStore . listAuthoredPaths ( refs ) ) . toBeNull ( ) ;
512+
513+ // The turn merges upstream, fixes the conflict by taking upstream, and edits one more file.
514+ yield * git ( tmp , [ "merge" , "upstream" , "--no-edit" ] , { allowNonZeroExit : true } ) ;
515+ yield * writeTextFile ( NodePath . join ( tmp , "shared.txt" ) , "upstream\n" ) ;
516+ yield * git ( tmp , [ "commit" , "-am" , "merge upstream" ] ) ;
517+ yield * writeTextFile ( NodePath . join ( tmp , "README.md" ) , "# edited\n" ) ;
518+ yield * checkpointStore . captureCheckpoint ( { cwd : tmp , checkpointRef : refs . toCheckpointRef } ) ;
519+
520+ const numstat = yield * checkpointStore . diffCheckpoints ( {
521+ ...refs ,
522+ ignoreWhitespace : false ,
523+ format : "numstat" ,
524+ } ) ;
525+ expect ( parseTurnDiffFilesFromNumstat ( numstat ) . map ( ( file ) => file . path ) ) . toEqual ( [
526+ "README.md" ,
527+ "shared.txt" ,
528+ "upstream.txt" ,
529+ ] ) ;
530+ expect ( yield * checkpointStore . listAuthoredPaths ( refs ) ) . toEqual (
531+ new Set ( [ "README.md" , "shared.txt" ] ) ,
532+ ) ;
533+ } ) ,
534+ ) ;
535+
536+ it . effect ( "leaves out paths that only a rebase onto upstream brought in" , ( ) =>
537+ Effect . gen ( function * ( ) {
538+ const tmp = yield * makeTmpDir ( ) ;
539+ yield * initRepoWithCommit ( tmp ) ;
540+ const checkpointStore = yield * CheckpointStore . CheckpointStore ;
541+ const threadId = ThreadId . make ( "checkpoint-authored-paths-rebase" ) ;
542+ const refs = {
543+ cwd : tmp ,
544+ fromCheckpointRef : checkpointRefForThreadTurn ( threadId , 0 ) ,
545+ toCheckpointRef : checkpointRefForThreadTurn ( threadId , 1 ) ,
546+ } ;
547+ const commitBeforeTurn = ( file : string ) =>
548+ Effect . gen ( function * ( ) {
549+ yield * writeTextFile ( NodePath . join ( tmp , file ) , `${ file } \n` ) ;
550+ yield * git ( tmp , [ "add" , "." ] ) ;
551+ yield * git ( tmp , [ "commit" , "-m" , file ] , { committedAt : "2020-01-01T00:00:00Z" } ) ;
552+ } ) ;
553+
554+ yield * git ( tmp , [ "checkout" , "-b" , "upstream" ] ) ;
555+ yield * commitBeforeTurn ( "upstream.txt" ) ;
556+ yield * git ( tmp , [ "checkout" , "-" ] ) ;
557+ yield * commitBeforeTurn ( "feature.txt" ) ;
558+ yield * checkpointStore . captureCheckpoint ( {
559+ cwd : tmp ,
560+ checkpointRef : refs . fromCheckpointRef ,
561+ } ) ;
562+ yield * git ( tmp , [ "rebase" , "upstream" ] ) ;
563+ yield * checkpointStore . captureCheckpoint ( { cwd : tmp , checkpointRef : refs . toCheckpointRef } ) ;
564+
565+ expect ( yield * checkpointStore . listAuthoredPaths ( refs ) ) . toEqual ( new Set ( [ "feature.txt" ] ) ) ;
566+ } ) ,
567+ ) ;
444568 } ) ;
445569} ) ;
0 commit comments