@@ -88,15 +88,34 @@ func prepareWorkTree(ctx context.Context, repoDir, workTreeCacheDir, commit stri
8888 }
8989
9090 isWorkTreeRegistered := false
91- if workTreeList , err := GetWorkTreeList (ctx , repoDir ); err != nil {
91+ isWorkTreePrunable := false
92+ var dirToPrune , pruneReason string
93+
94+ workTreeList , err := GetWorkTreeList (ctx , repoDir )
95+ if err != nil {
9296 return "" , fmt .Errorf ("unable to get worktree list for repo %s: %w" , repoDir , err )
93- } else {
94- for _ , workTreeDesc := range workTreeList {
95- if filepath .ToSlash (workTreeDesc .Path ) == filepath .ToSlash (resolvedWorkTreeDir ) {
96- isWorkTreeRegistered = true
97- }
97+ }
98+
99+ for _ , workTreeDesc := range workTreeList {
100+ if filepath .ToSlash (workTreeDesc .Path ) == filepath .ToSlash (resolvedWorkTreeDir ) {
101+ isWorkTreeRegistered = true
102+ }
103+ if workTreeDesc .Prunable {
104+ isWorkTreePrunable = true
105+ dirToPrune = workTreeDesc .Path
106+ pruneReason = workTreeDesc .PruneReason
98107 }
99108 }
109+
110+ if isWorkTreePrunable {
111+ logboek .Context (ctx ).Default ().LogFDetails ("Detected prunable worktree %s due to %s\n " , dirToPrune , pruneReason )
112+ logboek .Context (ctx ).Default ().LogF ("Removing invalidated work tree dir %q of repo %s\n " , dirToPrune , repoDir )
113+ err := RemoveWorkTree (ctx , repoDir , dirToPrune )
114+ if err != nil {
115+ return "" , fmt .Errorf ("unable to remove worktree %q: %w" , dirToPrune , err )
116+ }
117+ }
118+
100119 if ! isWorkTreeRegistered {
101120 logboek .Context (ctx ).Default ().LogFDetails ("Detected unregistered work tree dir %q of repo %s\n " , workTreeDir , repoDir )
102121 }
@@ -289,9 +308,11 @@ func ResolveRepoDir(ctx context.Context, repoDir string) (string, error) {
289308}
290309
291310type WorktreeDescriptor struct {
292- Path string
293- Head string
294- Branch string
311+ Path string
312+ Head string
313+ Branch string
314+ Prunable bool
315+ PruneReason string
295316}
296317
297318func GetWorkTreeList (ctx context.Context , repoDir string ) ([]WorktreeDescriptor , error ) {
@@ -316,6 +337,9 @@ func GetWorkTreeList(ctx context.Context, repoDir string) ([]WorktreeDescriptor,
316337 worktreeDesc .Head = strings .TrimPrefix (line , "HEAD " )
317338 case strings .HasPrefix (line , "branch " ):
318339 worktreeDesc .Branch = strings .TrimPrefix (line , "branch " )
340+ case strings .HasPrefix (line , "prunable " ):
341+ worktreeDesc .Prunable = true
342+ worktreeDesc .PruneReason = strings .TrimPrefix (line , "prunable " )
319343 case line == "" :
320344 res = append (res , * worktreeDesc )
321345 worktreeDesc = nil
@@ -324,3 +348,8 @@ func GetWorkTreeList(ctx context.Context, repoDir string) ([]WorktreeDescriptor,
324348
325349 return res , nil
326350}
351+
352+ func RemoveWorkTree (ctx context.Context , repoDir , workTreeDir string ) error {
353+ removeCmd := NewGitCmd (ctx , & GitCmdOptions {RepoDir : repoDir }, "worktree" , "remove" , workTreeDir )
354+ return removeCmd .Run (ctx )
355+ }
0 commit comments