@@ -243,6 +243,25 @@ pub fn get_branch_remote(
243
243
}
244
244
}
245
245
246
+ /// Retrieve the upstream merge of a local `branch`,
247
+ /// configured in "branch.*.merge"
248
+ ///
249
+ /// For details check git2 `branch_upstream_merge`
250
+ pub fn get_branch_upstream_merge (
251
+ repo_path : & RepoPath ,
252
+ branch : & str ,
253
+ ) -> Result < Option < String > > {
254
+ let repo = repo ( repo_path) ?;
255
+ let branch = repo. find_branch ( branch, BranchType :: Local ) ?;
256
+ let reference = bytes2string ( branch. get ( ) . name_bytes ( ) ) ?;
257
+ let remote_name = repo. branch_upstream_merge ( & reference) . ok ( ) ;
258
+ if let Some ( remote_name) = remote_name {
259
+ Ok ( Some ( bytes2string ( remote_name. as_ref ( ) ) ?) )
260
+ } else {
261
+ Ok ( None )
262
+ }
263
+ }
264
+
246
265
/// returns whether the pull merge strategy is set to rebase
247
266
pub fn config_is_pull_rebase ( repo_path : & RepoPath ) -> Result < bool > {
248
267
let repo = repo ( repo_path) ?;
@@ -673,6 +692,49 @@ mod tests_branches {
673
692
674
693
assert ! ( get_branch_remote( repo_path, "foo" ) . is_err( ) ) ;
675
694
}
695
+
696
+ #[ test]
697
+ fn test_branch_no_upstream_merge_config ( ) {
698
+ let ( _r, repo) = repo_init ( ) . unwrap ( ) ;
699
+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
700
+ let repo_path: & RepoPath =
701
+ & root. as_os_str ( ) . to_str ( ) . unwrap ( ) . into ( ) ;
702
+
703
+ let upstream_merge_res =
704
+ get_branch_upstream_merge ( & repo_path, "master" ) ;
705
+ assert ! (
706
+ upstream_merge_res. is_ok_and( |v| v. as_ref( ) . is_none( ) )
707
+ ) ;
708
+ }
709
+
710
+ #[ test]
711
+ fn test_branch_with_upstream_merge_config ( ) {
712
+ let ( _r, repo) = repo_init ( ) . unwrap ( ) ;
713
+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
714
+ let repo_path: & RepoPath =
715
+ & root. as_os_str ( ) . to_str ( ) . unwrap ( ) . into ( ) ;
716
+
717
+ let branch_name = "master" ;
718
+ let upstrem_merge = "refs/heads/master" ;
719
+
720
+ let mut config = repo. config ( ) . unwrap ( ) ;
721
+ config
722
+ . set_str (
723
+ & format ! ( "branch.{branch_name}.merge" ) ,
724
+ & upstrem_merge,
725
+ )
726
+ . expect ( "fail set branch merge config" ) ;
727
+
728
+ let upstream_merge_res =
729
+ get_branch_upstream_merge ( & repo_path, & branch_name) ;
730
+ assert ! ( upstream_merge_res
731
+ . as_ref( )
732
+ . is_ok_and( |v| v. as_ref( ) . is_some( ) ) ) ;
733
+ assert_eq ! (
734
+ & upstream_merge_res. unwrap( ) . unwrap( ) ,
735
+ upstrem_merge
736
+ ) ;
737
+ }
676
738
}
677
739
678
740
#[ cfg( test) ]
0 commit comments