@@ -17,6 +17,7 @@ import {
1717 isGitRepo ,
1818 successExitCode
1919} from "./state-repo/git-commands.js"
20+ import type { GitAuthEnv } from "./state-repo/github-auth.js"
2021import { isGithubHttpsRemote , resolveGithubToken , withGithubAskpassEnv } from "./state-repo/github-auth.js"
2122import { ensureStateGitignore } from "./state-repo/gitignore.js"
2223import { runStateSyncOps , runStateSyncWithToken } from "./state-repo/sync-ops.js"
@@ -132,16 +133,18 @@ export const autoSyncState = (message: string): Effect.Effect<void, never, State
132133type StateInitInput = {
133134 readonly repoUrl : string
134135 readonly repoRef : string
136+ readonly token ?: string
135137}
136138
137139const cloneStateRepo = (
138140 root : string ,
139- input : StateInitInput
141+ input : StateInitInput ,
142+ env : GitAuthEnv
140143) : Effect . Effect < void , CommandFailedError | PlatformError , CommandExecutor . CommandExecutor > =>
141144 Effect . gen ( function * ( _ ) {
142145 const cloneWithBranch = [ "clone" , "--branch" , input . repoRef , input . repoUrl , root ]
143146 const cloneBranchExit = yield * _ (
144- runCommandExitCode ( { cwd : root , command : "git" , args : cloneWithBranch , env : gitBaseEnv } )
147+ runCommandExitCode ( { cwd : root , command : "git" , args : cloneWithBranch , env } )
145148 )
146149 if ( cloneBranchExit === successExitCode ) {
147150 return
@@ -156,7 +159,7 @@ const cloneStateRepo = (
156159 )
157160 const cloneDefault = [ "clone" , input . repoUrl , root ]
158161 const cloneDefaultExit = yield * _ (
159- runCommandExitCode ( { cwd : root , command : "git" , args : cloneDefault , env : gitBaseEnv } )
162+ runCommandExitCode ( { cwd : root , command : "git" , args : cloneDefault , env } )
160163 )
161164 if ( cloneDefaultExit !== successExitCode ) {
162165 return yield * _ ( Effect . fail ( new CommandFailedError ( { command : "git clone" , exitCode : cloneDefaultExit } ) ) )
@@ -167,7 +170,8 @@ const initRepoIfNeeded = (
167170 fs : FileSystem . FileSystem ,
168171 path : Path . Path ,
169172 root : string ,
170- input : StateInitInput
173+ input : StateInitInput ,
174+ env : GitAuthEnv
171175) : Effect . Effect < void , CommandFailedError | PlatformError , StateRepoEnv > =>
172176 Effect . gen ( function * ( _ ) {
173177 yield * _ ( fs . makeDirectory ( root , { recursive : true } ) )
@@ -180,32 +184,34 @@ const initRepoIfNeeded = (
180184
181185 const entries = yield * _ ( fs . readDirectory ( root ) )
182186 if ( entries . length === 0 ) {
183- yield * _ ( cloneStateRepo ( root , input ) )
187+ yield * _ ( cloneStateRepo ( root , input , env ) )
184188 yield * _ ( Effect . log ( `State dir cloned: ${ root } ` ) )
185189 return
186190 }
187191
188- yield * _ ( git ( root , [ "init" , "--initial-branch=main" ] , gitBaseEnv ) )
192+ yield * _ ( git ( root , [ "init" , "--initial-branch=main" ] , env ) )
189193 } ) . pipe ( Effect . asVoid )
190194
191195const ensureOriginRemote = (
192196 root : string ,
193- repoUrl : string
197+ repoUrl : string ,
198+ env : GitAuthEnv
194199) : Effect . Effect < void , CommandFailedError | PlatformError , CommandExecutor . CommandExecutor > =>
195200 Effect . gen ( function * ( _ ) {
196- const setUrlExit = yield * _ ( gitExitCode ( root , [ "remote" , "set-url" , "origin" , repoUrl ] , gitBaseEnv ) )
201+ const setUrlExit = yield * _ ( gitExitCode ( root , [ "remote" , "set-url" , "origin" , repoUrl ] , env ) )
197202 if ( setUrlExit === successExitCode ) {
198203 return
199204 }
200- yield * _ ( git ( root , [ "remote" , "add" , "origin" , repoUrl ] , gitBaseEnv ) )
205+ yield * _ ( git ( root , [ "remote" , "add" , "origin" , repoUrl ] , env ) )
201206 } )
202207
203208const checkoutBranchBestEffort = (
204209 root : string ,
205- repoRef : string
210+ repoRef : string ,
211+ env : GitAuthEnv
206212) : Effect . Effect < void , CommandFailedError | PlatformError , CommandExecutor . CommandExecutor > =>
207213 Effect . gen ( function * ( _ ) {
208- const checkoutExit = yield * _ ( gitExitCode ( root , [ "checkout" , "-B" , repoRef ] , gitBaseEnv ) )
214+ const checkoutExit = yield * _ ( gitExitCode ( root , [ "checkout" , "-B" , repoRef ] , env ) )
209215 if ( checkoutExit === successExitCode ) {
210216 return
211217 }
@@ -214,21 +220,28 @@ const checkoutBranchBestEffort = (
214220
215221export const stateInit = (
216222 input : StateInitInput
217- ) : Effect . Effect < void , CommandFailedError | PlatformError , StateRepoEnv > =>
218- Effect . gen ( function * ( _ ) {
219- const fs = yield * _ ( FileSystem . FileSystem )
220- const path = yield * _ ( Path . Path )
221- const root = resolveStateRoot ( path , process . cwd ( ) )
222-
223- yield * _ ( initRepoIfNeeded ( fs , path , root , input ) )
224- yield * _ ( ensureOriginRemote ( root , input . repoUrl ) )
225- yield * _ ( adoptRemoteHistoryIfOrphan ( root , input . repoRef ) )
226- yield * _ ( checkoutBranchBestEffort ( root , input . repoRef ) )
227- yield * _ ( ensureStateGitignore ( fs , path , root ) )
228-
229- yield * _ ( Effect . log ( `State dir ready: ${ root } ` ) )
230- yield * _ ( Effect . log ( `Remote: ${ input . repoUrl } ` ) )
231- } ) . pipe ( Effect . asVoid )
223+ ) : Effect . Effect < void , CommandFailedError | PlatformError , StateRepoEnv > => {
224+ const doInit = ( env : GitAuthEnv ) =>
225+ Effect . gen ( function * ( _ ) {
226+ const fs = yield * _ ( FileSystem . FileSystem )
227+ const path = yield * _ ( Path . Path )
228+ const root = resolveStateRoot ( path , process . cwd ( ) )
229+
230+ yield * _ ( initRepoIfNeeded ( fs , path , root , input , env ) )
231+ yield * _ ( ensureOriginRemote ( root , input . repoUrl , env ) )
232+ yield * _ ( adoptRemoteHistoryIfOrphan ( root , input . repoRef , env ) )
233+ yield * _ ( checkoutBranchBestEffort ( root , input . repoRef , env ) )
234+ yield * _ ( ensureStateGitignore ( fs , path , root ) )
235+
236+ yield * _ ( Effect . log ( `State dir ready: ${ root } ` ) )
237+ yield * _ ( Effect . log ( `Remote: ${ input . repoUrl } ` ) )
238+ } ) . pipe ( Effect . asVoid )
239+
240+ const token = input . token ?. trim ( ) ?? ""
241+ return token . length > 0 && isGithubHttpsRemote ( input . repoUrl )
242+ ? withGithubAskpassEnv ( token , doInit )
243+ : doInit ( gitBaseEnv )
244+ }
232245
233246export const stateStatus = Effect . gen ( function * ( _ ) {
234247 const path = yield * _ ( Path . Path )
0 commit comments