mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-29 01:25:42 -05:00
Renamed to GitLens
Reworked Uri scheme to drastically reduce encoded data (big perf improvement)
This commit is contained in:
50
src/git.ts
50
src/git.ts
@@ -1,50 +1,23 @@
|
||||
'use strict';
|
||||
import {basename, dirname, extname} from 'path';
|
||||
import {basename, dirname, extname, relative} from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as tmp from 'tmp';
|
||||
import {spawnPromise} from 'spawn-rx';
|
||||
|
||||
export declare interface IGitBlameLine {
|
||||
sha: string;
|
||||
file: string;
|
||||
originalLine: number;
|
||||
author: string;
|
||||
date: Date;
|
||||
line: number;
|
||||
//code: string;
|
||||
}
|
||||
|
||||
export function gitRepoPath(cwd) {
|
||||
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
|
||||
}
|
||||
|
||||
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) {
|
||||
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
|
||||
return gitCommand(dirname(fileName), 'blame', '-fnw', '--root', '--', fileName).then(data => {
|
||||
let lines: Array<IGitBlameLine> = [];
|
||||
let m: Array<string>;
|
||||
while ((m = blameMatcher.exec(data)) != null) {
|
||||
lines.push({
|
||||
sha: m[1],
|
||||
file: m[2].trim(),
|
||||
originalLine: parseInt(m[3], 10) - 1,
|
||||
author: m[4].trim(),
|
||||
date: new Date(m[5]),
|
||||
line: parseInt(m[6], 10) - 1
|
||||
//code: m[7]
|
||||
});
|
||||
}
|
||||
return lines;
|
||||
});
|
||||
return gitCommand(dirname(fileName), 'blame', '-fnw', '--root', '--', fileName);
|
||||
}
|
||||
|
||||
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<string> {
|
||||
export function gitGetVersionFile(fileName: string, repoPath: string, sha: string) {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
gitCommand(repoPath, 'show', `${sha.replace('^', '')}:${source.replace(/\\/g, '/')}`).then(data => {
|
||||
let ext = extname(source);
|
||||
tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
|
||||
gitGetVersionText(fileName, repoPath, sha).then(data => {
|
||||
let ext = extname(fileName);
|
||||
tmp.file({ prefix: `${basename(fileName, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
@@ -65,9 +38,14 @@ export function gitGetVersionFile(repoPath: string, sha: string, source: string)
|
||||
});
|
||||
}
|
||||
|
||||
export function gitGetVersionText(repoPath: string, sha: string, source: string) {
|
||||
console.log('git', 'show', `${sha}:${source.replace(/\\/g, '/')}`);
|
||||
return gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`);
|
||||
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);
|
||||
}
|
||||
|
||||
function normalizeArgument(fileName: string, repoPath: string, sha: string) {
|
||||
return `${sha.replace('^', '')}:${relative(repoPath, fileName.replace(/\\/g, '/'))}`;
|
||||
}
|
||||
|
||||
function gitCommand(cwd: string, ...args) {
|
||||
|
||||
Reference in New Issue
Block a user