Skip to content

Commit 155c78d

Browse files
committed
Add new interface git.showBuffer to allow using git.show with binary file content.
Closes #921
1 parent f54cd0d commit 155c78d

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

simple-git/readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ For type details of the response for each of the tasks, please see the [TypeScri
209209
| `.rmKeepLocal([fileA, ...], handlerFn)` | removes files from source control but leaves them on disk |
210210
| `.tag(args[], handlerFn)` | Runs any supported [git tag](https://git-scm.com/docs/git-tag) commands with arguments passed as an array of strings . |
211211
| `.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. |
212-
| `.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 |
213212

214213
## git apply
215214

@@ -376,6 +375,11 @@ For type details of the response for each of the tasks, please see the [TypeScri
376375
- `.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)).
377376
- `.checkIsRepo('root')` gets whether the current working directory is the root directory for a repo (sub-directories will return false).
378377

378+
## git show
379+
380+
- `.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.
381+
- `.showBuffer(options)` same as the `.show` api, but returns the Buffer content directly to allow for showing binary file content.
382+
379383
## git status
380384

381385
- `.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

simple-git/src/git.js

-13
Original file line numberDiff line numberDiff line change
@@ -563,19 +563,6 @@ Git.prototype.revparse = function () {
563563
);
564564
};
565565

566-
/**
567-
* Show various types of objects, for example the file at a certain commit
568-
*
569-
* @param {string[]} [options]
570-
* @param {Function} [then]
571-
*/
572-
Git.prototype.show = function (options, then) {
573-
return this._runTask(
574-
straightThroughStringTask(['show', ...getTrailingOptions(arguments, 1)]),
575-
trailingFunctionArgument(arguments)
576-
);
577-
};
578-
579566
/**
580567
*/
581568
Git.prototype.clean = function (mode, options, then) {

simple-git/src/lib/simple-git-api.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { initTask } from './tasks/init';
1010
import log from './tasks/log';
1111
import { mergeTask } from './tasks/merge';
1212
import { pushTask } from './tasks/push';
13+
import show from './tasks/show';
1314
import { statusTask } from './tasks/status';
1415
import { configurationErrorTask, straightThroughStringTask } from './tasks/task';
1516
import version from './tasks/version';
@@ -138,4 +139,13 @@ export class SimpleGitApi implements SimpleGitBase {
138139
}
139140
}
140141

141-
Object.assign(SimpleGitApi.prototype, checkout(), commit(), config(), grep(), log(), version());
142+
Object.assign(
143+
SimpleGitApi.prototype,
144+
checkout(),
145+
commit(),
146+
config(),
147+
grep(),
148+
log(),
149+
show(),
150+
version()
151+
);

simple-git/src/lib/tasks/show.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { SimpleGit } from '../../../typings';
2+
import { SimpleGitApi } from '../simple-git-api';
3+
import { getTrailingOptions, trailingFunctionArgument } from '../utils';
4+
import { straightThroughBufferTask, straightThroughStringTask } from './task';
5+
6+
export default function (): Pick<SimpleGit, 'showBuffer' | 'show'> {
7+
return {
8+
showBuffer(this: SimpleGitApi) {
9+
const commands = ['show', ...getTrailingOptions(arguments, 1)];
10+
if (!commands.includes('--binary')) {
11+
commands.splice(1, 0, '--binary');
12+
}
13+
14+
return this._runTask(
15+
straightThroughBufferTask(commands),
16+
trailingFunctionArgument(arguments)
17+
);
18+
},
19+
20+
show(this: SimpleGitApi) {
21+
const commands = ['show', ...getTrailingOptions(arguments, 1)];
22+
return this._runTask(
23+
straightThroughStringTask(commands),
24+
trailingFunctionArgument(arguments)
25+
);
26+
},
27+
};
28+
}

simple-git/test/unit/show.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ describe('show', () => {
1111
callback = jest.fn();
1212
});
1313

14+
it('permits binary responses', async () => {
15+
const task = git.showBuffer('HEAD:img.jpg');
16+
await closeWithSuccess('some response');
17+
const result = await task;
18+
19+
expect(result).toEqual(expect.any(Buffer));
20+
expect(result.toString('utf8')).toEqual('some response');
21+
});
22+
1423
it('passes the response through without editing', async () => {
1524
const { stdOut } = showAbbrevCommitSingleFile();
1625

simple-git/typings/simple-git.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,8 @@ export interface SimpleGit extends SimpleGitBase {
899899

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

902+
showBuffer(option: string | types.TaskOptions): Response<Buffer>;
903+
902904
/**
903905
* @deprecated
904906
*

0 commit comments

Comments
 (0)