Skip to content

Commit

Permalink
feat: ✨ Add Command to open file history on GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
phibr0 committed Dec 30, 2021
1 parent 38c7240 commit e7dd288
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ChangedFilesModal } from "src/ui/modals/changedFilesModal";
import { CustomMessageModal } from "src/ui/modals/customMessageModal";
import { DEFAULT_SETTINGS, DIFF_VIEW_CONFIG, GIT_VIEW_CONFIG } from "./constants";
import { GitManager } from "./gitManager";
import openInGitHub from "./openInGitHub";
import { openHistoryInGitHub, openLineInGitHub } from "./openInGitHub";
import { SimpleGit } from "./simpleGit";
import { ObsidianGitSettings, PluginState } from "./types";
import DiffView from "./ui/diff/diffView";
Expand Down Expand Up @@ -80,7 +80,13 @@ export default class ObsidianGit extends Plugin {
this.addCommand({
id: 'view-file-in-github',
name: 'Open File in GitHub',
editorCallback: (editor, { file }) => openInGitHub(editor, file, this.gitManager),
editorCallback: (editor, { file }) => openLineInGitHub(editor, file, this.gitManager),
});

this.addCommand({
id: 'view-history-in-github',
name: 'Open File History on GitHub',
editorCallback: (_, { file }) => openHistoryInGitHub(file, this.gitManager),
});

this.addCommand({
Expand Down
15 changes: 13 additions & 2 deletions src/openInGitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Editor, Notice, TFile } from "obsidian";
import { GitManager } from "./gitManager";
import { shell } from "electron";

export default async function openInGitHub(editor: Editor, file: TFile, manager: GitManager) {
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$)/);
Expand All @@ -17,4 +17,15 @@ export default async function openInGitHub(editor: Editor, file: TFile, manager:
} else {
new Notice('It seems like you are not using GitHub');
}
}
}

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}`);
} else {
new Notice('It seems like you are not using GitHub');
}
};

0 comments on commit e7dd288

Please sign in to comment.