-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarcs-fast-export.rb
executable file
·154 lines (126 loc) · 3.69 KB
/
darcs-fast-export.rb
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
#!/usr/bin/ruby
=begin
darcs-fast-import:
convert a Darcs (Version 1) repository to Git, without invoking Darcs.
Copyright (C) 2008 James Sadler <[email protected]>
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
# TODO: assert that there are no untracked files after applying changes to
# the git repo.
#
# TODO: Git doesn't like empty directories, so insert a hidden
# .darcs-fast-export file in each new dir (and add it to git).
#
# TODO: Resumability.
require 'enumerator'
require 'optparse'
require 'open3'
require 'fileutils'
require 'sha1'
require 'patch_parser'
require 'inventory_parser'
require 'export_to_git_patch_handler'
require 'author_file'
class PatchExporter
def initialize(args)
parse_command_line(args)
begin
@authors = AuthorFile.new @authorsfile
rescue
log "could not find authors file #{@authorsfile}"
exit 1
end
end
def export
generate_patch_list.each do |patch|
log "converting patch #{patch.filename}"
instream = IO.popen("gunzip -c #{@darcsrepo}/_darcs/patches/#{patch.filename}")
DarcsPatchParser.new(instream,
ExportToGitPatchHandler.new(
@gitrepo, patch.filename, self, @skip_binaries, @authors)).parse
end
end
def log(msg)
STDERR.puts "darcs-fast-export: #{msg}"
end
private
def generate_patch_list
patches = []
File.open("#{@darcsrepo}/_darcs/inventory") do |f|
begin
patch = InventoryParser.read(f)
if !patch.nil?
patches << patch
else
break
end
rescue EOFError
break
rescue
log $!.message
exit 1
end while true
end
patches
end
def parse_command_line(args)
OptionParser.new do |options|
options.banner = "Usage #{$0} [options]"
options.on(
'-a [authorfile]',
'--authorfile [authorfile]',
'File of DARCS_USER_NAME=Joe Blogs <[email protected]> , one entry per line'
) do |authorfile|
@authorsfile = authorfile
end
options.on(
'-s [darcsrepo]',
'--source-repo [darcsrepo]',
'The source darcs repository'
) do |darcsrepo|
@darcsrepo = darcsrepo
end
options.on(
'-t [gittrepo]',
'--target-repo [gitrepo]',
'The target GIT repository (must already be initialised)'
) do |gitrepo|
@gitrepo = gitrepo
end
options.on(
'-b [skip]',
'--skip-binaries [skip]',
'Skips binary files (i.e. will not include them in the generated Git repo)'
) do |skip|
@skip_binaries = (skip =~ /^true$/)
end
begin
@skip_binaries = false
options.parse! args
rescue OptionParser::InvalidOption
log "Failed to parse options(#{$!})"
exit 1
end
check_arg(options, "authorfile", @authorsfile)
check_arg(options, "source-repo", @darcsrepo)
check_arg(options, "target-repo", @gitrepo)
end
end
def check_arg(options, arg, value)
unless value
warn "#{arg} required"
warn options
exit(1)
end
end
end
PatchExporter.new(ARGV).export() if __FILE__ == $0