@@ -355,6 +355,109 @@ def setup_completion(link:)
355355 end . to raise_error ( TapCoreRemoteMismatchError )
356356 end
357357
358+ it "creates core and cask taps as worktrees when the brew source repository has them" do
359+ source_repository = HOMEBREW_PREFIX . parent /"source-repository"
360+ worktree_git_dir = HOMEBREW_REPOSITORY /".git"
361+
362+ [ CoreTap . instance , CoreCaskTap . instance ] . each do |tap |
363+ source_tap = source_repository /"Library/Taps/#{ tap . full_name . downcase } "
364+
365+ FileUtils . rm_rf tap . path
366+ source_tap . mkpath
367+ source_tap . cd do
368+ system "git" , "init"
369+ FileUtils . touch "README.md"
370+ system "git" , "add" , "--all"
371+ system "git" , "commit" , "-m" , "init"
372+ end
373+ FileUtils . mkdir_p worktree_git_dir . dirname
374+ worktree_git_dir . write "gitdir: #{ source_repository } /.git/worktrees/#{ HOMEBREW_REPOSITORY . basename } \n "
375+
376+ allow ( tap ) . to receive_messages ( command_files : [ ] , formula_files : [ ] , cask_files : [ ] ,
377+ formula_names : [ ] , cask_tokens : [ ] )
378+ expect ( tap ) . to receive ( :safe_system )
379+ . with ( "git" , "-C" , source_tap , "worktree" , "add" , "--detach" , tap . path , "HEAD" )
380+ . and_wrap_original do
381+ tap . path . mkpath
382+ ( tap . path /".git" ) . write "gitdir: #{ source_tap } /.git/worktrees/#{ tap . full_repository . downcase } \n "
383+ end
384+
385+ tap . install
386+ end
387+ ensure
388+ FileUtils . rm_rf source_repository
389+ FileUtils . rm_rf CoreTap . instance . path
390+ FileUtils . rm_rf CoreCaskTap . instance . path
391+ ( CoreTap . instance . path /"Formula" ) . mkpath
392+ end
393+
394+ it "creates a tap from another brew worktree when that has the source repository" do
395+ tap = CoreCaskTap . instance
396+ source_repository = HOMEBREW_PREFIX . parent /"source-repository"
397+ source_worktree = HOMEBREW_PREFIX . parent /"source-worktree"
398+ source_tap = source_worktree /"Library/Taps/#{ tap . full_name . downcase } "
399+
400+ FileUtils . rm_rf tap . path
401+ source_tap . mkpath
402+ source_tap . cd do
403+ system "git" , "init"
404+ FileUtils . touch "README.md"
405+ system "git" , "add" , "--all"
406+ system "git" , "commit" , "-m" , "init"
407+ end
408+ FileUtils . mkdir_p ( HOMEBREW_REPOSITORY /".git" ) . dirname
409+ ( HOMEBREW_REPOSITORY /".git" )
410+ . write "gitdir: #{ source_repository } /.git/worktrees/#{ HOMEBREW_REPOSITORY . basename } \n "
411+
412+ allow ( Utils ) . to receive ( :popen_read ) . and_call_original
413+ allow ( Utils ) . to receive ( :popen_read )
414+ . with ( "git" , "-C" , HOMEBREW_REPOSITORY , "worktree" , "list" , "--porcelain" )
415+ . and_return ( "worktree #{ source_worktree } \n " )
416+ allow ( tap ) . to receive_messages ( command_files : [ ] , formula_files : [ ] , cask_files : [ ] ,
417+ formula_names : [ ] , cask_tokens : [ ] )
418+ expect ( tap ) . to receive ( :safe_system )
419+ . with ( "git" , "-C" , source_tap , "worktree" , "add" , "--detach" , tap . path , "HEAD" )
420+ . and_wrap_original do
421+ tap . path . mkpath
422+ ( tap . path /".git" ) . write "gitdir: #{ source_tap } /.git/worktrees/#{ tap . full_repository . downcase } \n "
423+ end
424+
425+ tap . install
426+ ensure
427+ FileUtils . rm_rf source_repository
428+ FileUtils . rm_rf source_worktree
429+ FileUtils . rm_rf CoreCaskTap . instance . path
430+ end
431+
432+ it "uses the requested remote for cask taps with an explicit clone target" do
433+ tap = CoreCaskTap . instance
434+ requested_remote = "https://example.com/Homebrew/homebrew-cask"
435+ source_repository = HOMEBREW_PREFIX . parent /"source-repository"
436+ source_tap = source_repository /"Library/Taps/#{ tap . full_name . downcase } "
437+
438+ FileUtils . rm_rf tap . path
439+ source_tap . mkpath
440+ ( source_tap /".git" ) . mkpath
441+ FileUtils . mkdir_p ( HOMEBREW_REPOSITORY /".git" ) . dirname
442+ ( HOMEBREW_REPOSITORY /".git" )
443+ . write "gitdir: #{ source_repository } /.git/worktrees/#{ HOMEBREW_REPOSITORY . basename } \n "
444+
445+ allow ( tap ) . to receive_messages ( command_files : [ ] , formula_files : [ ] , cask_files : [ ] ,
446+ formula_names : [ ] , cask_tokens : [ ] )
447+ expect ( tap ) . to receive ( :safe_system )
448+ . with ( "git" , "clone" , requested_remote , tap . path . to_s , "--origin=origin" , "--template=" ,
449+ "--config" , "core.fsmonitor=false" )
450+ . and_wrap_original do
451+ tap . path . mkpath
452+ ( tap . path /".git" ) . mkpath
453+ end
454+
455+ tap . install clone_target : requested_remote , force : true
456+ ensure
457+ FileUtils . rm_rf source_repository
458+ FileUtils . rm_rf CoreCaskTap . instance . path
459+ end
460+
358461 it "raises an error when run `brew tap --custom-remote` without a custom remote (already installed)" do
359462 setup_git_repo
360463 already_tapped_tap = klass . fetch ( "Homebrew" , "foo" )
@@ -391,6 +494,26 @@ def setup_completion(link:)
391494 tap = klass . fetch ( "Homebrew" , "bar" )
392495 expect { tap . uninstall } . to raise_error ( TapUnavailableError )
393496 end
497+
498+ it "removes Git worktree metadata for worktree-installed taps" do
499+ tap = CoreCaskTap . instance
500+ source_tap = HOMEBREW_PREFIX . parent /"source-tap"
501+
502+ FileUtils . rm_rf tap . path
503+ source_tap . mkpath
504+ ( source_tap /".git" ) . mkpath
505+ tap . path . mkpath
506+ ( tap . path /".git" ) . write "gitdir: #{ source_tap } /.git/worktrees/#{ tap . full_repository . downcase } \n "
507+
508+ allow ( tap ) . to receive_messages ( contents : [ ] , formula_names : [ ] , cask_tokens : [ ] )
509+ expect ( tap ) . to receive ( :safe_system )
510+ . with ( "git" , "-C" , source_tap , "worktree" , "remove" , "--force" , tap . path )
511+
512+ tap . uninstall
513+ ensure
514+ FileUtils . rm_rf source_tap
515+ FileUtils . rm_rf CoreCaskTap . instance . path
516+ end
394517 end
395518
396519 specify "#install and #uninstall" do
0 commit comments