mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Abstract and reuse detectFork logic
This commit is contained in:
3 files changed
+61
-26
No files matched your search
@@ -13,6 +13,8 @@ import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useGitSettings } from '@/hooks/useGitSettings';
|
||||
import { detectFork } from '@/lib/detectFork';
|
||||
|
||||
interface RepositoryCardProps {
|
||||
repo: Repository;
|
||||
@@ -21,6 +23,7 @@ interface RepositoryCardProps {
|
||||
export function RepositoryCard({ repo }: RepositoryCardProps) {
|
||||
const { data: authorData } = useAuthor(repo.pubkey);
|
||||
const { user } = useCurrentUser();
|
||||
const { settings: gitSettings } = useGitSettings();
|
||||
const navigate = useNavigate();
|
||||
const projectsManager = useProjectsManager();
|
||||
const { toast } = useToast();
|
||||
@@ -42,11 +45,15 @@ export function RepositoryCard({ repo }: RepositoryCardProps) {
|
||||
try {
|
||||
await projectsManager.init();
|
||||
|
||||
// Determine if this is a fork
|
||||
const repoUrl = nostrURI.toString();
|
||||
const fork = await detectFork(repoUrl, user?.pubkey, gitSettings.credentials);
|
||||
|
||||
// Clone the repository using the Nostr URI
|
||||
const project = await projectsManager.cloneProject({
|
||||
name: repo.name,
|
||||
repoUrl: nostrURI.toString(),
|
||||
fork: user?.pubkey !== repo.pubkey,
|
||||
repoUrl,
|
||||
fork,
|
||||
});
|
||||
|
||||
toast({
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { NostrURI } from '@/lib/NostrURI';
|
||||
import { findCredentialsForRepo } from '@/lib/gitCredentials';
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
|
||||
/**
|
||||
* Detect if a repository URL should be treated as a fork
|
||||
*
|
||||
* For Nostr URIs: A repository is a fork if the owner's pubkey differs from the current user's pubkey
|
||||
* For HTTPS URLs: A repository is a fork if the URL doesn't match ${credential.origin}/${credential.username}/
|
||||
*
|
||||
* @param repoUrl - The repository URL (Nostr URI or HTTPS)
|
||||
* @param currentUserPubkey - The current user's Nostr pubkey (optional)
|
||||
* @param gitCredentials - Array of Git credentials (optional)
|
||||
* @returns true if the repository should be treated as a fork, false otherwise
|
||||
*/
|
||||
export async function detectFork(
|
||||
repoUrl: string,
|
||||
currentUserPubkey?: string,
|
||||
gitCredentials: GitCredential[] = [],
|
||||
): Promise<boolean> {
|
||||
// Default to not a fork
|
||||
let fork = false;
|
||||
|
||||
if (repoUrl.startsWith('nostr://')) {
|
||||
// For Nostr URIs: check if the repository owner is different from current user
|
||||
if (currentUserPubkey) {
|
||||
try {
|
||||
const nostrURI = await NostrURI.parse(repoUrl);
|
||||
fork = nostrURI.pubkey !== currentUserPubkey;
|
||||
} catch {
|
||||
// If parsing fails, default to fork=false
|
||||
fork = false;
|
||||
}
|
||||
}
|
||||
} else if (repoUrl.startsWith('http://') || repoUrl.startsWith('https://')) {
|
||||
// For HTTPS URLs: check if the URL belongs to the user based on credentials
|
||||
// A repository is NOT a fork if it starts with ${credential.origin}/${credential.username}/
|
||||
const credential = findCredentialsForRepo(repoUrl, gitCredentials);
|
||||
|
||||
if (credential) {
|
||||
const expectedPrefix = `${credential.origin}/${credential.username}/`;
|
||||
fork = !repoUrl.startsWith(expectedPrefix);
|
||||
} else {
|
||||
// No matching credentials found, treat as fork
|
||||
fork = true;
|
||||
}
|
||||
}
|
||||
|
||||
return fork;
|
||||
}
|
||||
+2
-24
@@ -26,7 +26,7 @@ import { NSchema as n } from '@nostrify/nostrify';
|
||||
import type { NostrMetadata } from '@nostrify/nostrify';
|
||||
import { NostrURI } from '@/lib/NostrURI';
|
||||
import { useGitSettings } from '@/hooks/useGitSettings';
|
||||
import { findCredentialsForRepo } from '@/lib/gitCredentials';
|
||||
import { detectFork } from '@/lib/detectFork';
|
||||
|
||||
export default function Clone() {
|
||||
const { t } = useTranslation();
|
||||
@@ -159,29 +159,7 @@ export default function Clone() {
|
||||
const repoName = await extractRepoName(targetUrl);
|
||||
|
||||
// Determine if this is a fork
|
||||
let fork = false;
|
||||
|
||||
if (targetUrl.startsWith('nostr://') && user) {
|
||||
// For Nostr URIs: check if the repository owner is different from current user
|
||||
try {
|
||||
const nostrURI = await NostrURI.parse(targetUrl);
|
||||
fork = nostrURI.pubkey !== user.pubkey;
|
||||
} catch {
|
||||
// If parsing fails, default to fork=false
|
||||
}
|
||||
} else if (targetUrl.startsWith('http://') || targetUrl.startsWith('https://')) {
|
||||
// For HTTPS URLs: check if the URL belongs to the user based on credentials
|
||||
// A repository is NOT a fork if it starts with ${credential.origin}/${credential.username}/
|
||||
const credential = findCredentialsForRepo(targetUrl, gitSettings.credentials);
|
||||
|
||||
if (credential) {
|
||||
const expectedPrefix = `${credential.origin}/${credential.username}/`;
|
||||
fork = !targetUrl.startsWith(expectedPrefix);
|
||||
} else {
|
||||
// No matching credentials found, treat as fork
|
||||
fork = true;
|
||||
}
|
||||
}
|
||||
const fork = await detectFork(targetUrl, user?.pubkey, gitSettings.credentials);
|
||||
|
||||
// Clone the repository (Git class handles both regular Git URLs and Nostr URIs)
|
||||
const project = await projectsManager.cloneProject({
|
||||
|
||||
Reference in new issue
Block a user