Sha256: b9d3ad66138a61bb40209436c28099d15fe0030ccddb0642b0bd7cc0b1a39623

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

module  GitWakaTime
  class CommitedFile < Sequel::Model
    many_to_one :commit
    # No two committed files should have the same name + dependent_date this
    # means a split tree, and we should split time between the two, or more, commits.

    def before_create
      find_dependent_commit(name)
    end

    def to_s
      format('                        %-20s %-40s %-100s '.blue,
             (dependent_sha[0..8] if dependent_sha),
             ChronicDuration.output(time_in_seconds.to_f),
             name
             )
    end

    private

    def find_dependent_commit(name)
      i = 1

      # Call git log for path, loop through till we find a valid commit or run
      # out of commits to check
      commits = load_dependent_commits(name)
      begin
        commit = commits[i]

        if commit && allowed_commit(commit)

          self.dependent_date = commit.date
          self.dependent_sha = commit.sha

          # This is the magical fix for the split tree issue
          # Current though is this will fail if more than 2 split tree files
          split_tree_file = CommitedFile.where(name: name, dependent_sha: commit.sha).first
          if split_tree_file && split_tree_file.commit
            if self.commit.date < split_tree_file.commit.date
              self.dependent_date = split_tree_file.commit.date
           elsif self.commit.date > split_tree_file.commit.date
              split_tree_file.update(dependent_date: commit.date)
            end
          end
        end

        i += 1
      end until !dependent_sha.nil? || commit.nil?
    end

    def allowed_commit(commit)
      return false if commit.sha == sha
      return false if commit.author.name != GitWakaTime.config.user_name
      return false if commit.parents.size > 1
      true
    end

    def load_dependent_commits(name)
      GitWakaTime.config.git.log.object(sha).path(name)
    rescue Git::GitExecuteError
      puts error
      nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gitwakatime-0.1.2 lib/gitwakatime/commited_file.rb
gitwakatime-0.1.1 lib/gitwakatime/commited_file.rb
gitwakatime-0.1.0 lib/gitwakatime/commited_file.rb