is better suited to your data, check the
fast-import
man page for details about how to
provide your data in this manner.
The format for listing the new file contents or specifying a modified file with the
new contents is as follows:
M 644 inline path/to/file
data (size)
(file contents)
Here, 644 is the mode (if you have executable files, you need to detect and specify
755 instead), and inline says you’ll list the contents immediately after this line. Your
inline
data
method looks like this:
def inline_data(file, code = ’M’, mode = ’644’)
content = File.read(file)
puts "#{code} #{mode} inline #{file}"
export_data(content)
end
You reuse the
export
data
method you defined earlier, because it’s the same as the
way you specified your commit message data.
The last thing you need to do is to return the current mark so it can be passed to the
next iteration:
202

C
HAPTER
8
G
IT AND
O
THER
S
YSTEMS
return mark
That’s it. If you run this script, you’ll get content that looks something like this:
$ ruby import.rb /opt/import_from
commit refs/heads/master
mark :1
committer Scott Chacon <[email protected]> 1230883200 -0700
data 29
imported from back_2009_01_02deleteall
M 644 inline file.rb
data 12
version two
commit refs/heads/master
mark :2
committer Scott Chacon <[email protected]> 1231056000 -0700
data 29
imported from back_2009_01_04from :1
deleteall
M 644 inline file.rb
data 14
version three
M 644 inline new.rb
data 16
new version one
(...)
To run the importer, pipe this output through
git fast-import
while in the Git
directory you want to import into. You can create a new directory and then run
git
init
in it for a starting point, and then run your script:
$ git init
Initialized empty Git repository in /opt/import_to/.git/
$ ruby import.rb /opt/import_from | git fast-import
git-fast-import statistics:
---------------------------------------------------------------------
Alloc’d objects:
5000
Total objects:
18 (
1 duplicates
)
blobs
:
7 (
1 duplicates
0 deltas)
trees
:
6 (
0 duplicates
1 deltas)
commits:
5 (
0 duplicates
0 deltas)
tags
:
0 (
0 duplicates
0 deltas)
Total branches:
1 (
1 loads
)
marks:
1024 (
5 unique
)
atoms:
3
Memory total:
2255 KiB
pools:
2098 KiB
objects:
156 KiB
---------------------------------------------------------------------
pack_report: getpagesize()
=
4096
pack_report: core.packedGitWindowSize =
33554432
pack_report: core.packedGitLimit
=
268435456
pack_report: pack_used_ctr
=
9
203

P
RO
G
IT
S
COTT
C
HACON
pack_report: pack_mmap_calls
=
5
pack_report: pack_open_windows
=
1 /
1
pack_report: pack_mapped
=
1356 /
1356
---------------------------------------------------------------------
As you can see, when it completes successfully, it gives you a bunch of statistics
about what it accomplished. In this case, you imported 18 objects total for 5 commits
into 1 branch. Now, you can run
git log
to see your new history:
$ git log -2
commit 10bfe7d22ce15ee25b60a824c8982157ca593d41
Author: Scott Chacon <[email protected]>
Date:
Sun May 3 12:57:39 2009 -0700
imported from current
commit 7e519590de754d079dd73b44d695a42c9d2df452
Author: Scott Chacon <[email protected]>
Date:
Tue Feb 3 01:00:00 2009 -0700
imported from back_2009_02_03
There you go — a nice, clean Git repository. It’s important to note that nothing is
checked out — you don’t have any files in your working directory at first. To get them,
you must reset your branch to where
master
is now:
$ ls
$ git reset --hard master
HEAD is now at 10bfe7d imported from current
$ ls
file.rb
lib
You can do a lot more with the

