gitkraft 0.7.2

GitKraft — Git IDE desktop application (Iced GUI)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Async command helpers for repository operations.
//!
//! Each function returns an `iced::Task<Message>` that performs blocking git
//! work on a background thread via the `git_task!` macro, then maps the
//! result into a [`Message`] variant the update loop can handle.
//!
//! This module also contains async wrappers for persistence operations
//! (`record_repo_opened`, `load_settings`, `save_theme`) so that redb
//! database I/O never blocks the UI thread.

use std::path::PathBuf;

use iced::Task;

use crate::message::{Message, RepoPayload};

/// Open a folder-picker dialog and return the selected path.
pub fn pick_folder_open() -> Task<Message> {
    Task::perform(
        async {
            let handle = rfd::AsyncFileDialog::new()
                .set_title("Open Git Repository")
                .pick_folder()
                .await;
            handle.map(|h| h.path().to_path_buf())
        },
        Message::RepoSelected,
    )
}

/// Open a folder-picker dialog for initialising a new repository.
pub fn pick_folder_init() -> Task<Message> {
    Task::perform(
        async {
            let handle = rfd::AsyncFileDialog::new()
                .set_title("Choose folder for new repository")
                .pick_folder()
                .await;
            handle.map(|h| h.path().to_path_buf())
        },
        Message::RepoInitSelected,
    )
}

/// Load (open) a repository at `path` and gather all initial state.
pub fn load_repo(path: PathBuf) -> Task<Message> {
    git_task!(Message::RepoOpened, load_repo_blocking(&path))
}

