Skip to content

Commit

Permalink
Type & code refactoring and improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutchie committed Nov 18, 2019
1 parent 3aa31b0 commit f2627d6
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 105 deletions.
13 changes: 12 additions & 1 deletion src/avatarManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DataSource } from './dataSource';
import { ExtensionState } from './extensionState';
import { GitGraphView } from './gitGraphView';
import { Logger, maskEmail } from './logger';
import { AvatarCache } from './types';

export class AvatarManager {
private readonly dataSource: DataSource;
Expand Down Expand Up @@ -383,6 +382,14 @@ class AvatarRequestQueue {
}
}

export interface Avatar {
image: string;
timestamp: number;
identicon: boolean;
}

export type AvatarCache = { [email: string]: Avatar };

interface AvatarRequestItem {
email: string;
repo: string;
Expand All @@ -391,15 +398,19 @@ interface AvatarRequestItem {
checkAfter: number;
attempts: number;
}

interface GitHubRemoteSource {
type: 'github';
owner: string;
repo: string;
}

interface GitLabRemoteSource {
type: 'gitlab';
}

interface GravatarRemoteSource {
type: 'gravatar';
}

type RemoteSource = GitHubRemoteSource | GitLabRemoteSource | GravatarRemoteSource;
5 changes: 3 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DefaultColumnVisibility,
DialogDefaults,
FileViewType,
GitResetMode,
GraphStyle,
RefLabelAlignment,
TabIconColourTheme
Expand Down Expand Up @@ -145,10 +146,10 @@ class Config {
interactive: !!this.config.get('dialog.rebase.launchInteractiveRebase', false)
},
resetCommit: {
mode: resetCommitMode === 'Soft' ? 'soft' : (resetCommitMode === 'Hard' ? 'hard' : 'mixed')
mode: resetCommitMode === 'Soft' ? GitResetMode.Soft : (resetCommitMode === 'Hard' ? GitResetMode.Hard : GitResetMode.Mixed)
},
resetUncommitted: {
mode: resetUncommittedMode === 'Hard' ? 'hard' : 'mixed'
mode: resetUncommittedMode === 'Hard' ? GitResetMode.Hard : GitResetMode.Mixed
},
stashUncommittedChanges: {
includeUntracked: !!this.config.get('dialog.stashUncommittedChanges.includeUntracked', true)
Expand Down
50 changes: 29 additions & 21 deletions src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Uri } from 'vscode';
import { AskpassEnvironment, AskpassManager } from './askpass/askpassManager';
import { getConfig } from './config';
import { Logger } from './logger';
import { BranchOrCommit, CommitOrdering, DateType, ErrorInfo, GitCommitDetails, GitCommitNode, GitCommitStash, GitFileChange, GitFileStatus, GitRepoSettings, GitResetMode } from './types';
import { ActionOn, CommitOrdering, DateType, ErrorInfo, GitCommitDetails, GitCommitNode, GitCommitStash, GitFileChange, GitFileStatus, GitRepoSettings, GitResetMode } from './types';
import { abbrevCommit, compareVersions, constructIncompatibleGitVersionMessage, getPathFromStr, getPathFromUri, GitExecutable, realpath, runGitCommandInNewTerminal, UNABLE_TO_FIND_GIT_MSG, UNCOMMITTED } from './utils';


Expand Down Expand Up @@ -184,20 +184,20 @@ export class DataSource {

/* Get Data Methods - Commit Details View */

public getCommitDetails(repo: string, commitHash: string): Promise<GitCommitDetails> {
public getCommitDetails(repo: string, commitHash: string): Promise<GitCommitDetailsData> {
return Promise.all([
this.getCommitDetailsBase(repo, commitHash),
this.getDiffNameStatus(repo, commitHash, commitHash),
this.getDiffNumStat(repo, commitHash, commitHash)
]).then((results) => {
results[0].fileChanges = generateFileChanges(results[1], results[2], null);
return results[0];
return { commitDetails: results[0], error: null };
}).catch((errorMessage) => {
return { hash: '', parents: [], author: '', email: '', date: 0, committer: '', body: '', fileChanges: [], error: errorMessage };
return { commitDetails: null, error: errorMessage };
});
}

public getStashDetails(repo: string, commitHash: string, stash: GitCommitStash): Promise<GitCommitDetails> {
public getStashDetails(repo: string, commitHash: string, stash: GitCommitStash): Promise<GitCommitDetailsData> {
return Promise.all([
this.getCommitDetailsBase(repo, commitHash),
this.getDiffNameStatus(repo, stash.baseHash, commitHash),
Expand All @@ -214,24 +214,27 @@ export class DataSource {
}
});
}
return results[0];
return { commitDetails: results[0], error: null };
}).catch((errorMessage) => {
return { hash: '', parents: [], author: '', email: '', date: 0, committer: '', body: '', fileChanges: [], error: errorMessage };
return { commitDetails: null, error: errorMessage };
});
}

public getUncommittedDetails(repo: string): Promise<GitCommitDetails> {
let details: GitCommitDetails = { hash: UNCOMMITTED, parents: [], author: '', email: '', date: 0, committer: '', body: '', fileChanges: [], error: null };
public getUncommittedDetails(repo: string): Promise<GitCommitDetailsData> {
return Promise.all([
this.getDiffNameStatus(repo, 'HEAD', ''),
this.getDiffNumStat(repo, 'HEAD', ''),
this.getStatus(repo)
]).then((results) => {
details.fileChanges = generateFileChanges(results[0], results[1], results[2]);
return details;
return {
commitDetails: {
hash: UNCOMMITTED, parents: [], author: '', email: '', date: 0, committer: '', body: '',
fileChanges: generateFileChanges(results[0], results[1], results[2])
},
error: null
};
}).catch((errorMessage) => {
details.error = errorMessage;
return details;
return { commitDetails: null, error: errorMessage };
});
}

Expand Down Expand Up @@ -518,28 +521,28 @@ export class DataSource {

/* Git Action Methods - Branches & Commits */

public async merge(repo: string, obj: string, type: BranchOrCommit, createNewCommit: boolean, squash: boolean) {
public async merge(repo: string, obj: string, actionOn: ActionOn, createNewCommit: boolean, squash: boolean) {
let args = ['merge', obj];
if (squash) args.push('--squash');
else if (createNewCommit) args.push('--no-ff');

let mergeStatus = await this.runGitCommand(args, repo);
if (mergeStatus === null && squash) {
if (await this.areStagedChanges(repo)) {
return this.runGitCommand(['commit', '-m', 'Merge ' + type.toLowerCase() + ' \'' + obj + '\''], repo);
return this.runGitCommand(['commit', '-m', 'Merge ' + actionOn.toLowerCase() + ' \'' + obj + '\''], repo);
}
}
return mergeStatus;
}

public rebase(repo: string, obj: string, type: BranchOrCommit, ignoreDate: boolean, interactive: boolean) {
public rebase(repo: string, obj: string, actionOn: ActionOn, ignoreDate: boolean, interactive: boolean) {
if (interactive) {
return new Promise<ErrorInfo>(resolve => {
if (this.gitExecutable === null) return resolve(UNABLE_TO_FIND_GIT_MSG);

runGitCommandInNewTerminal(repo, this.gitExecutable.path,
'rebase --interactive ' + (type === 'Branch' ? obj.replace(/'/g, '"\'"') : obj),
'Git Rebase on "' + (type === 'Branch' ? obj : abbrevCommit(obj)) + '"');
'rebase --interactive ' + (actionOn === ActionOn.Branch ? obj.replace(/'/g, '"\'"') : obj),
'Git Rebase on "' + (actionOn === ActionOn.Branch ? obj : abbrevCommit(obj)) + '"');
setTimeout(() => resolve(null), 1000);
});
} else {
Expand Down Expand Up @@ -568,8 +571,8 @@ export class DataSource {
return this.runGitCommand(['rebase', '--onto', commitHash + '^', commitHash], repo);
}

public resetToCommit(repo: string, commitHash: string, resetMode: GitResetMode) {
return this.runGitCommand(['reset', '--' + resetMode, commitHash], repo);
public resetToCommit(repo: string, commit: string, resetMode: GitResetMode) {
return this.runGitCommand(['reset', '--' + resetMode, commit], repo);
}

public revertCommit(repo: string, commitHash: string, parentIndex: number) {
Expand Down Expand Up @@ -668,7 +671,7 @@ export class DataSource {
date: parseInt(commitInfo[4]),
committer: commitInfo[5],
body: lines.slice(1, lastLine + 1).join('\n'),
fileChanges: [], error: null
fileChanges: []
};
});
}
Expand Down Expand Up @@ -1031,6 +1034,11 @@ interface GitCommitData {
error: ErrorInfo;
}

export interface GitCommitDetailsData {
commitDetails: GitCommitDetails | null;
error: ErrorInfo;
}

interface GitCommitComparisonData {
fileChanges: GitFileChange[];
error: ErrorInfo;
Expand Down
3 changes: 2 additions & 1 deletion src/extensionState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import { ExtensionContext, Memento } from 'vscode';
import { Avatar, AvatarCache, CodeReview, ErrorInfo, FileViewType, GitRepoSet, GitRepoState } from './types';
import { Avatar, AvatarCache } from './avatarManager';
import { CodeReview, ErrorInfo, FileViewType, GitRepoSet, GitRepoState } from './types';
import { getPathFromStr } from './utils';

const AVATAR_STORAGE_FOLDER = '/avatars';
Expand Down
18 changes: 9 additions & 9 deletions src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as path from 'path';
import * as vscode from 'vscode';
import { AvatarManager } from './avatarManager';
import { getConfig } from './config';
import { DataSource } from './dataSource';
import { DataSource, GitCommitDetailsData } from './dataSource';
import { ExtensionState } from './extensionState';
import { Logger } from './logger';
import { RepoFileWatcher } from './repoFileWatcher';
import { RepoManager } from './repoManager';
import { ErrorInfo, GitCommitDetails, GitGraphViewInitialState, GitRepoSet, RefLabelAlignment, RequestMessage, ResponseMessage, TabIconColourTheme } from './types';
import { ErrorInfo, GitGraphViewInitialState, GitRepoSet, RefLabelAlignment, RequestMessage, ResponseMessage, TabIconColourTheme } from './types';
import { copyFilePathToClipboard, copyToClipboard, getNonce, openFile, UNABLE_TO_FIND_GIT_MSG, UNCOMMITTED, viewDiff, viewScm } from './utils';

export class GitGraphView {
Expand Down Expand Up @@ -156,7 +156,7 @@ export class GitGraphView {
this.extensionState.updateCodeReviewFileReviewed(msg.repo, msg.id, msg.filePath);
break;
case 'commitDetails':
let data = await Promise.all<GitCommitDetails, string | null>([
let data = await Promise.all<GitCommitDetailsData, string | null>([
msg.commitHash === UNCOMMITTED
? this.dataSource.getUncommittedDetails(msg.repo)
: msg.stash === null
Expand All @@ -166,7 +166,7 @@ export class GitGraphView {
]);
this.sendMessage({
command: 'commitDetails',
commitDetails: data[0],
...data[0],
avatar: data[1],
codeReview: msg.commitHash !== UNCOMMITTED ? this.extensionState.getCodeReview(msg.repo, msg.commitHash) : null,
refresh: msg.refresh
Expand Down Expand Up @@ -301,8 +301,8 @@ export class GitGraphView {
break;
case 'merge':
this.sendMessage({
command: 'merge', type: msg.type,
error: await this.dataSource.merge(msg.repo, msg.obj, msg.type, msg.createNewCommit, msg.squash)
command: 'merge', actionOn: msg.actionOn,
error: await this.dataSource.merge(msg.repo, msg.obj, msg.actionOn, msg.createNewCommit, msg.squash)
});
break;
case 'openFile':
Expand Down Expand Up @@ -349,8 +349,8 @@ export class GitGraphView {
break;
case 'rebase':
this.sendMessage({
command: 'rebase', type: msg.type, interactive: msg.interactive,
error: await this.dataSource.rebase(msg.repo, msg.obj, msg.type, msg.ignoreDate, msg.interactive)
command: 'rebase', actionOn: msg.actionOn, interactive: msg.interactive,
error: await this.dataSource.rebase(msg.repo, msg.obj, msg.actionOn, msg.ignoreDate, msg.interactive)
});
break;
case 'renameBranch':
Expand All @@ -367,7 +367,7 @@ export class GitGraphView {
case 'resetToCommit':
this.sendMessage({
command: 'resetToCommit',
error: await this.dataSource.resetToCommit(msg.repo, msg.commitHash, msg.resetMode)
error: await this.dataSource.resetToCommit(msg.repo, msg.commit, msg.resetMode)
});
break;
case 'revertCommit':
Expand Down
Loading

0 comments on commit f2627d6

Please sign in to comment.