annotate app/models/repository/mercurial.rb @ 1600:ed9c467ef922 dockerise

Add hggit extension
author Chris Cannam
date Wed, 23 Aug 2017 11:32:50 +0100
parents a1bdbf8a87d5
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1136 18 require_dependency 'redmine/scm/adapters/mercurial_adapter'
Chris@0 19
Chris@0 20 class Repository::Mercurial < Repository
Chris@119 21 # sort changesets by revision number
Chris@909 22 has_many :changesets,
Chris@909 23 :order => "#{Changeset.table_name}.id DESC",
Chris@909 24 :foreign_key => 'repository_id'
Chris@119 25
Chris@909 26 attr_protected :root_url
luis@225 27 # validates_presence_of :url
Chris@0 28
Chris@909 29 # number of changesets to fetch at once
Chris@909 30 FETCH_AT_ONCE = 100
Chris@245 31
Chris@1115 32 def self.human_attribute_name(attribute_key_name, *args)
Chris@1115 33 attr_name = attribute_key_name.to_s
Chris@441 34 if attr_name == "url"
Chris@441 35 attr_name = "path_to_repository"
Chris@441 36 end
Chris@1115 37 super(attr_name, *args)
Chris@245 38 end
Chris@245 39
Chris@245 40 def self.scm_adapter_class
Chris@0 41 Redmine::Scm::Adapters::MercurialAdapter
Chris@0 42 end
Chris@119 43
Chris@0 44 def self.scm_name
Chris@0 45 'Mercurial'
Chris@0 46 end
Chris@119 47
Chris@441 48 def supports_directory_revisions?
Chris@441 49 true
Chris@441 50 end
Chris@441 51
Chris@909 52 def supports_revision_graph?
Chris@909 53 true
Chris@909 54 end
Chris@909 55
Chris@245 56 def repo_log_encoding
Chris@245 57 'UTF-8'
Chris@245 58 end
Chris@245 59
Chris@119 60 # Returns the readable identifier for the given mercurial changeset
Chris@119 61 def self.format_changeset_identifier(changeset)
Chris@1517 62 "#{changeset.revision}:#{changeset.scmid[0, 12]}"
Chris@119 63 end
Chris@119 64
Chris@119 65 # Returns the identifier for the given Mercurial changeset
Chris@119 66 def self.changeset_identifier(changeset)
Chris@119 67 changeset.scmid
Chris@119 68 end
Chris@119 69
Chris@119 70 def diff_format_revisions(cs, cs_to, sep=':')
Chris@119 71 super(cs, cs_to, ' ')
Chris@119 72 end
Chris@119 73
Chris@1517 74 def modify_entry_lastrev_identifier(entry)
Chris@1517 75 if entry.lastrev && entry.lastrev.identifier
Chris@1517 76 entry.lastrev.identifier = scmid_for_inserting_db(entry.lastrev.identifier)
Chris@1517 77 end
Chris@1517 78 end
Chris@1517 79 private :modify_entry_lastrev_identifier
Chris@1517 80
Chris@1517 81 def entry(path=nil, identifier=nil)
Chris@1517 82 entry = scm.entry(path, identifier)
Chris@1517 83 return nil if entry.nil?
Chris@1517 84 modify_entry_lastrev_identifier(entry)
Chris@1517 85 entry
Chris@1517 86 end
Chris@1517 87
Chris@1517 88 def scm_entries(path=nil, identifier=nil)
Chris@1517 89 entries = scm.entries(path, identifier)
Chris@1517 90 return nil if entries.nil?
Chris@1517 91 entries.each {|entry| modify_entry_lastrev_identifier(entry)}
Chris@1517 92 entries
Chris@1517 93 end
Chris@1517 94 protected :scm_entries
Chris@1517 95
Chris@119 96 # Finds and returns a revision with a number or the beginning of a hash
Chris@119 97 def find_changeset_by_name(name)
Chris@1115 98 return nil if name.blank?
Chris@1115 99 s = name.to_s
Chris@1115 100 if /[^\d]/ =~ s or s.size > 8
Chris@1115 101 cs = changesets.where(:scmid => s).first
Chris@119 102 else
Chris@1115 103 cs = changesets.where(:revision => s).first
Chris@119 104 end
Chris@1115 105 return cs if cs
Chris@1115 106 changesets.where('scmid LIKE ?', "#{s}%").first
Chris@119 107 end
Chris@119 108
Chris@119 109 # Returns the latest changesets for +path+; sorted by revision number
Chris@441 110 #
Chris@441 111 # Because :order => 'id DESC' is defined at 'has_many',
Chris@441 112 # there is no need to set 'order'.
Chris@441 113 # But, MySQL test fails.
Chris@441 114 # Sqlite3 and PostgreSQL pass.
Chris@441 115 # Is this MySQL bug?
Chris@119 116 def latest_changesets(path, rev, limit=10)
Chris@1464 117 changesets.
Chris@1464 118 includes(:user).
Chris@1464 119 where(latest_changesets_cond(path, rev, limit)).
Chris@1464 120 limit(limit).
Chris@1464 121 order("#{Changeset.table_name}.id DESC").
Chris@1464 122 all
Chris@441 123 end
Chris@441 124
Chris@1517 125 def is_short_id_in_db?
Chris@1517 126 return @is_short_id_in_db unless @is_short_id_in_db.nil?
Chris@1517 127 cs = changesets.first
Chris@1517 128 @is_short_id_in_db = (!cs.nil? && cs.scmid.length != 40)
Chris@1517 129 end
Chris@1517 130 private :is_short_id_in_db?
Chris@1517 131
Chris@1517 132 def scmid_for_inserting_db(scmid)
Chris@1517 133 is_short_id_in_db? ? scmid[0, 12] : scmid
Chris@1517 134 end
Chris@1517 135
Chris@1517 136 def nodes_in_branch(rev, branch_limit)
Chris@1517 137 scm.nodes_in_branch(rev, :limit => branch_limit).collect do |b|
Chris@1517 138 scmid_for_inserting_db(b)
Chris@1517 139 end
Chris@1517 140 end
Chris@1517 141
Chris@1517 142 def tag_scmid(rev)
Chris@1517 143 scmid = scm.tagmap[rev]
Chris@1517 144 scmid.nil? ? nil : scmid_for_inserting_db(scmid)
Chris@1517 145 end
Chris@1517 146
Chris@441 147 def latest_changesets_cond(path, rev, limit)
Chris@441 148 cond, args = [], []
Chris@441 149 if scm.branchmap.member? rev
Chris@441 150 # Mercurial named branch is *stable* in each revision.
Chris@441 151 # So, named branch can be stored in database.
Chris@441 152 # Mercurial provides *bookmark* which is equivalent with git branch.
Chris@441 153 # But, bookmark is not implemented.
Chris@441 154 cond << "#{Changeset.table_name}.scmid IN (?)"
Chris@441 155 # Revisions in root directory and sub directory are not equal.
Chris@441 156 # So, in order to get correct limit, we need to get all revisions.
Chris@441 157 # But, it is very heavy.
Chris@441 158 # Mercurial does not treat direcotry.
Chris@441 159 # So, "hg log DIR" is very heavy.
Chris@441 160 branch_limit = path.blank? ? limit : ( limit * 5 )
Chris@1517 161 args << nodes_in_branch(rev, branch_limit)
Chris@1517 162 elsif last = rev ? find_changeset_by_name(tag_scmid(rev) || rev) : nil
Chris@441 163 cond << "#{Changeset.table_name}.id <= ?"
Chris@441 164 args << last.id
Chris@119 165 end
Chris@441 166 unless path.blank?
Chris@441 167 cond << "EXISTS (SELECT * FROM #{Change.table_name}
Chris@441 168 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
Chris@441 169 AND (#{Change.table_name}.path = ?
Chris@441 170 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
Chris@441 171 args << path.with_leading_slash
Chris@909 172 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
Chris@441 173 end
Chris@441 174 [cond.join(' AND '), *args] unless cond.empty?
Chris@119 175 end
Chris@441 176 private :latest_changesets_cond
Chris@119 177
Chris@0 178 def fetch_changesets
Chris@507 179 return if scm.info.nil?
Chris@245 180 scm_rev = scm.info.lastrev.revision.to_i
Chris@909 181 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
Chris@245 182 return unless db_rev < scm_rev # already up-to-date
Chris@245 183
Chris@245 184 logger.debug "Fetching changesets for repository #{url}" if logger
Chris@245 185 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
Chris@1115 186 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
Chris@1115 187 transaction do
Chris@1517 188 parents = (re.parents || []).collect do |rp|
Chris@1517 189 find_changeset_by_name(scmid_for_inserting_db(rp))
Chris@1517 190 end.compact
Chris@909 191 cs = Changeset.create(:repository => self,
Chris@909 192 :revision => re.revision,
Chris@1517 193 :scmid => scmid_for_inserting_db(re.scmid),
Chris@909 194 :committer => re.author,
Chris@245 195 :committed_on => re.time,
Chris@1115 196 :comments => re.message,
Chris@1115 197 :parents => parents)
Chris@1115 198 unless cs.new_record?
Chris@1517 199 re.paths.each do |e|
Chris@1517 200 if from_revision = e[:from_revision]
Chris@1517 201 e[:from_revision] = scmid_for_inserting_db(from_revision)
Chris@1517 202 end
Chris@1517 203 cs.create_change(e)
Chris@1517 204 end
Chris@909 205 end
Chris@0 206 end
Chris@0 207 end
Chris@0 208 end
Chris@0 209 end
Chris@0 210 end