lib/gitwakatime/commit.rb in gitwakatime-0.0.3 vs lib/gitwakatime/commit.rb in gitwakatime-0.1.0

- old
+ new

@@ -1,44 +1,41 @@ module GitWakaTime - class Commit - attr_accessor :raw_commit, :sha, :date, :message, :files, :time_in_seconds, :git, :author - - def initialize(git, commit, load_files = true) - @raw_commit = commit - @sha = @raw_commit.sha - @date = @raw_commit.date - @message = @raw_commit.message - @author = @raw_commit.author - @time_in_seconds = 0 - @git = git - @load_files = load_files - @files = get_files if load_files + class Commit < Sequel::Model + one_to_many :commited_files + def after_create + get_files end - def inspect - [sha[0..8], time_in_seconds] - end - def to_s format(' %-8s %8s %-30s %-80s'.green, sha[0..8], date, - ChronicDuration.output(time_in_seconds), + ChronicDuration.output(time_in_seconds.to_i), message ) end def oldest_dependent @files.sort { |f| f.commit.date }.first end + def time_in_seconds + commited_files.collect {|f| f.time_in_seconds}.inject(:+) + end + private def get_files + @raw_commit = GitWakaTime.config.git.gcommit(sha) # TODO: Assume gap time to lookup time prior to first commit. - return [] unless @raw_commit.parent - @raw_commit.diff_parent.stats[:files].keys.map do |file| - CommitedFile.new(git: @git, commit: @raw_commit, name: file, dependent: false) + if @raw_commit.parent + update(parent_sha: @raw_commit.parent.sha) + + @raw_commit.diff_parent.stats[:files].keys.map do |file| + CommitedFile.find_or_create(commit_id: id, name: file) do |c| + c.update(sha: sha, project: project) + end + end end end end end