mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-23 01:22:12 +03:00
in case of duplicates, prefer remote branches
This commit is contained in:
parent
360c7db73a
commit
0cc1b7ce79
@ -406,12 +406,12 @@ pub fn remote_branches(
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|branch| !branch.upstream.is_empty())
|
.filter(|branch| !branch.upstream.is_empty())
|
||||||
.map(|branch| branch.upstream.replace("refs/heads/", ""))
|
.map(|branch| branch.upstream.replace("refs/heads/", ""))
|
||||||
.collect::<Vec<_>>();
|
.collect::<HashSet<_>>();
|
||||||
|
|
||||||
println!("virtual branches: {:?}", virtual_branches);
|
println!("virtual branches: {:?}", virtual_branches);
|
||||||
|
|
||||||
let mut branches: Vec<RemoteBranch> = Vec::new();
|
let mut branches: Vec<RemoteBranch> = Vec::new();
|
||||||
let mut most_recent_branches: Vec<(git2::Branch, u64)> = Vec::new();
|
let mut most_recent_branches_by_hash: HashMap<git2::Oid, (git2::Branch, u64)> = HashMap::new();
|
||||||
|
|
||||||
for branch in repo.branches(None)? {
|
for branch in repo.branches(None)? {
|
||||||
let (branch, _) = branch?;
|
let (branch, _) = branch?;
|
||||||
@ -440,9 +440,8 @@ pub fn remote_branches(
|
|||||||
.context("could not get branch name")?
|
.context("could not get branch name")?
|
||||||
.to_string();
|
.to_string();
|
||||||
let branch_name = branch_name.replace("origin/", "");
|
let branch_name = branch_name.replace("origin/", "");
|
||||||
println!("branch name: {}", branch_name);
|
|
||||||
if virtual_branches.contains(&branch_name) {
|
if virtual_branches.contains(&branch_name) {
|
||||||
println!("skip");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if branch_name == "HEAD" {
|
if branch_name == "HEAD" {
|
||||||
@ -451,7 +450,32 @@ pub fn remote_branches(
|
|||||||
if branch_name == "gitbutler/temp" {
|
if branch_name == "gitbutler/temp" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
most_recent_branches.push((branch, seconds));
|
|
||||||
|
match most_recent_branches_by_hash.get(&branch_oid) {
|
||||||
|
Some((_, existing_seconds)) => {
|
||||||
|
let branch_name =
|
||||||
|
branch.get().name().context("could not get branch name")?;
|
||||||
|
if seconds < *existing_seconds {
|
||||||
|
// this branch is older than the one we already have
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if seconds > *existing_seconds {
|
||||||
|
most_recent_branches_by_hash.insert(branch_oid, (branch, seconds));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if branch_name.starts_with("refs/remotes") {
|
||||||
|
// this branch is a remote branch
|
||||||
|
// we always prefer the remote branch if it is the same age as the local branch
|
||||||
|
most_recent_branches_by_hash.insert(branch_oid, (branch, seconds));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// this is the first time we've seen this branch
|
||||||
|
// so we should add it to the list
|
||||||
|
most_recent_branches_by_hash.insert(branch_oid, (branch, seconds));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
continue;
|
continue;
|
||||||
@ -459,6 +483,9 @@ pub fn remote_branches(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut most_recent_branches: Vec<(git2::Branch, u64)> =
|
||||||
|
most_recent_branches_by_hash.into_values().collect();
|
||||||
|
|
||||||
// take the most recent 20 branches
|
// take the most recent 20 branches
|
||||||
most_recent_branches.sort_by(|a, b| b.1.cmp(&a.1)); // Sort by timestamp in descending order.
|
most_recent_branches.sort_by(|a, b| b.1.cmp(&a.1)); // Sort by timestamp in descending order.
|
||||||
let sorted_branches: Vec<git2::Branch> = most_recent_branches
|
let sorted_branches: Vec<git2::Branch> = most_recent_branches
|
||||||
|
Loading…
Reference in New Issue
Block a user