Chris@441
|
1 # Redmine - project management software
|
Chris@441
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@441
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@441
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'redmine/scm/adapters/mercurial_adapter'
|
Chris@0
|
19
|
Chris@0
|
20 class Repository::Mercurial < Repository
|
Chris@119
|
21 # sort changesets by revision number
|
Chris@119
|
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
|
Chris@119
|
23
|
Chris@0
|
24 attr_protected :root_url
|
Chris@0
|
25 validates_presence_of :url
|
Chris@0
|
26
|
Chris@245
|
27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
|
Chris@245
|
28
|
Chris@245
|
29 def self.human_attribute_name(attribute_key_name)
|
Chris@441
|
30 attr_name = attribute_key_name
|
Chris@441
|
31 if attr_name == "url"
|
Chris@441
|
32 attr_name = "path_to_repository"
|
Chris@441
|
33 end
|
Chris@441
|
34 super(attr_name)
|
Chris@245
|
35 end
|
Chris@245
|
36
|
Chris@245
|
37 def self.scm_adapter_class
|
Chris@0
|
38 Redmine::Scm::Adapters::MercurialAdapter
|
Chris@0
|
39 end
|
Chris@119
|
40
|
Chris@0
|
41 def self.scm_name
|
Chris@0
|
42 'Mercurial'
|
Chris@0
|
43 end
|
Chris@119
|
44
|
Chris@441
|
45 def supports_directory_revisions?
|
Chris@441
|
46 true
|
Chris@441
|
47 end
|
Chris@441
|
48
|
Chris@245
|
49 def repo_log_encoding
|
Chris@245
|
50 'UTF-8'
|
Chris@245
|
51 end
|
Chris@245
|
52
|
Chris@119
|
53 # Returns the readable identifier for the given mercurial changeset
|
Chris@119
|
54 def self.format_changeset_identifier(changeset)
|
Chris@119
|
55 "#{changeset.revision}:#{changeset.scmid}"
|
Chris@119
|
56 end
|
Chris@119
|
57
|
Chris@119
|
58 # Returns the identifier for the given Mercurial changeset
|
Chris@119
|
59 def self.changeset_identifier(changeset)
|
Chris@119
|
60 changeset.scmid
|
Chris@119
|
61 end
|
Chris@119
|
62
|
Chris@119
|
63 def diff_format_revisions(cs, cs_to, sep=':')
|
Chris@119
|
64 super(cs, cs_to, ' ')
|
Chris@119
|
65 end
|
Chris@119
|
66
|
Chris@119
|
67 # Finds and returns a revision with a number or the beginning of a hash
|
Chris@119
|
68 def find_changeset_by_name(name)
|
Chris@119
|
69 return nil if name.nil? || name.empty?
|
Chris@119
|
70 if /[^\d]/ =~ name or name.to_s.size > 8
|
Chris@119
|
71 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
|
Chris@119
|
72 else
|
Chris@119
|
73 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
|
Chris@119
|
74 end
|
Chris@119
|
75 return e if e
|
Chris@119
|
76 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
|
Chris@119
|
77 end
|
Chris@119
|
78
|
Chris@119
|
79 # Returns the latest changesets for +path+; sorted by revision number
|
Chris@441
|
80 #
|
Chris@441
|
81 # Because :order => 'id DESC' is defined at 'has_many',
|
Chris@441
|
82 # there is no need to set 'order'.
|
Chris@441
|
83 # But, MySQL test fails.
|
Chris@441
|
84 # Sqlite3 and PostgreSQL pass.
|
Chris@441
|
85 # Is this MySQL bug?
|
Chris@119
|
86 def latest_changesets(path, rev, limit=10)
|
Chris@441
|
87 changesets.find(:all, :include => :user,
|
Chris@441
|
88 :conditions => latest_changesets_cond(path, rev, limit),
|
Chris@441
|
89 :limit => limit, :order => "#{Changeset.table_name}.id DESC")
|
Chris@441
|
90 end
|
Chris@441
|
91
|
Chris@441
|
92 def latest_changesets_cond(path, rev, limit)
|
Chris@441
|
93 cond, args = [], []
|
Chris@441
|
94 if scm.branchmap.member? rev
|
Chris@441
|
95 # Mercurial named branch is *stable* in each revision.
|
Chris@441
|
96 # So, named branch can be stored in database.
|
Chris@441
|
97 # Mercurial provides *bookmark* which is equivalent with git branch.
|
Chris@441
|
98 # But, bookmark is not implemented.
|
Chris@441
|
99 cond << "#{Changeset.table_name}.scmid IN (?)"
|
Chris@441
|
100 # Revisions in root directory and sub directory are not equal.
|
Chris@441
|
101 # So, in order to get correct limit, we need to get all revisions.
|
Chris@441
|
102 # But, it is very heavy.
|
Chris@441
|
103 # Mercurial does not treat direcotry.
|
Chris@441
|
104 # So, "hg log DIR" is very heavy.
|
Chris@441
|
105 branch_limit = path.blank? ? limit : ( limit * 5 )
|
Chris@441
|
106 args << scm.nodes_in_branch(rev, :limit => branch_limit)
|
Chris@441
|
107 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
|
Chris@441
|
108 cond << "#{Changeset.table_name}.id <= ?"
|
Chris@441
|
109 args << last.id
|
Chris@119
|
110 end
|
Chris@441
|
111
|
Chris@441
|
112 unless path.blank?
|
Chris@441
|
113 cond << "EXISTS (SELECT * FROM #{Change.table_name}
|
Chris@441
|
114 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
|
Chris@441
|
115 AND (#{Change.table_name}.path = ?
|
Chris@441
|
116 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
|
Chris@441
|
117 args << path.with_leading_slash
|
Chris@441
|
118 args << "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%" << '\\'
|
Chris@441
|
119 end
|
Chris@441
|
120
|
Chris@441
|
121 [cond.join(' AND '), *args] unless cond.empty?
|
Chris@119
|
122 end
|
Chris@441
|
123 private :latest_changesets_cond
|
Chris@119
|
124
|
Chris@0
|
125 def fetch_changesets
|
Chris@507
|
126 return if scm.info.nil?
|
Chris@245
|
127 scm_rev = scm.info.lastrev.revision.to_i
|
Chris@245
|
128 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
|
Chris@245
|
129 return unless db_rev < scm_rev # already up-to-date
|
Chris@245
|
130
|
Chris@245
|
131 logger.debug "Fetching changesets for repository #{url}" if logger
|
Chris@245
|
132 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
|
Chris@245
|
133 transaction do
|
Chris@245
|
134 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
|
Chris@245
|
135 cs = Changeset.create(:repository => self,
|
Chris@245
|
136 :revision => re.revision,
|
Chris@245
|
137 :scmid => re.scmid,
|
Chris@245
|
138 :committer => re.author,
|
Chris@245
|
139 :committed_on => re.time,
|
Chris@245
|
140 :comments => re.message)
|
Chris@245
|
141 re.paths.each { |e| cs.create_change(e) }
|
Chris@0
|
142 end
|
Chris@0
|
143 end
|
Chris@0
|
144 end
|
Chris@245
|
145 self
|
Chris@0
|
146 end
|
Chris@0
|
147 end
|