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