python/mozversioncontrol/mozversioncontrol/repoupdate.py
author Dustin J. Mitchell <dustin@mozilla.com>
Thu, 20 Jul 2017 19:47:32 +0000
changeset 1192627 ebc0c700e9412aed662cf8752232e35998f7d213
parent 657562 b059936c030e8a5ec94bed55eb97cd0f6df3df46
child 1299017 80f4dd9806f03df0969f967d4b5ddfbb76e5b03c
permissions -rw-r--r--
Bug 1382707: fix typo MozReview-Commit-ID: 2PhnJojQK8I

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this,
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import unicode_literals

import os
import subprocess

# The logic here is far from robust. Improvements are welcome.

def update_mercurial_repo(hg, repo, path, revision='default',
                          hostfingerprints=None, global_args=None):
    """Ensure a HG repository exists at a path and is up to date."""
    hostfingerprints = hostfingerprints or {}

    args = [hg]
    if global_args:
        args.extend(global_args)

    for host, fingerprint in sorted(hostfingerprints.items()):
        args.extend(['--config', 'hostfingerprints.%s=%s' % (host,
            fingerprint)])

    if os.path.exists(path):
        subprocess.check_call(args + ['pull', repo], cwd=path)
    else:
        subprocess.check_call(args + ['clone', repo, path])

    subprocess.check_call([hg, 'update', '-r', revision], cwd=path)


def update_git_repo(git, repo, path, revision='origin/master'):
    """Ensure a Git repository exists at a path and is up to date."""
    if os.path.exists(path):
        subprocess.check_call([git, 'fetch', '--all'], cwd=path)
    else:
        subprocess.check_call([git, 'clone', repo, path])

    subprocess.check_call([git, 'checkout', revision], cwd=path)