repositories_helper.rb 10.9 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
  def render_properties(properties)
    unless properties.nil? || properties.empty?
      content = ''
      properties.keys.sort.each do |property|
39
        content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>".html_safe)
40
      end
41
      content_tag('ul', content.html_safe, :class => 'properties')
42 43
    end
  end
44

45
  def render_changeset_changes
46
    changes = @changeset.filechanges.limit(1000).reorder('path').collect do |change|
47 48 49 50
      case change.action
      when 'A'
        # Detects moved/copied files
        if !change.from_path.blank?
51
          change.action =
52
             @changeset.filechanges.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
53 54 55
        end
        change
      when 'D'
56
        @changeset.filechanges.detect {|c| c.from_path == change.path} ? nil : change
57 58 59
      else
        change
      end
60
    end.compact
61

62 63 64 65
    tree = { }
    changes.each do |change|
      p = tree
      dirs = change.path.to_s.split('/').select {|d| !d.blank?}
66
      path = ''
67
      dirs.each do |dir|
68
        path += '/' + dir
69 70
        p[:s] ||= {}
        p = p[:s]
71 72
        p[path] ||= {}
        p = p[path]
73 74 75 76 77
      end
      p[:c] = change
    end
    render_changes_tree(tree[:s])
  end
78

79 80 81 82 83 84
  def render_changes_tree(tree)
    return '' if tree.nil?
    output = ''
    output << '<ul>'
    tree.keys.sort.each do |file|
      style = 'change'
85 86 87 88
      text = File.basename(h(file))
      if s = tree[file][:s]
        style << ' folder'
        path_param = to_path_param(@repository.relative_path(file))
89
        text = link_to(h(text), :controller => 'repositories',
90 91
                             :action => 'show',
                             :id => @project,
92
                             :repository_id => @repository.identifier_param,
93
                             :path => path_param,
94
                             :rev => @changeset.identifier)
jplang's avatar
jplang committed
95
        output << "<li class='#{style}'>#{text}"
96
        output << render_changes_tree(s)
jplang's avatar
jplang committed
97
        output << "</li>"
98 99
      elsif c = tree[file][:c]
        style << " change-#{c.action}"
100
        path_param = to_path_param(@repository.relative_path(c.path))
101
        text = link_to(h(text), :controller => 'repositories',
102 103
                             :action => 'entry',
                             :id => @project,
104
                             :repository_id => @repository.identifier_param,
105
                             :path => path_param,
106
                             :rev => @changeset.identifier) unless c.action == 'D'
107
        text << " - #{h(c.revision)}" unless c.revision.blank?
108
        text << ' ('.html_safe + link_to(l(:label_diff), :controller => 'repositories',
109 110
                                       :action => 'diff',
                                       :id => @project,
111
                                       :repository_id => @repository.identifier_param,
112
                                       :path => path_param,
113 114
                                       :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?
115
        output << "<li class='#{style}'>#{text}</li>"
116 117 118
      end
    end
    output << '</ul>'
119
    output.html_safe
120
  end
121

122
  def repository_field_tags(form, repository)
123
    method = repository.class.name.demodulize.underscore + "_field_tags"
124 125 126 127
    if repository.is_a?(Repository) &&
        respond_to?(method) && method != 'repository_field_tags'
      send(method, form, repository)
    end
128
  end
129

jplang's avatar
jplang committed
130
  def scm_select_tag(repository)
131
    scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
132
    Redmine::Scm::Base.all.each do |scm|
133 134 135 136
    if Setting.enabled_scm.include?(scm) ||
          (repository && repository.class.name.demodulize == scm)
        scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
      end
137
    end
138
    select_tag('repository_scm',
139
               options_for_select(scm_options, repository.class.name.demodulize),
jplang's avatar
jplang committed
140
               :disabled => (repository && !repository.new_record?),
141
               :data => {:remote => true, :method => 'get', :url => new_project_repository_path(repository.project)})
142
  end
143

144
  def with_leading_slash(path)
145 146
    path.to_s.starts_with?('/') ? path : "/#{path}"
  end
147

148
  def subversion_field_tags(form, repository)
149
      content_tag('p', form.text_field(:url, :size => 60, :required => true,
150
                       :disabled => !repository.safe_attribute?('url')) +
151
                       scm_path_info_tag(repository)) +
152
      content_tag('p', form.text_field(:login, :size => 30)) +
153 154 155 156 157
      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]';"))
