diff --git a/src/gitManager.ts b/src/gitManager.ts index 95c5ecc2..99796ea0 100644 --- a/src/gitManager.ts +++ b/src/gitManager.ts @@ -61,7 +61,7 @@ export abstract class GitManager { abstract updateGitPath(gitPath: string): void; - abstract getDiffString(filePath: string): string; + abstract getDiffString(filePath: string): Promise; async formatCommitMessage(message?: string): Promise { let template = message ?? this.plugin.settings.commitMessage; diff --git a/src/main.ts b/src/main.ts index 42eb7687..e95c4a2f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -47,7 +47,6 @@ export default class ObsidianGit extends Plugin { this.registerView(DIFF_VIEW_CONFIG.type, (leaf) => { return new DiffView(leaf, this); }); - (this.app.workspace as any).registerHoverLinkSource(GIT_VIEW_CONFIG.type, { display: 'Git View', defaultMod: true, @@ -57,7 +56,7 @@ export default class ObsidianGit extends Plugin { this.addCommand({ id: 'open-git-view', - name: 'Open Source Control View', + name: 'Open source control view', callback: async () => { if (this.app.workspace.getLeavesOfType(GIT_VIEW_CONFIG.type).length === 0) { await this.app.workspace.getRightLeaf(false).setViewState({ @@ -70,7 +69,7 @@ export default class ObsidianGit extends Plugin { this.addCommand({ id: 'open-diff-view', - name: 'Open Diff View', + name: 'Open diff view', editorCallback: async (editor, view) => { this.app.workspace.createLeafBySplit(view.leaf).setViewState({ type: DIFF_VIEW_CONFIG.type }); dispatchEvent(new CustomEvent('diff-update', { detail: { path: view.file.path } })); @@ -78,14 +77,14 @@ export default class ObsidianGit extends Plugin { }); this.addCommand({ - id: 'view-file-in-github', - name: 'Open File in GitHub', + id: 'view-file-on-github', + name: 'Open file on GitHub', editorCallback: (editor, { file }) => openLineInGitHub(editor, file, this.gitManager), }); this.addCommand({ - id: 'view-history-in-github', - name: 'Open File History on GitHub', + id: 'view-history-on-github', + name: 'Open file history on GitHub', editorCallback: (_, { file }) => openHistoryInGitHub(file, this.gitManager), }); @@ -173,6 +172,8 @@ export default class ObsidianGit extends Plugin { async onunload() { (this.app.workspace as any).unregisterHoverLinkSource(GIT_VIEW_CONFIG.type); + this.app.workspace.detachLeavesOfType(GIT_VIEW_CONFIG.type); + this.app.workspace.detachLeavesOfType(DIFF_VIEW_CONFIG.type); this.clearAutoPull(); this.clearAutoBackup(); console.log('unloading ' + this.manifest.name + " plugin"); diff --git a/src/openInGitHub.ts b/src/openInGitHub.ts index b9dffbfe..0a24b8df 100644 --- a/src/openInGitHub.ts +++ b/src/openInGitHub.ts @@ -1,15 +1,13 @@ +import { shell } from "electron"; import { Editor, Notice, TFile } from "obsidian"; import { GitManager } from "./gitManager"; -import { shell } from "electron"; export async function openLineInGitHub(editor: Editor, file: TFile, manager: GitManager) { - const remoteUrl = await manager.getConfig('remote.origin.url') as string; - const branch = (await manager.branchInfo()).current; - const [isGitHub, user, repo] = remoteUrl.match(/(?:^https:\/\/github\.com\/(.*)\/(.*)\.git$)|(?:^git@github\.com:(.*)\/(.*)\.git$)/); - if (!!isGitHub) { + const { isGitHub, branch, repo, user } = await getData(manager); + if (isGitHub) { const from = editor.getCursor("from").line + 1; const to = editor.getCursor("to").line + 1; - if(from === to) { + if (from === to) { await shell.openExternal(`https://github.com/${user}/${repo}/blob/${branch}/${file.path}?plain=1#L${from}`); } else { await shell.openExternal(`https://github.com/${user}/${repo}/blob/${branch}/${file.path}?plain=1#L${from}-L${to}`); @@ -20,12 +18,27 @@ export async function openLineInGitHub(editor: Editor, file: TFile, manager: Git } export async function openHistoryInGitHub(file: TFile, manager: GitManager) { - const remoteUrl = await manager.getConfig('remote.origin.url') as string; - const branch = (await manager.branchInfo()).current; - const [isGitHub, user, repo] = remoteUrl.match(/(?:^https:\/\/github\.com\/(.*)\/(.*)\.git$)|(?:^git@github\.com:(.*)\/(.*)\.git$)/); - if (!!isGitHub) { - await shell.openExternal(`https://github.com/${user}/${repo}/commits/${branch}/${file.path}`); + const { isGitHub, branch, repo, user } = await getData(manager); + + if (isGitHub) { + await shell.openExternal(`https://github.com/${user}/${repo}/commits/${branch}/${file.path}`); } else { new Notice('It seems like you are not using GitHub'); } -}; \ No newline at end of file +}; + +async function getData(manager: GitManager): Promise<{ isGitHub: boolean, user: string, repo: string; branch: string; }> { + const branchInfo = await manager.branchInfo(); + const remoteBranch = branchInfo.tracking; + const branch = branchInfo.current; + + const remote = remoteBranch.substring(0, remoteBranch.indexOf("/")); + const remoteUrl = await manager.getConfig(`remote.${remote}.url`) as string; + const [isGitHub, httpsUser, httpsRepo, sshUser, sshRepo] = remoteUrl.match(/(?:^https:\/\/github\.com\/(.*)\/(.*)\.git$)|(?:^git@github\.com:(.*)\/(.*)\.git$)/); + return { + isGitHub: !!isGitHub, + repo: httpsRepo || sshRepo, + user: httpsUser || sshUser, + branch: branch, + }; +} \ No newline at end of file diff --git a/src/settings.ts b/src/settings.ts index d6e8d4ed..d8ac299c 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -256,10 +256,10 @@ export class ObsidianGitSettingsTab extends PluginSettingTab { plugin.saveSettings(); }) ); - + new Setting(containerEl) - .setName("Use Tree Layout for Sidebar") - .setDesc("If turned on, the Tree Layout will be the default.") + .setName("Use tree layout for sidebar") + .setDesc("If turned on, the tree layout will be the default.") .addToggle((toggle) => toggle .setValue(plugin.settings.treeStructure) diff --git a/src/simpleGit.ts b/src/simpleGit.ts index 1c81e473..b9d61d19 100644 --- a/src/simpleGit.ts +++ b/src/simpleGit.ts @@ -1,4 +1,4 @@ -import { spawn, spawnSync } from "child_process"; +import { spawnSync } from "child_process"; import { FileSystemAdapter } from "obsidian"; import * as path from "path"; import simpleGit, * as simple from "simple-git"; @@ -299,13 +299,9 @@ export class SimpleGit extends GitManager { this.setGitInstance(); } - getDiffString(filePath: string): string { - const command = spawnSync(this.plugin.settings.gitPath || 'git', ['diff', filePath], { - //@ts-ignore - cwd: this.plugin.app.vault.adapter.basePath - }); - this.git.diffSummary() - return command.output.toString(); + async getDiffString(filePath: string): Promise { + return (await this.git.diff([filePath])); + } private isGitInstalled(): boolean { diff --git a/src/ui/diff/diffView.ts b/src/ui/diff/diffView.ts index 33c514b9..0fbf410b 100644 --- a/src/ui/diff/diffView.ts +++ b/src/ui/diff/diffView.ts @@ -1,18 +1,17 @@ import { html } from "diff2html"; -import { HoverParent, HoverPopover, ItemView, MarkdownView, WorkspaceLeaf } from "obsidian"; +import { ItemView, MarkdownView, WorkspaceLeaf } from "obsidian"; import { DIFF_VIEW_CONFIG } from "src/constants"; import ObsidianGit from "src/main"; export default class DiffView extends ItemView { - plugin: ObsidianGit; filePath: string; parser: DOMParser; - - constructor(leaf: WorkspaceLeaf, plugin: ObsidianGit) { + intervalId: number; + gettingDiff: boolean = false; + constructor(leaf: WorkspaceLeaf, private plugin: ObsidianGit) { super(leaf); - this.plugin = plugin; this.parser = new DOMParser(); this.registerEvent(this.app.workspace.on('active-leaf-change', (leaf) => { @@ -23,9 +22,9 @@ export default class DiffView extends ItemView { } this.refresh(); })); - this.firstOpen = this.firstOpen.bind(this); - addEventListener('diff-update', this.firstOpen); - this.registerInterval(window.setInterval(() => this.refresh(), 10000)); + addEventListener('diff-update', this.firstOpen.bind(this)); + this.intervalId = window.setInterval(() => this.refresh(), 10000); + this.registerInterval(this.intervalId); } firstOpen(event: CustomEvent) { @@ -46,7 +45,8 @@ export default class DiffView extends ItemView { } onClose(): Promise { - removeEventListener('diff-update', this.firstOpen) + removeEventListener('diff-update', this.firstOpen.bind(this)); + window.clearInterval(this.intervalId); return super.onClose(); } @@ -55,13 +55,14 @@ export default class DiffView extends ItemView { return super.onOpen(); } - refresh(): void { - if (this.filePath) { - this.contentEl.empty(); + async refresh(): Promise { + if (this.filePath && !this.gettingDiff) { + this.gettingDiff = true; const diff = this.parser.parseFromString( - html(this.plugin.gitManager.getDiffString(this.filePath)), + html(await this.plugin.gitManager.getDiffString(this.filePath)), 'text/html') .querySelector('.d2h-file-diff'); + this.contentEl.empty(); if (diff) { this.contentEl.append(diff); } else { @@ -70,6 +71,7 @@ export default class DiffView extends ItemView { div.createEl('br'); div.createSpan({ text: 'No changes to this file.' }); } + this.gettingDiff = false; } } diff --git a/src/ui/sidebar/components/fileComponent.svelte b/src/ui/sidebar/components/fileComponent.svelte index 7df0e0cd..bda45dfd 100644 --- a/src/ui/sidebar/components/fileComponent.svelte +++ b/src/ui/sidebar/components/fileComponent.svelte @@ -1,7 +1,7 @@