mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-18 09:45:36 -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:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user