repositories_helper.rb 11.3 KB
Newer Older
1 2
# encoding: utf-8
#
3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
jplang's avatar
jplang committed
5 6 7 8 9
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
10
#
jplang's avatar
jplang committed
11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
jplang's avatar
jplang committed
16 17 18 19 20
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

module RepositoriesHelper
21 22 23 24 25 26
  def format_revision(revision)
    if revision.respond_to? :format_identifier
      revision.format_identifier
    else
      revision.to_s
    end
27
  end
28

29 30 31 32 33
  def truncate_at_line_break(text, length = 255)
    if text
      text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
    end
  end
34

35 36 37 38 39 40 41 42 43
  def render_pagination
    pagination_links_each @paginator do |text, parameters, options|
      if entry = @entries[parameters[:page] - 1]
        ent_path = Redmine::CodesetUtil.replace_invalid_utf8(entry.path)
        link_to text, {action: 'entry', id: @project, repository_id: @repository.identifier_param, path: to_path_param(ent_path), rev: @rev}
      end
    end if @paginator
  end

44 45 46 47
  def render_properties(properties)
    unless properties.nil? || properties.empty?
      content = ''
      properties.keys.sort.each do |property|
48
        content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>".html_safe)
49
      end
50
      content_tag('ul', content.html_safe, :class => 'properties')
51 52
    end
  end
53

54
  def render_changeset_changes
55
    changes = @changeset.filechanges.limit(1000).reorder('path').collect do |change|
56 57 58 59
      case change.action
      when 'A'
        # Detects moved/copied files
        if !change.from_path.blank?
60
          change.action =
61
             @changeset.filechanges.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
62 63 64
        end
        change
      when 'D'
65
        @changeset.filechanges.detect {|c| c.from_path == change.path} ? nil : change
66 67 68
      else
        change
      end
69
    end.compact
70

71 72 73 74
    tree = { }
    changes.each do |change|
      p = tree
      dirs = change.path.to_s.split('/').select {|d| !d.blank?}
75
      path = ''
76
      dirs.each do |dir|
77
        path += '/' + dir
78 79
        p[:s] ||= {}
        p = p[:s]
80 81
        p[path] ||= {}
        p = p[path]
82 83 84 85 86
      end
      p[:c] = change
    end
    render_changes_tree(tree[:s])
  end
87

88 89 90 91 92 93
  def render_changes_tree(tree)
    return '' if tree.nil?
    output = ''
    output << '<ul>'
    tree.keys.sort.each do |file|
      style = 'change'
94 95 96 97
      text = File.basename(h(file))
      if s = tree[file][:s]
        style << ' folder'
        path_param = to_path_param(@repository.relative_path(file))
98
        text = link_to(h(text), :controller => 'repositories',
99 100
                             :action => 'show',
                             :id => @project,
101
                             :repository_id => @repository.identifier_param,
102
                             :path => path_param,
103
                             :rev => @changeset.identifier)
jplang's avatar
jplang committed
104
        output << "<li class='#{style}'>#{text}"
105
        output << render_changes_tree(s)
jplang's avatar
jplang committed
106
        output << "</li>"
107 108
      elsif c = tree[file][:c]
        style << " change-#{c.action}"
109
        path_param = to_path_param(@repository.relative_path(c.path))
110
        text = link_to(h(text), :controller => 'repositories',
111 112
                             :action => 'entry',
                             :id => @project,
113
                             :repository_id => @repository.identifier_param,
114
                             :path => path_param,
115
                             :rev => @changeset.identifier) unless c.action == 'D'
116
        text << " - #{h(c.revision)}" unless c.revision.blank?
117
        text << ' ('.html_safe + link_to(l(:label_diff), :controller => 'repositories',
118 119
                                       :action => 'diff',
                                       :id => @project,
120
                                       :repository_id => @repository.identifier_param,
121
                                       :path => path_param,
122 123
                                       :rev => @changeset.identifier) + ') '.html_safe if c.action == 'M'
        text << ' '.html_safe + content_tag('span', h(c.from_path), :class => 'copied-from') unless c.from_path.blank?
