|
| 1 | +import { ok, type Result } from "../result"; |
| 2 | +import type { BookmarkTrackingStatus } from "../types"; |
| 3 | +import { runJJ } from "./runner"; |
| 4 | + |
| 5 | +export async function getBookmarkTracking( |
| 6 | + cwd = process.cwd(), |
| 7 | +): Promise<Result<BookmarkTrackingStatus[]>> { |
| 8 | + // Template to get bookmark name + tracking status from origin |
| 9 | + const template = `if(remote == "origin", name ++ "\\t" ++ tracking_ahead_count.exact() ++ "/" ++ tracking_behind_count.exact() ++ "\\n")`; |
| 10 | + const result = await runJJ(["bookmark", "list", "-T", template], cwd); |
| 11 | + if (!result.ok) return result; |
| 12 | + |
| 13 | + const statuses: BookmarkTrackingStatus[] = []; |
| 14 | + const lines = result.value.stdout.trim().split("\n").filter(Boolean); |
| 15 | + |
| 16 | + for (const line of lines) { |
| 17 | + const parts = line.split("\t"); |
| 18 | + if (parts.length !== 2) continue; |
| 19 | + const [name, counts] = parts; |
| 20 | + const [ahead, behind] = counts.split("/").map(Number); |
| 21 | + if (!Number.isNaN(ahead) && !Number.isNaN(behind)) { |
| 22 | + statuses.push({ name, aheadCount: ahead, behindCount: behind }); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return ok(statuses); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Clean up orphaned bookmarks: |
| 31 | + * 1. Local bookmarks marked as deleted (no target) |
| 32 | + * 2. Local bookmarks without origin pointing to empty changes |
| 33 | + */ |
| 34 | +export async function cleanupOrphanedBookmarks( |
| 35 | + cwd = process.cwd(), |
| 36 | +): Promise<Result<string[]>> { |
| 37 | + // Get all bookmarks with their remote status and target info |
| 38 | + // Format: name\tremote_or_local\thas_target\tis_empty |
| 39 | + const template = |
| 40 | + 'name ++ "\\t" ++ if(remote, remote, "local") ++ "\\t" ++ if(normal_target, "target", "no_target") ++ "\\t" ++ if(normal_target, normal_target.empty(), "") ++ "\\n"'; |
| 41 | + const result = await runJJ( |
| 42 | + ["bookmark", "list", "--all", "-T", template], |
| 43 | + cwd, |
| 44 | + ); |
| 45 | + if (!result.ok) return result; |
| 46 | + |
| 47 | + // Parse bookmarks and group by name |
| 48 | + const bookmarksByName = new Map< |
| 49 | + string, |
| 50 | + { hasOrigin: boolean; hasLocalTarget: boolean; isEmpty: boolean } |
| 51 | + >(); |
| 52 | + |
| 53 | + for (const line of result.value.stdout.trim().split("\n")) { |
| 54 | + if (!line) continue; |
| 55 | + const [name, remote, hasTarget, isEmpty] = line.split("\t"); |
| 56 | + if (!name) continue; |
| 57 | + |
| 58 | + const existing = bookmarksByName.get(name); |
| 59 | + if (remote === "origin") { |
| 60 | + if (existing) { |
| 61 | + existing.hasOrigin = true; |
| 62 | + } else { |
| 63 | + bookmarksByName.set(name, { |
| 64 | + hasOrigin: true, |
| 65 | + hasLocalTarget: false, |
| 66 | + isEmpty: false, |
| 67 | + }); |
| 68 | + } |
| 69 | + } else if (remote === "local") { |
| 70 | + const localHasTarget = hasTarget === "target"; |
| 71 | + const localIsEmpty = isEmpty === "true"; |
| 72 | + if (existing) { |
| 73 | + existing.hasLocalTarget = localHasTarget; |
| 74 | + existing.isEmpty = localIsEmpty; |
| 75 | + } else { |
| 76 | + bookmarksByName.set(name, { |
| 77 | + hasOrigin: false, |
| 78 | + hasLocalTarget: localHasTarget, |
| 79 | + isEmpty: localIsEmpty, |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Find bookmarks to forget: |
| 86 | + // 1. Deleted bookmarks (local has no target) - these show as "(deleted)" |
| 87 | + // 2. Orphaned bookmarks (no origin AND empty change) |
| 88 | + const forgotten: string[] = []; |
| 89 | + for (const [name, info] of bookmarksByName) { |
| 90 | + const isDeleted = !info.hasLocalTarget; |
| 91 | + const isOrphaned = !info.hasOrigin && info.isEmpty; |
| 92 | + |
| 93 | + if (isDeleted || isOrphaned) { |
| 94 | + const forgetResult = await runJJ(["bookmark", "forget", name], cwd); |
| 95 | + if (forgetResult.ok) { |
| 96 | + forgotten.push(name); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return ok(forgotten); |
| 102 | +} |
0 commit comments