From 509b41fb570ae907a9e1e975f376d0ed5abd9f00 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Thu, 9 Jan 2025 14:35:40 +0100 Subject: [PATCH] Add justDownloaded field in files metadata --- src/events/listener.ts | 21 +++++++++++++++++++++ src/github/client.ts | 1 + src/metadata-store.ts | 5 +++++ 3 files changed, 27 insertions(+) diff --git a/src/events/listener.ts b/src/events/listener.ts index 4d91443..e320dcd 100644 --- a/src/events/listener.ts +++ b/src/events/listener.ts @@ -33,11 +33,23 @@ export default class EventsListener { // The file has not been created in directory that we're syncing with GitHub return; } + + const data = this.metadataStore.data[file.path]; + if (data && data.justDownloaded) { + // This file was just downloaded and not created by the user. + // It's enough to makr it as non just downloaded. + this.metadataStore.data[file.path].justDownloaded = false; + await this.metadataStore.save(); + return; + } + this.metadataStore.data[file.path] = { localPath: file.path, remotePath: file.path.replace(this.localContentDir, this.repoContentDir), sha: null, dirty: true, + // This file has been created by the user + justDownloaded: false, }; await this.metadataStore.save(); this.eventsQueue.enqueue({ @@ -64,6 +76,15 @@ export default class EventsListener { // The file has not been create in directory that we're syncing with GitHub return; } + + const data = this.metadataStore.data[file.path]; + if (data && data.justDownloaded) { + // This file was just downloaded and not modified by the user. + // It's enough to makr it as non just downloaded. + this.metadataStore.data[file.path].justDownloaded = false; + await this.metadataStore.save(); + return; + } this.metadataStore.data[file.path].dirty = true; await this.metadataStore.save(); this.eventsQueue.enqueue({ diff --git a/src/github/client.ts b/src/github/client.ts index 3bae4c4..d699448 100644 --- a/src/github/client.ts +++ b/src/github/client.ts @@ -77,6 +77,7 @@ export default class GithubClient { remotePath: file.path, sha: file.sha, dirty: false, + justDownloaded: true, }; await this.metadataStore.save(); }), diff --git a/src/metadata-store.ts b/src/metadata-store.ts index 25e8e12..8980efb 100644 --- a/src/metadata-store.ts +++ b/src/metadata-store.ts @@ -15,6 +15,11 @@ export interface FileMetadata { sha: string | null; // Whether the file has been modified locally. dirty: boolean; + // This is mostly used to track if the file has been just downloaded from the remote. + // This is necessary since even when creating a file programatically after it has been + // downloaded it will trigger a 'create' or 'modify' event. + // This is a problem as we can't know whether an event has been triggered by us or the user. + justDownloaded: boolean; } export interface Metadata {