From 3c5920d019e19fd8a6b2dd015ec99201fbaee451 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sun, 4 Feb 2018 23:59:47 -0500 Subject: [PATCH] Marks model props readonly --- src/git/models/blame.ts | 22 +++++------ src/git/models/branch.ts | 22 +++++++---- src/git/models/diff.ts | 10 ++--- src/git/models/log.ts | 16 ++++---- src/git/models/stash.ts | 4 +- src/git/models/status.ts | 22 +++++------ src/git/parsers/statusParser.ts | 87 +++++++++++++++++++++++++---------------- 7 files changed, 104 insertions(+), 79 deletions(-) diff --git a/src/git/models/blame.ts b/src/git/models/blame.ts index f2205f5..4d822ee 100644 --- a/src/git/models/blame.ts +++ b/src/git/models/blame.ts @@ -3,24 +3,24 @@ import { GitAuthor, GitCommitLine } from './commit'; import { GitBlameCommit } from './blameCommit'; export interface GitBlame { - repoPath: string; - authors: Map; - commits: Map; - lines: GitCommitLine[]; + readonly repoPath: string; + readonly authors: Map; + readonly commits: Map; + readonly lines: GitCommitLine[]; } export interface GitBlameLine { - author: GitAuthor; - commit: GitBlameCommit; - line: GitCommitLine; + readonly author: GitAuthor; + readonly commit: GitBlameCommit; + readonly line: GitCommitLine; } export interface GitBlameLines extends GitBlame { - allLines: GitCommitLine[]; + readonly allLines: GitCommitLine[]; } export interface GitBlameCommitLines { - author: GitAuthor; - commit: GitBlameCommit; - lines: GitCommitLine[]; + readonly author: GitAuthor; + readonly commit: GitBlameCommit; + readonly lines: GitCommitLine[]; } \ No newline at end of file diff --git a/src/git/models/branch.ts b/src/git/models/branch.ts index c20bf7c..c948563 100644 --- a/src/git/models/branch.ts +++ b/src/git/models/branch.ts @@ -2,11 +2,11 @@ export class GitBranch { - current: boolean; - name: string; - remote: boolean; - tracking?: string; - state: { + readonly current: boolean; + readonly name: string; + readonly remote: boolean; + readonly tracking?: string; + readonly state: { ahead: number; behind: number; }; @@ -36,15 +36,21 @@ export class GitBranch { }; } + private _name: string | undefined; getName(): string { - return this.remote - ? this.name.substring(this.name.indexOf('/') + 1) - : this.name; + if (this._name === undefined) { + this._name = this.remote + ? this.name.substring(this.name.indexOf('/') + 1) + : this.name; + } + + return this._name; } getRemote(): string | undefined { if (this.remote) return GitBranch.getRemote(this.name); if (this.tracking !== undefined) return GitBranch.getRemote(this.tracking); + return undefined; } diff --git a/src/git/models/diff.ts b/src/git/models/diff.ts index cc76fdc..31ec7b6 100644 --- a/src/git/models/diff.ts +++ b/src/git/models/diff.ts @@ -34,13 +34,13 @@ export class GitDiffChunk { } export interface GitDiff { - chunks: GitDiffChunk[]; + readonly chunks: GitDiffChunk[]; - diff?: string; + readonly diff?: string; } export interface GitDiffShortStat { - files: number; - insertions: number; - deletions: number; + readonly files: number; + readonly insertions: number; + readonly deletions: number; } \ No newline at end of file diff --git a/src/git/models/log.ts b/src/git/models/log.ts index ee1d9ca..c4dfe91 100644 --- a/src/git/models/log.ts +++ b/src/git/models/log.ts @@ -4,15 +4,15 @@ import { GitAuthor } from './commit'; import { GitLogCommit } from './logCommit'; export interface GitLog { - repoPath: string; - authors: Map; - commits: Map; + readonly repoPath: string; + readonly authors: Map; + readonly commits: Map; - sha: string | undefined; - count: number; - maxCount: number | undefined; - range: Range; - truncated: boolean; + readonly sha: string | undefined; + readonly count: number; + readonly maxCount: number | undefined; + readonly range: Range; + readonly truncated: boolean; query: (maxCount: number | undefined) => Promise; } \ No newline at end of file diff --git a/src/git/models/stash.ts b/src/git/models/stash.ts index 42cf645..2648f99 100644 --- a/src/git/models/stash.ts +++ b/src/git/models/stash.ts @@ -2,6 +2,6 @@ import { GitStashCommit } from './stashCommit'; export interface GitStash { - repoPath: string; - commits: Map; + readonly repoPath: string; + readonly commits: Map; } \ No newline at end of file diff --git a/src/git/models/status.ts b/src/git/models/status.ts index 23ebe64..2ad9ade 100644 --- a/src/git/models/status.ts +++ b/src/git/models/status.ts @@ -8,30 +8,30 @@ import * as path from 'path'; export interface GitStatus { - branch: string; - repoPath: string; - sha: string; - state: { + readonly branch: string; + readonly repoPath: string; + readonly sha: string; + readonly state: { ahead: number; behind: number; }; - upstream?: string; + readonly upstream?: string; - files: GitStatusFile[]; + readonly files: GitStatusFile[]; } export declare type GitStatusFileStatus = '!' | '?' | 'A' | 'C' | 'D' | 'M' | 'R' | 'T' | 'U' | 'X' | 'B'; export interface IGitStatusFile { status: GitStatusFileStatus; - fileName: string; - originalFileName?: string; - workTreeStatus: GitStatusFileStatus; - indexStatus: GitStatusFileStatus; + readonly fileName: string; + readonly originalFileName?: string; + readonly workTreeStatus: GitStatusFileStatus; + readonly indexStatus: GitStatusFileStatus; } export interface IGitStatusFileWithCommit extends IGitStatusFile { - commit: GitLogCommit; + readonly commit: GitLogCommit; } export class GitStatusFile implements IGitStatusFile { diff --git a/src/git/parsers/statusParser.ts b/src/git/parsers/statusParser.ts index 9a25e87..db45fbb 100644 --- a/src/git/parsers/statusParser.ts +++ b/src/git/parsers/statusParser.ts @@ -13,43 +13,35 @@ export class GitStatusParser { const lines = data.split('\n').filter(l => !!l); if (lines.length === 0) return undefined; - const status = { - branch: '', - repoPath: Strings.normalizePath(repoPath), - sha: '', - state: { - ahead: 0, - behind: 0 - }, - files: [] - }; + if (porcelainVersion < 2) return this.parseV1(lines, repoPath); - if (porcelainVersion >= 2) { - this.parseV2(lines, repoPath, status); - } - else { - this.parseV1(lines, repoPath, status); - } - - return status; + return this.parseV2(lines, repoPath); } - private static parseV1(lines: string[], repoPath: string, status: GitStatus) { + private static parseV1(lines: string[], repoPath: string): GitStatus { + let branch: string | undefined; + const files = []; + const state = { + ahead: 0, + behind: 0 + }; + let upstream; + let position = -1; while (++position < lines.length) { const line = lines[position]; // Header if (line.startsWith('##')) { const lineParts = line.split(' '); - [status.branch, status.upstream] = lineParts[1].split('...'); + [branch, upstream] = lineParts[1].split('...'); if (lineParts.length > 2) { const upstreamStatus = lineParts.slice(2).join(' '); const aheadStatus = aheadStatusV1Regex.exec(upstreamStatus); - status.state.ahead = aheadStatus == null ? 0 : +aheadStatus[1] || 0; + state.ahead = aheadStatus == null ? 0 : +aheadStatus[1] || 0; const behindStatus = behindStatusV1Regex.exec(upstreamStatus); - status.state.behind = behindStatus == null ? 0 : +behindStatus[1] || 0; + state.behind = behindStatus == null ? 0 : +behindStatus[1] || 0; } } else { @@ -57,16 +49,34 @@ export class GitStatusParser { const fileName = line.substring(3); if (rawStatus[0] === 'R') { const [file1, file2] = fileName.replace(/\"/g, '').split('->'); - status.files.push(this.parseStatusFile(repoPath, rawStatus, file2.trim(), file1.trim())); + files.push(this.parseStatusFile(repoPath, rawStatus, file2.trim(), file1.trim())); } else { - status.files.push(this.parseStatusFile(repoPath, rawStatus, fileName)); + files.push(this.parseStatusFile(repoPath, rawStatus, fileName)); } } } + + return { + branch: branch || '', + repoPath: Strings.normalizePath(repoPath), + sha: '', + state: state, + files: files, + upstream: upstream + }; } - private static parseV2(lines: string[], repoPath: string, status: GitStatus) { + private static parseV2(lines: string[], repoPath: string): GitStatus { + let branch: string | undefined; + const files = []; + let sha: string | undefined; + const state = { + ahead: 0, + behind: 0 + }; + let upstream; + let position = -1; while (++position < lines.length) { const line = lines[position]; @@ -75,17 +85,17 @@ export class GitStatusParser { const lineParts = line.split(' '); switch (lineParts[1]) { case 'branch.oid': - status.sha = lineParts[2]; + sha = lineParts[2]; break; case 'branch.head': - status.branch = lineParts[2]; + branch = lineParts[2]; break; case 'branch.upstream': - status.upstream = lineParts[2]; + upstream = lineParts[2]; break; case 'branch.ab': - status.state.ahead = +lineParts[2].substring(1); - status.state.behind = +lineParts[3].substring(1); + state.ahead = +lineParts[2].substring(1); + state.behind = +lineParts[3].substring(1); break; } } @@ -93,21 +103,30 @@ export class GitStatusParser { const lineParts = line.split(' '); switch (lineParts[0][0]) { case '1': // normal - status.files.push(this.parseStatusFile(repoPath, lineParts[1], lineParts.slice(8).join(' '))); + files.push(this.parseStatusFile(repoPath, lineParts[1], lineParts.slice(8).join(' '))); break; case '2': // rename const file = lineParts.slice(9).join(' ').split('\t'); - status.files.push(this.parseStatusFile(repoPath, lineParts[1], file[0], file[1])); + files.push(this.parseStatusFile(repoPath, lineParts[1], file[0], file[1])); break; case 'u': // unmerged - status.files.push(this.parseStatusFile(repoPath, lineParts[1], lineParts.slice(10).join(' '))); + files.push(this.parseStatusFile(repoPath, lineParts[1], lineParts.slice(10).join(' '))); break; case '?': // untracked - status.files.push(this.parseStatusFile(repoPath, ' ?', lineParts.slice(1).join(' '))); + files.push(this.parseStatusFile(repoPath, ' ?', lineParts.slice(1).join(' '))); break; } } } + + return { + branch: branch || '', + repoPath: Strings.normalizePath(repoPath), + sha: sha || '', + state: state, + files: files, + upstream: upstream + }; } static parseStatusFile(repoPath: string, rawStatus: string, fileName: string, originalFileName?: string): GitStatusFile {