1- use bencher_json:: { Fingerprint , GitHash } ;
1+ use bencher_json:: { project :: report :: JsonReportContext , Fingerprint , GitHash } ;
22use gix:: Repository ;
33
44use super :: branch:: find_repo;
55
66const ROOT : & str = "root" ;
77
8- pub fn context ( ) {
8+ pub fn get_context ( ) -> Option < JsonReportContext > {
9+ let mut context = JsonReportContext :: new ( ) ;
10+
911 let repo = find_repo ( ) ;
1012 let repo_name = repo_name ( repo. as_ref ( ) ) ;
1113 if let Some ( repo_name) = repo_name {
12- println ! ( "{repo_name}" ) ;
14+ context. insert ( "bencher.dev/repo/name" . to_owned ( ) , Some ( repo_name) ) ;
15+ }
16+ if let Some ( repo) = & repo {
17+ if let Some ( root_commit) = find_default_branch_and_root_commit ( & repo) {
18+ context. insert (
19+ "bencher.dev/repo/hash" . to_owned ( ) ,
20+ Some ( root_commit. to_string ( ) ) ,
21+ ) ;
22+ }
1323 }
1424
1525 if let Some ( repo) = repo {
16- if let Some ( branch) = current_branch_name ( & repo) {
17- println ! ( "{branch:?}" ) ;
26+ if let Some ( ( branch_ref, branch_ref_name) ) = current_branch_name ( & repo) {
27+ context. insert ( "bencher.dev/branch/ref" . to_owned ( ) , Some ( branch_ref) ) ;
28+ context. insert (
29+ "bencher.dev/branch/ref/name" . to_owned ( ) ,
30+ Some ( branch_ref_name) ,
31+ ) ;
1832 }
1933
20- if let Some ( root_commit ) = find_default_branch_and_root_commit ( & repo) {
21- println ! ( "Root commit hash: {root_commit:?}" ) ;
34+ if let Some ( hash ) = current_branch_hash ( & repo) {
35+ context . insert ( "bencher.dev/branch/ hash" . to_owned ( ) , Some ( hash ) ) ;
2236 }
2337 }
2438
2539 let fingerprint = Fingerprint :: new ( ) ;
2640 if let Some ( fingerprint) = fingerprint {
27- println ! ( "{fingerprint}" ) ;
41+ context. insert (
42+ "bencher.dev/testbed/fingerprint" . to_owned ( ) ,
43+ Some ( fingerprint. to_string ( ) ) ,
44+ ) ;
2845 }
46+
47+ Some ( context)
2948}
3049
3150fn repo_name ( repo : Option < & Repository > ) -> Option < String > {
@@ -46,6 +65,12 @@ fn current_branch_name(repo: &Repository) -> Option<(String, String)> {
4665 } )
4766}
4867
68+ fn current_branch_hash ( repo : & Repository ) -> Option < String > {
69+ let head_id = repo. head_id ( ) . ok ( ) ?;
70+ let head_object = head_id. object ( ) . ok ( ) ?;
71+ Some ( head_object. id . to_string ( ) )
72+ }
73+
4974fn find_default_branch_and_root_commit ( repo : & Repository ) -> Option < GitHash > {
5075 let head_id = repo. head_id ( ) . ok ( ) ?;
5176 let rev_walk = repo. rev_walk ( [ head_id] ) . all ( ) . ok ( ) ?;
0 commit comments