Skip to content

Commit 71f02de

Browse files
committed
reimplement push and fetch mutations with the new three-mode setup
1 parent e47eb86 commit 71f02de

26 files changed

+564
-235
lines changed

src-tauri/src/callbacks.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ use anyhow::Result;
1111
use jj_lib::{git::RemoteCallbacks, repo::MutableRepo};
1212
use tauri::{Manager, Window};
1313

14-
use crate::{messages::InputRequest, worker::WorkerCallbacks, AppState};
14+
use crate::{
15+
messages::{InputField, InputRequest},
16+
worker::WorkerCallbacks,
17+
AppState,
18+
};
1519

1620
pub struct FrontendCallbacks(pub Window);
1721

@@ -29,7 +33,7 @@ impl WorkerCallbacks for FrontendCallbacks {
2933
let get_password = &mut |url: &str, username: &str| {
3034
self.request_input(
3135
format!("Please enter a password for {} at {}", username, url),
32-
["Password".into()],
36+
["Password"],
3337
)
3438
.and_then(|mut fields| fields.remove("Password"))
3539
};
@@ -38,7 +42,7 @@ impl WorkerCallbacks for FrontendCallbacks {
3842
let get_username_password = &mut |url: &str| {
3943
self.request_input(
4044
format!("Please enter a username and password for {}", url),
41-
["Username".into(), "Password".into()],
45+
["Username", "Password"],
4246
)
4347
.and_then(|mut fields| {
4448
fields.remove("Username").and_then(|username| {
@@ -52,10 +56,22 @@ impl WorkerCallbacks for FrontendCallbacks {
5256

5357
f(repo, cb)
5458
}
59+
60+
fn select_remote(&self, choices: &[&str]) -> Option<String> {
61+
let response = self.request_input(
62+
format!("Select a remote"),
63+
[InputField {
64+
label: "Select Remote".into(),
65+
choices: choices.iter().map(|choice| choice.to_string()).collect(),
66+
}],
67+
);
68+
69+
response.and_then(|mut fields| fields.remove("Select Remote").to_owned())
70+
}
5571
}
5672

5773
impl FrontendCallbacks {
58-
fn request_input<T: IntoIterator<Item = String>>(
74+
fn request_input<T: IntoIterator<Item = U>, U: Into<InputField>>(
5975
&self,
6076
detail: String,
6177
fields: T,
@@ -72,7 +88,7 @@ impl FrontendCallbacks {
7288
InputRequest {
7389
title: String::from("Git Login"),
7490
detail,
75-
fields: fields.into_iter().collect(),
91+
fields: fields.into_iter().map(|field| field.into()).collect(),
7692
},
7793
) {
7894
Ok(_) => (),

src-tauri/src/main.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use tauri_plugin_window_state::StateFlags;
2626

2727
use messages::{
2828
AbandonRevisions, CheckoutRevision, CopyChanges, CreateRef, CreateRevision, DeleteRef,
29-
DescribeRevision, DuplicateRevisions, FetchRemote, InputResponse, InsertRevision, MoveChanges,
30-
MoveRef, MoveRevision, MoveSource, MutationResult, PushRemote, RenameBranch, RevId,
29+
DescribeRevision, DuplicateRevisions, GitFetch, GitPush, InputResponse, InsertRevision,
30+
MoveChanges, MoveRef, MoveRevision, MoveSource, MutationResult, RenameBranch, RevId,
3131
TrackBranch, UndoOperation, UntrackBranch,
3232
};
3333
use worker::{Mutation, Session, SessionEvent, WorkerSession};
@@ -140,6 +140,7 @@ fn main() -> Result<()> {
140140
query_log,
141141
query_log_next_page,
142142
query_revision,
143+
query_remotes,
143144
checkout_revision,
144145
create_revision,
145146
insert_revision,
@@ -156,8 +157,8 @@ fn main() -> Result<()> {
156157
create_ref,
157158
delete_ref,
158159
move_ref,
159-
push_remote,
160-
fetch_remote,
160+
git_push,
161+
git_fetch,
161162
undo_operation
162163
])
163164
.menu(menu::build_main)
@@ -316,6 +317,27 @@ fn query_revision(
316317
.map_err(InvokeError::from_anyhow)
317318
}
318319

320+
#[tauri::command(async)]
321+
fn query_remotes(
322+
window: Window,
323+
app_state: State<AppState>,
324+
tracking_branch: Option<String>,
325+
) -> Result<Vec<String>, InvokeError> {
326+
let session_tx: Sender<SessionEvent> = app_state.get_session(window.label());
327+
let (call_tx, call_rx) = channel();
328+
329+
session_tx
330+
.send(SessionEvent::QueryRemotes {
331+
tx: call_tx,
332+
tracking_branch,
333+
})
334+
.map_err(InvokeError::from_error)?;
335+
call_rx
336+
.recv()
337+
.map_err(InvokeError::from_error)?
338+
.map_err(InvokeError::from_anyhow)
339+
}
340+
319341
#[tauri::command(async)]
320342
fn checkout_revision(
321343
window: Window,
@@ -461,19 +483,19 @@ fn move_ref(
461483
}
462484

463485
#[tauri::command(async)]
464-
fn push_remote(
486+
fn git_push(
465487
window: Window,
466488
app_state: State<AppState>,
467-
mutation: PushRemote,
489+
mutation: GitPush,
468490
) -> Result<MutationResult, InvokeError> {
469491
try_mutate(window, app_state, mutation)
470492
}
471493

472494
#[tauri::command(async)]
473-
fn fetch_remote(
495+
fn git_fetch(
474496
window: Window,
475497
app_state: State<AppState>,
476-
mutation: FetchRemote,
498+
mutation: GitFetch,
477499
) -> Result<MutationResult, InvokeError> {
478500
try_mutate(window, app_state, mutation)
479501
}

src-tauri/src/messages/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use queries::*;
88

99
use std::{collections::HashMap, path::Path};
1010

11+
use anyhow::{anyhow, Result};
1112
use chrono::{DateTime, FixedOffset, Local, LocalResult, TimeZone, Utc};
1213
use jj_lib::backend::{Signature, Timestamp};
1314
use serde::{Deserialize, Serialize};
@@ -143,6 +144,16 @@ pub enum StoreRef {
143144
},
144145
}
145146

147+
impl StoreRef {
148+
pub fn as_branch(&self) -> Result<&str> {
149+
match self {
150+
StoreRef::LocalBranch { branch_name, .. } => Ok(&branch_name),
151+
StoreRef::RemoteBranch { branch_name, .. } => Ok(&branch_name),
152+
_ => Err(anyhow!("not a local branch")),
153+
}
154+
}
155+
}
156+
146157
/// Refers to one of the repository's manipulatable objects
147158
#[derive(Serialize, Deserialize, Debug, Clone)]
148159
#[serde(tag = "type")]
@@ -182,7 +193,7 @@ pub enum Operand {
182193
pub struct InputRequest {
183194
pub title: String,
184195
pub detail: String,
185-
pub fields: Vec<String>,
196+
pub fields: Vec<InputField>,
186197
}
187198

188199
#[derive(Deserialize, Debug)]
@@ -195,3 +206,23 @@ pub struct InputResponse {
195206
pub cancel: bool,
196207
pub fields: HashMap<String, String>,
197208
}
209+
210+
#[derive(Serialize, Debug, Clone)]
211+
#[cfg_attr(
212+
feature = "ts-rs",
213+
derive(TS),
214+
ts(export, export_to = "../src/messages/")
215+
)]
216+
pub struct InputField {
217+
pub label: String,
218+
pub choices: Vec<String>,
219+
}
220+
221+
impl From<&str> for InputField {
222+
fn from(label: &str) -> Self {
223+
InputField {
224+
label: label.to_owned(),
225+
choices: vec![],
226+
}
227+
}
228+
}

src-tauri/src/messages/mutations.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,45 @@ pub struct MoveRef {
202202
pub to_id: RevId,
203203
}
204204

205+
/// XXX pushes all branches of a remote, all remotes of a branch, or one remotes of a branch - split up?
205206
#[derive(Deserialize, Debug)]
207+
#[serde(tag = "type")]
206208
#[cfg_attr(
207209
feature = "ts-rs",
208210
derive(TS),
209211
ts(export, export_to = "../src/messages/")
210212
)]
211-
pub struct PushRemote {
212-
pub remote_name: String,
213-
pub r#ref: Option<StoreRef>,
213+
pub enum GitPush {
214+
AllBranches {
215+
remote_name: String,
216+
},
217+
AllRemotes {
218+
branch_ref: StoreRef,
219+
},
220+
RemoteBranch {
221+
remote_name: String,
222+
branch_ref: StoreRef,
223+
},
214224
}
215225

216226
#[derive(Deserialize, Debug)]
227+
#[serde(tag = "type")]
217228
#[cfg_attr(
218229
feature = "ts-rs",
219230
derive(TS),
220231
ts(export, export_to = "../src/messages/")
221232
)]
222-
pub struct FetchRemote {
223-
pub remote_name: String,
224-
pub r#ref: Option<StoreRef>,
233+
pub enum GitFetch {
234+
AllBranches {
235+
remote_name: String,
236+
},
237+
AllRemotes {
238+
branch_ref: StoreRef,
239+
},
240+
RemoteBranch {
241+
remote_name: String,
242+
branch_ref: StoreRef,
243+
},
225244
}
226245

227246
#[derive(Deserialize, Debug)]

src-tauri/src/worker/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub trait WorkerCallbacks {
4545
repo: &mut MutableRepo,
4646
f: &dyn Fn(&mut MutableRepo, RemoteCallbacks<'_>) -> Result<()>,
4747
) -> Result<()>;
48+
49+
fn select_remote(&self, choices: &[&str]) -> Option<String>;
4850
}
4951

5052
struct NoCallbacks;
@@ -57,6 +59,10 @@ impl WorkerCallbacks for NoCallbacks {
5759
) -> Result<()> {
5860
f(repo, RemoteCallbacks::default())
5961
}
62+
63+
fn select_remote(&self, choices: &[&str]) -> Option<String> {
64+
choices.get(0).map(|choice| choice.to_string())
65+
}
6066
}
6167

6268
/// state that doesn't depend on jj-lib borrowings

0 commit comments

Comments
 (0)