124
        output << "<li class='#{style}'>#{text}</li>"
125 126 127
      end
    end
    output << '</ul>'
128
    output.html_safe
129
  end
130

131
  def repository_field_tags(form, repository)
132
    method = repository.class.name.demodulize.underscore + "_field_tags"
133 134 135 136
    if repository.is_a?(Repository) &&
        respond_to?(method) && method != 'repository_field_tags'
      send(method, form, repository)
    end
137
  end
138

jplang's avatar
jplang committed
139
  def scm_select_tag(repository)
140
    scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
141
    Redmine::Scm::Base.all.each do |scm|
142 143 144 145
    if Setting.enabled_scm.include?(scm) ||
          (repository && repository.class.name.demodulize == scm)
        scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
      end
146
    end
147
    select_tag('repository_scm',
148
               options_for_select(scm_options, repository.class.name.demodulize),
jplang's avatar
jplang committed
149
               :disabled => (repository && !repository.new_record?),
150
               :data => {:remote => true, :method => 'get', :url => new_project_repository_path(repository.project)})
151
  end
152

153
  def with_leading_slash(path)
154 155
    path.to_s.starts_with?('/') ? path : "/#{path}"
  end
156

157
  def subversion_field_tags(form, repository)
158
      content_tag('p', form.text_field(:url, :size => 60, :required => true,
159
                       :disabled => !repository.safe_attribute?('url')) +
160
                       scm_path_info_tag(repository)) +
161
      content_tag('p', form.text_field(:login, :size => 30)) +
162 163 164 165 166
      content_tag('p', form.password_field(
                            :password, :size => 30, :name => 'ignore',
                            :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
                            :onfocus => "this.value=''; this.name='repository[password]';",
                            :onchange => "this.name='repository[password]';"))
167 168 169
  end

  def mercurial_field_tags(form, repository)
170
    content_tag('p', form.text_field(
171
                       :url, :label => l(:field_path_to_repository),
172
                       :size => 60, :required => true,
173
                       :disabled => !repository.safe_attribute?('url')
174
                         ) +
175
                     scm_path_info_tag(repository)) +
jplang's avatar
jplang committed
176
    scm_path_encoding_tag(form, repository)
177 178
  end

179
  def git_field_tags(form, repository)
180
    content_tag('p', form.text_field(
181
                       :url, :label => l(:field_path_to_repository),
182
                       :size => 60, :required => true,
183
                       :disabled => !repository.safe_attribute?('url')
184
                         ) +
185
                      scm_path_info_tag(repository)) +
jplang's avatar
jplang committed
186
    scm_path_encoding_tag(form, repository) +
187
    content_tag('p', form.check_box(
188
                        :report_last_commit,
189
                        :label => l(:label_git_report_last_commit)
190
                         ))
191 192
  end

193
  def cvs_field_tags(form, repository)
194 195
    content_tag('p', form.text_field(
                     :root_url,
196
                     :label => l(:field_cvsroot),
197
                     :size => 60, :required => true,
198 199
                     :disabled => !repository.safe_attribute?('root_url')) +
                     scm_path_info_tag(repository)) +
200 201
    content_tag('p', form.text_field(
                     :url,
202
                     :label => l(:field_cvs_module),
203
                     :size => 30, :required => true,
204
                     :disabled => !repository.safe_attribute?('url'))) +
jplang's avatar
jplang committed
205 206
    scm_log_encoding_tag(form, repository) +
    scm_path_encoding_tag(form, repository)
207
  end
jplang's avatar
jplang committed
208 209

  def bazaar_field_tags(form, repository)
