Skip to content

Feature parent git #5

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

Merged
merged 2 commits into from
May 20, 2016
Merged
Show file tree
Hide file tree
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
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