Skip to main content

cgx_core/messages/
git.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use super::Message;
6use crate::git::GitSelector;
7
8/// Messages related to git operations.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(tag = "event", rename_all = "snake_case")]
11pub enum GitMessage {
12    /// About to check if the ref exists in the local bare repo
13    ResolvingRef { url: String, selector: GitSelector },
14    /// The ref was already present in the local bare repo (no fetch needed)
15    RefFoundLocally {
16        url: String,
17        selector: GitSelector,
18        commit: String,
19    },
20    /// Starting a network fetch because the ref was not present locally
21    FetchingRepo { url: String, selector: GitSelector },
22    /// The ref was resolved to a commit (only emitted after fetching)
23    ResolvedRef { commit: String },
24    /// Extracting a working tree from the bare repo
25    CheckingOut { commit: String, path: PathBuf },
26    /// Extraction completed (only emitted after [`CheckingOut`](Self::CheckingOut))
27    CheckoutComplete { path: PathBuf },
28    /// The checkout directory already exists (no extraction needed)
29    CheckoutExists { commit: String, path: PathBuf },
30}
31
32impl GitMessage {
33    pub fn resolving_ref(url: &str, selector: &GitSelector) -> Self {
34        Self::ResolvingRef {
35            url: url.to_string(),
36            selector: selector.clone(),
37        }
38    }
39
40    pub fn ref_found_locally(url: &str, selector: &GitSelector, commit: &str) -> Self {
41        Self::RefFoundLocally {
42            url: url.to_string(),
43            selector: selector.clone(),
44            commit: commit.to_string(),
45        }
46    }
47
48    pub fn fetching_repo(url: &str, selector: &GitSelector) -> Self {
49        Self::FetchingRepo {
50            url: url.to_string(),
51            selector: selector.clone(),
52        }
53    }
54
55    pub fn resolved_ref(commit: &str) -> Self {
56        Self::ResolvedRef {
57            commit: commit.to_string(),
58        }
59    }
60
61    pub fn checking_out(commit: &str, path: &std::path::Path) -> Self {
62        Self::CheckingOut {
63            commit: commit.to_string(),
64            path: path.to_path_buf(),
65        }
66    }
67
68    pub fn checkout_complete(path: &std::path::Path) -> Self {
69        Self::CheckoutComplete {
70            path: path.to_path_buf(),
71        }
72    }
73
74    pub fn checkout_exists(commit: &str, path: &std::path::Path) -> Self {
75        Self::CheckoutExists {
76            commit: commit.to_string(),
77            path: path.to_path_buf(),
78        }
79    }
80}
81
82impl From<GitMessage> for Message {
83    fn from(msg: GitMessage) -> Self {
84        Message::Git(msg)
85    }
86}