Skip to main content

agent_hub_runtime/commands/
project_git.rs

1use crate::core::{
2    error::AppError,
3    project_git::{self, GitStatus},
4    project_worktrees,
5    skill_store::SkillStore,
6};
7use std::sync::Arc;
8use tauri::State;
9
10#[derive(serde::Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct GitTagActionRequest {
13    action: project_git::GitTagAction,
14    tag: String,
15    expected_sha: Option<String>,
16    branch: Option<String>,
17    remote: Option<String>,
18}
19
20#[tauri::command]
21pub async fn run_project_git_tag_action(
22    store: State<'_, Arc<SkillStore>>,
23    project_id: String,
24    worktree_path: Option<String>,
25    request: GitTagActionRequest,
26) -> Result<(), AppError> {
27    let store = store.inner().clone();
28    tauri::async_runtime::spawn_blocking(move || {
29        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
30            .map_err(AppError::io)?;
31        project_git::tag_action(
32            &root,
33            request.action,
34            &request.tag,
35            request.expected_sha.as_deref(),
36            request.branch.as_deref(),
37            request.remote.as_deref(),
38        )
39        .map_err(AppError::io)
40    })
41    .await?
42}
43
44#[tauri::command]
45pub async fn run_project_git_action(
46    store: State<'_, Arc<SkillStore>>,
47    project_id: String,
48    worktree_path: Option<String>,
49    action: project_git::GitRepositoryAction,
50    branch: Option<String>,
51) -> Result<(), AppError> {
52    let store = store.inner().clone();
53    tauri::async_runtime::spawn_blocking(move || {
54        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
55            .map_err(AppError::io)?;
56        project_git::repository_action(&root, action, branch.as_deref()).map_err(AppError::io)
57    })
58    .await?
59}
60
61#[tauri::command]
62pub async fn get_project_git_remotes(
63    store: State<'_, Arc<SkillStore>>,
64    project_id: String,
65    worktree_path: Option<String>,
66) -> Result<Vec<project_git::GitRemote>, AppError> {
67    let store = store.inner().clone();
68    tauri::async_runtime::spawn_blocking(move || {
69        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
70            .map_err(AppError::io)?;
71        project_git::remotes(&root).map_err(AppError::io)
72    })
73    .await?
74}
75
76#[tauri::command]
77pub async fn get_project_git_status(
78    store: State<'_, Arc<SkillStore>>,
79    project_id: String,
80    worktree_path: Option<String>,
81) -> Result<Option<GitStatus>, AppError> {
82    let store = store.inner().clone();
83    tauri::async_runtime::spawn_blocking(move || {
84        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
85            .map_err(AppError::io)?;
86        project_git::inspect(&root).map_err(AppError::io)
87    })
88    .await?
89}
90
91#[tauri::command]
92pub async fn get_project_git_diff(
93    store: State<'_, Arc<SkillStore>>,
94    project_id: String,
95    worktree_path: Option<String>,
96    path: String,
97    staged: bool,
98) -> Result<String, AppError> {
99    let store = store.inner().clone();
100    tauri::async_runtime::spawn_blocking(move || {
101        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
102            .map_err(AppError::io)?;
103        project_git::diff(&root, &path, staged).map_err(AppError::io)
104    })
105    .await?
106}
107
108#[tauri::command]
109pub async fn set_project_git_staged(
110    store: State<'_, Arc<SkillStore>>,
111    project_id: String,
112    worktree_path: Option<String>,
113    paths: Vec<String>,
114    staged: bool,
115) -> Result<(), AppError> {
116    let store = store.inner().clone();
117    tauri::async_runtime::spawn_blocking(move || {
118        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
119            .map_err(AppError::io)?;
120        project_git::set_staged(&root, &paths, staged).map_err(AppError::io)
121    })
122    .await?
123}
124
125#[tauri::command]
126pub async fn discard_project_git_changes(
127    store: State<'_, Arc<SkillStore>>,
128    project_id: String,
129    worktree_path: Option<String>,
130    paths: Vec<String>,
131) -> Result<(), AppError> {
132    let store = store.inner().clone();
133    tauri::async_runtime::spawn_blocking(move || {
134        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
135            .map_err(AppError::io)?;
136        project_git::discard(&root, &paths).map_err(AppError::io)
137    })
138    .await?
139}
140
141#[tauri::command]
142pub async fn commit_project_git(
143    store: State<'_, Arc<SkillStore>>,
144    project_id: String,
145    worktree_path: Option<String>,
146    message: String,
147) -> Result<(), AppError> {
148    let store = store.inner().clone();
149    tauri::async_runtime::spawn_blocking(move || {
150        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
151            .map_err(AppError::io)?;
152        project_git::commit(&root, &message).map_err(AppError::io)
153    })
154    .await?
155}
156
157#[tauri::command]
158pub async fn get_project_git_commit(
159    store: State<'_, Arc<SkillStore>>,
160    project_id: String,
161    worktree_path: Option<String>,
162    sha: String,
163    path: Option<String>,
164) -> Result<project_git::GitCommitDetails, AppError> {
165    let store = store.inner().clone();
166    tauri::async_runtime::spawn_blocking(move || {
167        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
168            .map_err(AppError::io)?;
169        project_git::commit_details(&root, &sha, path.as_deref()).map_err(AppError::io)
170    })
171    .await?
172}
173
174#[tauri::command]
175pub async fn get_project_git_commit_diff(
176    store: State<'_, Arc<SkillStore>>,
177    project_id: String,
178    worktree_path: Option<String>,
179    sha: String,
180    path: String,
181    view: Option<String>,
182) -> Result<project_git::GitCommitFileView, AppError> {
183    let store = store.inner().clone();
184    tauri::async_runtime::spawn_blocking(move || {
185        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
186            .map_err(AppError::io)?;
187        project_git::commit_file_view(&root, &sha, &path, view.as_deref()).map_err(AppError::io)
188    })
189    .await?
190}
191
192#[tauri::command]
193pub async fn get_project_git_references(
194    store: State<'_, Arc<SkillStore>>,
195    project_id: String,
196    worktree_path: Option<String>,
197) -> Result<project_git::GitReferences, AppError> {
198    let store = store.inner().clone();
199    tauri::async_runtime::spawn_blocking(move || {
200        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
201            .map_err(AppError::io)?;
202        project_git::references(&root).map_err(AppError::io)
203    })
204    .await?
205}
206
207#[tauri::command]
208pub async fn get_project_git_history(
209    store: State<'_, Arc<SkillStore>>,
210    project_id: String,
211    worktree_path: Option<String>,
212    sha: String,
213    offset: usize,
214) -> Result<project_git::GitHistoryPage, AppError> {
215    let store = store.inner().clone();
216    tauri::async_runtime::spawn_blocking(move || {
217        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
218            .map_err(AppError::io)?;
219        project_git::commit_history(&root, &sha, offset).map_err(AppError::io)
220    })
221    .await?
222}
223
224#[tauri::command]
225pub async fn get_project_git_graph(
226    store: State<'_, Arc<SkillStore>>,
227    project_id: String,
228    worktree_path: Option<String>,
229    include_remotes: bool,
230    limit: usize,
231    branch: Option<String>,
232    path: Option<String>,
233) -> Result<project_git::GitGraph, AppError> {
234    let store = store.inner().clone();
235    tauri::async_runtime::spawn_blocking(move || {
236        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
237            .map_err(AppError::io)?;
238        match path {
239            Some(path) => project_git::graph_for_path(
240                &root,
241                include_remotes,
242                limit,
243                branch.as_deref(),
244                Some(&path),
245            ),
246            None => project_git::graph_for_branch(&root, include_remotes, limit, branch.as_deref()),
247        }
248        .map_err(AppError::io)
249    })
250    .await?
251}