mirror of
https://github.com/firstsun-dev/git-files-sync.git
synced 2026-07-22 17:20:30 +00:00
perf(sync): optimize GitLab push by avoiding redundant getFile calls
This commit is contained in:
parent
b4bc25a91c
commit
c2a6bb5fba
3 changed files with 14 additions and 11 deletions
|
|
@ -30,7 +30,7 @@ export class SyncManager {
|
|||
new SyncConflictModal(this.app, file, content, remote.content, (choice) => {
|
||||
void (async () => {
|
||||
if (choice === 'local') {
|
||||
await this.performPush(file, content);
|
||||
await this.performPush(file, content, remote.sha);
|
||||
} else {
|
||||
await this.performPull(file, remote.content, remote.sha);
|
||||
}
|
||||
|
|
@ -39,19 +39,20 @@ export class SyncManager {
|
|||
return;
|
||||
}
|
||||
|
||||
await this.performPush(file, content);
|
||||
await this.performPush(file, content, remote.sha);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
new Notice(`Failed to push ${file.name}: ${e instanceof Error ? e.message : String(e)}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async performPush(file: TFile, content: string) {
|
||||
private async performPush(file: TFile, content: string, existingSha?: string) {
|
||||
await this.gitlab.pushFile(
|
||||
file.path,
|
||||
content,
|
||||
this.settings.branch,
|
||||
`Update ${file.name} from Obsidian`
|
||||
`Update ${file.name} from Obsidian`,
|
||||
existingSha
|
||||
);
|
||||
|
||||
// Update metadata
|
||||
|
|
@ -95,7 +96,7 @@ export class SyncManager {
|
|||
new SyncConflictModal(this.app, file, localContent, remote.content, (choice) => {
|
||||
void (async () => {
|
||||
if (choice === 'local') {
|
||||
await this.performPush(file, localContent);
|
||||
await this.performPush(file, localContent, remote.sha);
|
||||
} else {
|
||||
await this.performPull(file, remote.content, remote.sha);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,11 +65,11 @@ export class GitLabService {
|
|||
};
|
||||
}
|
||||
|
||||
async pushFile(path: string, content: string, branch: string, commitMessage: string): Promise<string> {
|
||||
async pushFile(path: string, content: string, branch: string, commitMessage: string, existingSha?: string): Promise<string> {
|
||||
const url = this.getApiUrl(path);
|
||||
|
||||
const existingFile = await this.getFile(path, branch);
|
||||
const method = existingFile.sha ? 'PUT' : 'POST';
|
||||
const sha = existingSha !== undefined ? existingSha : (await this.getFile(path, branch)).sha;
|
||||
const method = sha ? 'PUT' : 'POST';
|
||||
|
||||
const response = await requestUrl({
|
||||
url,
|
||||
|
|
|
|||
|
|
@ -56,7 +56,8 @@ describe('SyncManager', () => {
|
|||
'test.md',
|
||||
'local content',
|
||||
'main',
|
||||
expect.stringContaining('Update test.md')
|
||||
'Update test.md from Obsidian',
|
||||
''
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -118,7 +119,7 @@ describe('SyncManager', () => {
|
|||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
const pushSpy = mockGitLab.pushFile as any;
|
||||
expect(pushSpy).toHaveBeenCalledWith('test.md', 'local content', 'main', expect.any(String));
|
||||
expect(pushSpy).toHaveBeenCalledWith('test.md', 'local content', 'main', 'Update test.md from Obsidian', 'remote-sha');
|
||||
expect(mockSettings.syncMetadata['test.md']?.lastSyncedSha).toBe('new-sha');
|
||||
});
|
||||
|
||||
|
|
@ -220,7 +221,8 @@ describe('SyncManager', () => {
|
|||
'new.md',
|
||||
'new local content',
|
||||
'main',
|
||||
expect.stringContaining('Update new.md')
|
||||
'Update new.md from Obsidian',
|
||||
''
|
||||
);
|
||||
expect(mockSettings.syncMetadata['new.md']?.lastSyncedSha).toBe('new-sha');
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue