@@ -5,7 +5,7 @@ import type { Database } from 'bun:sqlite'
55import type { Repo , CreateRepoInput } from '../types/repo'
66import { logger } from '../utils/logger'
77import { SettingsService } from './settings'
8- import { createGitEnvForRepoUrl , createNoPromptGitEnv , createGitHubGitEnv } from '../utils/git-auth'
8+ import { createGitEnv , createNoPromptGitEnv , createGitHubGitEnv , isGitHubHttpsUrl } from '../utils/git-auth'
99import { getReposPath } from '@opencode-manager/shared/config/env'
1010import path from 'path'
1111
@@ -34,27 +34,36 @@ async function executeGitWithFallback(
3434 options : GitCommandOptions = { }
3535) : Promise < string > {
3636 const { cwd, env = createNoPromptGitEnv ( ) , silent } = options
37-
37+
3838 try {
3939 return await executeCommand ( cmd , { cwd, env, silent } )
4040 } catch ( error : any ) {
4141 if ( ! isAuthenticationError ( error ) ) {
4242 throw error
4343 }
44-
45- logger . warn ( `Git command failed with auth, trying gh auth fallback` )
44+
45+ logger . warn ( `Git command failed with auth, trying CLI fallbacks` )
46+
47+ const url = cmd . find ( arg => arg . includes ( 'http://' ) || arg . includes ( 'https://' ) )
48+ if ( ! url ) {
49+ return await executeCommand ( cmd , { cwd, env : createNoPromptGitEnv ( ) , silent } )
50+ }
51+
4652 try {
47- const ghToken = ( await executeCommand ( [ 'gh' , 'auth' , 'token' ] ) ) . trim ( )
48- const ghEnv = createGitHubGitEnv ( ghToken )
49- return await executeCommand ( cmd , { cwd, env : ghEnv , silent } )
50- } catch ( ghError : any ) {
51- if ( ! isAuthenticationError ( ghError ) ) {
52- throw ghError
53+ if ( isGitHubHttpsUrl ( url ) ) {
54+ logger . warn ( `Detected GitHub URL, trying gh auth token` )
55+ const ghToken = ( await executeCommand ( [ 'gh' , 'auth' , 'token' ] ) ) . trim ( )
56+ const ghEnv = createGitHubGitEnv ( ghToken )
57+ return await executeCommand ( cmd , { cwd, env : ghEnv , silent } )
5358 }
54-
55- logger . warn ( `Git command failed with gh auth, trying without auth (public repo)` )
56- return await executeCommand ( cmd , { cwd, env : createNoPromptGitEnv ( ) , silent } )
59+
60+
61+ } catch ( cliError : any ) {
62+ logger . warn ( `CLI auth fallback failed:` , cliError . message )
5763 }
64+
65+ logger . warn ( `All auth fallbacks failed, trying without auth (public repo)` )
66+ return await executeCommand ( cmd , { cwd, env : createNoPromptGitEnv ( ) , silent } )
5867 }
5968}
6069
@@ -87,17 +96,13 @@ async function safeGetCurrentBranch(repoPath: string): Promise<string | null> {
8796 }
8897}
8998
90- function getGitEnv ( database : Database , repoUrl ?: string | null ) : Record < string , string > {
99+ function getGitEnv ( database : Database ) : Record < string , string > {
91100 try {
92101 const settingsService = new SettingsService ( database )
93102 const settings = settingsService . getSettings ( 'default' )
94- const gitToken = settings . preferences . gitToken
95-
96- if ( ! repoUrl ) {
97- return createNoPromptGitEnv ( )
98- }
103+ const gitCredentials = settings . preferences . gitCredentials || [ ]
99104
100- return createGitEnvForRepoUrl ( repoUrl , gitToken )
105+ return createGitEnv ( gitCredentials )
101106 } catch {
102107 return createNoPromptGitEnv ( )
103108 }
@@ -225,7 +230,7 @@ export async function cloneRepo(
225230 const repo = db . createRepo ( database , createRepoInput )
226231
227232 try {
228- const env = getGitEnv ( database , normalizedRepoUrl )
233+ const env = getGitEnv ( database )
229234
230235 if ( shouldUseWorktree ) {
231236 logger . info ( `Creating worktree for branch: ${ branch } ` )
@@ -408,7 +413,7 @@ export async function getCurrentBranch(repo: Repo): Promise<string | null> {
408413export async function listBranches ( database : Database , repo : Repo ) : Promise < { local : string [ ] , all : string [ ] , current : string | null } > {
409414 try {
410415 const repoPath = path . resolve ( getReposPath ( ) , repo . localPath )
411- const env = getGitEnv ( database , repo . repoUrl )
416+ const env = getGitEnv ( database )
412417
413418 if ( ! repo . isLocal ) {
414419 try {
@@ -457,7 +462,7 @@ export async function switchBranch(database: Database, repoId: number, branch: s
457462
458463 try {
459464 const repoPath = path . resolve ( getReposPath ( ) , repo . localPath )
460- const env = getGitEnv ( database , repo . repoUrl )
465+ const env = getGitEnv ( database )
461466
462467 const sanitizedBranch = branch
463468 . replace ( / ^ r e f s \/ h e a d s \/ / , '' )
@@ -537,7 +542,7 @@ export async function pullRepo(database: Database, repoId: number): Promise<void
537542 }
538543
539544 try {
540- const env = getGitEnv ( database , repo . repoUrl )
545+ const env = getGitEnv ( database )
541546
542547 logger . info ( `Pulling repo: ${ repo . repoUrl } ` )
543548 await executeCommand ( [ 'git' , '-C' , path . resolve ( getReposPath ( ) , repo . localPath ) , 'pull' ] , { env } )
0 commit comments