From 21827a53f7bb40904bc453470e82f33fc4ff4284 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 13 Nov 2019 16:19:19 -0800 Subject: [PATCH] Add head2branch plugin Support converting unnamed heads to named branches during mercurial conversions. Co-Authored-By: ostan89@gmail.com --- README.md | 2 ++ plugins/head2branch/README.md | 12 ++++++++++++ plugins/head2branch/__init__.py | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 plugins/head2branch/README.md create mode 100644 plugins/head2branch/__init__.py diff --git a/README.md b/README.md index 46c3fa0..3fb2474 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,8 @@ exactly one head each. Otherwise commits to the tip of these heads within the branch will get flattened into merge commits. Chris J Billington's [hg-export-tool] can help you to handle branches with duplicate heads. +Alternatively, you can use the [head2branch plugin](./plugins/head2branch) +to create a new named branch from an unnamed head. hg-fast-export will ignore any files or directories tracked by mercurial called `.git`, and will print a warning if it encounters one. Git cannot diff --git a/plugins/head2branch/README.md b/plugins/head2branch/README.md new file mode 100644 index 0000000..495166f --- /dev/null +++ b/plugins/head2branch/README.md @@ -0,0 +1,12 @@ +## Convert Head to Branch + +`fast-export` can only handle one head per branch. This plugin makes it possible +to create a new branch from a head by specifying the new branch name and +the first divergent commit for that head. The revision number for the commit +should be in decimal form. + +Note: you must run `fast-export` with `--ignore-unnamed-heads` option, +otherwise, the conversion will fail. + +To use the plugin, add the command line flag `--plugin head2branch=name,`. +The flag can be given multiple times to name more than one head. diff --git a/plugins/head2branch/__init__.py b/plugins/head2branch/__init__.py new file mode 100644 index 0000000..54e6785 --- /dev/null +++ b/plugins/head2branch/__init__.py @@ -0,0 +1,23 @@ +import sys + +def build_filter(args): + return Filter(args) + +class Filter: + + def __init__(self, args): + args = args.split(',') + self.branch_name = args[0] + self.starting_commit = int(args[1]) + self.branch_parents = set() + + def commit_message_filter(self, commit_data): + rev = commit_data['revision'] + rev_parents = commit_data['parents'] + if (rev == self.starting_commit + or any(rp in self.branch_parents for rp in rev_parents) + ): + self.branch_parents.add(rev) + commit_data['branch'] = self.branch_name.encode('ascii', 'replace') + sys.stderr.write('\nchanging r%s to branch %r\n' % (rev, self.branch_name)) + sys.stderr.flush()