<commit_before>from time import time
from collections import defaultdict
from json import dumps, loads
from shutil import rmtree

from dulwich.repo import Repo
from dulwich.objects import Blob, Tree, Commit, parse_timezone
from dulwich.diff_tree import tree_changes, RenameDetector

from slyd.jsondiff import merge_jsons


CHANGE_ADD = 'add'
CHANGE_MODIFY = 'modify'
CHANGE_DELETE = 'delete'
CHANGE_RENAME = 'rename'
CHANGE_COPY = 'copy'
CHANGE_UNCHANGED = 'unchanged'

FILE_MODE = 0100644


class Repoman(object):
    '''An interface to interact with Git repositories.

    Only json files are allowed into the repository as a custom merge algorithm
    is used to resolve conflicts. Changes must be recorded into the repo file
    by file using the save_file and delete_file methods.

    The expected work-flow for concurrent usage of a repo is:

    * User A branches the repo to make his edits.
    * User B branches the repo to make his edits.
    * User A changes some files using save_file and delete_file.
    * User B changes some files using save_file and delete_file.
    * User A publishes his changes using publish_branch.
    * User A deletes his edit branch.
    * User B tries to publish using publish_branch. If the automatic merge
    is not able to resolve all conflicts, the publish is unsuccessful but
    intermediate merge files are be created containing meta-data to help resolve
    the pending conflicts.
        * User B resolves the pending conflicts.
        * User B publishes his changes using publish_branch.
        * User B deletes his edit branch.
    '''

    @classmethod
    def create_repo(cls, repo_name, author=None):
        '''Creates a new repository named repo_name.'''
        repoman = Repoman(author)
        repoman._repo = Repo.init(repo_name, mkdir=True)
        tree = Tree()
        commit = repoman._create_commit()
        commit.tree = tree.id
        commit.message = 'Initialization commit'
        repoman._update_store(commit, tree)
        repoman._advance_branch('master', commit)
        return repoman

    @classmethod
    def open_repo(cls, repo_name, author=None):
        '''Opens an existing repository.'''
        repoman = Repoman(author)
        repoman._repo = Repo(repo_name)
        return repoman

    @classmethod
    def repo_exists(cls, repo_name):
        '''Returns true iff a repository named repo_name can be opened.'''
        try:
            Repoman.open_repo(repo_name)
        except OSError as ex:
            return False
        else:
            return True

    @classmethod
    def delete_repo(cls, repo_name):
        '''Deletes an existing repo.'''
        try:
            rmtree(repo_name)
        except OSError as ex:
            pass

    def __init__(self, author):
        '''Do not instantiate directly, use create_repo or open_repo.'''
        self._author = author
        self._encoding = 'UTF-8'
        self._time_zone = parse_timezone('+0000')[0]

    def create_branch(self, branch_name, at_revision=None):
        '''Creates a new branch.

        If no revision is specified, the branch is created from head.
        '''
        at_revision = at_revision or self._get_head()
        self._repo.refs['refs/heads/%s' % branch_name] = at_revision

    def delete_branch(self, branch_name):
        '''Deletes an existing branch.

        Only the reference to the branch is deleted, all commits trees and
        blobs are left untouched.
        '''
        del self._repo.refs['refs/heads/%s' % branch_name]

    def has_branch(self, branch_name):
        '''Returns true iff the specified branch exists in this repo.'''
        try:
            return self._repo.refs['refs/heads/%s' % branch_name] and True
        except KeyError as ex:
            return False<commit_msg>- Small fixes as per dangra's comments<commit_after>from time import time
from collections import defaultdict
from json import dumps, loads
from shutil import rmtree

from dulwich.repo import Repo
from dulwich.objects import Blob, Tree, Commit, parse_timezone
from dulwich.diff_tree import tree_changes, RenameDetector
from dulwich.errors import NotGitRepository

from slyd.jsondiff import merge_jsons


CHANGE_ADD = 'add'
CHANGE_MODIFY = 'modify'
CHANGE_DELETE = 'delete'
CHANGE_RENAME = 'rename'
CHANGE_COPY = 'copy'
CHANGE_UNCHANGED = 'unchanged'

FILE_MODE = 0100644


class Repoman(object):
    '''An interface to interact with Git repositories.

    Only json files are allowed into the repository as a custom merge algorithm
    is used to resolve conflicts. Changes must be recorded into the repo file
    by file using the save_file and delete_file methods.

    The expected work-flow for concurrent usage of a repo is:

    * User A branches the repo to make his edits.
    * User B branches the repo to make his edits.
    * User A changes some files using save_file and delete_file.
    * User B changes some files using save_file and delete_file.
    * User A publishes his changes using publish_branch.
    * User A deletes his edit branch.
    * User B tries to publish using publish_branch. If the automatic merge
    is not able to resolve all conflicts, the publish is unsuccessful but
    intermediate merge files are be created containing meta-data to help resolve
    the pending conflicts.
        * User B resolves the pending conflicts.
        * User B publishes his changes using publish_branch.
        * User B deletes his edit branch.
    '''

    @classmethod
    def create_repo(cls, repo_name, author=None):
        '''Creates a new repository named repo_name.'''
        repoman = Repoman(author)
        repoman._repo = Repo.init(repo_name, mkdir=True)
        tree = Tree()
        commit = repoman._create_commit()
        commit.tree = tree.id
        commit.message = 'Initialization commit'
        repoman._update_store(commit, tree)
        repoman._advance_branch('master', commit)
        return repoman

    @classmethod
    def open_repo(cls, repo_name, author=None):
        '''Opens an existing repository.'''
        repoman = Repoman(author)
        repoman._repo = Repo(repo_name)
        return repoman

    @classmethod
    def repo_exists(cls, repo_name):
        '''Returns true iff a repository named repo_name can be opened.'''
        try:
            Repoman.open_repo(repo_name)
        except NotGitRepository as ex:
            return False
        else:
            return True

    @classmethod
    def delete_repo(cls, repo_name):
        '''Deletes an existing repo.'''
        try:
            rmtree(repo_name)
        except OSError as ex:
            pass

    def __init__(self, author):
        '''Do not instantiate directly, use create_repo or open_repo.'''
        self._author = author
        self._encoding = 'UTF-8'
        self._time_zone = parse_timezone('+0000')[0]

    def create_branch(self, branch_name, at_revision=None):
        '''Creates a new branch.

        If no revision is specified, the branch is created from the latest
        commit in master.
        '''
        at_revision = at_revision or self._get_head()
        self._repo.refs['refs/heads/%s' % branch_name] = at_revision

    def delete_branch(self, branch_name):
        '''Deletes an existing branch.

        Only the reference to the branch is deleted, all commits trees and
        blobs are left untouched.
        '''
        del self._repo.refs['refs/heads/%s' % branch_name]

    def has_branch(self, branch_name):
        '''Returns true iff the specified branch exists in this repo.'''
        return 'refs/heads/%s' % branch_name in self._repo.refs
{'fraction_non_alphanumeric': 0.07217036939660838, 'fraction_numerical': 0.003417904561588011, 'mean_word_length': 2.7367387033398822, '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': '6470977', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2089, 'n_words': 784}