11# frozen_string_literal: true
22
3- require 'open3'
4-
53module RubyGit
64 # The working tree is a directory tree consisting of the checked out files that
75 # you are currently working on.
@@ -106,9 +104,64 @@ def self.clone(repository_url, to_path: '')
106104 new ( to_path )
107105 end
108106
107+ # Show the working tree and index status
108+ #
109+ # @example worktree = Worktree.open(worktree_path) worktree.status #=>
110+ # #<RubyGit::Status::Report ...>
111+ #
112+ # @param untracked_files [:all, :normal, :no] Defines how untracked files will be
113+ # handled
114+ #
115+ # See [git-staus
116+ # --untracked-files](https://git-scm.com/docs/git-status#Documentation/git-status.txt---untracked-filesltmodegt).
117+ #
118+ # @param ignored [:traditional, :matching, :no] Defines how ignored files will be
119+ # handled, :no to not include ignored files
120+ #
121+ # See [git-staus
122+ # --ignored](https://git-scm.com/docs/git-status#Documentation/git-status.txt---ignoredltmodegt).
123+ #
124+ # @param ignore_submodules [:all, :dirty, :untracked, :none] Default is :all
125+ #
126+ # See [git-staus
127+ # --ignore-submodules](https://git-scm.com/docs/git-status#Documentation/git-status.txt---ignore-submodulesltwhengt).
128+ #
129+ # @return [RubyGit::Status::Report] the status of the working tree
130+ #
131+ def status ( untracked_files : :all , ignored : :no , ignore_submodules : :all )
132+ command = %w[ status --porcelain=v2 --branch --show-stash --ahead-behind --renames -z ]
133+ command << "--untracked-files=#{ untracked_files } "
134+ command << "--ignored=#{ ignored } "
135+ command << "--ignore-submodules=#{ ignore_submodules } "
136+ options = { out : StringIO . new , err : StringIO . new }
137+ status_output = run ( *command , **options ) . stdout
138+ RubyGit ::Status . parse ( status_output )
139+ end
140+
141+ # Return the repository associated with the worktree
142+ #
143+ # @example
144+ # worktree = Worktree.open(worktree_path)
145+ # worktree.repository #=> #<RubyGit::Repository ...>
146+ #
147+ # @return [RubyGit::Repository] the repository associated with the worktree
148+ #
149+ def repository
150+ @repository ||= begin
151+ command = %w[ rev-parse --git-dir ]
152+ options = { chdir : path , chomp : true , out : StringIO . new , err : StringIO . new }
153+ # rev-parse path might be relative to the worktree, thus the need to expand it
154+ git_dir = File . expand_path ( RubyGit ::CommandLine . run ( *command , **options ) . stdout , path )
155+ Repository . new ( git_dir )
156+ end
157+ end
158+
109159 private
110160
111161 # Create a Worktree object
162+ #
163+ # @param worktree_path [String] a path anywhere in the worktree
164+ #
112165 # @api private
113166 #
114167 def initialize ( worktree_path )
@@ -132,25 +185,19 @@ def root_path(worktree_path)
132185 RubyGit ::CommandLine . run ( *command , **options ) . stdout
133186 end
134187
135- # def run(*command, **options)
136- # RubyGit::CommandLine.run(*command, worktree_path: path, **options)
137- # end
138-
139- # #
140- # # @param untracked_files [Symbol] Can be :all, :normal, :no
141- # # @param ignore_submodules [Symbol] Can be :all, :dirty, :untracked, :none
142- # # @param ignored [Symbol] Can be :traditional, :matching, :no
143- # # @param renames [Boolean] Whether to detect renames
144- # def status(untracked_files:)
145- # # -z for null-terminated output
146- # # --porcelain for machine-readable output
147- # git status --porcelain=v2 --untracked-files --branch --show-stash --ahead-behind --renames -z
148- # command = ['status', '--porcelain', '--branch', '-z']
149- # command << '--untracked-files=all' if untracked_files == :all
150- # command << '--untracked-files=no' if untracked_files == :no
151- # options = { chdir: path, out: StringIO.new, err: StringIO.new }
152- # result = RubyGit::CommandLine.run(*command, **options)
153- # result.stdout
154- # end
188+ # Run a Git command in this worktree
189+ #
190+ # Passes the repository path and worktree path to RubyGit::CommandLine.run
191+ #
192+ # @param command [Array<String>] the git command to run
193+ # @param options [Hash] options to pass to RubyGit::CommandLine.run
194+ #
195+ # @return [RubyGit::CommandLineResult] the result of the git command
196+ #
197+ # @api private
198+ #
199+ def run ( *command , **options )
200+ RubyGit ::CommandLine . run ( *command , repository_path : repository . path , worktree_path : path , **options )
201+ end
155202 end
156203end
0 commit comments