Fix data handling
[fast-export.git] / README.md
blob2419a4f8a086244e38fe122016fa4cd0f795fa69
1 hg-fast-export.sh - mercurial to git converter using git-fast-import
2 =========================================================================
4 Legal
5 -----
7 Most hg-* scripts are licensed under the [MIT license] and were written
8 by Rocco Rutte <pdmef@gmx.net> with hints and help from the git list and
9 \#mercurial on freenode. hg-reset.py is licensed under GPLv2 since it
10 copies some code from the mercurial sources.
12 The current maintainer is Frej Drejhammar <frej.drejhammar@gmail.com>.
14 [MIT license]: http://www.opensource.org/licenses/mit-license.php
16 Support
17 -------
19 If you have problems with hg-fast-export or have found a bug, please
20 create an issue at the [github issue tracker]. Before creating a new
21 issue, check that your problem has not already been addressed in an
22 already closed issue. Do not contact the maintainer directly unless
23 you want to report a security bug. That way the next person having the
24 same problem can benefit from the time spent solving the problem the
25 first time.
27 [github issue tracker]: https://github.com/frej/fast-export/issues
29 System Requirements
30 -------------------
32 This project depends on Python 2.7 or 3.5+, and the Mercurial >= 4.6
33 package (>= 5.2, if Python 3.5+). If Python is not installed, install
34 it before proceeding. The Mercurial package can be installed with `pip
35 install mercurial`.
37 On windows the bash that comes with "Git for Windows" is known to work
38 well.
40 Usage
41 -----
43 Using hg-fast-export is quite simple for a mercurial repository <repo>:
45 ```
46 mkdir repo-git # or whatever
47 cd repo-git
48 git init
49 hg-fast-export.sh -r <local-repo>
50 git checkout HEAD
51 ```
53 Please note that hg-fast-export does not automatically check out the
54 newly imported repository. You probably want to follow up the import
55 with a `git checkout`-command.
57 Incremental imports to track hg repos is supported, too.
59 Using hg-reset it is quite simple within a git repository that is
60 hg-fast-export'ed from mercurial:
62 ```
63 hg-reset.sh -R <revision>
64 ```
66 will give hints on which branches need adjustment for starting over
67 again.
69 When a mercurial repository does not use utf-8 for encoding author
70 strings and commit messages the `-e <encoding>` command line option
71 can be used to force fast-export to convert incoming meta data from
72 <encoding> to utf-8. This encoding option is also applied to file names.
74 In some locales Mercurial uses different encodings for commit messages
75 and file names. In that case, you can use `--fe <encoding>` command line
76 option which overrides the -e option for file names.
78 As mercurial appears to be much less picky about the syntax of the
79 author information than git, an author mapping file can be given to
80 hg-fast-export to fix up malformed author strings. The file is
81 specified using the -A option. The file should contain lines of the
82 form `"<key>"="<value>"`. Inside the key and value strings, all escape
83 sequences understood by the python `unicode_escape` encoding are
84 supported; strings are otherwise assumed to be UTF8-encoded.
85 (Versions of fast-export prior to v171002 had a different syntax, the
86 old syntax can be enabled by the flag `--mappings-are-raw`.)
88 The example authors.map below will translate `User
89 <garbage<tab><user@example.com>` to `User <user@example.com>`.
91 ```
92 -- Start of authors.map --
93 "User <garbage\t<user@example.com>"="User <user@example.com>"
94 -- End of authors.map --
95 ```
97 If you have many Mercurial repositories, Chris J Billington's
98 [hg-export-tool] allows you to batch convert them.
100 Tag and Branch Naming
101 ---------------------
103 As Git and Mercurial have differ in what is a valid branch and tag
104 name the -B and -T options allow a mapping file to be specified to
105 rename branches and tags (respectively). The syntax of the mapping
106 file is the same as for the author mapping.
108 When the -B and -T flags are used, you will probably want to use the
109 -n flag to disable the built-in (broken in many cases) sanitizing of
110 branch/tag names. In the future -n will become the default, but in
111 order to not break existing incremental conversions, the default
112 remains with the old behavior.
114 By default, the `default` mercurial branch is renamed to the `master` 
115 branch on git. If your mercurial repo contains both `default` and 
116 `master` branches, you'll need to override this behavior. Use
117 `-M <newName>` to specify what name to give the `default` branch.
119 Content filtering
120 -----------------
122 hg-fast-export supports filtering the content of exported files.
123 The filter is supplied to the --filter-contents option. hg-fast-export
124 runs the filter for each exported file, pipes its content to the filter's
125 standard input, and uses the filter's standard output in place
126 of the file's original content. The prototypical use of this feature
127 is to convert line endings in text files from CRLF to git's preferred LF:
130 -- Start of crlf-filter.sh --
131 #!/bin/sh
132 # $1 = pathname of exported file relative to the root of the repo
133 # $2 = Mercurial's hash of the file
134 # $3 = "1" if Mercurial reports the file as binary, otherwise "0"
136 if [ "$3" == "1" ]; then cat; else dos2unix -q; fi
137 # -q option in call to dos2unix allows to avoid returning an
138 # error code when handling non-ascii based text files (like UTF-16
139 # encoded text files)
140 -- End of crlf-filter.sh --
144 Plugins
145 -----------------
147 hg-fast-export supports plugins to manipulate the file data and commit
148 metadata. The plugins are enabled with the --plugin option. The value
149 of said option is a plugin name (by folder in the plugins directory),
150 and optionally, and equals-sign followed by an initialization string.
152 There is a readme accompanying each of the bundled plugins, with a
153 description of the usage. To create a new plugin, one must simply
154 add a new folder under the `plugins` directory, with the name of the
155 new plugin. Inside, there must be an `__init__.py` file, which contains
156 at a minimum:
159 def build_filter(args):
160     return Filter(args)
162 class Filter:
163     def __init__(self, args):
164         pass
165         #Or don't pass, if you want to do some init code here
168 Beyond the boilerplate initialization, you can see the two different
169 defined filter methods in the [dos2unix](./plugins/dos2unix) and
170 [branch_name_in_commit](./plugins/branch_name_in_commit) plugins.
173 commit_data = {'branch': branch, 'parents': parents, 'author': author, 'desc': desc, 'revision': revision, 'hg_hash': hg_hash, 'committer': 'committer', 'extra': extra}
175 def commit_message_filter(self,commit_data):
177 The `commit_message_filter` method is called for each commit, after parsing
178 from hg, but before outputting to git. The dictionary `commit_data` contains the
179 above attributes about the commit, and can be modified by any filter. The
180 values in the dictionary after filters have been run are used to create the git
181 commit.
184 file_data = {'filename':filename,'file_ctx':file_ctx,'d':d}
186 def file_data_filter(self,file_data):
188 The `file_data_filter` method is called for each file within each commit.
189 The dictionary `file_data` contains the above attributes about the file, and
190 can be modified by any filter. `file_ctx` is the filecontext from the
191 mercurial python library.  After all filters have been run, the values
192 are used to add the file to the git commit.
194 Submodules
195 ----------
196 See README-SUBMODULES.md for how to convert subrepositories into git
197 submodules.
199 Notes/Limitations
200 -----------------
202 hg-fast-export supports multiple branches but only named branches with
203 exactly one head each. Otherwise commits to the tip of these heads
204 within the branch will get flattened into merge commits. There are a
205 few options to deal with this:
206 1. Chris J Billington's [hg-export-tool] can help you to handle branches with
207    duplicate heads.
208 2. Use the [head2branch plugin](./plugins/head2branch) to create a new named
209    branch from an unnamed head.
210 3. You can ignore unnamed heads with the `--ignore-unnamed-heads` option, which
211    is appropriate in situations such as the extra heads being close commits
212    (abandoned, unmerged changes).
214 hg-fast-export will ignore any files or directories tracked by mercurial
215 called `.git`, and will print a warning if it encounters one. Git cannot
216 track such files or directories. This is not to be confused with submodules,
217 which are described in README-SUBMODULES.md.
219 As each git-fast-import run creates a new pack file, it may be
220 required to repack the repository quite often for incremental imports
221 (especially when importing a small number of changesets per
222 incremental import).
224 The way the hg API and remote access protocol is designed it is not
225 possible to use hg-fast-export on remote repositories
226 (http/ssh). First clone the repository, then convert it.
228 Design
229 ------
231 hg-fast-export was designed in a way that doesn't require a 2-pass
232 mechanism or any prior repository analysis: it just feeds what it
233 finds into git-fast-import. This also implies that it heavily relies
234 on strictly linear ordering of changesets from hg, i.e. its
235 append-only storage model so that changesets hg-fast-export already
236 saw never get modified.
238 Submitting Patches
239 ------------------
241 Please create a pull request at
242 [Github](https://github.com/frej/fast-export/pulls) to submit patches.
244 When submitting a patch make sure the commits in your pull request:
246 * Have good commit messages
248   Please read Chris Beams' blog post [How to Write a Git Commit
249   Message](https://chris.beams.io/posts/git-commit/) on how to write a
250   good commit message. Although the article recommends at most 50
251   characters for the subject, up to 72 characters are frequently
252   accepted for fast-export.
254 * Adhere to good [commit
255 hygiene](http://www.ericbmerritt.com/2011/09/21/commit-hygiene-and-git.html)
257   When developing a pull request for hg-fast-export, base your work on
258   the current `master` branch and rebase your work if it no longer can
259   be merged into the current `master` without conflicts. Never merge
260   `master` into your development branch, rebase if your work needs
261   updates from `master`.
263   When a pull request is modified due to review feedback, please
264   incorporate the changes into the proper commit. A good reference on
265   how to modify history is in the [Pro Git book, Section
266   7.6](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History).
268 Please do not submit a pull request if you are not willing to spend
269 the time required to address review comments or revise the patch until
270 it follows the guidelines above. A _take it or leave it_ approach to
271 contributing wastes both your and the maintainer's time.
273 Frequent Problems
274 =================
276 * git fast-import crashes with: `error: cannot lock ref 'refs/heads/...`
278   Branch names in git behave as file names (as they are just files and
279   sub-directories under `refs/heads/`, and a path cannot name both a
280   file and a directory, i.e. the branches `a` and `a/b` can never
281   exist at the same time in a git repo.
283   Use a mapping file to rename the troublesome branch names.
285 * `Branch [<branch-name>] modified outside hg-fast-export` but I have
286   not touched the repo!
288   If you are running fast-export on a case-preserving but
289   case-insensitive file system (Windows and OSX), this will make git
290   treat `A` and `a` as the same branch. The solution is to use a
291   mapping file to rename branches which only differ in case.
293 * My mapping file does not seem to work when I rename the branch `git
294   fast-import` crashes on!
296   fast-export (imperfectly) mangles branch names it thinks won't be
297   valid. The mechanism cannot be removed as it would break already
298   existing incremental imports that expects it. When fast export
299   mangles a name, it prints out a warning of the form `Warning:
300   sanitized branch [<unmangled>] to [<mangled>]`. If `git fast-import`
301   crashes on `<mangled>`, you need to put `<unmangled>` into the
302   mapping file.
304 * fast-import mangles valid git branch names which I have remapped!
306   Use the `-n` flag to hg-fast-export.sh.
308 * `git status` reports that all files are scheduled for deletion after
309   the initial conversion.
311   By design fast export does not touch your working directory, so to
312   git it looks like you have deleted all files, when in fact they have
313   never been checked out. Just do a checkout of the branch you want.
315 * `Error: repository has at least one unnamed head: hg r<N>`
317   By design, hg-fast-export cannot deal with extra heads on a branch.
318   There are a few options depending on whether the extra heads are
319   in-use/open or normally closed. See [Notes/Limitations](#noteslimitations)
320   section for more details.
322 [hg-export-tool]: https://github.com/chrisjbillington/hg-export-tool