Debian Codegraph @

hgext/convert/__init__.py

raw
1 # convert.py Foreign SCM converter
2 #
3 # Copyright 2005-2007 Olivia Mackall <[email protected]>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 '''import revisions from foreign VCS repositories into Mercurial'''
9
10 from __future__ import annotations
11
12 from mercurial.i18n import _
13 from mercurial import registrar
14
15 from . import (
16 convcmd,
17 cvsps,
18 subversion,
19 )
20
21 cmdtable = {}
22 command = registrar.command(cmdtable)
23 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
24 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
25 # be specifying the version(s) of Mercurial they are tested with, or
26 # leave the attribute unspecified.
27 testedwith = b'ships-with-hg-core'
28
29 # Commands definition was moved elsewhere to ease demandload job.
30
31
32 @command(
33 b'convert',
34 [
35 (
36 b'',
37 b'authors',
38 b'',
39 _(
40 b'username mapping filename (DEPRECATED) (use --authormap instead)'
41 ),
42 _(b'FILE'),
43 ),
44 (b's', b'source-type', b'', _(b'source repository type'), _(b'TYPE')),
45 (
46 b'd',
47 b'dest-type',
48 b'',
49 _(b'destination repository type'),
50 _(b'TYPE'),
51 ),
52 (b'r', b'rev', [], _(b'import up to source revision REV'), _(b'REV')),
53 (
54 b'A',
55 b'authormap',
56 b'',
57 _(b'remap usernames using this file'),
58 _(b'FILE'),
59 ),
60 (
61 b'',
62 b'filemap',
63 b'',
64 _(b'remap file names using contents of file'),
65 _(b'FILE'),
66 ),
67 (
68 b'',
69 b'full',
70 None,
71 _(b'apply filemap changes by converting all files again'),
72 ),
73 (
74 b'',
75 b'splicemap',
76 b'',
77 _(b'splice synthesized history into place'),
78 _(b'FILE'),
79 ),
80 (
81 b'',
82 b'branchmap',
83 b'',
84 _(b'change branch names while converting'),
85 _(b'FILE'),
86 ),
87 (b'', b'branchsort', None, _(b'try to sort changesets by branches')),
88 (b'', b'datesort', None, _(b'try to sort changesets by date')),
89 (b'', b'sourcesort', None, _(b'preserve source changesets order')),
90 (b'', b'closesort', None, _(b'try to reorder closed revisions')),
91 ],
92 _(b'hg convert [OPTION]... SOURCE [DEST [REVMAP]]'),
93 norepo=True,
94 )
95 def convert(ui, src, dest=None, revmapfile=None, **opts):
96 """convert a foreign SCM repository to a Mercurial one.
97
98 Accepted source formats [identifiers]:
99
100 - Mercurial [hg]
101 - CVS [cvs]
102 - Darcs [darcs]
103 - git [git]
104 - Subversion [svn]
105 - Monotone [mtn]
106 - GNU Arch [gnuarch]
107 - Bazaar [bzr]
108 - Perforce [p4]
109
110 Accepted destination formats [identifiers]:
111
112 - Mercurial [hg]
113 - Subversion [svn] (history on branches is not preserved)
114
115 If no revision is given, all revisions will be converted.
116 Otherwise, convert will only import up to the named revision
117 (given in a format understood by the source).
118
119 If no destination directory name is specified, it defaults to the
120 basename of the source with ``-hg`` appended. If the destination
121 repository doesn't exist, it will be created.
122
123 By default, all sources except Mercurial will use --branchsort.
124 Mercurial uses --sourcesort to preserve original revision numbers
125 order. Sort modes have the following effects:
126
127 --branchsort convert from parent to child revision when possible,
128 which means branches are usually converted one after
129 the other. It generates more compact repositories.
130
131 --datesort sort revisions by date. Converted repositories have
132 good-looking changelogs but are often an order of
133 magnitude larger than the same ones generated by
134 --branchsort.
135
136 --sourcesort try to preserve source revisions order, only
137 supported by Mercurial sources.
138
139 --closesort try to move closed revisions as close as possible
140 to parent branches, only supported by Mercurial
141 sources.
142
143 If ``REVMAP`` isn't given, it will be put in a default location
144 (``<dest>/.hg/shamap`` by default). The ``REVMAP`` is a simple
145 text file that maps each source commit ID to the destination ID
146 for that revision, like so::
147
148 <source ID> <destination ID>
149
150 If the file doesn't exist, it's automatically created. It's
151 updated on each commit copied, so :hg:`convert` can be interrupted
152 and can be run repeatedly to copy new commits.
153
154 The authormap is a simple text file that maps each source commit
155 author to a destination commit author. It is handy for source SCMs
156 that use unix logins to identify authors (e.g.: CVS). One line per
157 author mapping and the line format is::
158
159 source author = destination author
160
161 Empty lines and lines starting with a ``#`` are ignored.
162
163 The filemap is a file that allows filtering and remapping of files
164 and directories. Each line can contain one of the following
165 directives::
166
167 include path/to/file-or-dir
168
169 exclude path/to/file-or-dir
170
171 rename path/to/source path/to/destination
172
173 Comment lines start with ``#``. A specified path matches if it
174 equals the full relative name of a file or one of its parent
175 directories. The ``include`` or ``exclude`` directive with the
176 longest matching path applies, so line order does not matter.
177
178 The ``include`` directive causes a file, or all files under a
179 directory, to be included in the destination repository. The default
180 if there are no ``include`` statements is to include everything.
181 If there are any ``include`` statements, nothing else is included.
182 The ``exclude`` directive causes files or directories to
183 be omitted. The ``rename`` directive renames a file or directory if
184 it is converted. To rename from a subdirectory into the root of
185 the repository, use ``.`` as the path to rename to.
186
187 ``--full`` will make sure the converted changesets contain exactly
188 the right files with the right content. It will make a full
189 conversion of all files, not just the ones that have
190 changed. Files that already are correct will not be changed. This
191 can be used to apply filemap changes when converting
192 incrementally. This is currently only supported for Mercurial and
193 Subversion.
194
195 The splicemap is a file that allows insertion of synthetic
196 history, letting you specify the parents of a revision. This is
197 useful if you want to e.g. give a Subversion merge two parents, or
198 graft two disconnected series of history together. Each entry
199 contains a key, followed by a space, followed by one or two
200 comma-separated values::
201
202 key parent1, parent2
203
204 The key is the revision ID in the source
205 revision control system whose parents should be modified (same
206 format as a key in .hg/shamap). The values are the revision IDs
207 (in either the source or destination revision control system) that
208 should be used as the new parents for that node. For example, if
209 you have merged "release-1.0" into "trunk", then you should
210 specify the revision on "trunk" as the first parent and the one on
211 the "release-1.0" branch as the second.
212
213 The branchmap is a file that allows you to rename a branch when it is
214 being brought in from whatever external repository. When used in
215 conjunction with a splicemap, it allows for a powerful combination
216 to help fix even the most badly mismanaged repositories and turn them
217 into nicely structured Mercurial repositories. The branchmap contains
218 lines of the form::
219
220 original_branch_name new_branch_name
221
222 where "original_branch_name" is the name of the branch in the
223 source repository, and "new_branch_name" is the name of the branch
224 is the destination repository. No whitespace is allowed in the new
225 branch name. This can be used to (for instance) move code in one
226 repository from "default" to a named branch.
227
228 Mercurial Source
229 ################
230
231 The Mercurial source recognizes the following configuration
232 options, which you can set on the command line with ``--config``:
233
234 :convert.hg.ignoreerrors: ignore integrity errors when reading.
235 Use it to fix Mercurial repositories with missing revlogs, by
236 converting from and to Mercurial. Default is False.
237
238 :convert.hg.saverev: store original revision ID in changeset
239 (forces target IDs to change). It takes a boolean argument and
240 defaults to False.
241
242 :convert.hg.startrev: specify the initial Mercurial revision.
243 The default is 0.
244
245 :convert.hg.revs: revset specifying the source revisions to convert.
246
247 Bazaar Source
248 #############
249
250 The following options can be used with ``--config``:
251
252 :convert.bzr.saverev: whether to store the original Bazaar commit ID in
253 the metadata of the destination commit. The default is True.
254
255 CVS Source
256 ##########
257
258 CVS source will use a sandbox (i.e. a checked-out copy) from CVS
259 to indicate the starting point of what will be converted. Direct
260 access to the repository files is not needed, unless of course the
261 repository is ``:local:``. The conversion uses the top level
262 directory in the sandbox to find the CVS repository, and then uses
263 CVS rlog commands to find files to convert. This means that unless
264 a filemap is given, all files under the starting directory will be
265 converted, and that any directory reorganization in the CVS
266 sandbox is ignored.
267
268 The following options can be used with ``--config``:
269
270 :convert.cvsps.cache: Set to False to disable remote log caching,
271 for testing and debugging purposes. Default is True.
272
273 :convert.cvsps.fuzz: Specify the maximum time (in seconds) that is
274 allowed between commits with identical user and log message in
275 a single changeset. When very large files were checked in as
276 part of a changeset then the default may not be long enough.
277 The default is 60.
278
279 :convert.cvsps.logencoding: Specify encoding name to be used for
280 transcoding CVS log messages. Multiple encoding names can be
281 specified as a list (see :hg:`help config.Syntax`), but only
282 the first acceptable encoding in the list is used per CVS log
283 entries. This transcoding is executed before cvslog hook below.
284
285 :convert.cvsps.mergeto: Specify a regular expression to which
286 commit log messages are matched. If a match occurs, then the
287 conversion process will insert a dummy revision merging the
288 branch on which this log message occurs to the branch
289 indicated in the regex. Default is ``{{mergetobranch
290 ([-\\w]+)}}``
291
292 :convert.cvsps.mergefrom: Specify a regular expression to which
293 commit log messages are matched. If a match occurs, then the
294 conversion process will add the most recent revision on the
295 branch indicated in the regex as the second parent of the
296 changeset. Default is ``{{mergefrombranch ([-\\w]+)}}``
297
298 :convert.localtimezone: use local time (as determined by the TZ
299 environment variable) for changeset date/times. The default
300 is False (use UTC).
301
302 :hooks.cvslog: Specify a Python function to be called at the end of
303 gathering the CVS log. The function is passed a list with the
304 log entries, and can modify the entries in-place, or add or
305 delete them.
306
307 :hooks.cvschangesets: Specify a Python function to be called after
308 the changesets are calculated from the CVS log. The
309 function is passed a list with the changeset entries, and can
310 modify the changesets in-place, or add or delete them.
311
312 An additional "debugcvsps" Mercurial command allows the builtin
313 changeset merging code to be run without doing a conversion. Its
314 parameters and output are similar to that of cvsps 2.1. Please see
315 the command help for more details.
316
317 Subversion Source
318 #################
319
320 Subversion source detects classical trunk/branches/tags layouts.
321 By default, the supplied ``svn://repo/path/`` source URL is
322 converted as a single branch. If ``svn://repo/path/trunk`` exists
323 it replaces the default branch. If ``svn://repo/path/branches``
324 exists, its subdirectories are listed as possible branches. If
325 ``svn://repo/path/tags`` exists, it is looked for tags referencing
326 converted branches. Default ``trunk``, ``branches`` and ``tags``
327 values can be overridden with following options. Set them to paths
328 relative to the source URL, or leave them blank to disable auto
329 detection.
330
331 The following options can be set with ``--config``:
332
333 :convert.svn.branches: specify the directory containing branches.
334 The default is ``branches``.
335
336 :convert.svn.tags: specify the directory containing tags. The
337 default is ``tags``.
338
339 :convert.svn.trunk: specify the name of the trunk branch. The
340 default is ``trunk``.
341
342 :convert.localtimezone: use local time (as determined by the TZ
343 environment variable) for changeset date/times. The default
344 is False (use UTC).
345
346 Source history can be retrieved starting at a specific revision,
347 instead of being integrally converted. Only single branch
348 conversions are supported.
349
350 :convert.svn.startrev: specify start Subversion revision number.
351 The default is 0.
352
353 Git Source
354 ##########
355
356 The Git importer converts commits from all reachable branches (refs
357 in refs/heads) and remotes (refs in refs/remotes) to Mercurial.
358 Branches are converted to bookmarks with the same name, with the
359 leading 'refs/heads' stripped. Git submodules are converted to Git
360 subrepos in Mercurial.
361
362 The following options can be set with ``--config``:
363
364 :convert.git.similarity: specify how similar files modified in a
365 commit must be to be imported as renames or copies, as a
366 percentage between ``0`` (disabled) and ``100`` (files must be
367 identical). For example, ``90`` means that a delete/add pair will
368 be imported as a rename if more than 90% of the file hasn't
369 changed. The default is ``50``.
370
371 :convert.git.findcopiesharder: while detecting copies, look at all
372 files in the working copy instead of just changed ones. This
373 is very expensive for large projects, and is only effective when
374 ``convert.git.similarity`` is greater than 0. The default is False.
375
376 :convert.git.renamelimit: perform rename and copy detection up to this
377 many changed files in a commit. Increasing this will make rename
378 and copy detection more accurate but will significantly slow down
379 computation on large projects. The option is only relevant if
380 ``convert.git.similarity`` is greater than 0. The default is
381 ``400``.
382
383 :convert.git.committeractions: list of actions to take when processing
384 author and committer values.
385
386 Git commits have separate author (who wrote the commit) and committer
387 (who applied the commit) fields. Not all destinations support separate
388 author and committer fields (including Mercurial). This config option
389 controls what to do with these author and committer fields during
390 conversion.
391
392 A value of ``messagedifferent`` will append a ``committer: ...``
393 line to the commit message if the Git committer is different from the
394 author. The prefix of that line can be specified using the syntax
395 ``messagedifferent=<prefix>``. e.g. ``messagedifferent=git-committer:``.
396 When a prefix is specified, a space will always be inserted between the
397 prefix and the value.
398
399 ``messagealways`` behaves like ``messagedifferent`` except it will
400 always result in a ``committer: ...`` line being appended to the commit
401 message. This value is mutually exclusive with ``messagedifferent``.
402
403 ``dropcommitter`` will remove references to the committer. Only
404 references to the author will remain. Actions that add references
405 to the committer will have no effect when this is set.
406
407 ``replaceauthor`` will replace the value of the author field with
408 the committer. Other actions that add references to the committer
409 will still take effect when this is set.
410
411 The default is ``messagedifferent``.
412
413 :convert.git.extrakeys: list of extra keys from commit metadata to copy to
414 the destination. Some Git repositories store extra metadata in commits.
415 By default, this non-default metadata will be lost during conversion.
416 Setting this config option can retain that metadata. Some built-in
417 keys such as ``parent`` and ``branch`` are not allowed to be copied.
418
419 :convert.git.remoteprefix: remote refs are converted as bookmarks with
420 ``convert.git.remoteprefix`` as a prefix followed by a /. The default
421 is 'remote'.
422
423 :convert.git.saverev: whether to store the original Git commit ID in the
424 metadata of the destination commit. The default is True.
425
426 :convert.git.skipsubmodules: does not convert root level .gitmodules files
427 or files with 160000 mode indicating a submodule. Default is False.
428
429 Perforce Source
430 ###############
431
432 The Perforce (P4) importer can be given a p4 depot path or a
433 client specification as source. It will convert all files in the
434 source to a flat Mercurial repository, ignoring labels, branches
435 and integrations. Note that when a depot path is given you then
436 usually should specify a target directory, because otherwise the
437 target may be named ``...-hg``.
438
439 The following options can be set with ``--config``:
440
441 :convert.p4.encoding: specify the encoding to use when decoding standard
442 output of the Perforce command line tool. The default is default system
443 encoding.
444
445 :convert.p4.startrev: specify initial Perforce revision (a
446 Perforce changelist number).
447
448 Mercurial Destination
449 #####################
450
451 The Mercurial destination will recognize Mercurial subrepositories in the
452 destination directory, and update the .hgsubstate file automatically if the
453 destination subrepositories contain the <dest>/<sub>/.hg/shamap file.
454 Converting a repository with subrepositories requires converting a single
455 repository at a time, from the bottom up.
456
457 .. container:: verbose
458
459 An example showing how to convert a repository with subrepositories::
460
461 # so convert knows the type when it sees a non empty destination
462 $ hg init converted
463
464 $ hg convert orig/sub1 converted/sub1
465 $ hg convert orig/sub2 converted/sub2
466 $ hg convert orig converted
467
468 The following options are supported:
469
470 :convert.hg.clonebranches: dispatch source branches in separate
471 clones. The default is False.
472
473 :convert.hg.tagsbranch: branch name for tag revisions, defaults to
474 ``default``.
475
476 :convert.hg.usebranchnames: preserve branch names. The default is
477 True.
478
479 :convert.hg.sourcename: records the given string as a 'convert_source' extra
480 value on each commit made in the target repository. The default is None.
481
482 :convert.hg.preserve-hash: only works with mercurial sources. Make convert
483 prevent performance improvement to the list of modified files in commits
484 when such an improvement would cause the hash of a commit to change.
485 The default is False.
486
487 All Destinations
488 ################
489
490 All destination types accept the following options:
491
492 :convert.skiptags: does not convert tags from the source repo to the target
493 repo. The default is False.
494
495 Subversion Destination
496 ######################
497
498 Original commit dates are not preserved by default.
499
500 :convert.svn.dangerous-set-commit-dates: preserve original commit dates,
501 forcefully setting ``svn:date`` revision properties. This option is
502 DANGEROUS and may break some subversion functionality for the resulting
503 repository (e.g. filtering revisions with date ranges in ``svn log``),
504 as original commit dates are not guaranteed to be monotonically
505 increasing.
506
507 For commit dates setting to work destination repository must have
508 ``pre-revprop-change`` hook configured to allow setting of ``svn:date``
509 revision properties. See Subversion documentation for more details.
510 """
511 return convcmd.convert(ui, src, dest, revmapfile, **opts)
512
513
514 @command(b'debugsvnlog', [], b'hg debugsvnlog', norepo=True)
515 def debugsvnlog(ui, **opts):
516 return subversion.debugsvnlog(ui, **opts)
517
518
519 @command(
520 b'debugcvsps',
521 [
522 # Main options shared with cvsps-2.1
523 (
524 b'b',
525 b'branches',
526 [],
527 _(b'only return changes on specified branches'),
528 ),
529 (b'p', b'prefix', b'', _(b'prefix to remove from file names')),
530 (
531 b'r',
532 b'revisions',
533 [],
534 _(b'only return changes after or between specified tags'),
535 ),
536 (b'u', b'update-cache', None, _(b"update cvs log cache")),
537 (b'x', b'new-cache', None, _(b"create new cvs log cache")),
538 (b'z', b'fuzz', 60, _(b'set commit time fuzz in seconds')),
539 (b'', b'root', b'', _(b'specify cvsroot')),
540 # Options specific to builtin cvsps
541 (b'', b'parents', b'', _(b'show parent changesets')),
542 (
543 b'',
544 b'ancestors',
545 b'',
546 _(b'show current changeset in ancestor branches'),
547 ),
548 # Options that are ignored for compatibility with cvsps-2.1
549 (b'A', b'cvs-direct', None, _(b'ignored for compatibility')),
550 ],
551 _(b'hg debugcvsps [OPTION]... [PATH]...'),
552 norepo=True,
553 )
554 def debugcvsps(ui, *args, **opts):
555 """create changeset information from CVS
556
557 This command is intended as a debugging tool for the CVS to
558 Mercurial converter, and can be used as a direct replacement for
559 cvsps.
560
561 Hg debugcvsps reads the CVS rlog for current directory (or any
562 named directory) in the CVS repository, and converts the log to a
563 series of changesets based on matching commit log entries and
564 dates."""
565 return cvsps.debugcvsps(ui, *args, **opts)
566
567
568 def kwconverted(context, mapping, name):
569 ctx = context.resource(mapping, b'ctx')
570 rev = ctx.extra().get(b'convert_revision', b'')
571 if rev.startswith(b'svn:'):
572 if name == b'svnrev':
573 return b"%d" % subversion.revsplit(rev)[2]
574 elif name == b'svnpath':
575 return subversion.revsplit(rev)[1]
576 elif name == b'svnuuid':
577 return subversion.revsplit(rev)[0]
578 return rev
579
580
581 templatekeyword = registrar.templatekeyword()
582
583
584 @templatekeyword(b'svnrev', requires={b'ctx'})
585 def kwsvnrev(context, mapping):
586 """String. Converted subversion revision number."""
587 return kwconverted(context, mapping, b'svnrev')
588
589
590 @templatekeyword(b'svnpath', requires={b'ctx'})
591 def kwsvnpath(context, mapping):
592 """String. Converted subversion revision project path."""
593 return kwconverted(context, mapping, b'svnpath')
594
595
596 @templatekeyword(b'svnuuid', requires={b'ctx'})
597 def kwsvnuuid(context, mapping):
598 """String. Converted subversion revision repository identifier."""
599 return kwconverted(context, mapping, b'svnuuid')
600
601
602 # tell hggettext to extract docstrings from these functions:
603 i18nfunctions = [kwsvnrev, kwsvnpath, kwsvnuuid]