@@ -116,9 +116,11 @@ pub trait RepoCommands {
116116 ///
117117 /// If nothing could be found at `probably_relative_path`, the returned structure indicates this,
118118 /// but it's no error.
119- fn read_file_from_workspace (
119+ fn read_file_from_workspace ( & self , probably_relative_path : & Path ) -> Result < FileInfo > ;
120+
121+ fn read_file_from_commit (
120122 & self ,
121- treeish : Option < Oid > ,
123+ treeish : Oid ,
122124 probably_relative_path : & Path ,
123125 ) -> Result < FileInfo > ;
124126}
@@ -182,24 +184,10 @@ impl RepoCommands for Project {
182184 Ok ( ( ) )
183185 }
184186
185- fn read_file_from_workspace (
186- & self ,
187- treeish : Option < Oid > ,
188- probably_relative_path : & Path ,
189- ) -> Result < FileInfo > {
187+ fn read_file_from_workspace ( & self , probably_relative_path : & Path ) -> Result < FileInfo > {
190188 let ctx = CommandContext :: open ( self ) ?;
191189 let repo = ctx. repo ( ) ;
192190
193- if let Some ( treeish) = treeish {
194- if !probably_relative_path. is_relative ( ) {
195- bail ! (
196- "Refusing to read '{}' from tree as it's not relative to the worktree" ,
197- probably_relative_path. display( ) ,
198- ) ;
199- }
200- return read_file_from_tree ( repo, Some ( treeish) , probably_relative_path) ;
201- }
202-
203191 let ( path_in_worktree, relative_path) = if probably_relative_path. is_relative ( ) {
204192 (
205193 gix:: path:: realpath ( self . path . join ( probably_relative_path) ) ?,
@@ -238,30 +226,33 @@ impl RepoCommands for Project {
238226 let blob = repo. find_blob ( entry. id ) ?;
239227 FileInfo :: from_content ( & relative_path, blob. content ( ) )
240228 }
241- None => read_file_from_tree ( repo, None , & relative_path) ?,
229+ None => self . read_file_from_commit (
230+ repo. head ( ) ?. peel_to_commit ( ) ?. id ( ) ,
231+ & relative_path,
232+ ) ?,
242233 }
243234 }
244235 Err ( err) => return Err ( err. into ( ) ) ,
245236 } )
246237 }
247- }
248238
249- fn read_file_from_tree (
250- repo : & git2:: Repository ,
251- treeish : Option < Oid > ,
252- relative_path : & Path ,
253- ) -> Result < FileInfo > {
254- let tree = if let Some ( id) = treeish {
255- repo. find_object ( id, None ) ?. peel_to_tree ( ) ?
256- } else {
257- repo. head ( ) ?. peel_to_tree ( ) ?
258- } ;
259- Ok ( match tree. get_path ( relative_path) {
260- Ok ( entry) => {
261- let blob = repo. find_blob ( entry. id ( ) ) ?;
262- FileInfo :: from_content ( relative_path, blob. content ( ) )
239+ fn read_file_from_commit ( & self , commit_id : Oid , relative_path : & Path ) -> Result < FileInfo > {
240+ if !relative_path. is_relative ( ) {
241+ bail ! (
242+ "Refusing to read '{}' from tree as it's not relative to the worktree" ,
243+ relative_path. display( ) ,
244+ ) ;
263245 }
264- Err ( e) if e. code ( ) == git2:: ErrorCode :: NotFound => FileInfo :: deleted ( ) ,
265- Err ( e) => return Err ( e. into ( ) ) ,
266- } )
246+ let ctx = CommandContext :: open ( self ) ?;
247+ let repo = ctx. repo ( ) ;
248+ let tree = repo. find_commit ( commit_id) ?. tree ( ) ?;
249+ Ok ( match tree. get_path ( relative_path) {
250+ Ok ( entry) => {
251+ let blob = repo. find_blob ( entry. id ( ) ) ?;
252+ FileInfo :: from_content ( relative_path, blob. content ( ) )
253+ }
254+ Err ( e) if e. code ( ) == git2:: ErrorCode :: NotFound => FileInfo :: deleted ( ) ,
255+ Err ( e) => return Err ( e. into ( ) ) ,
256+ } )
257+ }
267258}
0 commit comments