Skip to content

Commit

Permalink
Adds ability to search commits by date in commit graph
Browse files Browse the repository at this point in the history
  • Loading branch information
nzaytsev committed Jan 15, 2025
1 parent 9bbeecb commit 30763be
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/constants.search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
type SearchOperatorsShortForm = '' | '=:' | '@:' | '#:' | '?:' | '~:' | 'is:';
export type SearchOperatorsLongForm = 'message:' | 'author:' | 'commit:' | 'file:' | 'change:' | 'type:';
type SearchOperatorsShortForm = '' | '=:' | '@:' | '#:' | '?:' | '~:' | 'is:' | 'after:' | 'before:';
export type SearchOperatorsLongForm =
| 'message:'
| 'author:'
| 'commit:'
| 'file:'
| 'change:'
| 'type:'
| 'after:'
| 'before:';
export type SearchOperators = SearchOperatorsShortForm | SearchOperatorsLongForm;

export const searchOperators = new Set<string>([
Expand All @@ -16,6 +24,8 @@ export const searchOperators = new Set<string>([
'change:',
'is:',
'type:',
'after:',
'before:',
]);

export const searchOperatorsToLongFormMap = new Map<SearchOperators, SearchOperatorsLongForm>([
Expand All @@ -32,13 +42,15 @@ export const searchOperatorsToLongFormMap = new Map<SearchOperators, SearchOpera
['change:', 'change:'],
['is:', 'type:'],
['type:', 'type:'],
['after:', 'after:'],
['before:', 'before:'],
]);

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

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

export interface SearchQuery {
query: string;
Expand Down
12 changes: 12 additions & 0 deletions src/git/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ export function getGitArgsFromSearchQuery(
}
}

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;
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/webviews/apps/shared/components/search/search-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,22 @@ export class GlSearchInput extends GlElement {
this.searchHistoryPos = this.searchHistory.length - 1;
}

private padDate(date: number) {
let stringDate = date.toString();
if (stringDate.length < 2) {
stringDate = `0${stringDate}`;
}
return stringDate;
}

private handleInsertDateToken(tokenPrefix: string) {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = this.padDate(currentDate.getMonth() + 1);
const date = this.padDate(currentDate.getDate());
this.handleInsertToken(`${tokenPrefix}${year}-${month}-${date}`);
}

override render() {
return html`<div class="field">
<div class="controls controls__start">
Expand Down Expand Up @@ -486,6 +502,24 @@ export class GlSearchInput extends GlElement {
Type <small>type:stash or is:stash</small>
</button>
</menu-item>
<menu-item role="none">
<button
class="menu-button"
type="button"
@click="${() => this.handleInsertDateToken('after:')}"
>
Date from <small>after:YYYY-MM-dd</small>
</button>
</menu-item>
<menu-item role="none">
<button
class="menu-button"
type="button"
@click="${() => this.handleInsertDateToken(`before:`)}"
>
Date to <small>before:YYYY-MM-dd</small>
</button>
</menu-item>
</div>
</gl-popover>
</div>
Expand Down

0 comments on commit 30763be

Please sign in to comment.