Sha256: c0b036192cf352f41b3f8b78bbc5ae6c03cb7f49c3a629d172dfb183d5114e8c

Contents?: true

Size: 1.57 KB

Versions: 55

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true
module Licensed
  module Git
    class << self
      # Returns whether git commands are available
      def available?
        @git ||= Licensed::Shell.tool_available?("git")
      end

      # Returns the root of the current git repository
      # or nil if not in a git repository.
      def repository_root
        return unless available?
        root = Licensed::Shell.execute("git", "rev-parse", "--show-toplevel", allow_failure: true)
        return nil if root.empty?
        root
      end

      # Returns true if a git repository is found, false otherwise
      def git_repo?
        !repository_root.to_s.empty?
      end

      # Returns the most recent git SHA for a file or directory
      # or nil if SHA is not available
      #
      # descriptor - file or directory to retrieve latest SHA for
      def version(descriptor)
        return unless git_repo? && descriptor
        Licensed::Shell.execute("git", "rev-list", "-1", "HEAD", "--", descriptor, allow_failure: true)
      end

      # Returns the commit date for the provided SHA as a timestamp
      #
      # sha - commit sha to retrieve date
      def commit_date(sha)
        return unless git_repo? && sha
        Licensed::Shell.execute("git", "show", "-s", "-1", "--format=%ct", sha)
      end

      # Returns the files in the git repository from `git ls-files --recurse-submodules`
      def files
        return unless git_repo?
        output = Licensed::Shell.execute("git", "ls-files", "--full-name", "--recurse-submodules")
        output.lines.map(&:strip)
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
licensed-3.7.0 lib/licensed/git.rb
licensed-3.6.0 lib/licensed/git.rb
licensed-3.5.0 lib/licensed/git.rb
licensed-3.4.4 lib/licensed/git.rb
licensed-3.4.3 lib/licensed/git.rb
licensed-3.4.2 lib/licensed/git.rb
licensed-3.4.1 lib/licensed/git.rb
licensed-3.4.0 lib/licensed/git.rb
licensed-3.3.1 lib/licensed/git.rb
licensed-3.3.0 lib/licensed/git.rb
licensed-3.2.3 lib/licensed/git.rb
licensed-3.2.2 lib/licensed/git.rb
licensed-3.2.1 lib/licensed/git.rb
licensed-3.2.0 lib/licensed/git.rb
licensed-3.1.0 lib/licensed/git.rb
licensed-3.0.1 lib/licensed/git.rb
licensed-3.0.0 lib/licensed/git.rb
licensed-2.15.2 lib/licensed/git.rb
licensed-2.15.1 lib/licensed/git.rb
licensed-2.15.0 lib/licensed/git.rb