<commit_before>#!/usr/bin/env node

var Promise = require('bluebird');
var git = require('nodegit');
var moment = require('moment');
var _ = require('lodash');

var config = {
    // Maximum time diff between 2 subsequent commits in minutes which are
    // counted to be in the same coding "session"
    maxCommitDiffInMinutes: 240,

    // How many minutes should be added for the first commit of coding session
    firstCommitAdditionInMinutes: 240
};

function main() {
    commits('.').then(function(commits) {
        var work = {
            total: {
                hours: estimateHours(_.pluck(commits, 'date')),
                commits: commits.length
            }
        };

        console.log(JSON.stringify(work, undefined, 2));
    }).catch(function(e) {
        console.error(e.stack);
    });
}

// Estimates spent working hours based on commit dates
function estimateHours(dates) {
    if (dates.length < 2) {
        return 0;
    }

    // Oldest commit first, newest last
    var sortedDates = dates.sort(function(a, b) {
        return a - b;
    });
    var allButLast = _.take(sortedDates, sortedDates.length - 1);

    var hours = _.reduce(allButLast, function(hours, date, index) {
        var nextDate = sortedDates[index + 1];
        var diffInMinutes = (nextDate - date) / 1000 / 60;

        // Check if commits are counted to be in same coding session
        if (diffInMinutes < config.maxCommitDiffInMinutes) {
            return hours + (diffInMinutes / 60);
        }

        // The date difference is too big to be inside single coding session
        // The work of first commit of a session cannot be seen in git history,
        // so we make a blunt estimate of it
        return hours + (config.firstCommitAdditionInMinutes / 60);
    }, 0);

    return Math.round(hours);
}


// Promisify nodegit's API of getting all commits in repository
function commits(gitPath) {
    return new Promise(function(resolve, reject) {
        git.Repo.open(gitPath, function(err, repo) {
            if (err) {
                reject(err);
                return;
            }

            repo.getMaster(function(err, branch) {
                if (err) {
                    reject(err);
                    return;
                }

                var history = branch.history();
                var commits = [];

                history.on("commit", function(commit) {
                    var author = null;
                    if (!_.isNull(commit.author())) {
                        author = {
                            name: commit.author().name(),
                            email: commit.author().email()
                        }
                    }
                    
                    var commitData = {
                        sha: commit.sha(),
                        date: commit.date(),<commit_msg>Fix linter error<commit_after>#!/usr/bin/env node

var Promise = require('bluebird');
var git = require('nodegit');
var moment = require('moment');
var _ = require('lodash');

var config = {
    // Maximum time diff between 2 subsequent commits in minutes which are
    // counted to be in the same coding "session"
    maxCommitDiffInMinutes: 240,

    // How many minutes should be added for the first commit of coding session
    firstCommitAdditionInMinutes: 240
};

function main() {
    commits('.').then(function(commits) {
        var work = {
            total: {
                hours: estimateHours(_.pluck(commits, 'date')),
                commits: commits.length
            }
        };

        console.log(JSON.stringify(work, undefined, 2));
    }).catch(function(e) {
        console.error(e.stack);
    });
}

// Estimates spent working hours based on commit dates
function estimateHours(dates) {
    if (dates.length < 2) {
        return 0;
    }

    // Oldest commit first, newest last
    var sortedDates = dates.sort(function(a, b) {
        return a - b;
    });
    var allButLast = _.take(sortedDates, sortedDates.length - 1);

    var hours = _.reduce(allButLast, function(hours, date, index) {
        var nextDate = sortedDates[index + 1];
        var diffInMinutes = (nextDate - date) / 1000 / 60;

        // Check if commits are counted to be in same coding session
        if (diffInMinutes < config.maxCommitDiffInMinutes) {
            return hours + (diffInMinutes / 60);
        }

        // The date difference is too big to be inside single coding session
        // The work of first commit of a session cannot be seen in git history,
        // so we make a blunt estimate of it
        return hours + (config.firstCommitAdditionInMinutes / 60);
    }, 0);

    return Math.round(hours);
}


// Promisify nodegit's API of getting all commits in repository
function commits(gitPath) {
    return new Promise(function(resolve, reject) {
        git.Repo.open(gitPath, function(err, repo) {
            if (err) {
                reject(err);
                return;
            }

            repo.getMaster(function(err, branch) {
                if (err) {
                    reject(err);
                    return;
                }

                var history = branch.history();
                var commits = [];

                history.on("commit", function(commit) {
                    var author = null;
                    if (!_.isNull(commit.author())) {
                        author = {
                            name: commit.author().name(),
                            email: commit.author().email()
                        };
                    }

                    var commitData = {
                        sha: commit.sha(),
                        date: commit.date(),
{'fraction_non_alphanumeric': 0.10403508771929824, 'fraction_numerical': 0.008070175438596491, 'mean_word_length': 1.6503951650395166, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1098878', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1463, 'n_words': 453}