mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat: auto-create remote branches on push (#91)
This commit is contained in:
1 parent
eccd49e877
commit
c32819fc1c
5 files changed
+173
-21
No files matched your search
@@ -1,11 +1,16 @@
|
||||
import { executeCommand } from '../../utils/process'
|
||||
import { GitAuthService } from '../git-auth'
|
||||
import { GitBranchService } from './GitBranchService'
|
||||
import { isNoUpstreamError, parseBranchNameFromError } from '../../utils/git-errors'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import * as db from '../../db/queries'
|
||||
import path from 'path'
|
||||
|
||||
export class GitPushService {
|
||||
constructor(private gitAuthService: GitAuthService) {}
|
||||
constructor(
|
||||
private gitAuthService: GitAuthService,
|
||||
private branchService: GitBranchService
|
||||
) {}
|
||||
|
||||
async push(
|
||||
repoId: number,
|
||||
@@ -18,13 +23,48 @@ export class GitPushService {
|
||||
}
|
||||
|
||||
const fullPath = path.resolve(repo.fullPath)
|
||||
const args = ['git', '-C', fullPath, 'push']
|
||||
const env = this.gitAuthService.getGitEnvironment()
|
||||
|
||||
if (options.setUpstream) {
|
||||
args.push('--set-upstream', 'origin', 'HEAD')
|
||||
return await this.pushWithUpstream(repoId, fullPath, env)
|
||||
}
|
||||
|
||||
try {
|
||||
const args = ['git', '-C', fullPath, 'push']
|
||||
return await executeCommand(args, { env })
|
||||
} catch (error) {
|
||||
if (isNoUpstreamError(error as Error)) {
|
||||
return await this.pushWithUpstream(repoId, fullPath, env)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
private async pushWithUpstream(
|
||||
repoId: number,
|
||||
fullPath: string,
|
||||
env: Record<string, string>
|
||||
): Promise<string> {
|
||||
let branchName: string | null = null
|
||||
|
||||
try {
|
||||
const result = await executeCommand(
|
||||
['git', '-C', fullPath, 'rev-parse', '--abbrev-ref', 'HEAD'],
|
||||
{ env }
|
||||
)
|
||||
branchName = result.trim()
|
||||
if (branchName === 'HEAD') {
|
||||
branchName = null
|
||||
}
|
||||
} catch (error) {
|
||||
branchName = parseBranchNameFromError(error as Error)
|
||||
}
|
||||
|
||||
const env = this.gitAuthService.getGitEnvironment()
|
||||
if (!branchName) {
|
||||
throw new Error('Unable to detect current branch. Ensure you are on a branch before pushing with --set-upstream.')
|
||||
}
|
||||
|
||||
const args = ['git', '-C', fullPath, 'push', '--set-upstream', 'origin', branchName]
|
||||
return executeCommand(args, { env })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user