Mercurial > hg > hg-fastimport
annotate hgfastimport/hgimport.py @ 37:513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
- fastimport library now returns filenames as byte strings,
so leave them be
- re-encode commit message as UTF-8
- monkeypatch mercurial.encoding to assume UTF-8 for everything
| author | Greg Ward <greg-hg@gerg.ca> |
|---|---|
| date | Fri, 08 May 2009 11:03:16 -0400 |
| parents | 08e2157aaa9a |
| children | 177a133519bc |
| rev | line source |
|---|---|
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
1 # Copyright (C) 2008 Canonical Ltd |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
2 # |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
3 # This program is free software; you can redistribute it and/or modify |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
4 # it under the terms of the GNU General Public License as published by |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
5 # the Free Software Foundation; either version 2 of the License, or |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
6 # (at your option) any later version. |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
7 # |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
8 # This program is distributed in the hope that it will be useful, |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
11 # GNU General Public License for more details. |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
12 # |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
13 # You should have received a copy of the GNU General Public License |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
14 # along with this program; if not, write to the Free Software |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
16 |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
17 """Processor of import commands. |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
18 |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
19 This module provides core processing functionality including an abstract class |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
20 for basing real processors on. See the processors package for examples. |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
21 """ |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
22 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
23 import os |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
24 import os.path |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
25 import errno |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
26 import shutil |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
27 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
28 import mercurial.hg |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
29 import mercurial.commands |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
30 from mercurial import util |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
31 from mercurial.node import nullrev |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
32 |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
33 from fastimport import processor |
|
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
34 from hgfastimport import hgechoprocessor |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
35 |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
36 class HgImportProcessor(processor.ImportProcessor): |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
37 |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
38 def __init__(self, ui, repo, **opts): |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
39 self.ui = ui |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
40 self.repo = repo |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
41 self.opts = opts |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
42 self.last_mark = None |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
43 self.mark_map = {} |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
44 self.branch_map = {} |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
45 #self.tag_map = {} |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
46 #self.tag_back_map = {} |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
47 self.finished = False |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
48 |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
49 self.numblobs = 0 # for progress reporting |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
50 self.blobdir = None |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
51 |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
52 def setup(self): |
|
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
53 """Setup before processing any streams.""" |
|
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
54 pass |
|
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
55 |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
56 def teardown(self): |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
57 """Cleanup after processing all streams.""" |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
58 if self.blobdir and os.path.exists(self.blobdir): |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
59 self.ui.status("Removing blob dir %r ...\n" % self.blobdir) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
60 shutil.rmtree(self.blobdir) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
61 |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
62 def progress_handler(self, cmd): |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
63 self.ui.write("Progress: %s\n" % cmd.message) |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
64 |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
65 def blob_handler(self, cmd): |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
66 if self.blobdir is None: # no blobs seen yet |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
67 # XXX cleanup? |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
68 self.blobdir = os.path.join(self.repo.root, ".hg", "blobs") |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
69 os.mkdir(self.blobdir) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
70 |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
71 fn = self.getblobfilename(cmd.id) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
72 blobfile = open(fn, "wb") |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
73 #self.ui.debug("writing blob %s to %s (%d bytes)\n" |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
74 # % (cmd.id, fn, len(cmd.data))) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
75 blobfile.write(cmd.data) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
76 blobfile.close() |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
77 |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
78 self.numblobs += 1 |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
79 if self.numblobs % 500 == 0: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
80 self.ui.status("%d blobs read\n" % self.numblobs) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
81 |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
82 def getblobfilename(self, blobid): |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
83 if self.blobdir is None: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
84 raise RuntimeError("no blobs seen, so no blob directory created") |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
85 # XXX should escape ":" for windows |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
86 return os.path.join(self.blobdir, "blob-" + blobid) |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
87 |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
88 def checkpoint_handler(self, cmd): |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
89 # This command means nothing to us |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
90 pass |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
91 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
92 def committish_rev(self, committish): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
93 if committish.startswith(":"): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
94 return self.mark_map[committish] |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
95 else: |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
96 return self.branch_map[committish] |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
97 |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
98 def commit_handler(self, cmd): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
99 if cmd.ref == "refs/heads/TAG.FIXUP": |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
100 #self.tag_back_map[cmd.mark] == first_parent |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
101 commit_handler = hgechoprocessor.HgEchoCommitHandler(cmd, self.ui, self.repo, **self.opts) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
102 commit_handler.process() |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
103 return |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
104 if cmd.from_: |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
105 first_parent = self.committish_rev(cmd.from_) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
106 else: |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
107 first_parent = self.branch_map.get(cmd.ref, nullrev) |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
108 #self.ui.write("First parent: %s\n" % first_parent) |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
109 # Update to the first parent |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
110 mercurial.hg.clean(self.repo, self.repo.lookup(first_parent)) |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
111 #self.ui.write("Bing\n") |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
112 if cmd.merges: |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
113 #self.ui.write("foo") |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
114 if len(cmd.merges) > 1: |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
115 raise NotImplementedError("Can't handle more than two parents") |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
116 second_parent = self.committish_rev(cmd.merges[0]) |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
117 #self.ui.write("Second parent: %s\n" % second_parent) |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
118 mercurial.commands.debugsetparents(self.ui, self.repo, |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
119 first_parent, second_parent) |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
120 #self.ui.write("Bing\n") |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
121 if cmd.ref == "refs/heads/master": |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
122 branch = "default" |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
123 else: |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
124 branch = cmd.ref[len("refs/heads/"):] |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
125 #self.ui.write("Branch: %s\n" % branch) |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
126 self.repo.dirstate.setbranch(branch) |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
127 #self.ui.write("Bing\n") |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
128 #print "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
129 commit_handler = HgImportCommitHandler( |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
130 self, cmd, self.ui, self.repo, **self.opts) |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
131 commit_handler.process() |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
132 #print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
133 #self.ui.write(cmd.dump_str(verbose=True)) |
|
16
234128693c29
Format the commit user as Full Name <user@example.com>
Paul Aurich <paul@darkrain42.org>
parents:
15
diff
changeset
|
134 |
|
234128693c29
Format the commit user as Full Name <user@example.com>
Paul Aurich <paul@darkrain42.org>
parents:
15
diff
changeset
|
135 # in case we are converting from git or bzr, prefer author but |
|
234128693c29
Format the commit user as Full Name <user@example.com>
Paul Aurich <paul@darkrain42.org>
parents:
15
diff
changeset
|
136 # fallback to committer (committer is required, author is |
|
234128693c29
Format the commit user as Full Name <user@example.com>
Paul Aurich <paul@darkrain42.org>
parents:
15
diff
changeset
|
137 # optional) |
|
234128693c29
Format the commit user as Full Name <user@example.com>
Paul Aurich <paul@darkrain42.org>
parents:
15
diff
changeset
|
138 userinfo = cmd.author or cmd.committer |
|
234128693c29
Format the commit user as Full Name <user@example.com>
Paul Aurich <paul@darkrain42.org>
parents:
15
diff
changeset
|
139 user = "%s <%s>" % (userinfo[0], userinfo[1]) |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
140 |
|
37
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
141 # Blech: have to monkeypatch mercurial.encoding to ensure that |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
142 # everything under rawcommit() assumes the same encoding, |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
143 # regardless of current locale. |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
144 from mercurial import encoding |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
145 encoding.encoding = "UTF-8" |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
146 |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
147 files = commit_handler.filelist() |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
148 assert type(cmd.message) is unicode |
|
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
149 text = cmd.message.encode("utf-8") # XXX cmd.message is unicode |
|
34
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
150 date = self.convert_date(userinfo) |
|
08e2157aaa9a
Remove local fork of bzr-fastimport; use my fastimport library instead.
Greg Ward <greg-hg@gerg.ca>
parents:
33
diff
changeset
|
151 node = self.repo.rawcommit( |
|
37
513449a88de2
Handle non-ASCII input correctly (assuming UTF-8 encoding).
Greg Ward <greg-hg@gerg.ca>
parents:
34
diff
changeset
|
152 files=files, text=text, user=user, date=date) |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
153 rev = self.repo.changelog.rev(node) |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
154 if cmd.mark is not None: |
|
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
155 self.mark_map[":" + cmd.mark] = rev |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
156 self.branch_map[cmd.ref] = rev |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
157 self.ui.write("Done commit of rev %d\n" % rev) |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
158 #self.ui.write("%s\n" % self.mark_map) |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
159 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
160 def convert_date(self, c): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
161 res = (int(c[2]), int(c[3])) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
162 #print c, res |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
163 #print type((0, 0)), type(res), len(res), type(res) is type((0, 0)) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
164 #if type(res) is type((0, 0)) and len(res) == 2: |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
165 # print "go for it" |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
166 #return res |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
167 return "%d %d" % res |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
168 |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
169 def reset_handler(self, cmd): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
170 if cmd.from_ is not None: |
| 15 | 171 self.branch_map[cmd.ref] = self.committish_rev(cmd.from_) |
| 172 | |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
173 def tag_handler(self, cmd): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
174 # self.tag_map[cmd.id] = self.tag_back_map[cmd.from_] |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
175 pass |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
176 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
177 class HgImportCommitHandler(processor.CommitHandler): |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
178 |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
179 def __init__(self, parent, command, ui, repo, **opts): |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
180 self.parent = parent # HgImportProcessor running the show |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
181 self.command = command |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
182 self.ui = ui |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
183 self.repo = repo |
|
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
184 self.opts = opts |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
185 self.files = set() |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
186 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
187 def _make_container(self, path): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
188 if '/' in path: |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
189 d = os.path.dirname(path) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
190 if not os.path.isdir(d): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
191 os.makedirs(d) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
192 |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
193 def modify_handler(self, filecmd): |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
194 #print "============================" + filecmd.path |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
195 # FIXME: handle mode |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
196 self.files.add(filecmd.path) |
|
10
18c1e7ac0012
Fix so "hg -R <repo> fastimport ..." works
Greg Ward <greg-hg@gerg.ca>
parents:
3
diff
changeset
|
197 fullpath = os.path.join(self.repo.root, filecmd.path) |
|
18c1e7ac0012
Fix so "hg -R <repo> fastimport ..." works
Greg Ward <greg-hg@gerg.ca>
parents:
3
diff
changeset
|
198 self._make_container(fullpath) |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
199 #print "made dirs, writing file" |
|
11
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
200 if filecmd.dataref: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
201 # reference to a blob that has already appeared in the stream |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
202 fn = self.parent.getblobfilename(filecmd.dataref) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
203 if os.path.exists(fullpath): |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
204 os.remove(fullpath) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
205 try: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
206 os.link(fn, fullpath) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
207 except OSError, err: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
208 if err.errno == errno.ENOENT: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
209 # if this happens, it's a problem in the fast-import |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
210 # stream |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
211 raise util.Abort("bad blob ref %r (no such file %s)" |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
212 % (filecmd.dataref, fn)) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
213 else: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
214 # anything else is a bug in this extension |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
215 # (cross-device move, permissions, etc.) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
216 raise |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
217 elif filecmd.data: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
218 f = open(fullpath, "w") |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
219 f.write(filecmd.data) |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
220 f.close() |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
221 else: |
|
9e9c215fcbd8
Handle blobs in the fast-import stream.
Greg Ward <greg-hg@gerg.ca>
parents:
10
diff
changeset
|
222 raise RuntimeError("either filecmd.dataref or filecmd.data must be set") |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
223 #print self.repo.add([filecmd.path]) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
224 #print "Done:", filecmd.path |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
225 |
|
3
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
226 def delete_handler(self, filecmd): |
|
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
227 self.files.add(filecmd.path) |
|
24c600e5cb71
fixes to file deletes, second parents, mark handling
Paul Crowley <paul@lshift.net>
parents:
1
diff
changeset
|
228 self.repo.remove([filecmd.path], unlink=True) |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
229 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
230 #def copy_handler(self, filecmd): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
231 # self.files.add(filecmd.path) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
232 # """Handle a filecopy command.""" |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
233 # self.ui.write("Cmd: %s\n" % repr(filecmd)) |
|
0
d107c6d36780
Add the start of the hg fastimport command
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
234 |
|
1
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
235 #def rename_handler(self, filecmd): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
236 # self.files.add(filecmd.path) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
237 # """Handle a filerename command.""" |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
238 # self.ui.write("Cmd: %s\n" % repr(filecmd)) |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
239 |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
240 def filelist(self): |
|
9461f5c3a67c
Actually imports something - incredible!
Paul Crowley <paul@lshift.net>
parents:
0
diff
changeset
|
241 return list(self.files) |
