@@ -58,6 +58,36 @@ impl crate::Repository {
5858 self . work_tree . as_deref ( )
5959 }
6060
61+ /// Forcefully set the given `workdir` to be the worktree of this repository, *in memory*,
62+ /// no matter if it had one or not, or unset it with `None`.
63+ /// Return the previous working directory if one existed.
64+ ///
65+ /// Fail if the `workdir`, if not `None`, isn't accessible or isn't a directory.
66+ /// No change is performed on error.
67+ ///
68+ /// ### About Worktrees
69+ ///
70+ /// * When setting a main worktree to a linked worktree directory, this repository instance
71+ /// will still claim that it is the [main worktree](crate::Worktree::is_main()) as that depends
72+ /// on the `git_dir`, not the worktree dir.
73+ /// * When setting a linked worktree to a main worktree directory, this repository instance
74+ /// will still claim that it is *not* a [main worktree](crate::Worktree::is_main()) as that depends
75+ /// on the `git_dir`, not the worktree dir.
76+ #[ doc( alias = "git2" ) ]
77+ pub fn set_workdir ( & mut self , workdir : impl Into < Option < PathBuf > > ) -> Result < Option < PathBuf > , std:: io:: Error > {
78+ let workdir = workdir. into ( ) ;
79+ Ok ( match workdir {
80+ None => self . work_tree . take ( ) ,
81+ Some ( new_workdir) => {
82+ _ = std:: fs:: read_dir ( & new_workdir) ?;
83+
84+ let old = self . work_tree . take ( ) ;
85+ self . work_tree = Some ( new_workdir) ;
86+ old
87+ }
88+ } )
89+ }
90+
6191 /// Return the work tree containing all checked out files, if there is one.
6292 pub fn workdir ( & self ) -> Option < & std:: path:: Path > {
6393 self . work_tree . as_deref ( )
0 commit comments