Skip to content

Commit ee083de

Browse files
authored
Merge pull request #4849 from Byron/nicer-clone
refactor clone for clarity
2 parents d1462f1 + a4df70c commit ee083de

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

crates/gitbutler-tauri/src/error.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,47 @@
1313
//!
1414
//! The values in these fields are controlled by attaching context, please [see the `error` docs](gitbutler_error::error))
1515
//! on how to do this.
16-
pub(crate) use frontend::Error;
16+
pub(crate) use frontend::{Error, UnmarkedError};
1717

1818
mod frontend {
1919
use std::borrow::Cow;
2020

2121
use gitbutler_error::error::AnyhowContextExt;
2222
use serde::{ser::SerializeMap, Serialize};
2323

24+
/// An error type for serialization which isn't expected to carry a code.
25+
#[derive(Debug)]
26+
pub struct UnmarkedError(anyhow::Error);
27+
28+
impl<T> From<T> for UnmarkedError
29+
where
30+
T: std::error::Error + Send + Sync + 'static,
31+
{
32+
fn from(err: T) -> Self {
33+
Self(err.into())
34+
}
35+
}
36+
37+
impl Serialize for UnmarkedError {
38+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
39+
where
40+
S: serde::Serializer,
41+
{
42+
let ctx = self.0.custom_context_or_root_cause();
43+
44+
let mut map = serializer.serialize_map(Some(2))?;
45+
map.serialize_entry("code", &ctx.code.to_string())?;
46+
let message = ctx.message.unwrap_or_else(|| {
47+
self.0
48+
.source()
49+
.map(|err| Cow::Owned(err.to_string()))
50+
.unwrap_or_else(|| Cow::Borrowed("Something went wrong"))
51+
});
52+
map.serialize_entry("message", &message)?;
53+
map.end()
54+
}
55+
}
56+
2457
/// An error type for serialization, dynamically extracting context information during serialization,
2558
/// meant for consumption by the frontend.
2659
#[derive(Debug)]

crates/gitbutler-tauri/src/repo.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
pub mod commands {
2-
use anyhow::{Context, Result};
2+
use anyhow::Result;
33
use gitbutler_branch_actions::RemoteBranchFile;
44
use gitbutler_project as projects;
55
use gitbutler_project::ProjectId;
66
use gitbutler_repo::RepoCommands;
7-
use gix::progress::Discard;
87
use std::path::Path;
98
use std::sync::atomic::AtomicBool;
109
use tauri::State;
1110
use tracing::instrument;
1211

13-
use crate::error::Error;
12+
use crate::error::{Error, UnmarkedError};
1413

1514
#[tauri::command(async)]
1615
#[instrument(skip(projects), err(Debug))]
@@ -46,19 +45,16 @@ pub mod commands {
4645
}
4746

4847
#[tauri::command(async)]
49-
pub fn git_clone_repository(repository_url: &str, target_dir: &Path) -> Result<(), Error> {
50-
let url =
51-
gix::url::parse(repository_url.into()).context("Failed to parse repository URL")?;
48+
pub fn git_clone_repository(
49+
repository_url: &str,
50+
target_dir: &Path,
51+
) -> Result<(), UnmarkedError> {
5252
let should_interrupt = AtomicBool::new(false);
53-
let mut prepared_clone =
54-
gix::prepare_clone(url, target_dir).context("Failed to prepare clone")?;
55-
let (mut prepared_checkout, _) = prepared_clone
56-
.fetch_then_checkout(Discard, &should_interrupt)
57-
.context("Failed to fetch")?;
58-
let should_interrupt = AtomicBool::new(false);
59-
prepared_checkout
60-
.main_worktree(Discard, &should_interrupt)
61-
.context("Failed to checkout main worktree")?;
53+
54+
gix::prepare_clone(repository_url, target_dir)?
55+
.fetch_then_checkout(gix::progress::Discard, &should_interrupt)
56+
.map(|(checkout, _outcome)| checkout)?
57+
.main_worktree(gix::progress::Discard, &should_interrupt)?;
6258
Ok(())
6359
}
6460

0 commit comments

Comments
 (0)