annotate app/models/repository/mercurial.rb @ 252:adc8466df404 cannam

* Restore branch menu in repo
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 03 Mar 2011 15:08:45 +0000
parents eeebe205a056
children 753f1380d6bc
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 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@0 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@0 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 ATTRIBUTE_KEY_NAMES = {
Chris@245 30 "url" => "Root directory",
Chris@245 31 }
Chris@245 32 def self.human_attribute_name(attribute_key_name)
Chris@245 33 ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
Chris@245 34 end
Chris@245 35
Chris@245 36 def self.scm_adapter_class
Chris@0 37 Redmine::Scm::Adapters::MercurialAdapter
Chris@0 38 end
Chris@119 39
Chris@0 40 def self.scm_name
Chris@0 41 'Mercurial'
Chris@0 42 end
Chris@119 43
Chris@245 44 def repo_log_encoding
Chris@245 45 'UTF-8'
Chris@245 46 end
Chris@245 47
Chris@119 48 # Returns the readable identifier for the given mercurial changeset
Chris@119 49 def self.format_changeset_identifier(changeset)
Chris@119 50 "#{changeset.revision}:#{changeset.scmid}"
Chris@119 51 end
Chris@119 52
Chris@119 53 # Returns the identifier for the given Mercurial changeset
Chris@119 54 def self.changeset_identifier(changeset)
Chris@119 55 changeset.scmid
Chris@119 56 end
Chris@119 57
Chris@245 58 def branches
chris@252 59 bras = scm.branches
chris@252 60 bras.sort unless bras == %w|default|
Chris@245 61 end
Chris@245 62
Chris@245 63 def tags
Chris@245 64 nil
Chris@245 65 end
Chris@245 66
Chris@119 67 def diff_format_revisions(cs, cs_to, sep=':')
Chris@119 68 super(cs, cs_to, ' ')
Chris@119 69 end
Chris@119 70
Chris@119 71 # Finds and returns a revision with a number or the beginning of a hash
Chris@119 72 def find_changeset_by_name(name)
Chris@119 73 return nil if name.nil? || name.empty?
Chris@119 74 if /[^\d]/ =~ name or name.to_s.size > 8
Chris@119 75 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
Chris@119 76 else
Chris@119 77 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
Chris@119 78 end
Chris@119 79 return e if e
Chris@119 80 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
Chris@119 81 end
Chris@119 82
Chris@119 83 # Returns the latest changesets for +path+; sorted by revision number
Chris@245 84 # Default behavior is to search in cached changesets
Chris@119 85 def latest_changesets(path, rev, limit=10)
Chris@119 86 if path.blank?
Chris@119 87 changesets.find(:all, :include => :user, :limit => limit)
Chris@119 88 else
Chris@245 89 changesets.find(:all, :select => "DISTINCT #{Changeset.table_name}.*",
Chris@245 90 :joins => :changes,
Chris@245 91 :conditions => ["#{Change.table_name}.path = ? OR #{Change.table_name}.path LIKE ? ESCAPE ?",
Chris@245 92 path.with_leading_slash,
Chris@245 93 "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%", '\\'],
Chris@245 94 :include => :user, :limit => limit)
Chris@119 95 end
Chris@119 96 end
Chris@119 97
Chris@0 98 def fetch_changesets
Chris@245 99 scm_rev = scm.info.lastrev.revision.to_i
Chris@245 100 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
Chris@245 101 return unless db_rev < scm_rev # already up-to-date
Chris@245 102
Chris@245 103 logger.debug "Fetching changesets for repository #{url}" if logger
Chris@245 104 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
Chris@245 105 transaction do
Chris@245 106 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
Chris@245 107 cs = Changeset.create(:repository => self,
Chris@245 108 :revision => re.revision,
Chris@245 109 :scmid => re.scmid,
Chris@245 110 :committer => re.author,
Chris@245 111 :committed_on => re.time,
Chris@245 112 :comments => re.message)
Chris@245 113 re.paths.each { |e| cs.create_change(e) }
Chris@0 114 end
Chris@0 115 end
Chris@0 116 end
Chris@245 117 self
Chris@0 118 end
Chris@0 119 end