From a8f6d92613940a05f3c632559c19c378fbeb6905 Mon Sep 17 00:00:00 2001 From: Mark Nauwelaerts Date: Sat, 3 May 2025 13:59:46 +0200 Subject: [PATCH] helper: add mapfile subcommand Fixes mnauw/git-remote-hg#55 --- git-hg-helper | 39 +++++++++++++++++++++++++++++++++++++++ test/helper.t | 6 ++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/git-hg-helper b/git-hg-helper index d7502b8..94ee906 100755 --- a/git-hg-helper +++ b/git-hg-helper @@ -558,6 +558,43 @@ class GcCommand(SubCommand): gm.store() +class MapFileCommand(SubCommand): + + def argumentparser(self): + usage = '%%(prog)s %s [options] ' % (self.subcommand) + p = argparse.ArgumentParser(usage=usage) + p.add_argument('--output', required=True, + help='mapfile to write') + p.epilog = textwrap.dedent("""\ + Writes a so-called git-mapfile, as used internally by hg-git. + This files consists of lines of format ` `. + + As such, the result could be used to coax hg-git in some manner. + However, as git-remote-hg and hg-git may (likely) produce different + commits (either git or hg), mixed use of both tools is not recommended. + """) + return p + + def do(self, options, args): + remotehg = import_sibling('remotehg', 'git-remote-hg') + + if not args or len(args) != 1: + self.usage('expect 1 remote') + + remote = args[0] + hgpath = remotehg.select_marks_dir(remote, self.githgrepo.gitdir, False) + puts(b"Loading hg marks ...") + hgm = remotehg.Marks(os.path.join(hgpath, b'marks-hg'), None) + puts(b"Loading git marks ...") + gm = GitMarks(os.path.join(hgpath, b'marks-git')) + puts(b"Writing mapfile ...") + with open(options.output, 'wb') as f: + for c, m in gm.marks.items(): + hgc = hgm.rev_marks.get(m, None) + if hgc: + f.write(b'%s %s\n' % (c, hgc)) + + class SubRepoCommand(SubCommand): def writestate(repo, state): @@ -924,6 +961,7 @@ def get_subcommands(): b'repo': RepoCommand, b'gc': GcCommand, b'sub': SubRepoCommand, + b'mapfile': MapFileCommand, b'help' : HelpCommand } # add remote named subcommands @@ -946,6 +984,7 @@ def do_usage(): gc \t perform maintenance and consistency cleanup on repo tracking marks sub \t manage subrepos repo \t show local hg repo backing a remote + mapfile \t dump a hg-git git-mapfile If the subcommand is the name of a remote hg repo, then any remaining arguments are considered a "hg command", e.g. hg heads, or thg, and it is then executed diff --git a/test/helper.t b/test/helper.t index 39c6942..2ffae33 100755 --- a/test/helper.t +++ b/test/helper.t @@ -99,7 +99,7 @@ test_expect_success 'subcommand repo - with local proxy' ' test_cmp expected actual ' -test_expect_success 'subcommands hg-rev and git-rev' ' +test_expect_success 'subcommands hg-rev and git-rev and mapfile' ' test_when_finished "rm -rf gitrepo* hgrepo*" && setup_repos && @@ -110,7 +110,9 @@ test_expect_success 'subcommands hg-rev and git-rev' ' test -s rev-HEAD && git-hg-helper hg-rev `cat rev-HEAD` > hg-HEAD && git-hg-helper git-rev `cat hg-HEAD` > git-HEAD && - test_cmp rev-HEAD git-HEAD + git-hg-helper mapfile --output mapfile origin && + test_cmp rev-HEAD git-HEAD && + grep "`cat rev-HEAD` `cat hg-HEAD`" mapfile ) '