Commit 8c8d40a1 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

convert-git-tags: new script

A boring operation that was very repetitive. Might as well
automate it.
parent 7b8384c6e61c
......@@ -162,4 +162,5 @@
hg pull ../gitlab-shell
## Conversion of Git tags into Mercurial tags
......@@ -165,4 +166,10 @@
For each converted Mercurial repositories, run the `convert-git-tags-push`
Example: `./convert-git-tags-push -R heptapod-rails`
Note: there is a confirmation step and a simulation mode.
#!/usr/bin/env python3
import argparse
import logging
import os
import shutil
import sys
from subprocess import check_call, check_output
GIT_TAGS_BRANCH = 'git-tags'
os.environ['HGPLAIN'] = 'yes'
parser = argparse.ArgumentParser(
description="Copy Git tags in Mercurial global tags."
parser.add_argument("-s", "--simulate", action="store_true",
help="Don't commit nor push, rollback changes "
"to the tags file instead.")
parser.add_argument("--logging-level", default='INFO')
parser.add_argument("-R", "--repository", default=".",
help="Mercurial repository to operate on.")
cl_args = parser.parse_args()
repo = cl_args.repository
simulate = cl_args.simulate
logging.basicConfig(level=getattr(logging, cl_args.logging_level.upper()))
logger = logging.getLogger(os.path.basename(sys.argv[0]))
dirty = check_output(('hg', 'status', '-mard')).strip()
if dirty:
logger.error("Repository %r has local modifications. Aborting.", repo)
sys.exit(1)"Updating repository %r to the %r branch", repo, GIT_TAGS_BRANCH)
check_call(('hg', 'update', "-q", GIT_TAGS_BRANCH))"Importing Git tags as local modifications")
shutil.copy2(os.path.join('.hg', 'git-tags'),
os.path.join('.hgtags'))"Displaying the diff.")
diff = check_output(('hg', 'diff'))
if not(diff.strip()):
logger.warning("Empty diff, nothing to do, see you next time.")
remote = check_output(('hg', 'config', 'paths.default')).strip().decode()
def revert():
check_call(('hg', 'revert', '.hgtags'))
confirmation = input("Commit and push those changes to '%s'?\n"
"Please enter 'YES' to confirm: " % remote)
if confirmation.strip() != 'YES':
logger.warning("Not confirmed, changes reverted. Bye")
def check_call_with_simulate(cmd):
if simulate:"Simulation mode: would have run %r", cmd)
def info_with_simulate(msg, *a, **kw):
if simulate:"Simulation mode: would (be) " + msg, *a, **kw)
else:, *a, **kw)
if simulate:"Simulation mode: reverting .hgtags")
revert()"Simulation mode: current working directory is %s",
info_with_simulate("committing the changes")
('hg', 'commit', '-m', "Updated Mercurial tags from Git tags", '.hgtags'))
check_call_with_simulate(('hg', 'push', '-r', '.'))
if simulate:
logger.warning("End of simulation, hope you enjoyed it. Bye")
logger.warning("Changes committed and pushed. Bye")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment