-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create interface for retrieving git version information (#850)
* Create interface for retrieving git version information
- Loading branch information
Showing
10 changed files
with
170 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'simple-git': minor | ||
--- | ||
|
||
Add `.version` to return git version information, including whether the git binary is installed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Check if git is installed | ||
|
||
To check if `git` (or the `customBinary` of your choosing) is accessible, use the | ||
`git.version()` api: | ||
|
||
```typescript | ||
import { simpleGit } from 'simple-git'; | ||
|
||
const {installed} = await simpleGit().version(); | ||
if (!installed) { | ||
throw new Error(`Exit: "git" not available.`); | ||
} | ||
|
||
// ... continue using git commands here | ||
``` | ||
|
||
## Check for a specific version of git | ||
|
||
Using the `git.version()` interface, you can query for the current `git` version | ||
information split by `major`, `minor` and `patch`: | ||
|
||
```typescript | ||
import { simpleGit } from 'simple-git'; | ||
import { lt } from 'semver'; | ||
|
||
const versionResult = await simpleGit().version(); | ||
if (lt(String(versionResult), '2.1.0')) { | ||
throw new Error(`Exit: "git" must be at least version 2.1.0.`); | ||
} | ||
|
||
// ... continue using git commands here compatible with 2.1.0 or higher | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { SimpleGitApi } from '../simple-git-api'; | ||
import type { SimpleGit } from '../../../typings'; | ||
import { asNumber, ExitCodes } from '../utils'; | ||
|
||
export interface VersionResult { | ||
major: number; | ||
minor: number; | ||
patch: number; | ||
agent: string; | ||
installed: boolean; | ||
} | ||
|
||
const NOT_INSTALLED = 'installed=false'; | ||
|
||
function versionResponse( | ||
major = 0, | ||
minor = 0, | ||
patch = 0, | ||
agent = '', | ||
installed = true | ||
): VersionResult { | ||
return Object.defineProperty( | ||
{ | ||
major, | ||
minor, | ||
patch, | ||
agent, | ||
installed, | ||
}, | ||
'toString', | ||
{ | ||
value() { | ||
return `${major}.${minor}.${patch}`; | ||
}, | ||
configurable: false, | ||
enumerable: false, | ||
} | ||
); | ||
} | ||
|
||
function notInstalledResponse() { | ||
return versionResponse(0, 0, 0, '', false); | ||
} | ||
|
||
export default function (): Pick<SimpleGit, 'version'> { | ||
return { | ||
version(this: SimpleGitApi) { | ||
return this._runTask({ | ||
commands: ['--version'], | ||
format: 'utf-8', | ||
parser(stdOut) { | ||
if (stdOut === NOT_INSTALLED) { | ||
return notInstalledResponse(); | ||
} | ||
|
||
const version = /version (\d+)\.(\d+)\.(\d+)(?:\s*\((.+)\))?/.exec(stdOut); | ||
|
||
if (!version) { | ||
return versionResponse(0, 0, 0, stdOut); | ||
} | ||
|
||
return versionResponse( | ||
asNumber(version[1]), | ||
asNumber(version[2]), | ||
asNumber(version[3]), | ||
version[4] || '' | ||
); | ||
}, | ||
onError(result, error, done, fail) { | ||
if (result.exitCode === ExitCodes.NOT_FOUND) { | ||
return done(Buffer.from(NOT_INSTALLED)); | ||
} | ||
|
||
fail(error); | ||
}, | ||
}); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
export enum ExitCodes { | ||
SUCCESS, | ||
ERROR, | ||
NOT_FOUND = -2, | ||
UNCLEAN = 128, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createTestContext, newSimpleGit, SimpleGitTestContext } from '@simple-git/test-utils'; | ||
|
||
describe('version', () => { | ||
let context: SimpleGitTestContext; | ||
|
||
beforeEach(async () => (context = await createTestContext())); | ||
|
||
it('gets the current version', async () => { | ||
const git = newSimpleGit(context.root); | ||
expect(await git.version()).toEqual({ | ||
major: 2, | ||
minor: expect.any(Number), | ||
patch: expect.any(Number), | ||
agent: expect.any(String), | ||
installed: true, | ||
}); | ||
}); | ||
|
||
it('gets the current version when the binary is not installed', async () => { | ||
const git = newSimpleGit(context.root).customBinary('bad'); | ||
expect(await git.version()).toEqual({ | ||
major: 0, | ||
minor: 0, | ||
patch: 0, | ||
agent: '', | ||
installed: false, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters