Skip to content

Commit 445f7eb

Browse files
committed
Adds ability to search commits by date in commit graph
1 parent 06f8cf2 commit 445f7eb

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/constants.search.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
type SearchOperatorsShortForm = '' | '=:' | '@:' | '#:' | '?:' | '~:' | 'is:';
2-
export type SearchOperatorsLongForm = 'message:' | 'author:' | 'commit:' | 'file:' | 'change:' | 'type:';
1+
type SearchOperatorsShortForm = '' | '=:' | '@:' | '#:' | '?:' | '~:' | 'is:' | 'after:' | 'before:';
2+
export type SearchOperatorsLongForm =
3+
| 'message:'
4+
| 'author:'
5+
| 'commit:'
6+
| 'file:'
7+
| 'change:'
8+
| 'type:'
9+
| 'date:'
10+
| 'after:'
11+
| 'before:';
312
export type SearchOperators = SearchOperatorsShortForm | SearchOperatorsLongForm;
413

514
export const searchOperators = new Set<string>([
@@ -16,6 +25,9 @@ export const searchOperators = new Set<string>([
1625
'change:',
1726
'is:',
1827
'type:',
28+
'date:',
29+
'after:',
30+
'before:',
1931
]);
2032

2133
export const searchOperatorsToLongFormMap = new Map<SearchOperators, SearchOperatorsLongForm>([
@@ -32,13 +44,16 @@ export const searchOperatorsToLongFormMap = new Map<SearchOperators, SearchOpera
3244
['change:', 'change:'],
3345
['is:', 'type:'],
3446
['type:', 'type:'],
47+
['after:', 'after:'],
48+
['before:', 'before:'],
49+
['date:', 'date:'],
3550
]);
3651

3752
export const searchOperationRegex =
38-
/(?:(?<op>=:|message:|@:|author:|#:|commit:|\?:|file:|~:|change:|is:|type:)\s?(?<value>".+?"|\S+}?))|(?<text>\S+)(?!(?:=|message|@|author|#|commit|\?|file|~|change|is|type):)/g;
53+
/(?:(?<op>=:|message:|@:|author:|#:|commit:|\?:|file:|~:|change:|is:|type:|date:|after:|before:)\s?(?<value>".+?"|\S+}?))|(?<text>\S+)(?!(?:=|message|@|author|#|commit|\?|file|~|change|is|type):)/g;
3954

4055
export const searchOperationHelpRegex =
41-
/(?:^|(\b|\s)*)((=:|message:|@:|author:|#:|commit:|\?:|file:|~:|change:|is:|type:)(?:"[^"]*"?|\w*))(?:$|(\b|\s))/g;
56+
/(?:^|(\b|\s)*)((=:|message:|@:|author:|#:|commit:|\?:|file:|~:|change:|is:|type:|date:|after:|before:)(?:"[^"]*"?|\w*))(?:$|(\b|\s))/g;
4257

4358
export interface SearchQuery {
4459
query: string;

src/git/search.ts

+19
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ export function getGitArgsFromSearchQuery(
213213
}
214214
}
215215

216+
break;
217+
case 'date:':
218+
for (const value of values) {
219+
const date = new Date(value);
220+
searchArgs.add('--after').add(date.toISOString());
221+
searchArgs.add('--before').add(new Date(date.getTime() + 1000 * 60 * 60 * 24).toISOString());
222+
}
223+
break;
224+
case 'after:':
225+
for (const value of values) {
226+
const date = new Date(value);
227+
searchArgs.add('--after').add(date.toISOString());
228+
}
229+
break;
230+
case 'before:':
231+
for (const value of values) {
232+
const date = new Date(value);
233+
searchArgs.add('--before').add(date.toISOString());
234+
}
216235
break;
217236
}
218237
}

src/webviews/apps/shared/components/search/search-input.ts

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { TemplateResult } from 'lit';
12
import { css, html } from 'lit';
23
import { customElement, property, query, state } from 'lit/decorators.js';
34
import type { SearchOperators, SearchOperatorsLongForm, SearchQuery } from '../../../../../constants.search';
@@ -389,7 +390,23 @@ export class GlSearchInput extends GlElement {
389390
this.searchHistoryPos = this.searchHistory.length - 1;
390391
}
391392

392-
override render(): unknown {
393+
private padDate(date: number) {
394+
let stringDate = date.toString();
395+
if (stringDate.length < 2) {
396+
stringDate = `0${stringDate}`;
397+
}
398+
return stringDate;
399+
}
400+
401+
private handleInsertDateToken(tokenPrefix: string) {
402+
const currentDate = new Date();
403+
const year = currentDate.getFullYear();
404+
const month = this.padDate(currentDate.getMonth() + 1);
405+
const date = this.padDate(currentDate.getDate());
406+
this.handleInsertToken(`${tokenPrefix}${year}-${month}-${date}`);
407+
}
408+
409+
override render(): TemplateResult {
393410
return html`<div class="field">
394411
<div class="controls controls__start">
395412
<gl-button
@@ -486,6 +503,33 @@ export class GlSearchInput extends GlElement {
486503
Type <small>type:stash or is:stash</small>
487504
</button>
488505
</menu-item>
506+
<menu-item role="none">
507+
<button
508+
class="menu-button"
509+
type="button"
510+
@click="${() => this.handleInsertDateToken('date:')}"
511+
>
512+
Date <small>date:YYYY-MM-dd</small>
513+
</button>
514+
</menu-item>
515+
<menu-item role="none">
516+
<button
517+
class="menu-button"
518+
type="button"
519+
@click="${() => this.handleInsertDateToken('after:')}"
520+
>
521+
Date from <small>after:YYYY-MM-dd</small>
522+
</button>
523+
</menu-item>
524+
<menu-item role="none">
525+
<button
526+
class="menu-button"
527+
type="button"
528+
@click="${() => this.handleInsertDateToken(`before:`)}"
529+
>
530+
Date to <small>before:YYYY-MM-dd</small>
531+
</button>
532+
</menu-item>
489533
</div>
490534
</gl-popover>
491535
</div>

0 commit comments

Comments
 (0)