<commit_before>require 'net/ssh'
require 'tempfile'
require 'tmpdir'
require 'stringio'

module GitHosting
  TEMP_DATA_DIR = "/tmp/redmine_git_hosting" # In case settings not migrated (normally from settings)
  SCRIPT_DIR = ""                            # In case settings not migrated (normally from settings)
  SCRIPT_PARENT = "bin"

  # Used to register errors when pulling and pushing the conf file
  class GitHostingException < StandardError
  end

  @@logger = nil
  def self.logger
    @@logger ||= MyLogger.new
  end

  @@web_user = nil
  def self.web_user
    if @@web_user.nil?
      @@web_user = (%x[whoami]).chomp.strip
    end
    return @@web_user
  end

  def self.web_user=(setuser)
    @@web_user = setuser
  end

  def self.git_user
    Setting.plugin_openproject_revisions_git['gitolite_user']
  end

  @@mirror_pubkey = nil
  def self.mirror_push_public_key
    if @@mirror_pubkey.nil?

      %x[cat '#{Setting.plugin_openproject_revisions_git['gitolite_ssh_private_key']}' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/gitolite_admin_id_rsa ' ]
      %x[cat '#{Setting.plugin_openproject_revisions_git['gitolite_ssh_public_key']}' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/gitolite_admin_id_rsa.pub ' ]
      %x[ #{GitHosting.git_user_runner} 'chmod 600 ~/.ssh/gitolite_admin_id_rsa' ]
      %x[ #{GitHosting.git_user_runner} 'chmod 644 ~/.ssh/gitolite_admin_id_rsa.pub' ]

      pubk =    ( %x[cat '#{Setting.plugin_openproject_revisions_git['gitolite_ssh_public_key']}' ]  ).chomp.strip
      git_user_dir = ( %x[ #{GitHosting.git_user_runner} "cd ~ ; pwd" ] ).chomp.strip
      %x[ #{GitHosting.git_user_runner} 'echo "#{pubk}"  > ~/.ssh/gitolite_admin_id_rsa.pub ' ]
      %x[ echo '#!/bin/sh' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/run_gitolite_admin_ssh']
      %x[ echo 'exec ssh -o BatchMode=yes -o StrictHostKeyChecking=no -i #{git_user_dir}/.ssh/gitolite_admin_id_rsa        "$@"' | #{GitHosting.git_user_runner} "cat >> ~/.ssh/run_gitolite_admin_ssh"  ]
      %x[ #{GitHosting.git_user_runner} 'chmod 644 ~/.ssh/gitolite_admin_id_rsa.pub' ]
      %x[ #{GitHosting.git_user_runner} 'chmod 600 ~/.ssh/gitolite_admin_id_rsa']
      %x[ #{GitHosting.git_user_runner} 'chmod 700 ~/.ssh/run_gitolite_admin_ssh']

      @@mirror_pubkey = pubk.split(/[\t ]+/)[0].to_s + " " + pubk.split(/[\t ]+/)[1].to_s

      #settings = Setting["plugin_openproject_revisions_git"]
      #settings["gitMirrorPushPublicKey"] = publicKey
      #Setting["plugin_openproject_revisions_git"] = settings
    end
    @@mirror_pubkey
  end

  @@git_hosting_tmp_dir = nil
  @@previous_git_tmp_dir = nil
  def self.get_tmp_dir
    tmp_dir = (Setting.plugin_openproject_revisions_git['gitTempDataDir'] || TEMP_DATA_DIR)
    if (@@previous_git_tmp_dir != tmp_dir)
      @@previous_git_tmp_dir = tmp_dir
      @@git_hosting_tmp_dir = File.join(tmp_dir,git_user) + "/"
    end
    if !File.directory?(@@git_hosting_tmp_dir)
      %x[mkdir -p "#{@@git_hosting_tmp_dir}"]
      %x[chmod 700 "#{@@git_hosting_tmp_dir}"]
      %x[chown #{web_user} "#{@@git_hosting_tmp_dir}"]
    end
    return @@git_hosting_tmp_dir
  end

  @@git_hosting_bin_dir = nil
  @@previous_git_script_dir = nil
  def self.get_bin_dir
    script_dir = Setting.plugin_openproject_revisions_git['gitScriptDir'] || SCRIPT_DIR
    if @@previous_git_script_dir != script_dir
      @@previous_git_script_dir = script_dir
      @@git_bin_dir_writeable = nil

      # Directory for binaries includes 'SCRIPT_PARENT' at the end.
      # Further, absolute path adds additional 'git_user' component for multi-gitolite installations.
      if script_dir[0,1] == "/"
        @@git_hosting_bin_dir = File.join(script_dir,git_user,SCRIPT_PARENT) + "/"
      else
        @@git_hosting_bin_dir = Rails.root.join("vendor/plugins/redmine_git_hosting",script_dir,SCRIPT_PARENT).to_s+"/"
      end
    end
    if !File.directory?(@@git_hosting_bin_dir)
      logger.info "Creating bin directory: #{@@git_hosting_bin_dir}, Owner #{web_user}"
      %x[mkdir -p "#{@@git_hosting_bin_dir}"]
      %x[chmod 750 "#{@@git_hosting_bin_dir}"]
      %x[chown #{web_user} "#{@@git_hosting_bin_dir}"]

      if !File.directory?(@@git_hosting_bin_dir)
        logger.error "Cannot create bin directory: #{@@git_hosting_bin_dir}"
      end
    end
    return @@git_hosting_bin_dir
  end

  @@git_bin_dir_writeable = nil
  def self.bin_dir_writeable?(*option)
    @@git_bin_dir_writeable = nil if option.length > 0 && option[0] == :reset
    if @@git_bin_dir_writeable == nil
      mybindir = get_bin_dir
      mytestfile = "#{mybindir}/writecheck"
      if (!File.directory?(mybindir))
      @@git_bin_dir_writeable = false
      else
        %x[touch "#{mytestfile}"]<commit_msg>Setting correct installation path for scripts<commit_after>require 'net/ssh'
require 'tempfile'
require 'tmpdir'
require 'stringio'

module GitHosting
  TEMP_DATA_DIR = "/tmp/redmine_git_hosting" # In case settings not migrated (normally from settings)
  SCRIPT_DIR = ""                            # In case settings not migrated (normally from settings)
  SCRIPT_PARENT = "bin"

  # Used to register errors when pulling and pushing the conf file
  class GitHostingException < StandardError
  end

  @@logger = nil
  def self.logger
    @@logger ||= MyLogger.new
  end

  @@web_user = nil
  def self.web_user
    if @@web_user.nil?
      @@web_user = (%x[whoami]).chomp.strip
    end
    return @@web_user
  end

  def self.web_user=(setuser)
    @@web_user = setuser
  end

  def self.git_user
    Setting.plugin_openproject_revisions_git['gitolite_user']
  end

  @@mirror_pubkey = nil
  def self.mirror_push_public_key
    if @@mirror_pubkey.nil?

      %x[cat '#{Setting.plugin_openproject_revisions_git['gitolite_ssh_private_key']}' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/gitolite_admin_id_rsa ' ]
      %x[cat '#{Setting.plugin_openproject_revisions_git['gitolite_ssh_public_key']}' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/gitolite_admin_id_rsa.pub ' ]
      %x[ #{GitHosting.git_user_runner} 'chmod 600 ~/.ssh/gitolite_admin_id_rsa' ]
      %x[ #{GitHosting.git_user_runner} 'chmod 644 ~/.ssh/gitolite_admin_id_rsa.pub' ]

      pubk =    ( %x[cat '#{Setting.plugin_openproject_revisions_git['gitolite_ssh_public_key']}' ]  ).chomp.strip
      git_user_dir = ( %x[ #{GitHosting.git_user_runner} "cd ~ ; pwd" ] ).chomp.strip
      %x[ #{GitHosting.git_user_runner} 'echo "#{pubk}"  > ~/.ssh/gitolite_admin_id_rsa.pub ' ]
      %x[ echo '#!/bin/sh' | #{GitHosting.git_user_runner} 'cat > ~/.ssh/run_gitolite_admin_ssh']
      %x[ echo 'exec ssh -o BatchMode=yes -o StrictHostKeyChecking=no -i #{git_user_dir}/.ssh/gitolite_admin_id_rsa        "$@"' | #{GitHosting.git_user_runner} "cat >> ~/.ssh/run_gitolite_admin_ssh"  ]
      %x[ #{GitHosting.git_user_runner} 'chmod 644 ~/.ssh/gitolite_admin_id_rsa.pub' ]
      %x[ #{GitHosting.git_user_runner} 'chmod 600 ~/.ssh/gitolite_admin_id_rsa']
      %x[ #{GitHosting.git_user_runner} 'chmod 700 ~/.ssh/run_gitolite_admin_ssh']

      @@mirror_pubkey = pubk.split(/[\t ]+/)[0].to_s + " " + pubk.split(/[\t ]+/)[1].to_s

      #settings = Setting["plugin_openproject_revisions_git"]
      #settings["gitMirrorPushPublicKey"] = publicKey
      #Setting["plugin_openproject_revisions_git"] = settings
    end
    @@mirror_pubkey
  end

  @@git_hosting_tmp_dir = nil
  @@previous_git_tmp_dir = nil
  def self.get_tmp_dir
    tmp_dir = (Setting.plugin_openproject_revisions_git['gitTempDataDir'] || TEMP_DATA_DIR)
    if (@@previous_git_tmp_dir != tmp_dir)
      @@previous_git_tmp_dir = tmp_dir
      @@git_hosting_tmp_dir = File.join(tmp_dir,git_user) + "/"
    end
    if !File.directory?(@@git_hosting_tmp_dir)
      %x[mkdir -p "#{@@git_hosting_tmp_dir}"]
      %x[chmod 700 "#{@@git_hosting_tmp_dir}"]
      %x[chown #{web_user} "#{@@git_hosting_tmp_dir}"]
    end
    return @@git_hosting_tmp_dir
  end

  @@git_hosting_bin_dir = nil
  @@previous_git_script_dir = nil
  def self.get_bin_dir
    script_dir = Setting.plugin_openproject_revisions_git['gitScriptDir'] || SCRIPT_DIR
    if @@previous_git_script_dir != script_dir
      @@previous_git_script_dir = script_dir
      @@git_bin_dir_writeable = nil

      # Directory for binaries includes 'SCRIPT_PARENT' at the end.
      # Further, absolute path adds additional 'git_user' component for multi-gitolite installations.
      if script_dir[0,1] == "/"
        @@git_hosting_bin_dir = File.join(script_dir, git_user, SCRIPT_PARENT) + "/"
      else
        @@git_hosting_bin_dir = File.join(Gem.loaded_specs['openproject-revisions_git'].full_gem_path.to_s, script_dir, SCRIPT_PARENT).to_s+"/"
      end
    end
    if !File.directory?(@@git_hosting_bin_dir)
      logger.info "Creating bin directory: #{@@git_hosting_bin_dir}, Owner #{web_user}"
      %x[mkdir -p "#{@@git_hosting_bin_dir}"]
      %x[chmod 750 "#{@@git_hosting_bin_dir}"]
      %x[chown #{web_user} "#{@@git_hosting_bin_dir}"]

      if !File.directory?(@@git_hosting_bin_dir)
        logger.error "Cannot create bin directory: #{@@git_hosting_bin_dir}"
      end
    end
    return @@git_hosting_bin_dir
  end

  @@git_bin_dir_writeable = nil
  def self.bin_dir_writeable?(*option)
    @@git_bin_dir_writeable = nil if option.length > 0 && option[0] == :reset
    if @@git_bin_dir_writeable == nil
      mybindir = get_bin_dir
      mytestfile = "#{mybindir}/writecheck"
      if (!File.directory?(mybindir))
      @@git_bin_dir_writeable = false
      else
        %x[touch "#{mytestfile}"]
{'fraction_non_alphanumeric': 0.13367690366007573, 'fraction_numerical': 0.005679427850231384, 'mean_word_length': 3.8889460154241644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1731887', 'n_tokens_mistral': 3778, 'n_tokens_neox': 3493, 'n_words': 598}