158 159 160
  end

  def mercurial_field_tags(form, repository)
161
    content_tag('p', form.text_field(
162
                       :url, :label => l(:field_path_to_repository),
163
                       :size => 60, :required => true,
164
                       :disabled => !repository.safe_attribute?('url')
165
                         ) +
166
                     scm_path_info_tag(repository)) +
jplang's avatar
jplang committed
167
    scm_path_encoding_tag(form, repository)
168 169
  end

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

184
  def cvs_field_tags(form, repository)
185 186
    content_tag('p', form.text_field(
                     :root_url,
187
                     :label => l(:field_cvsroot),
188
                     :size => 60, :required => true,
189 190
                     :disabled => !repository.safe_attribute?('root_url')) +
                     scm_path_info_tag(repository)) +
191 192
    content_tag('p', form.text_field(
                     :url,
193
                     :label => l(:field_cvs_module),
194
                     :size => 30, :required => true,
195
                     :disabled => !repository.safe_attribute?('url'))) +
jplang's avatar
jplang committed
196 197
    scm_log_encoding_tag(form, repository) +
    scm_path_encoding_tag(form, repository)
198
  end
jplang's avatar
jplang committed
199 200

  def bazaar_field_tags(form, repository)
201
    content_tag('p', form.text_field(
202
                     :url, :label => l(:field_path_to_repository),
203
                     :size => 60, :required => true,
204 205
                     :disabled => !repository.safe_attribute?('url')) +
                     scm_path_info_tag(repository)) +
jplang's avatar
jplang committed
206
    scm_log_encoding_tag(form, repository)
jplang's avatar
jplang committed
207
  end
208

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

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  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
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  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'))
253
  end
254

emassip's avatar
emassip committed
255
  def index_commits(commits, heads)
256 257
    return nil if commits.nil? or commits.first.parents.nil?
    refs_map = {}
emassip's avatar
emassip committed
258 259 260
    heads.each do |head|
      refs_map[head.scmid] ||= []
      refs_map[head.scmid] << head
261
    end
emassip's avatar
emassip committed
262 263 264 265 266 267 268 269 270
    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
      }
271
    end
272
    heads.sort_by!(&:to_s)
jplang's avatar
jplang committed
273
    space = nil
emassip's avatar
emassip committed
274 275
    heads.each do |head|
      if commits_by_scmid.include? head.scmid
276
        space = index_head((space || -1) + 1, head, commits_by_scmid)
277 278 279
      end
    end
    # when no head matched anything use first commit
280 281
    space ||= index_head(0, commits.first, commits_by_scmid)
    return commits_by_scmid, space
282 283
  end

284 285 286
  def index_head(space, commit, commits_by_scmid)
    stack = [[space, commits_by_scmid[commit.scmid]]]
    max_space = space
287
    until stack.empty?
288 289 290
      space, commit = stack.pop
      commit[:space] = space if commit[:space].nil?
      space -= 1
emassip's avatar
emassip committed
291 292
      commit[:parent_scmids].each_with_index do |parent_scmid, parent_index|
        parent_commit = commits_by_scmid[parent_scmid]
293 294
        if parent_commit and parent_commit[:space].nil?
          stack.unshift [space += 1, parent_commit]
295 296
        end
      end
297
      max_space = space if max_space < space
298
    end
299
    max_space
300
  end
jplang's avatar
jplang committed
301
end