Skip to main content

git_workty/commands/
list.rs

1use crate::git::GitRepo;
2use crate::status::WorktreeStatus;
3use crate::status::{get_all_statuses, get_all_statuses_fast};
4use crate::ui::{print_worktree_list, UiOptions};
5use crate::worktree::{list_worktrees, Worktree};
6use anyhow::Result;
7use std::path::PathBuf;
8
9pub fn execute(repo: &GitRepo, opts: &UiOptions, fast: bool) -> Result<()> {
10    let worktrees = list_worktrees(repo)?;
11    let statuses = if fast {
12        get_all_statuses_fast(repo, &worktrees)
13    } else {
14        get_all_statuses(repo, &worktrees)
15    };
16
17    let current_path = std::env::current_dir().unwrap_or_else(|_| PathBuf::new());
18
19    let sorted = sort_worktrees(statuses, &current_path);
20
21    print_worktree_list(repo, &sorted, &current_path, opts);
22
23    Ok(())
24}
25
26fn sort_worktrees(
27    mut worktrees: Vec<(Worktree, WorktreeStatus)>,
28    current_path: &PathBuf,
29) -> Vec<(Worktree, WorktreeStatus)> {
30    worktrees.sort_by(|(a, status_a), (b, status_b)| {
31        let a_is_current = a.path == *current_path;
32        let b_is_current = b.path == *current_path;
33
34        if a_is_current != b_is_current {
35            return b_is_current.cmp(&a_is_current);
36        }
37
38        let a_dirty = status_a.is_dirty();
39        let b_dirty = status_b.is_dirty();
40
41        if a_dirty != b_dirty {
42            return b_dirty.cmp(&a_dirty);
43        }
44
45        a.name().cmp(b.name())
46    });
47
48    worktrees
49}