Skip to main content

gitkraft_gui/features/staging/
update.rs

1//! Update logic for staging-related messages.
2
3use iced::Task;
4
5use crate::message::Message;
6use crate::state::GitKraft;
7
8use super::commands;
9
10/// Handle all staging-related messages, returning a [`Task`] for any follow-up
11/// async work.
12pub fn update(state: &mut GitKraft, message: Message) -> Task<Message> {
13    match message {
14        Message::StageFile(path) => {
15            with_repo!(state, format!("Staging '{path}'…"), |repo_path| {
16                commands::stage_file(repo_path, path)
17            })
18        }
19
20        Message::UnstageFile(path) => {
21            with_repo!(state, format!("Unstaging '{path}'…"), |repo_path| {
22                commands::unstage_file(repo_path, path)
23            })
24        }
25
26        Message::StageAll => {
27            with_repo!(state, "Staging all files…".into(), |repo_path| {
28                commands::stage_all(repo_path)
29            })
30        }
31
32        Message::UnstageAll => {
33            with_repo!(state, "Unstaging all files…".into(), |repo_path| {
34                commands::unstage_all(repo_path)
35            })
36        }
37
38        Message::DiscardFile(path) => {
39            let tab = state.active_tab_mut();
40            tab.pending_discard = Some(path);
41            tab.status_message =
42                Some("Click discard again to confirm, or press elsewhere to cancel.".into());
43            Task::none()
44        }
45
46        Message::ConfirmDiscard(path) => {
47            with_repo!(
48                state,
49                format!("Discarding changes in '{path}'…"),
50                |repo_path| {
51                    state.active_tab_mut().pending_discard = None;
52                    commands::discard_file(repo_path, path)
53                }
54            )
55        }
56
57        Message::CancelDiscard => {
58            let tab = state.active_tab_mut();
59            tab.pending_discard = None;
60            tab.status_message = None;
61            Task::none()
62        }
63
64        Message::StagingUpdated(result) => {
65            let tab = state.active_tab_mut();
66            match result {
67                Ok(payload) => {
68                    tab.unstaged_changes = payload.unstaged;
69                    tab.staged_changes = payload.staged;
70                    tab.status_message = Some("Staging area updated.".into());
71                }
72                Err(e) => {
73                    tab.error_message = Some(format!("Staging operation failed: {e}"));
74                    tab.status_message = None;
75                }
76            }
77            Task::none()
78        }
79
80        _ => Task::none(),
81    }
82}