mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add git autosync functionality
This commit is contained in:
7 files changed
+201
-51
No files matched your search
@@ -1,44 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useGit } from './useGit';
|
||||
|
||||
interface GitFetchResult {
|
||||
defaultBranch: string | null;
|
||||
fetchHead: string | null;
|
||||
fetchHeadDescription: string | null;
|
||||
}
|
||||
|
||||
export function useGitFetch(dir: string | undefined, remote = 'origin') {
|
||||
const { git } = useGit();
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: ['git-fetch', dir],
|
||||
queryFn: async (): Promise<GitFetchResult> => {
|
||||
const remotes = await git.listRemotes({ dir });
|
||||
const hasOrigin = !!remotes.find(r => r.remote === remote);
|
||||
|
||||
if (dir && hasOrigin) {
|
||||
try {
|
||||
return await git.fetch({
|
||||
dir,
|
||||
remote,
|
||||
});
|
||||
} catch (error) {
|
||||
// Fetch failed, but don't throw - just log and return null
|
||||
console.warn('Git fetch failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
defaultBranch: null,
|
||||
fetchHead: null,
|
||||
fetchHeadDescription: null,
|
||||
};
|
||||
},
|
||||
enabled: !!dir,
|
||||
refetchInterval: 60000, // Fetch every 60 seconds
|
||||
staleTime: 30000, // Consider data stale after 30 seconds
|
||||
retry: false, // Don't retry on failure
|
||||
});
|
||||
|
||||
return query;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { useGit } from './useGit';
|
||||
|
||||
interface GitSyncResult {
|
||||
defaultBranch: string | null;
|
||||
fetchHead: string | null;
|
||||
fetchHeadDescription: string | null;
|
||||
didPull?: boolean;
|
||||
didPush?: boolean;
|
||||
}
|
||||
|
||||
export function useGitSync(dir: string | undefined, remote = 'origin') {
|
||||
const { git } = useGit();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: ['git-sync', dir],
|
||||
queryFn: async (): Promise<GitSyncResult> => {
|
||||
const remotes = await git.listRemotes({ dir });
|
||||
const hasOrigin = !!remotes.find(r => r.remote === remote);
|
||||
|
||||
if (!dir || !hasOrigin) {
|
||||
return {
|
||||
defaultBranch: null,
|
||||
fetchHead: null,
|
||||
fetchHeadDescription: null,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if autosync is enabled
|
||||
const autosync = await git.getConfig({
|
||||
dir,
|
||||
path: 'shakespeare.autosync',
|
||||
});
|
||||
|
||||
// Always fetch to update remote tracking
|
||||
const fetchResult = await git.fetch({
|
||||
dir,
|
||||
remote,
|
||||
});
|
||||
|
||||
let didPull = false;
|
||||
let didPush = false;
|
||||
|
||||
// If autosync is enabled, perform pull and push
|
||||
if (autosync === 'true') {
|
||||
try {
|
||||
// Get current branch
|
||||
const currentBranch = await git.currentBranch({ dir });
|
||||
|
||||
if (currentBranch) {
|
||||
// Pull changes from remote
|
||||
try {
|
||||
await git.pull({
|
||||
dir,
|
||||
ref: currentBranch,
|
||||
singleBranch: true,
|
||||
});
|
||||
didPull = true;
|
||||
} catch (pullError) {
|
||||
// Pull might fail if there are conflicts or diverged branches
|
||||
// Don't throw - just log and continue
|
||||
console.warn('Auto-pull failed:', pullError);
|
||||
}
|
||||
|
||||
// Push local changes to remote
|
||||
try {
|
||||
await git.push({
|
||||
dir,
|
||||
remote,
|
||||
ref: currentBranch,
|
||||
});
|
||||
didPush = true;
|
||||
} catch (pushError) {
|
||||
// Push might fail if there are conflicts or no changes
|
||||
// Don't throw - just log and continue
|
||||
console.warn('Auto-push failed:', pushError);
|
||||
}
|
||||
|
||||
// If we successfully synced, invalidate git status to refresh UI
|
||||
if (didPull || didPush) {
|
||||
const projectId = dir.split('/').pop();
|
||||
if (projectId) {
|
||||
queryClient.invalidateQueries({ queryKey: ['git-status', projectId] });
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Auto-sync failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...fetchResult,
|
||||
didPull,
|
||||
didPush,
|
||||
};
|
||||
} catch (error) {
|
||||
// Fetch/sync failed, but don't throw - just log and return null
|
||||
console.warn('Git sync failed:', error);
|
||||
return {
|
||||
defaultBranch: null,
|
||||
fetchHead: null,
|
||||
fetchHeadDescription: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
enabled: !!dir,
|
||||
refetchInterval: 60000, // Sync every 60 seconds
|
||||
staleTime: 30000, // Consider data stale after 30 seconds
|
||||
retry: false, // Don't retry on failure
|
||||
});
|
||||
|
||||
return query;
|
||||
}
|
||||
Reference in new issue
Block a user