<commit_before>var git = require('gift')
, fs = require('fs')
, Jobs = require('./Jobs');

var GIT_DIR = 'views';

var LATEST_COMMIT;

var GitManager = function(){
    var me = this;
    me.repo = null;
    me.checkoutOrClone(function(){
        winston.info('Git: initialization of version control system complete for directory: '+GIT_DIR+' on branch: '+ENV_CONFIG.CMS.branchName);
        Jobs.create('GitUpdate', ENV_CONFIG.CMS.syncInterval, function(opts){
            me.sync(opts.success, opts.error);
        });
        Jobs.start('GitUpdate', function(job, hasFailed){
            if(!hasFailed){
                winston.verbose('Git: synchronization with remote branch: '+ENV_CONFIG.CMS.branchName+' complete at commit hash: '+LATEST_COMMIT);
            }
        });
    });
};

// checkout or clone repository
GitManager.prototype.checkoutOrClone = function(callback){
    var me = this;
    me.initAndCheckout(function(e){
        if(e){
            me.clone(callback);
        }else{
            callback();
        }
    });
};

// clone the repository
GitManager.prototype.clone = function(callback){
    var me = this;
    git.clone(ENV_CONFIG.CMS.repo, GIT_DIR, function(e, repo){
        if(e && e.code == 128){
            winston.info('Git: deleting and recreating folder "'+GIT_DIR+'" which was not under source control.');
            deleteFolder('./'+GIT_DIR);
            me.clone(callback);
        }else if(e){
            winston.error('Git: error cloning repo: ', e);
            throw(e);
        }else{
            me.repo = repo;
            me.checkout(callback);
        }
    });
};

// initialize and checkout the repository
GitManager.prototype.initAndCheckout = function(callback){
    var me = this;
    git.init(GIT_DIR, function(e, repo){
        if(e && e.code == 'ENOENT'){
            winston.info('Git: the folder "'+GIT_DIR+'" did not exist, attempting to clone repository.');
            callback('missing-git-folder');
        }else{
            me.repo = repo;
            me.checkout(callback);
        }
    });
};

// checkout the repository
GitManager.prototype.checkout = function(callback){
    this.repo.checkout(ENV_CONFIG.CMS.branchName, function(e, report){
        if(e){
            winston.error('Git: error checking out branch: ', e);
            callback('error-checkout-branch');
        }else{
            winston.info(report);
            callback(null, report);
        }
    });
};

// sync branch with remote
GitManager.prototype.sync = function(callback, failback){
    var me = this;
    me.repo.branches(function(e, heads){
        if(e){<commit_msg>catch error<commit_after>var git = require('gift')
, fs = require('fs')
, Jobs = require('./Jobs');

var GIT_DIR = 'views';

var LATEST_COMMIT;

var GitManager = function(){
    var me = this;
    me.repo = null;
    me.checkoutOrClone(function(){
        winston.info('Git: initialization of version control system complete for directory: '+GIT_DIR+' on branch: '+ENV_CONFIG.CMS.branchName);
        Jobs.create('GitUpdate', ENV_CONFIG.CMS.syncInterval, function(opts){
            me.sync(opts.success, opts.error);
        });
        Jobs.start('GitUpdate', function(job, hasFailed){
            if(!hasFailed){
                winston.verbose('Git: synchronization with remote branch: '+ENV_CONFIG.CMS.branchName+' complete at commit hash: '+LATEST_COMMIT);
            }
        });
    });
};

// checkout or clone repository
GitManager.prototype.checkoutOrClone = function(callback){
    var me = this;
    me.initAndCheckout(function(e){
        if(e){
            me.clone(callback);
        }else{
            callback();
        }
    });
};

// clone the repository
GitManager.prototype.clone = function(callback){
    var me = this;
    git.clone(ENV_CONFIG.CMS.repo, GIT_DIR, function(e, repo){
        if(e && e.code == 128){
            winston.info('Git: deleting and recreating folder "'+GIT_DIR+'" which was not under source control.');
            deleteFolder('./'+GIT_DIR);
            me.clone(callback);
        }else if(e){
            winston.error('Git: error cloning repo: ', e);
            throw(e);
        }else{
            me.repo = repo;
            me.checkout(callback);
        }
    });
};

// initialize and checkout the repository
GitManager.prototype.initAndCheckout = function(callback){
    var me = this;
    git.init(GIT_DIR, function(e, repo){
        if(e && e.code == 'ENOENT'){
            winston.info('Git: the folder "'+GIT_DIR+'" did not exist, attempting to clone repository.');
            callback('missing-git-folder');
        }else if(e){
            winston.error('Git: error initializing the repo: ', e);
            throw(e);
        }else{
            me.repo = repo;
            me.checkout(callback);
        }
    });
};

// checkout the repository
GitManager.prototype.checkout = function(callback){
    this.repo.checkout(ENV_CONFIG.CMS.branchName, function(e, report){
        if(e){
            winston.error('Git: error checking out branch: ', e);
            callback('error-checkout-branch');
        }else{
            winston.info(report);
            callback(null, report);
        }
    });
};

// sync branch with remote
GitManager.prototype.sync = function(callback, failback){
    var me = this;
    me.repo.branches(function(e, heads){
        if(e){
{'fraction_non_alphanumeric': 0.14219070722149654, 'fraction_numerical': 0.0011196118678857996, 'mean_word_length': 2.364720652856246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6465095', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1583, 'n_words': 357}