From 0be0a56577a5088cb32c1f6f1079643b82c616da Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 14 Nov 2017 09:33:32 -0500 Subject: [PATCH] Fixes #201 checks tracking vs has remotes --- CHANGELOG.md | 4 ++++ package.json | 18 +++++++++--------- src/annotations/annotations.ts | 12 ++++++------ src/annotations/blameAnnotationProvider.ts | 2 +- src/annotations/recentChangesAnnotationProvider.ts | 2 +- src/constants.ts | 2 +- src/currentLineController.ts | 2 +- src/git/gitContextTracker.ts | 8 +++----- src/git/models/repository.ts | 21 ++++++++++++++++----- src/gitService.ts | 9 +++++++++ 10 files changed, 51 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82517c1..a74bd11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] +### Fixed +- Fixes [#201](https://github.com/eamodio/vscode-gitlens/issues/201) - "Open in Remote" commands should check for branch upstream tracking + ## [6.1.0] - 2017-11-13 ### Added - Adds support for nested repositories and submodules -- closes [#198](https://github.com/eamodio/vscode-gitlens/issues/198) diff --git a/package.json b/package.json index a204e40..8b3a5da 100644 --- a/package.json +++ b/package.json @@ -1496,23 +1496,23 @@ }, { "command": "gitlens.openBranchesInRemote", - "when": "gitlens:activeHasRemotes" + "when": "gitlens:activeHasRemote" }, { "command": "gitlens.openBranchInRemote", - "when": "gitlens:activeHasRemotes" + "when": "gitlens:activeHasRemote" }, { "command": "gitlens.openCommitInRemote", - "when": "gitlens:activeIsBlameable && gitlens:activeHasRemotes" + "when": "gitlens:activeIsBlameable && gitlens:activeHasRemote" }, { "command": "gitlens.openFileInRemote", - "when": "gitlens:activeIsTracked && gitlens:activeHasRemotes" + "when": "gitlens:activeIsTracked && gitlens:activeHasRemote" }, { "command": "gitlens.openRepoInRemote", - "when": "gitlens:activeHasRemotes" + "when": "gitlens:activeHasRemote" }, { "command": "gitlens.stashApply", @@ -1638,7 +1638,7 @@ "editor/context": [ { "command": "gitlens.openFileInRemote", - "when": "editorTextFocus && gitlens:activeHasRemotes && config.gitlens.advanced.menus.editorContext.remote", + "when": "editorTextFocus && gitlens:activeHasRemote && config.gitlens.advanced.menus.editorContext.remote", "group": "navigation@100" }, { @@ -1706,12 +1706,12 @@ }, { "command": "gitlens.openFileInRemote", - "when": "gitlens:enabled && gitlens:activeHasRemotes && config.gitlens.advanced.menus.editorTitle.remote", + "when": "gitlens:enabled && gitlens:activeHasRemote && config.gitlens.advanced.menus.editorTitle.remote", "group": "1_gitlens" }, { "command": "gitlens.openRepoInRemote", - "when": "gitlens:enabled && gitlens:activeHasRemotes && config.gitlens.advanced.menus.editorTitle.remote", + "when": "gitlens:enabled && gitlens:activeHasRemote && config.gitlens.advanced.menus.editorTitle.remote", "group": "1_gitlens" }, { @@ -1743,7 +1743,7 @@ "editor/title/context": [ { "command": "gitlens.openFileInRemote", - "when": "gitlens:enabled && gitlens:hasRemotes && config.gitlens.advanced.menus.editorTitleContext.remote", + "when": "gitlens:enabled && gitlens:activeHasRemote && config.gitlens.advanced.menus.editorTitleContext.remote", "group": "1_gitlens" }, { diff --git a/src/annotations/annotations.ts b/src/annotations/annotations.ts index 61e8dba..d92148d 100644 --- a/src/annotations/annotations.ts +++ b/src/annotations/annotations.ts @@ -48,7 +48,7 @@ export class Annotations { return '#793738'; } - private static getHoverCommandBar(commit: GitCommit, hasRemotes: boolean, annotationType?: FileAnnotationType) { + private static getHoverCommandBar(commit: GitCommit, hasRemote: boolean, annotationType?: FileAnnotationType) { let commandBar = `[\`${GlyphChars.DoubleArrowLeft}\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes") `; if (commit.previousSha !== undefined) { @@ -62,7 +62,7 @@ export class Annotations { commandBar += `[\`${GlyphChars.SquareWithTopShadow}\`](${OpenFileRevisionCommand.getMarkdownCommandArgs(uri, annotationType || FileAnnotationType.Gutter, line)} "Blame Previous Revision") `; } - if (hasRemotes) { + if (hasRemote) { commandBar += `[\`${GlyphChars.ArrowUpRight}\`](${OpenCommitInRemoteCommand.getMarkdownCommandArgs(commit.sha)} "Open in Remote") `; } @@ -71,7 +71,7 @@ export class Annotations { return commandBar; } - static getHoverMessage(commit: GitCommit, dateFormat: string | null, hasRemotes: boolean, annotationType?: FileAnnotationType): MarkdownString { + static getHoverMessage(commit: GitCommit, dateFormat: string | null, hasRemote: boolean, annotationType?: FileAnnotationType): MarkdownString { if (dateFormat === null) { dateFormat = 'MMMM Do, YYYY h:MMa'; } @@ -80,7 +80,7 @@ export class Annotations { let commandBar = ''; let showCommitDetailsCommand = ''; if (!commit.isUncommitted) { - commandBar = `\n\n${this.getHoverCommandBar(commit, hasRemotes, annotationType)}`; + commandBar = `\n\n${this.getHoverCommandBar(commit, hasRemote, annotationType)}`; showCommitDetailsCommand = `[\`${commit.shortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(commit.sha)} "Show Commit Details")`; message = commit.message @@ -129,8 +129,8 @@ export class Annotations { } as DecorationOptions; } - static detailsHover(commit: GitCommit, dateFormat: string | null, hasRemotes: boolean, annotationType?: FileAnnotationType): DecorationOptions { - const message = this.getHoverMessage(commit, dateFormat, hasRemotes, annotationType); + static detailsHover(commit: GitCommit, dateFormat: string | null, hasRemote: boolean, annotationType?: FileAnnotationType): DecorationOptions { + const message = this.getHoverMessage(commit, dateFormat, hasRemote, annotationType); return { hoverMessage: message } as DecorationOptions; diff --git a/src/annotations/blameAnnotationProvider.ts b/src/annotations/blameAnnotationProvider.ts index 1efcaf8..e54748f 100644 --- a/src/annotations/blameAnnotationProvider.ts +++ b/src/annotations/blameAnnotationProvider.ts @@ -103,7 +103,7 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase } } - const message = Annotations.getHoverMessage(logCommit || commit, this._config.defaultDateFormat, await this.git.hasRemotes(commit.repoPath), this._config.blame.file.annotationType); + const message = Annotations.getHoverMessage(logCommit || commit, this._config.defaultDateFormat, await this.git.hasRemote(commit.repoPath), this._config.blame.file.annotationType); return new Hover(message, document.validateRange(new Range(position.line, 0, position.line, endOfLineIndex))); } diff --git a/src/annotations/recentChangesAnnotationProvider.ts b/src/annotations/recentChangesAnnotationProvider.ts index f283040..ead5327 100644 --- a/src/annotations/recentChangesAnnotationProvider.ts +++ b/src/annotations/recentChangesAnnotationProvider.ts @@ -48,7 +48,7 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase { if (cfg.hover.details) { this._decorations.push({ - hoverMessage: Annotations.getHoverMessage(commit, dateFormat, await this.git.hasRemotes(commit.repoPath), this._config.blame.file.annotationType), + hoverMessage: Annotations.getHoverMessage(commit, dateFormat, await this.git.hasRemote(commit.repoPath), this._config.blame.file.annotationType), range: range } as DecorationOptions); } diff --git a/src/constants.ts b/src/constants.ts index 3357750..24419a5 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -35,7 +35,7 @@ export enum CommandContext { GitExplorerView = 'gitlens:gitExplorer:view', HasRemotes = 'gitlens:hasRemotes', HasRepository = 'gitlens:hasRepository', - ActiveHasRemotes = 'gitlens:activeHasRemotes', + ActiveHasRemote = 'gitlens:activeHasRemote', ActiveIsBlameable = 'gitlens:activeIsBlameable', ActiveFileIsTracked = 'gitlens:activeIsTracked', Key = 'gitlens:key' diff --git a/src/currentLineController.ts b/src/currentLineController.ts index 27430da..dae410c 100644 --- a/src/currentLineController.ts +++ b/src/currentLineController.ts @@ -427,7 +427,7 @@ export class CurrentLineController extends Disposable { } } - const message = Annotations.getHoverMessage(logCommit || commit, this._config.defaultDateFormat, await this.git.hasRemotes(commit.repoPath), this._config.blame.file.annotationType); + const message = Annotations.getHoverMessage(logCommit || commit, this._config.defaultDateFormat, await this.git.hasRemote(commit.repoPath), this._config.blame.file.annotationType); return new Hover(message, range); } diff --git a/src/git/gitContextTracker.ts b/src/git/gitContextTracker.ts index 4909377..851d142 100644 --- a/src/git/gitContextTracker.ts +++ b/src/git/gitContextTracker.ts @@ -191,13 +191,11 @@ export class GitContextTracker extends Disposable { private async updateRemotes() { let hasRemotes = false; if (this._context.repo !== undefined) { - hasRemotes = await this._context.repo.hasRemotes(); - setCommandContext(CommandContext.ActiveHasRemotes, hasRemotes); - } - else { - setCommandContext(CommandContext.ActiveHasRemotes, false); + hasRemotes = await this._context.repo.hasRemote(); } + setCommandContext(CommandContext.ActiveHasRemote, hasRemotes); + if (!hasRemotes) { const repositories = await this.git.getRepositories(); for (const repo of repositories) { diff --git a/src/git/models/repository.ts b/src/git/models/repository.ts index 8b06423..d9a2334 100644 --- a/src/git/models/repository.ts +++ b/src/git/models/repository.ts @@ -65,6 +65,7 @@ export class Repository extends Disposable { readonly normalizedPath: string; readonly storage: Map = new Map(); + private _branch: Promise | undefined; private readonly _disposable: Disposable; private _fireChangeDebounced: ((e: RepositoryChangeEvent) => void) | undefined = undefined; private _fireFileSystemChangeDebounced: ((e: RepositoryFileSystemChangeEvent) => void) | undefined = undefined; @@ -72,7 +73,7 @@ export class Repository extends Disposable { private _fsWatcherDisposable: Disposable | undefined; private _pendingChanges: { repo?: RepositoryChangeEvent, fs?: RepositoryFileSystemChangeEvent } = { }; private _providerMap: RemoteProviderMap | undefined; - private _remotes: GitRemote[] | undefined; + private _remotes: Promise | undefined; private _suspended: boolean; constructor( @@ -148,6 +149,8 @@ export class Repository extends Disposable { return; } + this._branch = undefined; + if (uri !== undefined && uri.path.endsWith('refs/remotes')) { this._remotes = undefined; this.fireChange(RepositoryChange.Remotes); @@ -227,8 +230,11 @@ export class Repository extends Disposable { return this.folder === workspace.getWorkspaceFolder(uri); } - async getBranch(): Promise { - return this.git.getBranch(this.path); + getBranch(): Promise { + if (this._branch === undefined) { + this._branch = this.git.getBranch(this.path); + } + return this._branch; } async getBranches(): Promise { @@ -239,14 +245,14 @@ export class Repository extends Disposable { return this.git.getChangedFilesCount(this.path, sha); } - async getRemotes(): Promise { + getRemotes(): Promise { if (this._remotes === undefined) { if (this._providerMap === undefined) { const remotesCfg = configuration.get(configuration.name('remotes').value, this.folder.uri); this._providerMap = RemoteProviderFactory.createMap(remotesCfg); } - this._remotes = await this.git.getRemotesCore(this.path, this._providerMap); + this._remotes = this.git.getRemotesCore(this.path, this._providerMap); } return this._remotes; @@ -260,6 +266,11 @@ export class Repository extends Disposable { return this.git.getStatusForRepo(this.path); } + async hasRemote(): Promise { + const branch = await this.getBranch(); + return branch !== undefined && branch.tracking !== undefined; + } + async hasRemotes(): Promise { const remotes = await this.getRemotes(); return remotes !== undefined && remotes.length > 0; diff --git a/src/gitService.ts b/src/gitService.ts index ba57954..b82b841 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -992,6 +992,15 @@ export class GitService extends Disposable { } } + async hasRemote(repoPath: string | undefined): Promise { + if (repoPath === undefined) return false; + + const repository = await this.getRepository(repoPath); + if (repository === undefined) return false; + + return repository.hasRemote(); + } + async hasRemotes(repoPath: string | undefined): Promise { if (repoPath === undefined) return false;