Trivial simplification
[fast-export.git] / README.md
blob30df3a4d91d6f85bceeb384e15f9fd4eff54378d
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 git init repo-git # or whatever
47 cd repo-git
48 hg-fast-export.sh -r <local-repo>
49 git checkout
50 ```
52 Please note that hg-fast-export does not automatically check out the
53 newly imported repository. You probably want to follow up the import
54 with a `git checkout`-command.
56 Incremental imports to track hg repos is supported, too.
58 Using hg-reset it is quite simple within a git repository that is
59 hg-fast-export'ed from mercurial:
61 ```
62 hg-reset.sh -R <revision>
63 ```
65 will give hints on which branches need adjustment for starting over
66 again.
68 When a mercurial repository does not use utf-8 for encoding author
69 strings and commit messages the `-e <encoding>` command line option
70 can be used to force fast-export to convert incoming meta data from
71 <encoding> to utf-8. This encoding option is also applied to file names.
73 In some locales Mercurial uses different encodings for commit messages
74 and file names. In that case, you can use `--fe <encoding>` command line
75 option which overrides the -e option for file names.
77 As mercurial appears to be much less picky about the syntax of the
78 author information than git, an author mapping file can be given to
79 hg-fast-export to fix up malformed author strings. The file is
80 specified using the -A option. The file should contain lines of the
81 form `"<key>"="<value>"`. Inside the key and value strings, all escape
82 sequences understood by the python `unicode_escape` encoding are
83 supported; strings are otherwise assumed to be UTF8-encoded.
84 (Versions of fast-export prior to v171002 had a different syntax, the
85 old syntax can be enabled by the flag `--mappings-are-raw`.)
87 The example authors.map below will translate `User
88 <garbage<tab><user@example.com>` to `User <user@example.com>`.
90 ```
91 -- Start of authors.map --
92 "User <garbage\t<user@example.com>"="User <user@example.com>"
93 -- End of authors.map --
94 ```
96 If you have many Mercurial repositories, Chris J Billington's
97 [hg-export-tool] allows you to batch convert them.
99 Tag and Branch Naming
100 ---------------------
102 As Git and Mercurial have differ in what is a valid branch and tag
103 name the -B and -T options allow a mapping file to be specified to
104 rename branches and tags (respectively). The syntax of the mapping
105 file is the same as for the author mapping.
107 When the -B and -T flags are used, you will probably want to use the
108 -n flag to disable the built-in (broken in many cases) sanitizing of
109 branch/tag names. In the future -n will become the default, but in
110 order to not break existing incremental conversions, the default
111 remains with the old behavior.
113 By default, the `default` mercurial branch is renamed to the `master` 
114 branch on git. If your mercurial repo contains both `default` and 
115 `master` branches, you'll need to override this behavior. Use
116 `-M <newName>` to specify what name to give the `default` branch.
118 Content filtering
119 -----------------
121 hg-fast-export supports filtering the content of exported files.
122 The filter is supplied to the --filter-contents option. hg-fast-export
123 runs the filter for each exported file, pipes its content to the filter's
124 standard input, and uses the filter's standard output in place
125 of the file's original content. The prototypical use of this feature
126 is to convert line endings in text files from CRLF to git's preferred LF:
129 -- Start of crlf-filter.sh --
130 #!/bin/sh
131 # $1 = pathname of exported file relative to the root of the repo
132 # $2 = Mercurial's hash of the file
133 # $3 = "1" if Mercurial reports the file as binary, otherwise "0"
135 if [ "$3" == "1" ]; then cat; else dos2unix -q; fi
136 # -q option in call to dos2unix allows to avoid returning an
137 # error code when handling non-ascii based text files (like UTF-16
138 # encoded text files)
139 -- End of crlf-filter.sh --
143 Plugins
144 -----------------
146 hg-fast-export supports plugins to manipulate the file data and commit
147 metadata. The plugins are enabled with the --plugin option. The value
148 of said option is a plugin name (by folder in the plugins directory),
149 and optionally, and equals-sign followed by an initialization string.
151 There is a readme accompanying each of the bundled plugins, with a
152 description of the usage. To create a new plugin, one must simply
153 add a new folder under the `plugins` directory, with the name of the
154 new plugin. Inside, there must be an `__init__.py` file, which contains
155 at a minimum:
158 def build_filter(args):
159     return Filter(args)
161 class Filter:
162     def __init__(self, args):
163         pass
164         #Or don't pass, if you want to do some init code here
167 Beyond the boilerplate initialization, you can see the two different
168 defined filter methods in the [dos2unix](./plugins/dos2unix) and
169 [branch_name_in_commit](./plugins/branch_name_in_commit) plugins.
172 commit_data = {'branch': branch, 'parents': parents, 'author': author, 'desc': desc, 'revision': revision, 'hg_hash': hg_hash, 'committer': 'committer', 'extra': extra}
174 def commit_message_filter(self,commit_data):
176 The `commit_message_filter` method is called for each commit, after parsing
177 from hg, but before outputting to git. The dictionary `commit_data` contains the
178 above attributes about the commit, and can be modified by any filter. The
179 values in the dictionary after filters have been run are used to create the git
180 commit.
183 file_data = {'filename':filename,'file_ctx':file_ctx,'d':d}
185 def file_data_filter(self,file_data):
187 The `file_data_filter` method is called for each file within each commit.
188 The dictionary `file_data` contains the above attributes about the file, and
189 can be modified by any filter. `file_ctx` is the filecontext from the
190 mercurial python library.  After all filters have been run, the values
191 are used to add the file to the git commit.
193 Submodules
194 ----------
195 See README-SUBMODULES.md for how to convert subrepositories into git
196 submodules.
198 Notes/Limitations
199 -----------------
201 hg-fast-export supports multiple branches but only named branches with
202 exactly one head each. Otherwise commits to the tip of these heads
203 within the branch will get flattened into merge commits. There are a
204 few options to deal with this:
205 1. Chris J Billington's [hg-export-tool] can help you to handle branches with
206    duplicate heads.
207 2. Use the [head2branch plugin](./plugins/head2branch) to create a new named
208    branch from an unnamed head.
209 3. You can ignore unnamed heads with the `--ignore-unnamed-heads` option, which
210    is appropriate in situations such as the extra heads being close commits
211    (abandoned, unmerged changes).
213 hg-fast-export will ignore any files or directories tracked by mercurial
214 called `.git`, and will print a warning if it encounters one. Git cannot
215 track such files or directories. This is not to be confused with submodules,
216 which are described in README-SUBMODULES.md.
218 As each git-fast-import run creates a new pack file, it may be
219 required to repack the repository quite often for incremental imports
220 (especially when importing a small number of changesets per
221 incremental import).
223 The way the hg API and remote access protocol is designed it is not
224 possible to use hg-fast-export on remote repositories
225 (http/ssh). First clone the repository, then convert it.
227 Design
228 ------
230 hg-fast-export was designed in a way that doesn't require a 2-pass
231 mechanism or any prior repository analysis: it just feeds what it
232 finds into git-fast-import. This also implies that it heavily relies
233 on strictly linear ordering of changesets from hg, i.e. its
234 append-only storage model so that changesets hg-fast-export already
235 saw never get modified.
237 Submitting Patches
238 ------------------
240 Please create a pull request at
241 [Github](https://github.com/frej/fast-export/pulls) to submit patches.
243 When submitting a patch make sure the commits in your pull request:
245 * Have good commit messages
247   Please read Chris Beams' blog post [How to Write a Git Commit
248   Message](https://chris.beams.io/posts/git-commit/) on how to write a
249   good commit message. Although the article recommends at most 50
250   characters for the subject, up to 72 characters are frequently
251   accepted for fast-export.
253 * Adhere to good [commit
254 hygiene](http://www.ericbmerritt.com/2011/09/21/commit-hygiene-and-git.html)
256   When developing a pull request for hg-fast-export, base your work on
257   the current `master` branch and rebase your work if it no longer can
258   be merged into the current `master` without conflicts. Never merge
259   `master` into your development branch, rebase if your work needs
260   updates from `master`.
262   When a pull request is modified due to review feedback, please
263   incorporate the changes into the proper commit. A good reference on
264   how to modify history is in the [Pro Git book, Section
265   7.6](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History).
267 Please do not submit a pull request if you are not willing to spend
268 the time required to address review comments or revise the patch until
269 it follows the guidelines above. A _take it or leave it_ approach to
270 contributing wastes both your and the maintainer's time.
272 Frequent Problems
273 =================
275 * git fast-import crashes with: `error: cannot lock ref 'refs/heads/...`
277   Branch names in git behave as file names (as they are just files and
278   sub-directories under `refs/heads/`, and a path cannot name both a
279   file and a directory, i.e. the branches `a` and `a/b` can never
280   exist at the same time in a git repo.
282   Use a mapping file to rename the troublesome branch names.
284 * `Branch [<branch-name>] modified outside hg-fast-export` but I have
285   not touched the repo!
287   If you are running fast-export on a case-preserving but
288   case-insensitive file system (Windows and OSX), this will make git
289   treat `A` and `a` as the same branch. The solution is to use a
290   mapping file to rename branches which only differ in case.
292 * My mapping file does not seem to work when I rename the branch `git
293   fast-import` crashes on!
295   fast-export (imperfectly) mangles branch names it thinks won't be
296   valid. The mechanism cannot be removed as it would break already
297   existing incremental imports that expects it. When fast export
298   mangles a name, it prints out a warning of the form `Warning:
299   sanitized branch [<unmangled>] to [<mangled>]`. If `git fast-import`
300   crashes on `<mangled>`, you need to put `<unmangled>` into the
301   mapping file.
303 * fast-import mangles valid git branch names which I have remapped!
305   Use the `-n` flag to hg-fast-export.sh.
307 * `git status` reports that all files are scheduled for deletion after
308   the initial conversion.
310   By design fast export does not touch your working directory, so to
311   git it looks like you have deleted all files, when in fact they have
312   never been checked out. Just do a checkout of the branch you want.
314 * `Error: repository has at least one unnamed head: hg r<N>`
316   By design, hg-fast-export cannot deal with extra heads on a branch.
317   There are a few options depending on whether the extra heads are
318   in-use/open or normally closed. See [Notes/Limitations](#noteslimitations)
319   section for more details.
321 [hg-export-tool]: https://github.com/chrisjbillington/hg-export-tool