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 @ 1298:4f746d8966dd

History | View | Annotate | Download (5.31 KB)

1 441:cbce1fd3b1b7 Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  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 1136:51d7f3e06556 chris
require_dependency 'redmine/scm/adapters/mercurial_adapter'
19 0:513646585e45 Chris
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 1115:433d4f72a19b Chris
  def self.human_attribute_name(attribute_key_name, *args)
33
    attr_name = attribute_key_name.to_s
34 441:cbce1fd3b1b7 Chris
    if attr_name == "url"
35
      attr_name = "path_to_repository"
36
    end
37 1115:433d4f72a19b Chris
    super(attr_name, *args)
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 1115:433d4f72a19b Chris
    return nil if name.blank?
77
    s = name.to_s
78
    if /[^\d]/ =~ s or s.size > 8
79
      cs = changesets.where(:scmid => s).first
80 119:8661b858af72 Chris
    else
81 1115:433d4f72a19b Chris
      cs = changesets.where(:revision => s).first
82 119:8661b858af72 Chris
    end
83 1115:433d4f72a19b Chris
    return cs if cs
84
    changesets.where('scmid LIKE ?', "#{s}%").first
85 119:8661b858af72 Chris
  end
86
87
  # Returns the latest changesets for +path+; sorted by revision number
88 441:cbce1fd3b1b7 Chris
  #
89
  # Because :order => 'id DESC' is defined at 'has_many',
90
  # there is no need to set 'order'.
91
  # But, MySQL test fails.
92
  # Sqlite3 and PostgreSQL pass.
93
  # Is this MySQL bug?
94 119:8661b858af72 Chris
  def latest_changesets(path, rev, limit=10)
95 1295:622f24f53b42 Chris
    changesets.
96
      includes(:user).
97
      where(latest_changesets_cond(path, rev, limit)).
98
      limit(limit).
99
      order("#{Changeset.table_name}.id DESC").
100
      all
101 441:cbce1fd3b1b7 Chris
  end
102
103
  def latest_changesets_cond(path, rev, limit)
104
    cond, args = [], []
105
    if scm.branchmap.member? rev
106
      # Mercurial named branch is *stable* in each revision.
107
      # So, named branch can be stored in database.
108
      # Mercurial provides *bookmark* which is equivalent with git branch.
109
      # But, bookmark is not implemented.
110
      cond << "#{Changeset.table_name}.scmid IN (?)"
111
      # Revisions in root directory and sub directory are not equal.
112
      # So, in order to get correct limit, we need to get all revisions.
113
      # But, it is very heavy.
114
      # Mercurial does not treat direcotry.
115
      # So, "hg log DIR" is very heavy.
116
      branch_limit = path.blank? ? limit : ( limit * 5 )
117
      args << scm.nodes_in_branch(rev, :limit => branch_limit)
118
    elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
119
      cond << "#{Changeset.table_name}.id <= ?"
120
      args << last.id
121 119:8661b858af72 Chris
    end
122 441:cbce1fd3b1b7 Chris
    unless path.blank?
123
      cond << "EXISTS (SELECT * FROM #{Change.table_name}
124
                 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
125
                 AND (#{Change.table_name}.path = ?
126
                       OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
127
      args << path.with_leading_slash
128 909:cbb26bc654de Chris
      args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
129 441:cbce1fd3b1b7 Chris
    end
130
    [cond.join(' AND '), *args] unless cond.empty?
131 119:8661b858af72 Chris
  end
132 441:cbce1fd3b1b7 Chris
  private :latest_changesets_cond
133 119:8661b858af72 Chris
134 0:513646585e45 Chris
  def fetch_changesets
135 507:0c939c159af4 Chris
    return if scm.info.nil?
136 245:051f544170fe Chris
    scm_rev = scm.info.lastrev.revision.to_i
137 909:cbb26bc654de Chris
    db_rev  = latest_changeset ? latest_changeset.revision.to_i : -1
138 245:051f544170fe Chris
    return unless db_rev < scm_rev  # already up-to-date
139
140
    logger.debug "Fetching changesets for repository #{url}" if logger
141
    (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
142 1115:433d4f72a19b Chris
      scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
143
        transaction do
144
          parents = (re.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
145 909:cbb26bc654de Chris
          cs = Changeset.create(:repository   => self,
146
                                :revision     => re.revision,
147
                                :scmid        => re.scmid,
148
                                :committer    => re.author,
149 245:051f544170fe Chris
                                :committed_on => re.time,
150 1115:433d4f72a19b Chris
                                :comments     => re.message,
151
                                :parents      => parents)
152
          unless cs.new_record?
153
            re.paths.each { |e| cs.create_change(e) }
154 909:cbb26bc654de Chris
          end
155 0:513646585e45 Chris
        end
156
      end
157
    end
158
  end
159
end