Skip to content

Commit b58c023

Browse files
committed
Adds repo picker to commit search
Adds repo name to results if there is more than 1
1 parent 7c196e5 commit b58c023

File tree

8 files changed

+57
-14
lines changed

8 files changed

+57
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Added
9+
- Adds a repository quick pick menu to the *Show Commit Search* command (`gitlens.showCommitSearch`) when there is no active repository
10+
811
### Fixed
912
- Fixes [#257](https://github.com/eamodio/vscode-gitlens/issues/257) - Some branches fail to show history
1013
- Fixes [#259](https://github.com/eamodio/vscode-gitlens/issues/259) - File history lists unrelated merge commits

src/commands/showCommitSearch.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { GlyphChars } from '../constants';
66
import { Container } from '../container';
77
import { GitRepoSearchBy, GitService, GitUri } from '../gitService';
88
import { Logger } from '../logger';
9-
import { Messages } from '../messages';
10-
import { CommandQuickPickItem, CommitsQuickPick, ShowCommitsSearchInResultsQuickPickItem } from '../quickPicks';
9+
import { CommandQuickPickItem, CommitsQuickPick, RepositoriesQuickPick, ShowCommitsSearchInResultsQuickPickItem } from '../quickPicks';
1110
import { ShowQuickCommitDetailsCommandArgs } from './showQuickCommitDetails';
1211

1312
const searchByRegex = /^([@~=:#])/;
@@ -38,8 +37,14 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
3837

3938
const gitUri = uri === undefined ? undefined : await GitUri.fromUri(uri);
4039

41-
const repoPath = gitUri === undefined ? Container.git.getHighlanderRepoPath() : gitUri.repoPath;
42-
if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to show commit search`);
40+
let repoPath = gitUri === undefined ? Container.git.getHighlanderRepoPath() : gitUri.repoPath;
41+
if (!repoPath) {
42+
const pick = await RepositoriesQuickPick.show(`Search for commits in which repository${GlyphChars.Ellipsis}`, args.goBackCommand);
43+
if (pick instanceof CommandQuickPickItem) return pick.execute();
44+
if (pick === undefined) return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute();
45+
46+
repoPath = pick.repoPath;
47+
}
4348

4449
args = { ...args };
4550
const originalArgs = { ...args };

src/gitService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,11 @@ export class GitService extends Disposable {
11861186
return repo;
11871187
}
11881188

1189+
async getRepositoryCount(): Promise<number> {
1190+
const repositoryTree = await this.getRepositoryTree();
1191+
return repositoryTree.count();
1192+
}
1193+
11891194
async getStashList(repoPath: string | undefined): Promise<GitStash | undefined> {
11901195
if (repoPath === undefined) return undefined;
11911196

src/system/iterable.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
'use strict';
22

33
export namespace Iterables {
4+
export function count<T>(source: Iterable<T> | IterableIterator<T>): number {
5+
let count = 0;
6+
let next: IteratorResult<T>;
7+
8+
while (true) {
9+
next = (source as IterableIterator<T>).next();
10+
if (next.done) break;
11+
12+
count++;
13+
}
14+
15+
return count;
16+
}
17+
418
export function every<T>(source: Iterable<T> | IterableIterator<T>, predicate: (item: T) => boolean): boolean {
519
for (const item of source) {
620
if (!predicate(item)) return false;

src/system/searchTree.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ export class TernarySearchTree<E> {
339339
return this._root !== undefined && !this._root.isEmpty();
340340
}
341341

342+
count(): number {
343+
if (this._root === undefined || this._root.isEmpty()) return 0;
344+
345+
return Iterables.count(this.entries());
346+
}
347+
342348
entries(): Iterable<[E, string]> {
343349
return this._iterator(this._root!, []);
344350
}

src/views/commitsResultsNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class CommitsResultsNode extends ExplorerNode {
1313

1414
constructor(
1515
readonly repoPath: string,
16-
private readonly labelFn: (log: GitLog | undefined) => string,
16+
private readonly labelFn: (log: GitLog | undefined) => Promise<string>,
1717
private readonly logFn: (maxCount: number | undefined) => Promise<GitLog | undefined>,
1818
private readonly explorer: Explorer,
1919
private readonly contextValue: ResourceType = ResourceType.Results
@@ -49,7 +49,7 @@ export class CommitsResultsNode extends ExplorerNode {
4949
const log = await this.logFn(this.maxCount);
5050

5151
this._cache = {
52-
label: this.labelFn(log),
52+
label: await this.labelFn(log),
5353
log: log
5454
};
5555
}

src/views/comparisionResultsNode.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ComparisionResultsNode extends ExplorerNode {
2323
this.resetChildren();
2424

2525
const commitsQueryFn = (maxCount: number | undefined) => Container.git.getLog(this.uri.repoPath!, { maxCount: maxCount, ref: `${this.ref1}...${this.ref2 || 'HEAD'}` });
26-
const commitsLabelFn = (log: GitLog | undefined) => {
26+
const commitsLabelFn = async (log: GitLog | undefined) => {
2727
const count = log !== undefined ? log.count : 0;
2828
const truncated = log !== undefined ? log.truncated : false;
2929

@@ -48,9 +48,13 @@ export class ComparisionResultsNode extends ExplorerNode {
4848
}
4949

5050
async getTreeItem(): Promise<TreeItem> {
51-
const repo = await Container.git.getRepository(this.uri.repoPath!);
51+
let repository = '';
52+
if (await Container.git.getRepositoryCount() > 1) {
53+
const repo = await Container.git.getRepository(this.uri.repoPath!);
54+
repository = ` ${Strings.pad(GlyphChars.Dash, 1, 1)} ${(repo && repo.formattedName) || this.uri.repoPath}`;
55+
}
5256

53-
const item = new TreeItem(`Comparing ${GitService.shortenSha(this.ref1)} to ${this.ref2 !== '' ? GitService.shortenSha(this.ref2) : 'Working Tree'} ${Strings.pad(GlyphChars.Dash, 1, 1)} ${(repo && repo.formattedName) || this.uri.repoPath}`, TreeItemCollapsibleState.Expanded);
57+
const item = new TreeItem(`Comparing ${GitService.shortenSha(this.ref1)} to ${this.ref2 !== '' ? GitService.shortenSha(this.ref2) : 'Working Tree'}${repository}`, TreeItemCollapsibleState.Expanded);
5458
item.contextValue = ResourceType.ComparisonResults;
5559
return item;
5660
}

src/views/resultsExplorer.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
2-
import { Functions } from '../system';
2+
import { Functions, Strings } from '../system';
33
import { commands, ConfigurationChangeEvent, ConfigurationTarget, Disposable, Event, EventEmitter, TreeDataProvider, TreeItem, window } from 'vscode';
44
import { configuration, ExplorerFilesLayout, IExplorerConfig } from '../configuration';
5-
import { CommandContext, setCommandContext, WorkspaceState } from '../constants';
5+
import { CommandContext, GlyphChars, setCommandContext, WorkspaceState } from '../constants';
66
import { Container } from '../container';
77
import { RefreshNodeCommandArgs } from './explorerCommands';
88
import { CommitResultsNode, CommitsResultsNode, ComparisionResultsNode, ExplorerNode, MessageNode, RefreshReason, ResourceType } from './explorerNodes';
@@ -138,7 +138,7 @@ export class ResultsExplorer extends Disposable implements TreeDataProvider<Expl
138138
? (maxCount: number | undefined) => Promise.resolve(results)
139139
: results.query;
140140

141-
const labelFn = (log: GitLog | undefined) => {
141+
const labelFn = async (log: GitLog | undefined) => {
142142
if (typeof resultsLabel === 'string') return resultsLabel;
143143

144144
const count = log !== undefined ? log.count : 0;
@@ -148,8 +148,14 @@ export class ResultsExplorer extends Disposable implements TreeDataProvider<Expl
148148
? { singular: 'result', plural: 'results' }
149149
: resultsLabel.resultsType;
150150

151-
if (count === 1) return `1 ${resultsType.singular} for ${resultsLabel.label}`;
152-
return `${count === 0 ? 'No' : `${count}${truncated ? '+' : ''}`} ${resultsType.plural} for ${resultsLabel.label}`;
151+
let repository = '';
152+
if (await Container.git.getRepositoryCount() > 1) {
153+
const repo = await Container.git.getRepository(results.repoPath);
154+
repository = ` ${Strings.pad(GlyphChars.Dash, 1, 1)} ${(repo && repo.formattedName) || results.repoPath}`;
155+
}
156+
157+
if (count === 1) return `1 ${resultsType.singular} for ${resultsLabel.label}${repository}`;
158+
return `${count === 0 ? 'No' : `${count}${truncated ? '+' : ''}`} ${resultsType.plural} for ${resultsLabel.label}${repository}`;
153159
};
154160

155161
this.addResults(new CommitsResultsNode(results.repoPath, labelFn, Functions.seeded(query, results), this, ResourceType.SearchResults));

0 commit comments

Comments
 (0)