Kaynağa Gözat

Fixes issues with file renames

And other git related edge cases
main
Eric Amodio 8 yıl önce
ebeveyn
işleme
92beca2542
5 değiştirilmiş dosya ile 65 ekleme ve 40 silme
  1. +5
    -5
      src/codeLensProvider.ts
  2. +2
    -2
      src/contentProvider.ts
  3. +1
    -1
      src/extension.ts
  4. +18
    -9
      src/git.ts
  5. +39
    -23
      src/gitBlameProvider.ts

+ 5
- 5
src/codeLensProvider.ts Dosyayı Görüntüle

@ -10,7 +10,7 @@ export class GitBlameCodeLens extends CodeLens {
}
getBlame(): Promise<IGitBlame> {
return this.blameProvider.getBlameForRange(this.fileName, this.blameRange);
return this.blameProvider.getBlameForRange(this.fileName, this.blameProvider.repoPath, this.blameRange);
}
static toUri(lens: GitBlameCodeLens, repoPath: string, commit: IGitBlameCommit, index: number, commitCount: number): Uri {
@ -32,7 +32,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { }
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
this.blameProvider.blameFile(document.fileName);
this.blameProvider.blameFile(document.fileName, this.blameProvider.repoPath);
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => {
let lenses: CodeLens[] = [];
@ -67,11 +67,11 @@ export default class GitCodeLensProvider implements CodeLensProvider {
const line = document.lineAt(symbol.location.range.start);
let startChar = line.text.indexOf(symbol.name); //line.firstNonWhitespaceCharacterIndex;
let startChar = line.text.search(`\\b${symbol.name}\\b`); //line.firstNonWhitespaceCharacterIndex;
if (startChar === -1) {
startChar = line.firstNonWhitespaceCharacterIndex;
} else {
startChar += Math.floor(symbol.name.length / 2) - 1;
startChar += Math.floor(symbol.name.length / 2);
}
lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
@ -96,7 +96,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
lens.command = {
title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`,
command: Commands.ShowBlameHistory,
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start] //, lens.locations]
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start]
};
resolve(lens);
});

+ 2
- 2
src/contentProvider.ts Dosyayı Görüntüle

@ -52,7 +52,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
//const editor = this._findEditor(Uri.file(join(data.repoPath, data.file)));
return gitGetVersionText(data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
return gitGetVersionText(data.originalFileName || data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
this.update(uri);
// TODO: This only works on the first load -- not after since it is cached
@ -89,7 +89,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
let editor = this._findEditor(uri);
if (editor) {
clearInterval(handle);
this.blameProvider.getBlameForShaRange(data.fileName, data.sha, data.range).then(blame => {
this.blameProvider.getBlameForShaRange(data.fileName, this.blameProvider.repoPath, data.sha, data.range).then(blame => {
if (blame.lines.length) {
editor.setDecorations(this._blameDecoration, blame.lines.map(l => {
return {

+ 1
- 1
src/extension.ts Dosyayı Görüntüle

@ -36,7 +36,7 @@ export function activate(context: ExtensionContext) {
if (!uri) return;
}
return blameProvider.getBlameLocations(uri.path, blameRange).then(locations => {
return blameProvider.getBlameLocations(uri.path, blameProvider.repoPath, blameRange).then(locations => {
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, range, locations);
});
}));

+ 18
- 9
src/git.ts Dosyayı Görüntüle

@ -1,16 +1,25 @@
'use strict';
import {basename, dirname, extname, relative} from 'path';
import {basename, dirname, extname, isAbsolute, relative} from 'path';
import * as fs from 'fs';
import * as tmp from 'tmp';
import {spawnPromise} from 'spawn-rx';
export function gitNormalizePath(fileName: string, repoPath: string) {
fileName = fileName.replace(/\\/g, '/');
return isAbsolute(fileName) ? relative(repoPath, fileName) : fileName;
}
export function gitRepoPath(cwd) {
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
}
export function gitBlame(fileName: string) {
export function gitBlame(fileName: string, repoPath: string) {
fileName = gitNormalizePath(fileName, repoPath);
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
return gitCommand(dirname(fileName), 'blame', '-fnw', '--root', '--', fileName);
return gitCommand(repoPath, 'blame', '-fnw', '--root', '--', fileName);
// .then(s => { console.log(s); return s; })
// .catch(ex => console.error(ex));
}
export function gitGetVersionFile(fileName: string, repoPath: string, sha: string) {
@ -39,13 +48,13 @@ export function gitGetVersionFile(fileName: string, repoPath: string, sha: strin
}
export function gitGetVersionText(fileName: string, repoPath: string, sha: string) {
const gitArg = normalizeArgument(fileName, repoPath, sha);
console.log('git', 'show', gitArg);
return gitCommand(dirname(fileName), 'show', gitArg);
}
fileName = gitNormalizePath(fileName, repoPath);
sha = sha.replace('^', '');
function normalizeArgument(fileName: string, repoPath: string, sha: string) {
return `${sha.replace('^', '')}:${relative(repoPath, fileName.replace(/\\/g, '/'))}`;
console.log('git', 'show', `${sha}:${fileName}`);
return gitCommand(repoPath, 'show', `${sha}:${fileName}`);
// .then(s => { console.log(s); return s; })
// .catch(ex => console.error(ex));
}
function gitCommand(cwd: string, ...args) {

+ 39
- 23
src/gitBlameProvider.ts Dosyayı Görüntüle

@ -1,7 +1,7 @@
import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode';
import {DocumentSchemes, WorkspaceState} from './constants';
import {gitBlame} from './git';
import {basename, dirname, extname, join} from 'path';
import {gitBlame, gitNormalizePath} from './git';
import {basename, dirname, extname} from 'path';
import * as moment from 'moment';
import * as _ from 'lodash';
@ -29,46 +29,53 @@ export default class GitBlameProvider extends Disposable {
super.dispose();
}
blameFile(fileName: string) {
blameFile(fileName: string, repoPath: string) {
fileName = gitNormalizePath(fileName, repoPath);
let blame = this._files.get(fileName);
if (blame !== undefined) return blame;
blame = gitBlame(fileName)
blame = gitBlame(fileName, repoPath)
.then(data => {
const commits: Map<string, IGitBlameCommit> = new Map();
const lines: Array<IGitBlameLine> = [];
let m: Array<string>;
while ((m = blameMatcher.exec(data)) != null) {
let sha = m[1];
if (!commits.has(sha)) {
commits.set(sha, {
sha,
fileName: m[2].trim(),
fileName: fileName,
author: m[4].trim(),
date: new Date(m[5])
});
}
lines.push({
const line: IGitBlameLine = {
sha,
line: parseInt(m[6], 10) - 1,
originalLine: parseInt(m[3], 10) - 1,
line: parseInt(m[6], 10) - 1
//code: m[7]
});
}
let file = m[2].trim();
if (!fileName.toLowerCase().endsWith(file.toLowerCase())) {
line.originalFileName = file;
}
lines.push(line);
}
return { commits, lines };
});
// .catch(ex => {
// console.error(ex);
// });
this._files.set(fileName, blame);
return blame;
}
getBlameForRange(fileName: string, range: Range): Promise<IGitBlame> {
return this.blameFile(fileName).then(blame => {
getBlameForRange(fileName: string, repoPath: string, range: Range): Promise<IGitBlame> {
return this.blameFile(fileName, repoPath).then(blame => {
if (!blame.lines.length) return blame;
const lines = blame.lines.slice(range.start.line, range.end.line + 1);
@ -79,8 +86,8 @@ export default class GitBlameProvider extends Disposable {
});
}
getBlameForShaRange(fileName: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> {
return this.blameFile(fileName).then(blame => {
getBlameForShaRange(fileName: string, repoPath: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> {
return this.blameFile(fileName, repoPath).then(blame => {
return {
commit: blame.commits.get(sha),
lines: blame.lines.slice(range.start.line, range.end.line + 1).filter(l => l.sha === sha)
@ -88,8 +95,8 @@ export default class GitBlameProvider extends Disposable {
});
}
getBlameLocations(fileName: string, range: Range) {
return this.getBlameForRange(fileName, range).then(blame => {
getBlameLocations(fileName: string, repoPath: string, range: Range) {
return this.getBlameForRange(fileName, repoPath, range).then(blame => {
const commitCount = blame.commits.size;
const locations: Array<Location> = [];
@ -99,7 +106,10 @@ export default class GitBlameProvider extends Disposable {
const uri = GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount);
blame.lines
.filter(l => l.sha === c.sha)
.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
.forEach(l => locations.push(new Location(l.originalFileName
? GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount, l.originalFileName)
: uri,
new Position(l.originalLine, 0))));
});
return locations;
@ -110,12 +120,16 @@ export default class GitBlameProvider extends Disposable {
this._files.delete(fileName);
}
static toBlameUri(repoPath: string, commit: IGitBlameCommit, range: Range, index: number, commitCount: number) {
static toBlameUri(repoPath: string, commit: IGitBlameCommit, range: Range, index: number, commitCount: number, originalFileName?: string) {
const pad = n => ("0000000" + n).slice(-("" + commitCount).length);
const ext = extname(commit.fileName);
const path = `${dirname(commit.fileName)}/${commit.sha}: ${basename(commit.fileName, ext)}${ext}`;
const data: IGitBlameUriData = { fileName: join(repoPath, commit.fileName), sha: commit.sha, range: range, index: index };
const fileName = originalFileName || commit.fileName;
const ext = extname(fileName);
const path = `${dirname(fileName)}/${commit.sha}: ${basename(fileName, ext)}${ext}`;
const data: IGitBlameUriData = { fileName: commit.fileName, sha: commit.sha, range: range, index: index };
if (originalFileName) {
data.originalFileName = originalFileName;
}
// NOTE: Need to specify an index here, since I can't control the sort order -- just alphabetic or by file location
return Uri.parse(`${DocumentSchemes.GitBlame}:${pad(index)}. ${commit.author}, ${moment(commit.date).format('MMM D, YYYY hh:MMa')} - ${path}?${JSON.stringify(data)}`);
}
@ -140,12 +154,14 @@ export interface IGitBlameCommit {
}
export interface IGitBlameLine {
sha: string;
originalLine: number;
line: number;
originalLine: number;
originalFileName?: string;
code?: string;
}
export interface IGitBlameUriData {
fileName: string,
originalFileName?: string;
sha: string,
range: Range,
index: number

Yükleniyor…
İptal
Kaydet