Skip to content

Commit

Permalink
Merge branch 'fix-from-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuakto committed Mar 5, 2024
2 parents df90b61 + 80d2491 commit 228fc65
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 140 deletions.
43 changes: 19 additions & 24 deletions main.ts
Expand Up @@ -48,16 +48,13 @@ const DEFAULT_LOCAL_STORE: LocalStores = {

export default class FitPlugin extends Plugin {
settings: FitSettings;

localStore: LocalStores
fit: Fit;
vaultOps: VaultOperations;
fitPull: FitPull
fitPush: FitPush
fitPullRibbonIconEl: HTMLElement
fitPushRibbonIconEl: HTMLElement



checkSettingsConfigured(): boolean {
if (["<Personal-Access-Token> ", ""].includes(this.settings.pat)) {
Expand All @@ -82,8 +79,6 @@ export default class FitPlugin extends Plugin {
await this.saveLocalStore()
}



async onload() {
await this.loadSettings(Platform.isMobile);
await this.loadLocalStore();
Expand All @@ -105,7 +100,7 @@ export default class FitPlugin extends Plugin {
this.fitPullRibbonIconEl.removeClass('animate-icon')
return
}// early return to abort pull
await this.fitPull.pullRemoteToLocal(...[...checkResult, this.saveLocalStoreCallback])
await this.fitPull.pullRemoteToLocal(checkResult, this.saveLocalStoreCallback)
this.fitPullRibbonIconEl.removeClass('animate-icon')
});

Expand All @@ -125,9 +120,7 @@ export default class FitPlugin extends Plugin {
this.fitPushRibbonIconEl.removeClass('animate-icon')
return
} // early return if prepush checks not passed
const [changedFiles, latestRemoteCommitSha] = checksResult
await this.fitPush.pushChangedFilesToRemote(
changedFiles, latestRemoteCommitSha, this.saveLocalStoreCallback)
await this.fitPush.pushChangedFilesToRemote(checksResult, this.saveLocalStoreCallback)
this.fitPushRibbonIconEl.removeClass('animate-icon')
});

Expand Down Expand Up @@ -158,27 +151,29 @@ export default class FitPlugin extends Plugin {
if (isMobile && !userSetting) {
settings = Object.assign({}, DEFAULT_MOBILE_SETTINGS);
}
const settingsObj: FitSettings = Object.keys(DEFAULT_SETTINGS).reduce((obj, key: keyof FitSettings) => {
if (settings.hasOwnProperty(key)) {
if (key == "verbose") {
obj[key] = Boolean(settings["verbose"]);
} else {
obj[key] = settings[key];
const settingsObj: FitSettings = Object.keys(DEFAULT_SETTINGS).reduce(
(obj, key: keyof FitSettings) => {
if (settings.hasOwnProperty(key)) {
if (key == "verbose") {
obj[key] = Boolean(settings["verbose"]);
} else {
obj[key] = settings[key];
}
}
}
return obj;
}, {} as FitSettings);
return obj;
}, {} as FitSettings);
this.settings = settingsObj
}

async loadLocalStore() {
const localStore = Object.assign({}, DEFAULT_LOCAL_STORE, await this.loadData());
const localStoreObj: LocalStores = Object.keys(DEFAULT_LOCAL_STORE).reduce((obj, key: keyof LocalStores) => {
if (localStore.hasOwnProperty(key)) {
obj[key] = localStore[key];
}
return obj;
}, {} as LocalStores);
const localStoreObj: LocalStores = Object.keys(DEFAULT_LOCAL_STORE).reduce(
(obj, key: keyof LocalStores) => {
if (localStore.hasOwnProperty(key)) {
obj[key] = localStore[key];
}
return obj;
}, {} as LocalStores);
this.localStore = localStoreObj
}

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
@@ -1,7 +1,7 @@
{
"id": "fit",
"name": "Fit",
"version": "1.0.4",
"version": "1.0.5",
"minAppVersion": "0.15.0",
"description": "File gIT - a barebone git system for files in obsidian",
"author": "joshuakto",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "obsidian-sample-plugin",
"version": "1.0.4",
"version": "1.0.5",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand Down
46 changes: 30 additions & 16 deletions src/fit.ts
@@ -1,6 +1,8 @@
import { LocalStores, FitSettings } from "main"
import { Vault } from "obsidian"
import { Octokit } from "@octokit/core"
import { LocalChange } from "./fitPush"
import { RECOGNIZED_BINARY_EXT } from "./utils"

type TreeNode = {path: string, mode: string, type: string, sha: string | null}

Expand All @@ -21,7 +23,7 @@ export interface IFit {
getCommitTreeSha: (ref: string) => Promise<string>
getRemoteTreeSha: (tree_sha: string) => Promise<{[k:string]: string}>
createBlob: (content: string, encoding: string) =>Promise<string>
createTreeNodeFromFile: ({path, type, extension}: {path: string, type: string, extension?: string}) => Promise<TreeNode>
createTreeNodeFromFile: ({path, status, extension}: LocalChange) => Promise<TreeNode>
createCommit: (treeSha: string, parentSha: string) =>Promise<string>
updateRef: (sha: string, ref: string) => Promise<string>
getBlob: (file_sha:string) =>Promise<string>
Expand All @@ -46,7 +48,9 @@ export class Fit implements IFit {
this.loadLocalStore(localStores)
this.vault = vault
this.headers = {
"If-None-Match": '', // Hack to disable caching which leads to inconsistency for read after write https://github.com/octokit/octokit.js/issues/890
// Hack to disable caching which leads to inconsistency for
// read after write https://github.com/octokit/octokit.js/issues/890
"If-None-Match": '',
'X-GitHub-Api-Version': '2022-11-28'
}
}
Expand Down Expand Up @@ -109,9 +113,11 @@ export class Fit implements IFit {
return await this.getRef(ref)
}

// ref Can be a commit SHA, branch name (heads/BRANCH_NAME), or tag name (tags/TAG_NAME), refers to https://git-scm.com/book/en/v2/Git-Internals-Git-References
// ref Can be a commit SHA, branch name (heads/BRANCH_NAME), or tag name (tags/TAG_NAME),
// refers to https://git-scm.com/book/en/v2/Git-Internals-Git-References
async getCommitTreeSha(ref: string): Promise<string> {
const {data: commit} = await this.octokit.request( `GET /repos/${this.owner}/${this.repo}/commits/${ref}`, {
const {data: commit} = await this.octokit.request(
`GET /repos/${this.owner}/${this.repo}/commits/${ref}`, {
owner: this.owner,
repo: this.repo,
ref,
Expand All @@ -121,7 +127,8 @@ export class Fit implements IFit {
}

async getTree(tree_sha: string): Promise<TreeNode[]> {
const { data: tree } = await this.octokit.request(`GET /repos/${this.owner}/${this.repo}/git/trees/${tree_sha}`, {
const { data: tree } = await this.octokit.request(
`GET /repos/${this.owner}/${this.repo}/git/trees/${tree_sha}`, {
owner: this.owner,
repo: this.repo,
tree_sha,
Expand All @@ -135,7 +142,8 @@ export class Fit implements IFit {
async getRemoteTreeSha(tree_sha: string): Promise<{[k:string]: string}> {
const remoteTree = await this.getTree(tree_sha)
const remoteSha = Object.fromEntries(remoteTree.map((node: TreeNode) : [string, string] | null=>{
// currently ignoring directory changes, if you'd like to upload a new directory, a quick hack would be creating an empty file inside
// currently ignoring directory changes, if you'd like to upload a new directory,
// a quick hack would be creating an empty file inside
if (node.type=="blob") {
if (!node.path || !node.sha) {
throw new Error("Path or sha not found for blob node in remote");
Expand All @@ -148,7 +156,8 @@ export class Fit implements IFit {
}

async createBlob(content: string, encoding: string): Promise<string> {
const {data: blob} = await this.octokit.request(`POST /repos/${this.owner}/${this.repo}/git/blobs`, {
const {data: blob} = await this.octokit.request(
`POST /repos/${this.owner}/${this.repo}/git/blobs`, {
owner: this.owner,
repo: this.repo,
content,
Expand All @@ -159,8 +168,8 @@ export class Fit implements IFit {
}


async createTreeNodeFromFile({path, type, extension}: {path: string, type: string, extension?: string}): Promise<TreeNode> {
if (type === "deleted") {
async createTreeNodeFromFile({path, status, extension}: LocalChange): Promise<TreeNode> {
if (status === "deleted") {
return {
path,
mode: '100644',
Expand All @@ -169,11 +178,13 @@ export class Fit implements IFit {
}
}
if (!this.vault.adapter.exists(path)) {
throw new Error("Unexpected error: attempting to createBlob for non-existent file, please file an issue on github with info to reproduce the issue.");
throw new Error(
`Unexpected error: attempting to createBlob for non-existent file,
please file an issue on github with info to reproduce the issue.`);
}
let encoding: string;
let content: string
if (extension && ["pdf", "png", "jpeg"].includes(extension)) {
if (extension && RECOGNIZED_BINARY_EXT.includes(extension)) {
encoding = "base64"
const fileArrayBuf = await this.vault.adapter.readBinary(path)
const uint8Array = new Uint8Array(fileArrayBuf);
Expand All @@ -199,7 +210,8 @@ export class Fit implements IFit {
treeNodes: Array<TreeNode>,
base_tree_sha: string):
Promise<string> {
const {data: newTree} = await this.octokit.request(`POST /repos/${this.owner}/${this.repo}/git/trees`, {
const {data: newTree} = await this.octokit.request(
`POST /repos/${this.owner}/${this.repo}/git/trees`, {
owner: this.owner,
repo: this.repo,
tree: treeNodes,
Expand All @@ -209,10 +221,10 @@ export class Fit implements IFit {
return newTree.sha
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async createCommit(treeSha: string, parentSha: string): Promise<string> {
const message = `Commit from ${this.deviceName} on ${new Date().toLocaleString()}`
const { data: createdCommit } = await this.octokit.request(`POST /repos/${this.owner}/${this.repo}/git/commits` ,{
const { data: createdCommit } = await this.octokit.request(
`POST /repos/${this.owner}/${this.repo}/git/commits` , {
owner: this.owner,
repo: this.repo,
message,
Expand All @@ -224,7 +236,8 @@ export class Fit implements IFit {
}

async updateRef(sha: string, ref = `heads/${this.branch}`): Promise<string> {
const { data:updatedRef } = await this.octokit.request(`PATCH /repos/${this.owner}/${this.repo}/git/refs/${ref}`, {
const { data:updatedRef } = await this.octokit.request(
`PATCH /repos/${this.owner}/${this.repo}/git/refs/${ref}`, {
owner: this.owner,
repo: this.repo,
ref,
Expand All @@ -235,7 +248,8 @@ export class Fit implements IFit {
}

async getBlob(file_sha:string): Promise<string> {
const { data: blob } = await this.octokit.request(`GET /repos/${this.owner}/${this.repo}/git/blobs/${file_sha}`, {
const { data: blob } = await this.octokit.request(
`GET /repos/${this.owner}/${this.repo}/git/blobs/${file_sha}`, {
owner: this.owner,
repo: this.repo,
file_sha,
Expand Down

0 comments on commit 228fc65

Please sign in to comment.