@@ -4,6 +4,23 @@ use crate::OutputFormat;
44
55const HEAD_LENGTH : usize = 9 ;
66const ZERO_HEAD : & str = "000000000" ;
7+
8+ /// list: List all worktrees associated with the main repository.
9+ ///
10+ /// This function collects information about the main worktree and any linked
11+ /// worktrees, then writes them to the provided output stream in a human-readable
12+ /// format.
13+ ///
14+ /// # Parameters
15+ ///
16+ /// - `repo`: The Git repository from which the worktrees are retrieved.
17+ /// - `out`: The output stream where the worktree list is written.
18+ /// - `format`: The output format to use. Currently, only `OutputFormat::Human`
19+ /// is supported.
20+ ///
21+ /// # Returns
22+ ///
23+ /// Returns `Ok(())` if the worktrees are successfully listed.
724pub fn list ( repo : gix:: Repository , out : & mut dyn std:: io:: Write , format : OutputFormat ) -> anyhow:: Result < ( ) > {
825 if format != OutputFormat :: Human {
926 bail ! ( "JSON output isn't implemented yet" ) ;
@@ -37,13 +54,29 @@ pub fn list(repo: gix::Repository, out: &mut dyn std::io::Write, format: OutputF
3754 Ok ( ( ) )
3855}
3956
57+ /// WorktreeInfo
58+ ///
59+ /// Stores display worktree information
4060struct WorktreeInfo {
4161 base : String ,
4262 head : String ,
4363 branch : String ,
4464}
4565
4666impl WorktreeInfo {
67+ /// write: Writes the worktree information to the given output stream.
68+ ///
69+ /// The output contains the worktree path, the shortened HEAD commit hash,
70+ /// and the current branch name.
71+ ///
72+ /// # Parameters
73+ ///
74+ /// - `out`: The output stream where the worktree information is written.
75+ /// - `path_width`: The width used to align the worktree paths.
76+ ///
77+ /// # Returns
78+ ///
79+ /// Returns `Ok(())` if the information is successfully written.
4780 fn write ( & self , out : & mut dyn std:: io:: Write , path_width : usize ) -> std:: io:: Result < ( ) > {
4881 writeln ! (
4982 out,
@@ -56,6 +89,24 @@ impl WorktreeInfo {
5689 }
5790}
5891
92+ /// create_worktree_info: Creates display information for an accessible worktree.
93+ ///
94+ /// This function reads the worktree HEAD and branch name from the given
95+ /// repository. If the HEAD commit cannot be read, a zero hash is used instead.
96+ /// If the repository is in a detached HEAD state, the branch name is displayed
97+ /// as `<detached>`.
98+ ///
99+ /// # Parameters
100+ ///
101+ /// - `repo`: The repository associated with the worktree.
102+ /// - `base`: The resolved base path of the worktree.
103+ ///
104+ /// # Returns
105+ ///
106+ /// Returns a `WorktreeInfo` value containing:
107+ /// - the worktree base path,
108+ /// - the shortened HEAD commit hash,
109+ /// - the current branch name or `<detached>`.
59110fn create_worktree_info ( repo : & gix:: Repository , base : std:: path:: PathBuf ) -> anyhow:: Result < WorktreeInfo > {
60111 let head = repo. head_id ( ) . map_or_else (
61112 |_| ZERO_HEAD . to_string ( ) ,
@@ -74,6 +125,23 @@ fn create_worktree_info(repo: &gix::Repository, base: std::path::PathBuf) -> any
74125 } )
75126}
76127
128+ /// create_inaccessible_worktree_info: Creates display information for
129+ /// an inaccessible worktree.
130+ ///
131+ /// This function is used when a linked worktree exists but its repository
132+ /// cannot be opened. In that case, the HEAD is displayed as a zero hash and
133+ /// the branch is displayed as `<unknown>`.
134+ ///
135+ /// # Parameters
136+ ///
137+ /// - `base`: The resolved base path of the inaccessible worktree.
138+ ///
139+ /// # Returns
140+ ///
141+ /// Returns a `WorktreeInfo` value containing:
142+ /// - the worktree base path,
143+ /// - a zero hash as the HEAD value,
144+ /// - `<unknown>` as the branch name.
77145fn create_inaccessible_worktree_info ( base : std:: path:: PathBuf ) -> WorktreeInfo {
78146 WorktreeInfo {
79147 base : base. display ( ) . to_string ( ) ,
0 commit comments