<commit_before>import os
import subprocess

from ..source import Source
from ..cd import cd


class GitRepository(Source):
    def __init__(self, repository, commit, directory):
        Source.__init__(self)
        self.repository = repository
        self.commit = commit
        self.directory = directory

    def clean(self):
        if not os.path.exists(os.path.join(self.directory, '.git')):
            self.__fetch()

        with cd(self.directory):
            subprocess.check_call(['git', 'clean', '-xfd'])
            subprocess.check_call(['git', 'reset', '--hard', self.commit])
            subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'])

    def __fetch(self):
        if not os.path.exists(self.directory):
            os.makedirs(self.directory)

        with cd(self.directory):
            if not os.path.exists(os.path.join(self.directory, '.git')):
                subprocess.check_call(['git', 'clone', self.repository, '.'])
                subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'])
<commit_msg>slightly better git cloning<commit_after>import os
import subprocess

from ..source import Source
from ..cd import cd


class GitRepository(Source):
    def __init__(self, repository, commit, directory):
        Source.__init__(self)
        self.repository = repository
        self.commit = commit
        self.directory = directory

    def clean(self):
        if not os.path.exists(os.path.join(self.directory, '.git')):
            self.__fetch()

        with cd(self.directory):
            subprocess.check_call(['git', 'clean', '-xfd'])
            subprocess.check_call(['git', 'reset', '--hard', self.commit])
            subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'])

    def __fetch(self):
        if not os.path.exists(os.path.dirname(self.directory)):
            os.makedirs(os.path.dirname(self.directory))

        with cd(os.path.dirname(self.directory)):
            subprocess.check_call(['git', 'clone', self.repository, os.path.basename(self.directory)])
        
        with cd(self.directory):
            subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'])

{'fraction_non_alphanumeric': 0.15659955257270694, 'fraction_numerical': 0.0, 'mean_word_length': 2.65359477124183, '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': '1835354', 'n_tokens_mistral': 650, 'n_tokens_neox': 632, 'n_words': 136}