From 54fad5969f5f0aa649cccb27493e48d7cf99f5b3 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 27 Jun 2023 14:20:36 +0200 Subject: [PATCH] List recent branches --- crates/collab_ui/src/branch_list.rs | 23 ++++++++++++++++++----- crates/fs/src/repository.rs | 9 ++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/crates/collab_ui/src/branch_list.rs b/crates/collab_ui/src/branch_list.rs index 7da984a599009a3a4fbf6bdab7c550964e48c7c2..146e4c804113490779f30acd71747599772369a6 100644 --- a/crates/collab_ui/src/branch_list.rs +++ b/crates/collab_ui/src/branch_list.rs @@ -2,7 +2,7 @@ use fuzzy::{StringMatch, StringMatchCandidate}; use gpui::{elements::*, AppContext, ModelHandle, MouseState, Task, ViewContext}; use picker::{Picker, PickerDelegate, PickerEvent}; use project::Project; -use std::sync::Arc; +use std::{cmp::Ordering, sync::Arc}; use util::{ResultExt, TryFutureExt}; pub fn init(cx: &mut AppContext) { @@ -67,9 +67,23 @@ impl PickerDelegate for BranchListDelegate { .path .to_path_buf(); cwd.push(".git"); - let branches = project.fs().open_repo(&cwd).unwrap().lock().branches(); - branches + let mut branches = project + .fs() + .open_repo(&cwd) .unwrap() + .lock() + .branches() + .unwrap(); + if query.is_empty() { + const RECENT_BRANCHES_COUNT: usize = 10; + // Do a partial sort to show recent-ish branches first. + branches.select_nth_unstable_by(RECENT_BRANCHES_COUNT, |lhs, rhs| { + rhs.unix_timestamp.cmp(&lhs.unix_timestamp) + }); + branches.truncate(RECENT_BRANCHES_COUNT); + branches.sort_unstable_by(|lhs, rhs| lhs.name.cmp(&rhs.name)); + } + branches .iter() .cloned() .enumerate() @@ -106,15 +120,14 @@ impl PickerDelegate for BranchListDelegate { picker .update(&mut cx, |picker, _| { let delegate = picker.delegate_mut(); - //delegate.branches = actions; delegate.matches = matches; - delegate.last_query = query; if delegate.matches.is_empty() { delegate.selected_index = 0; } else { delegate.selected_index = core::cmp::min(delegate.selected_index, delegate.matches.len() - 1); } + delegate.last_query = query; }) .log_err(); }) diff --git a/crates/fs/src/repository.rs b/crates/fs/src/repository.rs index 36df0dfb981b10a0d06f48c546d0945f802471be..0e5fd8343fa1a7f441e9d7474c4ea0a96c0df575 100644 --- a/crates/fs/src/repository.rs +++ b/crates/fs/src/repository.rs @@ -124,9 +124,16 @@ impl GitRepository for LibGitRepository { .filter_map(|branch| { branch.ok().and_then(|(branch, _)| { let name = branch.name().ok().flatten().map(Box::from)?; + let timestamp = branch.get().peel_to_commit().ok()?.time(); + let unix_timestamp = timestamp.seconds(); + let timezone_offset = timestamp.offset_minutes(); + let utc_offset = + time::UtcOffset::from_whole_seconds(timezone_offset * 60).ok()?; + let unix_timestamp = + time::OffsetDateTime::from_unix_timestamp(unix_timestamp).ok()?; Some(Branch { name, - unix_timestamp: None, + unix_timestamp: Some(unix_timestamp.to_offset(utc_offset).unix_timestamp()), }) }) })