Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to search commits by date in commit graph #3777

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/constants.search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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:'
| 'date:'
| 'after:'
| 'before:';
export type SearchOperators = SearchOperatorsShortForm | SearchOperatorsLongForm;

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

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

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:|date:|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:|date:|after:|before:)(?:"[^"]*"?|\w*))(?:$|(\b|\s))/g;

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

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;
}
}
Expand Down
43 changes: 43 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,33 @@ 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('date:')}"
>
Date <small>date:YYYY-MM-dd</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
Loading