Skip to content

Commit

Permalink
Merge pull request #5 from fogzot/feature-parent-git
Browse files Browse the repository at this point in the history
Feature parent git
  • Loading branch information
wade-ryan committed May 20, 2016
2 parents a4effae + fa8b0eb commit de6c6a9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "out/src",
"outDir": "${workspaceRoot}/out/src",
"preLaunchTask": "npm"
},
{
Expand All @@ -21,7 +21,7 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "out/test",
"outDir": "${workspaceRoot}/out/test",
"preLaunchTask": "npm"
}
]
Expand Down
54 changes: 27 additions & 27 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,87 @@ import * as path from 'path';
import * as moment from 'moment';

export class GitBlameController {

private _disposable: Disposable;
private _textDecorator: TextDecorator
constructor(private gitBlame: GitBlame, private workspaceRoot: string, private view) {

constructor(private gitBlame: GitBlame, private gitRoot: string, private view) {
const self = this;

const disposables: Disposable[] = [];

window.onDidChangeActiveTextEditor(self.onTextEditorChange, self, disposables);
window.onDidChangeTextEditorSelection(self.onTextEditorSelectionChange, self, disposables);

this.onTextEditorChange(window.activeTextEditor);

this._disposable = Disposable.from(...disposables);
this._textDecorator = new TextDecorator();
}

onTextEditorChange(editor: TextEditor) : void {
this.clear();

if (!editor) return;

const doc = editor.document;

if (!doc) return;
if (doc.isUntitled) return; // Document hasn't been saved and is not in git.
if (doc.isUntitled) return; // Document hasn't been saved and is not in git.

const lineNumber = editor.selection.active.line + 1; // line is zero based
const file = path.relative(this.workspaceRoot, editor.document.fileName);
const file = path.relative(this.gitRoot, editor.document.fileName);

this.gitBlame.getBlameInfo(file).then((info) => {
this.show(info, lineNumber);
}, () => {
// Do nothing.
});
}

onTextEditorSelectionChange(textEditorSelectionChangeEvent: TextEditorSelectionChangeEvent) : void {
this.onTextEditorChange(textEditorSelectionChangeEvent.textEditor);
}

clear() {
this.view.refresh('');
}

show(blameInfo: Object, lineNumber: number) : void {

if (lineNumber in blameInfo['lines']) {
const hash = blameInfo['lines'][lineNumber]['hash'];
const commitInfo = blameInfo['commits'][hash];

this.view.refresh(this._textDecorator.toTextView(new Date(), commitInfo));
} else {
// No line info.
}
}

dispose() {
this._disposable.dispose();
}
}


export class TextDecorator {

toTextView(dateNow: Date, commit: Object) : string {
const author = commit['author'];
const dateText = this.toDateText(dateNow, new Date(author['timestamp'] * 1000));

return 'Last edit made by ' + author['name'] + ' ( ' + dateText + ' )';
}

toDateText(dateNow: Date, dateThen: Date) : string {

const momentNow = moment(dateNow);
const momentThen = moment(dateThen);

const months = momentNow.diff(momentThen, 'months');
const days = momentNow.diff(momentThen, 'days');

if (months <= 1) {
if (days == 0) {
return 'today';
Expand Down
40 changes: 27 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {GitBlame} from './gitblame';
import {StatusBarView} from './view';
import {GitBlameController} from './controller';
import {window, ExtensionContext, Disposable, StatusBarAlignment,
workspace, TextEditor, TextEditorSelectionChangeEvent} from 'vscode';
import {window, ExtensionContext, Disposable, StatusBarAlignment,
workspace, TextEditor, TextEditorSelectionChangeEvent} from 'vscode';
import * as fs from 'fs';
import * as path from 'path';

Expand All @@ -14,18 +14,32 @@ export function activate(context: ExtensionContext) {
if (!workspace.rootPath) {
return;
}

const repoPath = path.join(workspace.rootPath, '.git');

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

function lookupRepo(context: ExtensionContext, repoDir: string) {
const repoPath = path.join(repoDir, '.git');

fs.access(repoPath, (err) => {
if (err) return; // No access to git repo.

const statusBar = window.createStatusBarItem(StatusBarAlignment.Left);

const gitBlame = new GitBlame(repoPath, gitBlameShell);
const controller = new GitBlameController(gitBlame, workspace.rootPath, new StatusBarView(statusBar));

context.subscriptions.push(controller);
context.subscriptions.push(gitBlame);
if (err) {
// No access to git repo or no repo, try to go up.
const parentDir = path.dirname(repoDir);
if (parentDir != repoDir) {
lookupRepo(context, parentDir);
}
}
else {
const statusBar = window.createStatusBarItem(StatusBarAlignment.Left);
const gitBlame = new GitBlame(repoPath, gitBlameShell);
const controller = new GitBlameController(gitBlame, repoDir, new StatusBarView(statusBar));

context.subscriptions.push(controller);
context.subscriptions.push(gitBlame);
}
});
}

Expand Down

0 comments on commit de6c6a9

Please sign in to comment.