##// END OF EJS Templates
scm: git: set revisions ordering inconsistent in existing database (#5357)....
Toshi MARUYAMA -
r5733:098655dbd061
parent child
Show More
@@ -1,164 +1,174
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
4 #
4 #
5 # This program is free software; you can redistribute it and/or
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
8 # of the License, or (at your option) any later version.
9 #
9 #
10 # This program is distributed in the hope that it will be useful,
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
13 # GNU General Public License for more details.
14 #
14 #
15 # You should have received a copy of the GNU General Public License
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
18
19 require 'redmine/scm/adapters/git_adapter'
19 require 'redmine/scm/adapters/git_adapter'
20
20
21 class Repository::Git < Repository
21 class Repository::Git < Repository
22 attr_protected :root_url
22 attr_protected :root_url
23 validates_presence_of :url
23 validates_presence_of :url
24
24
25 def self.human_attribute_name(attribute_key_name)
25 def self.human_attribute_name(attribute_key_name)
26 attr_name = attribute_key_name
26 attr_name = attribute_key_name
27 if attr_name == "url"
27 if attr_name == "url"
28 attr_name = "path_to_repository"
28 attr_name = "path_to_repository"
29 end
29 end
30 super(attr_name)
30 super(attr_name)
31 end
31 end
32
32
33 def self.scm_adapter_class
33 def self.scm_adapter_class
34 Redmine::Scm::Adapters::GitAdapter
34 Redmine::Scm::Adapters::GitAdapter
35 end
35 end
36
36
37 def self.scm_name
37 def self.scm_name
38 'Git'
38 'Git'
39 end
39 end
40
40
41 def report_last_commit
41 def report_last_commit
42 extra_report_last_commit
42 extra_report_last_commit
43 end
43 end
44
44
45 def extra_report_last_commit
45 def extra_report_last_commit
46 return false if extra_info.nil?
46 return false if extra_info.nil?
47 v = extra_info["extra_report_last_commit"]
47 v = extra_info["extra_report_last_commit"]
48 return false if v.nil?
48 return false if v.nil?
49 v.to_s != '0'
49 v.to_s != '0'
50 end
50 end
51
51
52 def supports_directory_revisions?
52 def supports_directory_revisions?
53 true
53 true
54 end
54 end
55
55
56 def repo_log_encoding
56 def repo_log_encoding
57 'UTF-8'
57 'UTF-8'
58 end
58 end
59
59
60 # Returns the identifier for the given git changeset
60 # Returns the identifier for the given git changeset
61 def self.changeset_identifier(changeset)
61 def self.changeset_identifier(changeset)
62 changeset.scmid
62 changeset.scmid
63 end
63 end
64
64
65 # Returns the readable identifier for the given git changeset
65 # Returns the readable identifier for the given git changeset
66 def self.format_changeset_identifier(changeset)
66 def self.format_changeset_identifier(changeset)
67 changeset.revision[0, 8]
67 changeset.revision[0, 8]
68 end
68 end
69
69
70 def branches
70 def branches
71 scm.branches
71 scm.branches
72 end
72 end
73
73
74 def tags
74 def tags
75 scm.tags
75 scm.tags
76 end
76 end
77
77
78 def find_changeset_by_name(name)
78 def find_changeset_by_name(name)
79 return nil if name.nil? || name.empty?
79 return nil if name.nil? || name.empty?
80 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
80 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
81 return e if e
81 return e if e
82 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
82 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
83 end
83 end
84
84
85 def entries(path=nil, identifier=nil)
85 def entries(path=nil, identifier=nil)
86 scm.entries(path,
86 scm.entries(path,
87 identifier,
87 identifier,
88 options = {:report_last_commit => extra_report_last_commit})
88 options = {:report_last_commit => extra_report_last_commit})
89 end
89 end
90
90
91 # In Git and Mercurial, revisions are not in date order.
91 # In Git and Mercurial, revisions are not in date order.
92 # Mercurial fixed issues.
92 # Mercurial fixed issues.
93 # * Redmine Takes Too Long On Large Mercurial Repository
93 # * Redmine Takes Too Long On Large Mercurial Repository
94 # http://www.redmine.org/issues/3449
94 # http://www.redmine.org/issues/3449
95 # * Sorting for changesets might go wrong on Mercurial repos
95 # * Sorting for changesets might go wrong on Mercurial repos
96 # http://www.redmine.org/issues/3567
96 # http://www.redmine.org/issues/3567
97 # Database revision column is text, so Redmine can not sort by revision.
97 # Database revision column is text, so Redmine can not sort by revision.
98 # Mercurial has revision number, and revision number guarantees revision order.
98 # Mercurial has revision number, and revision number guarantees revision order.
99 # Mercurial adapter uses "hg log -r 0:tip --limit 10"
99 # Mercurial adapter uses "hg log -r 0:tip --limit 10"
100 # to get limited revisions from old to new.
100 # to get limited revisions from old to new.
101 # And Mercurial model stored revisions ordered by database id in database.
101 # And Mercurial model stored revisions ordered by database id in database.
102 # So, Mercurial can use correct order revisions.
102 # So, Mercurial can use correct order revisions.
103 #
103 #
104 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
104 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
105 #
105 #
106 # The repository can still be fully reloaded by calling #clear_changesets
106 # The repository can still be fully reloaded by calling #clear_changesets
107 # before fetching changesets (eg. for offline resync)
107 # before fetching changesets (eg. for offline resync)
108 def fetch_changesets
108 def fetch_changesets
109 scm_brs = branches
109 scm_brs = branches
110 return if scm_brs.nil? || scm_brs.empty?
110 return if scm_brs.nil? || scm_brs.empty?
111 h = extra_info || {}
111 h = extra_info || {}
112 h["branches"] ||= {}
112 h["branches"] ||= {}
113 h["db_consistent"] ||= {}
114 if changesets.count == 0
115 h["db_consistent"]["ordering"] = 1
116 merge_extra_info(h)
117 self.save
118 elsif ! h["db_consistent"].has_key?("ordering")
119 h["db_consistent"]["ordering"] = 0
120 merge_extra_info(h)
121 self.save
122 end
113 scm_brs.each do |br|
123 scm_brs.each do |br|
114 from_scmid = nil
124 from_scmid = nil
115 from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
125 from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
116 h["branches"][br] ||= {}
126 h["branches"][br] ||= {}
117 scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
127 scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
118 db_rev = find_changeset_by_name(rev.revision)
128 db_rev = find_changeset_by_name(rev.revision)
119 transaction do
129 transaction do
120 if db_rev.nil?
130 if db_rev.nil?
121 save_revision(rev)
131 save_revision(rev)
122 end
132 end
123 h["branches"][br]["last_scmid"] = rev.scmid
133 h["branches"][br]["last_scmid"] = rev.scmid
124 merge_extra_info(h)
134 merge_extra_info(h)
125 self.save
135 self.save
126 end
136 end
127 end
137 end
128 end
138 end
129 end
139 end
130
140
131 def save_revision(rev)
141 def save_revision(rev)
132 changeset = Changeset.new(
142 changeset = Changeset.new(
133 :repository => self,
143 :repository => self,
134 :revision => rev.identifier,
144 :revision => rev.identifier,
135 :scmid => rev.scmid,
145 :scmid => rev.scmid,
136 :committer => rev.author,
146 :committer => rev.author,
137 :committed_on => rev.time,
147 :committed_on => rev.time,
138 :comments => rev.message
148 :comments => rev.message
139 )
149 )
140 if changeset.save
150 if changeset.save
141 rev.paths.each do |file|
151 rev.paths.each do |file|
142 Change.create(
152 Change.create(
143 :changeset => changeset,
153 :changeset => changeset,
144 :action => file[:action],
154 :action => file[:action],
145 :path => file[:path])
155 :path => file[:path])
146 end
156 end
147 end
157 end
148 end
158 end
149 private :save_revision
159 private :save_revision
150
160
151 def latest_changesets(path,rev,limit=10)
161 def latest_changesets(path,rev,limit=10)
152 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
162 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
153 return [] if revisions.nil? || revisions.empty?
163 return [] if revisions.nil? || revisions.empty?
154
164
155 changesets.find(
165 changesets.find(
156 :all,
166 :all,
157 :conditions => [
167 :conditions => [
158 "scmid IN (?)",
168 "scmid IN (?)",
159 revisions.map!{|c| c.scmid}
169 revisions.map!{|c| c.scmid}
160 ],
170 ],
161 :order => 'committed_on DESC'
171 :order => 'committed_on DESC'
162 )
172 )
163 end
173 end
164 end
174 end
General Comments 0
You need to be logged in to leave comments. Login now