You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/09-git-and-other-scms/sections/import-custom.asc
+66-66Lines changed: 66 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
[[_custom_importer]]
2
-
==== A Custom Importer
2
+
==== 一个自定义的导入器
3
3
4
4
(((git commands, fast-import)))
5
5
(((Importing, from others)))
6
-
If your system isn't one of the above, you should look for an importer online – quality importers are available for many other systems, including CVS, Clear Case, Visual Source Safe, even a directory of archives.
7
-
If none of these tools works for you, you have a more obscure tool, or you otherwise need a more custom importing process, you should use `git fast-import`.
8
-
This command reads simple instructions from stdin to write specific Git data.
9
-
It's much easier to create Git objects this way than to run the raw Git commands or try to write the raw objects (see <<_git_internals>> for more information).
10
-
This way, you can write an import script that reads the necessary information out of the system you're importing from and prints straightforward instructions to stdout.
11
-
You can then run this program and pipe its output through `git fast-import`.
To quickly demonstrate, you'll write a simple importer.
14
-
Suppose you work in `current`, you back up your project by occasionally copying the directory into a time-stamped `back_YYYY_MM_DD` backup directory, and you want to import this into Git.
In order to import a Git directory, you need to review how Git stores its data.
28
-
As you may remember, Git is fundamentally a linked list of commit objects that point to a snapshot of content.
29
-
All you have to do is tell `fast-import` what the content snapshots are, what commit data points to them, and the order they go in.
30
-
Your strategy will be to go through the snapshots one at a time and create commits with the contents of each directory, linking each commit back to the previous one.
As we did in <<_an_example_git_enforced_policy>>, we'll write this in Ruby, because it's what we generally work with and it tends to be easy to read.
33
-
You can write this example pretty easily in anything you're familiar with – it just needs to print the appropriate information to `stdout`.
34
-
And, if you are running on Windows, this means you'll need to take special care to not introduce carriage returns at the end your lines – git fast-import is very particular about just wanting line feeds (LF) not the carriage return line feeds (CRLF) that Windows uses.
然而,如果你在 Windows 上,这意味着需要特别注意不要引入回车符到行尾 - git fast-import 非常特别地只接受换行符(LF)而不是 Windows 使用的回车换行符(CRLF)。
35
35
36
-
To begin, you'll change into the target directory and identify every subdirectory, each of which is a snapshot that you want to import as a commit.
37
-
You'll change into each subdirectory and print the commands necessary to export it.
38
-
Your basic main loop looks like this:
36
+
现在开始,需要进入目标目录中并识别每一个子目录,每一个都是你要导入为提交的快照。
37
+
要进入到每个子目录中并为导出它打印必要的命令。
38
+
基本主循环像这个样子:
39
39
40
40
[source,ruby]
41
41
----
@@ -54,17 +54,17 @@ Dir.chdir(ARGV[0]) do
54
54
end
55
55
----
56
56
57
-
You run `print_export` inside each directory, which takes the manifest and mark of the previous snapshot and returns the manifest and mark of this one; that way, you can link them properly.
58
-
``Mark'' is the `fast-import` term for an identifier you give to a commit; as you create commits, you give each one a mark that you can use to link to it from other commits.
59
-
So, the first thing to do in your `print_export` method is generate a mark from the directory name:
Now you're ready to begin printing out the commit data for your importer.
113
-
The initial information states that you're defining a commit object and what branch it's on, followed by the mark you've generated, the committer information and commit message, and then the previous commit, if any.
@@ -123,17 +123,17 @@ export_data('imported from ' + dir)
123
123
puts 'from :' + last_mark if last_mark
124
124
----
125
125
126
-
You hardcode the time zone (-0700) because doing so is easy.
127
-
If you're importing from another system, you must specify the time zone as an offset.
128
-
The commit message must be expressed in a special format:
126
+
我们将硬编码时区信息(-0700),因为这样很容易。
127
+
如果从其他系统导入,必须指定为一个偏移的时区。
128
+
提交信息必须指定为特殊的格式:
129
129
130
130
[source]
131
131
----
132
132
data (size)\n(contents)
133
133
----
134
134
135
-
The format consists of the word data, the size of the data to be read, a newline, and finally the data.
136
-
Because you need to use the same format to specify the file contents later, you create a helper method, `export_data`:
135
+
这个格式包括文本数据、将要读取数据的大小、一个换行符、最终的数据。
136
+
因为之后还需要为文件内容指定相同的数据格式,你需要创建一个帮助函数,`export_data`:
137
137
138
138
[source,ruby]
139
139
----
@@ -142,9 +142,9 @@ def export_data(string)
142
142
end
143
143
----
144
144
145
-
All that's left is to specify the file contents for each snapshot.
146
-
This is easy, because you have each one in a directory – you can print out the `deleteall` command followed by the contents of each file in the directory.
@@ -155,11 +155,11 @@ Dir.glob("**/*").each do |file|
155
155
end
156
156
----
157
157
158
-
Note: Because many systems think of their revisions as changes from one commit to another, fast-import can also take commands with each commit to specify which files have been added, removed, or modified and what the new contents are.
159
-
You could calculate the differences between snapshots and provide only this data, but doing so is more complex – you may as well give Git all the data and let it figure it out.
160
-
If this is better suited to your data, check the `fast-import` man page for details about how to provide your data in this manner.
如果这更适合你的数据,查阅 `fast-import` man 帮助页来了解如何以这种方式提供你的数据。
161
161
162
-
The format for listing the new file contents or specifying a modified file with the new contents is as follows:
162
+
这种列出新文件内容或用新内容指定修改文件的格式如同下面的内容:
163
163
164
164
[source]
165
165
----
@@ -168,8 +168,8 @@ data (size)
168
168
(file contents)
169
169
----
170
170
171
-
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.
It's important to note that nothing is checked out – you don't have any files in your working directory at first.
362
-
To get them, you must reset your branch to where `master` is now:
360
+
做得很好 - 一个漂亮、干净的 Git 仓库。
361
+
要注意的一点是并没有检出任何东西 - 一开始你的工作目录内并没有任何文件。
362
+
为了得到他们,你必须将分支重置到 `master` 所在的地方:
363
363
364
364
[source,console]
365
365
----
@@ -370,5 +370,5 @@ $ ls
370
370
README.md main.rb
371
371
----
372
372
373
-
You can do a lot more with the `fast-import` tool – handle different modes, binary data, multiple branches and merging, tags, progress indicators, and more.
374
-
A number of examples of more complex scenarios are available in the `contrib/fast-import` directory of the Git source code.
0 commit comments