Sha256: e83ca649e7a60f10e67ac6671ec4fd8504015889d1770d3d9b0f2f169234859a

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'tty/which'

module RubyCritic
  module SourceControlSystem
    class Git < Base
      register_system
      GIT_EXECUTABLE = TTY::Which.which('git')

      def self.git(arg)
        `#{GIT_EXECUTABLE} #{arg}`
      end

      def git(arg)
        self.class.git(arg)
      end

      def self.supported?
        git('branch 2>&1') && $?.success?
      end

      def self.to_s
        'Git'
      end

      def revisions_count(path)
        git("log --follow --format=%h #{path.shellescape}").count("\n")
      end

      def date_of_last_commit(path)
        git("log -1 --date=iso --format=%ad #{path.shellescape}").chomp!
      end

      def 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

      private

      def stash_changes
        stashes_count_before = stashes_count
        git('stash')
        stashes_count_after = stashes_count
        stashes_count_after > stashes_count_before
      end

      def stashes_count
        git('stash list --format=%h').count("\n")
      end

      def travel_to_original_state
        git('stash pop')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubycritic-3.4.0 lib/rubycritic/source_control_systems/git.rb
rubycritic-3.3.0 lib/rubycritic/source_control_systems/git.rb