Sha256: bf7ea7cb166e8fc67e3705f7ade53649c61b5f11eff4abaffb65bd1513beb9d8

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'git'

module Swoop

  class TimeMachine

    attr_reader :project

    def initialize(project, options)
      @project = project
      @tags = options[:tags] || 8
      @weeks = options[:weeks] || 0
      @filter = options[:filter]
    end

    def project_path
      @project_path ||= @project.path
    end

    def travel
      active_branch = git.current_branch

      begin
        logs.each do |t|
          log = t.log.first
          git.checkout(log.sha)
          yield(project, t.name, log.date)
        end
      rescue => e
        git.branches[active_branch].checkout
        raise e
      end

      git.branches[active_branch].checkout
      yield(project, active_branch, Time.now)
    end

    private

    def get_git_root
      current_dir = `pwd`
      project_dir = File.dirname(project_path)

      path = `cd #{project_dir};git rev-parse --show-toplevel;cd #{current_dir}`
      path.strip
    end

    def git
      @git ||= Git.open(get_git_root)
    end

    def logs
      return logs_by_week if @weeks > 0
      logs_by_tags
    end

    def logs_by_tags
      filtered_tags = git.tags
      filtered_tags = filtered_tags.select { |e| e.name.match(@filter) } unless @filter.nil? || @filter.empty?

      filtered_tags
        .sort { |a, b| a.log.first.date <=> b.log.first.date }
        .last(@tags)
    end

    def logs_by_week
      log = git.log(5000).since("#{@weeks+1} weeks ago")

      logs = []
      (0..log.size-1).each { |i| logs << log[i] }

      logs
        .reduce({}) { |memo, l|
          week = l.date.strftime('%W')
          memo[week] = l
          memo
        }
        .values
        .reverse
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
swoop_report-0.3.1 lib/swoop/time_machine.rb
swoop_report-0.3 lib/swoop/time_machine.rb