Skip to content

Commit e349026

Browse files
peffgitster
authored andcommitted
contrib/fast-import: add perl version of simple example
This is based on the git-import.sh script, but is a little more robust and efficient. More importantly, it should serve as a quick template for interfacing fast-import with perl scripts. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7f8cfad commit e349026

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/perl
2+
#
3+
# Performs an initial import of a directory. This is the equivalent
4+
# of doing 'git init; git add .; git commit'. It's a little slower,
5+
# but is meant to be a simple fast-import example.
6+
7+
use strict;
8+
use File::Find;
9+
10+
my $USAGE = 'Usage: git-import branch import-message';
11+
my $branch = shift or die "$USAGE\n";
12+
my $message = shift or die "$USAGE\n";
13+
14+
chomp(my $username = `git config user.name`);
15+
chomp(my $email = `git config user.email`);
16+
die 'You need to set user name and email'
17+
unless $username && $email;
18+
19+
system('git init');
20+
open(my $fi, '|-', qw(git fast-import --date-format=now))
21+
or die "unable to spawn fast-import: $!";
22+
23+
print $fi <<EOF;
24+
commit refs/heads/$branch
25+
committer $username <$email> now
26+
data <<MSGEOF
27+
$message
28+
MSGEOF
29+
30+
EOF
31+
32+
find(
33+
sub {
34+
if($File::Find::name eq './.git') {
35+
$File::Find::prune = 1;
36+
return;
37+
}
38+
return unless -f $_;
39+
40+
my $fn = $File::Find::name;
41+
$fn =~ s#^.\/##;
42+
43+
open(my $in, '<', $_)
44+
or die "unable to open $fn: $!";
45+
my @st = stat($in)
46+
or die "unable to stat $fn: $!";
47+
my $len = $st[7];
48+
49+
print $fi "M 644 inline $fn\n";
50+
print $fi "data $len\n";
51+
while($len > 0) {
52+
my $r = read($in, my $buf, $len < 4096 ? $len : 4096);
53+
defined($r) or die "read error from $fn: $!";
54+
$r > 0 or die "premature EOF from $fn: $!";
55+
print $fi $buf;
56+
$len -= $r;
57+
}
58+
print $fi "\n";
59+
60+
}, '.'
61+
);
62+
63+
close($fi);
64+
exit $?;

0 commit comments

Comments
 (0)