mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-15 09:35:42 -05:00
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:
@@ -6,13 +6,14 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as tmp from 'tmp';
|
||||
|
||||
export * from './gitEnrichment';
|
||||
export * from './enrichers/blameParserEnricher';
|
||||
export * from './enrichers/logParserEnricher';
|
||||
export * from './models/models';
|
||||
export * from './parsers/blameParser';
|
||||
export * from './parsers/logParser';
|
||||
export * from './parsers/statusParser';
|
||||
|
||||
let git: IGit;
|
||||
|
||||
const DefaultLogParams = [`log`, `--name-status`, `--full-history`, `-M`, `--date=iso8601-strict`, `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nparent %P%nsummary %B%nfilename ?`];
|
||||
const defaultLogParams = [`log`, `--name-status`, `--full-history`, `-M`, `--date=iso8601-strict`, `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nparent %P%nsummary %B%nfilename ?`];
|
||||
|
||||
async function gitCommand(cwd: string, ...args: any[]) {
|
||||
try {
|
||||
@@ -32,17 +33,10 @@ async function gitCommand(cwd: string, ...args: any[]) {
|
||||
}
|
||||
}
|
||||
|
||||
export type GitBlameFormat = '--incremental' | '--line-porcelain' | '--porcelain';
|
||||
export const GitBlameFormat = {
|
||||
incremental: '--incremental' as GitBlameFormat,
|
||||
linePorcelain: '--line-porcelain' as GitBlameFormat,
|
||||
porcelain: '--porcelain' as GitBlameFormat
|
||||
};
|
||||
|
||||
export class Git {
|
||||
|
||||
static ShaRegex = /^[0-9a-f]{40}( -)?$/;
|
||||
static UncommittedRegex = /^[0]+$/;
|
||||
static shaRegex = /^[0-9a-f]{40}( -)?$/;
|
||||
static uncommittedRegex = /^[0]+$/;
|
||||
|
||||
static gitInfo(): IGit {
|
||||
return git;
|
||||
@@ -84,11 +78,11 @@ export class Git {
|
||||
}
|
||||
|
||||
static isSha(sha: string) {
|
||||
return Git.ShaRegex.test(sha);
|
||||
return Git.shaRegex.test(sha);
|
||||
}
|
||||
|
||||
static isUncommitted(sha: string) {
|
||||
return Git.UncommittedRegex.test(sha);
|
||||
return Git.uncommittedRegex.test(sha);
|
||||
}
|
||||
|
||||
static normalizePath(fileName: string, repoPath?: string) {
|
||||
@@ -111,10 +105,10 @@ export class Git {
|
||||
|
||||
// Git commands
|
||||
|
||||
static blame(repoPath: string, fileName: string, format: GitBlameFormat, sha?: string, startLine?: number, endLine?: number) {
|
||||
static blame(repoPath: string, fileName: string, sha?: string, startLine?: number, endLine?: number) {
|
||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
||||
|
||||
const params = [`blame`, `--root`, format];
|
||||
const params = [`blame`, `--root`, `--incremental`];
|
||||
|
||||
if (startLine != null && endLine != null) {
|
||||
params.push(`-L ${startLine},${endLine}`);
|
||||
@@ -127,8 +121,11 @@ export class Git {
|
||||
return gitCommand(root, ...params, `--`, file);
|
||||
}
|
||||
|
||||
static branch(repoPath: string) {
|
||||
const params = [`branch`, `-a`];
|
||||
static branch(repoPath: string, all: boolean) {
|
||||
const params = [`branch`];
|
||||
if (all) {
|
||||
params.push(`-a`);
|
||||
}
|
||||
|
||||
return gitCommand(repoPath, ...params);
|
||||
}
|
||||
@@ -163,7 +160,7 @@ export class Git {
|
||||
}
|
||||
|
||||
static log(repoPath: string, sha?: string, maxCount?: number, reverse: boolean = false) {
|
||||
const params = [...DefaultLogParams];
|
||||
const params = [...defaultLogParams];
|
||||
if (maxCount && !reverse) {
|
||||
params.push(`-n${maxCount}`);
|
||||
}
|
||||
@@ -183,7 +180,7 @@ export class Git {
|
||||
static log_file(repoPath: string, fileName: string, sha?: string, maxCount?: number, reverse: boolean = false, startLine?: number, endLine?: number) {
|
||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
||||
|
||||
const params = [...DefaultLogParams, `--no-merges`, `--follow`];
|
||||
const params = [...defaultLogParams, `--no-merges`, `--follow`];
|
||||
if (maxCount && !reverse) {
|
||||
params.push(`-n${maxCount}`);
|
||||
}
|
||||
@@ -209,14 +206,14 @@ export class Git {
|
||||
}
|
||||
|
||||
static status(repoPath: string): Promise<string> {
|
||||
const params = ['status', '--short'];
|
||||
const params = ['status', '--porcelain=v2', '--branch'];
|
||||
return gitCommand(repoPath, ...params);
|
||||
}
|
||||
|
||||
static status_file(repoPath: string, fileName: string): Promise<string> {
|
||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
||||
|
||||
const params = ['status', file, '--short'];
|
||||
const params = ['status', '--porcelain=v2', file];
|
||||
return gitCommand(root, ...params);
|
||||
}
|
||||
}
|
||||
@@ -1,240 +0,0 @@
|
||||
'use strict';
|
||||
import { Uri } from 'vscode';
|
||||
import { Git } from './git';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface IGitEnricher<T> {
|
||||
enrich(data: string, ...args: any[]): T;
|
||||
}
|
||||
|
||||
export interface IGitBlame {
|
||||
repoPath: string;
|
||||
authors: Map<string, IGitAuthor>;
|
||||
commits: Map<string, GitCommit>;
|
||||
lines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitBlameLine {
|
||||
author: IGitAuthor;
|
||||
commit: GitCommit;
|
||||
line: IGitCommitLine;
|
||||
}
|
||||
|
||||
export interface IGitBlameLines extends IGitBlame {
|
||||
allLines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitBlameCommitLines {
|
||||
author: IGitAuthor;
|
||||
commit: GitCommit;
|
||||
lines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitAuthor {
|
||||
name: string;
|
||||
lineCount: number;
|
||||
}
|
||||
|
||||
interface IGitCommit {
|
||||
repoPath: string;
|
||||
sha: string;
|
||||
fileName: string;
|
||||
author: string;
|
||||
date: Date;
|
||||
message: string;
|
||||
lines: IGitCommitLine[];
|
||||
originalFileName?: string;
|
||||
previousSha?: string;
|
||||
previousFileName?: string;
|
||||
|
||||
readonly isUncommitted: boolean;
|
||||
previousUri: Uri;
|
||||
uri: Uri;
|
||||
}
|
||||
|
||||
export class GitCommit implements IGitCommit {
|
||||
|
||||
lines: IGitCommitLine[];
|
||||
originalFileName?: string;
|
||||
previousSha?: string;
|
||||
previousFileName?: string;
|
||||
private _isUncommitted: boolean | undefined;
|
||||
|
||||
constructor(
|
||||
public repoPath: string,
|
||||
public sha: string,
|
||||
public fileName: string,
|
||||
public author: string,
|
||||
public date: Date,
|
||||
public message: string,
|
||||
lines?: IGitCommitLine[],
|
||||
originalFileName?: string,
|
||||
previousSha?: string,
|
||||
previousFileName?: string
|
||||
) {
|
||||
this.fileName = this.fileName.replace(/, ?$/, '');
|
||||
|
||||
this.lines = lines || [];
|
||||
this.originalFileName = originalFileName;
|
||||
this.previousSha = previousSha;
|
||||
this.previousFileName = previousFileName;
|
||||
}
|
||||
|
||||
get shortSha() {
|
||||
return this.sha.substring(0, 8);
|
||||
}
|
||||
|
||||
get isUncommitted(): boolean {
|
||||
if (this._isUncommitted === undefined) {
|
||||
this._isUncommitted = Git.isUncommitted(this.sha);
|
||||
}
|
||||
return this._isUncommitted;
|
||||
}
|
||||
|
||||
get previousShortSha() {
|
||||
return this.previousSha && this.previousSha.substring(0, 8);
|
||||
}
|
||||
|
||||
get previousUri(): Uri {
|
||||
return this.previousFileName ? Uri.file(path.resolve(this.repoPath, this.previousFileName)) : this.uri;
|
||||
}
|
||||
|
||||
get uri(): Uri {
|
||||
return Uri.file(path.resolve(this.repoPath, this.originalFileName || this.fileName));
|
||||
}
|
||||
|
||||
getFormattedPath(separator: string = ' \u00a0\u2022\u00a0 '): string {
|
||||
const directory = path.dirname(this.fileName);
|
||||
return (!directory || directory === '.')
|
||||
? path.basename(this.fileName)
|
||||
: `${path.basename(this.fileName)}${separator}${directory}`;
|
||||
}
|
||||
}
|
||||
|
||||
export type GitLogType = 'file' | 'repo';
|
||||
|
||||
export class GitLogCommit extends GitCommit {
|
||||
|
||||
fileNames: string;
|
||||
fileStatuses: { status: GitFileStatus, fileName: string }[];
|
||||
nextSha?: string;
|
||||
nextFileName?: string;
|
||||
status: GitFileStatus;
|
||||
|
||||
constructor(
|
||||
public type: GitLogType,
|
||||
repoPath: string,
|
||||
sha: string,
|
||||
fileName: string,
|
||||
author: string,
|
||||
date: Date,
|
||||
message: string,
|
||||
status?: GitFileStatus,
|
||||
fileStatuses?: { status: GitFileStatus, fileName: string }[],
|
||||
lines?: IGitCommitLine[],
|
||||
originalFileName?: string,
|
||||
previousSha?: string,
|
||||
previousFileName?: string
|
||||
) {
|
||||
super(repoPath, sha, fileName, author, date, message, lines, originalFileName, previousSha, previousFileName);
|
||||
this.status = status;
|
||||
|
||||
this.fileNames = this.fileName;
|
||||
|
||||
if (fileStatuses) {
|
||||
this.fileStatuses = fileStatuses.filter(_ => !!_.fileName);
|
||||
this.fileName = this.fileStatuses[0].fileName;
|
||||
}
|
||||
else {
|
||||
this.fileStatuses = [{ status: status, fileName: fileName }];
|
||||
}
|
||||
}
|
||||
|
||||
get nextShortSha() {
|
||||
return this.nextSha && this.nextSha.substring(0, 8);
|
||||
}
|
||||
|
||||
get nextUri(): Uri {
|
||||
return this.nextFileName ? Uri.file(path.resolve(this.repoPath, this.nextFileName)) : this.uri;
|
||||
}
|
||||
}
|
||||
|
||||
export interface IGitCommitLine {
|
||||
sha: string;
|
||||
previousSha?: string;
|
||||
line: number;
|
||||
originalLine: number;
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export interface IGitLog {
|
||||
repoPath: string;
|
||||
authors: Map<string, IGitAuthor>;
|
||||
commits: Map<string, GitLogCommit>;
|
||||
|
||||
maxCount: number | undefined;
|
||||
truncated: boolean;
|
||||
}
|
||||
|
||||
export declare type GitFileStatus = '?' | 'A' | 'C' | 'D' | 'M' | 'R' | 'U';
|
||||
|
||||
export class GitFileStatusItem {
|
||||
|
||||
staged: boolean;
|
||||
status: GitFileStatus;
|
||||
fileName: string;
|
||||
|
||||
constructor(public repoPath: string, status: string) {
|
||||
this.fileName = status.substring(3);
|
||||
this.parseStatus(status);
|
||||
}
|
||||
|
||||
private parseStatus(status: string) {
|
||||
const indexStatus = status[0].trim();
|
||||
const workTreeStatus = status[1].trim();
|
||||
|
||||
this.staged = !!indexStatus;
|
||||
this.status = (indexStatus || workTreeStatus || 'U') as GitFileStatus;
|
||||
}
|
||||
}
|
||||
|
||||
const statusOcticonsMap = {
|
||||
'?': '$(diff-ignored)',
|
||||
A: '$(diff-added)',
|
||||
C: '$(diff-added)',
|
||||
D: '$(diff-removed)',
|
||||
M: '$(diff-modified)',
|
||||
R: '$(diff-renamed)',
|
||||
U: '$(question)'
|
||||
};
|
||||
export function getGitStatusIcon(status: GitFileStatus, missing: string = '\u00a0\u00a0\u00a0\u00a0'): string {
|
||||
return statusOcticonsMap[status] || missing;
|
||||
}
|
||||
|
||||
export class GitBranch {
|
||||
|
||||
current: boolean;
|
||||
name: string;
|
||||
remote: boolean;
|
||||
|
||||
constructor(branch: string) {
|
||||
branch = branch.trim();
|
||||
|
||||
if (branch.startsWith('* ')) {
|
||||
branch = branch.substring(2);
|
||||
this.current = true;
|
||||
}
|
||||
|
||||
if (branch.startsWith('remotes/')) {
|
||||
branch = branch.substring(8);
|
||||
this.remote = true;
|
||||
}
|
||||
|
||||
const index = branch.indexOf(' ');
|
||||
if (index !== -1) {
|
||||
branch = branch.substring(0, index);
|
||||
}
|
||||
|
||||
this.name = branch;
|
||||
}
|
||||
}
|
||||
@@ -87,9 +87,9 @@ export class GitUri extends Uri {
|
||||
}
|
||||
|
||||
export interface IGitCommitInfo {
|
||||
sha: string;
|
||||
repoPath: string;
|
||||
fileName: string;
|
||||
repoPath: string;
|
||||
sha?: string;
|
||||
originalFileName?: string;
|
||||
}
|
||||
|
||||
|
||||
25
src/git/models/blame.ts
Normal file
25
src/git/models/blame.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
import { GitCommit, IGitAuthor, IGitCommitLine } from './commit';
|
||||
|
||||
export interface IGitBlame {
|
||||
repoPath: string;
|
||||
authors: Map<string, IGitAuthor>;
|
||||
commits: Map<string, GitCommit>;
|
||||
lines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitBlameLine {
|
||||
author: IGitAuthor;
|
||||
commit: GitCommit;
|
||||
line: IGitCommitLine;
|
||||
}
|
||||
|
||||
export interface IGitBlameLines extends IGitBlame {
|
||||
allLines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitBlameCommitLines {
|
||||
author: IGitAuthor;
|
||||
commit: GitCommit;
|
||||
lines: IGitCommitLine[];
|
||||
}
|
||||
29
src/git/models/branch.ts
Normal file
29
src/git/models/branch.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
export class GitBranch {
|
||||
|
||||
current: boolean;
|
||||
name: string;
|
||||
remote: boolean;
|
||||
|
||||
constructor(branch: string) {
|
||||
branch = branch.trim();
|
||||
|
||||
if (branch.startsWith('* ')) {
|
||||
branch = branch.substring(2);
|
||||
this.current = true;
|
||||
}
|
||||
|
||||
if (branch.startsWith('remotes/')) {
|
||||
branch = branch.substring(8);
|
||||
this.remote = true;
|
||||
}
|
||||
|
||||
const index = branch.indexOf(' ');
|
||||
if (index !== -1) {
|
||||
branch = branch.substring(0, index);
|
||||
}
|
||||
|
||||
this.name = branch;
|
||||
}
|
||||
}
|
||||
94
src/git/models/commit.ts
Normal file
94
src/git/models/commit.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
'use strict';
|
||||
import { Uri } from 'vscode';
|
||||
import { Git } from '../git';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface IGitAuthor {
|
||||
name: string;
|
||||
lineCount: number;
|
||||
}
|
||||
|
||||
export interface IGitCommit {
|
||||
repoPath: string;
|
||||
sha: string;
|
||||
fileName: string;
|
||||
author: string;
|
||||
date: Date;
|
||||
message: string;
|
||||
lines: IGitCommitLine[];
|
||||
originalFileName?: string;
|
||||
previousSha?: string;
|
||||
previousFileName?: string;
|
||||
|
||||
readonly isUncommitted: boolean;
|
||||
previousUri: Uri;
|
||||
uri: Uri;
|
||||
}
|
||||
|
||||
export interface IGitCommitLine {
|
||||
sha: string;
|
||||
previousSha?: string;
|
||||
line: number;
|
||||
originalLine: number;
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export class GitCommit implements IGitCommit {
|
||||
|
||||
lines: IGitCommitLine[];
|
||||
originalFileName?: string;
|
||||
previousSha?: string;
|
||||
previousFileName?: string;
|
||||
workingFileName?: string;
|
||||
private _isUncommitted: boolean | undefined;
|
||||
|
||||
constructor(
|
||||
public repoPath: string,
|
||||
public sha: string,
|
||||
public fileName: string,
|
||||
public author: string,
|
||||
public date: Date,
|
||||
public message: string,
|
||||
lines?: IGitCommitLine[],
|
||||
originalFileName?: string,
|
||||
previousSha?: string,
|
||||
previousFileName?: string
|
||||
) {
|
||||
this.fileName = this.fileName.replace(/, ?$/, '');
|
||||
|
||||
this.lines = lines || [];
|
||||
this.originalFileName = originalFileName;
|
||||
this.previousSha = previousSha;
|
||||
this.previousFileName = previousFileName;
|
||||
}
|
||||
|
||||
get shortSha() {
|
||||
return this.sha.substring(0, 8);
|
||||
}
|
||||
|
||||
get isUncommitted(): boolean {
|
||||
if (this._isUncommitted === undefined) {
|
||||
this._isUncommitted = Git.isUncommitted(this.sha);
|
||||
}
|
||||
return this._isUncommitted;
|
||||
}
|
||||
|
||||
get previousShortSha() {
|
||||
return this.previousSha && this.previousSha.substring(0, 8);
|
||||
}
|
||||
|
||||
get previousUri(): Uri {
|
||||
return this.previousFileName ? Uri.file(path.resolve(this.repoPath, this.previousFileName)) : this.uri;
|
||||
}
|
||||
|
||||
get uri(): Uri {
|
||||
return Uri.file(path.resolve(this.repoPath, this.originalFileName || this.fileName));
|
||||
}
|
||||
|
||||
getFormattedPath(separator: string = ' \u00a0\u2022\u00a0 '): string {
|
||||
const directory = path.dirname(this.fileName);
|
||||
return (!directory || directory === '.')
|
||||
? path.basename(this.fileName)
|
||||
: `${path.basename(this.fileName)}${separator}${directory}`;
|
||||
}
|
||||
}
|
||||
14
src/git/models/log.ts
Normal file
14
src/git/models/log.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
import { Range } from 'vscode';
|
||||
import { IGitAuthor } from './commit';
|
||||
import { GitLogCommit } from './logCommit';
|
||||
|
||||
export interface IGitLog {
|
||||
repoPath: string;
|
||||
authors: Map<string, IGitAuthor>;
|
||||
commits: Map<string, GitLogCommit>;
|
||||
|
||||
maxCount: number | undefined;
|
||||
range: Range;
|
||||
truncated: boolean;
|
||||
}
|
||||
53
src/git/models/logCommit.ts
Normal file
53
src/git/models/logCommit.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
import { Uri } from 'vscode';
|
||||
import { GitCommit, IGitCommitLine } from './commit';
|
||||
import { GitStatusFileStatus } from './status';
|
||||
import * as path from 'path';
|
||||
|
||||
export type GitLogType = 'file' | 'repo';
|
||||
|
||||
export class GitLogCommit extends GitCommit {
|
||||
|
||||
fileNames: string;
|
||||
fileStatuses: { status: GitStatusFileStatus, fileName: string, originalFileName?: string }[];
|
||||
nextSha?: string;
|
||||
nextFileName?: string;
|
||||
status: GitStatusFileStatus;
|
||||
|
||||
constructor(
|
||||
public type: GitLogType,
|
||||
repoPath: string,
|
||||
sha: string,
|
||||
fileName: string,
|
||||
author: string,
|
||||
date: Date,
|
||||
message: string,
|
||||
status?: GitStatusFileStatus,
|
||||
fileStatuses?: { status: GitStatusFileStatus, fileName: string, originalFileName?: string }[],
|
||||
lines?: IGitCommitLine[],
|
||||
originalFileName?: string,
|
||||
previousSha?: string,
|
||||
previousFileName?: string
|
||||
) {
|
||||
super(repoPath, sha, fileName, author, date, message, lines, originalFileName, previousSha, previousFileName);
|
||||
this.status = status;
|
||||
|
||||
this.fileNames = this.fileName;
|
||||
|
||||
if (fileStatuses) {
|
||||
this.fileStatuses = fileStatuses.filter(_ => !!_.fileName);
|
||||
this.fileName = this.fileStatuses[0].fileName;
|
||||
}
|
||||
else {
|
||||
this.fileStatuses = [{ status: status, fileName: fileName }];
|
||||
}
|
||||
}
|
||||
|
||||
get nextShortSha() {
|
||||
return this.nextSha && this.nextSha.substring(0, 8);
|
||||
}
|
||||
|
||||
get nextUri(): Uri {
|
||||
return this.nextFileName ? Uri.file(path.resolve(this.repoPath, this.nextFileName)) : this.uri;
|
||||
}
|
||||
}
|
||||
7
src/git/models/models.ts
Normal file
7
src/git/models/models.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
export * from './blame';
|
||||
export * from './branch';
|
||||
export * from './commit';
|
||||
export * from './log';
|
||||
export * from './logCommit';
|
||||
export * from './status';
|
||||
45
src/git/models/status.ts
Normal file
45
src/git/models/status.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
export interface IGitStatus {
|
||||
|
||||
branch: string;
|
||||
repoPath: string;
|
||||
sha: string;
|
||||
state: {
|
||||
ahead: number;
|
||||
behind: number;
|
||||
};
|
||||
upstream?: string;
|
||||
|
||||
files: GitStatusFile[];
|
||||
}
|
||||
|
||||
export declare type GitStatusFileStatus = '!' | '?' | 'A' | 'C' | 'D' | 'M' | 'R' | 'U';
|
||||
|
||||
export class GitStatusFile {
|
||||
|
||||
originalFileName?: string;
|
||||
|
||||
constructor(public repoPath: string, public status: GitStatusFileStatus, public staged: boolean, public fileName: string, originalFileName?: string) {
|
||||
this.originalFileName = originalFileName;
|
||||
}
|
||||
|
||||
getIcon() {
|
||||
return getGitStatusIcon(this.status);
|
||||
}
|
||||
}
|
||||
|
||||
const statusOcticonsMap = {
|
||||
'!': '$(diff-ignored)',
|
||||
'?': '$(diff-added)',
|
||||
A: '$(diff-added)',
|
||||
C: '$(diff-added)',
|
||||
D: '$(diff-removed)',
|
||||
M: '$(diff-modified)',
|
||||
R: '$(diff-renamed)',
|
||||
U: '$(question)'
|
||||
};
|
||||
|
||||
export function getGitStatusIcon(status: GitStatusFileStatus, missing: string = '\u00a0\u00a0\u00a0\u00a0'): string {
|
||||
return statusOcticonsMap[status] || missing;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
'use strict';
|
||||
import { Git, GitBlameFormat, GitCommit, IGitAuthor, IGitBlame, IGitCommitLine, IGitEnricher } from './../git';
|
||||
import { Git, GitCommit, IGitAuthor, IGitBlame, IGitCommitLine } from './../git';
|
||||
import * as moment from 'moment';
|
||||
import * as path from 'path';
|
||||
|
||||
@@ -28,15 +28,9 @@ interface IBlameEntry {
|
||||
summary?: string;
|
||||
}
|
||||
|
||||
export class GitBlameParserEnricher implements IGitEnricher<IGitBlame> {
|
||||
export class GitBlameParser {
|
||||
|
||||
constructor(public format: GitBlameFormat) {
|
||||
if (format !== GitBlameFormat.incremental) {
|
||||
throw new Error(`Invalid blame format=${format}`);
|
||||
}
|
||||
}
|
||||
|
||||
private _parseEntries(data: string): IBlameEntry[] {
|
||||
private static _parseEntries(data: string): IBlameEntry[] {
|
||||
if (!data) return undefined;
|
||||
|
||||
const lines = data.split('\n');
|
||||
@@ -122,7 +116,7 @@ export class GitBlameParserEnricher implements IGitEnricher<IGitBlame> {
|
||||
return entries;
|
||||
}
|
||||
|
||||
enrich(data: string, fileName: string): IGitBlame {
|
||||
static parse(data: string, fileName: string): IGitBlame {
|
||||
const entries = this._parseEntries(data);
|
||||
if (!entries) return undefined;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
import { Git, GitFileStatus, GitLogCommit, GitLogType, IGitAuthor, IGitEnricher, IGitLog } from './../git';
|
||||
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';
|
||||
@@ -17,16 +18,16 @@ interface ILogEntry {
|
||||
|
||||
fileName?: string;
|
||||
originalFileName?: string;
|
||||
fileStatuses?: { status: GitFileStatus, fileName: string, originalFileName: string }[];
|
||||
fileStatuses?: { status: GitStatusFileStatus, fileName: string, originalFileName: string }[];
|
||||
|
||||
status?: GitFileStatus;
|
||||
status?: GitStatusFileStatus;
|
||||
|
||||
summary?: string;
|
||||
}
|
||||
|
||||
export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
|
||||
export class GitLogParser {
|
||||
|
||||
private _parseEntries(data: string, isRepoPath: boolean): ILogEntry[] {
|
||||
private static _parseEntries(data: string, isRepoPath: boolean): ILogEntry[] {
|
||||
if (!data) return undefined;
|
||||
|
||||
const lines = data.split('\n');
|
||||
@@ -43,7 +44,7 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
|
||||
}
|
||||
|
||||
if (!entry) {
|
||||
if (!Git.ShaRegex.test(lineParts[0])) continue;
|
||||
if (!Git.shaRegex.test(lineParts[0])) continue;
|
||||
|
||||
entry = {
|
||||
sha: lineParts[0]
|
||||
@@ -91,7 +92,7 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
|
||||
while (++position < lines.length) {
|
||||
lineParts = lines[position].split(' ');
|
||||
|
||||
if (Git.ShaRegex.test(lineParts[0])) {
|
||||
if (Git.shaRegex.test(lineParts[0])) {
|
||||
position--;
|
||||
break;
|
||||
}
|
||||
@@ -113,7 +114,7 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
|
||||
}
|
||||
|
||||
const status = {
|
||||
status: lineParts[0][0] as GitFileStatus,
|
||||
status: lineParts[0][0] as GitStatusFileStatus,
|
||||
fileName: lineParts[0].substring(1),
|
||||
originalFileName: undefined as string
|
||||
};
|
||||
@@ -141,11 +142,11 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
|
||||
position += 2;
|
||||
lineParts = lines[position].split(' ');
|
||||
if (lineParts.length === 1) {
|
||||
entry.status = lineParts[0][0] as GitFileStatus;
|
||||
entry.status = lineParts[0][0] as GitStatusFileStatus;
|
||||
entry.fileName = lineParts[0].substring(1);
|
||||
}
|
||||
else {
|
||||
entry.status = lineParts[3][0] as GitFileStatus;
|
||||
entry.status = lineParts[3][0] as GitStatusFileStatus;
|
||||
entry.fileName = lineParts[0].substring(1);
|
||||
position += 4;
|
||||
}
|
||||
@@ -175,7 +176,7 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
|
||||
return entries;
|
||||
}
|
||||
|
||||
enrich(data: string, type: GitLogType, fileNameOrRepoPath: string, maxCount: number | undefined, isRepoPath: boolean, reverse: boolean): IGitLog {
|
||||
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;
|
||||
|
||||
@@ -264,7 +265,8 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
|
||||
// commits: sortedCommits,
|
||||
commits: commits,
|
||||
maxCount: maxCount,
|
||||
truncated: maxCount && entries.length >= maxCount
|
||||
range: range,
|
||||
truncated: !!(maxCount && entries.length >= maxCount)
|
||||
} as IGitLog;
|
||||
}
|
||||
}
|
||||
89
src/git/parsers/statusParser.ts
Normal file
89
src/git/parsers/statusParser.ts
Normal 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
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user