Sha256: f33675ab2983170828ad372ff0763f0e954ed52f4aa1791c1bbae50a02d9c65c

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Rubycritic

  class Git < SourceControlSystem
    register_system

    def self.supported?
      `git branch 2>&1` && $?.success?
    end

    def self.to_s
      "Git"
    end

    def has_revision?
      head_reference && $?.success?
    end

    def head_reference
      `git rev-parse --verify HEAD`.chomp
    end

    def travel_to_head
      stash_successful = stash_changes
      yield
    ensure
      travel_to_original_state if stash_successful
    end

    def revisions_count(file)
      `git log --follow --format=oneline #{file}`.count("\n")
    end

    private

    def stash_changes
      return false if everything_commmited?

      stashes_count_before = stashes_count
      `git stash`
      stashes_count_after = stashes_count
      stashes_count_after > stashes_count_before
    end

    def everything_commmited?
      `git status --porcelain`.empty?
    end

    def stashes_count
      `git stash list`.split("\n").length
    end

    def travel_to_original_state
      `git stash pop`
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubycritic-0.0.11 lib/rubycritic/source_control_systems/git.rb