To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / repository / mercurial.rb @ 912:5e80956cc792

History | View | Annotate | Download (5.48 KB)

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