git_workty/commands/
list.rs

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