Skip to content

Commit

Permalink
Merge pull request eclipse-theia#34 from federicobozzini/new-branch
Browse files Browse the repository at this point in the history
Changed command to create a new branch
  • Loading branch information
kittaakos authored Jun 8, 2020
2 parents 9a39c6a + 43348fc commit f7e9a1a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dugite-extra",
"version": "0.1.13",
"version": "0.1.14",
"description": "High-level Git commands for dugite.",
"main": "lib/index",
"typings": "lib/index",
Expand Down
158 changes: 158 additions & 0 deletions src/command/branch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import * as temp from 'temp';
import * as fs from 'fs-extra';
import * as Path from 'path';
import { expect } from 'chai';
import { createCommit } from './commit';
import { initRepository } from './test-helper';
import { git } from '../core/git';
import { listBranch, createBranch } from './branch';
import { IGitResult } from 'dugite-no-gpl';

const track = temp.track();

let path: string;

const createAndCommit = async (filename: string, message: string): Promise<void> => {
fs.createFileSync(Path.join(path, filename));

await git(['add', '.'], path, 'add');
await createCommit(path, message);
}
const getCommitIds = async (repoPath: string): Promise<string[]> => {
let log: IGitResult;
try {
log = await git(['log', '--pretty=%H'], repoPath, 'log');
return log.stdout.trim().split('\n');
} catch (e) {
if (e.name === 'GitError' && e.result && e.result.exitCode && e.result.exitCode === 128) {
// git log on repo with no commits throws an error
return [];
} else {
throw e;
}
}
}

describe('branch', async () => {

beforeEach(async () => {
path = track.mkdirSync('branch');
await initRepository(path);
});

afterEach((done: ((err: any, result: temp.Stats) => void)) => {
track.cleanup(done);
});

it('no branch before first commit', async () => {
const localBranches = await listBranch(path, 'all');
expect(localBranches.length).to.be.equal(0);

const currentBranch = await listBranch(path, 'current');
expect(currentBranch).to.be.undefined;
});

it('only master branch after first commit', async () => {
await createAndCommit('some-file.txt', 'first commit');

const localBranches = await listBranch(path, 'all');
expect(localBranches.length).to.be.equal(1);
expect(localBranches[0].name).to.be.equal('master');

const curretnBranch = await listBranch(path, 'current');
expect(curretnBranch).to.not.be.undefined;
expect(curretnBranch!.name).to.be.equal('master');
});

it('new branch is selected on creation if checkout is true', async () => {
await createAndCommit('some-file.txt', 'first commit');

const newBranch = 'branch1';
await createBranch(path, newBranch, { checkout: true });

const localBranches = await listBranch(path, 'all');
expect(localBranches.length).to.be.equal(2);
expect(localBranches[0].name).to.be.equal('master');
expect(localBranches[1].name).to.be.equal(newBranch);

const currentBranch = await listBranch(path, 'current');
expect(currentBranch).to.not.be.undefined;
expect(currentBranch!.name).to.be.equal(newBranch);
});

it('new branch is not selected on creation if checkout is false', async () => {
await createAndCommit('some-file.txt', 'first commit');

const newBranch = 'branch1';
await createBranch(path, newBranch);

const localBranches = await listBranch(path, 'all');
expect(localBranches.length).to.be.equal(2);
expect(localBranches[0].name).to.be.equal('master');
expect(localBranches[1].name).to.be.equal(newBranch);

const currentBranch = await listBranch(path, 'current');
expect(currentBranch).to.not.be.undefined;
expect(currentBranch!.name).to.be.equal('master');
});

it('new branch is not created until first commit', async () => {
const newBranch = 'branch1';
await createBranch(path, newBranch, { checkout: true });

const localBranches = await listBranch(path, 'all');
expect(localBranches.length).to.be.equal(0);
});

it('new branch is created on first commit in place of master if checkout is true', async () => {
const newBranch = 'branch1';
await createBranch(path, newBranch, { checkout: true });

await createAndCommit('some-file.txt', 'first commit');

const localBranches = await listBranch(path, 'all');
expect(localBranches.length).to.be.equal(1);
expect(localBranches[0].name).to.be.equal(newBranch);

const currentBranch = await listBranch(path, 'current');
expect(currentBranch).to.not.be.undefined;
expect(currentBranch!.name).to.be.equal(newBranch);
});

it('a new branch cannot be created before master if checkout is false', async () => {
const newBranch = 'branch1';
try {
await createBranch(path, newBranch);
expect.fail('An error should have been thrown by createBranch');
} catch (e) {
if (e.name === 'GitError' && e.result && e.result.exitCode && e.result.exitCode === 128) {
// git branch on repo with no commits should throw an error
} else {
expect.fail('createBranch failed with an unexpected error')
}
}
});

it('new branch is created on correct start point', async () => {
await createAndCommit('some-file.txt', 'first commit');

const commitIds = await getCommitIds(path);
const firstCommitId = commitIds[0];

await createAndCommit('other-text.txt', 'second commit');

const newBranch = 'branch1';
await createBranch(path, newBranch, { startPoint: firstCommitId, checkout: true });

const localBranches = await listBranch(path, 'all');
expect(localBranches.length).to.be.equal(2);

const currentBranch = await listBranch(path, 'current');
expect(currentBranch).to.not.be.undefined;
expect(currentBranch!.name).to.be.equal(newBranch);

const newCommitIds = await getCommitIds(path);
expect(newCommitIds[0]).to.be.equal(firstCommitId);
});

});
12 changes: 9 additions & 3 deletions src/command/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ export async function listBranch(repositoryPath: string, type: 'current' | 'loca
}
}

export async function createBranch(repositoryPath: string, name: string, createOptions?: { startPoint?: string }, options?: IGitExecutionOptions): Promise<void> {
export async function createBranch(
repositoryPath: string,
name: string,
createOptions?: { startPoint?: string, checkout?: boolean },
options?: IGitExecutionOptions): Promise<void> {

const startPoint = createOptions ? createOptions.startPoint : undefined;
const args = ['branch', name];
const checkout = createOptions ? createOptions.checkout : false;
const args = checkout ? ['checkout', '-b', name] : ['branch', name];
if (startPoint) {
args.push(startPoint);
}
Expand Down Expand Up @@ -119,4 +125,4 @@ async function getBranches(repositoryPath: string, prefixes: string[], options?:
const type = ref.startsWith('refs/head') ? BranchType.Local : BranchType.Remote;
return new Branch(name, upstream.length > 0 ? upstream : undefined, tip, type);
});
}
}

0 comments on commit f7e9a1a

Please sign in to comment.