Skip to content

Commit

Permalink
Create GitCommandManager.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
rindeal authored Sep 4, 2024
1 parent 87c8bf0 commit 958f217
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/GitCommandManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* SPDX-FileCopyrightText: 2024 Jan Chren ~rindeal
*
* SPDX-License-Identifier: GPL-3.0-only OR GPL-2.0-only
*/

import { execFile } from 'child_process'


export class GitCommandManager {
gitVersion: string

async initialize() {
const versionOutput = await this.execGit(['version'])
const versionMatch = versionOutput.match(/git version (\d+\.\d+\.\d+)/)
if ( ! versionMatch ) {
throw new Error('Unable to determine Git version')
}
this.gitVersion = versionMatch[1]
}

async execGit(args: string[]): Promise<string> {
return new Promise((resolve, reject) => {
execFile('git', args, (error, stdout) => {
if ( error ) {
reject(error)
} else {
resolve(stdout)
}
})
})
}

async lsRemote(options: {
repository?: string,
branches?: boolean,
tags?: boolean,
exitCode?: boolean,
patterns?: string[]
}): Promise<string> {
const args = ['ls-remote']

if ( options.branches ) {
args.push('--heads')
}
if ( options.tags ) {
args.push('--tags')
}
if ( options.exitCode ) {
args.push('--exit-code')
}

args.push('--')

if ( options.repository ) {
if ( options.repository.startsWith('-') ) {
throw new Error('Invalid repository option')
}
args.push(options.repository)
}
if ( options.patterns ) {
if ( ! options.repository ) {
throw new Error('Option `repository` is needed for option `patterns`')
}
for (const pattern of options.patterns) {
if ( pattern.startsWith('-') ) {
throw new Error('Invalid pattern option')
}
args.push(pattern)
}
}

return this.execGit(args)
}
}

0 comments on commit 958f217

Please sign in to comment.