gitkraft 0.8.8

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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! 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 settings
//! file 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`].
pub(crate) fn load_repo_blocking(path: &std::path::Path) -> Result<RepoPayload, String> {
    gitkraft_core::load_repo_snapshot(path).map_err(|e| e.to_string())
}

/// 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)
        })()
    )
}

/// Cherry-pick a commit onto the current branch then reload.
pub fn cherry_pick_async(path: PathBuf, oid: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            gitkraft_core::features::repo::cherry_pick_commit(&wd, &oid)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Create a local branch at a specific commit OID then reload.
pub fn create_branch_at_commit_async(path: PathBuf, name: String, oid: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let repo = open_repo!(&path);
            gitkraft_core::features::branches::create_branch_at_commit(&repo, &name, &oid)
                .map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Load the commit history for a single file on a background thread.
pub fn file_history_async(repo_path: PathBuf, file_path: String) -> Task<Message> {
    git_task!(
        Message::FileHistoryLoaded,
        (|| {
            let repo = open_repo!(&repo_path);
            let commits =
                gitkraft_core::file_history(&repo, &file_path, 500).map_err(|e| e.to_string())?;
            Ok((file_path, commits))
        })()
    )
}

/// Load git-blame data for a single file on a background thread.
pub fn blame_file_async(repo_path: PathBuf, file_path: String) -> Task<Message> {
    git_task!(
        Message::FileBlameLoaded,
        (|| {
            let repo = open_repo!(&repo_path);
            let lines = gitkraft_core::blame_file(&repo, &file_path).map_err(|e| e.to_string())?;
            Ok((file_path, lines))
        })()
    )
}

/// Delete a file from the working directory then refresh the staging area.
pub fn delete_file_async(repo_path: PathBuf, file_path: String) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&repo_path)?;
            gitkraft_core::delete_file(&wd, &file_path).map_err(|e| e.to_string())?;
            load_repo_blocking(&repo_path)
        })()
    )
}

/// Execute any `CommitAction` against a specific commit then reload.
pub fn execute_commit_action_async(
    path: PathBuf,
    oid: String,
    action: gitkraft_core::CommitAction,
) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            action.execute(&wd, &oid).map_err(|e| e.to_string())?;
            load_repo_blocking(&path)
        })()
    )
}

/// Cherry-pick a list of commits (by OID) onto the current branch then reload.
pub fn cherry_pick_commits_async(path: PathBuf, oids: Vec<String>) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            for oid in &oids {
                gitkraft_core::features::repo::cherry_pick_commit(&wd, oid)
                    .map_err(|e| e.to_string())?;
            }
            load_repo_blocking(&path)
        })()
    )
}

/// Revert a list of commits (by OID) in order then reload.
pub fn revert_commits_async(path: PathBuf, oids: Vec<String>) -> Task<Message> {
    git_task!(
        Message::GitOperationResult,
        (|| {
            let wd = workdir(&path)?;
            for oid in &oids {
                gitkraft_core::features::repo::revert_commit(&wd, 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
/// settings file 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())
    )
}