<commit_before>from .crates import CrateBase
import subprocess, os, errno, sys

class GitCrate(CrateBase):
    def __init__(self, root, name, commit, url):
        CrateBase.__init__(self, root, name)
        self.commit = commit
        self.url = url

    def status(self):
        try:
            commit = subprocess.check_output(['git', 'rev-parse', '--verify', 'HEAD'], cwd=self.path).strip()
        except subprocess.CalledProcessError as e:
            return 'D'

        if self.commit == commit:
            return ' '
        else:
            return 'M'

    @classmethod
    def load(cls, root, name, spec):
        return cls(root, name, spec['commit'], spec['url'])

    def save(self):
        r = {
            'type': 'git',
            'commit': self.commit,
            'url': self.url,
            }
        self._save_deps(r)
        return r

    def checkout(self):
        self._clean_env()
        if os.path.isdir(os.path.join(self.path, '.git')):
            with open(os.devnull, 'w') as devnull:
                r = subprocess.call(['git', 'rev-parse', '--verify', '--quiet', self.commit], stdout=devnull, cwd=self.path)
            if r != 0:
                subprocess.check_call(['git', 'fetch', 'origin'], cwd=target_dir)

            commit = subprocess.check_output(['git', 'rev-parse', '--verify', 'HEAD'], cwd=self.path).strip()
            if commit == self.commit:
                return
        else:<commit_msg>Fix fetching when commit is not in tree.<commit_after>from .crates import CrateBase
import subprocess, os, errno, sys

class GitCrate(CrateBase):
    def __init__(self, root, name, commit, url):
        CrateBase.__init__(self, root, name)
        self.commit = commit
        self.url = url

    def status(self):
        try:
            commit = subprocess.check_output(['git', 'rev-parse', '--verify', 'HEAD'], cwd=self.path).strip()
        except subprocess.CalledProcessError as e:
            return 'D'

        if self.commit == commit:
            return ' '
        else:
            return 'M'

    @classmethod
    def load(cls, root, name, spec):
        return cls(root, name, spec['commit'], spec['url'])

    def save(self):
        r = {
            'type': 'git',
            'commit': self.commit,
            'url': self.url,
            }
        self._save_deps(r)
        return r

    def checkout(self):
        self._clean_env()
        if os.path.isdir(os.path.join(self.path, '.git')):
            with open(os.devnull, 'w') as devnull:
                r = subprocess.call(['git', 'rev-parse', '--verify', '--quiet', '{}^{{commit}}'.format(self.commit)], stdout=devnull, cwd=self.path)
            if r != 0:
                subprocess.check_call(['git', 'fetch', 'origin'], cwd=self.path)

            commit = subprocess.check_output(['git', 'rev-parse', '--verify', 'HEAD'], cwd=self.path).strip()
            if commit == self.commit:
                return
        else:
{'fraction_non_alphanumeric': 0.1527027027027027, 'fraction_numerical': 0.0006756756756756757, 'mean_word_length': 1.9758793969849247, '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': '1411492', 'n_tokens_mistral': 918, 'n_tokens_neox': 894, 'n_words': 218}