<commit_before># frozen_string_literal: true
# Copyright 2016 Liqwyd Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Top level module for the core Cyclid code.
module Cyclid
  # Module for the Cyclid API
  module API
    # Module for Cyclid Plugins
    module Plugins
      # Git source plugin
      class Git < Source
        # Run commands via. the transport to check out a given Git remote
        # repository
        def checkout(transport, ctx, sources = [])
          normalized_sources = normalize_and_dedup(sources)
          normalized_sources.each do |source|
            source.symbolize_keys!

            raise 'invalid git source definition' \
              unless source.key? :url

            # Add any context data (which could include secrets)
            source = source.interpolate(ctx)

            url = URI(source[:url])

            # If the source includes an OAuth token, add it to the URL as the
            # username
            url.user = source[:token] if source.key? :token

            success = transport.exec "git clone #{url}"
            return false unless success

            # If a branch was given, check it out
            if source.key? :branch
              branch = source[:branch]

              match = url.path.match(%r{^.*\/(\w*)})
              source_dir = "#{ctx[:workspace]}/#{match[1]}"

              success = transport.exec("git fetch origin #{branch}:#{branch}", source_dir)
              return false unless success

              success = transport.exec("git checkout #{branch}", source_dir)
              return false unless success
            end
          end

          return true
        end

        # Register this plugin
        register_plugin 'git'

        private

        # Remove any duplicate source definitions:
        #
        # 1. Normalize the source URLs
        # 2. Find all of the definitions for a given URL and merge them
        def normalize_and_dedup(sources)
          normalized_sources = normalize(sources)
          Cyclid.logger.debug "normalized_sources=#{normalized_sources}"
          deduped_sources = dedup(normalized_sources)
          Cyclid.logger.debug("deduped_sources=#{deduped_sources}")

          return deduped_sources
        end

        # Standardise the Git URLs. Git urls can be of the form:
        #<commit_msg>Short-circuit instead of using an entire if block<commit_after># frozen_string_literal: true
# Copyright 2016 Liqwyd Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Top level module for the core Cyclid code.
module Cyclid
  # Module for the Cyclid API
  module API
    # Module for Cyclid Plugins
    module Plugins
      # Git source plugin
      class Git < Source
        # Run commands via. the transport to check out a given Git remote
        # repository
        def checkout(transport, ctx, sources = [])
          normalized_sources = normalize_and_dedup(sources)
          normalized_sources.each do |source|
            source.symbolize_keys!

            raise 'invalid git source definition' \
              unless source.key? :url

            # Add any context data (which could include secrets)
            source = source.interpolate(ctx)

            url = URI(source[:url])

            # If the source includes an OAuth token, add it to the URL as the
            # username
            url.user = source[:token] if source.key? :token

            success = transport.exec "git clone #{url}"
            return false unless success

            # If a branch was given, check it out
            next unless source.key? :branch

            branch = source[:branch]

            match = url.path.match(%r{^.*\/(\w*)})
            source_dir = "#{ctx[:workspace]}/#{match[1]}"

            success = transport.exec("git fetch origin #{branch}:#{branch}", source_dir)
            return false unless success

            success = transport.exec("git checkout #{branch}", source_dir)
            return false unless success
          end

          return true
        end

        # Register this plugin
        register_plugin 'git'

        private

        # Remove any duplicate source definitions:
        #
        # 1. Normalize the source URLs
        # 2. Find all of the definitions for a given URL and merge them
        def normalize_and_dedup(sources)
          normalized_sources = normalize(sources)
          Cyclid.logger.debug "normalized_sources=#{normalized_sources}"
          deduped_sources = dedup(normalized_sources)
          Cyclid.logger.debug("deduped_sources=#{deduped_sources}")

          return deduped_sources
        end

        # Standardise the Git URLs. Git urls can be of the form:
        #
{'fraction_non_alphanumeric': 0.0735862311204777, 'fraction_numerical': 0.003863716192483316, 'mean_word_length': 2.2431662870159452, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4800997', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1405, 'n_words': 565}