<commit_before>/*globals define*/
/*jshint node:true*/
/**
 * @author pmeijer / https://github.com/pmeijer
 */

define(['common/util/assert', 'common/core/core'], function (ASSERT, Core) {
    'use strict';
    var HASH_REGEXP = new RegExp('^#[0-9a-zA-Z_]*$'),
        BRANCH_REGEXP = new RegExp('^[0-9a-zA-Z_]*$');
    /**
     * Opens the context specified by the parameters and returns a result object.
     * If no error is returned the database and the project are kept open, otherwise they are closed.
     * @param {object} storage - client-storage, server-user-storage or local-storage
     * @param {object} gmeConfig - global webgme configuration
     * @param {object} parameters
     * @param {string} [parameters.projectName] - name of project to open -> result.project
     * @param {boolean} [parameters.createProject] - if not found will create a project
     *                                              -> result.rootNode, result.commitHash
     * @param {boolean} [parameters.overwriteProject] - if found will overwrite the existing project
     *                                              -> result.rootNode, result.commitHash
     * @param {string} [parameters.branchName] - name of branch to load root from or name of branch to create when
     *                                           creating a project. -> result.rootNode, result.commitHash
     *
     * The following group is only valid when not creating a new project:
     * @param {string} [parameters.commitHash] - if branchName given commitHash will be loaded. -> result.rootNode
     * @param {string} [parameters.branchOrCommit] - REGEXPs will determine if branch or commit -> result.rootNode
     * @param {[string]} [parameters.nodePaths] -loads all specified node paths. -> result.nodes
     * @param {boolean} [parameters.meta] - loads all META-nodes. -> result.META
     *
     * @param {object} [parameters.core] - Used if branchName or commitHash is specified (a new Core will be created
     *                                     if needed and not provided). -> result.core
     * @param {function} callback
     */
    var openContext = function (storage, gmeConfig, parameters, callback) {
        var result = {},
            closeOnError = function (err) {
                if (result.project) {
                    result.project.closeProject(function () {
                        storage.closeDatabase(function () {
                            callback(err);
                        });
                    });
                } else {
                    storage.closeDatabase(function () {
                        callback(err);
                    });
                }
            };

        ASSERT(typeof storage !== 'undefined' && storage.hasOwnProperty('openDatabase'), 'storage must be given');
        ASSERT(typeof callback === 'function', 'a callback must be given');

        storage.openDatabase(function (err) {
            if (err) {
                closeOnError(err);
                return;
            }
            if (parameters.projectName) {
                storage.getProjectNames(function (err, projectNames) {
                    var projectExists = projectNames.indexOf(parameters.projectName) > -1;
                    if (err) {
                        closeOnError(err);
                        return;
                    }
                    if (!projectExists && !parameters.createProject && !parameters.overwriteProject) {
                        closeOnError('"' + parameters.projectName + '" does not exists among: ' +
                        projectNames.toString() + '. Set flag "createProject" to create a new project.');
                        return;
                    } else if (projectExists && parameters.createProject && !parameters.overwriteProject && !parameters.branchName) {
                        closeOnError('"' + parameters.projectName + '" already exists: ' +
                        projectNames.toString() + '. Set flag "overwriteProject" to overwrite project.');
                        return;
                    }

                    storage.openProject(parameters.projectName, function (err, project) {
                        if (err) {
                            closeOnError(err);
                            return;
                        }
                        result.project = project;
                        if (parameters.createProject || parameters.overwriteProject) {
                            if (projectExists && parameters.overwriteProject) {
                                _deleteAndPersistEmptyProject(storage, parameters, result, gmeConfig, function (err) {<commit_msg>#47 -small fix for open context<commit_after>/*globals define*/
/*jshint node:true*/
/**
 * @author pmeijer / https://github.com/pmeijer
 */

define(['common/util/assert', 'common/core/core'], function (ASSERT, Core) {
    'use strict';
    var HASH_REGEXP = new RegExp('^#[0-9a-zA-Z_]*$'),
        BRANCH_REGEXP = new RegExp('^[0-9a-zA-Z_]*$');
    /**
     * Opens the context specified by the parameters and returns a result object.
     * If no error is returned the database and the project are kept open, otherwise they are closed.
     * @param {object} storage - client-storage, server-user-storage or local-storage
     * @param {object} gmeConfig - global webgme configuration
     * @param {object} parameters
     * @param {string} [parameters.projectName] - name of project to open -> result.project
     * @param {boolean} [parameters.createProject] - if not found will create a project
     *                                              -> result.rootNode, result.commitHash
     * @param {boolean} [parameters.overwriteProject] - if found will overwrite the existing project
     *                                              -> result.rootNode, result.commitHash
     * @param {string} [parameters.branchName] - name of branch to load root from or name of branch to create when
     *                                           creating a project. -> result.rootNode, result.commitHash
     *
     * The following group is only valid when not creating a new project:
     * @param {string} [parameters.commitHash] - if branchName given commitHash will be loaded. -> result.rootNode
     * @param {string} [parameters.branchOrCommit] - REGEXPs will determine if branch or commit -> result.rootNode
     * @param {[string]} [parameters.nodePaths] -loads all specified node paths. -> result.nodes
     * @param {boolean} [parameters.meta] - loads all META-nodes. -> result.META
     *
     * @param {object} [parameters.core] - Used if branchName or commitHash is specified (a new Core will be created
     *                                     if needed and not provided). -> result.core
     * @param {function} callback
     */
    var openContext = function (storage, gmeConfig, parameters, callback) {
        var result = {},
            closeOnError = function (err) {
                if (result.project) {
                    result.project.closeProject(function () {
                        storage.closeDatabase(function () {
                            callback(err);
                        });
                    });
                } else {
                    storage.closeDatabase(function () {
                        callback(err);
                    });
                }
            };

        ASSERT(typeof storage !== 'undefined' && storage.hasOwnProperty('openDatabase'), 'storage must be given');
        ASSERT(typeof callback === 'function', 'a callback must be given');

        storage.openDatabase(function (err) {
            if (err) {
                closeOnError(err);
                return;
            }
            if (parameters.projectName) {
                storage.getProjectNames(function (err, projectNames) {
                    var projectExists = (projectNames || []).indexOf(parameters.projectName) > -1;
                    if (err) {
                        closeOnError(err);
                        return;
                    }
                    if (!projectExists && !parameters.createProject && !parameters.overwriteProject) {
                        closeOnError('"' + parameters.projectName + '" does not exists among: ' +
                        projectNames.toString() + '. Set flag "createProject" to create a new project.');
                        return;
                    } else if (projectExists && parameters.createProject && !parameters.overwriteProject && !parameters.branchName) {
                        closeOnError('"' + parameters.projectName + '" already exists: ' +
                        projectNames.toString() + '. Set flag "overwriteProject" to overwrite project.');
                        return;
                    }

                    storage.openProject(parameters.projectName, function (err, project) {
                        if (err) {
                            closeOnError(err);
                            return;
                        }
                        result.project = project;
                        if (parameters.createProject || parameters.overwriteProject) {
                            if (projectExists && parameters.overwriteProject) {
                                _deleteAndPersistEmptyProject(storage, parameters, result, gmeConfig, function (err) {
{'fraction_non_alphanumeric': 0.10151741825176319, 'fraction_numerical': 0.0012823252831801668, 'mean_word_length': 1.658051689860835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '793674', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2126, 'n_words': 628}