<commit_before>require_relative 'util'

module TeracyDev
  class Location
    @@logger = Logging.logger_for(self)

    # location sync
    def self.sync(location, sync_existing = true)
      self.git_sync(location, sync_existing) if Util.exist? location['git']
    end

    private

    def self.git_sync(location, sync_existing)
      @@logger.debug("git_sync: location: #{location}; sync_existing: #{sync_existing}")

      git = location['git']
      branch = location['tag'] ||= location['branch']
      ref = location['ref']
      lookup_path = location['lookup_path']
      path = location['path']
      if File.exist? path
        if sync_existing == true
          @@logger.debug("git_sync: sync existing, location: #{location}")

          Dir.chdir(path) do
            @@logger.debug("Checking #{path}")

            `git remote remove origin`

            `git remote add origin #{git}`

            `git fetch origin`

            if ref
              @@logger.debug("Ref detected, checking out #{ref}")

              `git checkout #{ref}`
            else
              branch ||= 'master'

              @@logger.debug("Sync with origin/#{branch}")

              `git checkout #{branch} && git reset --hard origin/#{branch}`
            end
          end
        end
      else
        if Vagrant::Util::Which.which('git') == nil
          @@logger.error("git is not avaiable")
          abort
        end
        Dir.chdir(lookup_path) do
          @@logger.info("cd #{lookup_path} && git clone #{git}")
          system("git clone #{git}")
        end

        Dir.chdir(path) do
          @@logger.info("cd #{path} && git checkout #{branch}")
          system("git checkout #{branch}")
        end
      end
    end

    # add other types of sync protocols here when required/ implemented

  end
end<commit_msg>@ #440 | verify ref before checkout<commit_after>require_relative 'util'

module TeracyDev
  class Location
    @@logger = Logging.logger_for(self)

    # location sync
    def self.sync(location, sync_existing = true)
      self.git_sync(location, sync_existing) if Util.exist? location['git']
    end

    private

    def self.git_sync(location, sync_existing)
      @@logger.debug("git_sync: location: #{location}; sync_existing: #{sync_existing}")

      git = location['git']
      branch = location['tag'] ||= location['branch']
      ref = location['ref']
      lookup_path = location['lookup_path']
      path = location['path']
      if File.exist? path
        if sync_existing == true
          @@logger.debug("git_sync: sync existing, location: #{location}")

          Dir.chdir(path) do
            @@logger.debug("Checking #{path}")

            `git remote remove origin`

            `git remote add origin #{git}`

            `git fetch origin`

            current_ref = `git rev-parse --verify HEAD`.strip

            if ref
              @@logger.debug("Ref detected, checking out #{ref}")

              if !current_ref.include? ref
                `git checkout #{ref}`
              end
            else
              @@logger.debug("Sync with origin/#{branch}")

              branch ||= 'master'

              remote_ref = `cat .git/refs/remotes/origin/#{branch}`.strip

              if current_ref != remote_ref
                `git checkout #{branch} && git reset --hard origin/#{branch}`
              end
            end
          end
        end
      else
        if Vagrant::Util::Which.which('git') == nil
          @@logger.error("git is not avaiable")
          abort
        end
        Dir.chdir(lookup_path) do
          @@logger.info("cd #{lookup_path} && git clone #{git}")
          system("git clone #{git}")
        end

        Dir.chdir(path) do
          @@logger.info("cd #{path} && git checkout #{branch}")
          system("git checkout #{branch}")
        end
      end
    end

    # add other types of sync protocols here when required/ implemented

  end
end
{'fraction_non_alphanumeric': 0.12537993920972645, 'fraction_numerical': 0.0007598784194528875, 'mean_word_length': 1.9939347990902199, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3596071', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1069, 'n_words': 298}