@@ -1,131 +1,119 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'redmine/scm/adapters/mercurial_adapter' |
|
19 | 19 | |
|
20 | 20 | class Repository::Mercurial < Repository |
|
21 | 21 | # sort changesets by revision number |
|
22 | 22 | has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id' |
|
23 | 23 | |
|
24 | 24 | attr_protected :root_url |
|
25 | 25 | validates_presence_of :url |
|
26 | 26 | |
|
27 | FETCH_AT_ONCE = 100 # number of changesets to fetch at once | |
|
28 | ||
|
27 | 29 | def self.scm_adapter_class |
|
28 | 30 | Redmine::Scm::Adapters::MercurialAdapter |
|
29 | 31 | end |
|
30 | 32 | |
|
31 | 33 | def self.scm_name |
|
32 | 34 | 'Mercurial' |
|
33 | 35 | end |
|
34 | 36 | |
|
35 | 37 | # Returns the readable identifier for the given mercurial changeset |
|
36 | 38 | def self.format_changeset_identifier(changeset) |
|
37 | 39 | "#{changeset.revision}:#{changeset.scmid}" |
|
38 | 40 | end |
|
39 | 41 | |
|
40 | 42 | # Returns the identifier for the given Mercurial changeset |
|
41 | 43 | def self.changeset_identifier(changeset) |
|
42 | 44 | changeset.scmid |
|
43 | 45 | end |
|
44 | 46 | |
|
45 | 47 | def diff_format_revisions(cs, cs_to, sep=':') |
|
46 | 48 | super(cs, cs_to, ' ') |
|
47 | 49 | end |
|
48 | 50 | |
|
49 | 51 | def entries(path=nil, identifier=nil) |
|
50 | 52 | entries=scm.entries(path, identifier) |
|
51 | 53 | if entries |
|
52 | 54 | entries.each do |entry| |
|
53 | 55 | next unless entry.is_file? |
|
54 | 56 | # Set the filesize unless browsing a specific revision |
|
55 | 57 | if identifier.nil? |
|
56 | 58 | full_path = File.join(root_url, entry.path) |
|
57 | 59 | entry.size = File.stat(full_path).size if File.file?(full_path) |
|
58 | 60 | end |
|
59 | 61 | # Search the DB for the entry's last change |
|
60 | 62 | change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC") |
|
61 | 63 | if change |
|
62 | 64 | entry.lastrev.identifier = change.changeset.revision |
|
63 | 65 | entry.lastrev.name = change.changeset.revision |
|
64 | 66 | entry.lastrev.author = change.changeset.committer |
|
65 | 67 | entry.lastrev.revision = change.revision |
|
66 | 68 | end |
|
67 | 69 | end |
|
68 | 70 | end |
|
69 | 71 | entries |
|
70 | 72 | end |
|
71 | 73 | |
|
72 | 74 | # Finds and returns a revision with a number or the beginning of a hash |
|
73 | 75 | def find_changeset_by_name(name) |
|
74 | 76 | return nil if name.nil? || name.empty? |
|
75 | 77 | if /[^\d]/ =~ name or name.to_s.size > 8 |
|
76 | 78 | e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s]) |
|
77 | 79 | else |
|
78 | 80 | e = changesets.find(:first, :conditions => ['revision = ?', name.to_s]) |
|
79 | 81 | end |
|
80 | 82 | return e if e |
|
81 | 83 | changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch |
|
82 | 84 | end |
|
83 | 85 | |
|
84 | 86 | # Returns the latest changesets for +path+; sorted by revision number |
|
85 | 87 | def latest_changesets(path, rev, limit=10) |
|
86 | 88 | if path.blank? |
|
87 | 89 | changesets.find(:all, :include => :user, :limit => limit) |
|
88 | 90 | else |
|
89 | 91 | changes.find(:all, :include => {:changeset => :user}, |
|
90 | 92 | :conditions => ["path = ?", path.with_leading_slash], |
|
91 | 93 | :order => "#{Changeset.table_name}.id DESC", |
|
92 | 94 | :limit => limit).collect(&:changeset) |
|
93 | 95 | end |
|
94 | 96 | end |
|
95 | 97 | |
|
96 | 98 | def fetch_changesets |
|
97 | scm_info = scm.info | |
|
98 | if scm_info | |
|
99 | # latest revision found in database | |
|
100 | db_revision = latest_changeset ? latest_changeset.revision.to_i : -1 | |
|
101 | # latest revision in the repository | |
|
102 | latest_revision = scm_info.lastrev | |
|
103 | return if latest_revision.nil? | |
|
104 | scm_revision = latest_revision.identifier.to_i | |
|
105 | if db_revision < scm_revision | |
|
106 | logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug? | |
|
107 | identifier_from = db_revision + 1 | |
|
108 | while (identifier_from <= scm_revision) | |
|
109 | # loads changesets by batches of 100 | |
|
110 | identifier_to = [identifier_from + 99, scm_revision].min | |
|
111 | revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true) | |
|
112 | transaction do | |
|
113 | revisions.each do |revision| | |
|
114 | changeset = Changeset.create(:repository => self, | |
|
115 | :revision => revision.revision, | |
|
116 | :scmid => revision.scmid, | |
|
117 | :committer => revision.author, | |
|
118 | :committed_on => revision.time, | |
|
119 | :comments => revision.message) | |
|
99 | scm_rev = scm.info.lastrev.revision.to_i | |
|
100 | db_rev = latest_changeset ? latest_changeset.revision.to_i : -1 | |
|
101 | return unless db_rev < scm_rev # already up-to-date | |
|
120 | 102 | |
|
121 | revision.paths.each do |change| | |
|
122 | changeset.create_change(change) | |
|
123 | end | |
|
124 | end | |
|
125 | end unless revisions.nil? | |
|
126 | identifier_from = identifier_to + 1 | |
|
103 | logger.debug "Fetching changesets for repository #{url}" if logger | |
|
104 | (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| | |
|
105 | transaction do | |
|
106 | scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| | |
|
107 | cs = Changeset.create(:repository => self, | |
|
108 | :revision => re.revision, | |
|
109 | :scmid => re.scmid, | |
|
110 | :committer => re.author, | |
|
111 | :committed_on => re.time, | |
|
112 | :comments => re.message) | |
|
113 | re.paths.each { |e| cs.create_change(e) } | |
|
127 | 114 | end |
|
128 | 115 | end |
|
129 | 116 | end |
|
117 | self | |
|
130 | 118 | end |
|
131 | 119 | end |
General Comments 0
You need to be logged in to leave comments.
Login now