@@ -15,6 +15,8 @@ use crate::run::{
1515 } ,
1616} ;
1717
18+ static FAKE_COMMIT_REF : & str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ;
19+
1820#[ derive( Debug ) ]
1921pub struct LocalProvider {
2022 repository_provider : RepositoryProvider ,
@@ -31,56 +33,10 @@ impl TryFrom<&Config> for LocalProvider {
3133 type Error = Error ;
3234 fn try_from ( config : & Config ) -> Result < Self > {
3335 let current_dir = std:: env:: current_dir ( ) ?;
34- match find_repository_root ( & current_dir) {
35- Some ( mut path) => {
36- // Add a trailing slash to the path
37- path. push ( "" ) ;
38- let repository_root_path = path. to_string_lossy ( ) . to_string ( ) ;
39- let git_repository =
40- Repository :: open ( repository_root_path. clone ( ) ) . context ( format ! (
41- "Failed to open repository at path: {}" ,
42- repository_root_path
43- ) ) ?;
44-
45- let remote = git_repository. find_remote ( "origin" ) ?;
46-
47- let ( repository_provider, owner, repository) = if let Some ( repo_override) =
48- config. repository_override . clone ( )
49- {
50- (
51- repo_override. repository_provider ,
52- repo_override. owner ,
53- repo_override. repository ,
54- )
55- } else {
56- extract_provider_owner_and_repository_from_remote_url ( remote. url ( ) . unwrap ( ) ) ?
57- } ;
58-
59- let head = git_repository. head ( ) . context ( "Failed to get HEAD" ) ?;
60- let ref_ = head
61- . peel_to_commit ( )
62- . context ( "Failed to get HEAD commit" ) ?
63- . id ( )
64- . to_string ( ) ;
65- let head_ref = if head. is_branch ( ) {
66- let branch = head. shorthand ( ) . context ( "Failed to get HEAD branch name" ) ?;
67- Some ( branch. to_string ( ) )
68- } else {
69- None
70- } ;
71-
72- Ok ( Self {
73- repository_provider,
74- ref_,
75- head_ref,
76- base_ref : None ,
77- owner,
78- repository,
79- event : RunEvent :: Local ,
80- repository_root_path,
81- } )
82- }
83- None => {
36+
37+ let repository_root_path = {
38+ let Some ( mut path) = find_repository_root ( & current_dir) else {
39+ // We are not in a git repository, use the repository_override with very minimal information
8440 let RepositoryOverride {
8541 owner,
8642 repository,
@@ -90,19 +46,64 @@ impl TryFrom<&Config> for LocalProvider {
9046 please make sure you are running the command from inside a git repository or provide repository with --repository flag",
9147 ) ?;
9248
93- Ok ( Self {
49+ return Ok ( Self {
9450 repository_provider,
95- // TODO: Handle this ?
96- ref_ : "fake_ref" . to_string ( ) ,
51+ ref_ : FAKE_COMMIT_REF . to_string ( ) ,
9752 head_ref : None ,
9853 base_ref : None ,
9954 owner,
10055 repository,
10156 repository_root_path : current_dir. to_string_lossy ( ) . to_string ( ) ,
10257 event : RunEvent :: Local ,
103- } )
104- }
105- }
58+ } ) ;
59+ } ;
60+
61+ // Add a trailing slash to the path
62+ path. push ( "" ) ;
63+ path. to_string_lossy ( ) . to_string ( )
64+ } ;
65+
66+ let git_repository = Repository :: open ( repository_root_path. clone ( ) ) . context ( format ! (
67+ "Failed to open repository at path: {}" ,
68+ repository_root_path
69+ ) ) ?;
70+
71+ let remote = git_repository. find_remote ( "origin" ) ?;
72+
73+ let ( repository_provider, owner, repository) =
74+ if let Some ( repo_override) = config. repository_override . clone ( ) {
75+ (
76+ repo_override. repository_provider ,
77+ repo_override. owner ,
78+ repo_override. repository ,
79+ )
80+ } else {
81+ extract_provider_owner_and_repository_from_remote_url ( remote. url ( ) . unwrap ( ) ) ?
82+ } ;
83+
84+ let head = git_repository. head ( ) . context ( "Failed to get HEAD" ) ?;
85+ let ref_ = head
86+ . peel_to_commit ( )
87+ . context ( "Failed to get HEAD commit" ) ?
88+ . id ( )
89+ . to_string ( ) ;
90+ let head_ref = if head. is_branch ( ) {
91+ let branch = head. shorthand ( ) . context ( "Failed to get HEAD branch name" ) ?;
92+ Some ( branch. to_string ( ) )
93+ } else {
94+ None
95+ } ;
96+
97+ Ok ( Self {
98+ repository_provider,
99+ ref_,
100+ head_ref,
101+ base_ref : None ,
102+ owner,
103+ repository,
104+ event : RunEvent :: Local ,
105+ repository_root_path,
106+ } )
106107 }
107108}
108109
@@ -216,6 +217,11 @@ mod tests {
216217 }
217218 }
218219
220+ #[ test]
221+ fn fake_commit_hash_ref ( ) {
222+ assert_eq ! ( FAKE_COMMIT_REF . len( ) , 40 ) ;
223+ }
224+
219225 // TODO: uncomment later when we have a way to mock git repository
220226 // #[test]
221227 // fn test_provider_metadata() {
0 commit comments