cross-project_revision_and_commit_links_v5.diff

Patch v5 - New wiki syntax, compatible with r2847 - Babar O'Cap, 2009-08-26 10:27

Download (11.9 KB)

View differences:

app/helpers/application_helper.rb
29 29
  def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
30 30

  
31 31
  # Return true if user is authorized for controller/action, otherwise false
32
  def authorize_for(controller, action)
33
    User.current.allowed_to?({:controller => controller, :action => action}, @project)
32
  def authorize_for(controller, action, project = @project)
33
    User.current.allowed_to?({:controller => controller, :action => action}, project)
34 34
  end
35 35

  
36 36
  # Display a link if user is authorized
......
422 422
    #     #52 -> Link to issue #52
423 423
    #   Changesets:
424 424
    #     r52 -> Link to revision 52
425
    #     project:r52 -> Link to revision 52 of an other project, using project name or identifier
425 426
    #     commit:a85130f -> Link to scmid starting with a85130f
427
    #     project:commit:a85130f -> Link to scmid starting with a85130f of an other project, using project name or identifier
428
    #     If your project name contains spaces, use quotation marks :
429
    #       "My Project":r52
430
    #       "My Project":commit:a85130f
426 431
    #   Documents:
427 432
    #     document#17 -> Link to document with id 17
428 433
    #     document:Greetings -> Link to the document with title "Greetings"
......
441 446
    #     export:some/file -> Force the download of the file
442 447
    #  Forum messages:
443 448
    #     message#1218 -> Link to message with id 1218
444
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
445
      leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
449
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|(\"[^:<>\r\n\b]+\":|[^:<>\s\r\n\b]+:)?(commit)|source|export|message)?((#|(\"[^:<>\r\n\b]+\":|[^:<>\s\r\n\b]+:)?(r))(\d+)|(:)([^"\s<>][^:\s\r\n\b<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
450
      leading, esc, prefix, sep, revproj, oid = $1, $2, $5 || $3, $11 || $9 || $7, $4 || $8, $10 || $12
446 451
      link = nil
447 452
      if esc.nil?
448 453
        if prefix.nil? && sep == 'r'
449
          if project && (changeset = project.changesets.find_by_revision(oid))
450
            link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
451
                                      :class => 'changeset',
452
                                      :title => truncate_single_line(changeset.comments, :length => 100))
454
          if revproj.nil?
455
            if project && (changeset = project.changesets.find_by_revision(oid))
456
              link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
457
                                        :class => 'changeset',
458
                                        :title => truncate_single_line(changeset.comments, :length => 100))
459
            end
460
          else
461
            revproj.gsub!(/[\:"]/,'')
462
            link_project = Project.find_by_name(h(revproj)) || Project.find_by_identifier(h(revproj))
463
            if link_project && (changeset = link_project.changesets.find_by_revision(oid))              
464
              title = truncate_single_line(changeset.comments, :length => 100) if authorize_for('repositories', 'revision', link_project)
465
              link = link_to h("#{revproj}:r#{oid}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :rev => oid},
466
                                                      :class => 'changeset',
467
                                                      :title => title
468
            end            
453 469
          end
454 470
        elsif sep == '#'
455 471
          oid = oid.to_i
......
497 513
                                              :class => 'version'
498 514
            end
499 515
          when 'commit'
500
            if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
501
              link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
502
                                           :class => 'changeset',
503
                                           :title => truncate_single_line(changeset.comments, :length => 100)
516
            if revproj.nil?
517
              if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
518
                link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
519
                                             :class => 'changeset',
520
                                             :title => truncate_single_line(changeset.comments, :length => 100)
521
              end
522
            else
523
              revproj.gsub!(/[\:"]/,'')
524
              link_project = Project.find_by_name(h(revproj)) || Project.find_by_identifier(h(revproj))
525
              if link_project && (changeset = link_project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
526
                title = truncate_single_line(changeset.comments, :length => 100) if authorize_for('repositories', 'revision', link_project)
527
                link = link_to h("#{revproj}:#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :rev => changeset.revision},
528
                                                        :class => 'changeset',
529
                                                        :title => title
530
              end
504 531
            end
505 532
          when 'source', 'export'
506 533
            if project && project.repository
......
521 548
          end
522 549
        end
523 550
      end
524
      leading + (link || "#{prefix}#{sep}#{oid}")
551
      leading + (link || (revproj ? "#{revproj}:" : "") + "#{prefix}#{sep}#{oid}")
525 552
    end
526 553

  
527 554
    text
public/help/wiki_syntax_detailed.html
49 49
            <li>Link to a changeset with a non-numeric hash: <strong>commit:c6f4d0fd</strong> (displays c6f4d0fd).  Added in <a href="/repositories/revision/1?rev=1236" class="changeset" title="Merged Git support branch (r1200 to r1226).">r1236</a>.</li>
50 50
        </ul>
51 51

  
52
        <p>You can also link to changesets of an other project repository:</p>
53

  
54
        <ul>
55
            <li>Link to a changeset of SandBox project: <strong>sandbox:r758</strong> (displays <a href="/repositories/revision/1?rev=758" class="changeset" title="Search engine now only searches objects the user is allowed to view.">sandbox:r758</a>)</li>
56
            <li>Link to a changeset of SandBox project with a non-numeric hash: <strong>sandbox:commit:c6f4d0fd</strong> (displays sandbox:c6f4d0fd).</li>
57
            <li>If your project name contains spaces, use quotation marks : <strong>"My Project":r758</strong> or <strong>"My Project":commit:c6f4d0fd</strong></li>
58
        </ul>
59

  
52 60
        <p>Wiki links:</p>
53 61

  
54 62
        <ul>
test/fixtures/changes.yml
20 20
  path: /test/some/path/in/the/repo
21 21
  from_path:
22 22
  from_revision:
23
changes_009:
24
  id: 9
25
  changeset_id: 108
26
  action: M
27
  path: /test/some/path/in/the/repo
28
  from_path:
29
  from_revision:
30
changes_0010:
31
  id: 10
32
  changeset_id: 109
33
  action: M
34
  path: /test/some/path/in/the/repo
35
  from_path:
36
  from_revision:
23 37
  
test/fixtures/changesets.yml
80 80
  user_id: 3
81 81
  repository_id: 10
82 82
  committer: dlopper
83
changesets_009:
84
  commit_date: 2007-04-11
85
  committed_on: 2007-04-11 15:14:44 +02:00
86
  revision: 1
87
  id: 108
88
  comments: My very first commit
89
  repository_id: 12
90
  committer: dlopper
91
  user_id: 3
92
changesets_010:
93
  commit_date: 2007-04-12
94
  committed_on: 2007-04-12 15:14:44 +02:00
95
  revision: 2
96
  id: 109
97
  comments: 'This commit fixes #5'
98
  repository_id: 12
99
  committer: dlopper
100
  user_id: 3
83 101
  
test/fixtures/repositories.yml
15 15
  password: ""
16 16
  login: ""
17 17
  type: Subversion
18

  
19
repositories_003:
20
  project_id: 3
21
  url: svn://localhost/test
22
  id: 12
23
  root_url: svn://localhost
24
  password: ""
25
  login: ""
26
  type: Subversion
test/unit/helpers/application_helper_test.rb
31 31

  
32 32
  def setup
33 33
    super
34
    @admin = User.find(1)
35
    @jsmith = User.find(2)
36
    @dlopper = User.find(3)
34 37
  end
35 38
  
36 39
  def test_auto_links
......
126 129
                                   :class => 'changeset', :title => 'My very first commit')
127 130
    changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
128 131
                                    :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
129
    
132

  
133
    changeset_link_other_project_id = link_to('subproject1:r1', {:controller => 'repositories', :action => 'revision', :id => 'subproject1', :rev => 1},
134
                                    :class => 'changeset', :title => 'My very first commit')
135

  
136
    changeset_link_other_project_name = link_to('eCookbook Subproject 1:r2', {:controller => 'repositories', :action => 'revision', :id => 'subproject1', :rev => 2},
137
                                    :class => 'changeset', :title => 'This commit fixes #5')
138

  
130 139
    document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
131 140
                                             :class => 'document')
132 141
    
......
146 155
      'r1.'                         => "#{changeset_link}.",
147 156
      'r1, r2'                      => "#{changeset_link}, #{changeset_link2}",
148 157
      'r1,r2'                       => "#{changeset_link},#{changeset_link2}",
158
      'subproject1:r1'              => "#{changeset_link_other_project_id}",
159
      'subproject1:r1.'            => "#{changeset_link_other_project_id}.",
160
      'subproject1:r1,"eCookbook Subproject 1":r2'            => "#{changeset_link_other_project_id},#{changeset_link_other_project_name}",
149 161
      # documents
150 162
      'document#1'                  => document_link,
151 163
      'document:"Test document"'    => document_link,
......
184 196
      "http://foo.bar/FAQ#3"       => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
185 197
    }
186 198
    @project = Project.find(1)
199
    User.current = @admin
200
    
187 201
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
202

  
203
    User.current = nil
188 204
  end
189 205
  
190 206
  def test_wiki_links
......
277 293
              }
278 294
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
279 295
  end
280
  
296

  
281 297
  def test_wiki_horizontal_rule
282 298
    assert_equal '<hr />', textilizable('---')
283 299
    assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')