src/utils/branches.js
function create(repositoryState, driver, name, opts = {}) {
    const {
        // Base branch for the new branch
        base = repositoryState.getCurrentBranch(),
        // Fetch the working state and switch to it ?
        checkout = true,
        // Drop changes from base branch the new working state ?
        clean = true,
        // Drop changes from the base branch ?
        cleanBase = false
    } = opts;

    let createdBranch;

    return driver.createBranch(base, name)
    // Update list of branches
    .then((branch) => {
        createdBranch = branch;
        let branches = repositoryState.getBranches();
        branches = branches.push(createdBranch);
        return repositoryState.set('branches', branches);
    })

    // Update working state or fetch it if needed
    .then((repoState) => {
        let baseWk = repoState.getWorkingStateForBranch(base);

        if (!baseWk) {
            return checkout ? RepoUtils.fetchTree(repoState, driver, createdBranch) : repoState;
        }

        // Reuse base WorkingState clean
        const headWk = clean ? baseWk.asClean() : baseWk;
        repoState = RepoUtils.updateWorkingState(repoState, createdBranch, headWk);

        // Clean base WorkingState
        baseWk = cleanBase ? baseWk.asClean() : baseWk;
        repoState = RepoUtils.updateWorkingState(repoState, base, baseWk);

        return repoState;
    })

    // Checkout the branch
    .then((repoState) => {
        if (!checkout) {
            return repoState;
        }

        return RepoUtils.checkout(repoState, createdBranch);
    });
}
function create(repositoryState, driver, name, opts = {}) {
    const {
        // Base branch for the new branch
        base = repositoryState.getCurrentBranch(),
        // Fetch the working state and switch to it ?
        checkout = true,
        // Drop changes from base branch the new working state ?
        clean = true,
        // Drop changes from the base branch ?
        cleanBase = false
    } = opts;

    let createdBranch;

    return driver.createBranch(base, name)
    // Update list of branches
    .then((branch) => {
        createdBranch = branch;
        let branches = repositoryState.getBranches();
        branches = branches.push(createdBranch);
        return repositoryState.set('branches', branches);
    })

    // Update working state or fetch it if needed
    .then((repoState) => {
        let baseWk = repoState.getWorkingStateForBranch(base);

        if (!baseWk) {
            return checkout ? RepoUtils.fetchTree(repoState, driver, createdBranch) : repoState;
        }

        // Reuse base WorkingState clean
        const headWk = clean ? baseWk.asClean() : baseWk;
        repoState = RepoUtils.updateWorkingState(repoState, createdBranch, headWk);

        // Clean base WorkingState
        baseWk = cleanBase ? baseWk.asClean() : baseWk;
        repoState = RepoUtils.updateWorkingState(repoState, base, baseWk);

        return repoState;
    })

    // Checkout the branch
    .then((repoState) => {
        if (!checkout) {
            return repoState;
        }

        return RepoUtils.checkout(repoState, createdBranch);
    });
}
Create a new branch with the given name.
@param {RepositoryState} repoState
@param {Driver} driver
@param {String} name
@param {Branch} [opts.base] Base branch, default to current branch
@param {Boolean} [opts.checkout=false] Directly fetch and checkout the branch
@return {Promise<RepositoryState>}