##// END OF EJS Templates
Reduces memory usage when importing large git repositories (#1482)....
Jean-Philippe Lang -
r1585:00593f2f345f
parent child
Show More
@@ -1,70 +1,68
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'redmine/scm/adapters/git_adapter'
18 require 'redmine/scm/adapters/git_adapter'
19
19
20 class Repository::Git < Repository
20 class Repository::Git < Repository
21 attr_protected :root_url
21 attr_protected :root_url
22 validates_presence_of :url
22 validates_presence_of :url
23
23
24 def scm_adapter
24 def scm_adapter
25 Redmine::Scm::Adapters::GitAdapter
25 Redmine::Scm::Adapters::GitAdapter
26 end
26 end
27
27
28 def self.scm_name
28 def self.scm_name
29 'Git'
29 'Git'
30 end
30 end
31
31
32 def changesets_for_path(path)
32 def changesets_for_path(path)
33 Change.find(:all, :include => :changeset,
33 Change.find(:all, :include => :changeset,
34 :conditions => ["repository_id = ? AND path = ?", id, path],
34 :conditions => ["repository_id = ? AND path = ?", id, path],
35 :order => "committed_on DESC, #{Changeset.table_name}.revision DESC").collect(&:changeset)
35 :order => "committed_on DESC, #{Changeset.table_name}.revision DESC").collect(&:changeset)
36 end
36 end
37
37
38 def fetch_changesets
38 def fetch_changesets
39 scm_info = scm.info
39 scm_info = scm.info
40 if scm_info
40 if scm_info
41 # latest revision found in database
41 # latest revision found in database
42 db_revision = latest_changeset ? latest_changeset.revision : nil
42 db_revision = latest_changeset ? latest_changeset.revision : nil
43 # latest revision in the repository
43 # latest revision in the repository
44 scm_revision = scm_info.lastrev.scmid
44 scm_revision = scm_info.lastrev.scmid
45
45
46 unless changesets.find_by_scmid(scm_revision)
46 unless changesets.find_by_scmid(scm_revision)
47
47 scm.revisions('', db_revision, nil, :reverse => true) do |revision|
48 revisions = scm.revisions('', db_revision, nil)
48 transaction do
49 transaction do
50 revisions.reverse_each do |revision|
51 changeset = Changeset.create(:repository => self,
49 changeset = Changeset.create(:repository => self,
52 :revision => revision.identifier,
50 :revision => revision.identifier,
53 :scmid => revision.scmid,
51 :scmid => revision.scmid,
54 :committer => revision.author,
52 :committer => revision.author,
55 :committed_on => revision.time,
53 :committed_on => revision.time,
56 :comments => revision.message)
54 :comments => revision.message)
57
55
58 revision.paths.each do |change|
56 revision.paths.each do |change|
59 Change.create(:changeset => changeset,
57 Change.create(:changeset => changeset,
60 :action => change[:action],
58 :action => change[:action],
61 :path => change[:path],
59 :path => change[:path],
62 :from_path => change[:from_path],
60 :from_path => change[:from_path],
63 :from_revision => change[:from_revision])
61 :from_revision => change[:from_revision])
64 end
62 end
65 end
63 end
66 end
64 end
67 end
65 end
68 end
66 end
69 end
67 end
70 end
68 end
@@ -1,260 +1,271
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'redmine/scm/adapters/abstract_adapter'
18 require 'redmine/scm/adapters/abstract_adapter'
19
19
20 module Redmine
20 module Redmine
21 module Scm
21 module Scm
22 module Adapters
22 module Adapters
23 class GitAdapter < AbstractAdapter
23 class GitAdapter < AbstractAdapter
24
24
25 # Git executable name
25 # Git executable name
26 GIT_BIN = "git"
26 GIT_BIN = "git"
27
27
28 # Get the revision of a particuliar file
28 # Get the revision of a particuliar file
29 def get_rev (rev,path)
29 def get_rev (rev,path)
30
30
31 if rev != 'latest' && !rev.nil?
31 if rev != 'latest' && !rev.nil?
32 cmd="#{GIT_BIN} --git-dir #{target('')} show #{shell_quote rev} -- #{shell_quote path}"
32 cmd="#{GIT_BIN} --git-dir #{target('')} show #{shell_quote rev} -- #{shell_quote path}"
33 else
33 else
34 branch = shellout("#{GIT_BIN} --git-dir #{target('')} branch") { |io| io.grep(/\*/)[0].strip.match(/\* (.*)/)[1] }
34 branch = shellout("#{GIT_BIN} --git-dir #{target('')} branch") { |io| io.grep(/\*/)[0].strip.match(/\* (.*)/)[1] }
35 cmd="#{GIT_BIN} --git-dir #{target('')} log -1 #{branch} -- #{shell_quote path}"
35 cmd="#{GIT_BIN} --git-dir #{target('')} log -1 #{branch} -- #{shell_quote path}"
36 end
36 end
37 rev=[]
37 rev=[]
38 i=0
38 i=0
39 shellout(cmd) do |io|
39 shellout(cmd) do |io|
40 files=[]
40 files=[]
41 changeset = {}
41 changeset = {}
42 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
42 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
43
43
44 io.each_line do |line|
44 io.each_line do |line|
45 if line =~ /^commit ([0-9a-f]{40})$/
45 if line =~ /^commit ([0-9a-f]{40})$/
46 key = "commit"
46 key = "commit"
47 value = $1
47 value = $1
48 if (parsing_descr == 1 || parsing_descr == 2)
48 if (parsing_descr == 1 || parsing_descr == 2)
49 parsing_descr = 0
49 parsing_descr = 0
50 rev = Revision.new({:identifier => changeset[:commit],
50 rev = Revision.new({:identifier => changeset[:commit],
51 :scmid => changeset[:commit],
51 :scmid => changeset[:commit],
52 :author => changeset[:author],
52 :author => changeset[:author],
53 :time => Time.parse(changeset[:date]),
53 :time => Time.parse(changeset[:date]),
54 :message => changeset[:description],
54 :message => changeset[:description],
55 :paths => files
55 :paths => files
56 })
56 })
57 changeset = {}
57 changeset = {}
58 files = []
58 files = []
59 end
59 end
60 changeset[:commit] = $1
60 changeset[:commit] = $1
61 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
61 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
62 key = $1
62 key = $1
63 value = $2
63 value = $2
64 if key == "Author"
64 if key == "Author"
65 changeset[:author] = value
65 changeset[:author] = value
66 elsif key == "Date"
66 elsif key == "Date"
67 changeset[:date] = value
67 changeset[:date] = value
68 end
68 end
69 elsif (parsing_descr == 0) && line.chomp.to_s == ""
69 elsif (parsing_descr == 0) && line.chomp.to_s == ""
70 parsing_descr = 1
70 parsing_descr = 1
71 changeset[:description] = ""
71 changeset[:description] = ""
72 elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
72 elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
73 parsing_descr = 2
73 parsing_descr = 2
74 fileaction = $1
74 fileaction = $1
75 filepath = $2
75 filepath = $2
76 files << {:action => fileaction, :path => filepath}
76 files << {:action => fileaction, :path => filepath}
77 elsif (parsing_descr == 1) && line.chomp.to_s == ""
77 elsif (parsing_descr == 1) && line.chomp.to_s == ""
78 parsing_descr = 2
78 parsing_descr = 2
79 elsif (parsing_descr == 1)
79 elsif (parsing_descr == 1)
80 changeset[:description] << line
80 changeset[:description] << line
81 end
81 end
82 end
82 end
83 rev = Revision.new({:identifier => changeset[:commit],
83 rev = Revision.new({:identifier => changeset[:commit],
84 :scmid => changeset[:commit],
84 :scmid => changeset[:commit],
85 :author => changeset[:author],
85 :author => changeset[:author],
86 :time => (changeset[:date] ? Time.parse(changeset[:date]) : nil),
86 :time => (changeset[:date] ? Time.parse(changeset[:date]) : nil),
87 :message => changeset[:description],
87 :message => changeset[:description],
88 :paths => files
88 :paths => files
89 })
89 })
90
90
91 end
91 end
92
92
93 get_rev('latest',path) if rev == []
93 get_rev('latest',path) if rev == []
94
94
95 return nil if $? && $?.exitstatus != 0
95 return nil if $? && $?.exitstatus != 0
96 return rev
96 return rev
97 end
97 end
98
98
99
99
100 def info
100 def info
101 revs = revisions(url,nil,nil,{:limit => 1})
101 revs = revisions(url,nil,nil,{:limit => 1})
102 if revs && revs.any?
102 if revs && revs.any?
103 Info.new(:root_url => url, :lastrev => revs.first)
103 Info.new(:root_url => url, :lastrev => revs.first)
104 else
104 else
105 nil
105 nil
106 end
106 end
107 rescue Errno::ENOENT => e
107 rescue Errno::ENOENT => e
108 return nil
108 return nil
109 end
109 end
110
110
111 def entries(path=nil, identifier=nil)
111 def entries(path=nil, identifier=nil)
112 path ||= ''
112 path ||= ''
113 entries = Entries.new
113 entries = Entries.new
114 cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l "
114 cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l "
115 cmd << shell_quote("HEAD:" + path) if identifier.nil?
115 cmd << shell_quote("HEAD:" + path) if identifier.nil?
116 cmd << shell_quote(identifier + ":" + path) if identifier
116 cmd << shell_quote(identifier + ":" + path) if identifier
117 shellout(cmd) do |io|
117 shellout(cmd) do |io|
118 io.each_line do |line|
118 io.each_line do |line|
119 e = line.chomp.to_s
119 e = line.chomp.to_s
120 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/
120 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/
121 type = $1
121 type = $1
122 sha = $2
122 sha = $2
123 size = $3
123 size = $3
124 name = $4
124 name = $4
125 entries << Entry.new({:name => name,
125 entries << Entry.new({:name => name,
126 :path => (path.empty? ? name : "#{path}/#{name}"),
126 :path => (path.empty? ? name : "#{path}/#{name}"),
127 :kind => ((type == "tree") ? 'dir' : 'file'),
127 :kind => ((type == "tree") ? 'dir' : 'file'),
128 :size => ((type == "tree") ? nil : size),
128 :size => ((type == "tree") ? nil : size),
129 :lastrev => get_rev(identifier,(path.empty? ? name : "#{path}/#{name}"))
129 :lastrev => get_rev(identifier,(path.empty? ? name : "#{path}/#{name}"))
130
130
131 }) unless entries.detect{|entry| entry.name == name}
131 }) unless entries.detect{|entry| entry.name == name}
132 end
132 end
133 end
133 end
134 end
134 end
135 return nil if $? && $?.exitstatus != 0
135 return nil if $? && $?.exitstatus != 0
136 entries.sort_by_name
136 entries.sort_by_name
137 end
137 end
138
138
139 def revisions(path, identifier_from, identifier_to, options={})
139 def revisions(path, identifier_from, identifier_to, options={})
140 revisions = Revisions.new
140 revisions = Revisions.new
141 cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw "
141 cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw "
142 cmd << " --reverse" if options[:reverse]
142 cmd << " -n #{options[:limit].to_i} " if (!options.nil?) && options[:limit]
143 cmd << " -n #{options[:limit].to_i} " if (!options.nil?) && options[:limit]
143 cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from
144 cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from
144 cmd << " #{shell_quote identifier_to} " if identifier_to
145 cmd << " #{shell_quote identifier_to} " if identifier_to
145 #cmd << " HEAD " if !identifier_to
146 shellout(cmd) do |io|
146 shellout(cmd) do |io|
147 files=[]
147 files=[]
148 changeset = {}
148 changeset = {}
149 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
149 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
150 revno = 1
150 revno = 1
151
151
152 io.each_line do |line|
152 io.each_line do |line|
153 if line =~ /^commit ([0-9a-f]{40})$/
153 if line =~ /^commit ([0-9a-f]{40})$/
154 key = "commit"
154 key = "commit"
155 value = $1
155 value = $1
156 if (parsing_descr == 1 || parsing_descr == 2)
156 if (parsing_descr == 1 || parsing_descr == 2)
157 parsing_descr = 0
157 parsing_descr = 0
158 revisions << Revision.new({:identifier => changeset[:commit],
158 revision = Revision.new({:identifier => changeset[:commit],
159 :scmid => changeset[:commit],
159 :scmid => changeset[:commit],
160 :author => changeset[:author],
160 :author => changeset[:author],
161 :time => Time.parse(changeset[:date]),
161 :time => Time.parse(changeset[:date]),
162 :message => changeset[:description],
162 :message => changeset[:description],
163 :paths => files
163 :paths => files
164 })
164 })
165 if block_given?
166 yield revision
167 else
168 revisions << revision
169 end
165 changeset = {}
170 changeset = {}
166 files = []
171 files = []
167 revno = revno + 1
172 revno = revno + 1
168 end
173 end
169 changeset[:commit] = $1
174 changeset[:commit] = $1
170 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
175 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
171 key = $1
176 key = $1
172 value = $2
177 value = $2
173 if key == "Author"
178 if key == "Author"
174 changeset[:author] = value
179 changeset[:author] = value
175 elsif key == "Date"
180 elsif key == "Date"
176 changeset[:date] = value
181 changeset[:date] = value
177 end
182 end
178 elsif (parsing_descr == 0) && line.chomp.to_s == ""
183 elsif (parsing_descr == 0) && line.chomp.to_s == ""
179 parsing_descr = 1
184 parsing_descr = 1
180 changeset[:description] = ""
185 changeset[:description] = ""
181 elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
186 elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
182 parsing_descr = 2
187 parsing_descr = 2
183 fileaction = $1
188 fileaction = $1
184 filepath = $2
189 filepath = $2
185 files << {:action => fileaction, :path => filepath}
190 files << {:action => fileaction, :path => filepath}
186 elsif (parsing_descr == 1) && line.chomp.to_s == ""
191 elsif (parsing_descr == 1) && line.chomp.to_s == ""
187 parsing_descr = 2
192 parsing_descr = 2
188 elsif (parsing_descr == 1)
193 elsif (parsing_descr == 1)
189 changeset[:description] << line[4..-1]
194 changeset[:description] << line[4..-1]
190 end
195 end
191 end
196 end
192
197
193 revisions << Revision.new({:identifier => changeset[:commit],
198 if changeset[:commit]
199 revision = Revision.new({:identifier => changeset[:commit],
194 :scmid => changeset[:commit],
200 :scmid => changeset[:commit],
195 :author => changeset[:author],
201 :author => changeset[:author],
196 :time => Time.parse(changeset[:date]),
202 :time => Time.parse(changeset[:date]),
197 :message => changeset[:description],
203 :message => changeset[:description],
198 :paths => files
204 :paths => files
199 }) if changeset[:commit]
205 })
200
206 if block_given?
207 yield revision
208 else
209 revisions << revision
210 end
211 end
201 end
212 end
202
213
203 return nil if $? && $?.exitstatus != 0
214 return nil if $? && $?.exitstatus != 0
204 revisions
215 revisions
205 end
216 end
206
217
207 def diff(path, identifier_from, identifier_to=nil)
218 def diff(path, identifier_from, identifier_to=nil)
208 path ||= ''
219 path ||= ''
209 if !identifier_to
220 if !identifier_to
210 identifier_to = nil
221 identifier_to = nil
211 end
222 end
212
223
213 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" if identifier_to.nil?
224 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" if identifier_to.nil?
214 cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" if !identifier_to.nil?
225 cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" if !identifier_to.nil?
215 cmd << " -- #{shell_quote path}" unless path.empty?
226 cmd << " -- #{shell_quote path}" unless path.empty?
216 diff = []
227 diff = []
217 shellout(cmd) do |io|
228 shellout(cmd) do |io|
218 io.each_line do |line|
229 io.each_line do |line|
219 diff << line
230 diff << line
220 end
231 end
221 end
232 end
222 return nil if $? && $?.exitstatus != 0
233 return nil if $? && $?.exitstatus != 0
223 diff
234 diff
224 end
235 end
225
236
226 def annotate(path, identifier=nil)
237 def annotate(path, identifier=nil)
227 identifier = 'HEAD' if identifier.blank?
238 identifier = 'HEAD' if identifier.blank?
228 cmd = "#{GIT_BIN} --git-dir #{target('')} blame -l #{shell_quote identifier} -- #{shell_quote path}"
239 cmd = "#{GIT_BIN} --git-dir #{target('')} blame -l #{shell_quote identifier} -- #{shell_quote path}"
229 blame = Annotate.new
240 blame = Annotate.new
230 content = nil
241 content = nil
231 shellout(cmd) { |io| io.binmode; content = io.read }
242 shellout(cmd) { |io| io.binmode; content = io.read }
232 return nil if $? && $?.exitstatus != 0
243 return nil if $? && $?.exitstatus != 0
233 # git annotates binary files
244 # git annotates binary files
234 return nil if content.is_binary_data?
245 return nil if content.is_binary_data?
235 content.split("\n").each do |line|
246 content.split("\n").each do |line|
236 next unless line =~ /([0-9a-f]{39,40})\s\((\w*)[^\)]*\)(.*)/
247 next unless line =~ /([0-9a-f]{39,40})\s\((\w*)[^\)]*\)(.*)/
237 blame.add_line($3.rstrip, Revision.new(:identifier => $1, :author => $2.strip))
248 blame.add_line($3.rstrip, Revision.new(:identifier => $1, :author => $2.strip))
238 end
249 end
239 blame
250 blame
240 end
251 end
241
252
242 def cat(path, identifier=nil)
253 def cat(path, identifier=nil)
243 if identifier.nil?
254 if identifier.nil?
244 identifier = 'HEAD'
255 identifier = 'HEAD'
245 end
256 end
246 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}"
257 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}"
247 cat = nil
258 cat = nil
248 shellout(cmd) do |io|
259 shellout(cmd) do |io|
249 io.binmode
260 io.binmode
250 cat = io.read
261 cat = io.read
251 end
262 end
252 return nil if $? && $?.exitstatus != 0
263 return nil if $? && $?.exitstatus != 0
253 cat
264 cat
254 end
265 end
255 end
266 end
256 end
267 end
257 end
268 end
258
269
259 end
270 end
260
271
General Comments 0
You need to be logged in to leave comments. Login now