freshtonic / undarcs

A Ruby script that can export a Darcs 1 repo to Git without invoking Darcs.

This URL has Read+Write access

undarcs / export_to_git_patch_handler.rb
100644 218 lines (186 sloc) 6.506 kb
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
=begin
 
darcs-fast-import:
convert a Darcs (Version 1) repository to Git, without invoking Darcs.
 
Copyright (C) 2008 James Sadler <freshtonic@gmail.com>
 
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 3 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, see <http://www.gnu.org/licenses/>.
 
=end
 
class ExportToGitPatchHandler < PatchHandler
 
  def initialize(gitrepo, patch_name, logger, skip_binaries, authors)
    @added = []
    @renamed = {}
    @deleted = []
    @logger = logger
    @gitrepo = gitrepo
    @patch_name = patch_name
    @skip_binaries = skip_binaries
    @authors = authors
  end
 
  def metadata author, short_msg, long_msg, timestamp, inverted
    @author = author
    @git_message = ("#{short_msg}\n#{long_msg}\n\n" +
      "Exported from Darcs patch: #{@patch_name}").gsub(/[']/, '"')
    @timestamp = timestamp
    @inverted = inverted
  end
 
  def addfile file
    @added << file
    log "adding file #{file}"
    FileUtils.touch("#{@gitrepo}/#{file}")
  end
 
  def adddir dir
    log "adding dir '#{dir}'"
    FileUtils.mkdir "#{@gitrepo}/#{dir}"
    FileUtils.touch "#{@gitrepo}/#{dir}/.keepme"
    if !File.exists? "#{@gitrepo}/#{dir}/.keepme"
      raise "WHOAH! file #{@gitrepo}/#{dir}/.keepme does not exist!"
    end
    @added << "#{dir}"
    @added << "#{dir}/.keepme"
  end
 
  def move file, to
    # Apparently darcs thinks is a good idea to add and then
    # move a file in the same patch. Before we attempt to move
    # a file that's not yet on disk, let's just simply remove
    # the name from 'added_files' if it's there, and replace
    # it with the new name.
    File.rename "#{@gitrepo}/#{file}", "#{@gitrepo}/#{to}"
    if @added.include? file
      @added.delete(file)
    end
    @added << to
    @renamed[file] = to
  end
 
  def hunk file, line_number, inserted_lines, deleted_lines
    if File.exists?("#{@gitrepo}/#{file}")
      orig_lines = read_original_file(file)
      orig_lines_index = line_number - 1
 
      deleted_lines.size.times {|i| orig_lines.delete_at orig_lines_index}
      orig_lines.insert(orig_lines_index, inserted_lines)
      orig_lines.flatten!
      
      out_file = File.new("#{@gitrepo}/#{file}", "w")
      begin
        orig_lines.each do |orig_line|
          out_file.write orig_line
        end
      ensure
        out_file.close
      end
    else
      log "WARN: attempt to apply hunk to non-existing file #{file}"
      if inserted_lines.size == 0 || only_whitespace?(inserted_lines)
        log "No lines were inserted"
      else
        log "lines were supposed to be inserted - something bad has happened. lines:\n" +
          inserted_lines.join("\n+")
      end
    end
  end
 
  def changepref
    # do nothing. No equivalent exists in Git
  end
 
  def rmfile file
    @deleted << file
    log "removing file '#{file}'"
    if File.exists? "#{@gitrepo}/#{file}"
      File.delete "#{@gitrepo}/#{file}"
    else
      log "file did not exist, this may be an error but its valid for " +
        "two patches to remove the same file and not conflict"
    end
    # weird, I know but darcs will happily create, edit, move and then
    # destroy a file all in the same patch. So we need to remove the file
    # from the 'add' list.
    @added.delete(file)
    @renamed.delete(@renamed.invert[file])
  end
 
  def rmdir dir
    if File.exists? "#{@gitrepo}/#{dir}"
      FileUtils.rm_rf "#{@gitrepo}/#{dir}"
      @added.delete(dir)
      @added.delete("#{dir}/.keepme")
      @renamed.delete(@renamed.invert[dir])
    end
  end
 
  def binary file, datastream
    if @skip_binaries
      log "NOT writing binary file #{file}, skipping"
    else
      log "writing binary file #{file}"
    end
  end
 
  def merger
    # do nothing (the parser currently skips over them)
  end
 
  def replace file, regexp, oldtext, newtext
    # NOTE: this is simplistic, and possibly won't work in all cases. By
    # won't work, I mean that it may not necessarily do what Darcs does,
    # as it ignores the token regex that appears in a 'replace'
    # directive in a Darcs patch.
    command = "sed -i 's/#{oldtext}/#{newtext}/g' #{@gitrepo}/#{file}"
    Open3.popen3("(#{command})") do |sin,sout,serr|
      log "executing command '#{command}'"
      sinlines = sout.readlines
      serrlines = serr.readlines
      if serrlines.size > 0
        serrlines.each {|line| $stderr.puts line }
        raise "error executing command '#{command}'"
      end
    end
  end
 
  def skip_binaries?
    @skip_binaries
  end
 
  def finished
    @renamed.keys.each_slice(80) {|files| run_git "add -u #{to_file_list(files)}"}
    @renamed.values.each_slice(80) {|files| run_git "add #{to_file_list(files)}"}
    @added = @added - @deleted
    @added.each_slice(80) {|files| run_git "add #{to_file_list(files)}"}
    # Darcs deletes a file by creating a hunk that removes all the lines
    # then deletes the file. In that case we want no files in the changed list
    # that are in the deleted list, or we will break Git.
    run_git "add -u" # take care of changed and deleted files
    run_git "commit -m '#{@git_message}' --author \"#{@authors.get_email(@author)}\""
  end
 
  private
 
  def to_file_list(file_names)
    (file_names.map{|file_name| "'#{file_name}'"}).join(" ")
  end
 
  def run_git(command)
    Open3.popen3("(cd #{@gitrepo} && git #{command})") do |sin,sout,serr|
      log "executing command '#{command}'"
      sinlines = sout.readlines
      serrlines = serr.readlines
      if serrlines.size > 0
        serrlines.each {|line| $stderr.puts line }
        log "in patch #{@current_patch}"
        raise "executing command '#{command}'"
      end
    end
  end
 
  def log(msg)
    @logger.log(msg)
  end
 
  def read_original_file(file)
    in_file = File.new("#{@gitrepo}/#{file}", "r")
    origin_lines = nil
    begin
      orig_lines = in_file.readlines
    ensure
      in_file.close
    end
    orig_lines
  end
 
  def only_whitespace?(lines)
    lines.each {|l| return false if l.strip != "" }
    true
  end
end