Skip to content

Commit

Permalink
Considers selected row in graph search
Browse files Browse the repository at this point in the history
Adds search shortcuts F3/Cmd+G to the graph
  • Loading branch information
eamodio committed Sep 25, 2022
1 parent 785048d commit 616b7fa
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 92 deletions.
88 changes: 47 additions & 41 deletions src/env/node/git/git.ts
Expand Up @@ -146,7 +146,7 @@ export class Git {
'core.quotepath=false',
'-c',
'color.ui=false',
...(configs !== undefined ? configs : emptyArray),
...(configs != null ? configs : emptyArray),
);

if (process.platform === 'win32') {
Expand Down Expand Up @@ -861,28 +861,18 @@ export class Git {
options?: { cancellation?: CancellationToken; configs?: readonly string[]; ref?: string; stdin?: string },
...args: string[]
) {
const params = ['log'];
if (options?.stdin) {
params.push('--stdin');
}
params.push(...args);

if (options?.ref && !GitRevision.isUncommittedStaged(options.ref)) {
params.push(options?.ref);
}

if (!params.includes('--')) {
params.push('--');
}

return this.git<string>(
{
cwd: repoPath,
cancellation: options?.cancellation,
configs: options?.configs ?? gitLogDefaultConfigs,
stdin: options?.stdin,
},
...params,
'log',
...(options?.stdin ? ['--stdin'] : emptyArray),
...args,
...(options?.ref && !GitRevision.isUncommittedStaged(options.ref) ? [options.ref] : emptyArray),
...(!args.includes('--') ? ['--'] : emptyArray),
);
}

Expand Down Expand Up @@ -1185,35 +1175,34 @@ export class Git {
log__search(
repoPath: string,
search: string[] = emptyArray,
{
limit,
ordering,
skip,
useShow,
}: { limit?: number; ordering?: 'date' | 'author-date' | 'topo' | null; skip?: number; useShow?: boolean } = {},
options?: {
limit?: number;
ordering?: 'date' | 'author-date' | 'topo' | null;
skip?: number;
useShow?: boolean;
},
) {
const params = [
useShow ? 'show' : 'log',
'--name-status',
`--format=${GitLogParser.defaultFormat}`,
'--use-mailmap',
];

if (limit && !useShow) {
params.push(`-n${limit + 1}`);
}

if (skip && !useShow) {
params.push(`--skip=${skip}`);
}

if (ordering && !useShow) {
params.push(`--${ordering}-order`);
if (options?.useShow) {
return this.git<string>(
{ cwd: repoPath },
'show',
'-s',
'--name-status',
`--format=${GitLogParser.defaultFormat}`,
'--use-mailmap',
...search,
);
}

return this.git<string>(
{ cwd: repoPath, configs: useShow ? undefined : gitLogDefaultConfigs },
...params,
{ cwd: repoPath, configs: gitLogDefaultConfigs },
'log',
'--name-status',
`--format=${GitLogParser.defaultFormat}`,
'--use-mailmap',
...(options?.limit ? [`-n${options.limit + 1}`] : emptyArray),
...(options?.skip ? [`--skip=${options.skip}`] : emptyArray),
...(options?.ordering ? [`--${options.ordering}-order`] : emptyArray),
...search,
);
}
Expand Down Expand Up @@ -1567,6 +1556,23 @@ export class Git {
}
}

show2(
repoPath: string,
options?: { cancellation?: CancellationToken; configs?: readonly string[] },
...args: string[]
) {
return this.git<string>(
{
cwd: repoPath,
cancellation: options?.cancellation,
configs: options?.configs ?? gitLogDefaultConfigs,
},
'show',
...args,
...(!args.includes('--') ? ['--'] : emptyArray),
);
}

show__diff(
repoPath: string,
fileName: string,
Expand Down
47 changes: 34 additions & 13 deletions src/env/node/git/localGitProvider.ts
Expand Up @@ -92,7 +92,8 @@ import {
createLogParserSingle,
createLogParserWithFiles,
getGraphParser,
getGraphRefParser,
getRefAndDateParser,
getRefParser,
GitLogParser,
LogType,
} from '../../../git/parsers/logParser';
Expand Down Expand Up @@ -1620,7 +1621,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
},
): Promise<GitGraph> {
const parser = getGraphParser();
const refParser = getGraphRefParser();
const refParser = getRefParser();

const defaultLimit = options?.limit ?? configuration.get('graph.defaultItemLimit') ?? 5000;
const defaultPageLimit = configuration.get('graph.pageItemLimit') ?? 1000;
Expand Down Expand Up @@ -2646,30 +2647,48 @@ export class LocalGitProvider implements GitProvider, Disposable {
const comparisonKey = getSearchQueryComparisonKey(search);
try {
const refAndDateParser = getRefAndDateParser();
const { args: searchArgs, files, shas } = getGitArgsFromSearchQuery(search);
if (shas?.size) {
const data = await this.git.show2(
repoPath,
{ cancellation: options?.cancellation },
'-s',
...refAndDateParser.arguments,
...shas.values(),
...searchArgs,
'--',
);
const results = new Map<string, number>(
map(refAndDateParser.parse(data), c => [
c.sha,
Number(options?.ordering === 'author-date' ? c.authorDate : c.committerDate) * 1000,
]),
);
return {
repoPath: repoPath,
query: search,
comparisonKey: comparisonKey,
results: shas,
results: results,
};
}
const refParser = getGraphRefParser();
const limit = options?.limit ?? configuration.get('advanced.maxSearchItems') ?? 0;
const similarityThreshold = configuration.get('advanced.similarityThreshold');
const args = [
...refParser.arguments,
...refAndDateParser.arguments,
`-M${similarityThreshold == null ? '' : `${similarityThreshold}%`}`,
'--use-mailmap',
];
if (options?.ordering) {
args.push(`--${options.ordering}-order`);
}
const results = new Set<string>();
const results = new Map<string, number>();
let total = 0;
let iterations = 0;
Expand Down Expand Up @@ -2702,19 +2721,21 @@ export class LocalGitProvider implements GitProvider, Disposable {
}
let count = 0;
let last: string | undefined;
for (const r of refParser.parse(data)) {
results.add(r);
for (const r of refAndDateParser.parse(data)) {
results.set(
r.sha,
Number(options?.ordering === 'author-date' ? r.authorDate : r.committerDate) * 1000,
);
count++;
last = r;
}
total += count;
const lastSha = last(results)?.[0];
cursor =
last != null
lastSha != null
? {
sha: last,
sha: lastSha,
skip: total - iterations,
}
: undefined;
Expand Down Expand Up @@ -2743,7 +2764,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
repoPath: repoPath,
query: search,
comparisonKey: comparisonKey,
results: new Set<string>(),
results: new Map<string, number>(),
};
}
}
Expand Down
26 changes: 20 additions & 6 deletions src/git/parsers/logParser.ts
Expand Up @@ -106,14 +106,28 @@ export function getGraphParser(): GraphParser {
return _graphParser;
}

type GraphRefParser = Parser<string>;
type RefParser = Parser<string>;

let _graphRefParser: GraphRefParser | undefined;
export function getGraphRefParser(): GraphRefParser {
if (_graphRefParser == null) {
_graphRefParser = createLogParserSingle('%H');
let _refParser: RefParser | undefined;
export function getRefParser(): RefParser {
if (_refParser == null) {
_refParser = createLogParserSingle('%H');
}
return _graphRefParser;
return _refParser;
}

type RefAndDateParser = Parser<{ sha: string; authorDate: string; committerDate: string }>;

let _refAndDateParser: RefAndDateParser | undefined;
export function getRefAndDateParser(): RefAndDateParser {
if (_refAndDateParser == null) {
_refAndDateParser = createLogParser({
sha: '%H',
authorDate: '%at',
committerDate: '%ct',
});
}
return _refAndDateParser;
}

export function createLogParser<T extends Record<string, unknown>>(
Expand Down
2 changes: 1 addition & 1 deletion src/git/search.ts
Expand Up @@ -47,7 +47,7 @@ export interface GitSearch {
repoPath: string;
query: SearchQuery;
comparisonKey: string;
results: Set<string>;
results: Map<string, number>;

readonly paging?: {
readonly limit: number | undefined;
Expand Down
8 changes: 6 additions & 2 deletions src/plus/github/github.ts
Expand Up @@ -1910,7 +1910,7 @@ export class GitHubApi implements Disposable {
order?: 'asc' | 'desc' | undefined;
sort?: 'author-date' | 'committer-date' | undefined;
},
): Promise<GitHubPagedResult<string> | undefined> {
): Promise<GitHubPagedResult<{ sha: string; authorDate: number; committerDate: number }> | undefined> {
const scope = getLogScope();

const limit = Math.min(100, options?.limit ?? 100);
Expand Down Expand Up @@ -1959,7 +1959,11 @@ export class GitHubApi implements Disposable {
hasNextPage: hasMore,
},
totalCount: data.total_count,
values: data.items.map(r => r.sha),
values: data.items.map(r => ({
sha: r.sha,
authorDate: new Date(r.commit.author.date).getTime(),
committerDate: new Date(r.commit.committer?.date ?? r.commit.author.date).getTime(),
})),
};
} catch (ex) {
if (ex instanceof ProviderRequestNotFoundError) return undefined;
Expand Down
24 changes: 18 additions & 6 deletions src/plus/github/githubGitProvider.ts
Expand Up @@ -1597,14 +1597,23 @@ export class GitHubGitProvider implements GitProvider, Disposable {
const comparisonKey = getSearchQueryComparisonKey(search);

try {
const results = new Set<string>();
const results = new Map<string, number>();
const operations = parseSearchQuery(search.query);

let op;
let values = operations.get('commit:');
if (values != null) {
for (const value of values) {
results.add(value.replace(doubleQuoteRegex, ''));
const commitsResults = await Promise.allSettled<Promise<GitCommit | undefined>[]>(
values.map(v => this.getCommit(repoPath, v.replace(doubleQuoteRegex, ''))),
);
for (const commitResult of commitsResults) {
const commit = getSettledValue(commitResult);
if (commit == null) continue;

results.set(
commit.sha,
Number(options?.ordering === 'author-date' ? commit.author.date : commit.committer.date),
);
}

return {
Expand Down Expand Up @@ -1681,8 +1690,11 @@ export class GitHubGitProvider implements GitProvider, Disposable {
return { repoPath: repoPath, query: search, comparisonKey: comparisonKey, results: results };
}

for (const sha of result.values) {
results.add(sha);
for (const commit of result.values) {
results.set(
commit.sha,
Number(options?.ordering === 'author-date' ? commit.authorDate : commit.committerDate),
);
}

cursor = result.pageInfo?.endCursor ?? undefined;
Expand Down Expand Up @@ -1710,7 +1722,7 @@ export class GitHubGitProvider implements GitProvider, Disposable {
repoPath: repoPath,
query: search,
comparisonKey: comparisonKey,
results: new Set<string>(),
results: new Map<string, number>(),
};
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/plus/webviews/graph/graphWebview.ts
Expand Up @@ -451,7 +451,7 @@ export class GraphWebview extends WebviewBase<State> {
DidSearchCommitsNotificationType,
{
results: {
ids: [...search.results.values()],
ids: Object.fromEntries(search.results),
paging: { hasMore: search.paging?.hasMore ?? false },
},
selectedRows: this._selectedRows,
Expand Down Expand Up @@ -497,14 +497,14 @@ export class GraphWebview extends WebviewBase<State> {
}

if (search.results.size > 0) {
this.setSelectedRows(first(search.results));
this.setSelectedRows(first(search.results)![0]);
}

void this.notify(
DidSearchCommitsNotificationType,
{
results: {
ids: [...search.results.values()],
ids: Object.fromEntries(search.results),
paging: { hasMore: search.paging?.hasMore ?? false },
},
selectedRows: this._selectedRows,
Expand Down Expand Up @@ -778,8 +778,8 @@ export class GraphWebview extends WebviewBase<State> {

if (this._search != null) {
const search = this._search;
const lastResult = last(search.results);
if (lastResult != null && updatedGraph.ids.has(lastResult)) {
const lastId = last(search.results)?.[0];
if (lastId != null && updatedGraph.ids.has(lastId)) {
queueMicrotask(() => void this.onSearchCommits({ search: search.query, more: true }));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/plus/webviews/graph/protocol.ts
Expand Up @@ -174,7 +174,7 @@ export const DidEnsureCommitNotificationType = new IpcNotificationType<DidEnsure
);

export interface DidSearchCommitsParams {
results: { ids: string[]; paging?: { hasMore: boolean } } | undefined;
results: { ids: { [sha: string]: number }; paging?: { hasMore: boolean } } | undefined;
selectedRows?: { [id: string]: true };
}
export const DidSearchCommitsNotificationType = new IpcNotificationType<DidSearchCommitsParams>(
Expand Down

0 comments on commit 616b7fa

Please sign in to comment.