##// END OF EJS Templates
scm: git: catch exception in model default_branch() (#8458, #6713)....
Toshi MARUYAMA -
r6100:ed37d8ed91c9
parent child
Show More
@@ -1,180 +1,183
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 default_branch
78 def default_branch
79 scm.default_branch
79 scm.default_branch
80 rescue Exception => e
81 logger.error "git: error during get default branch: #{e.message}"
82 nil
80 end
83 end
81
84
82 def find_changeset_by_name(name)
85 def find_changeset_by_name(name)
83 return nil if name.nil? || name.empty?
86 return nil if name.nil? || name.empty?
84 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
87 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
85 return e if e
88 return e if e
86 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
89 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
87 end
90 end
88
91
89 def entries(path=nil, identifier=nil)
92 def entries(path=nil, identifier=nil)
90 scm.entries(path,
93 scm.entries(path,
91 identifier,
94 identifier,
92 options = {:report_last_commit => extra_report_last_commit})
95 options = {:report_last_commit => extra_report_last_commit})
93 end
96 end
94
97
95 # In Git and Mercurial, revisions are not in date order.
98 # In Git and Mercurial, revisions are not in date order.
96 # Redmine Mercurial fixed issues.
99 # Redmine Mercurial fixed issues.
97 # * Redmine Takes Too Long On Large Mercurial Repository
100 # * Redmine Takes Too Long On Large Mercurial Repository
98 # http://www.redmine.org/issues/3449
101 # http://www.redmine.org/issues/3449
99 # * Sorting for changesets might go wrong on Mercurial repos
102 # * Sorting for changesets might go wrong on Mercurial repos
100 # http://www.redmine.org/issues/3567
103 # http://www.redmine.org/issues/3567
101 #
104 #
102 # Database revision column is text, so Redmine can not sort by revision.
105 # Database revision column is text, so Redmine can not sort by revision.
103 # Mercurial has revision number, and revision number guarantees revision order.
106 # Mercurial has revision number, and revision number guarantees revision order.
104 # Redmine Mercurial model stored revisions ordered by database id to database.
107 # Redmine Mercurial model stored revisions ordered by database id to database.
105 # So, Redmine Mercurial model can use correct ordering revisions.
108 # So, Redmine Mercurial model can use correct ordering revisions.
106 #
109 #
107 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
110 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
108 # to get limited revisions from old to new.
111 # to get limited revisions from old to new.
109 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
112 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
110 #
113 #
111 # The repository can still be fully reloaded by calling #clear_changesets
114 # The repository can still be fully reloaded by calling #clear_changesets
112 # before fetching changesets (eg. for offline resync)
115 # before fetching changesets (eg. for offline resync)
113 def fetch_changesets
116 def fetch_changesets
114 scm_brs = branches
117 scm_brs = branches
115 return if scm_brs.nil? || scm_brs.empty?
118 return if scm_brs.nil? || scm_brs.empty?
116 h1 = extra_info || {}
119 h1 = extra_info || {}
117 h = h1.dup
120 h = h1.dup
118 h["branches"] ||= {}
121 h["branches"] ||= {}
119 h["db_consistent"] ||= {}
122 h["db_consistent"] ||= {}
120 if changesets.count == 0
123 if changesets.count == 0
121 h["db_consistent"]["ordering"] = 1
124 h["db_consistent"]["ordering"] = 1
122 merge_extra_info(h)
125 merge_extra_info(h)
123 self.save
126 self.save
124 elsif ! h["db_consistent"].has_key?("ordering")
127 elsif ! h["db_consistent"].has_key?("ordering")
125 h["db_consistent"]["ordering"] = 0
128 h["db_consistent"]["ordering"] = 0
126 merge_extra_info(h)
129 merge_extra_info(h)
127 self.save
130 self.save
128 end
131 end
129 scm_brs.each do |br|
132 scm_brs.each do |br|
130 from_scmid = nil
133 from_scmid = nil
131 from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
134 from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
132 h["branches"][br] ||= {}
135 h["branches"][br] ||= {}
133 scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
136 scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
134 db_rev = find_changeset_by_name(rev.revision)
137 db_rev = find_changeset_by_name(rev.revision)
135 transaction do
138 transaction do
136 if db_rev.nil?
139 if db_rev.nil?
137 save_revision(rev)
140 save_revision(rev)
138 end
141 end
139 h["branches"][br]["last_scmid"] = rev.scmid
142 h["branches"][br]["last_scmid"] = rev.scmid
140 merge_extra_info(h)
143 merge_extra_info(h)
141 self.save
144 self.save
142 end
145 end
143 end
146 end
144 end
147 end
145 end
148 end
146
149
147 def save_revision(rev)
150 def save_revision(rev)
148 changeset = Changeset.new(
151 changeset = Changeset.new(
149 :repository => self,
152 :repository => self,
150 :revision => rev.identifier,
153 :revision => rev.identifier,
151 :scmid => rev.scmid,
154 :scmid => rev.scmid,
152 :committer => rev.author,
155 :committer => rev.author,
153 :committed_on => rev.time,
156 :committed_on => rev.time,
154 :comments => rev.message
157 :comments => rev.message
155 )
158 )
156 if changeset.save
159 if changeset.save
157 rev.paths.each do |file|
160 rev.paths.each do |file|
158 Change.create(
161 Change.create(
159 :changeset => changeset,
162 :changeset => changeset,
160 :action => file[:action],
163 :action => file[:action],
161 :path => file[:path])
164 :path => file[:path])
162 end
165 end
163 end
166 end
164 end
167 end
165 private :save_revision
168 private :save_revision
166
169
167 def latest_changesets(path,rev,limit=10)
170 def latest_changesets(path,rev,limit=10)
168 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
171 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
169 return [] if revisions.nil? || revisions.empty?
172 return [] if revisions.nil? || revisions.empty?
170
173
171 changesets.find(
174 changesets.find(
172 :all,
175 :all,
173 :conditions => [
176 :conditions => [
174 "scmid IN (?)",
177 "scmid IN (?)",
175 revisions.map!{|c| c.scmid}
178 revisions.map!{|c| c.scmid}
176 ],
179 ],
177 :order => 'committed_on DESC'
180 :order => 'committed_on DESC'
178 )
181 )
179 end
182 end
180 end
183 end
General Comments 0
You need to be logged in to leave comments. Login now