Skip to content

Commit e9b296a

Browse files
author
Stephan Dilly
committed
add remote name per branch
1 parent ae57cf3 commit e9b296a

File tree

1 file changed

+85
-5
lines changed
  • asyncgit/src/sync/branch

1 file changed

+85
-5
lines changed

asyncgit/src/sync/branch/mod.rs

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,40 @@ pub struct BranchInfo {
5858
pub is_head: bool,
5959
///
6060
pub has_upstream: bool,
61+
///
62+
pub remote: Option<String>,
6163
}
6264

6365
/// returns a list of `BranchInfo` with a simple summary of info about a single branch
6466
pub fn get_branches_info(repo_path: &str) -> Result<Vec<BranchInfo>> {
6567
scope_time!("get_branches_info");
6668

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
6971
.branches(Some(BranchType::Local))?
7072
.map(|b| {
7173
let branch = b?.0;
7274
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);
7384

7485
Ok(BranchInfo {
7586
name: bytes2string(branch.name_bytes()?)?,
76-
reference: bytes2string(branch.get().name_bytes())?,
87+
reference,
7788
top_commit_message: bytes2string(
7889
top_commit.summary_bytes().unwrap_or_default(),
7990
)?,
8091
top_commit: top_commit.id().into(),
8192
is_head: branch.is_head(),
82-
has_upstream: branch.upstream().is_ok(),
93+
has_upstream: upstream.is_ok(),
94+
remote,
8395
})
8496
})
8597
.filter_map(Result::ok)
@@ -297,7 +309,14 @@ mod tests_branch_compare {
297309
#[cfg(test)]
298310
mod tests_branches {
299311
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+
};
301320

302321
#[test]
303322
fn test_smoke() {
@@ -332,6 +351,67 @@ mod tests_branches {
332351
vec!["master", "test"]
333352
);
334353
}
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+
}
335415
}
336416

337417
#[cfg(test)]

0 commit comments

Comments
 (0)