Refactors git models & parsers

Adds full git status parsing
Adds git status info into status quick pick
Switches to async/await in file blame/log
This commit is contained in:
Eric Amodio
2017-03-19 00:36:51 -04:00
parent 14eebbba15
commit ef74ae0950
19 changed files with 556 additions and 407 deletions

View File

@@ -0,0 +1,201 @@
'use strict';
import { Git, GitCommit, IGitAuthor, IGitBlame, IGitCommitLine } from './../git';
import * as moment from 'moment';
import * as path from 'path';
interface IBlameEntry {
sha: string;
line: number;
originalLine: number;
lineCount: number;
author?: string;
authorEmail?: string;
authorDate?: string;
authorTimeZone?: string;
committer?: string;
committerEmail?: string;
committerDate?: string;
committerTimeZone?: string;
previousSha?: string;
previousFileName?: string;
fileName?: string;
summary?: string;
}
export class GitBlameParser {
private static _parseEntries(data: string): IBlameEntry[] {
if (!data) return undefined;
const lines = data.split('\n');
if (!lines.length) return undefined;
const entries: IBlameEntry[] = [];
let entry: IBlameEntry;
let position = -1;
while (++position < lines.length) {
let lineParts = lines[position].split(' ');
if (lineParts.length < 2) {
continue;
}
if (!entry) {
entry = {
sha: lineParts[0],
originalLine: parseInt(lineParts[1], 10) - 1,
line: parseInt(lineParts[2], 10) - 1,
lineCount: parseInt(lineParts[3], 10)
};
continue;
}
switch (lineParts[0]) {
case 'author':
entry.author = Git.isUncommitted(entry.sha)
? 'Uncommitted'
: lineParts.slice(1).join(' ').trim();
break;
// case 'author-mail':
// entry.authorEmail = lineParts[1].trim();
// break;
case 'author-time':
entry.authorDate = lineParts[1];
break;
case 'author-tz':
entry.authorTimeZone = lineParts[1];
break;
// case 'committer':
// entry.committer = lineParts.slice(1).join(' ').trim();
// break;
// case 'committer-mail':
// entry.committerEmail = lineParts[1].trim();
// break;
// case 'committer-time':
// entry.committerDate = lineParts[1];
// break;
// case 'committer-tz':
// entry.committerTimeZone = lineParts[1];
// break;
case 'summary':
entry.summary = lineParts.slice(1).join(' ').trim();
break;
case 'previous':
entry.previousSha = lineParts[1];
entry.previousFileName = lineParts.slice(2).join(' ');
break;
case 'filename':
entry.fileName = lineParts.slice(1).join(' ');
entries.push(entry);
entry = undefined;
break;
default:
break;
}
}
return entries;
}
static parse(data: string, fileName: string): IGitBlame {
const entries = this._parseEntries(data);
if (!entries) return undefined;
const authors: Map<string, IGitAuthor> = new Map();
const commits: Map<string, GitCommit> = new Map();
const lines: Array<IGitCommitLine> = [];
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} ${entry.authorTimeZone}`, 'X +-HHmm').toDate(), entry.summary);
if (relativeFileName !== entry.fileName) {
commit.originalFileName = entry.fileName;
}
if (entry.previousSha) {
commit.previousSha = entry.previousSha;
commit.previousFileName = entry.previousFileName;
}
commits.set(entry.sha, commit);
}
for (let j = 0, len = entry.lineCount; j < len; j++) {
const line: IGitCommitLine = {
sha: entry.sha,
line: entry.line + j,
originalLine: entry.originalLine + j
};
if (commit.previousSha) {
line.previousSha = commit.previousSha;
}
commit.lines.push(line);
lines[line.line] = line;
}
}
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 {
repoPath: repoPath,
authors: sortedAuthors,
// commits: sortedCommits,
commits: commits,
lines: lines
} as IGitBlame;
}
}

View File

@@ -0,0 +1,272 @@
'use strict';
import { Range } from 'vscode';
import { Git, GitStatusFileStatus, GitLogCommit, GitLogType, IGitAuthor, IGitLog } from './../git';
// import { Logger } from '../../logger';
import * as moment from 'moment';
import * as path from 'path';
interface ILogEntry {
sha: string;
author?: string;
authorDate?: string;
committer?: string;
committerDate?: string;
parentSha?: string;
fileName?: string;
originalFileName?: string;
fileStatuses?: { status: GitStatusFileStatus, fileName: string, originalFileName: string }[];
status?: GitStatusFileStatus;
summary?: string;
}
export class GitLogParser {
private static _parseEntries(data: string, isRepoPath: boolean): ILogEntry[] {
if (!data) return undefined;
const lines = data.split('\n');
if (!lines.length) return undefined;
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) {
if (!Git.shaRegex.test(lineParts[0])) continue;
entry = {
sha: lineParts[0]
};
continue;
}
switch (lineParts[0]) {
case 'author':
entry.author = Git.isUncommitted(entry.sha)
? 'Uncommitted'
: lineParts.slice(1).join(' ').trim();
break;
case 'author-date':
entry.authorDate = `${lineParts[1]}T${lineParts[2]}${lineParts[3]}`;
break;
// case 'committer':
// entry.committer = lineParts.slice(1).join(' ').trim();
// break;
// case 'committer-date':
// entry.committerDate = lineParts.slice(1).join(' ').trim();
// break;
case 'parent':
entry.parentSha = lineParts.slice(1).join(' ').trim();
break;
case 'summary':
entry.summary = lineParts.slice(1).join(' ').trim();
while (++position < lines.length) {
if (!lines[position]) break;
entry.summary += `\n${lines[position]}`;
}
break;
case 'filename':
if (isRepoPath) {
position++;
let diff = false;
while (++position < lines.length) {
lineParts = lines[position].split(' ');
if (Git.shaRegex.test(lineParts[0])) {
position--;
break;
}
if (diff) continue;
if (lineParts[0] === 'diff') {
diff = true;
entry.fileName = lineParts[2].substring(2);
const originalFileName = lineParts[3].substring(2);
if (entry.fileName !== originalFileName) {
entry.originalFileName = originalFileName;
}
continue;
}
if (entry.fileStatuses == null) {
entry.fileStatuses = [];
}
const status = {
status: lineParts[0][0] as GitStatusFileStatus,
fileName: lineParts[0].substring(1),
originalFileName: undefined as string
};
const index = status.fileName.indexOf('\t') + 1;
if (index) {
const next = status.fileName.indexOf('\t', index) + 1;
if (next) {
status.originalFileName = status.fileName.substring(index, next - 1);
status.fileName = status.fileName.substring(next);
}
else {
status.fileName = status.fileName.substring(index);
}
}
entry.fileStatuses.push(status);
}
if (entry.fileStatuses) {
entry.fileName = entry.fileStatuses.filter(_ => !!_.fileName).map(_ => _.fileName).join(', ');
}
}
else {
position += 2;
lineParts = lines[position].split(' ');
if (lineParts.length === 1) {
entry.status = lineParts[0][0] as GitStatusFileStatus;
entry.fileName = lineParts[0].substring(1);
}
else {
entry.status = lineParts[3][0] as GitStatusFileStatus;
entry.fileName = lineParts[0].substring(1);
position += 4;
}
const index = entry.fileName.indexOf('\t') + 1;
if (index) {
const next = entry.fileName.indexOf('\t', index) + 1;
if (next) {
entry.originalFileName = entry.fileName.substring(index, next - 1);
entry.fileName = entry.fileName.substring(next);
}
else {
entry.fileName = entry.fileName.substring(index);
}
}
}
entries.push(entry);
entry = undefined;
break;
default:
break;
}
}
return entries;
}
static parse(data: string, type: GitLogType, fileNameOrRepoPath: string, maxCount: number | undefined, isRepoPath: boolean, reverse: boolean, range: Range): IGitLog {
const entries = this._parseEntries(data, isRepoPath);
if (!entries) return undefined;
const authors: Map<string, IGitAuthor> = new Map();
const commits: Map<string, GitLogCommit> = new Map();
let repoPath: string;
let relativeFileName: string;
let recentCommit: GitLogCommit;
if (isRepoPath) {
repoPath = fileNameOrRepoPath;
}
for (let i = 0, len = entries.length; i < len; i++) {
// Since log --reverse doesn't properly honor a max count -- enforce it here
if (reverse && i >= maxCount) break;
const entry = entries[i];
if (i === 0 || isRepoPath) {
if (isRepoPath) {
relativeFileName = entry.fileName;
}
else {
// Try to get the repoPath from the most recent commit
repoPath = fileNameOrRepoPath.replace(fileNameOrRepoPath.startsWith('/') ? `/${entry.fileName}` : entry.fileName, '');
relativeFileName = path.relative(repoPath, fileNameOrRepoPath).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 GitLogCommit(type, repoPath, entry.sha, relativeFileName, entry.author, moment(entry.authorDate).toDate(), entry.summary, entry.status, entry.fileStatuses, undefined, entry.originalFileName);
if (relativeFileName !== entry.fileName) {
commit.originalFileName = entry.fileName;
}
commits.set(entry.sha, commit);
}
// else {
// Logger.log(`merge commit? ${entry.sha}`);
// }
if (recentCommit) {
recentCommit.previousSha = commit.sha;
// If the commit sha's match (merge commit), just forward it along
commit.nextSha = commit.sha !== recentCommit.sha ? recentCommit.sha : recentCommit.nextSha;
// Only add a filename if this is a file log
if (type === 'file') {
recentCommit.previousFileName = commit.originalFileName || commit.fileName;
commit.nextFileName = recentCommit.originalFileName || recentCommit.fileName;
}
}
recentCommit = 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 {
repoPath: repoPath,
authors: sortedAuthors,
// commits: sortedCommits,
commits: commits,
maxCount: maxCount,
range: range,
truncated: !!(maxCount && entries.length >= maxCount)
} as IGitLog;
}
}

View File

@@ -0,0 +1,89 @@
'use strict';
import { GitStatusFileStatus, GitStatusFile, IGitStatus } from './../git';
interface IFileStatusEntry {
staged: boolean;
status: GitStatusFileStatus;
fileName: string;
originalFileName: string;
}
export class GitStatusParser {
static parse(data: string, repoPath: string): IGitStatus {
if (!data) return undefined;
const lines = data.split('\n').filter(_ => !!_);
if (!lines.length) return undefined;
const status = {
repoPath: repoPath,
state: {
ahead: 0,
behind: 0
},
files: []
} as IGitStatus;
let position = -1;
while (++position < lines.length) {
const line = lines[position];
// Headers
if (line.startsWith('#')) {
const lineParts = line.split(' ');
switch (lineParts[1]) {
case 'branch.oid':
status.sha = lineParts[2];
break;
case 'branch.head':
status.branch = lineParts[2];
break;
case 'branch.upstream':
status.upstream = lineParts[2];
break;
case 'branch.ab':
status.state.ahead = +lineParts[2][1];
status.state.behind = +lineParts[3][1];
break;
}
}
else {
let lineParts = line.split(' ');
let entry: IFileStatusEntry;
switch (lineParts[0][0]) {
case '1': // normal
entry = this._parseFileEntry(lineParts[1], lineParts[8]);
break;
case '2': // rename
const file = lineParts[9].split('\t');
entry = this._parseFileEntry(lineParts[1], file[0], file[1]);
break;
case 'u': // unmerged
entry = this._parseFileEntry(lineParts[1], lineParts[10]);
break;
case '?': // untracked
entry = this._parseFileEntry(' ?', lineParts[1]);
break;
}
if (entry) {
status.files.push(new GitStatusFile(repoPath, entry.status, entry.staged, entry.fileName, entry.originalFileName));
}
}
}
return status;
}
private static _parseFileEntry(rawStatus: string, fileName: string, originalFileName?: string): IFileStatusEntry {
const indexStatus = rawStatus[0] !== '.' ? rawStatus[0].trim() : undefined;
const workTreeStatus = rawStatus[1] !== '.' ? rawStatus[1].trim() : undefined;
return {
status: (indexStatus || workTreeStatus || '?') as GitStatusFileStatus,
fileName: fileName,
originalFileName: originalFileName,
staged: !!indexStatus
};
}
}