Skip to content

Commit

Permalink
Changes branch/tag/commit to implement GitRef
Browse files Browse the repository at this point in the history
Adds isOfRefType to branch/tag/commit/ref
  • Loading branch information
eamodio committed Sep 3, 2019
1 parent 4584619 commit 7308996
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/commands/diffWithRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
}

commandArgs = { ...args };
const icon = GitTag.is(args.reference) ? '$(tag) ' : GitBranch.is(args.reference) ? '$(git-branch) ' : '';
const icon = GitTag.isOfRefType(args.reference)
? '$(tag) '
: GitBranch.isOfRefType(args.reference)
? '$(git-branch) '
: '';
const currentCommand = new CommandQuickPickItem(
{
label: `go back ${GlyphChars.ArrowBack}`,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/openFileRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ export class OpenFileRevisionCommand extends ActiveEditorCommand {
}

commandArgs = { ...args };
const icon = GitTag.is(args.reference)
const icon = GitTag.isOfRefType(args.reference)
? '$(tag) '
: GitBranch.is(args.reference)
: GitBranch.isOfRefType(args.reference)
? '$(git-branch) '
: '';
const currentCommand = new CommandQuickPickItem(
Expand Down
6 changes: 5 additions & 1 deletion src/commands/showQuickFileHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand {
}
}

const icon = GitTag.is(args.reference) ? '$(tag) ' : GitBranch.is(args.reference) ? '$(git-branch) ' : '';
const icon = GitTag.isOfRefType(args.reference)
? '$(tag) '
: GitBranch.isOfRefType(args.reference)
? '$(git-branch) '
: '';
// Create a command to get back to where we are right now
const currentCommand = new CommandQuickPickItem(
{
Expand Down
8 changes: 7 additions & 1 deletion src/git/models/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Container } from '../../container';
import { Git, GitRemote } from '../git';
import { GitStatus } from './status';
import { memoize } from '../../system';
import { GitReference } from './models';

const whitespaceRegex = /\s/;

Expand All @@ -12,11 +13,15 @@ export interface GitTrackingState {
behind: number;
}

export class GitBranch {
export class GitBranch implements GitReference {
static is(branch: any): branch is GitBranch {
return branch instanceof GitBranch;
}

static isOfRefType(branch: GitReference | undefined) {
return branch !== undefined && branch.refType === 'branch';
}

static sort(branches: GitBranch[]) {
return branches.sort(
(a, b) =>
Expand All @@ -27,6 +32,7 @@ export class GitBranch {
);
}

readonly refType = 'branch';
readonly detached: boolean;
readonly id: string;
readonly tracking?: string;
Expand Down
28 changes: 17 additions & 11 deletions src/git/models/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CommitFormatter } from '../formatters/formatters';
import { Git } from '../git';
import { GitUri } from '../gitUri';
import { getAvatarUri } from '../../avatars';
import { GitReference } from './models';

export interface GitAuthor {
name: string;
Expand Down Expand Up @@ -41,20 +42,17 @@ export const CommitFormatting = {
}
};

export abstract class GitCommit {
export abstract class GitCommit implements GitReference {
static is(commit: any): commit is GitCommit {
return (
commit instanceof GitCommit
// || (commit.repoPath !== undefined &&
// commit.sha !== undefined &&
// (commit.type === GitCommitType.Blame ||
// commit.type === GitCommitType.Log ||
// commit.type === GitCommitType.LogFile ||
// commit.type === GitCommitType.Stash ||
// commit.type === GitCommitType.StashFile))
);
return commit instanceof GitCommit;
}

static isOfRefType(branch: GitReference | undefined) {
return branch !== undefined && branch.refType === 'revision';
}

readonly refType = 'revision';

constructor(
public readonly type: GitCommitType,
public readonly repoPath: string,
Expand All @@ -72,6 +70,14 @@ export abstract class GitCommit {
this._fileName = fileName || '';
}

get ref() {
return this.sha;
}

get name() {
return this.shortSha;
}

private readonly _fileName: string;
get fileName() {
// If we aren't a single-file commit, return an empty file name (makes it default to the repoPath)
Expand Down
16 changes: 16 additions & 0 deletions src/git/models/models.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
'use strict';

import { Git } from '../git';

export interface GitReference {
readonly refType: 'branch' | 'tag' | 'revision';
name: string;
ref: string;
}

export namespace GitReference {
export function create(
ref: string,
{ name, refType }: { name?: string; refType?: 'branch' | 'tag' } = {}
): GitReference {
return { name: name || Git.shortenSha(ref, { force: true }), ref: ref, refType: refType || 'revision' };
}

export function isOfRefType(ref: GitReference | undefined, refType: 'branch' | 'tag' | 'revision' = 'revision') {
return ref !== undefined && ref.refType === refType;
}
}

export * from './blame';
export * from './blameCommit';
export * from './branch';
Expand Down
9 changes: 8 additions & 1 deletion src/git/models/tag.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
'use strict';
import { memoize } from '../../system';
import { GitReference } from './models';

export class GitTag {
export class GitTag implements GitReference {
static is(tag: any): tag is GitTag {
return tag instanceof GitTag;
}

static isOfRefType(tag: GitReference | undefined) {
return tag !== undefined && tag.refType === 'tag';
}

static sort(tags: GitTag[]) {
return tags.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' }));
}

readonly refType = 'tag';

constructor(
public readonly repoPath: string,
public readonly name: string,
Expand Down

0 comments on commit 7308996

Please sign in to comment.