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`
script.
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]))
os.chdir(repo)
dirty = check_output(('hg', 'status', '-mard')).strip()
if dirty:
logger.error("Repository %r has local modifications. Aborting.", repo)
sys.exit(1)
logger.info("Updating repository %r to the %r branch", repo, GIT_TAGS_BRANCH)
check_call(('hg', 'update', "-q", GIT_TAGS_BRANCH))
logger.info("Importing Git tags as local modifications")
shutil.copy2(os.path.join('.hg', 'git-tags'),
os.path.join('.hgtags'))
logger.info("Displaying the diff.")
sys.stderr.flush()
diff = check_output(('hg', 'diff'))
if not(diff.strip()):
logger.warning("Empty diff, nothing to do, see you next time.")
sys.exit(0)
remote = check_output(('hg', 'config', 'paths.default')).strip().decode()
sys.stdout.buffer.write(diff)
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':
revert()
logger.warning("Not confirmed, changes reverted. Bye")
sys.exit(0)
def check_call_with_simulate(cmd):
if simulate:
logger.info("Simulation mode: would have run %r", cmd)
else:
check_call(cmd)
def info_with_simulate(msg, *a, **kw):
if simulate:
logger.info("Simulation mode: would (be) " + msg, *a, **kw)
else:
logger.info(msg.capitalize(), *a, **kw)
if simulate:
logger.info("Simulation mode: reverting .hgtags")
revert()
logger.info("Simulation mode: current working directory is %s",
os.getcwd())
info_with_simulate("committing the changes")
check_call_with_simulate(
('hg', 'commit', '-m', "Updated Mercurial tags from Git tags", '.hgtags'))
info_with_simulate("pushing")
check_call_with_simulate(('hg', 'push', '-r', '.'))
if simulate:
logger.warning("End of simulation, hope you enjoyed it. Bye")
else:
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