about summary refs log blame commit diff
path: root/contrib/fast-import/import-directories.perl
blob: a16f79cfdc46271723d8aa5cd3e685b62845c047 (plain) (tree)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416































































































































































































































































































































































































































                                                                                                      
#!/usr/bin/perl
#
# Copyright 2008-2009 Peter Krefting <peter@softwolves.pp.se>
#
# ------------------------------------------------------------------------
#
# 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.
#
# 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/>.
#
# ------------------------------------------------------------------------

=pod

=head1 NAME

import-directories - Import bits and pieces to Git.

=head1 SYNOPSIS

B<import-directories.perl> F<configfile> F<outputfile>

=head1 DESCRIPTION

Script to import arbitrary projects version controlled by the "copy the
source directory to a new location and edit it there"-version controlled
projects into version control. Handles projects with arbitrary branching
and version trees, taking a file describing the inputs and generating a
file compatible with the L<git-fast-import(1)> format.

=head1 CONFIGURATION FILE

=head2 Format

The configuration file is based on the standard I<.ini> format.

 ; Comments start with semi-colons
 [section]
 key=value

Please see below for information on how to escape special characters.

=head2 Global configuration

Global configuration is done in the B<[config]> section, which should be
the first section in the file. Configuration can be changed by
repeating configuration sections later on.

 [config]
 ; configure conversion of CRLFs. "convert" means that all CRLFs
 ; should be converted into LFs (suitable for the core.autocrlf
 ; setting set to true in Git). "none" means that all data is
 ; treated as binary.
 crlf=convert

=head2 Revision configuration

Each revision that is to be imported is described in three
sections. Revisions should be defined in topological order, so
that a revision's parent has always been defined when a new revision
is introduced. All the sections for one revision must be defined
before defining the next revision.

Each revision is assigned a unique numerical identifier. The
numbers do not need to be consecutive, nor monotonically
increasing.

For instance, if your configuration file contains only the two
revisions 4711 and 42, where 4711 is the initial commit, the
only requirement is that 4711 is completely defined before 42.

=pod

=head3 Revision description section

A section whose section name is just an integer gives meta-data
about the revision.

 [3]
 ; author sets the author of the revisions
 author=Peter Krefting <peter@softwolves.pp.se>
 ; branch sets the branch that the revision should be committed to
 branch=master
 ; parent describes the revision that is the parent of this commit
 ; (optional)
 parent=1
 ; merges describes a revision that is merged into this commit
 ; (optional; can be repeated)
 merges=2
 ; selects one file to take the timestamp from
 ; (optional; if unspecified, the most recent file from the .files
 ;  section is used)
 timestamp=3/source.c

=head3 Revision contents section

A section whose section name is an integer followed by B<.files>
describe all the files included in this revision. If a file that
was available previously is not included in this revision, it will
be removed.

If an on-disk revision is incomplete, you can point to files from
a previous revision. There are no restrictions on where the source
files are located, nor on their names.

 [3.files]
 ; the key is the path inside the repository, the value is the path
 ; as seen from the importer script.
 source.c=ver-3.00/source.c
 source.h=ver-2.99/source.h
 readme.txt=ver-3.00/introduction to the project.txt

File names are treated as byte strings (but please see below on
quoting rules), and should be stored in the configuration file in
the encoding that should be used in the generated repository.

=head3 Revision commit message section

A section whose section name is an integer followed by B<.message>
gives the commit message. This section is read verbatim, up until
the beginning of the next section. As such, a commit message may not
contain a line that begins with an opening square bracket ("[") and
ends with a closing square bracket ("]"), unless they are surrounded
by whitespace or other characters.

 [3.message]
 Implement foobar.
 ; trailing blank lines are ignored.

=cut

# Globals
use strict;
use warnings;
use integer;
my $crlfmode = 0;
my @revs;
my (%revmap, %message, %files, %author, %branch, %parent, %merges, %time, %timesource);
my $sectiontype = 0;
my $rev = 0;
my $mark = 1;

