diff --git a/.changeset/nasty-bikes-dance.md b/.changeset/nasty-bikes-dance.md new file mode 100644 index 00000000..be3ed413 --- /dev/null +++ b/.changeset/nasty-bikes-dance.md @@ -0,0 +1,5 @@ +--- +'simple-git': minor +--- + +Support the use of `-B` in place of the default `-b` in checkout methods diff --git a/simple-git/src/git.js b/simple-git/src/git.js index 0ba6d32f..08b19946 100644 --- a/simple-git/src/git.js +++ b/simple-git/src/git.js @@ -25,7 +25,6 @@ const { checkIgnoreTask } = require('./lib/tasks/check-ignore'); const { checkIsRepoTask } = require('./lib/tasks/check-is-repo'); const { cloneTask, cloneMirrorTask } = require('./lib/tasks/clone'); const { cleanWithOptionsTask, isCleanOptionsArray } = require('./lib/tasks/clean'); -const { commitTask } = require('./lib/tasks/commit'); const { diffSummaryTask } = require('./lib/tasks/diff'); const { fetchTask } = require('./lib/tasks/fetch'); const { moveTask } = require('./lib/tasks/move'); @@ -283,33 +282,6 @@ Git.prototype.addAnnotatedTag = function (tagName, tagMessage) { ); }; -/** - * Check out a tag or revision, any number of additional arguments can be passed to the `git checkout` command - * by supplying either a string or array of strings as the first argument. - */ -Git.prototype.checkout = function () { - const commands = ['checkout', ...getTrailingOptions(arguments, true)]; - return this._runTask(straightThroughStringTask(commands), trailingFunctionArgument(arguments)); -}; - -/** - * Check out a remote branch - * - * @param {string} branchName name of branch - * @param {string} startPoint (e.g origin/development) - * @param {Function} [then] - */ -Git.prototype.checkoutBranch = function (branchName, startPoint, then) { - return this.checkout(['-b', branchName, startPoint], trailingFunctionArgument(arguments)); -}; - -/** - * Check out a local branch - */ -Git.prototype.checkoutLocalBranch = function (branchName, then) { - return this.checkout(['-b', branchName], trailingFunctionArgument(arguments)); -}; - /** * Delete a local branch */ diff --git a/simple-git/src/lib/simple-git-api.ts b/simple-git/src/lib/simple-git-api.ts index ee5182c2..9cfadbed 100644 --- a/simple-git/src/lib/simple-git-api.ts +++ b/simple-git/src/lib/simple-git-api.ts @@ -1,6 +1,7 @@ import { SimpleGitBase } from '../../typings'; import { taskCallback } from './task-callback'; import { changeWorkingDirectoryTask } from './tasks/change-working-directory'; +import checkout from './tasks/checkout'; import commit from './tasks/commit'; import config from './tasks/config'; import grep from './tasks/grep'; @@ -137,4 +138,4 @@ export class SimpleGitApi implements SimpleGitBase { } } -Object.assign(SimpleGitApi.prototype, commit(), config(), grep(), log(), version()); +Object.assign(SimpleGitApi.prototype, checkout(), commit(), config(), grep(), log(), version()); diff --git a/simple-git/src/lib/tasks/checkout.ts b/simple-git/src/lib/tasks/checkout.ts new file mode 100644 index 00000000..739e4b69 --- /dev/null +++ b/simple-git/src/lib/tasks/checkout.ts @@ -0,0 +1,38 @@ +import type { SimpleGit } from '../../../typings'; +import type { SimpleGitApi } from '../simple-git-api'; +import { getTrailingOptions, remove, trailingFunctionArgument } from '../utils'; +import { straightThroughStringTask } from './task'; + +function checkoutTask(args: string[]) { + const commands = ['checkout', ...args]; + if (commands[1] === '-b' && commands.includes('-B')) { + commands[1] = remove(commands, '-B'); + } + + return straightThroughStringTask(commands); +} + +export default function (): Pick { + return { + checkout(this: SimpleGitApi) { + return this._runTask( + checkoutTask(getTrailingOptions(arguments, 1)), + trailingFunctionArgument(arguments) + ); + }, + + checkoutBranch(this: SimpleGitApi, branchName, startPoint) { + return this._runTask( + checkoutTask(['-b', branchName, startPoint, ...getTrailingOptions(arguments)]), + trailingFunctionArgument(arguments) + ); + }, + + checkoutLocalBranch(this: SimpleGitApi, branchName) { + return this._runTask( + checkoutTask(['-b', branchName, ...getTrailingOptions(arguments)]), + trailingFunctionArgument(arguments) + ); + }, + }; +} diff --git a/simple-git/test/unit/checkout.spec.ts b/simple-git/test/unit/checkout.spec.ts index a3419138..0563e6f2 100644 --- a/simple-git/test/unit/checkout.spec.ts +++ b/simple-git/test/unit/checkout.spec.ts @@ -57,6 +57,13 @@ describe('checkout', () => { }); describe('checkoutLocalBranch', () => { + it('allows using -B', async () => { + git.checkoutLocalBranch('foo', { '-B': null }); + await closeWithSuccess(); + + assertExecutedCommands('checkout', '-B', 'foo'); + }); + it('with callback', async () => { git.checkoutLocalBranch('new-branch', callback); await closeWithSuccess(); @@ -76,6 +83,13 @@ describe('checkout', () => { }); describe('checkoutBranch', () => { + it('allows using -B', async () => { + git.checkoutBranch('foo', 'bar', ['-B']); + await closeWithSuccess(); + + assertExecutedCommands('checkout', '-B', 'foo', 'bar'); + }); + it('with callback', async function () { git.checkoutBranch('branch', 'start', callback); diff --git a/simple-git/typings/simple-git.d.ts b/simple-git/typings/simple-git.d.ts index f4ecfdc2..55f4332d 100644 --- a/simple-git/typings/simple-git.d.ts +++ b/simple-git/typings/simple-git.d.ts @@ -319,7 +319,7 @@ export interface SimpleGit extends SimpleGitBase { ): Response; /** - * Checkout a remote branch. + * Checkout a remote branch - equivalent to `git checkout -b ${branchName} ${startPoint}` * * - branchName name of branch. * - startPoint (e.g origin/development). @@ -330,6 +330,13 @@ export interface SimpleGit extends SimpleGitBase { callback?: types.SimpleGitTaskCallback ): Response; + checkoutBranch( + branchName: string, + startPoint: string, + options?: types.TaskOptions, + callback?: types.SimpleGitTaskCallback + ): Response; + /** * Internally uses pull and tags to get the list of tags then checks out the latest tag. */ @@ -340,13 +347,19 @@ export interface SimpleGit extends SimpleGitBase { ): Response; /** - * Checkout a local branch + * Checkout a local branch - equivalent to `git checkout -b ${branchName}` */ checkoutLocalBranch( branchName: string, callback?: types.SimpleGitTaskCallback ): Response; + checkoutLocalBranch( + branchName: string, + options?: types.TaskOptions, + callback?: types.SimpleGitTaskCallback + ): Response; + /** * Deletes unwanted content from the local repo - when supplying the first argument as * an array of `CleanOptions`, the array must include one of `CleanOptions.FORCE` or