Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Make IsGistNewer compare against latestDownload

Instead of comparing the gist's latestUpload time against the local
latestUpload time, this function should compare the gist time against
the local latestDownload time in order to determine if the cloud
settings are newer than the local settings.

Also, on an upload, set the local latestDownload time to the same
time as the upload since the settings are effectively "downloaded" on
an upload.

This protects against the following scenario:

- Machine A uploads settings to cloud
- Machine B downloads these settings
- Machine B changes settings and uploads them to the cloud
- Machine A downloads the settings
- Machine A changes settings and tries to upload them

The last upload attempt fails because A's last upload time is before
the cloud's last upload time, even though A downloaded the settings.
A forced upload is needed in order to upload the settings.
It therefore makes sense to compare against the local download time.

* Fix changed files check to work with snippets

When checking for the existance of files in the gist, use the
settings file "gistName" to lookup the file in the gist instead of
"fileName" in order to find any files in a subdir.  The gist uses "|"
as a separator in the "gistName" for files in a subdir and so using
a plain filename does not match.

This fixes the case where a user has snippets and does an upload with
no changes to settings or snippets.  The snippets are already up in the
gist.  The code that checks for files that are local and not in the gist
fails to match the fileName to the gistName and incorrectly concludes
that the file is not in the gist.  This causes the upload code to upload
all the files even if there were no changes, resulting in an "empty"
gist revision with only the upload timestamp changed.

* Fix uploadCancelled status bar message timeouts
  • Loading branch information
karl-lunarg authored and shanalikhan committed Sep 5, 2019
1 parent 9b17d07 commit f0ecb0c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/service/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class GitHubService {

public async IsGistNewer(
GIST: string,
localLastUpload: Date
localLastDownload: Date
): Promise<boolean> {
const gist = await this.ReadGist(GIST);
if (!gist) {
Expand All @@ -155,10 +155,10 @@ export class GitHubService {
try {
gistCloudSetting = JSON.parse(gist.data.files.cloudSettings.content);
const gistLastUpload = new Date(gistCloudSetting.lastUpload);
if (!localLastUpload) {
if (!localLastDownload) {
return false;
}
return gistLastUpload >= localLastUpload;
return gistLastUpload > localLastDownload;
} catch (err) {
return false;
}
Expand Down
15 changes: 8 additions & 7 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class Sync {
if (syncSetting.gist != null && syncSetting.gist !== "") {
const gistNewer = await github.IsGistNewer(
syncSetting.gist,
new Date(customSettings.lastUpload)
new Date(customSettings.lastDownload)
);
if (gistNewer) {
if (
Expand All @@ -284,7 +284,7 @@ export class Sync {
} else {
vscode.window.setStatusBarMessage(
localize("cmd.updateSettings.info.uploadCanceled"),
3
3000
);
return;
}
Expand All @@ -293,6 +293,7 @@ export class Sync {
}

customSettings.lastUpload = dateNow;
customSettings.lastDownload = dateNow;
let gistObj = await github.ReadGist(syncSetting.gist);

if (!gistObj) {
Expand Down Expand Up @@ -327,17 +328,17 @@ export class Sync {

if (
!allSettingFiles.some(fileToUpload => {
if (fileToUpload.fileName === "cloudSettings") {
if (fileToUpload.gistName === "cloudSettings") {
return false;
}
if (!gistObj.data.files[fileToUpload.fileName]) {
if (!gistObj.data.files[fileToUpload.gistName]) {
return true;
}
if (
gistObj.data.files[fileToUpload.fileName].content !==
gistObj.data.files[fileToUpload.gistName].content !==
fileToUpload.content
) {
console.info(`Sync: file ${fileToUpload.fileName} has changed`);
console.info(`Sync: file ${fileToUpload.gistName} has changed`);
return true;
}
})
Expand Down Expand Up @@ -366,7 +367,7 @@ export class Sync {
} else {
vscode.window.setStatusBarMessage(
localize("cmd.updateSettings.info.uploadCanceled"),
3
3000
);
return;
}
Expand Down

0 comments on commit f0ecb0c

Please sign in to comment.