11export * as Vcs from "./vcs.js"
22
33import path from "path"
4- import { Context , Effect , Layer , Stream } from "effect"
4+ import { Cause , Context , Effect , Layer , Schema , Stream } from "effect"
5+ import type { VcsDefinition , VcsDraft } from "@opencode-ai/plugin/effect/vcs"
56import { FileDiff } from "@opencode-ai/schema/file-diff"
67import { FileSystem } from "@opencode-ai/schema/filesystem"
78import { BranchList , FileStatus , Info , Mode } from "@opencode-ai/schema/vcs"
@@ -11,8 +12,10 @@ import { FSUtil } from "@opencode-ai/util/fs-util"
1112import { Location } from "./location.js"
1213import { AppProcess } from "@opencode-ai/util/process"
1314import { Bus } from "./bus.js"
15+ import { State } from "./state.js"
1416import { VcsGit } from "./vcs/git.js"
1517import { VcsHg } from "./vcs/hg.js"
18+ import { emptyPatch , MAX_TOTAL_PATCH_BYTES , PATCH_CONTEXT_LINES } from "./vcs/patch.js"
1619
1720export { BranchList , FileStatus , Info , Mode }
1821
@@ -25,13 +28,20 @@ export interface BranchOptions {
2528 readonly limit ?: number
2629}
2730
28- export interface Interface {
31+ export interface Adapter {
2932 readonly info : ( ) => Effect . Effect < Info >
3033 readonly branches : ( options ?: BranchOptions ) => Effect . Effect < BranchList >
3134 readonly status : ( ) => Effect . Effect < FileStatus [ ] >
3235 readonly diff : ( mode : Mode , options ?: DiffOptions ) => Effect . Effect < FileDiff . Info [ ] >
3336}
3437
38+ export interface Interface extends Adapter , State . Transformable < VcsDraft > { }
39+
40+ interface Data {
41+ readonly providers : Map < string , VcsDefinition >
42+ selection ?: string
43+ }
44+
3545export class Service extends Context . Service < Service , Interface > ( ) ( "@opencode/Vcs" ) { }
3646
3747// Adapter seam: one working-copy implementation per VCS type, selected by the
@@ -50,11 +60,59 @@ const layer = Layer.effect(
5060 const fs = yield * FSUtil . Service
5161 const location = yield * Location . Service
5262 const bus = yield * Bus . Service
53- const impl = adapter ( proc , fs , location )
63+ const native = adapter ( proc , fs , location )
5464 const vcs = location . vcs
55- const state = { info : impl ? yield * impl . info ( ) : ( { branch : { } } satisfies Info ) }
65+ const current = { info : native ? yield * native . info ( ) : ( { branch : { } } satisfies Info ) }
66+ const scope = {
67+ directory : location . directory ,
68+ worktree : location . project . directory ,
69+ canonical : location . project . canonical ,
70+ ...( vcs ? { store : vcs . store } : { } ) ,
71+ }
72+ const decodeInfo = Schema . decodeUnknownEffect ( Info )
73+ const decodeBranches = Schema . decodeUnknownEffect ( BranchList )
74+ const decodeStatus = Schema . decodeUnknownEffect ( Schema . Array ( FileStatus ) )
75+ const decodeDiff = Schema . decodeUnknownEffect ( Schema . Array ( FileDiff . Info ) )
76+ const state : State . Interface < Data , VcsDraft > = State . create < Data , VcsDraft > ( {
77+ name : "vcs" ,
78+ initial : ( ) => ( { providers : new Map ( ) } ) ,
79+ draft : ( draft ) => ( {
80+ add : ( provider ) => draft . providers . set ( provider . id , provider ) ,
81+ default : {
82+ get : ( ) => draft . selection ,
83+ set : ( selection ) => ( draft . selection = selection ) ,
84+ } ,
85+ } ) ,
86+ finalize : ( ) => refresh ( ) ,
87+ } )
88+ const selected = ( ) => {
89+ const value = state . get ( )
90+ const id = value . selection ?? vcs ?. type
91+ return id ? value . providers . get ( id ) : undefined
92+ }
93+ const protect = < A > ( provider : VcsDefinition , operation : string , effect : Effect . Effect < A , unknown > , fallback : A ) =>
94+ effect . pipe (
95+ Effect . catchCause ( ( cause ) =>
96+ Cause . hasInterrupts ( cause )
97+ ? Effect . failCause ( cause ) . pipe ( Effect . orDie )
98+ : Effect . logWarning ( "vcs provider failed" , { provider : provider . id , operation, cause } ) . pipe (
99+ Effect . as ( fallback ) ,
100+ ) ,
101+ ) ,
102+ )
103+ const refresh = Effect . fn ( "Vcs.refresh" ) ( function * ( ) {
104+ const provider = selected ( )
105+ const next : Info = provider
106+ ? yield * protect ( provider , "info" , provider . info ( scope ) . pipe ( Effect . flatMap ( decodeInfo ) ) , { branch : { } } )
107+ : native
108+ ? yield * native . info ( )
109+ : { branch : { } }
110+ const changed = current . info . branch . current !== next . branch . current
111+ current . info = next
112+ if ( changed ) yield * bus . publish ( VcsEvent . BranchUpdated , { branch : next . branch . current } )
113+ } )
56114
57- if ( vcs && impl ) {
115+ if ( vcs && native ) {
58116 const store = yield * fs . realPath ( vcs . store ) . pipe ( Effect . orElseSucceed ( ( ) => vcs . store ) )
59117 const isBranchMetadata =
60118 vcs . type === "git"
@@ -63,33 +121,68 @@ const layer = Layer.effect(
63121 yield * bus . subscribe ( FileSystem . Event . Changed ) . pipe (
64122 Stream . filter ( ( event ) => isBranchMetadata ( event . data . file ) ) ,
65123 Stream . runForEach ( ( event ) =>
66- Effect . gen ( function * ( ) {
67- const next = yield * impl . info ( )
68- const changed = state . info . branch . current !== next . branch . current
69- state . info = next
70- if ( ! changed ) return
71- yield * bus . publish ( VcsEvent . BranchUpdated , { branch : next . branch . current } )
72- } ) . pipe ( Effect . withSpan ( "Vcs.refreshBranch" , { attributes : { file : event . data . file } } ) ) ,
124+ refresh ( ) . pipe ( Effect . withSpan ( "Vcs.refreshBranch" , { attributes : { file : event . data . file } } ) ) ,
73125 ) ,
74126 Effect . forkScoped ( { startImmediately : true } ) ,
75127 )
76128 }
77129
78130 return Service . of ( {
131+ transform : state . transform ,
132+ reload : state . reload ,
79133 info : Effect . fn ( "Vcs.info" ) ( function * ( ) {
80- return state . info
134+ return current . info
81135 } ) ,
82136 branches : Effect . fn ( "Vcs.branches" ) ( function * ( options ?: BranchOptions ) {
83- if ( ! impl ) return [ ]
84- return yield * impl . branches ( options )
137+ const provider = selected ( )
138+ if ( provider )
139+ return yield * protect (
140+ provider ,
141+ "branches" ,
142+ provider . branches ( { ...scope , ...options } ) . pipe ( Effect . flatMap ( decodeBranches ) ) ,
143+ [ ] ,
144+ )
145+ if ( ! native ) return [ ]
146+ return yield * native . branches ( options )
85147 } ) ,
86148 status : Effect . fn ( "Vcs.status" ) ( function * ( ) {
87- if ( ! impl ) return [ ]
88- return yield * impl . status ( )
149+ const provider = selected ( )
150+ if ( provider )
151+ return yield * protect (
152+ provider ,
153+ "status" ,
154+ provider . status ( scope ) . pipe (
155+ Effect . flatMap ( decodeStatus ) ,
156+ Effect . map ( ( rows ) => Array . from ( rows ) ) ,
157+ ) ,
158+ [ ] ,
159+ )
160+ if ( ! native ) return [ ]
161+ return yield * native . status ( )
89162 } ) ,
90163 diff : Effect . fn ( "Vcs.diff" ) ( function * ( mode : Mode , options ?: DiffOptions ) {
91- if ( ! impl ) return [ ]
92- return yield * impl . diff ( mode , options )
164+ const provider = selected ( )
165+ if ( ! provider ) return native ? yield * native . diff ( mode , options ) : [ ]
166+ const rows = yield * protect (
167+ provider ,
168+ "diff" ,
169+ provider
170+ . diff ( {
171+ ...scope ,
172+ mode,
173+ context : options ?. context ?? PATCH_CONTEXT_LINES ,
174+ maxOutputBytes : MAX_TOTAL_PATCH_BYTES ,
175+ } )
176+ . pipe ( Effect . flatMap ( decodeDiff ) ) ,
177+ [ ] ,
178+ )
179+ let total = 0
180+ return rows . map ( ( row ) => {
181+ const bytes = Buffer . byteLength ( row . patch )
182+ if ( total + bytes > MAX_TOTAL_PATCH_BYTES ) return { ...row , patch : emptyPatch ( row . file ) }
183+ total += bytes
184+ return row
185+ } )
93186 } ) ,
94187 } )
95188 } ) ,
0 commit comments