##// END OF EJS Templates
scm: mercurial: change two revision diff text of mercurial (#3724)....
Toshi MARUYAMA -
r4579:eda1cd2faa39
parent child
Show More
@@ -1,126 +1,130
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/mercurial_adapter'
19 19
20 20 class Repository::Mercurial < Repository
21 21 # sort changesets by revision number
22 22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
23 23
24 24 attr_protected :root_url
25 25 validates_presence_of :url
26 26
27 27 def scm_adapter
28 28 Redmine::Scm::Adapters::MercurialAdapter
29 29 end
30 30
31 31 def self.scm_name
32 32 'Mercurial'
33 33 end
34 34
35 35 # Returns the readable identifier for the given mercurial changeset
36 36 def self.format_changeset_identifier(changeset)
37 37 "#{changeset.revision}:#{changeset.scmid}"
38 38 end
39 39
40 40 # Returns the identifier for the given Mercurial changeset
41 41 def self.changeset_identifier(changeset)
42 42 changeset.scmid
43 43 end
44 44
45 def diff_format_revisions(cs, cs_to, sep=':')
46 super(cs, cs_to, ' ')
47 end
48
45 49 def entries(path=nil, identifier=nil)
46 50 entries=scm.entries(path, identifier)
47 51 if entries
48 52 entries.each do |entry|
49 53 next unless entry.is_file?
50 54 # Set the filesize unless browsing a specific revision
51 55 if identifier.nil?
52 56 full_path = File.join(root_url, entry.path)
53 57 entry.size = File.stat(full_path).size if File.file?(full_path)
54 58 end
55 59 # Search the DB for the entry's last change
56 60 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
57 61 if change
58 62 entry.lastrev.identifier = change.changeset.revision
59 63 entry.lastrev.name = change.changeset.revision
60 64 entry.lastrev.author = change.changeset.committer
61 65 entry.lastrev.revision = change.revision
62 66 end
63 67 end
64 68 end
65 69 entries
66 70 end
67 71
68 72 # Finds and returns a revision with a number or the beginning of a hash
69 73 def find_changeset_by_name(name)
70 74 if /[^\d]/ =~ name or name.to_s.size > 8
71 75 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
72 76 else
73 77 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
74 78 end
75 79 return e if e
76 80 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
77 81 end
78 82
79 83 # Returns the latest changesets for +path+; sorted by revision number
80 84 def latest_changesets(path, rev, limit=10)
81 85 if path.blank?
82 86 changesets.find(:all, :include => :user, :limit => limit)
83 87 else
84 88 changes.find(:all, :include => {:changeset => :user},
85 89 :conditions => ["path = ?", path.with_leading_slash],
86 90 :order => "#{Changeset.table_name}.id DESC",
87 91 :limit => limit).collect(&:changeset)
88 92 end
89 93 end
90 94
91 95 def fetch_changesets
92 96 scm_info = scm.info
93 97 if scm_info
94 98 # latest revision found in database
95 99 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
96 100 # latest revision in the repository
97 101 latest_revision = scm_info.lastrev
98 102 return if latest_revision.nil?
99 103 scm_revision = latest_revision.identifier.to_i
100 104 if db_revision < scm_revision
101 105 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
102 106 identifier_from = db_revision + 1
103 107 while (identifier_from <= scm_revision)
104 108 # loads changesets by batches of 100
105 109 identifier_to = [identifier_from + 99, scm_revision].min
106 110 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
107 111 transaction do
108 112 revisions.each do |revision|
109 113 changeset = Changeset.create(:repository => self,
110 114 :revision => revision.identifier,
111 115 :scmid => revision.scmid,
112 116 :committer => revision.author,
113 117 :committed_on => revision.time,
114 118 :comments => revision.message)
115 119
116 120 revision.paths.each do |change|
117 121 changeset.create_change(change)
118 122 end
119 123 end
120 124 end unless revisions.nil?
121 125 identifier_from = identifier_to + 1
122 126 end
123 127 end
124 128 end
125 129 end
126 130 end
@@ -1,180 +1,202
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19 require 'repositories_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class RepositoriesController; def rescue_action(e) raise e end; end
23 23
24 24 class RepositoriesMercurialControllerTest < ActionController::TestCase
25 25 fixtures :projects, :users, :roles, :members, :member_roles, :repositories, :enabled_modules
26 26
27 27 # No '..' in the repository path
28 28 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
29 29
30 30 def setup
31 31 @controller = RepositoriesController.new
32 32 @request = ActionController::TestRequest.new
33 33 @response = ActionController::TestResponse.new
34 34 User.current = nil
35 35 @repository = Repository::Mercurial.create(:project => Project.find(3), :url => REPOSITORY_PATH)
36 36 assert @repository
37 37 end
38 38
39 39 if File.directory?(REPOSITORY_PATH)
40 40 def test_show
41 41 get :show, :id => 3
42 42 assert_response :success
43 43 assert_template 'show'
44 44 assert_not_nil assigns(:entries)
45 45 assert_not_nil assigns(:changesets)
46 46 end
47 47
48 48 def test_show_root
49 49 get :show, :id => 3
50 50 assert_response :success
51 51 assert_template 'show'
52 52 assert_not_nil assigns(:entries)
53 53 assert_equal 4, assigns(:entries).size
54 54 assert assigns(:entries).detect {|e| e.name == 'images' && e.kind == 'dir'}
55 55 assert assigns(:entries).detect {|e| e.name == 'sources' && e.kind == 'dir'}
56 56 assert assigns(:entries).detect {|e| e.name == 'README' && e.kind == 'file'}
57 57 end
58 58
59 59 def test_show_directory
60 60 get :show, :id => 3, :path => ['images']
61 61 assert_response :success
62 62 assert_template 'show'
63 63 assert_not_nil assigns(:entries)
64 64 assert_equal ['delete.png', 'edit.png'], assigns(:entries).collect(&:name)
65 65 entry = assigns(:entries).detect {|e| e.name == 'edit.png'}
66 66 assert_not_nil entry
67 67 assert_equal 'file', entry.kind
68 68 assert_equal 'images/edit.png', entry.path
69 69 end
70 70
71 71 def test_show_at_given_revision
72 72 [0, '0', '0885933ad4f6'].each do |r1|
73 73 get :show, :id => 3, :path => ['images'], :rev => r1
74 74 assert_response :success
75 75 assert_template 'show'
76 76 assert_not_nil assigns(:entries)
77 77 assert_equal ['delete.png'], assigns(:entries).collect(&:name)
78 78 end
79 79 end
80 80
81 81 def test_show_directory_sql_escape_percent
82 82 [13, '13', '3a330eb32958'].each do |r1|
83 83 get :show, :id => 3, :path => ['sql_escape', 'percent%dir'], :rev => r1
84 84 assert_response :success
85 85 assert_template 'show'
86 86
87 87 assert_not_nil assigns(:entries)
88 88 assert_equal ['percent%file1.txt', 'percentfile1.txt'], assigns(:entries).collect(&:name)
89 89 changesets = assigns(:changesets)
90 90
91 91 ## This is not yet implemented.
92 92 # assert_not_nil changesets
93 93 # assert_equal %w(13 11 10 9), changesets.collect(&:revision)
94 94 end
95 95 end
96 96
97 97 def test_changes
98 98 get :changes, :id => 3, :path => ['images', 'edit.png']
99 99 assert_response :success
100 100 assert_template 'changes'
101 101 assert_tag :tag => 'h2', :content => 'edit.png'
102 102 end
103 103
104 104 def test_entry_show
105 105 get :entry, :id => 3, :path => ['sources', 'watchers_controller.rb']
106 106 assert_response :success
107 107 assert_template 'entry'
108 108 # Line 10
109 109 assert_tag :tag => 'th',
110 110 :content => '10',
111 111 :attributes => { :class => 'line-num' },
112 112 :sibling => { :tag => 'td', :content => /WITHOUT ANY WARRANTY/ }
113 113 end
114 114
115 115 def test_entry_download
116 116 get :entry, :id => 3, :path => ['sources', 'watchers_controller.rb'], :format => 'raw'
117 117 assert_response :success
118 118 # File content
119 119 assert @response.body.include?('WITHOUT ANY WARRANTY')
120 120 end
121 121
122 122 def test_directory_entry
123 123 get :entry, :id => 3, :path => ['sources']
124 124 assert_response :success
125 125 assert_template 'show'
126 126 assert_not_nil assigns(:entry)
127 127 assert_equal 'sources', assigns(:entry).name
128 128 end
129 129
130 130 def test_diff
131 @repository.fetch_changesets
132 @repository.reload
133
131 134 [4, '4', 'def6d2f1254a'].each do |r1|
132 135 # Full diff of changeset 4
133 136 get :diff, :id => 3, :rev => 4
134 137 assert_response :success
135 138 assert_template 'diff'
136 139
137 140 if @repository.scm.class.client_version_above?([1, 2])
138 141 # Line 22 removed
139 142 assert_tag :tag => 'th',
140 143 :content => '22',
141 144 :sibling => { :tag => 'td',
142 145 :attributes => { :class => /diff_out/ },
143 146 :content => /def remove/ }
147 assert_tag :tag => 'h2', :content => /4:def6d2f1254a/
148 end
149 end
150 end
151
152 def test_diff_two_revs
153 @repository.fetch_changesets
154 @repository.reload
155
156 [2, '400bb8672109', '400', 400].each do |r1|
157 [4, 'def6d2f1254a'].each do |r2|
158 get :diff, :id => 3, :rev => r1,
159 :rev_to => r2
160 assert_response :success
161 assert_template 'diff'
162
163 diff = assigns(:diff)
164 assert_not_nil diff
165 assert_tag :tag => 'h2', :content => /4:def6d2f1254a 2:400bb8672109/
144 166 end
145 167 end
146 168 end
147 169
148 170 def test_annotate
149 171 get :annotate, :id => 3, :path => ['sources', 'watchers_controller.rb']
150 172 assert_response :success
151 173 assert_template 'annotate'
152 174 # Line 23, revision 4:def6d2f1254a
153 175 assert_tag :tag => 'th',
154 176 :content => '23',
155 177 :attributes => { :class => 'line-num' },
156 178 :sibling =>
157 179 {
158 180 :tag => 'td',
159 181 :attributes => { :class => 'revision' },
160 182 :child => { :tag => 'a', :content => '4:def6d2f1254a' }
161 183 }
162 184 assert_tag :tag => 'th',
163 185 :content => '23',
164 186 :attributes => { :class => 'line-num' },
165 187 :sibling =>
166 188 {
167 189 :tag => 'td' ,
168 190 :content => 'jsmith' ,
169 191 :attributes => { :class => 'author' },
170 192 }
171 193 assert_tag :tag => 'th',
172 194 :content => '23',
173 195 :attributes => { :class => 'line-num' },
174 196 :sibling => { :tag => 'td', :content => /watcher =/ }
175 197 end
176 198 else
177 199 puts "Mercurial test repository NOT FOUND. Skipping functional tests !!!"
178 200 def test_fake; assert true end
179 201 end
180 202 end
General Comments 0
You need to be logged in to leave comments. Login now