Sha256: b5b49170d6563e9d7a5a4b1393f44ae0f5bc108e5d45b1ebe50026357527bbfb

Contents?: true

Size: 948 Bytes

Versions: 3

Compression:

Stored size: 948 Bytes

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class Git
      attr_reader :root, :url

      def initialize(url:)
        @url = url
        @root = path_for(url)
      end

      def read(path)
        full_path = File.join(root, path)

        IO.read(full_path) if File.exist?(full_path)
      end

      def update!
        dotgit? ? pull! : clone!
      end

      private

      def path_for(url)
        uri = URI.parse(url)
        name = uri.path.gsub(/\.git$/, '')
        File.expand_path(File.join(Dir.home, '.local', 'share', name))
      end

      def dotgit?
        File.directory?(File.join(root, '.git'))
      end

      def clone!
        system('git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', 'master', url, root)
      end

      def pull!
        Dir.chdir(root) do
          system('git', 'pull', '--no-rebase', '--quiet', 'origin', 'master')
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spandx-0.13.4 lib/spandx/core/git.rb
spandx-0.13.3 lib/spandx/core/git.rb
spandx-0.13.2 lib/spandx/core/git.rb