Skip to content

Instantly share code, notes, and snippets.

@lawrencejones
Last active August 29, 2015 14:03
Show Gist options
  • Save lawrencejones/5edac9f00878ec2eb00b to your computer and use it in GitHub Desktop.
Save lawrencejones/5edac9f00878ec2eb00b to your computer and use it in GitHub Desktop.
A very crappy git hour calculator. Buggy as hell, but works with before/after flags also

git-hours

Relies on the git_time_extractor gem, which can be installed with gem install git git_time_extractor.

Example usage...

$ git-hours --after 06/20/2014
Lawrence Jones: 38.8hrs
#!/usr/bin/env coffee
_ = require 'underscore'
spawn = (require 'child_process').spawn
options =
after: null
before: null
Array::cons = ->
@[..-2].reduce ((a,c,i) => a.push [c,@[i+1]]; a), []
for pair in process.argv[2..].cons()
[opt, val] = pair
if (key = opt.match(/^--(\w+)$/)?[1])? and options.hasOwnProperty key
options[key] = new Date val
isBetween = (after, before, date) ->
try date = new Date(date)
catch err then return false
!((after? and date < after) or
(before? and date > before))
csv = ''
git_time = spawn 'git_time_extractor'
git_time.stdout.encoding = 'utf8'
git_time.stdout.on 'data', (data) ->
csv += data
git_time.on 'exit', (err) ->
process.exit err if err != 0
print_times process_csv csv
process_csv = (csv) ->
[headers, rows...] = (csv.split '\n').map (line) -> line.split ','
workers = new Object
sum = rows
.map (tokens) -> _.object headers, tokens
.filter (record) ->
isBetween(options.after, options.before, record.Date) &&
record.Minutes? &&
(record.Minutes = parseInt(record.Minutes, 10))
.map (c) ->
if c.Person?
workers[c.Person] = (workers[c.Person] ? 0) + c.Minutes
return workers
print_times = (workers) ->
for own person,mins of workers
console.log """
#{person}:\t#{Math.floor(mins/6)/10}hrs"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment