<commit_before>module RedmineGitolite

  module AdminRepositoriesHelper

    def handle_repository_add(repository, opts = {})
      force = (opts.has_key?(:force) && opts[:force] == true) || false
      old_perms = (opts.has_key?(:old_perms) && opts[:old_perms].is_a?(Hash)) ? opts[:old_perms] : {}

      repo_name = repository.gitolite_repository_name
      repo_path = repository.gitolite_repository_path
      repo_conf = @gitolite_config.repos[repo_name]

      if !repo_conf
        logger.info { "#{@action} : repository '#{repo_name}' does not exist in Gitolite, create it ..." }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        old_permissions = old_perms
      else
        if force
          logger.warn { "#{@action} : repository '#{repo_name}' already exists in Gitolite, force mode !" }
          logger.debug { "#{@action} : repository path '#{repo_path}'" }
          old_permissions = get_old_permissions(repo_conf)
          @gitolite_config.rm_repo(repo_name)
        else
          logger.warn { "#{@action} : repository '#{repo_name}' already exists in Gitolite, exit !" }
          logger.debug { "#{@action} : repository path '#{repo_path}'" }
          return false
        end
      end

      do_update_repository(repository, old_permissions)
    end


    def handle_repository_update(repository)
      repo_name = repository.gitolite_repository_name
      repo_path = repository.gitolite_repository_path
      repo_conf = @gitolite_config.repos[repo_name]

      if repo_conf
        logger.info { "#{@action} : repository '#{repo_name}' exists in Gitolite, update it ..." }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        old_perms = get_old_permissions(repo_conf)
        @gitolite_config.rm_repo(repo_name)
      else
        logger.warn { "#{@action} : repository '#{repo_name}' does not exist in Gitolite, exit !" }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        return false
      end

      do_update_repository(repository, old_perms)
    end


    def handle_repository_delete(repository_data)
      repo_name = repository_data['repo_name']
      repo_path = repository_data['repo_path']
      repo_conf = @gitolite_config.repos[repo_name]

      if !repo_conf
        logger.warn { "#{@action} : repository '#{repo_name}' does not exist in Gitolite, exit !" }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        return false
      else
        logger.info { "#{@action} : repository '#{repo_name}' exists in Gitolite, delete it ..." }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        @gitolite_config.rm_repo(repo_name)
      end
    end


    def handle_repositories_move(project)
      projects = project.self_and_descendants

      # Only take projects that have Git repos.
      git_projects = projects.uniq.select{|p| p.gitolite_repos.any?}
      return if git_projects.empty?

      git_projects.reverse.each do |project|
        repo_list = []

        project.gitolite_repos.reverse.each do |repository|
          repo_list.push(repository.gitolite_repository_name)
          do_move_repositories(repository)
        end

        gitolite_admin_repo_commit("#{@action} : #{project.identifier} | #{repo_list}")
      end
    end


    def do_update_repository(repository, old_permissions)
      repo_name = repository.gitolite_repository_name
      repo_conf = @gitolite_config.repos[repo_name]
      project   = repository.project

      # Create new repo object
      repo_conf = Gitolite::Config::Repo.new(repo_name)

      # Set post-receive hook params
      repo_conf.set_git_config("redminegitolite.projectid", repository.project.identifier.to_s)
      repo_conf.set_git_config("redminegitolite.repositoryid", "#{repository.identifier || ''}")
      repo_conf.set_git_config("redminegitolite.repositorykey", repository.extra.key)

      if project.active?
        if User.anonymous.allowed_to?(:view_changesets, project) || repository.extra.git_http != 0
          repo_conf.set_git_config("http.uploadpack", 'true')
        else
          repo_conf.set_git_config("http.uploadpack", 'false')
        end

        # Set mail-notifications hook params
        mailing_list = repository.mailing_list_params[:mailing_list]

        if repository.extra.git_notify == 1 && !mailing_list.empty?
          email_prefix   = repository.mailing_list_params[:email_prefix]
          sender_address = repository.mailing_list_params[:sender_address]

          repo_conf.set_git_config("multimailhook.enabled", 'true')
          repo_conf.set_git_config("multimailhook.environment", "gitolite")
          repo_conf.set_git_config("multimailhook.mailinglist", mailing_list.keys.join(", "))
          repo_conf.set_git_config("multimailhook.from", email_prefix)
          repo_conf.set_git_config("multimailhook.emailPrefix", sender_address)

          # Set SMTP server for mail-notifications hook
          #~ repo_conf.set_git_config("multimailhook.smtpServer", ActionMailer::Base.smtp_settings[:address])
        else
          repo_conf.set_git_config("multimailhook.enabled", 'false')
        end

        # Set Git config keys
        if repository.repository_git_config_keys.any?
          repository.repository_git_config_keys.each do |git_config_key|
            repo_conf.set_git_config(git_config_key.key, git_config_key.value)
          end
        end
      else
        repo_conf.set_git_config("http.uploadpack", 'false')
        repo_conf.set_git_config("multimailhook.enabled", 'false')
      end

      @gitolite_config.add_repo(repo_conf)

      current_permissions = build_permissions(repository)
      current_permissions = merge_permissions(current_permissions, old_permissions)

      repo_conf.permissions = [current_permissions]
    end


    def do_move_repositories(repository)
      repo_id   = repository.redmine_name
      repo_name = repository.old_repository_name
