Alien-SVN

 view release on metacpan or  search on metacpan

src/subversion/subversion/bindings/swig/ruby/svn/commit-mailer.rb  view on Meta::CPAN


    def multi_project?
      @multi_project
    end

    def show_path?
      @show_path
    end

    private
    def extract_email_address(address)
      if /<(.+?)>/ =~ address
        $1
      else
        address
      end
    end

    def send_mail(mail)
      _from = extract_email_address(from)
      to = @to.collect {|address| extract_email_address(address)}
      Net::SMTP.start(@server || "localhost", @port) do |smtp|
        smtp.open_message_stream(_from, to) do |f|
          f.print(mail)
        end
      end
    end

    def output_rss
      return unless rss_output_available?
      prev_rss = nil
      begin
        if File.exist?(@rss_path)
          File.open(@rss_path) do |f|
            prev_rss = RSS::Parser.parse(f)
          end
        end
      rescue RSS::Error
      end

      rss = make_rss(prev_rss).to_s
      File.open(@rss_path, "w") do |f|
        f.print(rss)
      end
    end

    def rss_output_available?
      if @repository_uri and @rss_path and @rss_uri
        begin
          require 'rss'
          true
        rescue LoadError
          false
        end
      else
        false
      end
    end

    def make_mail
      utf8_body = make_body
      utf7_body = nil
      utf7_body = utf8_to_utf7(utf8_body) if use_utf7?
      if utf7_body
        body = utf7_body
        encoding = "utf-7"
        bit = "7bit"
      else
        body = utf8_body
        encoding = "utf-8"
        bit = "8bit"
      end

      unless @max_size.nil?
        body = truncate_body(body, !utf7_body.nil?)
      end

      make_header(encoding, bit) + "\n" + body
    end

    def make_body
      body = ""
      body << "#{@info.author}\t#{format_time(@info.date)}\n"
      body << "\n"
      body << "  New Revision: #{@info.revision}\n"
      body << "\n"
      body << "  Log:\n"
      stripped_log = @info.log.rstrip
      stripped_log << "\n" unless stripped_log.empty?
      stripped_log.each_line do |line|
        body << "    #{line}"
      end
      body << "\n"
      body << added_dirs
      body << added_files
      body << copied_dirs
      body << copied_files
      body << deleted_dirs
      body << deleted_files
      body << modified_dirs
      body << modified_files
      body << "\n"
      body << change_info
      body
    end

    def format_time(time)
      time.strftime('%Y-%m-%d %X %z (%a, %d %b %Y)')
    end

    def changed_items(title, type, items)
      rv = ""
      unless items.empty?
        rv << "  #{title} #{type}:\n"
        if block_given?
          yield(rv, items)
        else
          rv << items.collect {|item| "    #{item}\n"}.join('')
        end
      end
      rv
    end

    def changed_files(title, files, &block)
      changed_items(title, "files", files, &block)
    end

    def added_files
      changed_files("Added", @info.added_files)

src/subversion/subversion/bindings/swig/ruby/svn/commit-mailer.rb  view on Meta::CPAN

    end

    def affected_paths(project)
      paths = []
      [nil, :branches_path, :tags_path].each do |target|
        prefix = [project]
        prefix << send(target) if target
        prefix = prefix.compact.join("/")
        sub_paths = @info.sub_paths(prefix)
        if target.nil?
          sub_paths = sub_paths.find_all do |sub_path|
            sub_path == trunk_path
          end
        end
        paths.concat(sub_paths)
      end
      paths.uniq
    end

    def make_subject
      subject = ""
      project = detect_project
      subject << "#{@name} " if @name
      revision_info = "r#{@info.revision}"
      if show_path?
        _affected_paths = affected_paths(project)
        unless _affected_paths.empty?
          revision_info = "(#{_affected_paths.join(',')}) #{revision_info}"
        end
      end
      if project
        subject << "[#{project} #{revision_info}] "
      else
        subject << "#{revision_info}: "
      end
      subject << @info.log.lstrip.to_a.first.to_s.chomp
      NKF.nkf("-WM", subject)
    end

    def x_author
      "X-SVN-Author: #{@info.author}"
    end

    def x_revision
      "X-SVN-Revision: #{@info.revision}"
    end

    def x_repository
      if @repository_uri
        repository = "#{@repository_uri}/"
      else
        repository = "XXX"
      end
      "X-SVN-Repository: #{repository}"
    end

    def x_id
      "X-SVN-Commit-Id: #{@info.entire_sha256}"
    end

    def utf8_to_utf7(utf8)
      require 'iconv'
      Iconv.conv("UTF-7", "UTF-8", utf8)
    rescue InvalidEncoding
      begin
        Iconv.conv("UTF7", "UTF8", utf8)
      rescue Exception
        nil
      end
    rescue Exception
      nil
    end

    def truncate_body(body, use_utf7)
      return body if body.size < @max_size

      truncated_body = body[0, @max_size]
      formatted_size = self.class.format_size(@max_size)
      truncated_message = "... truncated to #{formatted_size}\n"
      truncated_message = utf8_to_utf7(truncated_message) if use_utf7
      truncated_message_size = truncated_message.size

      lf_index = truncated_body.rindex(/(?:\r|\r\n|\n)/)
      while lf_index
        if lf_index + truncated_message_size < @max_size
          truncated_body[lf_index, @max_size] = "\n#{truncated_message}"
          break
        else
          lf_index = truncated_body.rindex(/(?:\r|\r\n|\n)/, lf_index - 1)
        end
      end

      truncated_body
    end

    def make_rss(base_rss)
      RSS::Maker.make("1.0") do |maker|
        maker.encoding = "UTF-8"

        maker.channel.about = @rss_uri
        maker.channel.title = rss_title
        maker.channel.link = @repository_uri
        maker.channel.description = rss_description
        maker.channel.dc_date = @info.date

        if base_rss
          base_rss.items.each do |item|
            item.setup_maker(maker)
          end
        end

        diff_info.each do |name, infos|
          infos.each do |desc, link|
            item = maker.items.new_item
            item.title = name
            item.description = @info.log
            item.content_encoded = "<pre>#{RSS::Utils.html_escape(desc)}</pre>"
            item.link = link
            item.dc_date = @info.date
            item.dc_creator = @info.author
          end
        end

        maker.items.do_sort = true
        maker.items.max_size = 15
      end
    end

    def rss_title
      @rss_title || @name || @repository_uri
    end

    def rss_description
      @rss_description || "Repository of #{@name || @repository_uri}"
    end
  end
end



( run in 3.288 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )