@@ -2,9 +2,10 @@ use std::collections::HashMap;
22
33use gix:: Repository ;
44
5- mod fingerprint ;
5+ mod platform ;
66
7- pub use fingerprint:: Fingerprint ;
7+ pub use platform:: Fingerprint ;
8+ use platform:: OperatingSystem ;
89
910use crate :: ReportContext ;
1011
@@ -15,7 +16,8 @@ impl ReportContext {
1516 get_context ( )
1617 }
1718
18- fn insert ( & mut self , key : String , value : String ) -> Option < String > {
19+ fn insert ( & mut self , path : & str , value : String ) -> Option < String > {
20+ let key = format ! ( "bencher.dev{path}" ) ;
1921 self . 0 . insert ( key, value)
2022 }
2123}
@@ -27,62 +29,73 @@ impl From<ReportContext> for HashMap<String, String> {
2729 }
2830}
2931
30- pub fn find_repo ( ) -> Option < Repository > {
31- let current_dir = std:: env:: current_dir ( ) . ok ( ) ?;
32- for directory in current_dir. ancestors ( ) {
33- if let Ok ( repo) = gix:: open ( directory) {
34- return Some ( repo) ;
35- }
36- }
37- None
38- }
39-
4032pub fn get_context ( ) -> ReportContext {
4133 let mut context = ReportContext :: default ( ) ;
34+ git_context ( & mut context) ;
35+ platform_context ( & mut context) ;
36+ context
37+ }
38+
39+ fn git_context ( context : & mut ReportContext ) {
40+ let Some ( repo) = find_repo ( ) else {
41+ return ;
42+ } ;
4243
43- let repo = find_repo ( ) ;
44- let repo_name = repo_name ( repo. as_ref ( ) ) ;
45- if let Some ( repo_name) = repo_name {
46- context. insert ( "bencher.dev/repo/name" . to_owned ( ) , repo_name) ;
44+ if let Some ( repo_name) = repo_name ( & repo) {
45+ context. insert ( "/repo/name" , repo_name) ;
4746 }
48- if let Some ( repo) = & repo {
49- if let Some ( root_commit) = find_default_branch_and_root_commit ( repo) {
50- context. insert ( "bencher.dev/repo/hash" . to_owned ( ) , root_commit) ;
51- }
47+
48+ if let Some ( root_commit) = repo_hash ( & repo) {
49+ context. insert ( "/repo/hash" , root_commit) ;
5250 }
5351
54- if let Some ( repo) = & repo {
55- if let Some ( ( branch_ref, branch_ref_name) ) = current_branch_name ( repo) {
56- context. insert ( "bencher.dev/branch/ref" . to_owned ( ) , branch_ref) ;
57- context. insert ( "bencher.dev/branch/ref/name" . to_owned ( ) , branch_ref_name) ;
58- }
52+ if let Some ( ( branch_ref, branch_ref_name) ) = branch_ref ( & repo) {
53+ context. insert ( "/branch/ref" , branch_ref) ;
54+ context. insert ( "/branch/ref/name" , branch_ref_name) ;
55+ }
5956
60- if let Some ( hash) = current_branch_hash ( repo) {
61- context. insert ( "bencher.dev/branch/hash" . to_owned ( ) , hash) ;
62- }
57+ if let Some ( hash) = branch_hash ( & repo) {
58+ context. insert ( "/branch/hash" , hash) ;
6359 }
60+ }
61+
62+ fn platform_context ( context : & mut ReportContext ) {
63+ context. insert ( "/testbed/os" , OperatingSystem :: current ( ) . to_string ( ) ) ;
6464
65- let fingerprint = Fingerprint :: new ( ) ;
66- if let Some ( fingerprint) = fingerprint {
67- context. insert (
68- "bencher.dev/testbed/fingerprint" . to_owned ( ) ,
69- fingerprint. to_string ( ) ,
70- ) ;
65+ if let Some ( fingerprint) = Fingerprint :: current ( ) {
66+ context. insert ( "/testbed/fingerprint" , fingerprint. to_string ( ) ) ;
7167 }
68+ }
7269
73- context
70+ fn find_repo ( ) -> Option < Repository > {
71+ let current_dir = std:: env:: current_dir ( ) . ok ( ) ?;
72+ for directory in current_dir. ancestors ( ) {
73+ if let Ok ( repo) = gix:: open ( directory) {
74+ return Some ( repo) ;
75+ }
76+ }
77+ None
7478}
7579
76- fn repo_name ( repo : Option < & Repository > ) -> Option < String > {
77- let repo = repo?;
80+ fn repo_name ( repo : & Repository ) -> Option < String > {
7881 let Some ( parent) = repo. path ( ) . parent ( ) else {
7982 return Some ( ROOT . to_owned ( ) ) ;
8083 } ;
8184 let file_name = parent. file_name ( ) ?;
8285 file_name. to_str ( ) . map ( ToOwned :: to_owned)
8386}
8487
85- fn current_branch_name ( repo : & Repository ) -> Option < ( String , String ) > {
88+ fn repo_hash ( repo : & Repository ) -> Option < String > {
89+ let head_id = repo. head_id ( ) . ok ( ) ?;
90+ let rev_walk = repo. rev_walk ( [ head_id] ) . all ( ) . ok ( ) ?;
91+ if let Some ( Ok ( commit) ) = rev_walk. last ( ) {
92+ Some ( commit. id ( ) . object ( ) . ok ( ) ?. id . to_string ( ) )
93+ } else {
94+ None
95+ }
96+ }
97+
98+ fn branch_ref ( repo : & Repository ) -> Option < ( String , String ) > {
8699 repo. head ( ) . ok ( ) ?. referent_name ( ) . map ( |name| {
87100 (
88101 String :: from_utf8_lossy ( name. as_bstr ( ) ) . to_string ( ) ,
@@ -91,18 +104,8 @@ fn current_branch_name(repo: &Repository) -> Option<(String, String)> {
91104 } )
92105}
93106
94- fn current_branch_hash ( repo : & Repository ) -> Option < String > {
107+ fn branch_hash ( repo : & Repository ) -> Option < String > {
95108 let head_id = repo. head_id ( ) . ok ( ) ?;
96109 let head_object = head_id. object ( ) . ok ( ) ?;
97110 Some ( head_object. id . to_string ( ) )
98111}
99-
100- fn find_default_branch_and_root_commit ( repo : & Repository ) -> Option < String > {
101- let head_id = repo. head_id ( ) . ok ( ) ?;
102- let rev_walk = repo. rev_walk ( [ head_id] ) . all ( ) . ok ( ) ?;
103- if let Some ( Ok ( commit) ) = rev_walk. last ( ) {
104- Some ( commit. id ( ) . object ( ) . ok ( ) ?. id . to_string ( ) )
105- } else {
106- None
107- }
108- }
0 commit comments