Add branch_name_in_commit plugin
[fast-export.git] / README.md
blob34c6bf0c936ea877b45cb321ad923ea103629506
1 hg-fast-export.(sh|py) - mercurial to git converter using git-fast-import
2 =========================================================================
4 Legal
5 -----
7 Most hg-* scripts are licensed under the [MIT license]
8 (http://www.opensource.org/licenses/mit-license.php) and were written
9 by Rocco Rutte <pdmef@gmx.net> with hints and help from the git list and
10 \#mercurial on freenode. hg-reset.py is licensed under GPLv2 since it
11 copies some code from the mercurial sources.
13 The current maintainer is Frej Drejhammar <frej.drejhammar@gmail.com>.
15 Support
16 -------
18 If you have problems with hg-fast-export or have found a bug, please
19 create an issue at the [github issue tracker]
20 (https://github.com/frej/fast-export/issues). 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 System Requirements
28 -------------------
30 This project depends on Python 2.7 and the Mercurial 4.6 package. If
31 Python is not installed, install it before proceeding. The Mercurial
32 package can be installed with `pip install mercurial`.
34 If you're on Windows, run the following commands in git bash (Git for
35 Windows).
37 Usage
38 -----
40 Using hg-fast-export is quite simple for a mercurial repository <repo>:
42 ```
43 mkdir repo-git # or whatever
44 cd repo-git
45 git init
46 hg-fast-export.sh -r <local-repo>
47 git checkout HEAD
48 ```
50 Please note that hg-fast-export does not automatically check out the
51 newly imported repository. You probably want to follow up the import
52 with a `git checkout`-command.
54 Incremental imports to track hg repos is supported, too.
56 Using hg-reset it is quite simple within a git repository that is
57 hg-fast-export'ed from mercurial:
59 ```
60 hg-reset.sh -R <revision>
61 ```
63 will give hints on which branches need adjustment for starting over
64 again.
66 When a mercurial repository does not use utf-8 for encoding author
67 strings and commit messages the `-e <encoding>` command line option
68 can be used to force fast-export to convert incoming meta data from
69 <encoding> to utf-8. This encoding option is also applied to file names.
71 In some locales Mercurial uses different encodings for commit messages
72 and file names. In that case, you can use `--fe <encoding>` command line
73 option which overrides the -e option for file names.
75 As mercurial appears to be much less picky about the syntax of the
76 author information than git, an author mapping file can be given to
77 hg-fast-export to fix up malformed author strings. The file is
78 specified using the -A option. The file should contain lines of the
79 form `"<key>"="<value>"`. Inside the key and value strings, all escape
80 sequences understood by the python `string_escape` encoding are
81 supported. (Versions of fast-export prior to v171002 had a different
82 syntax, the old syntax can be enabled by the flag
83 `--mappings-are-raw`.)
85 The example authors.map below will translate `User
86 <garbage<tab><user@example.com>` to `User <user@example.com>`.
88 ```
89 -- Start of authors.map --
90 "User <garbage\t<user@example.com>"="User <user@example.com>"
91 -- End of authors.map --
92 ```
94 Tag and Branch Naming
95 ---------------------
97 As Git and Mercurial have differ in what is a valid branch and tag
98 name the -B and -T options allow a mapping file to be specified to
99 rename branches and tags (respectively). The syntax of the mapping
100 file is the same as for the author mapping.
102 Content filtering
103 -----------------
105 hg-fast-export supports filtering the content of exported files.
106 The filter is supplied to the --filter-contents option. hg-fast-export
107 runs the filter for each exported file, pipes its content to the filter's
108 standard input, and uses the filter's standard output in place
109 of the file's original content. The prototypical use of this feature
110 is to convert line endings in text files from CRLF to git's preferred LF:
113 -- Start of crlf-filter.sh --
114 #!/bin/sh
115 # $1 = pathname of exported file relative to the root of the repo
116 # $2 = Mercurial's hash of the file
117 # $3 = "1" if Mercurial reports the file as binary, otherwise "0"
119 if [ "$3" == "1" ]; then cat; else dos2unix; fi
120 -- End of crlf-filter.sh --
124 Plugins
125 -----------------
127 hg-fast-export supports plugins to manipulate the file data and commit
128 metadata. The plugins are enabled with the --plugin option. The value
129 of said option is a plugin name (by folder in the plugins directory),
130 and optionally, and equals-sign followed by an initialization string.
132 There is a readme accompanying each of the bundled plugins, with a
133 description of the usage. To create a new plugin, one must simply
134 add a new folder under the `plugins` directory, with the name of the
135 new plugin. Inside, there must be an `__init__.py` file, which contains
136 at a minimum:
139 def build_filter(args):
140     return Filter(args)
142 class Filter:
143     def __init__(self, args):
144         pass
145         #Or don't pass, if you want to do some init code here
148 Beyond the boilerplate initialization, you can see the two different
149 defined filter methods in the [dos2unix](./plugins/dos2unix) and
150 [branch_name_in_commit](./plugins/branch_name_in_commit) plugins.
153 commit_data = {'branch': branch, 'parents': parents, 'author': author, 'desc': desc}
155 def commit_message_filter(self,commit_data):
157 The `commit_message_filter` method is called for each commit, after parsing
158 from hg, but before outputting to git. The dictionary `commit_data` contains the
159 above attributes about the commit, and can be modified by any filter. The
160 values in the dictionary after filters have been run are used to create the git
161 commit.
164 file_data = {'filename':filename,'file_ctx':file_ctx,'d':d}
166 def file_data_filter(self,file_data):
168 The `file_data_filter` method is called for each file within each commit.
169 The dictionary `file_data` contains the above attributes about the file, and
170 can be modified by any filter. `file_ctx` is the filecontext from the
171 mercurial python library.  After all filters have been run, the values
172 are used to add the file to the git commit.
174 Notes/Limitations
175 -----------------
177 hg-fast-export supports multiple branches but only named branches with
178 exactly one head each. Otherwise commits to the tip of these heads
179 within the branch will get flattened into merge commits.
181 As each git-fast-import run creates a new pack file, it may be
182 required to repack the repository quite often for incremental imports
183 (especially when importing a small number of changesets per
184 incremental import).
186 The way the hg API and remote access protocol is designed it is not
187 possible to use hg-fast-export on remote repositories
188 (http/ssh). First clone the repository, then convert it.
190 Design
191 ------
193 hg-fast-export.py was designed in a way that doesn't require a 2-pass
194 mechanism or any prior repository analysis: if just feeds what it
195 finds into git-fast-import. This also implies that it heavily relies
196 on strictly linear ordering of changesets from hg, i.e. its
197 append-only storage model so that changesets hg-fast-export already
198 saw never get modified.
200 Submitting Patches
201 ------------------
203 Please use the issue-tracker at github
204 https://github.com/frej/fast-export to report bugs and submit
205 patches.