mirror of
https://github.com/silvanocerza/github-gitless-sync.git
synced 2026-07-22 12:10:28 +00:00
Compare commits
43 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ebd201da9 | ||
|
|
ee9c2ee236 | ||
|
|
b6eb9ed62d | ||
|
|
59f951f2b8 | ||
|
|
c62afe938b | ||
|
|
69841aa574 | ||
|
|
345826443f | ||
|
|
e33de48075 | ||
|
|
b2f3db6050 | ||
|
|
be547d8691 | ||
|
|
4fa2526322 | ||
|
|
786cefaf4d | ||
|
|
654fa6b455 | ||
|
|
1e16d01adf | ||
|
|
56bd6dee4c | ||
|
|
b6be53ca16 | ||
|
|
53651f5b88 | ||
|
|
263623dad7 | ||
|
|
13383dc65c | ||
|
|
15cc51014c | ||
|
|
96da957f18 | ||
|
|
457a0b6d24 | ||
|
|
1a2eb64959 | ||
|
|
5ab20cf482 | ||
|
|
d0f04d5217 | ||
|
|
7e7eddaec3 | ||
|
|
6c9b8625c3 | ||
|
|
271d90b7f9 | ||
|
|
de1c814260 | ||
|
|
32dee2db4c | ||
|
|
415532ff07 | ||
|
|
5879d22a52 | ||
|
|
bbe05f716a | ||
|
|
17f869f9b3 | ||
|
|
b26853fb3a | ||
|
|
91137ebf82 | ||
|
|
035f223aef | ||
|
|
f2452b1c60 | ||
|
|
4f018043f2 | ||
|
|
e0fca4dc7a | ||
|
|
eec44d8190 | ||
|
|
acf5157971 | ||
|
|
03758866fb |
26 changed files with 1662 additions and 311 deletions
20
.github/workflows/release.yml
vendored
20
.github/workflows/release.yml
vendored
|
|
@ -3,7 +3,7 @@ name: Release plugin
|
|||
on:
|
||||
push:
|
||||
tags:
|
||||
- "[0-9].[0-9]+.[0-9]+"
|
||||
- "[0-9].[0-9]+.[0-9]+*"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
|
@ -20,13 +20,21 @@ jobs:
|
|||
|
||||
- name: Build plugin
|
||||
run: |
|
||||
npm install
|
||||
npm run build
|
||||
npm install -g pnpm@latest-10
|
||||
pnpm install
|
||||
pnpm build
|
||||
|
||||
- name: Create release
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
gh release create "${{ github.ref_name }}" \
|
||||
--title="${{ github.ref_name }}" \
|
||||
main.js manifest.json styles.css
|
||||
if [[ "${{ github.ref_name }}" == *-beta* ]]; then
|
||||
gh release create "${{ github.ref_name }}" \
|
||||
--title="${{ github.ref_name }}" \
|
||||
--prerelease \
|
||||
main.js manifest.json styles.css
|
||||
else
|
||||
gh release create "${{ github.ref_name }}" \
|
||||
--title="${{ github.ref_name }}" \
|
||||
main.js manifest.json styles.css
|
||||
fi
|
||||
|
|
|
|||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -156,3 +156,5 @@ data.json
|
|||
|
||||
# Exclude macOS Finder (System Explorer) View States
|
||||
.DS_Store
|
||||
|
||||
benchmark_result.json
|
||||
|
|
|
|||
36
README.md
36
README.md
|
|
@ -20,23 +20,17 @@ These are the main features of the plugin:
|
|||
|
||||
## Installation
|
||||
|
||||
The plugin is still not available in the community plugins.
|
||||
The plugin is available as a community plugin, you can easily search for it in Obsidian and install it from there.
|
||||
|
||||
For the time being you can install it with BRAT. If you never used BRAT see [the official quick start guide](https://tfthacker.com/brat-quick-guide).
|
||||
|
||||
If you already have BRAT installed to install GitHub Sync copy the following link and paste it in the browser address bar.
|
||||
|
||||
```
|
||||
obsidian://brat?plugin=https://github.com/silvanocerza/obsidian-github-sync
|
||||
```
|
||||

|
||||
|
||||
### Issues
|
||||
|
||||
If you find any problem please open an issue with as many details as possible.
|
||||
|
||||
Please also provide logs if possible, you can find the `github-sync.log` file in your config directory. Remember to enable logging first.
|
||||
Please also provide logs if possible, you can copy them from the settings page. Remember to enable logging first.
|
||||
|
||||

|
||||

|
||||
|
||||
## Usage
|
||||
|
||||
|
|
@ -108,7 +102,9 @@ If you need to reset the plugin settings and metadata you can easily do that in
|
|||
|
||||
That will completely wipe all the sync metadata so you'll have to repeat the first sync as if you just enabled the plugin for the first time.
|
||||
|
||||
## What's different from other sync plugins?
|
||||
## FAQs
|
||||
|
||||
### What's different from other sync plugins?
|
||||
|
||||
There are obviously other plugins that let you sync your vault with GitHub or other git hosts, like [`obsidian-git`](https://github.com/Vinzent03/obsidian-git) and [`Obsidian-GitHub-Sync`](https://github.com/kevinmkchin/Obsidian-GitHub-Sync) just to name a couple.
|
||||
|
||||
|
|
@ -122,6 +118,24 @@ This obviously comes with some limitations. Since `git` is not used you can't in
|
|||
|
||||
Also since this relies only on the GitHub APIs you can only sync with GitHub and no other host.
|
||||
|
||||
### Can I use this with other sync plugins?
|
||||
|
||||
No.
|
||||
|
||||
To work correctly this plugin uses a custom metadata file that is updated every time we sync, if you commit changes outside the plugin that file is not updated properly.
|
||||
|
||||
Other plugins don't know about that file, so if you sync with others too you risk losing data.
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are obviously accepted.
|
||||
|
||||
Bug fixes are always welcome, new feature must be discussed first. Open a [discussion](https://github.com/silvanocerza/github-gitless-sync/discussions) and let's talk in that case.
|
||||
|
||||
Keep PRs as small as possible, I won't review PRs with hundreds of lines if it's mostly code.
|
||||
|
||||
Low quality or vibe coded PRs are not welcome. Put some effort in it please.
|
||||
|
||||
## License
|
||||
|
||||
The project is licensed under the [AGPLv3](https://www.gnu.org/licenses/agpl-3.0.en.html) license.
|
||||
|
|
|
|||
BIN
assets/install_instructions.png
Normal file
BIN
assets/install_instructions.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 106 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB |
BIN
assets/logs_settings.png
Normal file
BIN
assets/logs_settings.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
280
benchmark.ts
Normal file
280
benchmark.ts
Normal file
|
|
@ -0,0 +1,280 @@
|
|||
import * as fs from "fs";
|
||||
import * as proxyquire from "proxyquire";
|
||||
import * as obsidianMocks from "./mock-obsidian";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import * as crypto from "crypto";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
const proxyquireNonStrict = proxyquire.noCallThru();
|
||||
|
||||
const LoggerModule = proxyquireNonStrict("./src/logger", {
|
||||
obsidian: obsidianMocks,
|
||||
});
|
||||
|
||||
const MetadataStoreModule = proxyquireNonStrict("./src/metadata-store", {
|
||||
obsidian: obsidianMocks,
|
||||
});
|
||||
|
||||
const EventsListenerModule = proxyquireNonStrict("./src/events-listener", {
|
||||
obsidian: obsidianMocks,
|
||||
"./metadata-store": MetadataStoreModule,
|
||||
});
|
||||
|
||||
const UtilsModule = proxyquireNonStrict("./src/utils", {
|
||||
obsidian: obsidianMocks,
|
||||
});
|
||||
|
||||
const GithubClientModule = proxyquireNonStrict("./src/github/client", {
|
||||
obsidian: obsidianMocks,
|
||||
"src/utils": UtilsModule,
|
||||
});
|
||||
|
||||
const SyncManagerModule = proxyquireNonStrict("./src/sync-manager", {
|
||||
obsidian: obsidianMocks,
|
||||
"./metadata-store": MetadataStoreModule,
|
||||
"./events-listener": EventsListenerModule,
|
||||
"./github/client": GithubClientModule,
|
||||
"./utils": UtilsModule,
|
||||
});
|
||||
|
||||
async function runBenchmark(vaultRootDir: string) {
|
||||
const vault = new obsidianMocks.Vault(vaultRootDir);
|
||||
|
||||
// Create a real logger with our mock vault
|
||||
const logger = new LoggerModule.default(vault, false);
|
||||
|
||||
// Settings for the sync manager
|
||||
const settings = {
|
||||
githubToken: process.env.GITHUB_TOKEN,
|
||||
githubOwner: process.env.REPO_OWNER,
|
||||
githubRepo: process.env.REPO_NAME,
|
||||
githubBranch: process.env.REPO_BRANCH,
|
||||
syncConfigDir: false,
|
||||
};
|
||||
|
||||
// We're not going to get any conflicts, this is useless
|
||||
const onConflicts = async () => {
|
||||
return [];
|
||||
};
|
||||
|
||||
// Create the sync manager
|
||||
const SyncManager = SyncManagerModule.default;
|
||||
const syncManager = new SyncManager(vault, settings, onConflicts, logger);
|
||||
await syncManager.loadMetadata();
|
||||
|
||||
const startTime = performance.now();
|
||||
await syncManager.firstSync();
|
||||
return performance.now() - startTime;
|
||||
}
|
||||
|
||||
const generateRandomFiles = (
|
||||
rootPath: string,
|
||||
numFiles: number,
|
||||
maxDepth: number,
|
||||
fileSize: number,
|
||||
) => {
|
||||
const metadata: { lastSync: number; files: { [key: string]: {} } } = {
|
||||
lastSync: 0,
|
||||
files: {},
|
||||
};
|
||||
|
||||
// Create root directory if it doesn't exist
|
||||
if (!fs.existsSync(rootPath)) {
|
||||
fs.mkdirSync(rootPath, { recursive: true });
|
||||
}
|
||||
|
||||
// Generate folder structure first
|
||||
const allFolderPaths = [rootPath];
|
||||
|
||||
for (let depth = 1; depth <= maxDepth; depth++) {
|
||||
const numFoldersAtThisDepth = Math.floor(Math.random() * 3) + 1; // 1-3 folders per level
|
||||
|
||||
for (let i = 0; i < numFoldersAtThisDepth; i++) {
|
||||
const parentPath =
|
||||
allFolderPaths[Math.floor(Math.random() * allFolderPaths.length)];
|
||||
const currentDepthOfParent =
|
||||
parentPath.split(path.sep).length - rootPath.split(path.sep).length;
|
||||
|
||||
// Only create subfolders if we haven't reached max depth for this path
|
||||
if (currentDepthOfParent < maxDepth) {
|
||||
const folderName = crypto.randomBytes(5).toString("hex");
|
||||
const newFolderPath = path.join(parentPath, folderName);
|
||||
|
||||
fs.mkdirSync(newFolderPath, { recursive: true });
|
||||
allFolderPaths.push(newFolderPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now generate files
|
||||
const contentSize = fileSize / 2; // We divide by two as converting bytes to hex doubles the size
|
||||
|
||||
for (let i = 0; i < numFiles; i++) {
|
||||
// Pick a random folder to place the file in
|
||||
const targetFolder =
|
||||
allFolderPaths[Math.floor(Math.random() * allFolderPaths.length)];
|
||||
|
||||
// Generate random file name
|
||||
const fileName = crypto.randomBytes(8).toString("hex") + ".md";
|
||||
const filePath = path.join(targetFolder, fileName);
|
||||
|
||||
// Generate random content
|
||||
const content = crypto.randomBytes(contentSize).toString("hex");
|
||||
|
||||
// Write file
|
||||
fs.writeFileSync(filePath, content);
|
||||
|
||||
const relativeFilePath = filePath.replace(`${rootPath}/`, "");
|
||||
metadata.files[relativeFilePath] = {
|
||||
path: relativeFilePath,
|
||||
sha: null,
|
||||
dirty: true,
|
||||
justDownloaded: false,
|
||||
lastModified: Date.now(),
|
||||
};
|
||||
}
|
||||
|
||||
const metadataFilePath = path.join(
|
||||
rootPath,
|
||||
".obsidian",
|
||||
"github-sync-metadata.json",
|
||||
);
|
||||
fs.mkdirSync(path.join(rootPath, ".obsidian"));
|
||||
fs.writeFileSync(metadataFilePath, JSON.stringify(metadata), { flag: "w" });
|
||||
};
|
||||
|
||||
const cleanupRemote = () => {
|
||||
const url = `git@github.com:${process.env.REPO_OWNER}/${process.env.REPO_NAME}.git`;
|
||||
const clonedDir = path.join(os.tmpdir(), "temp-clone");
|
||||
|
||||
// Remove the folder in case it already exists
|
||||
fs.rmSync(clonedDir, { recursive: true, force: true });
|
||||
|
||||
try {
|
||||
// Clone the repository
|
||||
execSync(`git clone ${url} ${clonedDir}`, { stdio: "ignore" });
|
||||
|
||||
const repoExists = fs.existsSync(clonedDir);
|
||||
if (!repoExists) {
|
||||
throw Error("Failed to clone repo");
|
||||
}
|
||||
|
||||
// Remove all files except .git
|
||||
execSync('find . -type f -not -path "./.git*" -delete', {
|
||||
stdio: "ignore",
|
||||
cwd: clonedDir,
|
||||
});
|
||||
|
||||
// Commit empty state
|
||||
execSync("git add -A", { stdio: "ignore", cwd: clonedDir });
|
||||
execSync('git commit -m "Cleanup"', {
|
||||
stdio: "ignore",
|
||||
cwd: clonedDir,
|
||||
});
|
||||
|
||||
// Push changes
|
||||
execSync("git push", { stdio: "ignore", cwd: clonedDir });
|
||||
} catch (error) {
|
||||
console.error(`Error: ${error.message}`);
|
||||
}
|
||||
|
||||
// Remove the folder when everything is done
|
||||
fs.rmSync(clonedDir, { recursive: true, force: true });
|
||||
};
|
||||
|
||||
const BENCHMARK_DATA = [
|
||||
{
|
||||
files: 1,
|
||||
maxDepth: 0,
|
||||
// 15 Kb
|
||||
fileSize: 1024 * 15,
|
||||
},
|
||||
{
|
||||
files: 10,
|
||||
maxDepth: 0,
|
||||
// 15 Kb
|
||||
fileSize: 1024 * 15,
|
||||
},
|
||||
{
|
||||
files: 100,
|
||||
maxDepth: 0,
|
||||
// 15 Kb
|
||||
fileSize: 1024 * 15,
|
||||
},
|
||||
{
|
||||
files: 1000,
|
||||
maxDepth: 0,
|
||||
// 15 Kb
|
||||
fileSize: 1024 * 15,
|
||||
},
|
||||
{
|
||||
files: 10000,
|
||||
maxDepth: 0,
|
||||
// 15 Kb
|
||||
fileSize: 1024 * 15,
|
||||
},
|
||||
{
|
||||
files: 100000,
|
||||
maxDepth: 0,
|
||||
// 15 Kb
|
||||
fileSize: 1024 * 15,
|
||||
},
|
||||
];
|
||||
|
||||
(async () => {
|
||||
const tmp = os.tmpdir();
|
||||
const benchmarkRootDir = path.join(tmp, "github-gitless-sync-benchmark");
|
||||
try {
|
||||
const results = [];
|
||||
for (const data of BENCHMARK_DATA) {
|
||||
console.log(
|
||||
`Running benchmark with ${data.files} files totaling ${data.fileSize} bytes`,
|
||||
);
|
||||
const vaultRootDir = path.join(
|
||||
benchmarkRootDir,
|
||||
`${data.files}-${data.maxDepth}-${data.fileSize}`,
|
||||
);
|
||||
// Generates random files
|
||||
generateRandomFiles(
|
||||
vaultRootDir,
|
||||
data.files,
|
||||
data.maxDepth,
|
||||
data.fileSize,
|
||||
);
|
||||
|
||||
// Run first sync by uploading all local files
|
||||
console.log("First sync from local");
|
||||
const uploadTime = await runBenchmark(vaultRootDir);
|
||||
|
||||
// Cleanup vault dir completely
|
||||
fs.rmSync(vaultRootDir, { recursive: true, force: true });
|
||||
|
||||
// Run first sync again, this time we download the files we just uploaded
|
||||
console.log("Second sync from remote");
|
||||
const downloadTime = await runBenchmark(vaultRootDir);
|
||||
|
||||
// Cleanup the remote repo so it's ready for another benchmark
|
||||
cleanupRemote();
|
||||
|
||||
results.push({
|
||||
data,
|
||||
uploadTime,
|
||||
downloadTime,
|
||||
});
|
||||
|
||||
// Cleanup vault dir again, it's not necessary to keep it around
|
||||
fs.rmSync(vaultRootDir, { recursive: true, force: true });
|
||||
|
||||
// Wait 2 seconds between each run just to avoid annoying Github
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||
console.log("");
|
||||
}
|
||||
fs.writeFileSync("benchmark_result.json", JSON.stringify(results), {
|
||||
flag: "w",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Benchmark failed:", error);
|
||||
}
|
||||
fs.rmSync(benchmarkRootDir, { recursive: true, force: true });
|
||||
})();
|
||||
11
manifest-beta.json
Normal file
11
manifest-beta.json
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"id": "github-gitless-sync",
|
||||
"name": "GitHub Gitless Sync",
|
||||
"version": "1.0.7-beta",
|
||||
"minAppVersion": "1.7.7",
|
||||
"description": "Sync a GitHub repository with vaults on different platforms without requiring git installation",
|
||||
"author": "Silvano Cerza",
|
||||
"authorUrl": "https://silvanocerza.com",
|
||||
"fundingUrl": "https://ko-fi.com/silvanocerza",
|
||||
"isDesktopOnly": false
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"id": "github-gitless-sync",
|
||||
"name": "GitHub Gitless Sync",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.7",
|
||||
"minAppVersion": "1.7.7",
|
||||
"description": "Sync a GitHub repository with vaults on different platforms without requiring git installation",
|
||||
"author": "Silvano Cerza",
|
||||
|
|
|
|||
163
mock-obsidian.ts
Normal file
163
mock-obsidian.ts
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
import {
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
promises as fs,
|
||||
} from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
// Mock Obsidian's Vault class
|
||||
export class Vault {
|
||||
configDir: string;
|
||||
private rootPath: string;
|
||||
|
||||
constructor(rootPath: string) {
|
||||
this.rootPath = rootPath;
|
||||
this.configDir = ".obsidian";
|
||||
|
||||
// Ensure vault directory exists
|
||||
if (!existsSync(this.rootPath)) {
|
||||
mkdirSync(this.rootPath, { recursive: true });
|
||||
}
|
||||
|
||||
// Ensure config directory exists
|
||||
if (!existsSync(path.join(this.rootPath, this.configDir))) {
|
||||
mkdirSync(path.join(this.rootPath, this.configDir), { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
getRoot() {
|
||||
return { path: this.rootPath };
|
||||
}
|
||||
|
||||
get adapter() {
|
||||
return {
|
||||
read: async (filePath: string) => {
|
||||
const fullPath = path.join(this.rootPath, filePath);
|
||||
return readFileSync(fullPath, "utf8");
|
||||
},
|
||||
|
||||
write: async (filePath: string, data: string) => {
|
||||
const fullPath = path.join(this.rootPath, filePath);
|
||||
const dir = path.dirname(fullPath);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
writeFileSync(fullPath, data);
|
||||
},
|
||||
|
||||
readBinary: async (filePath: string) => {
|
||||
const fullPath = path.join(this.rootPath, filePath);
|
||||
return readFileSync(fullPath);
|
||||
},
|
||||
|
||||
writeBinary: async (filePath: string, data: ArrayBuffer) => {
|
||||
const fullPath = path.join(this.rootPath, filePath);
|
||||
const dir = path.dirname(fullPath);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
writeFileSync(fullPath, Buffer.from(data));
|
||||
},
|
||||
|
||||
exists: async (filePath: string) => {
|
||||
const fullPath = path.join(this.rootPath, filePath);
|
||||
return existsSync(fullPath);
|
||||
},
|
||||
|
||||
mkdir: async (dirPath: string) => {
|
||||
const fullPath = path.join(this.rootPath, dirPath);
|
||||
if (!existsSync(fullPath)) {
|
||||
mkdirSync(fullPath, { recursive: true });
|
||||
}
|
||||
},
|
||||
|
||||
remove: async (filePath: string) => {
|
||||
const fullPath = path.join(this.rootPath, filePath);
|
||||
if (existsSync(fullPath)) {
|
||||
await fs.unlink(fullPath);
|
||||
}
|
||||
},
|
||||
|
||||
list: async (dirPath: string) => {
|
||||
const fullPath = path.join(this.rootPath, dirPath);
|
||||
if (!existsSync(fullPath)) {
|
||||
return { files: [], folders: [] };
|
||||
}
|
||||
|
||||
const entries = await fs.readdir(fullPath, { withFileTypes: true });
|
||||
const files = entries
|
||||
.filter((entry) => entry.isFile())
|
||||
.map((entry) => path.join(dirPath, entry.name));
|
||||
|
||||
const folders = entries
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map((entry) => path.join(dirPath, entry.name));
|
||||
|
||||
return { files, folders };
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Mock Notice
|
||||
export class Notice {
|
||||
constructor(message: string, timeout?: number) {
|
||||
console.log(`NOTICE: ${message}`);
|
||||
}
|
||||
|
||||
hide() {
|
||||
// Do nothing in mock
|
||||
}
|
||||
}
|
||||
|
||||
interface RequestUrlParam {
|
||||
url: string;
|
||||
method?: string;
|
||||
headers?: Record<string, string>;
|
||||
contentType?: string;
|
||||
body?: string | ArrayBuffer;
|
||||
throw?: boolean;
|
||||
}
|
||||
|
||||
export async function requestUrl(options: RequestUrlParam) {
|
||||
const response = await fetch(options.url, {
|
||||
method: options.method || "GET",
|
||||
headers: options.headers,
|
||||
body: options.body,
|
||||
});
|
||||
|
||||
const isJsonResponse = response.headers
|
||||
.get("content-type")
|
||||
?.includes("application/json");
|
||||
|
||||
// Convert to expected Obsidian response format
|
||||
if (isJsonResponse) {
|
||||
return {
|
||||
status: response.status,
|
||||
json: await response.json(),
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: response.status,
|
||||
arrayBuffer: await response.arrayBuffer(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Mock utility functions
|
||||
export function normalizePath(path: string): string {
|
||||
return path.replace(/\\/g, "/");
|
||||
}
|
||||
|
||||
export function arrayBufferToBase64(buffer: ArrayBuffer): string {
|
||||
return Buffer.from(buffer).toString("base64");
|
||||
}
|
||||
|
||||
export function base64ToArrayBuffer(base64: string): ArrayBuffer {
|
||||
return Buffer.from(base64, "base64");
|
||||
}
|
||||
|
||||
// Mock Event reference
|
||||
export type EventRef = string;
|
||||
12
package.json
12
package.json
|
|
@ -1,26 +1,32 @@
|
|||
{
|
||||
"name": "github-gitless-sync",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.7",
|
||||
"description": "Sync a GitHub repository with vaults on different platforms without requiring git installation",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"version": "node version-bump.mjs"
|
||||
"version": "node version-bump.mjs",
|
||||
"benchmark": "tsx benchmark.ts"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Silvano Cerza",
|
||||
"license": "AGPL-3.0-only",
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.5",
|
||||
"@types/proxyquire": "^1.3.31",
|
||||
"@types/react": "^19.0.4",
|
||||
"@types/react-dom": "^19.0.2",
|
||||
"@types/sinon": "^17.0.4",
|
||||
"@typescript-eslint/eslint-plugin": "^8.19.0",
|
||||
"@typescript-eslint/parser": "^8.19.0",
|
||||
"builtin-modules": "^4.0.0",
|
||||
"esbuild": "^0.24.2",
|
||||
"obsidian": "^1.7.2",
|
||||
"proxyquire": "^2.1.3",
|
||||
"sinon": "^20.0.0",
|
||||
"tslib": "^2.8.1",
|
||||
"tsx": "^4.19.3",
|
||||
"typescript": "^5.7.2"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
@ -28,8 +34,8 @@
|
|||
"@codemirror/state": "^6.5.2",
|
||||
"@codemirror/view": "^6.36.2",
|
||||
"@uiw/react-codemirror": "^4.23.8",
|
||||
"@zip.js/zip.js": "^2.7.60",
|
||||
"codemirror": "^6.0.1",
|
||||
"file-type": "^20.4.1",
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0"
|
||||
}
|
||||
|
|
|
|||
506
pnpm-lock.yaml
506
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
|
|
@ -1,7 +1,7 @@
|
|||
import { Vault, TAbstractFile, TFolder, EventRef } from "obsidian";
|
||||
import { Vault, TAbstractFile, TFolder } from "obsidian";
|
||||
import MetadataStore, { MANIFEST_FILE_NAME } from "./metadata-store";
|
||||
import { GitHubSyncSettings } from "./settings/settings";
|
||||
import Logger from "./logger";
|
||||
import Logger, { LOG_FILE_NAME } from "./logger";
|
||||
import GitHubSyncPlugin from "./main";
|
||||
|
||||
/**
|
||||
|
|
@ -145,6 +145,9 @@ export default class EventsListener {
|
|||
) {
|
||||
// Obsidian recommends not syncing the workspace files
|
||||
return false;
|
||||
} else if (filePath === `${this.vault.configDir}/${LOG_FILE_NAME}`) {
|
||||
// Don't sync the log file, doesn't make sense
|
||||
return false;
|
||||
} else if (
|
||||
this.settings.syncConfigDir &&
|
||||
filePath.startsWith(this.vault.configDir)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { requestUrl } from "obsidian";
|
||||
import Logger from "src/logger";
|
||||
import { GitHubSyncSettings } from "src/settings/settings";
|
||||
import { retryUntil } from "src/utils";
|
||||
|
||||
export type RepoContent = {
|
||||
files: { [key: string]: GetTreeResponseItem };
|
||||
|
|
@ -75,22 +76,35 @@ export default class GithubClient {
|
|||
/**
|
||||
* Gets the content of the repo.
|
||||
*
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
* @returns Array of files in the directory in the remote repo
|
||||
*/
|
||||
async getRepoContent(): Promise<RepoContent> {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/trees/${this.settings.githubBranch}?recursive=1`,
|
||||
headers: this.headers(),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to get repo content", res);
|
||||
async getRepoContent({
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
} = {}): Promise<RepoContent> {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/trees/${this.settings.githubBranch}?recursive=1`,
|
||||
headers: this.headers(),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422, // Retry condition: only retry on 422 status
|
||||
retry ? maxRetries : 0, // Use 0 retries if retry is false
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to get repo content", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to get repo content, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to get repo content, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
const files = res.json.tree
|
||||
|
||||
const files = response.json.tree
|
||||
.filter((file: GetTreeResponseItem) => file.type === "blob")
|
||||
.reduce(
|
||||
(
|
||||
|
|
@ -99,80 +113,168 @@ export default class GithubClient {
|
|||
) => ({ ...acc, [file.path]: file }),
|
||||
{},
|
||||
);
|
||||
return { files, sha: res.json.sha };
|
||||
return { files, sha: response.json.sha };
|
||||
}
|
||||
|
||||
async createTree(tree: { tree: NewTreeRequestItem[]; base_tree: string }) {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/trees`,
|
||||
headers: this.headers(),
|
||||
method: "POST",
|
||||
body: JSON.stringify(tree),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to create tree", res);
|
||||
/**
|
||||
* Creates a new tree in the GitHub repository.
|
||||
*
|
||||
* @param tree The tree object to create
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
* @returns The SHA of the created tree
|
||||
*/
|
||||
async createTree({
|
||||
tree,
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
}: {
|
||||
tree: { tree: NewTreeRequestItem[]; base_tree: string };
|
||||
retry?: boolean;
|
||||
maxRetries?: number;
|
||||
}) {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/trees`,
|
||||
headers: this.headers(),
|
||||
method: "POST",
|
||||
body: JSON.stringify(tree),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to create tree", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to create tree, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to create tree, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
return res.json.sha;
|
||||
return response.json.sha;
|
||||
}
|
||||
|
||||
async createCommit(message: string, treeSha: string, parent: string) {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/commits`,
|
||||
headers: this.headers(),
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
message: message,
|
||||
tree: treeSha,
|
||||
parents: [parent],
|
||||
}),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to create commit", res);
|
||||
/**
|
||||
* Creates a new commit in the repository.
|
||||
*
|
||||
* @param message The commit message
|
||||
* @param treeSha The SHA of the tree
|
||||
* @param parent The SHA of the parent commit
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
* @returns The SHA of the created commit
|
||||
*/
|
||||
async createCommit({
|
||||
message,
|
||||
treeSha,
|
||||
parent,
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
}: {
|
||||
message: string;
|
||||
treeSha: string;
|
||||
parent: string;
|
||||
retry?: boolean;
|
||||
maxRetries?: number;
|
||||
}): Promise<string> {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/commits`,
|
||||
headers: this.headers(),
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
message: message,
|
||||
tree: treeSha,
|
||||
parents: [parent],
|
||||
}),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to create commit", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to create commit, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to create commit, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
return res.json.sha;
|
||||
return response.json.sha;
|
||||
}
|
||||
|
||||
async getBranchHeadSha() {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/refs/heads/${this.settings.githubBranch}`,
|
||||
headers: this.headers(),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to get branch head sha", res);
|
||||
/**
|
||||
* Gets the SHA of the branch head.
|
||||
*
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
* @returns The SHA of the branch head
|
||||
*/
|
||||
async getBranchHeadSha({ retry = false, maxRetries = 5 } = {}) {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/refs/heads/${this.settings.githubBranch}`,
|
||||
headers: this.headers(),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to get branch head sha", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to get branch head sha, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to get branch head sha, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
return res.json.object.sha;
|
||||
return response.json.object.sha;
|
||||
}
|
||||
|
||||
async updateBranchHead(sha: string) {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/refs/heads/${this.settings.githubBranch}`,
|
||||
headers: this.headers(),
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({
|
||||
sha: sha,
|
||||
}),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to update branch head sha", res);
|
||||
/**
|
||||
* Updates the branch head to point to a new commit.
|
||||
*
|
||||
* @param sha The SHA of the commit to point to
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
*/
|
||||
async updateBranchHead({
|
||||
sha,
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
}: {
|
||||
sha: string;
|
||||
retry?: boolean;
|
||||
maxRetries?: number;
|
||||
}) {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/refs/heads/${this.settings.githubBranch}`,
|
||||
headers: this.headers(),
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({
|
||||
sha: sha,
|
||||
}),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to update branch head sha", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to update branch head sha, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to update branch head sha, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -182,76 +284,166 @@ export default class GithubClient {
|
|||
*
|
||||
* @param content The content of the blob to upload
|
||||
* @param encoding Content encoding, can be "utf-8" or "base64". Defaults to "base64"
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
* @returns The SHA of the newly uploaded blob
|
||||
*/
|
||||
async createBlob(
|
||||
content: string,
|
||||
encoding: "utf-8" | "base64" = "base64",
|
||||
): Promise<CreatedBlob> {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/blobs`,
|
||||
headers: this.headers(),
|
||||
method: "POST",
|
||||
body: JSON.stringify({ content, encoding }),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to create blob", res);
|
||||
async createBlob({
|
||||
content,
|
||||
encoding = "base64",
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
}: {
|
||||
content: string;
|
||||
encoding?: "utf-8" | "base64";
|
||||
retry?: boolean;
|
||||
maxRetries?: number;
|
||||
}): Promise<CreatedBlob> {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/blobs`,
|
||||
headers: this.headers(),
|
||||
method: "POST",
|
||||
body: JSON.stringify({ content, encoding }),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to create blob", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to create blob, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to create blob, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
return {
|
||||
sha: res.json["sha"],
|
||||
sha: response.json["sha"],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a blob from its sha
|
||||
* @param url blob sha
|
||||
*
|
||||
* @param sha The SHA of the blob
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
* @returns The blob file
|
||||
*/
|
||||
async getBlob(sha: string): Promise<BlobFile> {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/blobs/${sha}`,
|
||||
headers: this.headers(),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to get blob", res);
|
||||
async getBlob({
|
||||
sha,
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
}: {
|
||||
sha: string;
|
||||
retry?: boolean;
|
||||
maxRetries?: number;
|
||||
}): Promise<BlobFile> {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/git/blobs/${sha}`,
|
||||
headers: this.headers(),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to get blob", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to get blob, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to get blob, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
return res.json;
|
||||
return response.json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new file in the repo, the content must be base64 encoded or the request will fail.
|
||||
*
|
||||
* @param message commit message
|
||||
* @param path path to create in the repo
|
||||
* @param content base64 encoded content of the file
|
||||
* @param path Path to create in the repo
|
||||
* @param content Base64 encoded content of the file
|
||||
* @param message Commit message
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
*/
|
||||
async createFile(path: string, content: string, message: string) {
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/contents/${path}`,
|
||||
headers: this.headers(),
|
||||
method: "PUT",
|
||||
body: JSON.stringify({
|
||||
message: message,
|
||||
content: content,
|
||||
branch: this.settings.githubBranch,
|
||||
}),
|
||||
throw: false,
|
||||
});
|
||||
if (res.status < 200 || res.status >= 400) {
|
||||
await this.logger.error("Failed to create file", res);
|
||||
async createFile({
|
||||
path,
|
||||
content,
|
||||
message,
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
}: {
|
||||
path: string;
|
||||
content: string;
|
||||
message: string;
|
||||
retry?: boolean;
|
||||
maxRetries?: number;
|
||||
}) {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/contents/${path}`,
|
||||
headers: this.headers(),
|
||||
method: "PUT",
|
||||
body: JSON.stringify({
|
||||
message: message,
|
||||
content: content,
|
||||
branch: this.settings.githubBranch,
|
||||
}),
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to create file", response);
|
||||
throw new GithubAPIError(
|
||||
res.status,
|
||||
`Failed to create file, status ${res.status}`,
|
||||
response.status,
|
||||
`Failed to create file, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads the repository as a ZIP archive from GitHub.
|
||||
*
|
||||
* @param retry Whether to retry the request on failure (default: false)
|
||||
* @param maxRetries Maximum number of retry attempts (default: 5)
|
||||
* @returns The archive contents as an ArrayBuffer
|
||||
*/
|
||||
async downloadRepositoryArchive({
|
||||
retry = false,
|
||||
maxRetries = 5,
|
||||
} = {}): Promise<ArrayBuffer> {
|
||||
const response = await retryUntil(
|
||||
async () => {
|
||||
return requestUrl({
|
||||
url: `https://api.github.com/repos/${this.settings.githubOwner}/${this.settings.githubRepo}/zipball/${this.settings.githubBranch}`,
|
||||
headers: this.headers(),
|
||||
method: "GET",
|
||||
throw: false,
|
||||
});
|
||||
},
|
||||
(res) => res.status !== 422,
|
||||
retry ? maxRetries : 0,
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
await this.logger.error("Failed to download zip archive", response);
|
||||
throw new GithubAPIError(
|
||||
response.status,
|
||||
`Failed to download zip archive, status ${response.status}`,
|
||||
);
|
||||
}
|
||||
return response.arrayBuffer;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { Vault, normalizePath } from "obsidian";
|
||||
|
||||
const LOG_FILE_NAME = "github-sync.log" as const;
|
||||
export const LOG_FILE_NAME = "github-sync.log" as const;
|
||||
|
||||
export default class Logger {
|
||||
private logFile: string;
|
||||
|
|
@ -40,6 +40,14 @@ export default class Logger {
|
|||
);
|
||||
}
|
||||
|
||||
async read(): Promise<string> {
|
||||
return await this.vault.adapter.read(this.logFile);
|
||||
}
|
||||
|
||||
async clean(): Promise<void> {
|
||||
return await this.vault.adapter.write(this.logFile, "");
|
||||
}
|
||||
|
||||
enable(): void {
|
||||
this.enabled = true;
|
||||
}
|
||||
|
|
|
|||
16
src/main.ts
16
src/main.ts
|
|
@ -152,9 +152,19 @@ export default class GitHubSyncPlugin extends Plugin {
|
|||
return;
|
||||
}
|
||||
if (this.settings.firstSync) {
|
||||
await this.syncManager.firstSync();
|
||||
this.settings.firstSync = false;
|
||||
this.saveSettings();
|
||||
const notice = new Notice("Syncing...");
|
||||
try {
|
||||
await this.syncManager.firstSync();
|
||||
this.settings.firstSync = false;
|
||||
this.saveSettings();
|
||||
// Shown only if sync doesn't fail
|
||||
new Notice("Sync successful", 5000);
|
||||
} catch (err) {
|
||||
// Show the error to the user, it's not automatically dismissed to make sure
|
||||
// the user sees it.
|
||||
new Notice(`Error syncing. ${err}`);
|
||||
}
|
||||
notice.hide();
|
||||
} else {
|
||||
await this.syncManager.sync();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,13 @@
|
|||
import { PluginSettingTab, App, Setting, TextComponent, Modal } from "obsidian";
|
||||
import {
|
||||
PluginSettingTab,
|
||||
App,
|
||||
Setting,
|
||||
TextComponent,
|
||||
Modal,
|
||||
Notice,
|
||||
} from "obsidian";
|
||||
import GitHubSyncPlugin from "src/main";
|
||||
import { copyToClipboard } from "src/utils";
|
||||
|
||||
export default class GitHubSyncSettingsTab extends PluginSettingTab {
|
||||
plugin: GitHubSyncPlugin;
|
||||
|
|
@ -268,6 +276,30 @@ export default class GitHubSyncSettingsTab extends PluginSettingTab {
|
|||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Copy logs")
|
||||
.setDesc("Copy the log file content, this is useful to report bugs.")
|
||||
.addButton((button) => {
|
||||
button.setButtonText("Copy").onClick(async () => {
|
||||
const logs: string = await this.plugin.logger.read();
|
||||
try {
|
||||
await copyToClipboard(logs);
|
||||
new Notice("Logs copied", 5000);
|
||||
} catch (err) {
|
||||
new Notice(`Failed copying logs: ${err}`, 10000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Clean logs")
|
||||
.setDesc("Delete all existing logs.")
|
||||
.addButton((button) => {
|
||||
button.setButtonText("Clean").onClick(async () => {
|
||||
await this.plugin.logger.clean();
|
||||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Reset")
|
||||
.setDesc("Reset the plugin settings and metadata")
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import {
|
|||
Notice,
|
||||
normalizePath,
|
||||
base64ToArrayBuffer,
|
||||
EventRef,
|
||||
arrayBufferToBase64,
|
||||
} from "obsidian";
|
||||
import GithubClient, {
|
||||
|
|
@ -18,10 +17,10 @@ import MetadataStore, {
|
|||
} from "./metadata-store";
|
||||
import EventsListener from "./events-listener";
|
||||
import { GitHubSyncSettings } from "./settings/settings";
|
||||
import Logger from "./logger";
|
||||
import { decodeBase64String } from "./utils";
|
||||
import Logger, { LOG_FILE_NAME } from "./logger";
|
||||
import { decodeBase64String, hasTextExtension } from "./utils";
|
||||
import GitHubSyncPlugin from "./main";
|
||||
import { fileTypeFromBuffer } from "file-type";
|
||||
import { BlobReader, Entry, Uint8ArrayWriter, ZipReader } from "@zip.js/zip.js";
|
||||
|
||||
interface SyncAction {
|
||||
type: "upload" | "download" | "delete_local" | "delete_remote";
|
||||
|
|
@ -96,19 +95,14 @@ export default class SyncManager {
|
|||
return;
|
||||
}
|
||||
|
||||
const notice = new Notice("Syncing...");
|
||||
this.syncing = true;
|
||||
try {
|
||||
await this.firstSyncImpl();
|
||||
// Shown only if sync doesn't fail
|
||||
new Notice("Sync successful", 5000);
|
||||
} catch (err) {
|
||||
// Show the error to the user, it's not automatically dismissed to make sure
|
||||
// the user sees it.
|
||||
new Notice(`Error syncing. ${err}`);
|
||||
this.syncing = false;
|
||||
throw err;
|
||||
}
|
||||
this.syncing = false;
|
||||
notice.hide();
|
||||
}
|
||||
|
||||
private async firstSyncImpl() {
|
||||
|
|
@ -143,14 +137,18 @@ export default class SyncManager {
|
|||
// API doesn't let us create a new tree when the repo is empty.
|
||||
// So we create a the manifest file as the first commit, since we're going
|
||||
// to create that in any case right after this.
|
||||
await this.client.createFile(
|
||||
`${this.vault.configDir}/${MANIFEST_FILE_NAME}`,
|
||||
"",
|
||||
"First sync",
|
||||
const buffer = await this.vault.adapter.readBinary(
|
||||
normalizePath(`${this.vault.configDir}/${MANIFEST_FILE_NAME}`),
|
||||
);
|
||||
await this.client.createFile({
|
||||
path: `${this.vault.configDir}/${MANIFEST_FILE_NAME}`,
|
||||
content: arrayBufferToBase64(buffer),
|
||||
message: "First sync",
|
||||
retry: true,
|
||||
});
|
||||
// Now get the repo content again cause we know for sure it will return a
|
||||
// valid sha that we can use to create the first sync commit.
|
||||
res = await this.client.getRepoContent();
|
||||
res = await this.client.getRepoContent({ retry: true });
|
||||
files = res.files;
|
||||
treeSha = res.sha;
|
||||
}
|
||||
|
|
@ -188,24 +186,98 @@ export default class SyncManager {
|
|||
treeSha: string,
|
||||
) {
|
||||
await this.logger.info("Starting first sync from remote files");
|
||||
|
||||
// We want to avoid getting throttled by GitHub, so instead of making a request for each
|
||||
// file we download the whole repository as a ZIP file and extract it in the vault.
|
||||
// We exclude config dir files if the user doesn't want to sync those.
|
||||
const zipBuffer = await this.client.downloadRepositoryArchive();
|
||||
const zipBlob = new Blob([zipBuffer]);
|
||||
const reader = new ZipReader(new BlobReader(zipBlob));
|
||||
const entries = await reader.getEntries();
|
||||
|
||||
await this.logger.info("Extracting files from ZIP", {
|
||||
length: entries.length,
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
Object.keys(files)
|
||||
.filter((filePath: string) => {
|
||||
if (
|
||||
this.settings.syncConfigDir &&
|
||||
filePath.startsWith(this.vault.configDir) &&
|
||||
filePath !== `${this.vault.configDir}/${MANIFEST_FILE_NAME}`
|
||||
) {
|
||||
// Include files in the config dir only if the user has enabled it.
|
||||
// The metadata file must always be synced.
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.map(async (filePath: string) => {
|
||||
await this.downloadFile(files[filePath], Date.now());
|
||||
}),
|
||||
entries.map(async (entry: Entry) => {
|
||||
// All repo ZIPs contain a root directory that contains all the content
|
||||
// of that repo, we need to ignore that directory so we strip the first
|
||||
// folder segment from the path
|
||||
const pathParts = entry.filename.split("/");
|
||||
const targetPath =
|
||||
pathParts.length > 1 ? pathParts.slice(1).join("/") : entry.filename;
|
||||
|
||||
if (targetPath === "") {
|
||||
// Must be the root folder, skip it.
|
||||
// This is really important as that would lead us to try and
|
||||
// create the folder "/" and crash Obsidian
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
this.settings.syncConfigDir &&
|
||||
targetPath.startsWith(this.vault.configDir) &&
|
||||
targetPath !== `${this.vault.configDir}/${MANIFEST_FILE_NAME}`
|
||||
) {
|
||||
await this.logger.info("Skipped config", { targetPath });
|
||||
return;
|
||||
}
|
||||
|
||||
if (entry.directory) {
|
||||
const normalizedPath = normalizePath(targetPath);
|
||||
await this.vault.adapter.mkdir(normalizedPath);
|
||||
await this.logger.info("Created directory", {
|
||||
normalizedPath,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetPath === `${this.vault.configDir}/${LOG_FILE_NAME}`) {
|
||||
// We don't want to download the log file if the user synced it in the past.
|
||||
// This is necessary because in the past we forgot to ignore the log file
|
||||
// from syncing if the user enabled configs sync.
|
||||
// To avoid downloading it we ignore it if still present in the remote repo.
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetPath.split("/").last()?.startsWith(".")) {
|
||||
// We must skip hidden files as that creates issues with syncing.
|
||||
// This is fine as users can't edit hidden files in Obsidian anyway.
|
||||
await this.logger.info("Skipping hidden file", targetPath);
|
||||
return;
|
||||
}
|
||||
|
||||
const writer = new Uint8ArrayWriter();
|
||||
await entry.getData!(writer);
|
||||
const data = await writer.getData();
|
||||
const dir = targetPath.split("/").splice(0, -1).join("/");
|
||||
if (dir !== "") {
|
||||
const normalizedDir = normalizePath(dir);
|
||||
await this.vault.adapter.mkdir(normalizedDir);
|
||||
await this.logger.info("Created directory", {
|
||||
normalizedDir,
|
||||
});
|
||||
}
|
||||
|
||||
const normalizedPath = normalizePath(targetPath);
|
||||
await this.vault.adapter.writeBinary(normalizedPath, data);
|
||||
await this.logger.info("Written file", {
|
||||
normalizedPath,
|
||||
});
|
||||
this.metadataStore.data.files[normalizedPath] = {
|
||||
path: normalizedPath,
|
||||
sha: files[normalizedPath].sha,
|
||||
dirty: false,
|
||||
justDownloaded: true,
|
||||
lastModified: Date.now(),
|
||||
};
|
||||
await this.metadataStore.save();
|
||||
}),
|
||||
);
|
||||
|
||||
await this.logger.info("Extracted zip");
|
||||
|
||||
const newTreeFiles = Object.keys(files)
|
||||
.map((filePath: string) => ({
|
||||
path: files[filePath].path,
|
||||
|
|
@ -222,18 +294,33 @@ export default class SyncManager {
|
|||
);
|
||||
// Add files that are in the manifest but not in the tree.
|
||||
await Promise.all(
|
||||
Object.keys(this.metadataStore.data.files).map(
|
||||
async (filePath: string) => {
|
||||
Object.keys(this.metadataStore.data.files)
|
||||
.filter((filePath: string) => {
|
||||
return !Object.keys(files).contains(filePath);
|
||||
})
|
||||
.map(async (filePath: string) => {
|
||||
const normalizedPath = normalizePath(filePath);
|
||||
const content = await this.vault.adapter.read(normalizedPath);
|
||||
// We need to check whether the file is a text file or not before
|
||||
// reading it here because trying to read a binary file as text fails
|
||||
// on iOS, and probably on other mobile devices too, so we read the file
|
||||
// content only if we're sure it contains text only.
|
||||
//
|
||||
// It's fine not reading the binary file in here and just setting some bogus
|
||||
// content because when committing the sync we're going to read the binary
|
||||
// file and upload its blob if it needs to be synced. The important thing is
|
||||
// that some content is set so we know the file changed locally and needs to be
|
||||
// uploaded.
|
||||
let content = "binaryfile";
|
||||
if (hasTextExtension(normalizedPath)) {
|
||||
content = await this.vault.adapter.read(normalizedPath);
|
||||
}
|
||||
newTreeFiles[filePath] = {
|
||||
path: filePath,
|
||||
mode: "100644",
|
||||
type: "blob",
|
||||
content: content,
|
||||
content,
|
||||
};
|
||||
},
|
||||
),
|
||||
}),
|
||||
);
|
||||
await this.commitSync(newTreeFiles, treeSha);
|
||||
}
|
||||
|
|
@ -266,18 +353,36 @@ export default class SyncManager {
|
|||
{},
|
||||
);
|
||||
await Promise.all(
|
||||
Object.keys(this.metadataStore.data.files).map(
|
||||
async (filePath: string) => {
|
||||
Object.keys(this.metadataStore.data.files)
|
||||
.filter((filePath: string) => {
|
||||
// We should not try to sync deleted files, this can happen when
|
||||
// the user renames or deletes files after enabling the plugin but
|
||||
// before syncing for the first time
|
||||
return !this.metadataStore.data.files[filePath].deleted;
|
||||
})
|
||||
.map(async (filePath: string) => {
|
||||
const normalizedPath = normalizePath(filePath);
|
||||
const content = await this.vault.adapter.read(normalizedPath);
|
||||
// We need to check whether the file is a text file or not before
|
||||
// reading it here because trying to read a binary file as text fails
|
||||
// on iOS, and probably on other mobile devices too, so we read the file
|
||||
// content only if we're sure it contains text only.
|
||||
//
|
||||
// It's fine not reading the binary file in here and just setting some bogus
|
||||
// content because when committing the sync we're going to read the binary
|
||||
// file and upload its blob if it needs to be synced. The important thing is
|
||||
// that some content is set so we know the file changed locally and needs to be
|
||||
// uploaded.
|
||||
let content = "binaryfile";
|
||||
if (hasTextExtension(normalizedPath)) {
|
||||
content = await this.vault.adapter.read(normalizedPath);
|
||||
}
|
||||
newTreeFiles[filePath] = {
|
||||
path: filePath,
|
||||
mode: "100644",
|
||||
type: "blob",
|
||||
content: content,
|
||||
content,
|
||||
};
|
||||
},
|
||||
),
|
||||
}),
|
||||
);
|
||||
await this.commitSync(newTreeFiles, treeSha);
|
||||
}
|
||||
|
|
@ -310,7 +415,9 @@ export default class SyncManager {
|
|||
|
||||
private async syncImpl() {
|
||||
await this.logger.info("Starting sync");
|
||||
const { files, sha: treeSha } = await this.client.getRepoContent();
|
||||
const { files, sha: treeSha } = await this.client.getRepoContent({
|
||||
retry: true,
|
||||
});
|
||||
const manifest = files[`${this.vault.configDir}/${MANIFEST_FILE_NAME}`];
|
||||
|
||||
if (manifest === undefined) {
|
||||
|
|
@ -318,7 +425,17 @@ export default class SyncManager {
|
|||
throw new Error("Remote manifest is missing");
|
||||
}
|
||||
|
||||
const blob = await this.client.getBlob(manifest.sha);
|
||||
if (
|
||||
Object.keys(files).contains(`${this.vault.configDir}/${LOG_FILE_NAME}`)
|
||||
) {
|
||||
// We don't want to download the log file if the user synced it in the past.
|
||||
// This is necessary because in the past we forgot to ignore the log file
|
||||
// from syncing if the user enabled configs sync.
|
||||
// To avoid downloading it we delete it if still around.
|
||||
delete files[`${this.vault.configDir}/${LOG_FILE_NAME}`];
|
||||
}
|
||||
|
||||
const blob = await this.client.getBlob({ sha: manifest.sha });
|
||||
const remoteMetadata: Metadata = JSON.parse(
|
||||
decodeBase64String(blob.content),
|
||||
);
|
||||
|
|
@ -514,9 +631,11 @@ export default class SyncManager {
|
|||
// Load contents in parallel
|
||||
const [remoteContent, localContent] = await Promise.all([
|
||||
await (async () => {
|
||||
const res = await this.client.getBlob(
|
||||
filesMetadata[filePath].sha!,
|
||||
);
|
||||
const res = await this.client.getBlob({
|
||||
sha: filesMetadata[filePath].sha!,
|
||||
retry: true,
|
||||
maxRetries: 1,
|
||||
});
|
||||
return decodeBase64String(res.content);
|
||||
})(),
|
||||
await this.vault.adapter.read(normalizePath(filePath)),
|
||||
|
|
@ -581,16 +700,19 @@ export default class SyncManager {
|
|||
type: "delete_local",
|
||||
filePath: filePath,
|
||||
});
|
||||
return;
|
||||
} else if (
|
||||
localFile.lastModified > (remoteFile.deletedAt as number)
|
||||
) {
|
||||
actions.push({ type: "upload", filePath: filePath });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!remoteFile.deleted && localFile.deleted) {
|
||||
if (remoteFile.lastModified > (localFile.deletedAt as number)) {
|
||||
actions.push({ type: "download", filePath: filePath });
|
||||
return;
|
||||
} else if (
|
||||
(localFile.deletedAt as number) > remoteFile.lastModified
|
||||
) {
|
||||
|
|
@ -598,6 +720,7 @@ export default class SyncManager {
|
|||
type: "delete_remote",
|
||||
filePath: filePath,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -605,8 +728,10 @@ export default class SyncManager {
|
|||
// Conflicts are already filtered out so we can make this decision easily
|
||||
if (localSHA !== localFile.sha) {
|
||||
actions.push({ type: "upload", filePath: filePath });
|
||||
return;
|
||||
} else {
|
||||
actions.push({ type: "download", filePath: filePath });
|
||||
return;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
|
@ -665,9 +790,13 @@ export default class SyncManager {
|
|||
* This is the same identical algoritm used by git to calculate
|
||||
* a blob's SHA.
|
||||
* @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 contentBytes = new Uint8Array(contentBuffer);
|
||||
const header = new TextEncoder().encode(`blob ${contentBytes.length}\0`);
|
||||
|
|
@ -724,32 +853,29 @@ export default class SyncManager {
|
|||
Object.keys(treeFiles)
|
||||
.filter((filePath: string) => treeFiles[filePath].content)
|
||||
.map(async (filePath: string) => {
|
||||
const buffer = await this.vault.adapter.readBinary(filePath);
|
||||
const fileType = await fileTypeFromBuffer(buffer);
|
||||
let newSha = "";
|
||||
if (
|
||||
// We can't determine the file type
|
||||
fileType === undefined ||
|
||||
// This is not a text file
|
||||
!fileType.mime.startsWith("text/") ||
|
||||
// Neither a json file
|
||||
fileType.mime !== "application/json"
|
||||
) {
|
||||
// We treat this file as a binary file. We can't upload these setting the content
|
||||
// of a tree item, we first need to create a Git blob by uploading the file, then
|
||||
// we must update the tree item to point the SHA to the blob we just created.
|
||||
const hash = arrayBufferToBase64(buffer);
|
||||
const { sha } = await this.client.createBlob(hash);
|
||||
treeFiles[filePath].sha = sha;
|
||||
// Can't have both sha and content set, so we delete it
|
||||
delete treeFiles[filePath].content;
|
||||
newSha = sha;
|
||||
} else {
|
||||
// File is text, we can upload the content directly
|
||||
// so we just calculate the new SHA to keep track of it
|
||||
newSha = await this.calculateSHA(filePath);
|
||||
// I don't fully trust file extensions as they're not completely reliable
|
||||
// to determine the file type, though I feel it's ok to compromise and rely
|
||||
// on them if it makes the plugin handle upload better on certain devices.
|
||||
if (hasTextExtension(filePath)) {
|
||||
const sha = await this.calculateSHA(filePath);
|
||||
this.metadataStore.data.files[filePath].sha = sha;
|
||||
return;
|
||||
}
|
||||
this.metadataStore.data.files[filePath].sha = newSha;
|
||||
|
||||
// We can't upload binary files by setting the content of a tree item,
|
||||
// we first need to create a Git blob by uploading the file, then
|
||||
// we must update the tree item to point the SHA to the blob we just created.
|
||||
const buffer = await this.vault.adapter.readBinary(filePath);
|
||||
const { sha } = await this.client.createBlob({
|
||||
content: arrayBufferToBase64(buffer),
|
||||
retry: true,
|
||||
maxRetries: 3,
|
||||
});
|
||||
await this.logger.info("Created blob", filePath);
|
||||
treeFiles[filePath].sha = sha;
|
||||
// Can't have both sha and content set, so we delete it
|
||||
delete treeFiles[filePath].content;
|
||||
this.metadataStore.data.files[filePath].sha = sha;
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
@ -765,18 +891,21 @@ export default class SyncManager {
|
|||
),
|
||||
base_tree: baseTreeSha,
|
||||
};
|
||||
const newTreeSha = await this.client.createTree(newTree);
|
||||
const newTreeSha = await this.client.createTree({
|
||||
tree: newTree,
|
||||
retry: true,
|
||||
});
|
||||
|
||||
const branchHeadSha = await this.client.getBranchHeadSha();
|
||||
const branchHeadSha = await this.client.getBranchHeadSha({ retry: true });
|
||||
|
||||
const commitSha = await this.client.createCommit(
|
||||
const commitSha = await this.client.createCommit({
|
||||
// TODO: Make this configurable or find a nicer commit message
|
||||
"Sync",
|
||||
newTreeSha,
|
||||
branchHeadSha,
|
||||
);
|
||||
message: "Sync",
|
||||
treeSha: newTreeSha,
|
||||
parent: branchHeadSha,
|
||||
});
|
||||
|
||||
await this.client.updateBranchHead(commitSha);
|
||||
await this.client.updateBranchHead({ sha: commitSha, retry: true });
|
||||
|
||||
// Update the local content of all files that had conflicts we resolved
|
||||
await Promise.all(
|
||||
|
|
@ -803,7 +932,7 @@ export default class SyncManager {
|
|||
// File already exists and has the same SHA, no need to download it again.
|
||||
return;
|
||||
}
|
||||
const blob = await this.client.getBlob(file.sha);
|
||||
const blob = await this.client.getBlob({ sha: file.sha, retry: true });
|
||||
const normalizedPath = normalizePath(file.path);
|
||||
const fileFolder = normalizePath(
|
||||
normalizedPath.split("/").slice(0, -1).join("/"),
|
||||
|
|
|
|||
85
src/utils.ts
85
src/utils.ts
|
|
@ -1,5 +1,15 @@
|
|||
import { base64ToArrayBuffer } from "obsidian";
|
||||
|
||||
const TEXT_EXTENSIONS = [
|
||||
".css",
|
||||
".md",
|
||||
".json",
|
||||
".txt",
|
||||
".csv",
|
||||
".js",
|
||||
".log",
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Decodes a base64 encoded string, this properly
|
||||
* handles emojis and other non ASCII chars.
|
||||
|
|
@ -12,3 +22,78 @@ export function decodeBase64String(s: string): string {
|
|||
const decoder = new TextDecoder();
|
||||
return decoder.decode(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the provided text to the system clipboard.
|
||||
* Uses the modern Clipboard API with a fallback to older APIs.
|
||||
*
|
||||
* @param text The string to be copied to clipboard
|
||||
* @returns A promise that resolves when the text has been copied
|
||||
*/
|
||||
export async function copyToClipboard(text: string) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} catch (err) {
|
||||
// Fallback for devices like iOS that don't support Clipboard API
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.setAttribute("readonly", "");
|
||||
textarea.style.position = "absolute";
|
||||
textarea.style.left = "-9999px";
|
||||
document.body.appendChild(textarea);
|
||||
|
||||
textarea.select();
|
||||
document.execCommand("copy");
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file path has one of the predefined text extensions.
|
||||
* This is a best guess at best.
|
||||
*
|
||||
* @param filePath The path of the file to check
|
||||
* @returns True if the file has a text extension, false otherwise
|
||||
*/
|
||||
export function hasTextExtension(filePath: string) {
|
||||
for (const extension of TEXT_EXTENSIONS) {
|
||||
if (filePath.endsWith(extension)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retries an async function until its return value satisfies a condition or max retries is reached.
|
||||
* Uses exponential backoff between retry attempts.
|
||||
*
|
||||
* @param fn - The async function to execute and potentially retry
|
||||
* @param condition - Function that evaluates if the result is acceptable
|
||||
* @param maxRetries - Maximum number of retry attempts (default: 5)
|
||||
* @param initialDelay - Initial delay in ms before first retry (default: 1000)
|
||||
* @param backoffFactor - Multiplicative factor for delay between retries (default: 2)
|
||||
* @returns The result of the function execution
|
||||
*/
|
||||
export async function retryUntil<T>(
|
||||
fn: () => Promise<T>,
|
||||
condition: (result: T) => boolean,
|
||||
maxRetries: number = 5,
|
||||
initialDelay: number = 1000,
|
||||
backoffFactor: number = 2,
|
||||
): Promise<T> {
|
||||
let retries = 0;
|
||||
let delay = initialDelay;
|
||||
|
||||
while (true) {
|
||||
const result = await fn();
|
||||
|
||||
if (condition(result) || retries >= maxRetries) {
|
||||
return result;
|
||||
}
|
||||
|
||||
retries++;
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
delay *= backoffFactor;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -175,7 +175,6 @@ const ActionsGutter: React.FC<ActionsGutterProps> = ({
|
|||
style={{
|
||||
width: "100%",
|
||||
height: "100%",
|
||||
overflow: "hidden",
|
||||
position: "relative",
|
||||
backgroundColor: "var(--background-secondary)",
|
||||
}}
|
||||
|
|
|
|||
|
|
@ -36,13 +36,11 @@ const DiffView: React.FC<DiffViewProps> = ({
|
|||
return (
|
||||
<div
|
||||
style={{
|
||||
width: "100%",
|
||||
height: "100%",
|
||||
display: "flex",
|
||||
overflow: "hidden",
|
||||
}}
|
||||
>
|
||||
<div style={{ flex: 1, overflow: "hidden" }}>
|
||||
<div style={{ flex: 1 }}>
|
||||
<EditorPane
|
||||
content={remoteText}
|
||||
highlightPluginSpec={{
|
||||
|
|
@ -142,7 +140,7 @@ const DiffView: React.FC<DiffViewProps> = ({
|
|||
}}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ flex: 1, overflow: "hidden" }}>
|
||||
<div style={{ flex: 1 }}>
|
||||
<EditorPane
|
||||
content={localText}
|
||||
highlightPluginSpec={{
|
||||
|
|
|
|||
|
|
@ -46,9 +46,9 @@ const SplitView = ({
|
|||
<React.StrictMode>
|
||||
<div
|
||||
style={{
|
||||
height: "100%",
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
height: "100%",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
|
|
@ -88,21 +88,28 @@ const SplitView = ({
|
|||
currentFile={currentFile?.filePath || ""}
|
||||
setCurrentFileIndex={setCurrentFileIndex}
|
||||
/>
|
||||
<DiffView
|
||||
remoteText={currentFile?.remoteContent || ""}
|
||||
localText={currentFile?.localContent || ""}
|
||||
onRemoteTextChange={(content: string) => {
|
||||
const tempFiles = [...files];
|
||||
tempFiles[currentFileIndex].remoteContent = content;
|
||||
setFiles(tempFiles);
|
||||
<div
|
||||
style={{
|
||||
overflow: "auto",
|
||||
flex: 1,
|
||||
}}
|
||||
onLocalTextChange={(content: string) => {
|
||||
const tempFiles = [...files];
|
||||
tempFiles[currentFileIndex].localContent = content;
|
||||
setFiles(tempFiles);
|
||||
}}
|
||||
onConflictResolved={onConflictResolved}
|
||||
/>
|
||||
>
|
||||
<DiffView
|
||||
remoteText={currentFile?.remoteContent || ""}
|
||||
localText={currentFile?.localContent || ""}
|
||||
onRemoteTextChange={(content: string) => {
|
||||
const tempFiles = [...files];
|
||||
tempFiles[currentFileIndex].remoteContent = content;
|
||||
setFiles(tempFiles);
|
||||
}}
|
||||
onLocalTextChange={(content: string) => {
|
||||
const tempFiles = [...files];
|
||||
tempFiles[currentFileIndex].localContent = content;
|
||||
setFiles(tempFiles);
|
||||
}}
|
||||
onConflictResolved={onConflictResolved}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -183,7 +183,15 @@ const DiffView: React.FC<DiffViewProps> = ({
|
|||
}, [initialRemoteText, initialLocalText]);
|
||||
|
||||
return (
|
||||
<div style={{ width: "100%", height: "100%", overflow: "hidden" }}>
|
||||
<div
|
||||
style={{
|
||||
width: "100%",
|
||||
height: "100%",
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
overflow: "hidden",
|
||||
}}
|
||||
>
|
||||
<CodeMirror
|
||||
value={doc}
|
||||
height="100%"
|
||||
|
|
|
|||
|
|
@ -42,6 +42,8 @@ const UnifiedView = ({
|
|||
paddingTop: "var(--size-4-4)",
|
||||
paddingBottom: "var(--size-4-4)",
|
||||
borderBottom: "1px solid var(--background-modifier-border)",
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
|
|
@ -67,7 +69,14 @@ const UnifiedView = ({
|
|||
|
||||
return (
|
||||
<React.StrictMode>
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
height: "100%",
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
overflow: "auto",
|
||||
}}
|
||||
>
|
||||
{files.length === 0 ? (
|
||||
<div
|
||||
style={{
|
||||
|
|
|
|||
|
|
@ -4,4 +4,6 @@
|
|||
|
||||
.padless-conflicts-view-container {
|
||||
padding: 0;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,5 +12,6 @@
|
|||
"1.0.3": "1.7.7",
|
||||
"1.0.4": "1.7.7",
|
||||
"1.0.5": "1.7.7",
|
||||
"1.0.6": "1.7.7"
|
||||
"1.0.6": "1.7.7",
|
||||
"1.0.7": "1.7.7"
|
||||
}
|
||||
Loading…
Reference in a new issue