<commit_before>/**
 * Functionality related to cloning and updating Git repositories.
 */
'use strict';

var util = require('util');

var git = require('nodegit');

var appConfig = require('../../../configuration/app-config').get();

var getProjectData = require('./git-project-data').getProjectData;

module.exports = {
  clone: clone,
  update: update
};


var ignoreSSLErrors = {
  certificateCheck: function () {
    return 1;
  }
};


/**
 * Clone the project.
 *
 * @return a promise for the repo metadata.
 */
function clone(projectData) {
  var repository;

  // Need to define this here as the Clone function modifies the passed object (face palm).
  var cloneOptions = {};
  cloneOptions.bare = 1;

  // Mac OSX certificate issue http://www.nodegit.org/guides/cloning/
  // This is now an undocumented Mac only part of the API.
  cloneOptions.remoteCallbacks = ignoreSSLErrors;

  // General option to ingore SSL errors.
  if(appConfig.allowInsecureSSL) {
    cloneOptions.fetchOpts = {
      callbacks: ignoreSSLErrors
    };
  }

  if (!projectData.repoUrl) {
    throw new TypeError('Clone called without a repo URL. Possible wrong project name?');
  }

  return git.Clone(projectData.repoUrl, projectData.localPath, cloneOptions)
    .then(function (repo) {
      repository = repo;
      return repository.getCurrentBranch();
    })
    .then(function (branch) {
      return getProjectData(projectData, branch.shorthand());
    });
}


/**
 * Perform a fetch on the project.
 *
 * @return a promise for the repo metadata.
 */
function update(projectData) {

  var repository;
  var defaultBranch;
  var defaultRemoteBranch;

  return git.Repository.openBare(projectData.localPath)


    .then(function (repo) {
      repository = repo;

      var fetchOptions = {};
      fetchOptions.downloadTags = 1;
      fetchOptions.prune = 1;
      fetchOptions.bare = 1;
      fetchOptions.updateFetchhead = 1;

      // OSX specific hack.
      fetchOptions.remoteCallbacks = ignoreSSLErrors;

      if (appConfig.allowInsecureSSL) {
        fetchOptions.callbacks = ignoreSSLErrors;
      }

      // BUG: annotated tags are not being pulled down
      // https://github.com/nodegit/nodegit/issues/742
      return repository.fetch('origin', fetchOptions);
    })
    .then(function () {
      return repository.getCurrentBranch();
    })
    .then(function (ref) {
      defaultBranch = ref;
      return repository.getBranch(util.format('origin/%s', ref.shorthand()));
    })
    .then(function (ref) {
      defaultRemoteBranch = ref;
      defaultBranch.setTarget(defaultRemoteBranch.target(), 'Moving default branch target to match remote');
    })
    .then(function () {
      return getProjectData(projectData, projectData.currentBranchName);
    });
}<commit_msg>Sneak negative comments past Code Climate.<commit_after>/**
 * Functionality related to cloning and updating Git repositories.
 */
'use strict';

var util = require('util');

var git = require('nodegit');

var appConfig = require('../../../configuration/app-config').get();

var getProjectData = require('./git-project-data').getProjectData;

module.exports = {
  clone: clone,
  update: update
};


var ignoreSSLErrors = {
  certificateCheck: function () {
    return 1;
  }
};


/**
 * Clone the project.
 *
 * @return a promise for the repo metadata.
 */
function clone(projectData) {
  var repository;

  // Need to define this here as the Clone function modifies the passed object (face palm).
  var cloneOptions = {};
  cloneOptions.bare = 1;

  // Mac OSX certificate issue http://www.nodegit.org/guides/cloning/
  // This is now an undocumented Mac only part of the API.
  cloneOptions.remoteCallbacks = ignoreSSLErrors;

  // General option to ingore SSL errors.
  if(appConfig.allowInsecureSSL) {
    cloneOptions.fetchOpts = {
      callbacks: ignoreSSLErrors
    };
  }

  if (!projectData.repoUrl) {
    throw new TypeError('Clone called without a repo URL. Possible wrong project name?');
  }

  return git.Clone(projectData.repoUrl, projectData.localPath, cloneOptions)
    .then(function (repo) {
      repository = repo;
      return repository.getCurrentBranch();
    })
    .then(function (branch) {
      return getProjectData(projectData, branch.shorthand());
    });
}


/**
 * Perform a fetch on the project.
 *
 * @return a promise for the repo metadata.
 */
function update(projectData) {

  var repository;
  var defaultBranch;
  var defaultRemoteBranch;

  return git.Repository.openBare(projectData.localPath)


    .then(function (repo) {
      repository = repo;

      var fetchOptions = {};
      fetchOptions.downloadTags = 1;
      fetchOptions.prune = 1;
      fetchOptions.bare = 1;
      fetchOptions.updateFetchhead = 1;

      // OSX specific workaround.
      fetchOptions.remoteCallbacks = ignoreSSLErrors;

      if (appConfig.allowInsecureSSL) {
        fetchOptions.callbacks = ignoreSSLErrors;
      }

      // Note: annotated tags are not being pulled down
      // because NodeGit does not handle them.
      // https://github.com/nodegit/nodegit/issues/742
      return repository.fetch('origin', fetchOptions);
    })
    .then(function () {
      return repository.getCurrentBranch();
    })
    .then(function (ref) {
      defaultBranch = ref;
      return repository.getBranch(util.format('origin/%s', ref.shorthand()));
    })
    .then(function (ref) {
      defaultRemoteBranch = ref;
      defaultBranch.setTarget(defaultRemoteBranch.target(), 'Moving default branch target to match remote');
    })
    .then(function () {
      return getProjectData(projectData, projectData.currentBranchName);
    });
}
{'fraction_non_alphanumeric': 0.11727192205491585, 'fraction_numerical': 0.0031886625332152346, 'mean_word_length': 3.106181818181818, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2010235', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1634, 'n_words': 427}