Visitar URL original
feat(app): show worktree badges in the sidebar by arodu · Pull Request #63553 · anomalyco/opencode · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/app/src/runtime/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ export const dict = {
"sidebar.recent.moveUp": "Move up",
"sidebar.recent.moveDown": "Move down",
"sidebar.project.recentSessions": "Recent sessions",
"sidebar.worktree.local": "Local",
"sidebar.project.clearNotifications": "Clear notifications",

"settings.section.server": "Server",
Expand Down
14 changes: 14 additions & 0 deletions packages/app/src/shell/titlebar/project-session-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function ProjectSessionRow(props: {
active: boolean
/** A faint trailing label, such as the server a session belongs to. */
label?: string
/** A small colored worktree badge, when the project has worktrees. */
worktree?: { label: string; color: string }
/** Extra nesting steps of 14px, for subagent sessions and nested groups. */
depth?: number
/** Shows the project avatar in the leading slot, where no project header names it. */
Expand Down Expand Up @@ -156,6 +158,18 @@ export function ProjectSessionRow(props: {
<span class="min-w-0 flex-1 truncate" dir="auto">
{sessionLabel(props.session)}
</span>
<Show when={props.worktree}>
{(worktree) => (
<span
title={worktree().label}
class="inline-flex max-w-[40%] shrink-0 items-center gap-1 rounded-full px-1.5 text-[10px] leading-[15px] font-medium text-v2-text-text-muted"
style={{ "background-color": `color-mix(in srgb, ${worktree().color} 16%, transparent)` }}
>
<span class="size-1.5 shrink-0 rounded-full" style={{ "background-color": worktree().color }} />
<span class="truncate">{worktree().label}</span>
</span>
)}
</Show>
<Show when={props.label}>
{(label) => (
<span class="max-w-[40%] shrink-0 truncate text-[11px] leading-[var(--line-height-compact)] text-v2-text-text-faint [font-weight:400]">
Expand Down
9 changes: 9 additions & 0 deletions packages/app/src/shell/titlebar/sidebar-customized.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ProjectIcon } from "@/shell/layout/project-icon"
import { createProjectSessionActions } from "@/shell/tabs/project-sessions"
import { tabKey, useTabs, type Tab } from "@/shell/tabs/tabs"
import { ProjectSessionRow } from "./project-session-row"
import { worktreeBadge } from "./sidebar-worktree"
import { createSidebarActivityListener } from "./sidebar-organized"
import { mergeSidebarSessions } from "./sidebar-sessions"
import type { TabStripSection } from "./tab-strip"
Expand Down Expand Up @@ -311,6 +312,13 @@ export function SidebarCustomized(props: {
const projectOptions = () =>
[...projectLabels().values()].map((project) => ({ id: project.id, label: displayName(project) }))

const worktreeOf = (row: Row) =>
worktreeBadge(
row.session?.location?.directory,
row.project ?? (row.projectID ? projectLabels().get(row.projectID) : undefined),
language.t("sidebar.worktree.local"),
)

const showLabel = (row: Row, group: SidebarGroup<Row> | undefined) => {
if (!view.show.serverLabels || !row.conn) return

Expand Down Expand Up @@ -377,6 +385,7 @@ export function SidebarCustomized(props: {
openTab={false}
active={value().row.active}
label={showLabel(value().row, group())}
worktree={worktreeOf(value().row)}
depth={rowProps.flat ? 0 : depthOf(value().row, rowByKey())}
project={avatarOf(value().row, group())}
onOpen={() => openSession(value().row)}
Expand Down
71 changes: 71 additions & 0 deletions packages/app/src/shell/titlebar/sidebar-worktree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, test } from "bun:test"
import type { WorktreeDirectory } from "@opencode/client/promise"
import type { Project } from "@/runtime/server/types"
import { worktreeBadge } from "./sidebar-worktree"

const fixture = (overrides: Partial<Project> = {}) =>
({
id: "fixture",
worktree: "/repo",
sandboxes: [],
worktrees: [],
...overrides,
}) as unknown as Project

const worktree = (directory: string, strategy?: string) => ({ directory, strategy }) as WorktreeDirectory

describe("worktreeBadge", () => {
test("returns nothing for a project without worktrees", () => {
expect(worktreeBadge("/repo", fixture(), "Local")).toBeUndefined()
expect(worktreeBadge(undefined, fixture(), "Local")).toBeUndefined()
expect(worktreeBadge("/repo", undefined, "Local")).toBeUndefined()
})

test("labels the root checkout as local, including subdirectories", () => {
const project = fixture({ sandboxes: ["/repo-qa"] })

expect(worktreeBadge("/repo", project, "Local")?.label).toBe("Local")
expect(worktreeBadge("/repo/packages/app", project, "Local")?.label).toBe("Local")
})

test("labels a worktree by its directory, including subdirectories", () => {
const project = fixture({ sandboxes: ["/repo-qa", "/repo-cake5"] })

expect(worktreeBadge("/repo-qa", project, "Local")?.label).toBe("qa")
expect(worktreeBadge("/repo-qa/src", project, "Local")?.label).toBe("qa")
expect(worktreeBadge("/repo-cake5", project, "Local")?.label).toBe("cake5")
})

test("includes plain git worktrees that are not in the sandbox list", () => {
const project = fixture({ worktrees: [worktree("/repo-plain")] })

expect(worktreeBadge("/repo-plain", project, "Local")?.label).toBe("plain")
})

test("uses the longest containing worktree", () => {
const project = fixture({
sandboxes: ["/repo-qa"],
worktrees: [worktree("/repo-qa/packages", "git")],
})

expect(worktreeBadge("/repo-qa/packages/app", project, "Local")?.label).toBe("packages")
})

test("handles Windows paths", () => {
const project = fixture({
worktree: "C:\\repo",
sandboxes: ["C:\\repo-qa"],
})

expect(worktreeBadge("C:\\repo-qa\\src", project, "Local")?.label).toBe("qa")
})

test("derives the color from the worktree, not the session directory", () => {
const project = fixture({ sandboxes: ["/repo-qa"] })
const root = worktreeBadge("/repo-qa", project, "Local")
const nested = worktreeBadge("/repo-qa/src", project, "Local")

expect(root?.color).toMatch(/^hsl\(/)
expect(nested?.color).toBe(root?.color)
})
})
48 changes: 48 additions & 0 deletions packages/app/src/shell/titlebar/sidebar-worktree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { containsDirectory, getFilename, sameDirectory } from "@opencode/util/path"
import type { Project } from "@/runtime/server/types"
import { managedWorkspaceDirectories, workspaceDirectories } from "@/workspaces/paths"

export type WorktreeBadge = { label: string; color: string }

const hueOf = (value: string) =>
Array.from(value).reduce((hash, character) => (hash * 31 + character.charCodeAt(0)) >>> 0, 0) % 360

/**
* The badge for the worktree that contains the session's directory, or undefined when the project has no worktrees.
* A session started in a subdirectory belongs to the worktree that contains it, so the label and color come from the
* worktree root, not from the session directory.
*/
export function worktreeBadge(
directory: string | undefined,
project: Project | undefined,
local: string,
): WorktreeBadge | undefined {
if (!directory || !project?.worktree) return undefined

const entries = [
project.worktree,
...workspaceDirectories(project),
...(project.worktrees ?? []).map((worktree) => worktree.directory),
...managedWorkspaceDirectories(project),
].filter((entry, index, items) => items.findIndex((item) => sameDirectory(item, entry)) === index)

if (entries.length <= 1) return undefined

const containing = entries
.filter((entry) => containsDirectory(entry, directory))
.toSorted((a, b) => b.length - a.length)[0]

if (!containing) return undefined

const color = `hsl(${hueOf(containing)} 65% 55%)`

if (sameDirectory(containing, project.worktree)) return { label: local, color }

const name = getFilename(containing)
const root = getFilename(project.worktree)

return {
label: root && name.startsWith(`${root}-`) ? name.slice(root.length + 1) : name,
color,
}
}
Loading