Skip to content

Commit 91f5c20

Browse files
authored
feat: respect vscode git.checkoutType setting (#43)
1 parent b6654a2 commit 91f5c20

1 file changed

Lines changed: 49 additions & 8 deletions

File tree

src/core/quickPick/pickBranch.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type ResolveValue = IPickBranchResolveValue;
1818
type ResolveType = (value: ResolveValue) => void;
1919
type RejectType = (value?: any) => void;
2020

21+
type CheckoutType = 'local' | 'remote' | 'tags';
22+
2123
interface HandlerArgs {
2224
resolve: ResolveType;
2325
reject: RejectType;
@@ -173,6 +175,33 @@ const getRefList = async (cwd?: string) => {
173175
return mapRefList(allRefList);
174176
};
175177

178+
/**
179+
* Get normalized "git.checkoutType" setting.
180+
* @see https://github.com/microsoft/vscode/blob/1.107.1/extensions/git/src/commands.ts#L392
181+
*/
182+
const getCheckoutTypes = () => {
183+
const defaultCheckoutTypes: CheckoutType[] = ['local', 'remote', 'tags'];
184+
const config = vscode.workspace.getConfiguration('git');
185+
const checkoutTypeConfig = config.get<string | string[]>('checkoutType');
186+
187+
let checkoutTypes: string[];
188+
189+
if (checkoutTypeConfig === 'all' || !checkoutTypeConfig || !checkoutTypeConfig.length) {
190+
checkoutTypes = defaultCheckoutTypes;
191+
} else if (typeof checkoutTypeConfig === 'string') {
192+
checkoutTypes = [checkoutTypeConfig];
193+
} else {
194+
checkoutTypes = checkoutTypeConfig;
195+
}
196+
197+
checkoutTypes = checkoutTypes.filter((type) => defaultCheckoutTypes.includes(type as CheckoutType));
198+
if (!checkoutTypes.length) {
199+
return defaultCheckoutTypes;
200+
}
201+
202+
return checkoutTypes as CheckoutType[];
203+
};
204+
176205
const buildBranchDesc = (hash: string, authordate: string) =>
177206
`$(git-commit) ${hash} $(circle-small-filled) ${formatTime(authordate)}`;
178207
const buildWorktreeBranchDesc = (hash: string, authordate: string) =>
@@ -325,12 +354,14 @@ const mapRefItems = ({
325354
tagList,
326355
showCreate,
327356
mainFolder,
357+
checkoutTypes,
328358
}: {
329359
branchList: RefList;
330360
remoteBranchList: RefList;
331361
tagList: RefList;
332362
showCreate: boolean;
333363
mainFolder: string;
364+
checkoutTypes: CheckoutType[];
334365
}) => {
335366
let defaultBranch: RefItem | undefined = void 0;
336367
let branchItems: RefList = [];
@@ -340,13 +371,22 @@ const mapRefItems = ({
340371
if (item.worktreepath) worktreeItems.push(item);
341372
else branchItems.push(item);
342373
});
343-
return [
344-
...getPreItems(showCreate),
345-
...mapWorktreeBranchItems(worktreeItems, mainFolder, defaultBranch),
346-
...mapBranchItems(branchItems, mainFolder),
347-
...mapRemoteBranchItems(remoteBranchList),
348-
...mapTagItems(tagList),
349-
];
374+
375+
const items = [...getPreItems(showCreate), ...mapWorktreeBranchItems(worktreeItems, mainFolder, defaultBranch)];
376+
const checkoutTypeItemsGetters = {
377+
local: () => mapBranchItems(branchItems, mainFolder),
378+
remote: () => mapRemoteBranchItems(remoteBranchList),
379+
tags: () => mapTagItems(tagList),
380+
};
381+
// 根据配置的 checkoutType 顺序添加对应的 items
382+
checkoutTypes.forEach((type) => {
383+
const getter = checkoutTypeItemsGetters[type];
384+
if (getter) {
385+
items.push(...getter());
386+
}
387+
});
388+
389+
return items;
350390
};
351391

352392
const getRefListCache = async (mainFolder: string, cwd: string) => {
@@ -394,7 +434,8 @@ const updateQuickItems = async ({
394434
}) => {
395435
// Read cache
396436
const refList = await getRefListCache(mainFolder, cwd);
397-
if (refList) quickPick.items = mapRefItems({ ...refList, showCreate, mainFolder });
437+
const checkoutTypes = getCheckoutTypes();
438+
if (refList) quickPick.items = mapRefItems({ ...refList, showCreate, mainFolder, checkoutTypes });
398439
};
399440

400441
export const pickBranch: IPickBranch = async ({

0 commit comments

Comments
 (0)