<commit_msg>multimailhook swap sender_address and email_prefix<commit_after>module RedmineGitolite

  module AdminRepositoriesHelper

    def handle_repository_add(repository, opts = {})
      force = (opts.has_key?(:force) && opts[:force] == true) || false
      old_perms = (opts.has_key?(:old_perms) && opts[:old_perms].is_a?(Hash)) ? opts[:old_perms] : {}

      repo_name = repository.gitolite_repository_name
      repo_path = repository.gitolite_repository_path
      repo_conf = @gitolite_config.repos[repo_name]

      if !repo_conf
        logger.info { "#{@action} : repository '#{repo_name}' does not exist in Gitolite, create it ..." }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        old_permissions = old_perms
      else
        if force
          logger.warn { "#{@action} : repository '#{repo_name}' already exists in Gitolite, force mode !" }
          logger.debug { "#{@action} : repository path '#{repo_path}'" }
          old_permissions = get_old_permissions(repo_conf)
          @gitolite_config.rm_repo(repo_name)
        else
          logger.warn { "#{@action} : repository '#{repo_name}' already exists in Gitolite, exit !" }
          logger.debug { "#{@action} : repository path '#{repo_path}'" }
          return false
        end
      end

      do_update_repository(repository, old_permissions)
    end


    def handle_repository_update(repository)
      repo_name = repository.gitolite_repository_name
      repo_path = repository.gitolite_repository_path
      repo_conf = @gitolite_config.repos[repo_name]

      if repo_conf
        logger.info { "#{@action} : repository '#{repo_name}' exists in Gitolite, update it ..." }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        old_perms = get_old_permissions(repo_conf)
        @gitolite_config.rm_repo(repo_name)
      else
        logger.warn { "#{@action} : repository '#{repo_name}' does not exist in Gitolite, exit !" }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        return false
      end

      do_update_repository(repository, old_perms)
    end


    def handle_repository_delete(repository_data)
      repo_name = repository_data['repo_name']
      repo_path = repository_data['repo_path']
      repo_conf = @gitolite_config.repos[repo_name]

      if !repo_conf
        logger.warn { "#{@action} : repository '#{repo_name}' does not exist in Gitolite, exit !" }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        return false
      else
        logger.info { "#{@action} : repository '#{repo_name}' exists in Gitolite, delete it ..." }
        logger.debug { "#{@action} : repository path '#{repo_path}'" }
        @gitolite_config.rm_repo(repo_name)
      end
    end


    def handle_repositories_move(project)
      projects = project.self_and_descendants

      # Only take projects that have Git repos.
      git_projects = projects.uniq.select{|p| p.gitolite_repos.any?}
      return if git_projects.empty?

      git_projects.reverse.each do |project|
        repo_list = []

        project.gitolite_repos.reverse.each do |repository|
          repo_list.push(repository.gitolite_repository_name)
          do_move_repositories(repository)
        end

        gitolite_admin_repo_commit("#{@action} : #{project.identifier} | #{repo_list}")
      end
    end


    def do_update_repository(repository, old_permissions)
      repo_name = repository.gitolite_repository_name
      repo_conf = @gitolite_config.repos[repo_name]
      project   = repository.project

      # Create new repo object
      repo_conf = Gitolite::Config::Repo.new(repo_name)

      # Set post-receive hook params
      repo_conf.set_git_config("redminegitolite.projectid", repository.project.identifier.to_s)
      repo_conf.set_git_config("redminegitolite.repositoryid", "#{repository.identifier || ''}")
      repo_conf.set_git_config("redminegitolite.repositorykey", repository.extra.key)

      if project.active?
        if User.anonymous.allowed_to?(:view_changesets, project) || repository.extra.git_http != 0
          repo_conf.set_git_config("http.uploadpack", 'true')
        else
          repo_conf.set_git_config("http.uploadpack", 'false')
        end

        # Set mail-notifications hook params
        mailing_list = repository.mailing_list_params[:mailing_list]

        if repository.extra.git_notify == 1 && !mailing_list.empty?
          email_prefix   = repository.mailing_list_params[:email_prefix]
          sender_address = repository.mailing_list_params[:sender_address]

          repo_conf.set_git_config("multimailhook.enabled", 'true')
          repo_conf.set_git_config("multimailhook.environment", "gitolite")
          repo_conf.set_git_config("multimailhook.mailinglist", mailing_list.keys.join(", "))
          repo_conf.set_git_config("multimailhook.from", sender_address)
          repo_conf.set_git_config("multimailhook.emailPrefix", email_prefix)

          # Set SMTP server for mail-notifications hook
          #~ repo_conf.set_git_config("multimailhook.smtpServer", ActionMailer::Base.smtp_settings[:address])
        else
          repo_conf.set_git_config("multimailhook.enabled", 'false')
        end

        # Set Git config keys
        if repository.repository_git_config_keys.any?
          repository.repository_git_config_keys.each do |git_config_key|
            repo_conf.set_git_config(git_config_key.key, git_config_key.value)
          end
        end
      else
        repo_conf.set_git_config("http.uploadpack", 'false')
        repo_conf.set_git_config("multimailhook.enabled", 'false')
      end

      @gitolite_config.add_repo(repo_conf)

      current_permissions = build_permissions(repository)
      current_permissions = merge_permissions(current_permissions, old_permissions)

      repo_conf.permissions = [current_permissions]
    end


    def do_move_repositories(repository)
      repo_id   = repository.redmine_name
      repo_name = repository.old_repository_name

{'fraction_non_alphanumeric': 0.10642720545439428, 'fraction_numerical': 0.0003325850170449821, 'mean_word_length': 3.1953261248692013, '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': '3011664', 'n_tokens_mistral': 3722, 'n_tokens_neox': 3575, 'n_words': 642}