Skip to content

Instantly share code, notes, and snippets.

@oa414
Created October 21, 2015 08:22
Show Gist options
  • Save oa414/41fd81f086b57459b7da to your computer and use it in GitHub Desktop.
Save oa414/41fd81f086b57459b7da to your computer and use it in GitHub Desktop.
require 'git'
# 一个计算在项目中 coding 时间的 Ruby 脚本,适用于频繁提交 commit 的个人项目。目前局限性还很大。
# 扫描 Git 的 Log,对于两个时间间隔短的 commit 那么判定这个时间段在 coding,统计的总时间会略少一点。
# 具体如何判定间隔可以修改 judge 函数。
# 使用:
# gem install git
# ruby git-time.rb ~/YOUR/GIT/DIR
g = Git.open(File.expand_path ARGV[0])
INTERVAL_BIG = 14400
INTERVAL_SMALL = 7200
def judge(l,r)
interval = r.date - l.date
if ((r.date.day == l.date.day) and (interval < INTERVAL_BIG)) or
((r.date.day != l.date.day) and (interval < INTERVAL_SMALL)) then
return true
else
return false
end
end
total = 0
r = nil
interval = 0
g.log.each do |l|
p l.date
if r and l then
#p "#{last} #{l}"
total = total + (r.date - l.date) if (judge(l,r))
end
r = l
end
p total / 3600.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment