Skip to content

Commit

Permalink
fix: many minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jan 8, 2022
1 parent e7dd288 commit 7d29bef
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export abstract class GitManager {

abstract updateGitPath(gitPath: string): void;

abstract getDiffString(filePath: string): string;
abstract getDiffString(filePath: string): Promise<string>;

async formatCommitMessage(message?: string): Promise<string> {
let template = message ?? this.plugin.settings.commitMessage;
Expand Down
15 changes: 8 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({
Expand All @@ -70,22 +69,22 @@ 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 } }));
},
});

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),
});

Expand Down Expand Up @@ -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");
Expand Down
37 changes: 25 additions & 12 deletions src/openInGitHub.ts
Original file line number Diff line number Diff line change
@@ -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}`);
Expand All @@ -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');
}
};
};

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,
};
}
6 changes: 3 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<string> {
return (await this.git.diff([filePath]));

}

private isGitInstalled(): boolean {
Expand Down
28 changes: 15 additions & 13 deletions src/ui/diff/diffView.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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) {
Expand All @@ -46,7 +45,8 @@ export default class DiffView extends ItemView {
}

onClose(): Promise<void> {
removeEventListener('diff-update', this.firstOpen)
removeEventListener('diff-update', this.firstOpen.bind(this));
window.clearInterval(this.intervalId);
return super.onClose();
}

Expand All @@ -55,13 +55,14 @@ export default class DiffView extends ItemView {
return super.onOpen();
}

refresh(): void {
if (this.filePath) {
this.contentEl.empty();
async refresh(): Promise<void> {
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 {
Expand All @@ -70,6 +71,7 @@ export default class DiffView extends ItemView {
div.createEl('br');
div.createSpan({ text: 'No changes to this file.' });
}
this.gettingDiff = false;
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/ui/sidebar/components/fileComponent.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { setIcon, Workspace } from "obsidian";
import { hoverPreview, openOrSwitch } from "obsidian-community-lib";
import { DIFF_VIEW_CONFIG } from "src/constants";
import { DIFF_VIEW_CONFIG } from "src/constants";
import { GitManager } from "src/gitManager";
import { FileStatusResult } from "src/types";
import { DiscardModal } from "src/ui/modals/discardModal";
Expand Down Expand Up @@ -54,8 +54,12 @@ import { DIFF_VIEW_CONFIG } from "src/constants";
}
function showDiff() {
workspace.createLeafInParent(workspace.rootSplit, 0).setViewState({ type: DIFF_VIEW_CONFIG.type });
dispatchEvent(new CustomEvent('diff-update', { detail: { path: change.path } }));
workspace
.createLeafInParent(workspace.rootSplit, 0)
.setViewState({ type: DIFF_VIEW_CONFIG.type });
dispatchEvent(
new CustomEvent("diff-update", { detail: { path: change.path } })
);
}
function discard() {
Expand Down
6 changes: 3 additions & 3 deletions src/ui/sidebar/components/treeComponent.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import FileComponent from "./fileComponent.svelte";
import ObsidianGit from "src/main";
import { slide } from "svelte/transition";
import GitView from "../sidebarView";
import FileComponent from "./fileComponent.svelte";
import StagedFileComponent from "./stagedFileComponent.svelte";
import { slide } from "svelte/transition";
export let hierarchy: any;
export let plugin: ObsidianGit;
export let view: GitView;
Expand Down Expand Up @@ -93,6 +93,6 @@
}
.file-view {
margin-left: 5px
margin-left: 5px;
}
</style>

0 comments on commit 7d29bef

Please sign in to comment.