Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146/** * The repository's references, as the sidebar sees them. */
import { createSignal } from "solid-js";
import { listReferences } from "./api";import { isGigitError, type RefEntry, type RepositoryId } from "./bindings";
/** Refs split into the groups the sidebar shows. */export type GroupedRefs = { branches: RefEntry[]; remotes: RefEntry[]; tags: RefEntry[];};
/** * Sorts names the way a person reads them, so `v9` comes before `v10` rather * than after it. */const BY_NAME = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
/** * Split refs by kind and sort each group. * * `other` refs — notes, `refs/stash`, replacements — are deliberately dropped. * They are machinery rather than things a person navigates by, and showing * them would bury the branches. */export function groupRefs(entries: RefEntry[]): GroupedRefs { const sorted = (kind: RefEntry["kind"]) => entries .filter((entry) => entry.kind === kind) // Sorting in place is safe here: filter already returned a fresh array, // so there is nothing of the caller's to mutate. // oxlint-disable-next-line unicorn/no-array-sort .sort((left, right) => BY_NAME.compare(left.shorthand, right.shorthand));
return { branches: sorted("localBranch"), remotes: sorted("remoteBranch"), tags: sorted("tag"), };}
/** * Keep the refs whose shorthand contains `query`, ignoring case. * * A plain substring match rather than anything fuzzy: it is predictable, and a * person can see why a result appeared without having to learn the rules. The * shorthand is what the sidebar shows, so it is what gets matched — searching * text that is not on screen would be a surprise. */export function filterRefs(entries: RefEntry[], query: string): RefEntry[] { const wanted = query.trim().toLowerCase(); if (!wanted) return entries;
return entries.filter((entry) => entry.shorthand.toLowerCase().includes(wanted));}
/** A run of text, and whether the filter matched it. */export type Segment = { text: string; matched: boolean };
/** * Split text into matched and unmatched runs, so the matches can be marked. * * Done by scanning rather than with a regular expression, which would mean * escaping whatever the user typed — a branch name can contain `.` and `+`, * and `*` would otherwise throw. */export function highlight(text: string, query: string): Segment[] { const wanted = query.trim().toLowerCase(); if (!wanted) return [{ text, matched: false }];
const haystack = text.toLowerCase(); const segments: Segment[] = []; let cursor = 0;
for (;;) { const found = haystack.indexOf(wanted, cursor); if (found === -1) break;
if (found > cursor) segments.push({ text: text.slice(cursor, found), matched: false }); segments.push({ text: text.slice(found, found + wanted.length), matched: true }); cursor = found + wanted.length; }
if (cursor < text.length) segments.push({ text: text.slice(cursor), matched: false });
return segments;}
export function createRefs() { const [entries, setEntries] = createSignal<RefEntry[]>([]); const [query, setQuery] = createSignal(""); const [message, setMessage] = createSignal<string | undefined>();
const matching = () => filterRefs(entries(), query());
/** Load a repository's refs, replacing whatever was shown before. */ async function show(id: RepositoryId) { setMessage(undefined); // A filter written for one repository almost never means anything in the // next, and leaving it on would make a full sidebar look empty. setQuery("");
try { setEntries(await listReferences(id)); } catch (error) { // The sidebar failing is not a reason to lose the history beside it. setEntries([]); setMessage(isGigitError(error) ? error.message : String(error)); } }
function clear() { setEntries([]); setQuery(""); setMessage(undefined); }
return { entries, query, setQuery, /** Grouped and sorted, with the filter applied. */ grouped: () => groupRefs(matching()), /** How many refs the filter kept, of how many there are worth showing. */ counts: () => { const shown = groupRefs(matching()); const all = groupRefs(entries());
return { matched: shown.branches.length + shown.remotes.length + shown.tags.length, total: all.branches.length + all.remotes.length + all.tags.length, }; }, isFiltering: () => query().trim().length > 0, message, show, clear, };}
export type Refs = ReturnType<typeof createRefs>;