Introduce rust code for listing and adding remotes

This commit is contained in:
Caleb Owens 2024-05-28 11:54:01 +02:00
parent 6d1ed8474c
commit 9956e9f889
11 changed files with 114 additions and 3 deletions

View File

@ -0,0 +1,16 @@
import { invoke } from '$lib/backend/ipc';
import { showError } from '$lib/notifications/toasts';
export class RemotesService {
async remotes(projectId: string) {
return await invoke<string[]>('list_remotes', { projectId });
}
async addRemote(projectId: string, name: string, url: string) {
try {
await invoke('add_remote', { projectId, name, url });
} catch (e) {
showError('Failed to add remote', e);
}
}
}

View File

@ -13,6 +13,7 @@
import ShareIssueModal from '$lib/components/ShareIssueModal.svelte';
import { GitHubService } from '$lib/github/service';
import ToastController from '$lib/notifications/ToastController.svelte';
import { RemotesService } from '$lib/remotes/service';
import { SETTINGS, loadUserSettings } from '$lib/settings/userSettings';
import { User, UserService } from '$lib/stores/user';
import * as events from '$lib/utils/events';
@ -42,6 +43,7 @@
setContext(AuthService, data.authService);
setContext(HttpClient, data.cloud);
setContext(User, data.userService.user);
setContext(RemotesService, data.remotesService);
let shareIssueModal: ShareIssueModal;

View File

@ -7,6 +7,7 @@ import { ProjectService } from '$lib/backend/projects';
import { PromptService } from '$lib/backend/prompt';
import { UpdaterService } from '$lib/backend/updater';
import { GitHubService } from '$lib/github/service';
import { RemotesService } from '$lib/remotes/service';
import { UserService } from '$lib/stores/user';
import { mockTauri } from '$lib/testing/index';
import lscache from 'lscache';
@ -52,6 +53,7 @@ export async function load() {
const gitConfig = new GitConfigService();
const aiService = new AIService(gitConfig, httpClient);
const remotesService = new RemotesService();
return {
authService,
@ -64,6 +66,7 @@ export async function load() {
// These observables are provided for convenience
remoteUrl$,
gitConfig,
aiService
aiService,
remotesService
};
}

View File

@ -655,6 +655,11 @@ impl Repository {
})
.map_err(super::Error::Remotes)
}
pub fn add_remote(&self, name: &str, url: &str) -> Result<()> {
self.0.remote(name, url)?;
Ok(())
}
}
pub struct CheckoutTreeBuidler<'a> {

View File

@ -27,6 +27,7 @@ pub mod path;
pub mod project_repository;
pub mod projects;
pub mod reader;
pub mod remotes;
pub mod ssh;
pub mod storage;
pub mod synchronize;

View File

@ -611,6 +611,12 @@ impl Repository {
pub fn remotes(&self) -> Result<Vec<String>> {
self.git_repository.remotes().map_err(anyhow::Error::from)
}
pub fn add_remote(&self, name: &str, url: &str) -> Result<()> {
self.git_repository
.add_remote(name, url)
.map_err(anyhow::Error::from)
}
}
#[derive(Debug, thiserror::Error)]

View File

@ -0,0 +1,35 @@
use crate::{
error::Error,
project_repository,
projects::{self, ProjectId},
};
#[derive(Clone)]
pub struct Controller {
projects: projects::Controller,
}
impl Controller {
pub fn new(projects: projects::Controller) -> Self {
Self { projects }
}
pub async fn remotes(&self, project_id: &ProjectId) -> Result<Vec<String>, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::open(&project)?;
project_repository.remotes().map_err(Into::into)
}
pub async fn add_remote(
&self,
project_id: &ProjectId,
name: &str,
url: &str,
) -> Result<(), Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::open(&project)?;
project_repository.add_remote(name, url).map_err(Into::into)
}
}

View File

@ -0,0 +1,2 @@
pub mod controller;
pub use controller::Controller;

View File

@ -25,6 +25,7 @@ pub mod error;
pub mod github;
pub mod keys;
pub mod projects;
pub mod remotes;
pub mod undo;
pub mod users;
pub mod virtual_branches;

View File

@ -15,8 +15,8 @@
use gitbutler_core::{assets, git, storage};
use gitbutler_tauri::{
app, askpass, commands, github, keys, logs, menu, projects, undo, users, virtual_branches,
watcher, zip,
app, askpass, commands, github, keys, logs, menu, projects, remotes, undo, users,
virtual_branches, watcher, zip,
};
use tauri::{generate_context, Manager};
use tauri_plugin_log::LogTarget;
@ -134,6 +134,12 @@ fn main() {
git_credentials_controller.clone(),
));
let remotes_controller = gitbutler_core::remotes::controller::Controller::new(
projects_controller.clone(),
);
app_handle.manage(remotes_controller.clone());
let app = app::App::new(
projects_controller,
);
@ -210,6 +216,8 @@ fn main() {
github::commands::init_device_oauth,
github::commands::check_auth_status,
askpass::commands::submit_prompt_response,
remotes::list_remotes,
remotes::add_remote
])
.menu(menu::build(tauri_context.package_info()))
.on_menu_event(|event|menu::handle_event(&event))

View File

@ -0,0 +1,32 @@
use crate::error::Error;
use gitbutler_core::{projects::ProjectId, remotes::Controller};
use tauri::Manager;
use tracing::instrument;
#[tauri::command(async)]
#[instrument(skip(handle), err(Debug))]
pub async fn list_remotes(
handle: tauri::AppHandle,
project_id: ProjectId,
) -> Result<Vec<String>, Error> {
handle
.state::<Controller>()
.remotes(&project_id)
.await
.map_err(Into::into)
}
#[tauri::command(async)]
#[instrument(skip(handle), err(Debug))]
pub async fn add_remote(
handle: tauri::AppHandle,
project_id: ProjectId,
name: &str,
url: &str,
) -> Result<(), Error> {
handle
.state::<Controller>()
.add_remote(&project_id, name, url)
.await
.map_err(Into::into)
}