<commit_before>require 'fileutils'
require 'pathname'
require 'digest'

require 'librarian/particularity'
require 'librarian/source/git/repository'
require 'librarian/source/local'

module Librarian
  module Source
    class Git

      include Particularity
      include Local

      class << self
        LOCK_NAME = 'GIT'
        def lock_name
          LOCK_NAME
        end
        def from_lock_options(options)
          new(options[:remote], options.reject{|k, v| k == :remote})
        end
      end

      DEFAULTS = {
        :ref => 'master'
      }

      attr_reader :uri, :ref, :sha, :path

      def initialize(uri, options = {})
        @uri = uri
        @ref = options[:ref] || DEFAULTS[:ref]
        @sha = options[:sha]
        @path = options[:path]
        @repository = nil
        @repository_cache_path = nil
      end

      def to_s
        "#{uri}##{ref}"
      end

      def ==(other)
        other &&
        self.class  == other.class  &&
        self.uri    == other.uri    &&
        self.ref    == other.ref    &&
        self.path   == other.path   &&
        (self.sha.nil? || other.sha.nil? || self.sha == other.sha)
      end

      def to_spec_args
        options = {:ref => ref}
        options.merge!(:path => path) if path
        [uri, options]
      end

      def to_lock_options
        options = {:remote => uri, :ref => ref, :sha => sha}
        options.merge!(:path => path) if path
        options
      end

      def cache!(dependencies)
        unless repository.git?
          repository.path.rmtree if repository.path.exist?
          repository.path.mkpath
          repository.clone!(uri)
        end
        unless sha == repository.current_commit_hash
          repository.checkout!(sha || ref)
          @sha ||= repository.current_commit_hash
        end
      end

      def repository_cache_path
        @repository_cache_path ||= begin
          dir = path ? "#{uri}/#{path}" : uri
          dir = Digest::MD5.hexdigest(dir)
          root_module.cache_path.join("source/git/#{dir}")
        end
      end

      def repository
        @repository ||= begin
          Repository.new(root_module, repository_cache_path)
        end
      end

      def filesystem_path
        @filesystem_path ||= repository.path
      end

      # Override Local#manifest_search_paths
      def manifest_search_paths(dependency)
        if path.nil?
          paths = [filesystem_path, filesystem_path.join(dependency.name)]
          paths.select{|s| s.exist?}
        else
          [filesystem_path.join(path)]
        end
      end

    end
  end
end<commit_msg>The filesystem path for a git source should include the path declared for it, if any. That removes the need to special-case the search for manifests.<commit_after>require 'fileutils'
require 'pathname'
require 'digest'

require 'librarian/particularity'
require 'librarian/source/git/repository'
require 'librarian/source/local'

module Librarian
  module Source
    class Git

      include Particularity
      include Local

      class << self
        LOCK_NAME = 'GIT'
        def lock_name
          LOCK_NAME
        end
        def from_lock_options(options)
          new(options[:remote], options.reject{|k, v| k == :remote})
        end
      end

      DEFAULTS = {
        :ref => 'master'
      }

      attr_reader :uri, :ref, :sha, :path

      def initialize(uri, options = {})
        @uri = uri
        @ref = options[:ref] || DEFAULTS[:ref]
        @sha = options[:sha]
        @path = options[:path]
        @repository = nil
        @repository_cache_path = nil
      end

      def to_s
        path ? "#{uri}##{ref}(#{path})" : "#{uri}##{ref}"
      end

      def ==(other)
        other &&
        self.class  == other.class  &&
        self.uri    == other.uri    &&
        self.ref    == other.ref    &&
        self.path   == other.path   &&
        (self.sha.nil? || other.sha.nil? || self.sha == other.sha)
      end

      def to_spec_args
        options = {:ref => ref}
        options.merge!(:path => path) if path
        [uri, options]
      end

      def to_lock_options
        options = {:remote => uri, :ref => ref, :sha => sha}
        options.merge!(:path => path) if path
        options
      end

      def cache!(dependencies)
        unless repository.git?
          repository.path.rmtree if repository.path.exist?
          repository.path.mkpath
          repository.clone!(uri)
        end
        unless sha == repository.current_commit_hash
          repository.checkout!(sha || ref)
          @sha ||= repository.current_commit_hash
        end
      end

      def repository_cache_path
        @repository_cache_path ||= begin
          dir = path ? "#{uri}/#{path}" : uri
          dir = Digest::MD5.hexdigest(dir)
          root_module.cache_path.join("source/git/#{dir}")
        end
      end

      def repository
        @repository ||= begin
          Repository.new(root_module, repository_cache_path)
        end
      end

      def filesystem_path
        @filesystem_path ||= path ? repository.path.join(path) : repository.path
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.11044137662842698, 'fraction_numerical': 0.00038887808671981335, 'mean_word_length': 1.8850252383623107, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5802463', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1489, 'n_words': 361}