-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathsearch.ts
241 lines (202 loc) · 6.43 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import type { SearchOperators, SearchOperatorsLongForm, SearchQuery } from '../constants.search';
import { searchOperationRegex, searchOperatorsToLongFormMap } from '../constants.search';
import type { StoredSearchQuery } from '../constants.storage';
import type { GitRevisionReference } from './models/reference';
import type { GitUser } from './models/user';
import { isSha, shortenRevision } from './utils/revision.utils';
export interface GitGraphSearchResultData {
date: number;
i: number;
}
export type GitGraphSearchResults = Map<string, GitGraphSearchResultData>;
export interface GitGraphSearch {
repoPath: string;
query: SearchQuery;
comparisonKey: string;
results: GitGraphSearchResults;
readonly paging?: {
readonly limit: number | undefined;
readonly hasMore: boolean;
};
more?(limit: number): Promise<GitGraphSearch>;
}
export function getSearchQuery(search: StoredSearchQuery): SearchQuery {
return {
query: search.pattern,
matchAll: search.matchAll,
matchCase: search.matchCase,
matchRegex: search.matchRegex,
};
}
export function getStoredSearchQuery(search: SearchQuery): StoredSearchQuery {
return {
pattern: search.query,
matchAll: search.matchAll,
matchCase: search.matchCase,
matchRegex: search.matchRegex,
};
}
export function getSearchQueryComparisonKey(search: SearchQuery | StoredSearchQuery): string {
return `${'query' in search ? search.query : search.pattern}|${search.matchAll ? 'A' : ''}${
search.matchCase ? 'C' : ''
}${search.matchRegex ? 'R' : ''}`;
}
export function createSearchQueryForCommit(ref: string): string;
export function createSearchQueryForCommit(commit: GitRevisionReference): string;
export function createSearchQueryForCommit(refOrCommit: string | GitRevisionReference): string {
return `#:${typeof refOrCommit === 'string' ? shortenRevision(refOrCommit) : refOrCommit.name}`;
}
export function createSearchQueryForCommits(refs: string[]): string;
export function createSearchQueryForCommits(commits: GitRevisionReference[]): string;
export function createSearchQueryForCommits(refsOrCommits: (string | GitRevisionReference)[]): string {
return refsOrCommits.map(r => `#:${typeof r === 'string' ? shortenRevision(r) : r.name}`).join(' ');
}
export function parseSearchQuery(search: SearchQuery): Map<SearchOperatorsLongForm, Set<string>> {
const operations = new Map<SearchOperatorsLongForm, Set<string>>();
let op: SearchOperators | undefined;
let value: string | undefined;
let text: string | undefined;
let match;
do {
match = searchOperationRegex.exec(search.query);
if (match?.groups == null) break;
op = searchOperatorsToLongFormMap.get(match.groups.op as SearchOperators);
({ value, text } = match.groups);
if (text) {
if (!searchOperatorsToLongFormMap.has(text.trim() as SearchOperators)) {
op = text === '@me' ? 'author:' : isSha(text) ? 'commit:' : 'message:';
value = text;
}
}
if (op && value) {
let values = operations.get(op);
if (values == null) {
values = new Set();
operations.set(op, values);
}
values.add(value);
}
} while (match != null);
return operations;
}
const doubleQuoteRegex = /"/g;
export function getGitArgsFromSearchQuery(
search: SearchQuery,
currentUser: GitUser | undefined,
): {
args: string[];
files: string[];
shas?: Set<string> | undefined;
} {
const operations = parseSearchQuery(search);
const searchArgs = new Set<string>();
const files: string[] = [];
let shas;
let op;
let values = operations.get('commit:');
if (values != null) {
for (const value of values) {
searchArgs.add(value.replace(doubleQuoteRegex, ''));
}
shas = searchArgs;
} else {
searchArgs.add('--all');
searchArgs.add(search.matchRegex ? '--extended-regexp' : '--fixed-strings');
if (search.matchRegex && !search.matchCase) {
searchArgs.add('--regexp-ignore-case');
}
for ([op, values] of operations.entries()) {
switch (op) {
case 'message:':
if (search.matchAll) {
searchArgs.add('--all-match');
}
for (let value of values) {
if (!value) continue;
value = value.replace(doubleQuoteRegex, search.matchRegex ? '\\b' : '');
if (!value) continue;
searchArgs.add(`--grep=${value}`);
}
break;
case 'author:':
for (let value of values) {
if (!value) continue;
value = value.replace(doubleQuoteRegex, search.matchRegex ? '\\b' : '');
if (!value) continue;
if (value === '@me') {
if (currentUser?.name == null) continue;
value = currentUser.name;
}
if (value.startsWith('@')) {
searchArgs.add(`--author=${value.slice(1)}`);
continue;
}
searchArgs.add(`--author=${value}`);
}
break;
case 'change:':
for (let value of values) {
if (!value) continue;
if (value.startsWith('"')) {
value = value.replace(doubleQuoteRegex, '');
if (!value) continue;
}
searchArgs.add(search.matchRegex ? `-G${value}` : `-S${value}`);
}
break;
case 'file:':
for (let value of values) {
if (!value) continue;
if (value.startsWith('"')) {
value = value.replace(doubleQuoteRegex, '');
if (!value) continue;
files.push(value);
} else {
const prefix = search.matchCase ? '' : ':(icase)';
if (/[/\\*?|![\]{}]/.test(value)) {
files.push(`${prefix}${value}`);
} else {
const index = value.indexOf('.');
if (index > 0) {
// maybe a file extension
files.push(`${prefix}**/${value}`);
} else {
files.push(`${prefix}*${value}*`);
}
}
}
}
break;
case 'type:':
for (const value of values) {
if (value === 'stash') {
if (!searchArgs.has('--no-walk')) {
searchArgs.add('--no-walk');
}
}
}
break;
case 'date:':
for (const value of values) {
const date = new Date(value);
searchArgs.add('--after').add(date.toISOString());
searchArgs.add('--before').add(new Date(date.getTime() + 1000 * 60 * 60 * 24).toISOString());
}
break;
case 'after:':
for (const value of values) {
const date = new Date(value);
searchArgs.add('--after').add(date.toISOString());
}
break;
case 'before:':
for (const value of values) {
const date = new Date(value);
searchArgs.add('--before').add(date.toISOString());
}
break;
}
}
}
return { args: [...searchArgs.values()], files: files, shas: shas };
}