/// Initialise a new repository at `path` and then load it.
pub fn init_repo(path: PathBuf) -> Task<Message> {
    git_task!(
        Message::RepoOpened,
        (|| {
            gitkraft_core::features::repo::init_repo(&path).map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Refresh only the staging area (unstaged + staged diffs) — lightweight.
pub fn refresh_staging_only(path: PathBuf) -> Task<Message> {
    git_task!(
        Message::StagingUpdated,
        (|| {
            let repo = open_repo!(&path);
            let unstaged = gitkraft_core::features::diff::get_working_dir_diff(&repo)
                .map_err(|e| e.to_string())?;
            let staged =
                gitkraft_core::features::diff::get_staged_diff(&repo).map_err(|e| e.to_string())?;
            Ok(crate::message::StagingPayload { unstaged, staged })
        })()
    )
}

/// Refresh all data for an already-open repository.
pub fn refresh_repo(path: PathBuf) -> Task<Message> {
    git_task!(Message::RepoRefreshed, load_repo_blocking(&path))
}

/// Blocking helper shared by `load_repo` and `refresh_repo`.
///
/// Opens the repository and collects every piece of state the UI needs into a
/// single [`RepoPayload`].
fn load_repo_blocking(path: &std::path::Path) -> Result<RepoPayload, String> {
    // Open the repository once and reuse the handle for every operation.
    // `list_stashes` needs `&mut`, so we declare the binding as `mut`.
    let mut repo = open_repo!(path);

    let info = gitkraft_core::features::repo::get_repo_info(&repo).map_err(|e| e.to_string())?;
    let branches =
        gitkraft_core::features::branches::list_branches(&repo).map_err(|e| e.to_string())?;
    let commits =
        gitkraft_core::features::commits::list_commits(&repo, 500).map_err(|e| e.to_string())?;
    let graph_rows = gitkraft_core::features::graph::build_graph(&commits);
    let unstaged =
        gitkraft_core::features::diff::get_working_dir_diff(&repo).map_err(|e| e.to_string())?;
    let staged =
        gitkraft_core::features::diff::get_staged_diff(&repo).map_err(|e| e.to_string())?;
    let remotes =
        gitkraft_core::features::remotes::list_remotes(&repo).map_err(|e| e.to_string())?;
    let stashes =
        gitkraft_core::features::stash::list_stashes(&mut repo).map_err(|e| e.to_string())?;

    Ok(RepoPayload {
        info,
        branches,
        commits,
        graph_rows,
        unstaged,
        staged,
        stashes,
        remotes,
    })
}

/// Get the working directory of a repository, returning a user-friendly error
/// for bare repositories.
fn workdir(path: &std::path::Path) -> Result<std::path::PathBuf, String> {
    let repo = open_repo!(path);
    repo.workdir()
        .map(|p| p.to_path_buf())
        .ok_or_else(|| "bare repository has no working directory".to_string())
}

// ── Context-menu git commands ─────────────────────────────────────────────────

/// Push `branch` to `remote` then reload the repo.
pub fn push_branch_async(path: PathBuf, branch: String, remote: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::branches::push_branch(&wd, &branch, &remote)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Pull the current branch from `remote` with `--rebase` then reload.
pub fn pull_rebase_async(path: PathBuf, remote: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::branches::pull_rebase(&wd, &remote)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Rebase current HEAD onto `target` (branch name or OID) then reload.
pub fn rebase_onto_async(path: PathBuf, target: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::branches::rebase_onto(&wd, &target)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Rename a local branch then reload.
pub fn rename_branch_async(path: PathBuf, old_name: String, new_name: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let repo = open_repo!(&path);
            gitkraft_core::features::branches::rename_branch(&repo, &old_name, &new_name)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Checkout a commit in detached HEAD mode then reload.
pub fn checkout_commit_async(path: PathBuf, oid: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let repo = open_repo!(&path);
            gitkraft_core::features::repo::checkout_commit_detached(&repo, &oid)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Revert a commit (`git revert --no-edit`) then reload.
pub fn revert_commit_async(path: PathBuf, oid: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::repo::revert_commit(&wd, &oid).map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Reset the current branch to `oid` using the given `mode`
/// (`"soft"`, `"mixed"`, or `"hard"`).
pub fn reset_to_commit_async(path: PathBuf, oid: String, mode: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::repo::reset_to_commit(&wd, &oid, &mode)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Merge `branch_name` into the current HEAD then reload.
pub fn merge_branch_async(path: PathBuf, branch_name: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let repo = open_repo!(&path);
            gitkraft_core::features::branches::merge_branch(&repo, &branch_name)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Delete a remote branch using `git push --delete`.
pub fn delete_remote_branch_async(path: PathBuf, full_name: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::branches::delete_remote_branch(&wd, &full_name)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Checkout a remote branch by creating a local tracking branch.
pub fn checkout_remote_branch_async(path: PathBuf, full_name: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::branches::checkout_remote_branch(&wd, &full_name)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Create a lightweight tag `name` pointing at `oid` then reload.
pub fn create_tag_async(path: PathBuf, name: String, oid: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let repo = open_repo!(&path);
            gitkraft_core::features::branches::create_tag(&repo, &name, &oid)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Create an annotated tag `name` with `message` pointing at `oid` then reload.
pub fn create_annotated_tag_async(
    path: PathBuf,
    name: String,
    message: String,
    oid: String,
) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let repo = open_repo!(&path);
            gitkraft_core::features::branches::create_annotated_tag(&repo, &name, &message, &oid)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

// ── Async persistence helpers ─────────────────────────────────────────────────

/// Record that a repo was opened and return the refreshed recent-repos list.
///
/// Runs `record_repo_opened` + `load_settings` on a background thread so that
/// the redb database I/O never blocks the Iced event loop.
pub fn record_repo_opened_async(path: std::path::PathBuf) -> Task<Message> {
    git_task!(
        Message::RepoRecorded,
        (|| {
            gitkraft_core::features::persistence::ops::record_repo_opened(&path)
                .map_err(|e| e.to_string())?;
            let settings = gitkraft_core::features::persistence::ops::load_settings()
                .map_err(|e| e.to_string())?;
            Ok(settings.recent_repos)
        })()
    )
}

/// Load the recent-repos list from persisted settings on a background thread.
pub fn load_recent_repos_async() -> Task<Message> {
    git_task!(
        Message::SettingsLoaded,
        (|| {
            let settings = gitkraft_core::features::persistence::ops::load_settings()
                .map_err(|e| e.to_string())?;
            Ok(settings.recent_repos)
        })()
    )
}

/// Save the theme preference on a background thread (fire-and-forget).
pub fn save_theme_async(theme_name: String) -> Task<Message> {
    git_task!(
        Message::ThemeSaved,
        gitkraft_core::features::persistence::ops::save_theme(&theme_name)
            .map_err(|e| e.to_string())
    )
}

/// Save layout preferences on a background thread (fire-and-forget).
pub fn save_layout_async(layout: gitkraft_core::LayoutSettings) -> Task<Message> {
    git_task!(
        Message::LayoutSaved,
        gitkraft_core::features::persistence::ops::save_layout(&layout).map_err(|e| e.to_string())
    )
}

/// Load layout preferences from persisted settings on a background thread.
pub fn load_layout_async() -> Task<Message> {
    git_task!(
        Message::LayoutLoaded,
        gitkraft_core::features::persistence::ops::get_saved_layout().map_err(|e| e.to_string())
    )
}

/// Load a repository at `path` directly into tab `tab_index`.
/// Used on startup to restore all saved tabs in parallel.
pub fn load_repo_at(tab_index: usize, path: PathBuf) -> Task<Message> {
    git_task!(
        move |result| Message::RepoRestoredAt(tab_index, result),
        load_repo_blocking(&path)
    )
}

/// Record a repo open AND save the full session in one DB write.
pub fn record_repo_and_save_session_async(
    path: PathBuf,
    open_tabs: Vec<PathBuf>,
    active_tab_index: usize,
) -> Task<Message> {
    git_task!(
        Message::RepoRecorded,
        gitkraft_core::features::persistence::ops::record_repo_and_save_session(
            &path,
            &open_tabs,
            active_tab_index,
        )
        .map_err(|e| e.to_string())
    )
}

/// Load the next page of commit history.
///
/// Fetches all commits up to `skip + count` from HEAD, rebuilds the full graph,
/// and returns a `CommitPage` for the update handler to swap in.
pub fn load_more_commits(path: PathBuf, skip: usize, count: usize) -> Task<Message> {
    let total = skip + count;
    git_task!(
        Message::MoreCommitsLoaded,
        (|| {
            let repo = open_repo!(&path);
            let commits = gitkraft_core::features::commits::list_commits(&repo, total)
                .map_err(|e| e.to_string())?;
            let graph_rows = gitkraft_core::features::graph::build_graph(&commits);
            Ok(crate::message::CommitPage {
                commits,
                graph_rows,
            })
        })()
    )
}

/// Persist the selected editor name (fire-and-forget).
pub fn save_editor_async(editor_name: String) -> Task<Message> {
    git_task!(
        Message::EditorSaved,
        gitkraft_core::features::persistence::save_editor(&editor_name).map_err(|e| e.to_string())
    )
}

/// Save the session (open tab paths + active tab index) asynchronously.
pub fn save_session_async(open_tabs: Vec<PathBuf>, active_tab_index: usize) -> Task<Message> {
    git_task!(
        Message::SessionSaved,
        gitkraft_core::features::persistence::ops::save_session(&open_tabs, active_tab_index)
            .map_err(|e| e.to_string())
    )
}