##// END OF EJS Templates
Fixed: repository: mercurial: sort changesets by revision (#3449, #3567)....
Toshi MARUYAMA -
r4491:2ae2d3ef834c
parent child
Show More
@@ -1,90 +1,105
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'redmine/scm/adapters/mercurial_adapter'
18 require 'redmine/scm/adapters/mercurial_adapter'
19
19
20 class Repository::Mercurial < Repository
20 class Repository::Mercurial < Repository
21 # sort changesets by revision number
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
23
21 attr_protected :root_url
24 attr_protected :root_url
22 validates_presence_of :url
25 validates_presence_of :url
23
26
24 def scm_adapter
27 def scm_adapter
25 Redmine::Scm::Adapters::MercurialAdapter
28 Redmine::Scm::Adapters::MercurialAdapter
26 end
29 end
27
30
28 def self.scm_name
31 def self.scm_name
29 'Mercurial'
32 'Mercurial'
30 end
33 end
31
34
32 def entries(path=nil, identifier=nil)
35 def entries(path=nil, identifier=nil)
33 entries=scm.entries(path, identifier)
36 entries=scm.entries(path, identifier)
34 if entries
37 if entries
35 entries.each do |entry|
38 entries.each do |entry|
36 next unless entry.is_file?
39 next unless entry.is_file?
37 # Set the filesize unless browsing a specific revision
40 # Set the filesize unless browsing a specific revision
38 if identifier.nil?
41 if identifier.nil?
39 full_path = File.join(root_url, entry.path)
42 full_path = File.join(root_url, entry.path)
40 entry.size = File.stat(full_path).size if File.file?(full_path)
43 entry.size = File.stat(full_path).size if File.file?(full_path)
41 end
44 end
42 # Search the DB for the entry's last change
45 # Search the DB for the entry's last change
43 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
46 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
44 if change
47 if change
45 entry.lastrev.identifier = change.changeset.revision
48 entry.lastrev.identifier = change.changeset.revision
46 entry.lastrev.name = change.changeset.revision
49 entry.lastrev.name = change.changeset.revision
47 entry.lastrev.author = change.changeset.committer
50 entry.lastrev.author = change.changeset.committer
48 entry.lastrev.revision = change.revision
51 entry.lastrev.revision = change.revision
49 end
52 end
50 end
53 end
51 end
54 end
52 entries
55 entries
53 end
56 end
54
57
58 # Returns the latest changesets for +path+; sorted by revision number
59 def latest_changesets(path, rev, limit=10)
60 if path.blank?
61 changesets.find(:all, :include => :user, :limit => limit)
62 else
63 changes.find(:all, :include => {:changeset => :user},
64 :conditions => ["path = ?", path.with_leading_slash],
65 :order => "#{Changeset.table_name}.id DESC",
66 :limit => limit).collect(&:changeset)
67 end
68 end
69
55 def fetch_changesets
70 def fetch_changesets
56 scm_info = scm.info
71 scm_info = scm.info
57 if scm_info
72 if scm_info
58 # latest revision found in database
73 # latest revision found in database
59 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
74 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
60 # latest revision in the repository
75 # latest revision in the repository
61 latest_revision = scm_info.lastrev
76 latest_revision = scm_info.lastrev
62 return if latest_revision.nil?
77 return if latest_revision.nil?
63 scm_revision = latest_revision.identifier.to_i
78 scm_revision = latest_revision.identifier.to_i
64 if db_revision < scm_revision
79 if db_revision < scm_revision
65 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
80 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
66 identifier_from = db_revision + 1
81 identifier_from = db_revision + 1
67 while (identifier_from <= scm_revision)
82 while (identifier_from <= scm_revision)
68 # loads changesets by batches of 100
83 # loads changesets by batches of 100
69 identifier_to = [identifier_from + 99, scm_revision].min
84 identifier_to = [identifier_from + 99, scm_revision].min
70 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
85 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
71 transaction do
86 transaction do
72 revisions.each do |revision|
87 revisions.each do |revision|
73 changeset = Changeset.create(:repository => self,
88 changeset = Changeset.create(:repository => self,
74 :revision => revision.identifier,
89 :revision => revision.identifier,
75 :scmid => revision.scmid,
90 :scmid => revision.scmid,
76 :committer => revision.author,
91 :committer => revision.author,
77 :committed_on => revision.time,
92 :committed_on => revision.time,
78 :comments => revision.message)
93 :comments => revision.message)
79
94
80 revision.paths.each do |change|
95 revision.paths.each do |change|
81 changeset.create_change(change)
96 changeset.create_change(change)
82 end
97 end
83 end
98 end
84 end unless revisions.nil?
99 end unless revisions.nil?
85 identifier_from = identifier_to + 1
100 identifier_from = identifier_to + 1
86 end
101 end
87 end
102 end
88 end
103 end
89 end
104 end
90 end
105 end
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,83 +1,94
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class RepositoryMercurialTest < ActiveSupport::TestCase
20 class RepositoryMercurialTest < ActiveSupport::TestCase
21 fixtures :projects
21 fixtures :projects
22
22
23 # No '..' in the repository path
23 # No '..' in the repository path
24 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
24 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
25
25
26 def setup
26 def setup
27 @project = Project.find(1)
27 @project = Project.find(1)
28 assert @repository = Repository::Mercurial.create(:project => @project, :url => REPOSITORY_PATH)
28 assert @repository = Repository::Mercurial.create(:project => @project, :url => REPOSITORY_PATH)
29 end
29 end
30
30
31 if File.directory?(REPOSITORY_PATH)
31 if File.directory?(REPOSITORY_PATH)
32 def test_fetch_changesets_from_scratch
32 def test_fetch_changesets_from_scratch
33 @repository.fetch_changesets
33 @repository.fetch_changesets
34 @repository.reload
34 @repository.reload
35
35
36 assert_equal 6, @repository.changesets.count
36 assert_equal 6, @repository.changesets.count
37 assert_equal 11, @repository.changes.count
37 assert_equal 11, @repository.changes.count
38 assert_equal "Initial import.\nThe repository contains 3 files.", @repository.changesets.find_by_revision('0').comments
38 assert_equal "Initial import.\nThe repository contains 3 files.", @repository.changesets.find_by_revision('0').comments
39 end
39 end
40
40
41 def test_fetch_changesets_incremental
41 def test_fetch_changesets_incremental
42 @repository.fetch_changesets
42 @repository.fetch_changesets
43 # Remove changesets with revision > 2
43 # Remove changesets with revision > 2
44 @repository.changesets.find(:all).each {|c| c.destroy if c.revision.to_i > 2}
44 @repository.changesets.find(:all).each {|c| c.destroy if c.revision.to_i > 2}
45 @repository.reload
45 @repository.reload
46 assert_equal 3, @repository.changesets.count
46 assert_equal 3, @repository.changesets.count
47
47
48 @repository.fetch_changesets
48 @repository.fetch_changesets
49 assert_equal 6, @repository.changesets.count
49 assert_equal 6, @repository.changesets.count
50 end
50 end
51
51
52 def test_entries
52 def test_entries
53 assert_equal 2, @repository.entries("sources", 2).size
53 assert_equal 2, @repository.entries("sources", 2).size
54 assert_equal 1, @repository.entries("sources", 3).size
54 assert_equal 1, @repository.entries("sources", 3).size
55 end
55 end
56
56
57 def test_locate_on_outdated_repository
57 def test_locate_on_outdated_repository
58 # Change the working dir state
58 # Change the working dir state
59 %x{hg -R #{REPOSITORY_PATH} up -r 0}
59 %x{hg -R #{REPOSITORY_PATH} up -r 0}
60 assert_equal 1, @repository.entries("images", 0).size
60 assert_equal 1, @repository.entries("images", 0).size
61 assert_equal 2, @repository.entries("images").size
61 assert_equal 2, @repository.entries("images").size
62 assert_equal 2, @repository.entries("images", 2).size
62 assert_equal 2, @repository.entries("images", 2).size
63 end
63 end
64
64
65 def test_cat
65 def test_cat
66 assert @repository.scm.cat("sources/welcome_controller.rb", 2)
66 assert @repository.scm.cat("sources/welcome_controller.rb", 2)
67 assert_nil @repository.scm.cat("sources/welcome_controller.rb")
67 assert_nil @repository.scm.cat("sources/welcome_controller.rb")
68 end
68 end
69
69
70 def test_isodatesec
70 def test_isodatesec
71 # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
71 # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
72 if @repository.scm.class.client_version_above?([1, 0])
72 if @repository.scm.class.client_version_above?([1, 0])
73 @repository.fetch_changesets
73 @repository.fetch_changesets
74 @repository.reload
74 @repository.reload
75 rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
75 rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
76 assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
76 assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
77 end
77 end
78 end
78 end
79
80 def test_changeset_order_by_revision
81 @repository.fetch_changesets
82 @repository.reload
83
84 c0 = @repository.latest_changeset
85 c1 = @repository.changesets.find_by_revision('0')
86 # sorted by revision (id), not by date
87 assert c0.revision.to_i > c1.revision.to_i
88 assert c0.committed_on < c1.committed_on
89 end
79 else
90 else
80 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
91 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
81 def test_fake; assert true end
92 def test_fake; assert true end
82 end
93 end
83 end
94 end
General Comments 0
You need to be logged in to leave comments. Login now