This commit is contained in:
Eric Amodio
2016-11-03 03:09:33 -04:00
parent 8df6b80725
commit 409be335f9
38 changed files with 1094 additions and 520 deletions

View File

@@ -1,5 +1,5 @@
'use strict'
import {GitBlameFormat, GitCommit, IGitAuthor, IGitBlame, IGitCommit, IGitCommitLine, IGitEnricher} from './../git';
'use strict';
import { GitBlameFormat, GitCommit, IGitAuthor, IGitBlame, IGitCommit, IGitCommitLine, IGitEnricher } from './../git';
import * as moment from 'moment';
import * as path from 'path';
@@ -45,7 +45,7 @@ export class GitBlameParserEnricher implements IGitEnricher<IGitBlame> {
let entry: IBlameEntry;
let position = -1;
while (++position < lines.length) {
let lineParts = lines[position].split(" ");
let lineParts = lines[position].split(' ');
if (lineParts.length < 2) {
continue;
}
@@ -62,49 +62,49 @@ export class GitBlameParserEnricher implements IGitEnricher<IGitBlame> {
}
switch (lineParts[0]) {
case "author":
entry.author = lineParts.slice(1).join(" ").trim();
case 'author':
entry.author = lineParts.slice(1).join(' ').trim();
break;
// case "author-mail":
// case 'author-mail':
// entry.authorEmail = lineParts[1].trim();
// break;
case "author-time":
case 'author-time':
entry.authorDate = lineParts[1];
break;
case "author-tz":
case 'author-tz':
entry.authorTimeZone = lineParts[1];
break;
// case "committer":
// entry.committer = lineParts.slice(1).join(" ").trim();
// case 'committer':
// entry.committer = lineParts.slice(1).join(' ').trim();
// break;
// case "committer-mail":
// case 'committer-mail':
// entry.committerEmail = lineParts[1].trim();
// break;
// case "committer-time":
// case 'committer-time':
// entry.committerDate = lineParts[1];
// break;
// case "committer-tz":
// case 'committer-tz':
// entry.committerTimeZone = lineParts[1];
// break;
case "summary":
entry.summary = lineParts.slice(1).join(" ").trim();
case 'summary':
entry.summary = lineParts.slice(1).join(' ').trim();
break;
case "previous":
case 'previous':
entry.previousSha = lineParts[1].substring(0, 8);
entry.previousFileName = lineParts.slice(2).join(" ");
entry.previousFileName = lineParts.slice(2).join(' ');
break;
case "filename":
entry.fileName = lineParts.slice(1).join(" ");
case 'filename':
entry.fileName = lineParts.slice(1).join(' ');
entries.push(entry);
entry = null;
@@ -149,7 +149,7 @@ export class GitBlameParserEnricher implements IGitEnricher<IGitBlame> {
authors.set(entry.author, author);
}
commit = new GitCommit(repoPath, entry.sha, relativeFileName, entry.author, moment(`${entry.authorDate} ${entry.authorTimeZone}`, 'X Z').toDate(), entry.summary);
commit = new GitCommit(repoPath, entry.sha, relativeFileName, entry.author, moment(`${entry.authorDate} ${entry.authorTimeZone}`, 'X +-HHmm').toDate(), entry.summary);
if (relativeFileName !== entry.fileName) {
commit.originalFileName = entry.fileName;
@@ -168,7 +168,7 @@ export class GitBlameParserEnricher implements IGitEnricher<IGitBlame> {
sha: entry.sha,
line: entry.line + j,
originalLine: entry.originalLine + j
}
};
if (commit.previousSha) {
line.previousSha = commit.previousSha;
@@ -182,7 +182,8 @@ export class GitBlameParserEnricher implements IGitEnricher<IGitBlame> {
commits.forEach(c => authors.get(c.author).lineCount += c.lines.length);
const sortedAuthors: Map<string, IGitAuthor> = new Map();
const values = Array.from(authors.values())
// const values =
Array.from(authors.values())
.sort((a, b) => b.lineCount - a.lineCount)
.forEach(a => sortedAuthors.set(a.name, a));

View File

@@ -0,0 +1,143 @@
'use strict';
import { GitCommit, IGitAuthor, IGitCommit, IGitEnricher, IGitLog } from './../git';
import * as moment from 'moment';
import * as path from 'path';
interface ILogEntry {
sha: string;
author?: string;
authorDate?: string;
committer?: string;
committerDate?: string;
fileName?: string;
summary?: string;
}
export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
private _parseEntries(data: string): ILogEntry[] {
if (!data) return null;
const lines = data.split('\n');
if (!lines.length) return null;
const entries: ILogEntry[] = [];
let entry: ILogEntry;
let position = -1;
while (++position < lines.length) {
let lineParts = lines[position].split(' ');
if (lineParts.length < 2) {
continue;
}
if (!entry) {
entry = {
sha: lineParts[0].substring(0, 8)
};
continue;
}
switch (lineParts[0]) {
case 'author':
entry.author = lineParts.slice(1).join(' ').trim();
break;
case 'author-date':
entry.authorDate = lineParts.slice(1).join(' ').trim();
break;
// case 'committer':
// entry.committer = lineParts.slice(1).join(' ').trim();
// break;
// case 'committer-date':
// entry.committerDate = lineParts.slice(1).join(' ').trim();
// break;
case 'summary':
entry.summary = lineParts.slice(1).join(' ').trim();
break;
case 'filename':
position += 2;
lineParts = lines[position].split(' ');
entry.fileName = lineParts.join(' ');
entries.push(entry);
entry = null;
break;
default:
break;
}
}
return entries;
}
enrich(data: string, fileName: string): IGitLog {
const entries = this._parseEntries(data);
if (!entries) return null;
const authors: Map<string, IGitAuthor> = new Map();
const commits: Map<string, IGitCommit> = new Map();
let repoPath: string;
let relativeFileName: string;
for (let i = 0, len = entries.length; i < len; i++) {
const entry = entries[i];
if (i === 0) {
// Try to get the repoPath from the most recent commit
repoPath = fileName.replace(`/${entry.fileName}`, '');
relativeFileName = path.relative(repoPath, fileName).replace(/\\/g, '/');
}
let commit = commits.get(entry.sha);
if (!commit) {
let author = authors.get(entry.author);
if (!author) {
author = {
name: entry.author,
lineCount: 0
};
authors.set(entry.author, author);
}
commit = new GitCommit(repoPath, entry.sha, relativeFileName, entry.author, moment(entry.authorDate).toDate(), entry.summary);
if (relativeFileName !== entry.fileName) {
commit.originalFileName = entry.fileName;
}
commits.set(entry.sha, commit);
}
}
commits.forEach(c => authors.get(c.author).lineCount += c.lines.length);
const sortedAuthors: Map<string, IGitAuthor> = new Map();
// const values =
Array.from(authors.values())
.sort((a, b) => b.lineCount - a.lineCount)
.forEach(a => sortedAuthors.set(a.name, a));
// const sortedCommits: Map<string, IGitCommit> = new Map();
// Array.from(commits.values())
// .sort((a, b) => b.date.getTime() - a.date.getTime())
// .forEach(c => sortedCommits.set(c.sha, c));
return <IGitLog>{
repoPath: repoPath,
authors: sortedAuthors,
// commits: sortedCommits,
commits: commits
};
}
}

View File

@@ -2,28 +2,29 @@
import * as fs from 'fs';
import * as path from 'path';
import * as tmp from 'tmp';
import {spawnPromise} from 'spawn-rx';
import { spawnPromise } from 'spawn-rx';
export * from './gitEnrichment';
export * from './enrichers/blameParserEnricher';
export * from './enrichers/logParserEnricher';
const UncommittedRegex = /^[0]+$/;
function gitCommand(cwd: string, ...args) {
return spawnPromise('git', args, { cwd: cwd })
.then(s => {
console.log('[GitLens]', 'git', ...args, cwd);
return s;
})
.catch(ex => {
const msg = ex && ex.toString();
if (msg && (msg.includes('is outside repository') || msg.includes('no such path'))) {
console.warn('[GitLens]', 'git', ...args, cwd, msg && msg.replace(/\r?\n|\r/g, ' '));
} else {
console.error('[GitLens]', 'git', ...args, cwd, msg && msg.replace(/\r?\n|\r/g, ' '));
}
throw ex;
});
async function gitCommand(cwd: string, ...args: any[]) {
try {
const s = await spawnPromise('git', args, { cwd: cwd });
console.log('[GitLens]', 'git', ...args, cwd);
return s;
}
catch (ex) {
const msg = ex && ex.toString();
if (msg && (msg.includes('is outside repository') || msg.includes('no such path'))) {
console.warn('[GitLens]', 'git', ...args, cwd, msg && msg.replace(/\r?\n|\r/g, ' '));
} else {
console.error('[GitLens]', 'git', ...args, cwd, msg && msg.replace(/\r?\n|\r/g, ' '));
}
throw ex;
}
}
export type GitBlameFormat = '--incremental' | '--line-porcelain' | '--porcelain';
@@ -31,7 +32,7 @@ export const GitBlameFormat = {
incremental: '--incremental' as GitBlameFormat,
linePorcelain: '--line-porcelain' as GitBlameFormat,
porcelain: '--porcelain' as GitBlameFormat
}
};
export default class Git {
static normalizePath(fileName: string, repoPath?: string) {
@@ -72,6 +73,12 @@ export default class Git {
return gitCommand(root, 'blame', `-L ${startLine},${endLine}`, format, '--root', '--', file);
}
static log(fileName: string, repoPath?: string) {
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
return gitCommand(root, 'log', `--follow`, `--name-only`, `--no-merges`, `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nsummary %s%nfilename -`, file);
}
static getVersionedFile(fileName: string, repoPath: string, sha: string) {
return new Promise<string>((resolve, reject) => {
Git.getVersionedFileText(fileName, repoPath, sha).then(data => {

View File

@@ -1,10 +1,10 @@
'use strict'
'use strict';
import {Uri} from 'vscode';
import Git from './git';
import * as path from 'path';
export interface IGitEnricher<T> {
enrich(data: string, ...args): T;
enrich(data: string, ...args: any[]): T;
}
export interface IGitBlame {
@@ -89,4 +89,10 @@ export interface IGitCommitLine {
line: number;
originalLine: number;
code?: string;
}
export interface IGitLog {
repoPath: string;
authors: Map<string, IGitAuthor>;
commits: Map<string, IGitCommit>;
}