order branch listing by last modified

also fixes an error in last modified computation
This commit is contained in:
Kiril Videlov 2024-07-28 19:35:54 +02:00
parent 2d0fa99452
commit 3033c7e7f5
No known key found for this signature in database
GPG Key ID: A4C733025427C471
2 changed files with 8 additions and 11 deletions

View File

@ -1,4 +1,5 @@
import { invoke } from '$lib/backend/ipc'; import { invoke } from '$lib/backend/ipc';
import { Transform } from 'class-transformer';
import { plainToInstance } from 'class-transformer'; import { plainToInstance } from 'class-transformer';
export class BranchListingService { export class BranchListingService {
@ -47,6 +48,7 @@ export class BranchListing {
numberOfCommits!: number; numberOfCommits!: number;
/// Timestamp in milliseconds since the branch was last updated. /// Timestamp in milliseconds since the branch was last updated.
/// This includes any commits, uncommited changes or even updates to the branch metadata (e.g. renaming). /// This includes any commits, uncommited changes or even updates to the branch metadata (e.g. renaming).
@Transform((obj) => new Date(obj.value))
updatedAt!: number; updatedAt!: number;
/// A list of authors that have contributes commits to this branch. /// A list of authors that have contributes commits to this branch.
/// In the case of multiple remote tracking branches, it takes the full list of unique authors. /// In the case of multiple remote tracking branches, it takes the full list of unique authors.

View File

@ -16,12 +16,11 @@ use gitbutler_command_context::ProjectRepository;
use crate::{VirtualBranch, VirtualBranchesExt}; use crate::{VirtualBranch, VirtualBranchesExt};
use gitbutler_reference::normalize_branch_name; use gitbutler_reference::normalize_branch_name;
use gitbutler_repo::RepoActionsExt; use gitbutler_repo::RepoActionsExt;
use itertools::Itertools;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
/// Returns a list of branches associated with this project. /// Returns a list of branches associated with this project.
// TODO: Implement pagination for this thing
// TODO: The results should be sortedb by updated_at
pub fn list_branches( pub fn list_branches(
ctx: &ProjectRepository, ctx: &ProjectRepository,
filter: Option<BranchListingFilter>, filter: Option<BranchListingFilter>,
@ -75,6 +74,7 @@ pub fn list_branches(
let branches: Vec<BranchListing> = branches let branches: Vec<BranchListing> = branches
.into_iter() .into_iter()
.filter(|branch| matches_all(branch, &filter)) .filter(|branch| matches_all(branch, &filter))
.sorted_by(|a, b| b.updated_at.cmp(&a.updated_at))
.collect(); .collect();
Ok(branches) Ok(branches)
} }
@ -214,6 +214,10 @@ fn branch_group_to_branch(
.map(|vb| normalize_branch_name(&vb.name)) .map(|vb| normalize_branch_name(&vb.name))
.unwrap_or_default(), .unwrap_or_default(),
); );
let last_modified_ms = max(
(repo.find_commit(head)?.time().seconds() * 1000) as u128,
virtual_branch.map_or(0, |x| x.updated_timestamp_ms),
);
let repo_head = repo.head()?.peel_to_commit()?; let repo_head = repo.head()?.peel_to_commit()?;
// If no merge base can be found, return with zero stats // If no merge base can be found, return with zero stats
let branch = if let Ok(base) = repo.merge_base(repo_head.id(), head) { let branch = if let Ok(base) = repo.merge_base(repo_head.id(), head) {
@ -222,14 +226,11 @@ fn branch_group_to_branch(
revwalk.hide(base)?; revwalk.hide(base)?;
let mut commits = Vec::new(); let mut commits = Vec::new();
let mut authors = HashSet::new(); let mut authors = HashSet::new();
let mut last_commit_time_ms = i64::MIN;
for oid in revwalk { for oid in revwalk {
let commit = repo.find_commit(oid?)?; let commit = repo.find_commit(oid?)?;
last_commit_time_ms = max(last_commit_time_ms, commit.time().seconds() * 1000);
authors.insert(commit.author().into()); authors.insert(commit.author().into());
commits.push(commit); commits.push(commit);
} }
// If there are no commits (i.e. virtual branch only) it is considered the users own // If there are no commits (i.e. virtual branch only) it is considered the users own
let own_branch = commits.is_empty() let own_branch = commits.is_empty()
|| commits.iter().any(|commit| { || commits.iter().any(|commit| {
@ -238,11 +239,6 @@ fn branch_group_to_branch(
&& local_author.email_bytes() == commit_author.email_bytes() && local_author.email_bytes() == commit_author.email_bytes()
}); });
let last_modified_ms = max(
last_commit_time_ms as u128,
virtual_branch.map_or(0, |x| x.updated_timestamp_ms),
);
BranchListing { BranchListing {
name: identity, name: identity,
remotes, remotes,
@ -253,7 +249,6 @@ fn branch_group_to_branch(
own_branch, own_branch,
} }
} else { } else {
let last_modified_ms = (repo.find_commit(head)?.time().seconds() * 1000) as u128;
BranchListing { BranchListing {
name: identity, name: identity,
remotes, remotes,