# Check command line
if ($#ARGV < 1 || $ARGV[0] =~ /^--?h/)
{
    exec('perldoc', $0);
    exit 1;
}

# Open configuration
my $config = $ARGV[0];
open CFG, '<', $config or die "Cannot open configuration file \"$config\": ";

# Open output
my $output = $ARGV[1];
open OUT, '>', $output or die "Cannot create output file \"$output\": ";
binmode OUT;

LINE: while (my $line = <CFG>)
{
	$line =~ s/\r?\n$//;
	next LINE if $sectiontype != 4 && $line eq '';
	next LINE if $line =~ /^;/;
	my $oldsectiontype = $sectiontype;
	my $oldrev = $rev;

	# Sections
	if ($line =~ m"^\[(config|(\d+)(|\.files|\.message))\]$")
	{
		if ($1 eq 'config')
		{
			$sectiontype = 1;
		}
		elsif ($3 eq '')
		{
			$sectiontype = 2;
			$rev = $2;
			# Create a new revision
			die "Duplicate rev: $line\n " if defined $revmap{$rev};
			print "Reading revision $rev\n";
			push @revs, $rev;
			$revmap{$rev} = $mark ++;
			$time{$revmap{$rev}} = 0;
		}
		elsif ($3 eq '.files')
		{
			$sectiontype = 3;
			$rev = $2;
			die "Revision mismatch: $line\n " unless $rev == $oldrev;
		}
		elsif ($3 eq '.message')
		{
			$sectiontype = 4;
			$rev = $2;
			die "Revision mismatch: $line\n " unless $rev == $oldrev;
		}
		else
		{
			die "Internal parse error: $line\n ";
		}
		next LINE;
	}

	# Parse data
	if ($sectiontype != 4)
	{
		# Key and value
		if ($line =~ m"^\s*([^\s].*=.*[^\s])\s*$")
		{
			my ($key, $value) = &parsekeyvaluepair($1);
			# Global configuration
			if (1 == $sectiontype)
			{
				if ($key eq 'crlf')
				{
					$crlfmode = 1, next LINE if $value eq 'convert';
					$crlfmode = 0, next LINE if $value eq 'none';
				}
				die "Unknown configuration option: $line\n ";
			}
			# Revision specification
			if (2 == $sectiontype)
			{
				my $current = $revmap{$rev};
				$author{$current} = $value, next LINE if $key eq 'author';
				$branch{$current} = $value, next LINE if $key eq 'branch';
				$parent{$current} = $value, next LINE if $key eq 'parent';
				$timesource{$current} = $value, next LINE if $key eq 'timestamp';
				push(@{$merges{$current}}, $value), next LINE if $key eq 'merges';
				die "Unknown revision option: $line\n ";
			}
			# Filespecs
			if (3 == $sectiontype)
			{
				# Add the file and create a marker
				die "File not found: $line\n " unless -f $value;
				my $current = $revmap{$rev};
				${$files{$current}}{$key} = $mark;
				my $time = &fileblob($value, $crlfmode, $mark ++);

				# Update revision timestamp if more recent than other
				# files seen, or if this is the file we have selected
				# to take the time stamp from using the "timestamp"
				# directive.
				if ((defined $timesource{$current} && $timesource{$current} eq $value)
				    || $time > $time{$current})
				{
					$time{$current} = $time;
				}
			}
		}
		else
		{
			die "Parse error: $line\n ";
		}
	}
	else
	{
		# Commit message
		my $current = $revmap{$rev};
		if (defined $message{$current})
		{
			$message{$current} .= "\n";
		}
		$message{$current} .= $line;
	}
}
close CFG;

# Start spewing out data for git-fast-import
foreach my $commit (@revs)
{
	# Progress
	print OUT "progress Creating revision $commit\n";

	# Create commit header
	my $mark = $revmap{$commit};

	# Branch and commit id
	print OUT "commit refs/heads/", $branch{$mark}, "\nmark :", $mark, "\n";

	# Author and timestamp
	die "No timestamp defined for $commit (no files?)\n" unless defined $time{$mark};
	print OUT "committer ", $author{$mark}, " ", $time{$mark}, " +0100\n";

	# Commit message
	die "No message defined for $commit\n" unless defined $message{$mark};
	my $message = $message{$mark};
	$message =~ s/\n$//; # Kill trailing empty line
	print OUT "data ", length($message), "\n", $message, "\n";

	# Parent and any merges
	print OUT "from :", $revmap{$parent{$mark}}, "\n" if defined $parent{$mark};
	if (defined $merges{$mark})
	{
		foreach my $merge (@{$merges{$mark}})
		{
			print OUT "merge :", $revmap{$merge}, "\n";
		}
	}

	# Output file marks
	print OUT "deleteall\n"; # start from scratch
	foreach my $file (sort keys %{$files{$mark}})
	{
		print OUT "M 644 :", ${$files{$mark}}{$file}, " $file\n";
	}
	print OUT "\n";
}

# Create one file blob
sub fileblob
{
	my ($filename, $crlfmode, $mark) = @_;

	# Import the file
	print OUT "progress Importing $filename\nblob\nmark :$mark\n";
	open FILE, '<', $filename or die "Cannot read $filename\n ";
	binmode FILE;
	my ($size, $mtime) = (stat(FILE))[7,9];
	my $file;
	read FILE, $file, $size;
	close FILE;
	$file =~ s/\r\n/\n/g if $crlfmode;
	print OUT "data ", length($file), "\n", $file, "\n";

	return $mtime;
}

# Parse a key=value pair
sub parsekeyvaluepair
{
=pod

=head2 Escaping special characters

Key and value strings may be enclosed in quotes, in which case
whitespace inside the quotes is preserved. Additionally, an equal
sign may be included in the key by preceding it with a backslash.
For example:

 "key1 "=value1
 key2=" value2"
 key\=3=value3
 key4=value=4
 "key5""=value5

Here the first key is "key1 " (note the trailing white-space) and the
second value is " value2" (note the leading white-space). The third
key contains an equal sign "key=3" and so does the fourth value, which
does not need to be escaped. The fifth key contains a trailing quote,
which does not need to be escaped since it is inside a surrounding
quote.

=cut
	my $pair = shift;

	# Separate key and value by the first non-quoted equal sign
	my ($key, $value);
	if ($pair =~ /^(.*[^\\])=(.*)$/)
	{
		($key, $value) = ($1, $2)
	}
	else
	{
		die "Parse error: $pair\n ";
	}

	# Unquote and unescape the key and value separately
	return (&unescape($key), &unescape($value));
}

# Unquote and unescape
sub unescape
{
	my $string = shift;

	# First remove enclosing quotes. Backslash before the trailing
	# quote leaves both.
	if ($string =~ /^"(.*[^\\])"$/)
	{
		$string = $1;
	}

	# Second remove any backslashes inside the unquoted string.
	# For later: Handle special sequences like \t ?
	$string =~ s/\\(.)/$1/g;

	return $string;
}

__END__

=pod

=head1 EXAMPLES

B<import-directories.perl> F<project.import>

=head1 AUTHOR

Copyright 2008-2009 Peter Krefting E<lt>peter@softwolves.pp.se>

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.

=cut