210
    content_tag('p', form.text_field(
211
                     :url, :label => l(:field_path_to_repository),
212
                     :size => 60, :required => true,
213 214
                     :disabled => !repository.safe_attribute?('url')) +
                     scm_path_info_tag(repository)) +
jplang's avatar
jplang committed
215
    scm_log_encoding_tag(form, repository)
jplang's avatar
jplang committed
216
  end
217

218
  def filesystem_field_tags(form, repository)
219
    content_tag('p', form.text_field(
220
                     :url, :label => l(:field_root_directory),
221
                     :size => 60, :required => true,
222 223
                     :disabled => !repository.safe_attribute?('url')) +
                     scm_path_info_tag(repository)) +
jplang's avatar
jplang committed
224 225 226
    scm_path_encoding_tag(form, repository)
  end

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  def scm_path_info_tag(repository)
    text = scm_path_info(repository)
    if text.present?
      content_tag('em', text, :class => 'info')
    else
      ''
    end
  end

  def scm_path_info(repository)
    scm_name = repository.scm_name.to_s.downcase

    info_from_config = Redmine::Configuration["scm_#{scm_name}_path_info"].presence
    return info_from_config.html_safe if info_from_config

    l("text_#{scm_name}_repository_note", :default => '')
  end

jplang's avatar
jplang committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  def scm_log_encoding_tag(form, repository)
    select = form.select(
      :log_encoding,
      [nil] + Setting::ENCODINGS,
      :label => l(:field_commit_logs_encoding),
      :required => true
    )
    content_tag('p', select)
  end

  def scm_path_encoding_tag(form, repository)
    select = form.select(
      :path_encoding,
      [nil] + Setting::ENCODINGS,
      :label => l(:field_scm_path_encoding)
    )
    content_tag('p', select + content_tag('em', l(:text_scm_path_encoding_note), :class => 'info'))
262
  end
263

emassip's avatar
emassip committed
264
  def index_commits(commits, heads)
265 266
    return nil if commits.nil? or commits.first.parents.nil?
    refs_map = {}
emassip's avatar
emassip committed
267 268 269
    heads.each do |head|
      refs_map[head.scmid] ||= []
      refs_map[head.scmid] << head
270
    end
emassip's avatar
emassip committed
271 272 273 274 275 276 277 278 279
    commits_by_scmid = {}
    commits.reverse.each_with_index do |commit, commit_index|
      commits_by_scmid[commit.scmid] = {
        :parent_scmids => commit.parents.collect { |parent| parent.scmid },
        :rdmid => commit_index,
        :refs  => refs_map.include?(commit.scmid) ? refs_map[commit.scmid].join(" ") : nil,
        :scmid => commit.scmid,
        :href  => block_given? ? yield(commit.scmid) : commit.scmid
      }
280
    end
281
    heads.sort_by!(&:to_s)
jplang's avatar
jplang committed
282
    space = nil
emassip's avatar
emassip committed
283 284
    heads.each do |head|
      if commits_by_scmid.include? head.scmid
285
        space = index_head((space || -1) + 1, head, commits_by_scmid)
286 287 288
      end
    end
    # when no head matched anything use first commit
289 290
    space ||= index_head(0, commits.first, commits_by_scmid)
    return commits_by_scmid, space
291 292
  end

293 294 295
  def index_head(space, commit, commits_by_scmid)
    stack = [[space, commits_by_scmid[commit.scmid]]]
    max_space = space
296
    until stack.empty?
297 298 299
      space, commit = stack.pop
      commit[:space] = space if commit[:space].nil?
      space -= 1
emassip's avatar
emassip committed
300 301
      commit[:parent_scmids].each_with_index do |parent_scmid, parent_index|
        parent_commit = commits_by_scmid[parent_scmid]
302 303
        if parent_commit and parent_commit[:space].nil?
          stack.unshift [space += 1, parent_commit]
304 305
        end
      end
306
      max_space = space if max_space < space
307
    end
308
    max_space
309
  end
jplang's avatar
jplang committed
310
end