|
|
@ -1,41 +1,54 @@ |
|
|
|
'use strict' |
|
|
|
import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode'; |
|
|
|
import {DocumentSchemes, WorkspaceState} from './constants'; |
|
|
|
import {gitBlame, gitNormalizePath} from './git'; |
|
|
|
import Git from './git'; |
|
|
|
import {basename, dirname, extname} from 'path'; |
|
|
|
import * as moment from 'moment'; |
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
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 default class GitBlameProvider extends Disposable { |
|
|
|
export default class GitProvider extends Disposable { |
|
|
|
public repoPath: string; |
|
|
|
|
|
|
|
private _files: Map<string, Promise<IGitBlame>>; |
|
|
|
private _subscriptions: Disposable; |
|
|
|
private _blames: Map<string, Promise<IGitBlame>>; |
|
|
|
private _subscription: Disposable; |
|
|
|
|
|
|
|
constructor(context: ExtensionContext) { |
|
|
|
super(() => this.dispose()); |
|
|
|
|
|
|
|
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string; |
|
|
|
|
|
|
|
this._files = new Map(); |
|
|
|
this._subscriptions = Disposable.from(workspace.onDidCloseTextDocument(d => this._removeFile(d.fileName)), |
|
|
|
workspace.onDidChangeTextDocument(e => this._removeFile(e.document.fileName))); |
|
|
|
this._blames = new Map(); |
|
|
|
this._subscription = Disposable.from(workspace.onDidCloseTextDocument(d => this._removeFile(d.fileName)), |
|
|
|
workspace.onDidChangeTextDocument(e => this._removeFile(e.document.fileName))); |
|
|
|
} |
|
|
|
|
|
|
|
dispose() { |
|
|
|
this._files.clear(); |
|
|
|
this._subscriptions && this._subscriptions.dispose(); |
|
|
|
this._blames.clear(); |
|
|
|
this._subscription && this._subscription.dispose(); |
|
|
|
super.dispose(); |
|
|
|
} |
|
|
|
|
|
|
|
blameFile(fileName: string, repoPath: string) { |
|
|
|
fileName = gitNormalizePath(fileName, repoPath); |
|
|
|
private _removeFile(fileName: string) { |
|
|
|
this._blames.delete(fileName); |
|
|
|
} |
|
|
|
|
|
|
|
getRepoPath(cwd: string) { |
|
|
|
return Git.repoPath(cwd); |
|
|
|
} |
|
|
|
|
|
|
|
getCommitMessage(sha: string) { |
|
|
|
return Git.getCommitMessage(sha, this.repoPath); |
|
|
|
} |
|
|
|
|
|
|
|
getBlameForFile(fileName: string) { |
|
|
|
fileName = Git.normalizePath(fileName, this.repoPath); |
|
|
|
|
|
|
|
let blame = this._files.get(fileName); |
|
|
|
let blame = this._blames.get(fileName); |
|
|
|
if (blame !== undefined) return blame; |
|
|
|
|
|
|
|
blame = gitBlame(fileName, repoPath) |
|
|
|
blame = Git.blame(fileName, this.repoPath) |
|
|
|
.then(data => { |
|
|
|
const commits: Map<string, IGitBlameCommit> = new Map(); |
|
|
|
const lines: Array<IGitBlameLine> = []; |
|
|
@ -70,12 +83,12 @@ export default class GitBlameProvider extends Disposable { |
|
|
|
return { commits, lines }; |
|
|
|
}); |
|
|
|
|
|
|
|
this._files.set(fileName, blame); |
|
|
|
this._blames.set(fileName, blame); |
|
|
|
return blame; |
|
|
|
} |
|
|
|
|
|
|
|
getBlameForRange(fileName: string, repoPath: string, range: Range): Promise<IGitBlame> { |
|
|
|
return this.blameFile(fileName, repoPath).then(blame => { |
|
|
|
getBlameForRange(fileName: string, range: Range): Promise<IGitBlame> { |
|
|
|
return this.getBlameForFile(fileName).then(blame => { |
|
|
|
if (!blame.lines.length) return blame; |
|
|
|
|
|
|
|
const lines = blame.lines.slice(range.start.line, range.end.line + 1); |
|
|
@ -86,8 +99,8 @@ export default class GitBlameProvider extends Disposable { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
getBlameForShaRange(fileName: string, repoPath: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> { |
|
|
|
return this.blameFile(fileName, repoPath).then(blame => { |
|
|
|
getBlameForShaRange(fileName: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> { |
|
|
|
return this.getBlameForFile(fileName).then(blame => { |
|
|
|
return { |
|
|
|
commit: blame.commits.get(sha), |
|
|
|
lines: blame.lines.slice(range.start.line, range.end.line + 1).filter(l => l.sha === sha) |
|
|
@ -95,19 +108,19 @@ export default class GitBlameProvider extends Disposable { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
getBlameLocations(fileName: string, repoPath: string, range: Range) { |
|
|
|
return this.getBlameForRange(fileName, repoPath, range).then(blame => { |
|
|
|
getBlameLocations(fileName: string, range: Range) { |
|
|
|
return this.getBlameForRange(fileName, range).then(blame => { |
|
|
|
const commitCount = blame.commits.size; |
|
|
|
|
|
|
|
const locations: Array<Location> = []; |
|
|
|
Array.from(blame.commits.values()) |
|
|
|
.sort((a, b) => b.date.getTime() - a.date.getTime()) |
|
|
|
.forEach((c, i) => { |
|
|
|
const uri = GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount); |
|
|
|
const uri = this.toBlameUri(c, range, i + 1, commitCount); |
|
|
|
blame.lines |
|
|
|
.filter(l => l.sha === c.sha) |
|
|
|
.forEach(l => locations.push(new Location(l.originalFileName |
|
|
|
? GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount, l.originalFileName) |
|
|
|
? this.toBlameUri(c, range, i + 1, commitCount, l.originalFileName) |
|
|
|
: uri, |
|
|
|
new Position(l.originalLine, 0)))); |
|
|
|
}); |
|
|
@ -116,11 +129,15 @@ export default class GitBlameProvider extends Disposable { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private _removeFile(fileName: string) { |
|
|
|
this._files.delete(fileName); |
|
|
|
getVersionedFile(fileName: string, sha: string) { |
|
|
|
return Git.getVersionedFile(fileName, this.repoPath, sha); |
|
|
|
} |
|
|
|
|
|
|
|
getVersionedFileText(fileName: string, sha: string) { |
|
|
|
return Git.getVersionedFileText(fileName, this.repoPath, sha); |
|
|
|
} |
|
|
|
|
|
|
|
static toBlameUri(repoPath: string, commit: IGitBlameCommit, range: Range, index: number, commitCount: number, originalFileName?: string) { |
|
|
|
toBlameUri(commit: IGitBlameCommit, range: Range, index: number, commitCount: number, originalFileName?: string) { |
|
|
|
const pad = n => ("0000000" + n).slice(-("" + commitCount).length); |
|
|
|
|
|
|
|
const fileName = originalFileName || commit.fileName; |
|
|
@ -131,10 +148,10 @@ export default class GitBlameProvider extends Disposable { |
|
|
|
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)}`); |
|
|
|
return Uri.parse(`${DocumentSchemes.GitBlame}:${pad(index)}. ${commit.author}, ${moment(commit.date).format('MMM D, YYYY hh:MM a')} - ${path}?${JSON.stringify(data)}`); |
|
|
|
} |
|
|
|
|
|
|
|
static fromBlameUri(uri: Uri): IGitBlameUriData { |
|
|
|
fromBlameUri(uri: Uri) { |
|
|
|
const data = JSON.parse(uri.query); |
|
|
|
data.range = new Range(data.range[0].line, data.range[0].character, data.range[1].line, data.range[1].character); |
|
|
|
return data; |