Skip to content

Commit

Permalink
feat: add pagination to checkout to command
Browse files Browse the repository at this point in the history
  • Loading branch information
conwnet committed May 25, 2022
1 parent 9734dd5 commit f769d39
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
6 changes: 5 additions & 1 deletion extensions/github1s/src/adapters/github1s/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ export class GitHub1sDataSource extends DataSource {
const { owner, repo } = parseRepoFullName(repoFullName);
const requestParams = { owner, repo, ref };
const { data } = await fetcher.request('GET /repos/{owner}/{repo}/git/matching-refs/{ref}', requestParams);
return data.map((item) => ({ name: item.ref.slice(ref === 'heads' ? 11 : 10), commitSha: item.object.sha }));
return data.map((item) => ({
name: item.ref.slice(ref === 'heads' ? 11 : 10),
commitSha: item.object.sha,
description: `${ref === 'heads' ? 'Branch' : 'Tag'} at ${item.object.sha.slice(0, 8)}`,
}));
}
);

Expand Down
2 changes: 2 additions & 0 deletions extensions/github1s/src/adapters/sourcegraph/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ export const getAllRefs = async (repository: string): Promise<{ branches: Branch
const branches = (repositoryData.branches?.nodes || []).map?.((branch) => ({
name: branch.displayName,
commitSha: branch.target?.commit?.oid,
description: `Branch at ${branch.target?.commit?.oid?.slice(0, 8)}`,
}));
const tags = (repositoryData.tags?.nodes || []).map?.((tag) => ({
name: tag?.displayName,
commitSha: tag?.target?.commit?.oid,
description: `Tag at ${tag?.target?.commit?.oid?.slice(0, 8)}`,
}));
return { branches, tags };
};
6 changes: 4 additions & 2 deletions extensions/github1s/src/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export interface SymbolicLink {

export interface Branch {
name: string;
commitSha: string;
commitSha?: string;
description?: string;
}

export interface Tag {
name: string;
commitSha: string;
commitSha?: string;
description?: string;
}

export type CommitsQueryOptions = { from?: string; author?: string; path?: string } & CommonQueryOptions;
Expand Down
58 changes: 36 additions & 22 deletions extensions/github1s/src/commands/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,52 @@ import router from '@/router';
import { adapterManager } from '@/adapters';
import { Repository } from '@/repository';

const loadMorePickerItem: vscode.QuickPickItem = {
label: '$(more) Load More',
alwaysShow: true,
};

const checkoutToItem: vscode.QuickPickItem = {
label: '$(debug-disconnect) Checkout detached',
alwaysShow: true,
};

// check out to branch/tag/commit
const commandCheckoutTo = async () => {
const routerParser = await router.resolveParser();
const routeState = await router.getState();

const scheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(scheme, routeState.repo);
const [branchRefs, tagRefs] = await Promise.all([repository.getBranchList(), repository.getTagList()]);
const branchPickerItems: vscode.QuickPickItem[] = branchRefs.map((branchRef) => ({
label: branchRef.name,
description: (branchRef.commitSha || '').slice(0, 8),
}));
const tagPickerItems: vscode.QuickPickItem[] = tagRefs.map((tagRef) => ({
label: tagRef.name,
description: `Tag at ${(tagRef.commitSha || '').slice(0, 8)}`,
}));

const quickPick = vscode.window.createQuickPick();
quickPick.placeholder = routeState.ref;
quickPick.items = [...branchPickerItems, ...tagPickerItems];

const loadMoreRefPickerItems = async () => {
quickPick.busy = true;
const scheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(scheme, routeState.repo);
await Promise.all([repository.loadMoreBranches(), repository.loadMoreTags()]);
const [branchRefs, tagRefs] = await Promise.all([repository.getBranchList(), repository.getTagList()]);
const refPickerItems = [...branchRefs, ...tagRefs].map((ref) => ({
label: ref.name,
description: ref.description,
}));
const hasMore = (await Promise.all([repository.hasMoreBranches(), repository.hasMoreTags()])).some(Boolean);
quickPick.items = [...refPickerItems, hasMore ? loadMorePickerItem : null!, checkoutToItem].filter(Boolean);
quickPick.busy = false;
};

quickPick.placeholder = 'Input a ref to checkout';
quickPick.items = [checkoutToItem];
loadMoreRefPickerItems();
quickPick.show();
const choice = await new Promise<vscode.QuickPickItem | undefined>((resolve) =>
quickPick.onDidAccept(() => resolve(quickPick.activeItems[0]))
);
quickPick.hide();

const selectedRef = choice?.label || quickPick.value;
if (selectedRef) {
quickPick.onDidAccept(async () => {
const choice = quickPick.activeItems[0];
if (choice === loadMorePickerItem) {
return loadMoreRefPickerItems();
}
const selectedRef = choice === checkoutToItem ? quickPick.value : choice?.label;
const targetRef = selectedRef.toUpperCase() !== 'HEAD' ? selectedRef : undefined;
router.push(await routerParser.buildTreePath(routeState.repo, targetRef));
}
quickPick.hide();
});
};

export const registerRefCommands = (context: vscode.ExtensionContext) => {
Expand Down

0 comments on commit f769d39

Please sign in to comment.