From 705c1cfdd7926fd7896877fff5ee661ab42293a0 Mon Sep 17 00:00:00 2001 From: carloscz Date: Fri, 8 Jul 2016 15:40:19 +0200 Subject: [PATCH] Shows commit hash and message When Git Blame command is executed executed --- src/extension.ts | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index f4b7a427..2d7516d0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -15,15 +15,15 @@ export function activate(context: ExtensionContext) { return; } + const workspaceRoot = workspace.rootPath; commands.registerCommand('extension.blame', () => { - // TODO - add some more info about the blame - window.showInformationMessage('Hello World!'); + showMessage(context, workspaceRoot); }); // Try to find the repo first in the workspace, then in parent directories // because sometimes one opens a subdirectory but still wants information // about the full repo. - lookupRepo(context, workspace.rootPath); + lookupRepo(context, workspaceRoot); } function lookupRepo(context: ExtensionContext, repoDir: string) { @@ -48,5 +48,43 @@ function lookupRepo(context: ExtensionContext, repoDir: string) { }); } +function showMessage(context: ExtensionContext, repoDir: string) +{ + const repoPath = path.join(repoDir, '.git'); + + fs.access(repoPath, (err) => { + if (err) { + // No access to git repo or no repo, try to go up. + const parentDir = path.dirname(repoDir); + if (parentDir != repoDir) { + showMessage(context, parentDir); + } + } + else { + const editor = window.activeTextEditor; + + if (!editor) return; + + const doc = editor.document; + + if (!doc) return; + if (doc.isUntitled) return; // Document hasn't been saved and is not in git. + + const gitBlame = new GitBlame(repoPath, gitBlameShell); + const lineNumber = editor.selection.active.line + 1; // line is zero based + const file = path.relative(repoDir, editor.document.fileName); + + gitBlame.getBlameInfo(file).then((info) => { + + if (lineNumber in info['lines']) { + + const hash = info['lines'][lineNumber]['hash']; + const commitInfo = info['commits'][hash]; + + window.showInformationMessage(hash + ' ' + commitInfo['summary']); + } + }); +} +