summaryrefslogtreecommitdiffstats
path: root/lib/redmine/scm/adapters/mercurial_adapter.rb
blob: 26bf0114068bd8d25eadd960c1eba5adeb1c0754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# redMine - project management software
# Copyright (C) 2006-2007  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require 'redmine/scm/adapters/abstract_adapter'

module Redmine
  module Scm
    module Adapters    
      class MercurialAdapter < AbstractAdapter
      
        # Mercurial executable name
        HG_BIN = "hg"
        
        def info
          cmd = "#{HG_BIN} -R #{target('')} root"
          root_url = nil
          shellout(cmd) do |io|
            root_url = io.gets
          end
          return nil if $? && $?.exitstatus != 0
          info = Info.new({:root_url => root_url.chomp,
                           :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
                         })
          info
        rescue CommandFailed
          return nil
        end
        
        def entries(path=nil, identifier=nil)
          path ||= ''
          entries = Entries.new
          cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate"
          cmd << " -r #{identifier.to_i}" if identifier
          cmd << " " + shell_quote('glob:**')
          shellout(cmd) do |io|
            io.each_line do |line|
              e = line.chomp.split(%r{[\/\\]})
              entries << Entry.new({:name => e.first,
                                    :path => (path.empty? ? e.first : "#{path}/#{e.first}"),
                                    :kind => (e.size > 1 ? 'dir' : 'file'),
                                    :lastrev => Revision.new
                                    }) unless entries.detect{|entry| entry.name == e.first}
            end
          end
          return nil if $? && $?.exitstatus != 0
          entries.sort_by_name
        end
  
        def entry(path=nil, identifier=nil)
          path ||= ''
          search_path = path.split('/')[0..-2].join('/')
          entry_name = path.split('/').last
          e = entries(search_path, identifier)
          e ? e.detect{|entry| entry.name == entry_name} : nil
        end
          
        def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
          revisions = Revisions.new
          cmd = "#{HG_BIN} -v -R #{target('')} log"
          cmd << " -r #{identifier_from.to_i}:" if identifier_from
          cmd << " --limit #{options[:limit].to_i}" if options[:limit]
          shellout(cmd) do |io|
            changeset = {}
            parsing_descr = false
            line_feeds = 0
            
            io.each_line do |line|
              if line =~ /^(\w+):\s*(.*)$/
                key = $1
                value = $2
                if parsing_descr && line_feeds > 1
                  parsing_descr = false
                  revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
                                             :scmid => changeset[:changeset].split(':').last,
                                             :author => changeset[:user],
                                             :time => Time.parse(changeset[:date]),
                                             :message => changeset[:description],
                                             :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
                  })
                  changeset = {}
                end
                if !parsing_descr
                  changeset.store key.to_sym, value
                  if $1 == "description"
                    parsing_descr = true
                    line_feeds = 0
                    next
                  end
                end
              end
              if parsing_descr
                changeset[:description] << line
                line_feeds += 1 if line.chomp.empty?
              end
            end
            revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
                                       :scmid => changeset[:changeset].split(':').last,
                                       :author => changeset[:user],
                                       :time => Time.parse(changeset[:date]),
                                       :message => changeset[:description],
                                       :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
            })
          end
          return nil if $? && $?.exitstatus != 0
          revisions
        end
        
        def diff(path, identifier_from, identifier_to=nil, type="inline")
          path ||= ''
          if identifier_to
            identifier_to = identifier_to.to_i 
          else
            identifier_to = identifier_from.to_i - 1
          end
          cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
          cmd << " -I #{target(path)}" unless path.empty?
          diff = []
          shellout(cmd) do |io|
            io.each_line do |line|
              diff << line
            end
          end
          return nil if $? && $?.exitstatus != 0
          DiffTableList.new diff, type
        end
        
        def cat(path, identifier=nil)
          cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}"
          cat = nil
          shellout(cmd) do |io|
            io.binmode
            cat = io.read
          end
          return nil if $? && $?.exitstatus != 0
          cat
        end
        
        def annotate(path, identifier=nil)
          path ||= ''
          cmd = "#{HG_BIN} -R #{target('')}"
          cmd << " annotate -n -u"
          cmd << " -r #{identifier.to_i}" if identifier
          cmd << " #{target(path)}"
          blame = Annotate.new
          shellout(cmd) do |io|
            io.each_line do |line|
              next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
              blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
            end
          end
          return nil if $? && $?.exitstatus != 0
          blame
        end
      end
    end
  end
end