Skip to content
File

Blob: src/shared/git/ref-display.ts

typescript43 lines
1export type RefKind = "branch" | "tag" | "other";
2 
3export type RefLike = {
4 name: string;
5};
6 
7export function shortRefName(refName: string): string {
8 return refName.replace(/^refs\/(heads|tags)\//, "");
9}
10 
11export function classifyRef(refName: string): RefKind {
12 if (refName.startsWith("refs/heads/")) return "branch";
13 if (refName.startsWith("refs/tags/")) return "tag";
14 return "other";
15}
16 
17export function formatRefOption(ref: RefLike): { name: string; displayName: string } {
18 const short = shortRefName(ref.name);
19 return {
20 name: encodeURIComponent(short),
21 displayName: short.length > 30 ? `${short.slice(0, 27)}...` : short,
22 };
23}
24 
25export function countRefsByKind(refs: readonly RefLike[]): {
26 branchCount: number;
27 tagCount: number;
28 otherCount: number;
29} {
30 let branchCount = 0;
31 let tagCount = 0;
32 let otherCount = 0;
33 
34 for (const ref of refs) {
35 const kind = classifyRef(ref.name);
36 if (kind === "branch") branchCount++;
37 else if (kind === "tag") tagCount++;
38 else otherCount++;
39 }
40 
41 return { branchCount, tagCount, otherCount };
42}