-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for existing fork before adding new one
- can prevent a cascading error - exposes remote urls to front end
- Loading branch information
Showing
6 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import { invoke } from '$lib/backend/ipc'; | ||
import { showError } from '$lib/notifications/toasts'; | ||
|
||
export interface GitRemote { | ||
name?: string; | ||
url?: string; | ||
} | ||
|
||
export class RemotesService { | ||
async remotes(projectId: string) { | ||
return await invoke<string[]>('list_remotes', { projectId }); | ||
return await invoke<GitRemote[]>('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); | ||
const remotes = await this.remotes(projectId); | ||
|
||
const sameNameRemote = remotes.find((remote) => remote.name === name); | ||
if (sameNameRemote) { | ||
throw new Error(`Remote with name ${sameNameRemote.name} already exists.`); | ||
} | ||
|
||
const sameUrlRemote = remotes.find((remote) => remote.url === url); | ||
if (sameUrlRemote) { | ||
// This should not happen, and indicates we are incorrectly showing an "apply from fork" | ||
// button in the user interface. | ||
throw new Error(`Remote ${sameUrlRemote.name} with url ${sameUrlRemote.url} already exists.`); | ||
} | ||
|
||
return await invoke<string>('add_remote', { projectId, name, url }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use serde::Serialize; | ||
|
||
/// Struct for exposing remote information to the front end. | ||
#[derive(Default, Debug, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct GitRemote { | ||
pub name: Option<String>, | ||
pub url: Option<String>, | ||
} | ||
|
||
impl From<git2::Remote<'_>> for GitRemote { | ||
fn from(value: git2::Remote) -> Self { | ||
GitRemote { | ||
name: value.name().map(|name| name.to_owned()), | ||
url: value.url().map(|url| url.to_owned()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters