Add a pattern to the git BranchQuery (#99681)

* Add a pattern to the git BranchQuery

* Add count, which makes getting refs even faster

* Update order of args in getRefs to match for-each-ref doc
This commit is contained in:
Alex Ross
2020-06-10 13:51:00 +02:00
committed by GitHub
parent 12b634ec05
commit 92164dd93d
2 changed files with 15 additions and 3 deletions

View File

@@ -1791,13 +1791,23 @@ export class Repository {
.map(([ref]) => ({ name: ref, type: RefType.Head } as Branch));
}
async getRefs(opts?: { sort?: 'alphabetically' | 'committerdate', contains?: string }): Promise<Ref[]> {
const args = ['for-each-ref', '--format', '%(refname) %(objectname)'];
async getRefs(opts?: { sort?: 'alphabetically' | 'committerdate', contains?: string, pattern?: string, count?: number }): Promise<Ref[]> {
const args = ['for-each-ref'];
if (opts?.count) {
args.push(`--count=${opts.count}`);
}
if (opts && opts.sort && opts.sort !== 'alphabetically') {
args.push('--sort', `-${opts.sort}`);
}
args.push('--format', '%(refname) %(objectname)');
if (opts?.pattern) {
args.push(opts.pattern);
}
if (opts?.contains) {
args.push('--contains', opts.contains);
}
@@ -1920,7 +1930,7 @@ export class Repository {
}
async getBranches(query: BranchQuery): Promise<Ref[]> {
const refs = await this.getRefs({ contains: query.contains });
const refs = await this.getRefs({ contains: query.contains, pattern: query.pattern ? `refs/${query.pattern}` : undefined, count: query.count });
return refs.filter(value => (value.type !== RefType.Tag) && (query.remote || !value.remote));
}