From 444564a43d09a8813779e1cdb3cb1965ea18b609 Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Mon, 19 Oct 2020 00:51:57 +0530 Subject: [PATCH] Use upstream remote URL for the currently checked out branch Signed-off-by: ATechnoHazard --- src/main.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 580cd12..f448041 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,13 +31,25 @@ fn main() -> anyhow::Result<()> { let head = repo.head()?; if let Some(head) = head.name() { - repos.push(PersistableRepo { - // Ideally we wanna do this, but it moves `dir`. - // path: entry.path().to_string_lossy().strip_prefix(dir).unwrap().to_string(), - path: entry.path().to_string_lossy().to_string(), - remote_url: repo.remotes()?.get(0).unwrap_or("None").to_string(), - head: head.to_string(), - }); + if let Ok(upstream) = repo.branch_upstream_name(head) { + if let Ok(remote) = repo.find_remote( + // This is a rather ugly hack, but not sure how else to get the required name + // doesn't seem to work with the full name such as `refs/remotes/origin/master` + upstream + .as_str() + .unwrap_or("None") + .split("/") + .collect::>()[2], + ) { + repos.push(PersistableRepo { + // Ideally we wanna do this, but it moves `dir`. + // path: entry.path().to_string_lossy().strip_prefix(dir).unwrap().to_string(), + path: entry.path().to_string_lossy().to_string(), + remote_url: remote.url().unwrap_or("None").to_owned(), + head: head.to_owned(), + }); + } + } }; } };