<commit_before>module BullsTools

  module Cmd

    def run_cmd(cmd)
      puts nil, cmd
      out = `#{cmd}`
      puts out.gsub(/^/,"  ...\t") unless out == ''
      raise "Failure while trying to run '#{cmd}': #{$?}" unless $?.success?
      out
    end

    def wrap_errors(options=nil, &block)
      options ||= {}
      yield
      true
    rescue => e
      if options[:rescue]
        begin
          options[:rescue].call
        rescue => e2
          puts e2
        end
      end
      puts '', "ERROR: #{e}"
      false
    end


    class << self

      def check_args_for_help(msg, args=nil, options=nil)
        args ||= ARGV
        options ||= {}
        # Look for a help flag.
        wants_help = (args & ['help', '--help', '-h', '-help', '-?', '?']).length > 0
        # Prevent empty args.
        # Use a flag to allow empty args.
        unless options[:skip_empty_check]
          no_args = args.empty?
          if args.length == 1 && ['-y', '--yes', '-f', '--force'].include?(args.first)
            args.shift # remove the flag.
          end
        end
        if wants_help || no_args
          unless options[:skip_empty_check]
            msg += "\n" unless msg.match(/\n\z/)
            msg += "(Empty args safety check: use '-y' option to run with default args.)\n"
          end
          puts msg
          exit
        end
      end

    end # class << self

  end


  module Git

    class << self
      include Cmd

      def force_push_safely(local=nil, remote=nil, repo=nil)
        wrap_errors do
          force_push_safely_helper(local, remote, repo)
        end
      end

      def merge_safely(child, parent=nil, remote=nil)
        wrap_errors(rescue: ->{ run_cmd("git status") }) do
          merge_safely_helper(child, parent, remote)
        end
      end

      def grep_except_libs(args)
        wrap_errors do
          grep_except_libs_helper(args)
        end
      end


      private

      EXCLUDED_PATHS = %w( node_modules vendor.* ).freeze
      def grep_except_libs_helper(args)
        excluded = EXCLUDED_PATHS.collect{|p| "^#{p}/"}.join('|')
        cmd = "git grep #{args.join(' ')} -- `git ls-files | grep -vE '#{excluded}'`"
        puts cmd
        puts
        cmd += ' ; echo \'\n'+cmd.split("'").join("'\\''")+"'"
        # cmd.gsub("'", "'\\''") doesn't work - due to back-references?
        exec(cmd)
      end

      def force_push_safely_helper(local=nil, remote=nil, repo=nil)
        local, remote, repo = default_branch_params(local, remote, repo)

        # Get the latest updates from the remote repo.
        run_cmd("git fetch #{repo} #{remote}")

        # Look for the last commit in the local branch.<commit_msg>Enabled more complex args for git-grep-except-libs.<commit_after>module BullsTools

  module Cmd

    def run_cmd(cmd)
      puts nil, cmd
      out = `#{cmd}`
      puts out.gsub(/^/,"  ...\t") unless out == ''
      raise "Failure while trying to run '#{cmd}': #{$?}" unless $?.success?
      out
    end

    def wrap_errors(options=nil, &block)
      options ||= {}
      yield
      true
    rescue => e
      if options[:rescue]
        begin
          options[:rescue].call
        rescue => e2
          puts e2
        end
      end
      puts '', "ERROR: #{e}"
      false
    end

    def escape_quotes(cmd)
      # cmd.gsub("'", "'\\''") doesn't work - due to back-references?
      cmd.split("'").join("'\\''")
    end


    class << self

      def check_args_for_help(msg, args=nil, options=nil)
        args ||= ARGV
        options ||= {}
        # Look for a help flag.
        wants_help = (args & ['help', '--help', '-h', '-help', '-?', '?']).length > 0
        # Prevent empty args.
        # Use a flag to allow empty args.
        unless options[:skip_empty_check]
          no_args = args.empty?
          if args.length == 1 && ['-y', '--yes', '-f', '--force'].include?(args.first)
            args.shift # remove the flag.
          end
        end
        if wants_help || no_args
          unless options[:skip_empty_check]
            msg += "\n" unless msg.match(/\n\z/)
            msg += "(Empty args safety check: use '-y' option to run with default args.)\n"
          end
          puts msg
          exit
        end
      end

    end # class << self

  end


  module Git

    class << self
      include Cmd

      def force_push_safely(local=nil, remote=nil, repo=nil)
        wrap_errors do
          force_push_safely_helper(local, remote, repo)
        end
      end

      def merge_safely(child, parent=nil, remote=nil)
        wrap_errors(rescue: ->{ run_cmd("git status") }) do
          merge_safely_helper(child, parent, remote)
        end
      end

      def grep_except_libs(args)
        wrap_errors do
          grep_except_libs_helper(args)
        end
      end


      private

      EXCLUDED_PATHS = %w( node_modules vendor.* ).freeze
      def grep_except_libs_helper(args)
        excluded = EXCLUDED_PATHS.collect{|p| "^#{p}/"}.join('|')
        cmd = "git grep "+args.collect{|a| "'"+escape_quotes(a)+"'"}.join(' ')+" -- `git ls-files | grep -vE '#{excluded}'`"
        puts cmd
        puts
        cmd += ' ; echo \'\n'+escape_quotes(cmd)+"'"
        exec(cmd)
      end

      def force_push_safely_helper(local=nil, remote=nil, repo=nil)
        local, remote, repo = default_branch_params(local, remote, repo)

        # Get the latest updates from the remote repo.
        run_cmd("git fetch #{repo} #{remote}")

        # Look for the last commit in the local branch.
{'fraction_non_alphanumeric': 0.13687251715420728, 'fraction_numerical': 0.001444564824846515, 'mean_word_length': 1.862532299741602, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '4959757', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1735, 'n_words': 475}