@@ -1,171 +1,171 | |||
|
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 |
cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate |
|
|
46 | cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate" | |
|
47 | 47 | cmd << " -r #{identifier.to_i}" if identifier |
|
48 |
cmd << " |
|
|
48 | cmd << " glob:**" | |
|
49 | 49 | shellout(cmd) do |io| |
|
50 | 50 | io.each_line do |line| |
|
51 |
e = line.chomp.split( |
|
|
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 | 74 | cmd << " -r #{identifier_from.to_i}:" if identifier_from |
|
75 | 75 | cmd << " --limit #{options[:limit].to_i}" if options[:limit] |
|
76 | 76 | shellout(cmd) do |io| |
|
77 | 77 | changeset = {} |
|
78 | 78 | parsing_descr = false |
|
79 | 79 | line_feeds = 0 |
|
80 | 80 | |
|
81 | 81 | io.each_line do |line| |
|
82 | 82 | if line =~ /^(\w+):\s*(.*)$/ |
|
83 | 83 | key = $1 |
|
84 | 84 | value = $2 |
|
85 | 85 | if parsing_descr && line_feeds > 1 |
|
86 | 86 | parsing_descr = false |
|
87 | 87 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, |
|
88 | 88 | :scmid => changeset[:changeset].split(':').last, |
|
89 | 89 | :author => changeset[:user], |
|
90 | 90 | :time => Time.parse(changeset[:date]), |
|
91 | 91 | :message => changeset[:description], |
|
92 | 92 | :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}} |
|
93 | 93 | }) |
|
94 | 94 | changeset = {} |
|
95 | 95 | end |
|
96 | 96 | if !parsing_descr |
|
97 | 97 | changeset.store key.to_sym, value |
|
98 | 98 | if $1 == "description" |
|
99 | 99 | parsing_descr = true |
|
100 | 100 | line_feeds = 0 |
|
101 | 101 | next |
|
102 | 102 | end |
|
103 | 103 | end |
|
104 | 104 | end |
|
105 | 105 | if parsing_descr |
|
106 | 106 | changeset[:description] << line |
|
107 | 107 | line_feeds += 1 if line.chomp.empty? |
|
108 | 108 | end |
|
109 | 109 | end |
|
110 | 110 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, |
|
111 | 111 | :scmid => changeset[:changeset].split(':').last, |
|
112 | 112 | :author => changeset[:user], |
|
113 | 113 | :time => Time.parse(changeset[:date]), |
|
114 | 114 | :message => changeset[:description], |
|
115 | 115 | :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}} |
|
116 | 116 | }) |
|
117 | 117 | end |
|
118 | 118 | return nil if $? && $?.exitstatus != 0 |
|
119 | 119 | revisions |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def diff(path, identifier_from, identifier_to=nil, type="inline") |
|
123 | 123 | path ||= '' |
|
124 | 124 | if identifier_to |
|
125 | 125 | identifier_to = identifier_to.to_i |
|
126 | 126 | else |
|
127 | 127 | identifier_to = identifier_from.to_i - 1 |
|
128 | 128 | end |
|
129 | 129 | cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates" |
|
130 | 130 | cmd << " -I #{target(path)}" unless path.empty? |
|
131 | 131 | diff = [] |
|
132 | 132 | shellout(cmd) do |io| |
|
133 | 133 | io.each_line do |line| |
|
134 | 134 | diff << line |
|
135 | 135 | end |
|
136 | 136 | end |
|
137 | 137 | return nil if $? && $?.exitstatus != 0 |
|
138 | 138 | DiffTableList.new diff, type |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | def cat(path, identifier=nil) |
|
142 | 142 | cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}" |
|
143 | 143 | cat = nil |
|
144 | 144 | shellout(cmd) do |io| |
|
145 | 145 | io.binmode |
|
146 | 146 | cat = io.read |
|
147 | 147 | end |
|
148 | 148 | return nil if $? && $?.exitstatus != 0 |
|
149 | 149 | cat |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | def annotate(path, identifier=nil) |
|
153 | 153 | path ||= '' |
|
154 | 154 | cmd = "#{HG_BIN} -R #{target('')}" |
|
155 | 155 | cmd << " annotate -n -u" |
|
156 | 156 | cmd << " -r #{identifier.to_i}" if identifier |
|
157 | 157 | cmd << " #{target(path)}" |
|
158 | 158 | blame = Annotate.new |
|
159 | 159 | shellout(cmd) do |io| |
|
160 | 160 | io.each_line do |line| |
|
161 | 161 | next unless line =~ %r{^([^:]+)\s(\d+):(.*)$} |
|
162 | 162 | blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip)) |
|
163 | 163 | end |
|
164 | 164 | end |
|
165 | 165 | return nil if $? && $?.exitstatus != 0 |
|
166 | 166 | blame |
|
167 | 167 | end |
|
168 | 168 | end |
|
169 | 169 | end |
|
170 | 170 | end |
|
171 | 171 | end |
General Comments 0
You need to be logged in to leave comments.
Login now