<commit_before>require 'digest'

module GitRunner
  class Instruction
    class Base
    end

    # Performs deployments using capistrano (cap deploy)
    class Deploy < Base
      VERSION = '0.1.4'

      attr_accessor :clone_directory


      def should_run?
        branches.empty? || branches.include?(branch.name)
      end

      def perform
        GitRunner::Hooks.fire(:deploy_begin, self)

        start_time = Time.now

        Text.out(Text.green("Performing Deploy (#{environment_from_branch(branch)})"), :heading)

        checkout_branch
        ensure_presence_of_capfile
        prepare_deploy_environment
        perform_deploy

        end_time = Time.now

        Text.out(Text.green("\u2714 Deploy successful, completed in #{(end_time - start_time).ceil} seconds"))


      rescue Exception => ex
        GitRunner::Hooks.fire(:deploy_failure, self)
        raise ex
      end


    private
      def branches
        args.split(/\s+/)
      end

      def ensure_presence_of_capfile
        unless File.exists?("#{clone_directory}/Capfile")
          Text.out(Text.red("Missing Capfile, unable to complete deploy."))
          fail!
        end
      end

      def uses_bundler?
        File.exists?("#{clone_directory}/Gemfile")
      end

      def multistage?
        result = execute("grep -e 'require.*capistrano.*multistage' #{clone_directory}/#{Configuration.instruction_file} || true")
        !result.empty?
      end

      def checkout_branch
        self.clone_directory = File.join(Configuration.tmp_directory, "#{branch.repository_name}")
        revision             = execute("git ls-remote file://#{branch.repository_path} #{branch.name}").split("\t")[0]

        if File.exist?(clone_directory)
          Text.out("Checking out #{branch.name} to #{clone_directory}")

          execute(
            "cd #{clone_directory}",
            "git fetch origin",
            "git fetch --tags origin",
            "git reset --hard #{revision}",
            "git clean -x -f"
          )
        else
          Text.out("Checking out #{branch.name} to #{clone_directory} (fresh clone)")

          execute(
            "mkdir -p #{clone_directory}",
            "git clone file://#{branch.repository_path} #{clone_directory}",
            "cd #{clone_directory}",
            "git checkout -b git-runner-deploy #{revision}"
          )
        end
      end

      def prepare_deploy_environment
        Text.out("Preparing deploy environment")

        if uses_bundler?
          execute(
            "cd #{clone_directory}",
            "bundle install --path=.git-runner/gems"
          )
        end
      end

      def perform_deploy
        cap_deploy_command = if multistage?
          Text.out("Deploying application (multistage detected)")
          "cap #{environment_from_branch(branch)} deploy"
        else
          Text.out("Deploying application")
          "cap deploy"
        end

        Text.indent do
          execute(
            "cd #{clone_directory}",
            cap_deploy_command,
            :errproc => method(:cap_deploy_outproc)
          )
        end

        GitRunner::Hooks.fire(:deploy_success, self)
      end

      def cap_deploy_outproc(out)
        if out =~ /executing `(.*)'/
          case $1
          when 'bundle:install'
            Text.out('* Installing gems')

          when 'assets:precompile'
            Text.out('* Precompilng assets')

          when 'deploy:restart'
            Text.out('* Restarting application')
          end
        end
      end
<commit_msg>cap deploy uses bundle exec if bundler is being used. updated check for asset precompilation stage of deployment<commit_after>require 'digest'

module GitRunner
  class Instruction
    class Base
    end

    # Performs deployments using capistrano (cap deploy)
    class Deploy < Base
      VERSION = '0.1.5'

      attr_accessor :clone_directory


      def should_run?
        branches.empty? || branches.include?(branch.name)
      end

      def perform
        GitRunner::Hooks.fire(:deploy_begin, self)

        start_time = Time.now

        Text.out(Text.green("Performing Deploy (#{environment_from_branch(branch)})"), :heading)

        checkout_branch
        ensure_presence_of_capfile
        prepare_deploy_environment
        perform_deploy

        end_time = Time.now

        Text.out(Text.green("\u2714 Deploy successful, completed in #{(end_time - start_time).ceil} seconds"))


      rescue Exception => ex
        GitRunner::Hooks.fire(:deploy_failure, self)
        raise ex
      end


    private
      def branches
        args.split(/\s+/)
      end

      def ensure_presence_of_capfile
        unless File.exists?("#{clone_directory}/Capfile")
          Text.out(Text.red("Missing Capfile, unable to complete deploy."))
          fail!
        end
      end

      def uses_bundler?
        File.exists?("#{clone_directory}/Gemfile")
      end

      def multistage?
        result = execute("grep -e 'require.*capistrano.*multistage' #{clone_directory}/#{Configuration.instruction_file} || true")
        !result.empty?
      end

      def checkout_branch
        self.clone_directory = File.join(Configuration.tmp_directory, "#{branch.repository_name}")
        revision             = execute("git ls-remote file://#{branch.repository_path} #{branch.name}").split("\t")[0]

        if File.exist?(clone_directory)
          Text.out("Checking out #{branch.name} to #{clone_directory}")

          execute(
            "cd #{clone_directory}",
            "git fetch origin",
            "git fetch --tags origin",
            "git reset --hard #{revision}",
            "git clean -x -f"
          )
        else
          Text.out("Checking out #{branch.name} to #{clone_directory} (fresh clone)")

          execute(
            "mkdir -p #{clone_directory}",
            "git clone file://#{branch.repository_path} #{clone_directory}",
            "cd #{clone_directory}",
            "git checkout -b git-runner-deploy #{revision}"
          )
        end
      end

      def prepare_deploy_environment
        Text.out("Preparing deploy environment")

        if uses_bundler?
          execute(
            "cd #{clone_directory}",
            "bundle install --path=.git-runner/gems"
          )
        end
      end

      def perform_deploy
        cap_deploy_command = if multistage?
          Text.out("Deploying application (multistage detected)")
          "cap #{environment_from_branch(branch)} deploy"
        else
          Text.out("Deploying application")
          "cap deploy"
        end

        # 'bundle exec' if bundler is being used
        cap_deploy_command = "bundle exec #{cap_deploy_command}" if uses_bundler?

        Text.indent do
          execute(
            "cd #{clone_directory}",
            cap_deploy_command,
            :errproc => method(:cap_deploy_outproc)
          )
        end

        GitRunner::Hooks.fire(:deploy_success, self)
      end

      def cap_deploy_outproc(out)
        if out =~ /executing `(.*)'/
          case $1
          when 'deploy:update_code'
            Text.out('* Copying application code')

          when 'bundle:install'
            Text.out('* Installing gems')

          when 'assets:precompile', 'deploy:assets:precompile'
            Text.out('* Precompilng assets')

          when 'deploy:restart'
            Text.out('* Restarting application')
          end
        end
      end

{'fraction_non_alphanumeric': 0.10617217833712679, 'fraction_numerical': 0.0024099611728477708, 'mean_word_length': 1.9927884615384615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '481661', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2032, 'n_words': 479}