Adds 'Show Stashed Changes` command

Adds experimental 'Apply Stashed Changes' command
Adds experimental 'Delete Stashed Changes' to stashed changes quick pick
This commit is contained in:
Eric Amodio
2017-03-28 18:43:33 -04:00
parent 19fe22f061
commit 9945ee6d94
21 changed files with 566 additions and 137 deletions

View File

@@ -9,6 +9,7 @@ import * as tmp from 'tmp';
export * from './models/models';
export * from './parsers/blameParser';
export * from './parsers/logParser';
export * from './parsers/stashParser';
export * from './parsers/statusParser';
export * from './remotes/provider';
@@ -16,6 +17,7 @@ let git: IGit;
// `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nparents %P%nsummary %B%nfilename ?`
const defaultLogParams = [`log`, `--name-status`, `--full-history`, `-M`, `--date=iso8601`, `--format=%H -%nauthor %an%nauthor-date %ai%nparents %P%nsummary %B%nfilename ?`];
const defaultStashParams = [`stash`, `list`, `--name-status`, `--full-history`, `-M`, `--format=%H -%nauthor-date %ai%nreflog-selector %gd%nsummary %B%nfilename ?`];
async function gitCommand(cwd: string, ...args: any[]) {
try {
@@ -163,14 +165,6 @@ export class Git {
return gitCommand(repoPath, ...params);
}
static show(repoPath: string, fileName: string, branchOrSha: string) {
const [file, root] = Git.splitPath(fileName, repoPath);
branchOrSha = branchOrSha.replace('^', '');
if (Git.isUncommitted(branchOrSha)) return Promise.reject(new Error(`sha=${branchOrSha} is uncommitted`));
return gitCommand(root, 'show', `${branchOrSha}:./${file}`);
}
static log(repoPath: string, sha?: string, maxCount?: number, reverse: boolean = false) {
const params = [...defaultLogParams, `-m`];
if (maxCount && !reverse) {
@@ -227,26 +221,44 @@ export class Git {
}
static remote(repoPath: string): Promise<string> {
const params = ['remote', '-v'];
return gitCommand(repoPath, ...params);
return gitCommand(repoPath, 'remote', '-v');
}
static remote_url(repoPath: string, remote: string): Promise<string> {
const params = ['remote', 'get-url', remote];
return gitCommand(repoPath, ...params);
return gitCommand(repoPath, 'remote', 'get-url', remote);
}
static show(repoPath: string, fileName: string, branchOrSha: string) {
const [file, root] = Git.splitPath(fileName, repoPath);
branchOrSha = branchOrSha.replace('^', '');
if (Git.isUncommitted(branchOrSha)) return Promise.reject(new Error(`sha=${branchOrSha} is uncommitted`));
return gitCommand(root, 'show', `${branchOrSha}:./${file}`);
}
static stash_apply(repoPath: string, stashName: string, deleteAfter: boolean) {
if (!stashName) return undefined;
return gitCommand(repoPath, 'stash', deleteAfter ? 'pop' : 'apply', stashName);
}
static stash_delete(repoPath: string, stashName: string) {
if (!stashName) return undefined;
return gitCommand(repoPath, 'stash', 'drop', stashName);
}
static stash_list(repoPath: string) {
return gitCommand(repoPath, ...defaultStashParams);
}
static status(repoPath: string, porcelainVersion: number = 1): Promise<string> {
const porcelain = porcelainVersion >= 2 ? `--porcelain=v${porcelainVersion}` : '--porcelain';
const params = ['status', porcelain, '--branch'];
return gitCommand(repoPath, ...params);
return gitCommand(repoPath, 'status', porcelain, '--branch');
}
static status_file(repoPath: string, fileName: string, porcelainVersion: number = 1): Promise<string> {
const [file, root] = Git.splitPath(fileName, repoPath);
const porcelain = porcelainVersion >= 2 ? `--porcelain=v${porcelainVersion}` : '--porcelain';
const params = ['status', porcelain, file];
return gitCommand(root, ...params);
return gitCommand(root, 'status', porcelain, file);
}
}

View File

@@ -34,7 +34,7 @@ export interface IGitCommitLine {
code?: string;
}
export type GitCommitType = 'blame' | 'file' | 'repo';
export type GitCommitType = 'blame' | 'file' | 'repo' | 'stash';
export class GitCommit implements IGitCommit {

View File

@@ -5,4 +5,6 @@ export * from './commit';
export * from './log';
export * from './logCommit';
export * from './remote';
export * from './stash';
export * from './stashCommit';
export * from './status';

7
src/git/models/stash.ts Normal file
View File

@@ -0,0 +1,7 @@
'use strict';
import { GitStashCommit } from './stashCommit';
export interface IGitStash {
repoPath: string;
commits: Map<string, GitStashCommit>;
}

View File

@@ -0,0 +1,24 @@
'use strict';
import { IGitCommitLine } from './commit';
import { GitLogCommit } from './logCommit';
import { IGitStatusFile, GitStatusFileStatus } from './status';
export class GitStashCommit extends GitLogCommit {
constructor(
public stashName: string,
repoPath: string,
sha: string,
fileName: string,
date: Date,
message: string,
status?: GitStatusFileStatus,
fileStatuses?: IGitStatusFile[],
lines?: IGitCommitLine[],
originalFileName?: string,
previousSha?: string,
previousFileName?: string
) {
super('stash', repoPath, sha, fileName, undefined, date, message, status, fileStatuses, lines, originalFileName, previousSha, previousFileName);
}
}

View File

@@ -0,0 +1,147 @@
'use strict';
import { Git, GitStashCommit, GitStatusFileStatus, IGitStash, IGitStatusFile } from './../git';
// import { Logger } from '../../logger';
import * as moment from 'moment';
interface IStashEntry {
sha: string;
date?: string;
fileNames?: string;
fileStatuses?: IGitStatusFile[];
summary?: string;
stashName?: string;
}
export class GitStashParser {
private static _parseEntries(data: string): IStashEntry[] {
if (!data) return undefined;
const lines = data.split('\n');
if (!lines.length) return undefined;
const entries: IStashEntry[] = [];
let entry: IStashEntry;
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-date':
entry.date = `${lineParts[1]}T${lineParts[2]}${lineParts[3]}`;
break;
case 'summary':
entry.summary = lineParts.slice(1).join(' ').trim();
while (++position < lines.length) {
const next = lines[position];
if (!next) break;
if (next === 'filename ?') {
position--;
break;
}
entry.summary += `\n${lines[position]}`;
}
break;
case 'reflog-selector':
entry.stashName = lineParts.slice(1).join(' ').trim();
break;
case 'filename':
const nextLine = lines[position + 1];
// If the next line isn't blank, make sure it isn't starting a new commit
if (nextLine && Git.shaRegex.test(nextLine)) continue;
position++;
while (++position < lines.length) {
const line = lines[position];
lineParts = line.split(' ');
if (Git.shaRegex.test(lineParts[0])) {
position--;
break;
}
if (entry.fileStatuses == null) {
entry.fileStatuses = [];
}
const status = {
status: line[0] as GitStatusFileStatus,
fileName: line.substring(1),
originalFileName: undefined as string
} as IGitStatusFile;
this._parseFileName(status);
entry.fileStatuses.push(status);
}
if (entry.fileStatuses) {
entry.fileNames = entry.fileStatuses.filter(_ => !!_.fileName).map(_ => _.fileName).join(', ');
}
entries.push(entry);
entry = undefined;
break;
default:
break;
}
}
return entries;
}
static parse(data: string, repoPath: string): IGitStash {
const entries = this._parseEntries(data);
if (!entries) return undefined;
const commits: Map<string, GitStashCommit> = new Map();
for (let i = 0, len = entries.length; i < len; i++) {
const entry = entries[i];
let commit = commits.get(entry.sha);
if (!commit) {
commit = new GitStashCommit(entry.stashName, repoPath, entry.sha, entry.fileNames, moment(entry.date).toDate(), entry.summary, undefined, entry.fileStatuses);
commits.set(entry.sha, commit);
}
}
return {
repoPath: repoPath,
commits: commits
} as IGitStash;
}
private static _parseFileName(entry: { fileName?: string, originalFileName?: string }) {
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);
}
}
}
}