mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-16 10:58:34 -05:00
Richer blame info & highlight (wip)
This commit is contained in:
BIN
blame.png
BIN
blame.png
Binary file not shown.
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 24 KiB |
@@ -1,11 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
import {CancellationToken, CodeLens, CodeLensProvider, commands, Location, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode';
|
import {CancellationToken, CodeLens, CodeLensProvider, commands, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode';
|
||||||
import {Commands, VsCodeCommands} from './constants';
|
import {Commands, VsCodeCommands} from './constants';
|
||||||
import {IGitBlameLine, gitBlame} from './git';
|
import {IGitBlameLine, gitBlame} from './git';
|
||||||
import {toGitBlameUri} from './contentProvider';
|
import {toGitBlameUri} from './contentProvider';
|
||||||
import * as moment from 'moment';
|
import * as moment from 'moment';
|
||||||
|
|
||||||
export class GitCodeLens extends CodeLens {
|
export class GitBlameCodeLens extends CodeLens {
|
||||||
constructor(private blame: Promise<IGitBlameLine[]>, public repoPath: string, public fileName: string, private blameRange: Range, range: Range) {
|
constructor(private blame: Promise<IGitBlameLine[]>, public repoPath: string, public fileName: string, private blameRange: Range, range: Range) {
|
||||||
super(range);
|
super(range);
|
||||||
}
|
}
|
||||||
@@ -14,11 +14,21 @@ export class GitCodeLens extends CodeLens {
|
|||||||
return this.blame.then(allLines => allLines.slice(this.blameRange.start.line, this.blameRange.end.line + 1));
|
return this.blame.then(allLines => allLines.slice(this.blameRange.start.line, this.blameRange.end.line + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
static toUri(lens: GitCodeLens, line: IGitBlameLine, lines: IGitBlameLine[]): Uri {
|
static toUri(lens: GitBlameCodeLens, index: number, line: IGitBlameLine, lines: IGitBlameLine[]): Uri {
|
||||||
return toGitBlameUri(Object.assign({ repoPath: lens.repoPath, range: lens.blameRange, lines: lines }, line));
|
return toGitBlameUri(Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines }, line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class GitHistoryCodeLens extends CodeLens {
|
||||||
|
constructor(public repoPath: string, public fileName: string, range: Range) {
|
||||||
|
super(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
// static toUri(lens: GitHistoryCodeLens, index: number): Uri {
|
||||||
|
// return toGitBlameUri(Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines }, line));
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
export default class GitCodeLensProvider implements CodeLensProvider {
|
export default class GitCodeLensProvider implements CodeLensProvider {
|
||||||
constructor(public repoPath: string) { }
|
constructor(public repoPath: string) { }
|
||||||
|
|
||||||
@@ -29,39 +39,52 @@ export default class GitCodeLensProvider implements CodeLensProvider {
|
|||||||
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => {
|
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => {
|
||||||
let lenses: CodeLens[] = [];
|
let lenses: CodeLens[] = [];
|
||||||
symbols.forEach(sym => this._provideCodeLens(document, sym, blame, lenses));
|
symbols.forEach(sym => this._provideCodeLens(document, sym, blame, lenses));
|
||||||
|
|
||||||
|
// Check if we have a lens for the whole document -- if not add one
|
||||||
|
if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) {
|
||||||
|
const docRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000));
|
||||||
|
lenses.push(new GitBlameCodeLens(blame, this.repoPath, document.fileName, docRange, new Range(0, 0, 0, docRange.start.character)));
|
||||||
|
}
|
||||||
return lenses;
|
return lenses;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _provideCodeLens(document: TextDocument, symbol: SymbolInformation, blame: Promise<IGitBlameLine[]>, lenses: CodeLens[]): void {
|
private _provideCodeLens(document: TextDocument, symbol: SymbolInformation, blame: Promise<IGitBlameLine[]>, lenses: CodeLens[]): void {
|
||||||
switch (symbol.kind) {
|
switch (symbol.kind) {
|
||||||
|
case SymbolKind.Package:
|
||||||
case SymbolKind.Module:
|
case SymbolKind.Module:
|
||||||
case SymbolKind.Class:
|
case SymbolKind.Class:
|
||||||
case SymbolKind.Interface:
|
case SymbolKind.Interface:
|
||||||
case SymbolKind.Method:
|
|
||||||
case SymbolKind.Function:
|
|
||||||
case SymbolKind.Constructor:
|
case SymbolKind.Constructor:
|
||||||
case SymbolKind.Field:
|
case SymbolKind.Method:
|
||||||
case SymbolKind.Property:
|
case SymbolKind.Property:
|
||||||
|
case SymbolKind.Field:
|
||||||
|
case SymbolKind.Function:
|
||||||
|
case SymbolKind.Enum:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var line = document.lineAt(symbol.location.range.start);
|
var line = document.lineAt(symbol.location.range.start);
|
||||||
let lens = new GitCodeLens(blame, this.repoPath, document.fileName, symbol.location.range, line.range);
|
lenses.push(new GitBlameCodeLens(blame, this.repoPath, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, line.firstNonWhitespaceCharacterIndex))));
|
||||||
lenses.push(lens);
|
lenses.push(new GitHistoryCodeLens(this.repoPath, document.fileName, line.range.with(new Position(line.range.start.line, line.firstNonWhitespaceCharacterIndex + 1))));
|
||||||
}
|
}
|
||||||
|
|
||||||
resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> {
|
resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||||
if (lens instanceof GitCodeLens) {
|
if (lens instanceof GitBlameCodeLens) {
|
||||||
return lens.getBlameLines().then(lines => {
|
return lens.getBlameLines().then(lines => {
|
||||||
|
if (!lines.length) {
|
||||||
|
console.error('No blame lines found', lens);
|
||||||
|
throw new Error('No blame lines found');
|
||||||
|
}
|
||||||
|
|
||||||
let recentLine = lines[0];
|
let recentLine = lines[0];
|
||||||
|
|
||||||
let locations: Location[] = [];
|
let locations: Location[] = [];
|
||||||
if (lines.length > 1) {
|
if (lines.length > 1) {
|
||||||
let sorted = lines.sort((a, b) => a.date.getTime() - b.date.getTime());
|
let sorted = lines.sort((a, b) => b.date.getTime() - a.date.getTime());
|
||||||
recentLine = sorted[sorted.length - 1];
|
recentLine = sorted[0];
|
||||||
|
|
||||||
console.log(lens.fileName, 'Blame lines:', sorted);
|
console.log(lens.fileName, 'Blame lines:', sorted);
|
||||||
|
|
||||||
@@ -75,20 +98,35 @@ export default class GitCodeLensProvider implements CodeLensProvider {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
locations = Array.from(map.values()).map(l => new Location(GitCodeLens.toUri(lens, l[0], l), lens.range.start))
|
Array.from(map.values()).forEach((lines, i) => {
|
||||||
|
const uri = GitBlameCodeLens.toUri(lens, i + 1, lines[0], lines);
|
||||||
|
lines.forEach(l => {
|
||||||
|
locations.push(new Location(uri, new Position(l.originalLine, 0)));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//locations = Array.from(map.values()).map((l, i) => new Location(GitBlameCodeLens.toUri(lens, i, l[0], l), new Position(l[0].originalLine, 0)));//lens.range.start))
|
||||||
} else {
|
} else {
|
||||||
locations = [new Location(GitCodeLens.toUri(lens, recentLine, lines), lens.range.start)];
|
locations = [new Location(GitBlameCodeLens.toUri(lens, 1, recentLine, lines), lens.range.start)];
|
||||||
}
|
}
|
||||||
|
|
||||||
lens.command = {
|
lens.command = {
|
||||||
title: `${recentLine.author}, ${moment(recentLine.date).fromNow()}`,
|
title: `${recentLine.author}, ${moment(recentLine.date).fromNow()}`,
|
||||||
command: Commands.ShowBlameHistory,
|
command: Commands.ShowBlameHistory,
|
||||||
arguments: [Uri.file(lens.fileName), lens.range.start, locations]
|
arguments: [Uri.file(lens.fileName), lens.range.start, locations]
|
||||||
// command: 'git.viewFileHistory',
|
|
||||||
// arguments: [Uri.file(codeLens.fileName)]
|
|
||||||
};
|
};
|
||||||
return lens;
|
return lens;
|
||||||
}).catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing
|
}).catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Play with this more -- get this to open the correct diff to the right place
|
||||||
|
if (lens instanceof GitHistoryCodeLens) {
|
||||||
|
lens.command = {
|
||||||
|
title: `View Diff`,
|
||||||
|
command: 'git.viewFileHistory', // viewLineHistory
|
||||||
|
arguments: [Uri.file(lens.fileName)]
|
||||||
|
};
|
||||||
|
return Promise.resolve(lens);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
import {Disposable, EventEmitter, ExtensionContext, Location, OverviewRulerLane, Range, TextEditorDecorationType, TextDocumentContentProvider, Uri, window, workspace} from 'vscode';
|
import {Disposable, EventEmitter, ExtensionContext, OverviewRulerLane, Range, TextEditor, TextEditorDecorationType, TextDocumentContentProvider, Uri, window, workspace} from 'vscode';
|
||||||
import {DocumentSchemes} from './constants';
|
import {DocumentSchemes} from './constants';
|
||||||
import {gitGetVersionFile, gitGetVersionText, IGitBlameLine} from './git';
|
import {gitGetVersionFile, gitGetVersionText, IGitBlameLine} from './git';
|
||||||
import {basename, dirname, extname} from 'path';
|
import {basename, dirname, extname, join} from 'path';
|
||||||
import * as moment from 'moment';
|
import * as moment from 'moment';
|
||||||
|
|
||||||
export default class GitBlameContentProvider implements TextDocumentContentProvider {
|
export default class GitBlameContentProvider implements TextDocumentContentProvider {
|
||||||
@@ -10,18 +10,37 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
|
|||||||
|
|
||||||
private _blameDecoration: TextEditorDecorationType;
|
private _blameDecoration: TextEditorDecorationType;
|
||||||
private _onDidChange = new EventEmitter<Uri>();
|
private _onDidChange = new EventEmitter<Uri>();
|
||||||
|
private _subscriptions: Disposable;
|
||||||
|
// private _dataMap: Map<string, IGitBlameUriData>;
|
||||||
|
|
||||||
constructor(context: ExtensionContext) {
|
constructor(context: ExtensionContext) {
|
||||||
let image = context.asAbsolutePath('blame.png');
|
// TODO: Light & Dark
|
||||||
this._blameDecoration = window.createTextEditorDecorationType({
|
this._blameDecoration = window.createTextEditorDecorationType({
|
||||||
backgroundColor: 'rgba(21, 251, 126, 0.7)',
|
backgroundColor: 'rgba(254, 220, 95, 0.15)',
|
||||||
gutterIconPath: image,
|
gutterIconPath: context.asAbsolutePath('blame.png'),
|
||||||
gutterIconSize: 'auto'
|
overviewRulerColor: 'rgba(254, 220, 95, 0.60)',
|
||||||
|
overviewRulerLane: OverviewRulerLane.Right,
|
||||||
|
isWholeLine: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// this._dataMap = new Map();
|
||||||
|
// this._subscriptions = Disposable.from(
|
||||||
|
// workspace.onDidOpenTextDocument(d => {
|
||||||
|
// let data = this._dataMap.get(d.uri.toString());
|
||||||
|
// if (!data) return;
|
||||||
|
|
||||||
|
// // TODO: This only works on the first load -- not after since it is cached
|
||||||
|
// this._tryAddBlameDecorations(d.uri, data);
|
||||||
|
// }),
|
||||||
|
// workspace.onDidCloseTextDocument(d => {
|
||||||
|
// this._dataMap.delete(d.uri.toString());
|
||||||
|
// })
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
this._onDidChange.dispose();
|
this._onDidChange.dispose();
|
||||||
|
this._subscriptions && this._subscriptions.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
get onDidChange() {
|
get onDidChange() {
|
||||||
@@ -34,21 +53,20 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
|
|||||||
|
|
||||||
provideTextDocumentContent(uri: Uri): string | Thenable<string> {
|
provideTextDocumentContent(uri: Uri): string | Thenable<string> {
|
||||||
const data = fromGitBlameUri(uri);
|
const data = fromGitBlameUri(uri);
|
||||||
|
// this._dataMap.set(uri.toString(), data);
|
||||||
|
|
||||||
console.log('provideTextDocumentContent', uri, data);
|
//const editor = this._findEditor(Uri.file(join(data.repoPath, data.file)));
|
||||||
|
|
||||||
|
//console.log('provideTextDocumentContent', uri, data);
|
||||||
return gitGetVersionText(data.repoPath, data.sha, data.file).then(text => {
|
return gitGetVersionText(data.repoPath, data.sha, data.file).then(text => {
|
||||||
this.update(uri);
|
this.update(uri);
|
||||||
|
|
||||||
setTimeout(() => {
|
// TODO: This only works on the first load -- not after since it is cached
|
||||||
let uriString = uri.toString();
|
this._tryAddBlameDecorations(uri, data);
|
||||||
let editor = window.visibleTextEditors.find((e: any) => (e._documentData && e._documentData._uri && e._documentData._uri.toString()) === uriString);
|
|
||||||
if (editor) {
|
// TODO: This needs to move to selection somehow to show on the main file editor
|
||||||
editor.setDecorations(this._blameDecoration, data.lines.map(l => new Range(l.line, 0, l.line, 1)));
|
//this._addBlameDecorations(editor, data);
|
||||||
}
|
|
||||||
}, 1500);
|
|
||||||
|
|
||||||
// let foo = text.split('\n');
|
|
||||||
// return foo.slice(data.range.start.line, data.range.end.line).join('\n')
|
|
||||||
return text;
|
return text;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -60,18 +78,47 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
|
|||||||
// });
|
// });
|
||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _findEditor(uri: Uri): TextEditor {
|
||||||
|
let uriString = uri.toString();
|
||||||
|
const matcher = (e: any) => (e._documentData && e._documentData._uri && e._documentData._uri.toString()) === uriString;
|
||||||
|
if (matcher(window.activeTextEditor)) {
|
||||||
|
return window.activeTextEditor;
|
||||||
|
}
|
||||||
|
return window.visibleTextEditors.find(matcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _tryAddBlameDecorations(uri: Uri, data: IGitBlameUriData) {
|
||||||
|
let handle = setInterval(() => {
|
||||||
|
let editor = this._findEditor(uri);
|
||||||
|
if (editor) {
|
||||||
|
clearInterval(handle);
|
||||||
|
editor.setDecorations(this._blameDecoration, data.lines.map(l => {
|
||||||
|
return {
|
||||||
|
range: editor.document.validateRange(new Range(l.originalLine, 0, l.originalLine, 1000000)),
|
||||||
|
hoverMessage: `${moment(l.date).fromNow()}\n${l.author}\n${l.sha}`
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
// private _addBlameDecorations(editor: TextEditor, data: IGitBlameUriData) {
|
||||||
|
// editor.setDecorations(this._blameDecoration, data.lines.map(l => editor.document.validateRange(new Range(l.line, 0, l.line, 1000000))));
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IGitBlameUriData extends IGitBlameLine {
|
export interface IGitBlameUriData extends IGitBlameLine {
|
||||||
repoPath: string,
|
repoPath: string,
|
||||||
range: Range,
|
range: Range,
|
||||||
|
index: number,
|
||||||
lines: IGitBlameLine[]
|
lines: IGitBlameLine[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toGitBlameUri(data: IGitBlameUriData) {
|
export function toGitBlameUri(data: IGitBlameUriData) {
|
||||||
let ext = extname(data.file);
|
let ext = extname(data.file);
|
||||||
let path = `${dirname(data.file)}/${data.sha}: ${basename(data.file, ext)}${ext}`;
|
let path = `${dirname(data.file)}/${data.sha}: ${basename(data.file, ext)}${ext}`;
|
||||||
return Uri.parse(`${DocumentSchemes.GitBlame}:${path}?${JSON.stringify(data)}`);
|
return Uri.parse(`${DocumentSchemes.GitBlame}:${data.index}. ${moment(data.date).format('YYYY-MM-DD hh:MMa')} ${path}?${JSON.stringify(data)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fromGitBlameUri(uri: Uri): IGitBlameUriData {
|
export function fromGitBlameUri(uri: Uri): IGitBlameUriData {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export function activate(context: ExtensionContext) {
|
|||||||
return commands.executeCommand(VsCodeCommands.ShowReferences, ...args);
|
return commands.executeCommand(VsCodeCommands.ShowReferences, ...args);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let selector: DocumentSelector = { scheme: 'file' };
|
const selector: DocumentSelector = { scheme: 'file' };
|
||||||
context.subscriptions.push(languages.registerCodeLensProvider(selector, new GitCodeLensProvider(repoPath)));
|
context.subscriptions.push(languages.registerCodeLensProvider(selector, new GitCodeLensProvider(repoPath)));
|
||||||
}).catch(reason => console.warn(reason));
|
}).catch(reason => console.warn(reason));
|
||||||
}
|
}
|
||||||
|
|||||||
65
src/git.ts
65
src/git.ts
@@ -14,48 +14,54 @@ export declare interface IGitBlameLine {
|
|||||||
code: string;
|
code: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function gitRepoPath(cwd) {
|
export function gitRepoPath(cwd): Promise<string> {
|
||||||
const mapper = (input, output) => {
|
let data: Array<string> = [];
|
||||||
output.push(input.toString().replace(/\r?\n|\r/g, ''))
|
const capture = input => data.push(input.toString().replace(/\r?\n|\r/g, ''));
|
||||||
};
|
const output = () => data[0];
|
||||||
|
|
||||||
return new Promise<string>((resolve, reject) => {
|
return gitCommand(cwd, capture, output, 'rev-parse', '--show-toplevel');
|
||||||
gitCommand(cwd, mapper, 'rev-parse', '--show-toplevel')
|
|
||||||
.then(result => resolve(result[0]))
|
// return new Promise<string>((resolve, reject) => {
|
||||||
.catch(reason => reject(reason));
|
// gitCommand(cwd, capture, output, 'rev-parse', '--show-toplevel')
|
||||||
});
|
// .then(result => resolve(result[0]))
|
||||||
|
// .catch(reason => reject(reason));
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
//const blameMatcher = /^(.*)\t\((.*)\t(.*)\t(.*?)\)(.*)$/gm;
|
//const blameMatcher = /^(.*)\t\((.*)\t(.*)\t(.*?)\)(.*)$/gm;
|
||||||
const blameMatcher = /^([0-9a-fA-F]{8})\s([\S]*)\s([0-9\S]+)\s\((.*?)\s([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[-|+][0-9]{4})\s([0-9]+)\)(.*)$/gm;
|
//const blameMatcher = /^([0-9a-fA-F]{8})\s([\S]*)\s([0-9\S]+)\s\((.*?)\s([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[-|+][0-9]{4})\s([0-9]+)\)(.*)$/gm;
|
||||||
|
const blameMatcher = /^([0-9a-fA-F]{8})\s([\S]*)\s+([0-9\S]+)\s\((.*)\s([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[-|+][0-9]{4})\s+([0-9]+)\)(.*)$/gm;
|
||||||
|
|
||||||
export function gitBlame(fileName: string): Promise<IGitBlameLine[]> {
|
export function gitBlame(fileName: string): Promise<IGitBlameLine[]> {
|
||||||
const mapper = (input, output) => {
|
let data: string = '';
|
||||||
let blame = input.toString();
|
const capture = input => data += input.toString();
|
||||||
console.log(fileName, 'Blame:', blame);
|
const output = () => {
|
||||||
|
let lines: Array<IGitBlameLine> = [];
|
||||||
let m: Array<string>;
|
let m: Array<string>;
|
||||||
while ((m = blameMatcher.exec(blame)) != null) {
|
while ((m = blameMatcher.exec(data)) != null) {
|
||||||
output.push({
|
lines.push({
|
||||||
sha: m[1],
|
sha: m[1],
|
||||||
file: m[2].trim(),
|
file: m[2].trim(),
|
||||||
originalLine: parseInt(m[3], 10),
|
originalLine: parseInt(m[3], 10) - 1,
|
||||||
author: m[4].trim(),
|
author: m[4].trim(),
|
||||||
date: new Date(m[5]),
|
date: new Date(m[5]),
|
||||||
line: parseInt(m[6], 10),
|
line: parseInt(m[6], 10) - 1,
|
||||||
code: m[7]
|
code: m[7]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return lines;
|
||||||
};
|
};
|
||||||
|
|
||||||
return gitCommand(dirname(fileName), mapper, 'blame', '-fnw', '--', fileName);
|
return gitCommand(dirname(fileName), capture, output, 'blame', '-fnw', '--', fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<any> {
|
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<any> {
|
||||||
const mapper = (input, output) => output.push(input);
|
let data: Array<any> = [];
|
||||||
|
const capture = input => data.push(input);
|
||||||
|
const output = () => data;
|
||||||
|
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
(gitCommand(repoPath, mapper, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<Buffer>>).then(o => {
|
(gitCommand(repoPath, capture, output, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<Buffer>>).then(o => {
|
||||||
let ext = extname(source);
|
let ext = extname(source);
|
||||||
tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
|
tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -79,19 +85,20 @@ export function gitGetVersionFile(repoPath: string, sha: string, source: string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function gitGetVersionText(repoPath: string, sha: string, source: string): Promise<string> {
|
export function gitGetVersionText(repoPath: string, sha: string, source: string): Promise<string> {
|
||||||
const mapper = (input, output) => output.push(input.toString());
|
let data: Array<string> = [];
|
||||||
|
const capture = input => data.push(input.toString());
|
||||||
|
const output = () => data;
|
||||||
|
|
||||||
return new Promise<string>((resolve, reject) => (gitCommand(repoPath, mapper, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<string>>).then(o => resolve(o.join())));
|
return new Promise<string>((resolve, reject) => (gitCommand(repoPath, capture, output, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<string>>).then(o => resolve(o.join())));
|
||||||
}
|
}
|
||||||
|
|
||||||
function gitCommand(cwd: string, mapper: (input: Buffer, output: Array<any>) => void, ...args): Promise<any> {
|
function gitCommand(cwd: string, capture: (input: Buffer) => void, output: () => any, ...args): Promise<any> {
|
||||||
return new Promise<any>((resolve, reject) => {
|
return new Promise<any>((resolve, reject) => {
|
||||||
let spawn = require('child_process').spawn;
|
let spawn = require('child_process').spawn;
|
||||||
let process = spawn('git', args, { cwd: cwd });
|
let process = spawn('git', args, { cwd: cwd });
|
||||||
|
|
||||||
let output: Array<any> = [];
|
|
||||||
process.stdout.on('data', data => {
|
process.stdout.on('data', data => {
|
||||||
mapper(data, output);
|
capture(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
let errors: Array<string> = [];
|
let errors: Array<string> = [];
|
||||||
@@ -105,7 +112,11 @@ function gitCommand(cwd: string, mapper: (input: Buffer, output: Array<any>) =>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(output);
|
try {
|
||||||
|
resolve(output());
|
||||||
|
} catch (ex) {
|
||||||
|
reject(ex);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user