<commit_before># -*- coding: utf8 -*-
from __future__ import absolute_import
import logging
import os
import subprocess

from django.conf import settings


log = logging.getLogger(__name__)


class PullFromRepositoryException(Exception):
    pass


class PullFromRepository(object):

    def __init__(self, source, target, branch):
        self.source = source
        self.target = target
        self.branch = branch

    def pull(self, source=None, target=None):
        raise NotImplementedError


class PullFromGit(PullFromRepository):

    def pull(self, source=None, target=None, branch=None):
        log.debug("Git: Update repository.")

        source = source or self.source
        target = target or self.target
        branch = branch or self.branch

        command = ["git", "fetch", "--all"]
        execute(command, target)

        # Undo local changes
        remote = "origin"
        if branch:
            remote += "/" + branch

        command = ["git", "reset", "--hard", remote]
        code, output, error = execute(command, target)

        if code != 0:
            log.info("Git: " + unicode(error))
            log.debug("Git: Clone instead.")
            command = ["git", "clone", source, target]
            code, output, error = execute(command)

            if code != 0:
                raise PullFromRepositoryException(unicode(error))

            log.debug("Git: Repository at " + source + " cloned.")
        else:
            log.debug("Git: Repository at " + source + " updated.")

        if branch:
            command = ["git", "checkout", branch]
            code, output, error = execute(command, target)

            if code != 0:
                raise PullFromRepositoryException(unicode(error))

            log.debug("Git: Branch " + branch + " checked out.")


class PullFromHg(PullFromRepository):

    def pull(self, source=None, target=None):
        log.debug("Mercurial: Update repository.")

        source = source or self.source
        target = target or self.target

        # Undo local changes: Mercurial doesn't offer anything more elegant
        command = ["rm", "-rf", target]
        code, output, error = execute(command)

        command = ["hg", "clone", source, target]
        code, output, error = execute(command)

        if code == 0:
            log.debug("Mercurial: Repository at " + source + " cloned.")

        else:
            raise PullFromRepositoryException(unicode(error))


class PullFromSvn(PullFromRepository):

    def pull(self, source=None, target=None):
        log.debug("Subversion: Checkout or update repository.")

        source = source or self.source
        target = target or self.target

        if os.path.exists(target):
            status = "updated"
            command = ["svn", "update", "--accept", "theirs-full", target]

        else:
            status = "checked out"
            command = ["svn", "checkout", "--trust-server-cert",
                       "--non-interactive", source, target]<commit_msg>Make HG pull faster by not cloning when not necessary<commit_after># -*- coding: utf8 -*-
from __future__ import absolute_import
import logging
import os
import subprocess

from django.conf import settings


log = logging.getLogger(__name__)


class PullFromRepositoryException(Exception):
    pass


class PullFromRepository(object):

    def __init__(self, source, target, branch):
        self.source = source
        self.target = target
        self.branch = branch

    def pull(self, source=None, target=None):
        raise NotImplementedError


class PullFromGit(PullFromRepository):

    def pull(self, source=None, target=None, branch=None):
        log.debug("Git: Update repository.")

        source = source or self.source
        target = target or self.target
        branch = branch or self.branch

        command = ["git", "fetch", "--all"]
        execute(command, target)

        # Undo local changes
        remote = "origin"
        if branch:
            remote += "/" + branch

        command = ["git", "reset", "--hard", remote]
        code, output, error = execute(command, target)

        if code != 0:
            log.info("Git: " + unicode(error))
            log.debug("Git: Clone instead.")
            command = ["git", "clone", source, target]
            code, output, error = execute(command)

            if code != 0:
                raise PullFromRepositoryException(unicode(error))

            log.debug("Git: Repository at " + source + " cloned.")
        else:
            log.debug("Git: Repository at " + source + " updated.")

        if branch:
            command = ["git", "checkout", branch]
            code, output, error = execute(command, target)

            if code != 0:
                raise PullFromRepositoryException(unicode(error))

            log.debug("Git: Branch " + branch + " checked out.")


class PullFromHg(PullFromRepository):

    def pull(self, source=None, target=None):
        source = source or self.source
        target = target or self.target

        if os.path.exists(target):
            log.debug("Mercurial: Update repository.")

            # Undo local changes: discard uncommitted changes
            command = ["hg", "update", "--clean"]
            execute(command, target)

            # Undo local changes: delete untracked files
            command = ["hg", "--config", "extensions.strip=", "purge"]
            execute(command, target)

            # Undo local changes: discard local commits
            command = ["hg", "--config", "extensions.strip=", "strip", "'roots(draft())'"]
            execute(command, target)

            command = ["hg", "pull", "-u"]
            code, output, error = execute(command, target)

            if code == 0:
                return log.debug("Mercurial: Repository at " + source + " updated.")
            else:
                log.info("Mercurial: " + unicode(error))

        log.debug("Mercurial: Clone repository.")
        command = ["hg", "clone", source, target]
        code, output, error = execute(command)

        if code == 0:
            log.debug("Mercurial: Repository at " + source + " cloned.")

        else:
            raise PullFromRepositoryException(unicode(error))


class PullFromSvn(PullFromRepository):

    def pull(self, source=None, target=None):
        log.debug("Subversion: Checkout or update repository.")

        source = source or self.source
        target = target or self.target

        if os.path.exists(target):
            status = "updated"
            command = ["svn", "update", "--accept", "theirs-full", target]

        else:
            status = "checked out"
            command = ["svn", "checkout", "--trust-server-cert",
                       "--non-interactive", source, target]
{'fraction_non_alphanumeric': 0.12125220458553791, 'fraction_numerical': 0.0016166960611405056, 'mean_word_length': 2.1695388914764786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4604303', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1733, 'n_words': 528}