From 807eec928ee186f1a977aa010d21540623e733ff Mon Sep 17 00:00:00 2001
From: fyears <1142836+fyears@users.noreply.github.com>
Date: Sun, 19 May 2024 17:51:44 +0800
Subject: [PATCH] optimize sync plan export
---
src/baseTypes.ts | 2 ++
src/debugMode.ts | 30 +++++++++++++++++++++++---
src/langs/en.json | 2 ++
src/langs/zh_cn.json | 2 ++
src/langs/zh_tw.json | 2 ++
src/main.ts | 25 +++++++++++++++++++---
src/settings.ts | 22 ++++++++++++++++---
src/sync.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 127 insertions(+), 9 deletions(-)
diff --git a/src/baseTypes.ts b/src/baseTypes.ts
index e4ccda8..feb1d1a 100644
--- a/src/baseTypes.ts
+++ b/src/baseTypes.ts
@@ -252,6 +252,8 @@ export interface MixedEntity {
decision?: DecisionTypeForMixedEntity;
conflictAction?: ConflictActionType;
+ change?: boolean;
+
sideNotes?: any;
}
diff --git a/src/debugMode.ts b/src/debugMode.ts
index 40df93a..e18acc7 100644
--- a/src/debugMode.ts
+++ b/src/debugMode.ts
@@ -11,12 +11,30 @@ import {
} from "./localdb";
import type { InternalDBs } from "./localdb";
import { mkdirpInVault } from "./misc";
+import type { SyncPlanType } from "./sync";
+
+const getSubsetOfSyncPlan = (x: string, onlyChange: boolean) => {
+ if (!onlyChange) {
+ return x;
+ }
+ const y: SyncPlanType = JSON.parse(x);
+ const z: SyncPlanType = Object.fromEntries(
+ Object.entries(y).filter(([key, val]) => {
+ if (key === "/$@meta") {
+ return true;
+ }
+ return val.change === undefined || val.change === true;
+ })
+ );
+ return JSON.stringify(z, null, 2);
+};
export const exportVaultSyncPlansToFiles = async (
db: InternalDBs,
vault: Vault,
vaultRandomID: string,
- howMany: number
+ howMany: number,
+ onlyChange: boolean
) => {
console.info("exporting sync plans");
await mkdirpInVault(DEFAULT_DEBUG_FOLDER, vault);
@@ -28,12 +46,18 @@ export const exportVaultSyncPlansToFiles = async (
if (howMany <= 0) {
md =
"Sync plans found:\n\n" +
- records.map((x) => "```json\n" + x + "\n```\n").join("\n");
+ records
+ .map(
+ (x) => "```json\n" + getSubsetOfSyncPlan(x, onlyChange) + "\n```\n"
+ )
+ .join("\n");
} else {
md =
"Sync plans found:\n\n" +
records
- .map((x) => "```json\n" + x + "\n```\n")
+ .map(
+ (x) => "```json\n" + getSubsetOfSyncPlan(x, onlyChange) + "\n```\n"
+ )
.slice(0, howMany)
.join("\n");
}
diff --git a/src/langs/en.json b/src/langs/en.json
index ec6e05b..872f1f7 100644
--- a/src/langs/en.json
+++ b/src/langs/en.json
@@ -38,6 +38,7 @@
"protocol_onedrive_connect_unknown": "Do not know how to deal with the callback: {{params}}",
"command_startsync": "start sync",
"command_drynrun": "start sync (dry run only)",
+ "command_exportsyncplans_1_only_change": "export sync plans (latest 1) (change part)",
"command_exportsyncplans_1": "export sync plans (latest 1)",
"command_exportsyncplans_5": "export sync plans (latest 5)",
"command_exportsyncplans_all": "export sync plans (all)",
@@ -318,6 +319,7 @@
"settings_viewconsolelog_desc": "On desktop, please press \"ctrl+shift+i\" or \"cmd+shift+i\" to view the log. On mobile, please install the third-party plugin Logstravaganza to export the console log to a note.",
"settings_syncplans": "Export Sync Plans",
"settings_syncplans_desc": "Sync plans are created every time after you trigger sync and before the actual sync. Useful to know what would actually happen in those sync. Click the button to export sync plans.",
+ "settings_syncplans_button_1_only_change": "Export latest 1 (change part)",
"settings_syncplans_button_1": "Export latest 1",
"settings_syncplans_button_5": "Export latest 5",
"settings_syncplans_button_all": "Export All",
diff --git a/src/langs/zh_cn.json b/src/langs/zh_cn.json
index c62bd27..adfa6d9 100644
--- a/src/langs/zh_cn.json
+++ b/src/langs/zh_cn.json
@@ -39,6 +39,7 @@
"command_startsync": "开始同步(start sync)",
"command_drynrun": "开始同步(空跑模式)(start sync (dry run only))",
"command_exportsyncplans_json": "导出同步计划为 json 格式(export sync plans in json format)",
+ "command_exportsyncplans_1_only_change": "导出同步计划(最近 1 次)(仅修改部分)(export sync plans (latest 1) (change part))",
"command_exportsyncplans_1": "导出同步计划(最近 1 次)(export sync plans (latest 1))",
"command_exportsyncplans_5": "导出同步计划(最近 5 次)(export sync plans (latest 5))",
"command_exportsyncplans_all": "导出同步计划(所有)(export sync plans (all))",
@@ -317,6 +318,7 @@
"settings_viewconsolelog_desc": "电脑上,输入“ctrl+shift+i”或“cmd+shift+i”来查看终端输出。手机上,安装第三方插件 Logstravaganza 来导出终端输出到一篇笔记上。",
"settings_syncplans": "导出同步计划",
"settings_syncplans_desc": "每次您启动同步,并在实际上传下载前,插件会生成同步计划。它可以使您知道每次同步发生了什么。点击按钮可以导出同步计划。",
+ "settings_syncplans_button_1_only_change": "导出最近 1 次(仅修改部分)",
"settings_syncplans_button_1": "导出最近 1 次",
"settings_syncplans_button_5": "导出最近 5 次",
"settings_syncplans_button_all": "导出所有",
diff --git a/src/langs/zh_tw.json b/src/langs/zh_tw.json
index b24ff9d..ff48510 100644
--- a/src/langs/zh_tw.json
+++ b/src/langs/zh_tw.json
@@ -38,6 +38,7 @@
"protocol_onedrive_connect_unknown": "不知道如何處理此 callback:{{params}}",
"command_startsync": "開始同步(start sync)",
"command_drynrun": "開始同步(空跑模式)(start sync (dry run only))",
+ "command_exportsyncplans_1_only_change": "匯出同步計劃(最近 1 次)(僅修改部分)(export sync plans (latest 1) (change part))",
"command_exportsyncplans_1": "匯出同步計劃(最近 1 次)(export sync plans (latest 1))",
"command_exportsyncplans_5": "匯出同步計劃(最近 5 次)(export sync plans (latest 5))",
"command_exportsyncplans_all": "匯出同步計劃(所有)(export sync plans (all))",
@@ -316,6 +317,7 @@
"settings_viewconsolelog_desc": "電腦上,輸入“ctrl+shift+i”或“cmd+shift+i”來檢視終端輸出。手機上,安裝第三方外掛 Logstravaganza 來匯出終端輸出到一篇筆記上。",
"settings_syncplans": "匯出同步計劃",
"settings_syncplans_desc": "每次您啟動同步,並在實際上傳下載前,外掛會生成同步計劃。它可以使您知道每次同步發生了什麼。點選按鈕可以匯出同步計劃。",
+ "settings_syncplans_button_1_only_change": "匯出最近 1 次(僅修改部分)",
"settings_syncplans_button_1": "匯出最近 1 次",
"settings_syncplans_button_5": "匯出最近 5 次",
"settings_syncplans_button_all": "匯出所有",
diff --git a/src/main.ts b/src/main.ts
index e20fb6f..949f0c0 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -745,6 +745,22 @@ export default class RemotelySavePlugin extends Plugin {
},
});
+ this.addCommand({
+ id: "export-sync-plans-1-only-change",
+ name: t("command_exportsyncplans_1_only_change"),
+ icon: iconNameLogs,
+ callback: async () => {
+ await exportVaultSyncPlansToFiles(
+ this.db,
+ this.app.vault,
+ this.vaultRandomID,
+ 1,
+ true
+ );
+ new Notice(t("settings_syncplans_notice"));
+ },
+ });
+
this.addCommand({
id: "export-sync-plans-1",
name: t("command_exportsyncplans_1"),
@@ -754,7 +770,8 @@ export default class RemotelySavePlugin extends Plugin {
this.db,
this.app.vault,
this.vaultRandomID,
- 1
+ 1,
+ false
);
new Notice(t("settings_syncplans_notice"));
},
@@ -769,7 +786,8 @@ export default class RemotelySavePlugin extends Plugin {
this.db,
this.app.vault,
this.vaultRandomID,
- 5
+ 5,
+ false
);
new Notice(t("settings_syncplans_notice"));
},
@@ -784,7 +802,8 @@ export default class RemotelySavePlugin extends Plugin {
this.db,
this.app.vault,
this.vaultRandomID,
- -1
+ -1,
+ false
);
new Notice(t("settings_syncplans_notice"));
},
diff --git a/src/settings.ts b/src/settings.ts
index 3894fc8..c765d85 100644
--- a/src/settings.ts
+++ b/src/settings.ts
@@ -2416,6 +2416,19 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
new Setting(debugDiv)
.setName(t("settings_syncplans"))
.setDesc(t("settings_syncplans_desc"))
+ .addButton(async (button) => {
+ button.setButtonText(t("settings_syncplans_button_1_only_change"));
+ button.onClick(async () => {
+ await exportVaultSyncPlansToFiles(
+ this.plugin.db,
+ this.app.vault,
+ this.plugin.vaultRandomID,
+ 1,
+ true
+ );
+ new Notice(t("settings_syncplans_notice"));
+ });
+ })
.addButton(async (button) => {
button.setButtonText(t("settings_syncplans_button_1"));
button.onClick(async () => {
@@ -2423,7 +2436,8 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
this.plugin.db,
this.app.vault,
this.plugin.vaultRandomID,
- 1
+ 1,
+ false
);
new Notice(t("settings_syncplans_notice"));
});
@@ -2435,7 +2449,8 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
this.plugin.db,
this.app.vault,
this.plugin.vaultRandomID,
- 5
+ 5,
+ false
);
new Notice(t("settings_syncplans_notice"));
});
@@ -2447,7 +2462,8 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
this.plugin.db,
this.app.vault,
this.plugin.vaultRandomID,
- -1
+ -1,
+ false
);
new Notice(t("settings_syncplans_notice"));
});
diff --git a/src/sync.ts b/src/sync.ts
index 15eaba5..7146c16 100644
--- a/src/sync.ts
+++ b/src/sync.ts
@@ -326,69 +326,83 @@ const getSyncPlanInplace = async (
if (local !== undefined && remote !== undefined) {
mixedEntry.decisionBranch = 101;
mixedEntry.decision = "folder_existed_both_then_do_nothing";
+ mixedEntry.change = false;
} else if (local !== undefined && remote === undefined) {
if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 107;
mixedEntry.decision = "folder_to_skip";
+ mixedEntry.change = false;
} else {
mixedEntry.decisionBranch = 102;
mixedEntry.decision =
"folder_existed_local_then_also_create_remote";
+ mixedEntry.change = true;
}
} else if (local === undefined && remote !== undefined) {
if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 108;
mixedEntry.decision = "folder_to_skip";
+ mixedEntry.change = false;
} else {
mixedEntry.decisionBranch = 103;
mixedEntry.decision =
"folder_existed_remote_then_also_create_local";
+ mixedEntry.change = true;
}
} else {
// why?? how??
mixedEntry.decisionBranch = 104;
mixedEntry.decision = "folder_to_be_created";
+ mixedEntry.change = true;
}
keptFolder.delete(key); // no need to save it in the Set later
} else {
if (howToCleanEmptyFolder === "skip") {
mixedEntry.decisionBranch = 105;
mixedEntry.decision = "folder_to_skip";
+ mixedEntry.change = false;
} else if (howToCleanEmptyFolder === "clean_both") {
if (local !== undefined && remote !== undefined) {
if (syncDirection === "bidirectional") {
mixedEntry.decisionBranch = 106;
mixedEntry.decision = "folder_to_be_deleted_on_both";
+ mixedEntry.change = true;
} else {
// right now it does nothing because of "incremental"
// TODO: should we delete??
mixedEntry.decisionBranch = 109;
mixedEntry.decision = "folder_to_skip";
+ mixedEntry.change = false;
}
} else if (local !== undefined && remote === undefined) {
if (syncDirection === "bidirectional") {
mixedEntry.decisionBranch = 110;
mixedEntry.decision = "folder_to_be_deleted_on_local";
+ mixedEntry.change = true;
} else {
// right now it does nothing because of "incremental"
// TODO: should we delete??
mixedEntry.decisionBranch = 111;
mixedEntry.decision = "folder_to_skip";
+ mixedEntry.change = false;
}
} else if (local === undefined && remote !== undefined) {
if (syncDirection === "bidirectional") {
mixedEntry.decisionBranch = 112;
mixedEntry.decision = "folder_to_be_deleted_on_remote";
+ mixedEntry.change = true;
} else {
// right now it does nothing because of "incremental"
// TODO: should we delete??
mixedEntry.decisionBranch = 113;
mixedEntry.decision = "folder_to_skip";
+ mixedEntry.change = false;
}
} else {
// no folder to delete, do nothing
mixedEntry.decisionBranch = 114;
mixedEntry.decision = "folder_to_skip";
+ mixedEntry.change = false;
}
} else {
throw Error(
@@ -403,6 +417,7 @@ const getSyncPlanInplace = async (
// both deleted, only in history
mixedEntry.decisionBranch = 1;
mixedEntry.decision = "only_history";
+ mixedEntry.change = false;
} else if (local !== undefined && remote !== undefined) {
if (
(local.mtimeCli === remote.mtimeCli ||
@@ -412,6 +427,7 @@ const getSyncPlanInplace = async (
// completely equal / identical
mixedEntry.decisionBranch = 2;
mixedEntry.decision = "equal";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
} else {
// Both exists, but modified or conflict
@@ -434,10 +450,12 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 26;
mixedEntry.decision = "conflict_modified_then_keep_local";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 9;
mixedEntry.decision = "remote_is_modified_then_pull";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {
@@ -456,10 +474,12 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 27;
mixedEntry.decision = "conflict_modified_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 10;
mixedEntry.decision = "local_is_modified_then_push";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {
@@ -481,34 +501,41 @@ const getSyncPlanInplace = async (
) {
mixedEntry.decisionBranch = 11;
mixedEntry.decision = "conflict_created_then_keep_local";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 12;
mixedEntry.decision = "conflict_created_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else if (conflictAction === "keep_larger") {
if (local.sizeEnc! >= remote.sizeEnc!) {
mixedEntry.decisionBranch = 13;
mixedEntry.decision = "conflict_created_then_keep_local";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 14;
mixedEntry.decision = "conflict_created_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {
mixedEntry.decisionBranch = 15;
mixedEntry.decision = "conflict_created_then_keep_both";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 22;
mixedEntry.decision = "conflict_created_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 23;
mixedEntry.decision = "conflict_created_then_keep_local";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
throw Error(
@@ -525,34 +552,41 @@ const getSyncPlanInplace = async (
) {
mixedEntry.decisionBranch = 16;
mixedEntry.decision = "conflict_modified_then_keep_local";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 17;
mixedEntry.decision = "conflict_modified_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else if (conflictAction === "keep_larger") {
if (local.sizeEnc! >= remote.sizeEnc!) {
mixedEntry.decisionBranch = 18;
mixedEntry.decision = "conflict_modified_then_keep_local";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 19;
mixedEntry.decision = "conflict_modified_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {
mixedEntry.decisionBranch = 20;
mixedEntry.decision = "conflict_modified_then_keep_both";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 24;
mixedEntry.decision = "conflict_modified_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 25;
mixedEntry.decision = "conflict_modified_then_keep_local";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
throw Error(
@@ -566,6 +600,7 @@ const getSyncPlanInplace = async (
// The result should be equal!!!
mixedEntry.decisionBranch = 21;
mixedEntry.decision = "equal";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
}
}
@@ -580,15 +615,18 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 28;
mixedEntry.decision = "conflict_created_then_do_nothing";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 3;
mixedEntry.decision = "remote_is_created_then_pull";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {
mixedEntry.decisionBranch = 36;
mixedEntry.decision = "remote_is_created_too_large_then_do_nothing";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
}
} else if (
@@ -600,14 +638,17 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 29;
mixedEntry.decision = "conflict_created_then_do_nothing";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
} else if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 35;
mixedEntry.decision = "conflict_created_then_keep_remote";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 4;
mixedEntry.decision = "local_is_deleted_thus_also_delete_remote";
+ mixedEntry.change = true;
}
} else {
// if B is in the previous list and MODIFIED, B has been deleted by A but modified by B
@@ -618,10 +659,12 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 30;
mixedEntry.decision = "conflict_created_then_do_nothing";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 5;
mixedEntry.decision = "remote_is_modified_then_pull";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {
@@ -641,15 +684,18 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 31;
mixedEntry.decision = "conflict_created_then_do_nothing";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 6;
mixedEntry.decision = "local_is_created_then_push";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {
mixedEntry.decisionBranch = 37;
mixedEntry.decision = "local_is_created_too_large_then_do_nothing";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
}
} else if (
@@ -661,12 +707,15 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_push_only") {
mixedEntry.decisionBranch = 32;
mixedEntry.decision = "conflict_created_then_keep_local";
+ mixedEntry.change = true;
} else if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 33;
mixedEntry.decision = "conflict_created_then_do_nothing";
+ mixedEntry.change = false;
} else {
mixedEntry.decisionBranch = 7;
mixedEntry.decision = "remote_is_deleted_thus_also_delete_local";
+ mixedEntry.change = true;
}
} else {
// if A is in the previous list and MODIFIED, A has been deleted by B but modified by A
@@ -674,10 +723,12 @@ const getSyncPlanInplace = async (
if (syncDirection === "incremental_pull_only") {
mixedEntry.decisionBranch = 34;
mixedEntry.decision = "conflict_created_then_do_nothing";
+ mixedEntry.change = false;
keptFolder.add(getParentFolder(key));
} else {
mixedEntry.decisionBranch = 8;
mixedEntry.decision = "local_is_modified_then_push";
+ mixedEntry.change = true;
keptFolder.add(getParentFolder(key));
}
} else {