<commit_before>from time import time

import arrow
from dulwich.objects import Blob, Tree, Commit


class TODOBranch(object):
    def __init__(self, repo, ref_name):
        self.repo = repo
        self.ref_name = ref_name

    @property
    def exists(self):
        return self.ref_name in self.repo.refs

    def init_ref(self, author_name, author_email):
        if self.ref_name not in self.repo.refs:
            author = '{} <{}>'.format(author_name, author_email).encode('utf8')

            # create TODO branch
            blob = Blob.from_string(b'')
            tree = Tree()
            tree.add(b'TODO', 0o100644, blob.id)

            commit = Commit()
            commit.tree = tree.id
            commit.message = 'Initial commit for TODO branch'
            commit.encoding = 'UTF-8'

            tz = arrow.now().utcoffset().seconds
            commit.author = commit.committer = author
            commit.author_time = commit.commit_time = int(time())
            commit.commit_timezone = commit.author_timezone = tz

            # add objects to repo
            store = self.repo.object_store
            store.add_object(blob)
            store.add_object(tree)
            store.add_object(commit)

            # set branch
            self.repo.refs[self.ref_name] = commit.id
            return True
        return False

    def get_todo(self):
        commit = self.repo[self.repo.refs[self.ref_name]]
        tree = self.repo[commit.tree]
        mode, hash = tree.lookup_path(self.repo.__getitem__, 'TODO')<commit_msg>Factor out save commit and create_todo_commit from init_ref.<commit_after>from time import time

import arrow
from dulwich.objects import Blob, Tree, Commit


class TODOBranch(object):
    def __init__(self, repo, ref_name):
        self.repo = repo
        self.ref_name = ref_name

    @property
    def exists(self):
        return self.ref_name in self.repo.refs

    def init_ref(self, author_name, author_email):
        self.save_commit(
            *self.create_todo_commit(
                b'', 'Initial commit for TODO branch', author_name,
                author_email),
            update_ref=self.ref_name)

    def create_todo_commit(self, contents, message, author_name, author_email,
                           parent=None):
        author = '{} <{}>'.format(author_name, author_email).encode('utf8')

        # create TODO branch
        blob = Blob.from_string(contents)
        tree = Tree()
        tree.add(b'TODO', 0o100644, blob.id)

        commit = Commit()
        commit.tree = tree.id
        commit.message = message.encode('utf8')
        commit.encoding = 'UTF-8'

        tz = arrow.now().utcoffset().seconds
        commit.author = commit.committer = author
        commit.author_time = commit.commit_time = int(time())
        commit.commit_timezone = commit.author_timezone = tz

        # add objects to repo
        return commit, tree, blob

    def save_commit(self, commit, tree, blob, update_ref=None):
        store = self.repo.object_store
        store.add_object(blob)
        store.add_object(tree)
        store.add_object(commit)

        if update_ref:
            self.repo.refs[update_ref] = commit.id

    def get_todo(self):
        commit = self.repo[self.repo.refs[self.ref_name]]
        tree = self.repo[commit.tree]
        mode, hash = tree.lookup_path(self.repo.__getitem__, 'TODO')
{'fraction_non_alphanumeric': 0.1023296962547921, 'fraction_numerical': 0.005603066941905043, 'mean_word_length': 2.18796992481203, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1305480', 'n_tokens_mistral': 1033, 'n_tokens_neox': 991, 'n_words': 223}