annotate .svn/pristine/be/be06c1fdad33ad10b8d30c53349e2482e780c5fc.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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