Skip to content

Commit

Permalink
Add new interface git.showBuffer to allow using git.show with bin…
Browse files Browse the repository at this point in the history
…ary file content.

Closes #921
  • Loading branch information
steveukx committed Apr 25, 2023
1 parent f54cd0d commit 155c78d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
6 changes: 5 additions & 1 deletion simple-git/readme.md
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions simple-git/src/git.js
Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion simple-git/src/lib/simple-git-api.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
);
28 changes: 28 additions & 0 deletions 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<SimpleGit, 'showBuffer' | 'show'> {
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)
);
},
};
}
9 changes: 9 additions & 0 deletions simple-git/test/unit/show.spec.ts
Expand Up @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions simple-git/typings/simple-git.d.ts
Expand Up @@ -899,6 +899,8 @@ export interface SimpleGit extends SimpleGitBase {

show(callback?: types.SimpleGitTaskCallback<string>): Response<string>;

showBuffer(option: string | types.TaskOptions): Response<Buffer>;

/**
* @deprecated
*
Expand Down

0 comments on commit 155c78d

Please sign in to comment.