Skip to content

Commit 804f18c

Browse files
authored
Git - ref pickers should use commitShortHashLength setting (microsoft#249869)
1 parent 116f1f7 commit 804f18c

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

extensions/git/src/commands.ts

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ class RefItem implements QuickPickItem {
9999

100100
get refName(): string | undefined { return this.ref.name; }
101101
get refRemote(): string | undefined { return this.ref.remote; }
102-
get shortCommit(): string { return (this.ref.commit || '').substr(0, 8); }
102+
get shortCommit(): string { return (this.ref.commit || '').substring(0, this.shortCommitLength); }
103103

104104
private _buttons?: QuickInputButton[];
105105
get buttons(): QuickInputButton[] | undefined { return this._buttons; }
106106
set buttons(newButtons: QuickInputButton[] | undefined) { this._buttons = newButtons; }
107107

108-
constructor(protected readonly ref: Ref) { }
108+
constructor(protected readonly ref: Ref, private readonly shortCommitLength: number) { }
109109
}
110110

111111
class BranchItem extends RefItem {
@@ -122,8 +122,8 @@ class BranchItem extends RefItem {
122122
return description.length > 0 ? description.join('$(circle-small-filled)') : this.shortCommit;
123123
}
124124

125-
constructor(override readonly ref: Branch) {
126-
super(ref);
125+
constructor(override readonly ref: Branch, shortCommitLength: number) {
126+
super(ref, shortCommitLength);
127127
}
128128
}
129129

@@ -242,10 +242,10 @@ class RebaseUpstreamItem extends RebaseItem {
242242

243243
class HEADItem implements QuickPickItem {
244244

245-
constructor(private repository: Repository) { }
245+
constructor(private repository: Repository, private readonly shortCommitLength: number) { }
246246

247247
get label(): string { return 'HEAD'; }
248-
get description(): string { return (this.repository.HEAD && this.repository.HEAD.commit || '').substr(0, 8); }
248+
get description(): string { return (this.repository.HEAD?.commit ?? '').substring(0, this.shortCommitLength); }
249249
get alwaysShow(): boolean { return true; }
250250
get refName(): string { return 'HEAD'; }
251251
}
@@ -413,12 +413,7 @@ async function getRemoteRefItemButtons(repository: Repository) {
413413
class RefProcessor {
414414
protected readonly refs: Ref[] = [];
415415

416-
get items(): QuickPickItem[] {
417-
const items = this.refs.map(r => new this.ctor(r));
418-
return items.length === 0 ? items : [new RefItemSeparator(this.type), ...items];
419-
}
420-
421-
constructor(protected readonly type: RefType, protected readonly ctor: { new(ref: Ref): QuickPickItem } = RefItem) { }
416+
constructor(protected readonly type: RefType, protected readonly ctor: { new(ref: Ref, shortCommitLength: number): QuickPickItem } = RefItem) { }
422417

423418
processRef(ref: Ref): boolean {
424419
if (!ref.name && !ref.commit) {
@@ -431,9 +426,15 @@ class RefProcessor {
431426
this.refs.push(ref);
432427
return true;
433428
}
429+
430+
getItems(shortCommitLength: number): QuickPickItem[] {
431+
const items = this.refs.map(r => new this.ctor(r, shortCommitLength));
432+
return items.length === 0 ? items : [new RefItemSeparator(this.type), ...items];
433+
}
434434
}
435435

436436
class RefItemsProcessor {
437+
protected readonly shortCommitLength: number;
437438

438439
constructor(
439440
protected readonly repository: Repository,
@@ -442,7 +443,10 @@ class RefItemsProcessor {
442443
skipCurrentBranch?: boolean;
443444
skipCurrentBranchRemote?: boolean;
444445
} = {}
445-
) { }
446+
) {
447+
const config = workspace.getConfiguration('git', Uri.file(repository.root));
448+
this.shortCommitLength = config.get<number>('commitShortHashLength', 7);
449+
}
446450

447451
processRefs(refs: Ref[]): QuickPickItem[] {
448452
const refsToSkip = this.getRefsToSkip();
@@ -460,7 +464,7 @@ class RefItemsProcessor {
460464

461465
const result: QuickPickItem[] = [];
462466
for (const processor of this.processors) {
463-
result.push(...processor.items);
467+
result.push(...processor.getItems(this.shortCommitLength));
464468
}
465469

466470
return result;
@@ -483,19 +487,19 @@ class RefItemsProcessor {
483487

484488
class CheckoutRefProcessor extends RefProcessor {
485489

486-
override get items(): QuickPickItem[] {
490+
constructor(private readonly repository: Repository) {
491+
super(RefType.Head);
492+
}
493+
494+
override getItems(shortCommitLength: number): QuickPickItem[] {
487495
const items = this.refs.map(ref => {
488496
return this.repository.isBranchProtected(ref) ?
489-
new CheckoutProtectedItem(ref) :
490-
new CheckoutItem(ref);
497+
new CheckoutProtectedItem(ref, shortCommitLength) :
498+
new CheckoutItem(ref, shortCommitLength);
491499
});
492500

493501
return items.length === 0 ? items : [new RefItemSeparator(this.type), ...items];
494502
}
495-
496-
constructor(private readonly repository: Repository) {
497-
super(RefType.Head);
498-
}
499503
}
500504

501505
class CheckoutItemsProcessor extends RefItemsProcessor {
@@ -532,7 +536,7 @@ class CheckoutItemsProcessor extends RefItemsProcessor {
532536

533537
const result: QuickPickItem[] = [];
534538
for (const processor of this.processors) {
535-
for (const item of processor.items) {
539+
for (const item of processor.getItems(this.shortCommitLength)) {
536540
if (!(item instanceof RefItem)) {
537541
result.push(item);
538542
continue;
@@ -3011,6 +3015,7 @@ export class CommandCenter {
30113015

30123016
const config = workspace.getConfiguration('git');
30133017
const showRefDetails = config.get<boolean>('showReferenceDetails') === true;
3018+
const commitShortHashLength = config.get<number>('commitShortHashLength') ?? 7;
30143019

30153020
if (from) {
30163021
const getRefPicks = async () => {
@@ -3021,7 +3026,7 @@ export class CommandCenter {
30213026
new RefProcessor(RefType.Tag)
30223027
]);
30233028

3024-
return [new HEADItem(repository), ...refProcessors.processRefs(refs)];
3029+
return [new HEADItem(repository, commitShortHashLength), ...refProcessors.processRefs(refs)];
30253030
};
30263031

30273032
const placeHolder = l10n.t('Select a ref to create the branch from');
@@ -3233,6 +3238,7 @@ export class CommandCenter {
32333238
async rebase(repository: Repository): Promise<void> {
32343239
const config = workspace.getConfiguration('git');
32353240
const showRefDetails = config.get<boolean>('showReferenceDetails') === true;
3241+
const commitShortHashLength = config.get<number>('commitShortHashLength') ?? 7;
32363242

32373243
const getQuickPickItems = async (): Promise<QuickPickItem[]> => {
32383244
const refs = await repository.getRefs({ includeCommitDetails: showRefDetails });
@@ -3251,7 +3257,7 @@ export class CommandCenter {
32513257
ref.name === `${repository.HEAD!.upstream!.remote}/${repository.HEAD!.upstream!.name}`);
32523258

32533259
if (upstreamRef) {
3254-
quickPickItems.splice(0, 0, new RebaseUpstreamItem(upstreamRef));
3260+
quickPickItems.splice(0, 0, new RebaseUpstreamItem(upstreamRef, commitShortHashLength));
32553261
}
32563262
}
32573263

@@ -3292,10 +3298,13 @@ export class CommandCenter {
32923298
async deleteTag(repository: Repository): Promise<void> {
32933299
const config = workspace.getConfiguration('git');
32943300
const showRefDetails = config.get<boolean>('showReferenceDetails') === true;
3301+
const commitShortHashLength = config.get<number>('commitShortHashLength') ?? 7;
32953302

32963303
const tagPicks = async (): Promise<TagDeleteItem[] | QuickPickItem[]> => {
32973304
const remoteTags = await repository.getRefs({ pattern: 'refs/tags', includeCommitDetails: showRefDetails });
3298-
return remoteTags.length === 0 ? [{ label: l10n.t('$(info) This repository has no tags.') }] : remoteTags.map(ref => new TagDeleteItem(ref));
3305+
return remoteTags.length === 0
3306+
? [{ label: l10n.t('$(info) This repository has no tags.') }]
3307+
: remoteTags.map(ref => new TagDeleteItem(ref, commitShortHashLength));
32993308
};
33003309

33013310
const placeHolder = l10n.t('Select a tag to delete');
@@ -3318,6 +3327,9 @@ export class CommandCenter {
33183327

33193328
@command('git.deleteRemoteTag', { repository: true })
33203329
async deleteRemoteTag(repository: Repository): Promise<void> {
3330+
const config = workspace.getConfiguration('git');
3331+
const commitShortHashLength = config.get<number>('commitShortHashLength') ?? 7;
3332+
33213333
const remotePicks = repository.remotes
33223334
.filter(r => r.pushUrl !== undefined)
33233335
.map(r => new RemoteItem(repository, r));
@@ -3354,7 +3366,9 @@ export class CommandCenter {
33543366
}
33553367
}
33563368

3357-
return remoteTags.length === 0 ? [{ label: l10n.t('$(info) Remote "{0}" has no tags.', remoteName) }] : remoteTags.map(ref => new RemoteTagDeleteItem(ref));
3369+
return remoteTags.length === 0
3370+
? [{ label: l10n.t('$(info) Remote "{0}" has no tags.', remoteName) }]
3371+
: remoteTags.map(ref => new RemoteTagDeleteItem(ref, commitShortHashLength));
33583372
};
33593373

33603374
const tagPickPlaceholder = l10n.t('Select a remote tag to delete');
@@ -3442,6 +3456,9 @@ export class CommandCenter {
34423456

34433457
@command('git.pullFrom', { repository: true })
34443458
async pullFrom(repository: Repository): Promise<void> {
3459+
const config = workspace.getConfiguration('git');
3460+
const commitShortHashLength = config.get<number>('commitShortHashLength') ?? 7;
3461+
34453462
const remotes = repository.remotes;
34463463

34473464
if (remotes.length === 0) {
@@ -3464,7 +3481,7 @@ export class CommandCenter {
34643481

34653482
const getBranchPicks = async (): Promise<RefItem[]> => {
34663483
const remoteRefs = await repository.getRefs({ pattern: `refs/remotes/${remoteName}/` });
3467-
return remoteRefs.map(r => new RefItem(r));
3484+
return remoteRefs.map(r => new RefItem(r, commitShortHashLength));
34683485
};
34693486

34703487
const branchPlaceHolder = l10n.t('Pick a branch to pull from');

0 commit comments

Comments
 (0)