Skip to content

Commit 991c856

Browse files
committed
Adds ability to search commits by date in commit graph
1 parent 1605896 commit 991c856

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/constants.search.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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+
| 'after:'
10+
| 'before:';
311
export type SearchOperators = SearchOperatorsShortForm | SearchOperatorsLongForm;
412

513
export const searchOperators = new Set<string>([
@@ -16,6 +24,8 @@ export const searchOperators = new Set<string>([
1624
'change:',
1725
'is:',
1826
'type:',
27+
'after:',
28+
'before:',
1929
]);
2030

2131
export const searchOperatorsToLongFormMap = new Map<SearchOperators, SearchOperatorsLongForm>([
@@ -32,13 +42,15 @@ export const searchOperatorsToLongFormMap = new Map<SearchOperators, SearchOpera
3242
['change:', 'change:'],
3343
['is:', 'type:'],
3444
['type:', 'type:'],
45+
['after:', 'after:'],
46+
['before:', 'before:'],
3547
]);
3648

3749
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;
50+
/(?:(?<op>=:|message:|@:|author:|#:|commit:|\?:|file:|~:|change:|is:|type:|after:|before:)\s?(?<value>".+?"|\S+}?))|(?<text>\S+)(?!(?:=|message|@|author|#|commit|\?|file|~|change|is|type):)/g;
3951

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

4355
export interface SearchQuery {
4456
query: string;

src/git/search.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ export function getGitArgsFromSearchQuery(
213213
}
214214
}
215215

216+
break;
217+
case 'after:':
218+
for (const value of values) {
219+
const date = new Date(value);
220+
searchArgs.add('--after').add(date.toISOString());
221+
}
222+
break;
223+
case 'before:':
224+
for (const value of values) {
225+
const date = new Date(value);
226+
searchArgs.add('--before').add(date.toISOString());
227+
}
216228
break;
217229
}
218230
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,22 @@ export class GlSearchInput extends GlElement {
462462
this.searchHistoryPos = this.searchHistory.length - 1;
463463
}
464464

465+
private padDate(date: number) {
466+
let stringDate = date.toString();
467+
if (stringDate.length < 2) {
468+
stringDate = `0${stringDate}`;
469+
}
470+
return stringDate;
471+
}
472+
473+
private handleInsertDateToken(tokenPrefix: string) {
474+
const currentDate = new Date();
475+
const year = currentDate.getFullYear();
476+
const month = this.padDate(currentDate.getMonth() + 1);
477+
const date = this.padDate(currentDate.getDate());
478+
this.handleInsertToken(`${tokenPrefix}${year}-${month}-${date}`);
479+
}
480+
465481
override render() {
466482
return html`<gl-popover
467483
class="popover"
@@ -519,6 +535,24 @@ export class GlSearchInput extends GlElement {
519535
Type <small>type:stash or is:stash</small>
520536
</button>
521537
</menu-item>
538+
<menu-item role="none">
539+
<button
540+
class="menu-button"
541+
type="button"
542+
@click="${() => this.handleInsertDateToken('after:')}"
543+
>
544+
Date from <small>after:YYYY-MM-dd</small>
545+
</button>
546+
</menu-item>
547+
<menu-item role="none">
548+
<button
549+
class="menu-button"
550+
type="button"
551+
@click="${() => this.handleInsertDateToken(`before:`)}"
552+
>
553+
Date to <small>before:YYYY-MM-dd</small>
554+
</button>
555+
</menu-item>
522556
</div>
523557
</gl-popover>
524558
<gl-tooltip hoist placement="top" content="Filter Commits">

0 commit comments

Comments
 (0)