<commit_before># vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2

# Copyright 2013-present Facebook
#
# 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.

require 'pathname'
require 'mixlib/shellout'
require 'between_meals/changeset'
require 'between_meals/repo/hg/cmd'

module BetweenMeals
  class Repo
    class Hg < BetweenMeals::Repo
      def setup
        @bin = 'hg'
        @cmd = BetweenMeals::Repo::Hg::Cmd.new(
          :bin => @bin,
          :cwd => @repo_path,
          :logger => @logger,
        )
      end

      def exists?
        Dir.exists?(Pathname.new(@repo_path).join('.hg'))
      end

      def head_rev
        @cmd.log('node').stdout
      end

      def checkout(url)
        @cmd.clone(url, @repo_path)
      end

      # Return files changed between two revisions
      def changes(start_ref, end_ref)
        valid_ref?(start_ref)
        valid_ref?(end_ref) if end_ref
        stdout = @cmd.status(start_ref, end_ref).stdout
        begin
          parse_status(stdout).compact
        rescue StandardError => e
          # We've seen some weird non-reproducible failures here
          @logger.error(
            'Something went wrong. Please report this output.',
          )
          @logger.error(e)
          stdout.lines.each do |line|
            @logger.error(line.strip)
          end
          exit(1)
        end
      end

      def update
        @cmd.pull.stdout
      rescue StandardError
        @logger.error('Something went wrong with hg!')
        @logger.error(cmd.stdout)
        raise
      end

      # Return all files
      def files
        @cmd.manifest.stdout.split("\n").map do |x|
          { :path => x, :status => :created }
        end
      end

      def head_parents
        [{
          :time => Time.parse(@cmd.log('date|isodate', 'master').stdout),
          :rev => @cmd.log('node', 'master').stdout,
        }]
      rescue StandardError
        [{
          :time => nil,
          :rev => nil,
        }]
      end<commit_msg>Fix logging. If `hg pull` fails shell out throws exception, we catch it and try to log cmd.stdout. There are 2 issues with this: 1. cmd is not defined, but there is @cmd 2. The cmd object with stdout is returned as result as call, but since it's interruped with exception, we cannot get that stdout.<commit_after># vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2

# Copyright 2013-present Facebook
#
# 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.

require 'pathname'
require 'mixlib/shellout'
require 'between_meals/changeset'
require 'between_meals/repo/hg/cmd'

module BetweenMeals
  class Repo
    class Hg < BetweenMeals::Repo
      def setup
        @bin = 'hg'
        @cmd = BetweenMeals::Repo::Hg::Cmd.new(
          :bin => @bin,
          :cwd => @repo_path,
          :logger => @logger,
        )
      end

      def exists?
        Dir.exists?(Pathname.new(@repo_path).join('.hg'))
      end

      def head_rev
        @cmd.log('node').stdout
      end

      def checkout(url)
        @cmd.clone(url, @repo_path)
      end

      # Return files changed between two revisions
      def changes(start_ref, end_ref)
        valid_ref?(start_ref)
        valid_ref?(end_ref) if end_ref
        stdout = @cmd.status(start_ref, end_ref).stdout
        begin
          parse_status(stdout).compact
        rescue StandardError => e
          # We've seen some weird non-reproducible failures here
          @logger.error(
            'Something went wrong. Please report this output.',
          )
          @logger.error(e)
          stdout.lines.each do |line|
            @logger.error(line.strip)
          end
          exit(1)
        end
      end

      def update
        @cmd.pull.stdout
      rescue StandardError => e
        @logger.error('Something went wrong with hg!')
        @logger.error(e)
        raise
      end

      # Return all files
      def files
        @cmd.manifest.stdout.split("\n").map do |x|
          { :path => x, :status => :created }
        end
      end

      def head_parents
        [{
          :time => Time.parse(@cmd.log('date|isodate', 'master').stdout),
          :rev => @cmd.log('node', 'master').stdout,
        }]
      rescue StandardError
        [{
          :time => nil,
          :rev => nil,
        }]
      end
{'fraction_non_alphanumeric': 0.1016949152542373, 'fraction_numerical': 0.005084745762711864, 'mean_word_length': 2.1859628074385125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '941958', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1512, 'n_words': 480}