From 155c78de48e85bae9047aa446c99a6362d3e7112 Mon Sep 17 00:00:00 2001 From: Steve King Date: Tue, 25 Apr 2023 08:06:30 +0100 Subject: [PATCH] Add new interface `git.showBuffer` to allow using `git.show` with binary file content. Closes #921 --- simple-git/readme.md | 6 +++++- simple-git/src/git.js | 13 ------------- simple-git/src/lib/simple-git-api.ts | 12 +++++++++++- simple-git/src/lib/tasks/show.ts | 28 ++++++++++++++++++++++++++++ simple-git/test/unit/show.spec.ts | 9 +++++++++ simple-git/typings/simple-git.d.ts | 2 ++ 6 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 simple-git/src/lib/tasks/show.ts diff --git a/simple-git/readme.md b/simple-git/readme.md index 66705f97..2eb311f9 100644 --- a/simple-git/readme.md +++ b/simple-git/readme.md @@ -209,7 +209,6 @@ For type details of the response for each of the tasks, please see the [TypeScri | `.rmKeepLocal([fileA, ...], handlerFn)` | removes files from source control but leaves them on disk | | `.tag(args[], handlerFn)` | Runs any supported [git tag](https://git-scm.com/docs/git-tag) commands with arguments passed as an array of strings . | | `.tags([options, ] handlerFn)` | list all tags, use the optional [options](#how-to-specify-options) object to set any options allows by the [git tag](https://git-scm.com/docs/git-tag) command. Tags will be sorted by semantic version number by default, for git versions 2.7 and above, use the `--sort` option to set a custom sort. | -| `.show([options], handlerFn)` | Show various types of objects, for example the file content at a certain commit. `options` is the single value string or array of string commands you want to run | ## git apply @@ -376,6 +375,11 @@ For type details of the response for each of the tasks, please see the [TypeScri - `.checkIsRepo('bare')` gets whether the current working directory is within a bare git repo (see either [git clone --bare](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---bare) or [git init --bare](https://git-scm.com/docs/git-init#Documentation/git-init.txt---bare)). - `.checkIsRepo('root')` gets whether the current working directory is the root directory for a repo (sub-directories will return false). +## git show + +- `.show(options)` show various types of objects for example the file content at a certain commit. `options` is the single value string or any [options](#how-to-specify-options) supported by the [git show](https://git-scm.com/docs/git-show) command. +- `.showBuffer(options)` same as the `.show` api, but returns the Buffer content directly to allow for showing binary file content. + ## git status - `.status([options])` gets the status of the current repo, resulting in a [StatusResult](https://github.com/steveukx/git-js/blob/main/simple-git/typings/response.d.ts). Additional arguments diff --git a/simple-git/src/git.js b/simple-git/src/git.js index 08b19946..af60fc6b 100644 --- a/simple-git/src/git.js +++ b/simple-git/src/git.js @@ -563,19 +563,6 @@ Git.prototype.revparse = function () { ); }; -/** - * Show various types of objects, for example the file at a certain commit - * - * @param {string[]} [options] - * @param {Function} [then] - */ -Git.prototype.show = function (options, then) { - return this._runTask( - straightThroughStringTask(['show', ...getTrailingOptions(arguments, 1)]), - trailingFunctionArgument(arguments) - ); -}; - /** */ Git.prototype.clean = function (mode, options, then) { diff --git a/simple-git/src/lib/simple-git-api.ts b/simple-git/src/lib/simple-git-api.ts index 9cfadbed..18a1df0f 100644 --- a/simple-git/src/lib/simple-git-api.ts +++ b/simple-git/src/lib/simple-git-api.ts @@ -10,6 +10,7 @@ import { initTask } from './tasks/init'; import log from './tasks/log'; import { mergeTask } from './tasks/merge'; import { pushTask } from './tasks/push'; +import show from './tasks/show'; import { statusTask } from './tasks/status'; import { configurationErrorTask, straightThroughStringTask } from './tasks/task'; import version from './tasks/version'; @@ -138,4 +139,13 @@ export class SimpleGitApi implements SimpleGitBase { } } -Object.assign(SimpleGitApi.prototype, checkout(), commit(), config(), grep(), log(), version()); +Object.assign( + SimpleGitApi.prototype, + checkout(), + commit(), + config(), + grep(), + log(), + show(), + version() +); diff --git a/simple-git/src/lib/tasks/show.ts b/simple-git/src/lib/tasks/show.ts new file mode 100644 index 00000000..36268f49 --- /dev/null +++ b/simple-git/src/lib/tasks/show.ts @@ -0,0 +1,28 @@ +import { SimpleGit } from '../../../typings'; +import { SimpleGitApi } from '../simple-git-api'; +import { getTrailingOptions, trailingFunctionArgument } from '../utils'; +import { straightThroughBufferTask, straightThroughStringTask } from './task'; + +export default function (): Pick { + return { + showBuffer(this: SimpleGitApi) { + const commands = ['show', ...getTrailingOptions(arguments, 1)]; + if (!commands.includes('--binary')) { + commands.splice(1, 0, '--binary'); + } + + return this._runTask( + straightThroughBufferTask(commands), + trailingFunctionArgument(arguments) + ); + }, + + show(this: SimpleGitApi) { + const commands = ['show', ...getTrailingOptions(arguments, 1)]; + return this._runTask( + straightThroughStringTask(commands), + trailingFunctionArgument(arguments) + ); + }, + }; +} diff --git a/simple-git/test/unit/show.spec.ts b/simple-git/test/unit/show.spec.ts index 2594ab98..e3268b57 100644 --- a/simple-git/test/unit/show.spec.ts +++ b/simple-git/test/unit/show.spec.ts @@ -11,6 +11,15 @@ describe('show', () => { callback = jest.fn(); }); + it('permits binary responses', async () => { + const task = git.showBuffer('HEAD:img.jpg'); + await closeWithSuccess('some response'); + const result = await task; + + expect(result).toEqual(expect.any(Buffer)); + expect(result.toString('utf8')).toEqual('some response'); + }); + it('passes the response through without editing', async () => { const { stdOut } = showAbbrevCommitSingleFile(); diff --git a/simple-git/typings/simple-git.d.ts b/simple-git/typings/simple-git.d.ts index 55f4332d..4796581c 100644 --- a/simple-git/typings/simple-git.d.ts +++ b/simple-git/typings/simple-git.d.ts @@ -899,6 +899,8 @@ export interface SimpleGit extends SimpleGitBase { show(callback?: types.SimpleGitTaskCallback): Response; + showBuffer(option: string | types.TaskOptions): Response; + /** * @deprecated *