Skip to content

Commit 97fde2c

Browse files
committed
Add support for using the -B modifier instead of the default -b when using checkoutBranch / checkoutLocalBranch.
Based on requirement detailed in #873
1 parent edfd459 commit 97fde2c

File tree

6 files changed

+74
-31
lines changed

6 files changed

+74
-31
lines changed

.changeset/nasty-bikes-dance.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'simple-git': minor
3+
---
4+
5+
Support the use of `-B` in place of the default `-b` in checkout methods

simple-git/src/git.js

-28
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const { checkIgnoreTask } = require('./lib/tasks/check-ignore');
2525
const { checkIsRepoTask } = require('./lib/tasks/check-is-repo');
2626
const { cloneTask, cloneMirrorTask } = require('./lib/tasks/clone');
2727
const { cleanWithOptionsTask, isCleanOptionsArray } = require('./lib/tasks/clean');
28-
const { commitTask } = require('./lib/tasks/commit');
2928
const { diffSummaryTask } = require('./lib/tasks/diff');
3029
const { fetchTask } = require('./lib/tasks/fetch');
3130
const { moveTask } = require('./lib/tasks/move');
@@ -283,33 +282,6 @@ Git.prototype.addAnnotatedTag = function (tagName, tagMessage) {
283282
);
284283
};
285284

286-
/**
287-
* Check out a tag or revision, any number of additional arguments can be passed to the `git checkout` command
288-
* by supplying either a string or array of strings as the first argument.
289-
*/
290-
Git.prototype.checkout = function () {
291-
const commands = ['checkout', ...getTrailingOptions(arguments, true)];
292-
return this._runTask(straightThroughStringTask(commands), trailingFunctionArgument(arguments));
293-
};
294-
295-
/**
296-
* Check out a remote branch
297-
*
298-
* @param {string} branchName name of branch
299-
* @param {string} startPoint (e.g origin/development)
300-
* @param {Function} [then]
301-
*/
302-
Git.prototype.checkoutBranch = function (branchName, startPoint, then) {
303-
return this.checkout(['-b', branchName, startPoint], trailingFunctionArgument(arguments));
304-
};
305-
306-
/**
307-
* Check out a local branch
308-
*/
309-
Git.prototype.checkoutLocalBranch = function (branchName, then) {
310-
return this.checkout(['-b', branchName], trailingFunctionArgument(arguments));
311-
};
312-
313285
/**
314286
* Delete a local branch
315287
*/

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SimpleGitBase } from '../../typings';
22
import { taskCallback } from './task-callback';
33
import { changeWorkingDirectoryTask } from './tasks/change-working-directory';
4+
import checkout from './tasks/checkout';
45
import commit from './tasks/commit';
56
import config from './tasks/config';
67
import grep from './tasks/grep';
@@ -137,4 +138,4 @@ export class SimpleGitApi implements SimpleGitBase {
137138
}
138139
}
139140

140-
Object.assign(SimpleGitApi.prototype, commit(), config(), grep(), log(), version());
141+
Object.assign(SimpleGitApi.prototype, checkout(), commit(), config(), grep(), log(), version());

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

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { SimpleGit } from '../../../typings';
2+
import type { SimpleGitApi } from '../simple-git-api';
3+
import { getTrailingOptions, remove, trailingFunctionArgument } from '../utils';
4+
import { straightThroughStringTask } from './task';
5+
6+
function checkoutTask(args: string[]) {
7+
const commands = ['checkout', ...args];
8+
if (commands[1] === '-b' && commands.includes('-B')) {
9+
commands[1] = remove(commands, '-B');
10+
}
11+
12+
return straightThroughStringTask(commands);
13+
}
14+
15+
export default function (): Pick<SimpleGit, 'checkout' | 'checkoutBranch' | 'checkoutLocalBranch'> {
16+
return {
17+
checkout(this: SimpleGitApi) {
18+
return this._runTask(
19+
checkoutTask(getTrailingOptions(arguments, 1)),
20+
trailingFunctionArgument(arguments)
21+
);
22+
},
23+
24+
checkoutBranch(this: SimpleGitApi, branchName, startPoint) {
25+
return this._runTask(
26+
checkoutTask(['-b', branchName, startPoint, ...getTrailingOptions(arguments)]),
27+
trailingFunctionArgument(arguments)
28+
);
29+
},
30+
31+
checkoutLocalBranch(this: SimpleGitApi, branchName) {
32+
return this._runTask(
33+
checkoutTask(['-b', branchName, ...getTrailingOptions(arguments)]),
34+
trailingFunctionArgument(arguments)
35+
);
36+
},
37+
};
38+
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ describe('checkout', () => {
5757
});
5858

5959
describe('checkoutLocalBranch', () => {
60+
it('allows using -B', async () => {
61+
git.checkoutLocalBranch('foo', { '-B': null });
62+
await closeWithSuccess();
63+
64+
assertExecutedCommands('checkout', '-B', 'foo');
65+
});
66+
6067
it('with callback', async () => {
6168
git.checkoutLocalBranch('new-branch', callback);
6269
await closeWithSuccess();
@@ -76,6 +83,13 @@ describe('checkout', () => {
7683
});
7784

7885
describe('checkoutBranch', () => {
86+
it('allows using -B', async () => {
87+
git.checkoutBranch('foo', 'bar', ['-B']);
88+
await closeWithSuccess();
89+
90+
assertExecutedCommands('checkout', '-B', 'foo', 'bar');
91+
});
92+
7993
it('with callback', async function () {
8094
git.checkoutBranch('branch', 'start', callback);
8195

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export interface SimpleGit extends SimpleGitBase {
319319
): Response<string>;
320320

321321
/**
322-
* Checkout a remote branch.
322+
* Checkout a remote branch - equivalent to `git checkout -b ${branchName} ${startPoint}`
323323
*
324324
* - branchName name of branch.
325325
* - startPoint (e.g origin/development).
@@ -330,6 +330,13 @@ export interface SimpleGit extends SimpleGitBase {
330330
callback?: types.SimpleGitTaskCallback<void>
331331
): Response<void>;
332332

333+
checkoutBranch(
334+
branchName: string,
335+
startPoint: string,
336+
options?: types.TaskOptions,
337+
callback?: types.SimpleGitTaskCallback<void>
338+
): Response<void>;
339+
333340
/**
334341
* Internally uses pull and tags to get the list of tags then checks out the latest tag.
335342
*/
@@ -340,13 +347,19 @@ export interface SimpleGit extends SimpleGitBase {
340347
): Response<void>;
341348

342349
/**
343-
* Checkout a local branch
350+
* Checkout a local branch - equivalent to `git checkout -b ${branchName}`
344351
*/
345352
checkoutLocalBranch(
346353
branchName: string,
347354
callback?: types.SimpleGitTaskCallback<void>
348355
): Response<void>;
349356

357+
checkoutLocalBranch(
358+
branchName: string,
359+
options?: types.TaskOptions,
360+
callback?: types.SimpleGitTaskCallback<void>
361+
): Response<void>;
362+
350363
/**
351364
* Deletes unwanted content from the local repo - when supplying the first argument as
352365
* an array of `CleanOptions`, the array must include one of `CleanOptions.FORCE` or

0 commit comments

Comments
 (0)