@@ -58,28 +58,40 @@ pub struct BranchInfo {
58
58
pub is_head : bool ,
59
59
///
60
60
pub has_upstream : bool ,
61
+ ///
62
+ pub remote : Option < String > ,
61
63
}
62
64
63
65
/// returns a list of `BranchInfo` with a simple summary of info about a single branch
64
66
pub fn get_branches_info ( repo_path : & str ) -> Result < Vec < BranchInfo > > {
65
67
scope_time ! ( "get_branches_info" ) ;
66
68
67
- let cur_repo = utils:: repo ( repo_path) ?;
68
- let branches_for_display = cur_repo
69
+ let repo = utils:: repo ( repo_path) ?;
70
+ let branches_for_display = repo
69
71
. branches ( Some ( BranchType :: Local ) ) ?
70
72
. map ( |b| {
71
73
let branch = b?. 0 ;
72
74
let top_commit = branch. get ( ) . peel_to_commit ( ) ?;
75
+ let reference = bytes2string ( branch. get ( ) . name_bytes ( ) ) ?;
76
+ let upstream = branch. upstream ( ) ;
77
+
78
+ let remote = repo
79
+ . branch_upstream_remote ( & reference)
80
+ . ok ( )
81
+ . as_ref ( )
82
+ . and_then ( |buf| buf. as_str ( ) )
83
+ . map ( String :: from) ;
73
84
74
85
Ok ( BranchInfo {
75
86
name : bytes2string ( branch. name_bytes ( ) ?) ?,
76
- reference : bytes2string ( branch . get ( ) . name_bytes ( ) ) ? ,
87
+ reference,
77
88
top_commit_message : bytes2string (
78
89
top_commit. summary_bytes ( ) . unwrap_or_default ( ) ,
79
90
) ?,
80
91
top_commit : top_commit. id ( ) . into ( ) ,
81
92
is_head : branch. is_head ( ) ,
82
- has_upstream : branch. upstream ( ) . is_ok ( ) ,
93
+ has_upstream : upstream. is_ok ( ) ,
94
+ remote,
83
95
} )
84
96
} )
85
97
. filter_map ( Result :: ok)
@@ -297,7 +309,14 @@ mod tests_branch_compare {
297
309
#[ cfg( test) ]
298
310
mod tests_branches {
299
311
use super :: * ;
300
- use crate :: sync:: tests:: repo_init;
312
+ use crate :: sync:: {
313
+ remotes:: { get_remotes, push:: push} ,
314
+ rename_branch,
315
+ tests:: {
316
+ debug_cmd_print, repo_clone, repo_init, repo_init_bare,
317
+ write_commit_file,
318
+ } ,
319
+ } ;
301
320
302
321
#[ test]
303
322
fn test_smoke ( ) {
@@ -332,6 +351,67 @@ mod tests_branches {
332
351
vec![ "master" , "test" ]
333
352
) ;
334
353
}
354
+
355
+ fn clone_branch_commit_push ( target : & str , branch_name : & str ) {
356
+ let ( dir, repo) = repo_clone ( target) . unwrap ( ) ;
357
+ let dir = dir. path ( ) . to_str ( ) . unwrap ( ) ;
358
+
359
+ write_commit_file ( & repo, "f1.txt" , "foo" , "c1" ) ;
360
+ rename_branch ( dir, "refs/heads/master" , branch_name) . unwrap ( ) ;
361
+ push ( dir, "origin" , branch_name, false , None , None ) . unwrap ( ) ;
362
+ }
363
+
364
+ #[ test]
365
+ fn test_remotes_of_branches ( ) {
366
+ let ( r1_path, _remote1) = repo_init_bare ( ) . unwrap ( ) ;
367
+ let ( r2_path, _remote2) = repo_init_bare ( ) . unwrap ( ) ;
368
+ let ( _r, repo) = repo_init ( ) . unwrap ( ) ;
369
+
370
+ let r1_path = r1_path. path ( ) . to_str ( ) . unwrap ( ) ;
371
+ let r2_path = r2_path. path ( ) . to_str ( ) . unwrap ( ) ;
372
+
373
+ //Note: create those test branches in our remotes
374
+ clone_branch_commit_push ( r1_path, "r1branch" ) ;
375
+ clone_branch_commit_push ( r2_path, "r2branch" ) ;
376
+
377
+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
378
+ let repo_path = root. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
379
+
380
+ //add the remotes
381
+ repo. remote ( "r1" , r1_path) . unwrap ( ) ;
382
+ repo. remote ( "r2" , r2_path) . unwrap ( ) ;
383
+
384
+ //verify we got the remotes
385
+ let remotes = get_remotes ( repo_path) . unwrap ( ) ;
386
+ assert_eq ! (
387
+ remotes,
388
+ vec![ String :: from( "r1" ) , String :: from( "r2" ) ]
389
+ ) ;
390
+
391
+ //verify we got only master right now
392
+ let branches = get_branches_info ( repo_path) . unwrap ( ) ;
393
+ assert_eq ! ( branches. len( ) , 1 ) ;
394
+ assert_eq ! ( branches[ 0 ] . name, String :: from( "master" ) ) ;
395
+
396
+ //pull stuff from the two remotes
397
+ debug_cmd_print ( repo_path, "git pull r1" ) ;
398
+ debug_cmd_print ( repo_path, "git pull r2" ) ;
399
+
400
+ //create local tracking branches
401
+ debug_cmd_print (
402
+ repo_path,
403
+ "git checkout --track r1/r1branch" ,
404
+ ) ;
405
+ debug_cmd_print (
406
+ repo_path,
407
+ "git checkout --track r2/r2branch" ,
408
+ ) ;
409
+
410
+ let branches = get_branches_info ( repo_path) . unwrap ( ) ;
411
+ assert_eq ! ( branches. len( ) , 3 ) ;
412
+ assert_eq ! ( branches[ 1 ] . remote. as_ref( ) . unwrap( ) , "r1" ) ;
413
+ assert_eq ! ( branches[ 2 ] . remote. as_ref( ) . unwrap( ) , "r2" ) ;
414
+ }
335
415
}
336
416
337
417
#[ cfg( test) ]
0 commit comments