Fix sync not working if files have been deleted, moved, or renamed

This commit is contained in:
Silvano Cerza 2025-05-22 16:50:02 +02:00
parent c62afe938b
commit 59f951f2b8

View file

@ -700,16 +700,19 @@ export default class SyncManager {
type: "delete_local", type: "delete_local",
filePath: filePath, filePath: filePath,
}); });
return;
} else if ( } else if (
localFile.lastModified > (remoteFile.deletedAt as number) localFile.lastModified > (remoteFile.deletedAt as number)
) { ) {
actions.push({ type: "upload", filePath: filePath }); actions.push({ type: "upload", filePath: filePath });
return;
} }
} }
if (!remoteFile.deleted && localFile.deleted) { if (!remoteFile.deleted && localFile.deleted) {
if (remoteFile.lastModified > (localFile.deletedAt as number)) { if (remoteFile.lastModified > (localFile.deletedAt as number)) {
actions.push({ type: "download", filePath: filePath }); actions.push({ type: "download", filePath: filePath });
return;
} else if ( } else if (
(localFile.deletedAt as number) > remoteFile.lastModified (localFile.deletedAt as number) > remoteFile.lastModified
) { ) {
@ -717,6 +720,7 @@ export default class SyncManager {
type: "delete_remote", type: "delete_remote",
filePath: filePath, filePath: filePath,
}); });
return;
} }
} }
@ -724,8 +728,10 @@ export default class SyncManager {
// Conflicts are already filtered out so we can make this decision easily // Conflicts are already filtered out so we can make this decision easily
if (localSHA !== localFile.sha) { if (localSHA !== localFile.sha) {
actions.push({ type: "upload", filePath: filePath }); actions.push({ type: "upload", filePath: filePath });
return;
} else { } else {
actions.push({ type: "download", filePath: filePath }); actions.push({ type: "download", filePath: filePath });
return;
} }
}), }),
); );
@ -784,9 +790,13 @@ export default class SyncManager {
* This is the same identical algoritm used by git to calculate * This is the same identical algoritm used by git to calculate
* a blob's SHA. * a blob's SHA.
* @param filePath normalized path to file * @param filePath normalized path to file
* @returns String containing the file SHA1 * @returns String containing the file SHA1 or null in case the file doesn't exist
*/ */
async calculateSHA(filePath: string): Promise<string> { async calculateSHA(filePath: string): Promise<string | null> {
if (!(await this.vault.adapter.exists(filePath))) {
// The file doesn't exist, can't calculate any SHA
return null;
}
const contentBuffer = await this.vault.adapter.readBinary(filePath); const contentBuffer = await this.vault.adapter.readBinary(filePath);
const contentBytes = new Uint8Array(contentBuffer); const contentBytes = new Uint8Array(contentBuffer);
const header = new TextEncoder().encode(`blob ${contentBytes.length}\0`); const header = new TextEncoder().encode(`blob ${contentBytes.length}\0`);