Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shows commit hash and message #10

Merged
merged 1 commit into from
Jul 20, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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']);
}
});
}