##// END OF EJS Templates
Mercurial adapter:...
Jean-Philippe Lang -
r1240:01fdaf597742
parent child
Show More
@@ -1,81 +1,87
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 attr_protected :root_url
22 22 validates_presence_of :url
23 23
24 24 def scm_adapter
25 25 Redmine::Scm::Adapters::MercurialAdapter
26 26 end
27 27
28 28 def self.scm_name
29 29 'Mercurial'
30 30 end
31 31
32 32 def entries(path=nil, identifier=nil)
33 33 entries=scm.entries(path, identifier)
34 34 if entries
35 35 entries.each do |entry|
36 36 next unless entry.is_file?
37 37 # Search the DB for the entry's last change
38 38 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
39 39 if change
40 40 entry.lastrev.identifier = change.changeset.revision
41 41 entry.lastrev.name = change.changeset.revision
42 42 entry.lastrev.author = change.changeset.committer
43 43 entry.lastrev.revision = change.revision
44 44 end
45 45 end
46 46 end
47 47 entries
48 48 end
49 49
50 50 def fetch_changesets
51 51 scm_info = scm.info
52 52 if scm_info
53 53 # latest revision found in database
54 db_revision = latest_changeset ? latest_changeset.revision : nil
54 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
55 55 # latest revision in the repository
56 56 scm_revision = scm_info.lastrev.identifier.to_i
57
58 unless changesets.find_by_revision(scm_revision)
59 revisions = scm.revisions('', db_revision, nil)
60 transaction do
61 revisions.reverse_each do |revision|
62 changeset = Changeset.create(:repository => self,
63 :revision => revision.identifier,
64 :scmid => revision.scmid,
65 :committer => revision.author,
66 :committed_on => revision.time,
67 :comments => revision.message)
68
69 revision.paths.each do |change|
70 Change.create(:changeset => changeset,
71 :action => change[:action],
72 :path => change[:path],
73 :from_path => change[:from_path],
74 :from_revision => change[:from_revision])
57 if db_revision < scm_revision
58 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
59 identifier_from = db_revision + 1
60 while (identifier_from <= scm_revision)
61 # loads changesets by batches of 100
62 identifier_to = [identifier_from + 99, scm_revision].min
63 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
64 transaction do
65 revisions.each do |revision|
66 changeset = Changeset.create(:repository => self,
67 :revision => revision.identifier,
68 :scmid => revision.scmid,
69 :committer => revision.author,
70 :committed_on => revision.time,
71 :comments => revision.message)
72
73 revision.paths.each do |change|
74 Change.create(:changeset => changeset,
75 :action => change[:action],
76 :path => change[:path],
77 :from_path => change[:from_path],
78 :from_revision => change[:from_revision])
79 end
75 80 end
76 end
81 end unless revisions.nil?
82 identifier_from = identifier_to + 1
77 83 end
78 84 end
79 85 end
80 86 end
81 87 end
@@ -1,171 +1,175
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/abstract_adapter'
19 19
20 20 module Redmine
21 21 module Scm
22 22 module Adapters
23 23 class MercurialAdapter < AbstractAdapter
24 24
25 25 # Mercurial executable name
26 26 HG_BIN = "hg"
27 27
28 28 def info
29 29 cmd = "#{HG_BIN} -R #{target('')} root"
30 30 root_url = nil
31 31 shellout(cmd) do |io|
32 32 root_url = io.gets
33 33 end
34 34 return nil if $? && $?.exitstatus != 0
35 35 info = Info.new({:root_url => root_url.chomp,
36 36 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
37 37 })
38 38 info
39 39 rescue CommandFailed
40 40 return nil
41 41 end
42 42
43 43 def entries(path=nil, identifier=nil)
44 44 path ||= ''
45 45 entries = Entries.new
46 46 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate"
47 47 cmd << " -r #{identifier.to_i}" if identifier
48 48 cmd << " " + shell_quote('glob:**')
49 49 shellout(cmd) do |io|
50 50 io.each_line do |line|
51 51 e = line.chomp.split(%r{[\/\\]})
52 52 entries << Entry.new({:name => e.first,
53 53 :path => (path.empty? ? e.first : "#{path}/#{e.first}"),
54 54 :kind => (e.size > 1 ? 'dir' : 'file'),
55 55 :lastrev => Revision.new
56 56 }) unless entries.detect{|entry| entry.name == e.first}
57 57 end
58 58 end
59 59 return nil if $? && $?.exitstatus != 0
60 60 entries.sort_by_name
61 61 end
62 62
63 63 def entry(path=nil, identifier=nil)
64 64 path ||= ''
65 65 search_path = path.split('/')[0..-2].join('/')
66 66 entry_name = path.split('/').last
67 67 e = entries(search_path, identifier)
68 68 e ? e.detect{|entry| entry.name == entry_name} : nil
69 69 end
70 70
71 71 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
72 72 revisions = Revisions.new
73 73 cmd = "#{HG_BIN} -v -R #{target('')} log"
74 cmd << " -r #{identifier_from.to_i}:" if identifier_from
74 if identifier_from && identifier_to
75 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
76 elsif identifier_from
77 cmd << " -r #{identifier_from.to_i}:"
78 end
75 79 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
76 80 shellout(cmd) do |io|
77 81 changeset = {}
78 82 parsing_descr = false
79 83 line_feeds = 0
80 84
81 85 io.each_line do |line|
82 86 if line =~ /^(\w+):\s*(.*)$/
83 87 key = $1
84 88 value = $2
85 89 if parsing_descr && line_feeds > 1
86 90 parsing_descr = false
87 91 revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
88 92 :scmid => changeset[:changeset].split(':').last,
89 93 :author => changeset[:user],
90 94 :time => Time.parse(changeset[:date]),
91 95 :message => changeset[:description],
92 96 :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
93 97 })
94 98 changeset = {}
95 99 end
96 100 if !parsing_descr
97 101 changeset.store key.to_sym, value
98 102 if $1 == "description"
99 103 parsing_descr = true
100 104 line_feeds = 0
101 105 next
102 106 end
103 107 end
104 108 end
105 109 if parsing_descr
106 110 changeset[:description] << line
107 111 line_feeds += 1 if line.chomp.empty?
108 112 end
109 113 end
110 114 revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
111 115 :scmid => changeset[:changeset].split(':').last,
112 116 :author => changeset[:user],
113 117 :time => Time.parse(changeset[:date]),
114 118 :message => changeset[:description],
115 119 :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
116 120 })
117 121 end
118 122 return nil if $? && $?.exitstatus != 0
119 123 revisions
120 124 end
121 125
122 126 def diff(path, identifier_from, identifier_to=nil, type="inline")
123 127 path ||= ''
124 128 if identifier_to
125 129 identifier_to = identifier_to.to_i
126 130 else
127 131 identifier_to = identifier_from.to_i - 1
128 132 end
129 133 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
130 134 cmd << " -I #{target(path)}" unless path.empty?
131 135 diff = []
132 136 shellout(cmd) do |io|
133 137 io.each_line do |line|
134 138 diff << line
135 139 end
136 140 end
137 141 return nil if $? && $?.exitstatus != 0
138 142 DiffTableList.new diff, type
139 143 end
140 144
141 145 def cat(path, identifier=nil)
142 146 cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}"
143 147 cat = nil
144 148 shellout(cmd) do |io|
145 149 io.binmode
146 150 cat = io.read
147 151 end
148 152 return nil if $? && $?.exitstatus != 0
149 153 cat
150 154 end
151 155
152 156 def annotate(path, identifier=nil)
153 157 path ||= ''
154 158 cmd = "#{HG_BIN} -R #{target('')}"
155 159 cmd << " annotate -n -u"
156 160 cmd << " -r #{identifier.to_i}" if identifier
157 161 cmd << " #{target(path)}"
158 162 blame = Annotate.new
159 163 shellout(cmd) do |io|
160 164 io.each_line do |line|
161 165 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
162 166 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
163 167 end
164 168 end
165 169 return nil if $? && $?.exitstatus != 0
166 170 blame
167 171 end
168 172 end
169 173 end
170 174 end
171 175 end
General Comments 0
You need to be logged in to leave comments. Login now