当前位置: 首页>>代码示例>>Python>>正文


Python Repository.create_blob方法代码示例

本文整理汇总了Python中pygit2.Repository.create_blob方法的典型用法代码示例。如果您正苦于以下问题:Python Repository.create_blob方法的具体用法?Python Repository.create_blob怎么用?Python Repository.create_blob使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pygit2.Repository的用法示例。


在下文中一共展示了Repository.create_blob方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GitHandler

# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import create_blob [as 别名]
class GitHandler(object):
    def __init__(self, path, repo_path=None, update_working_copy=True):
        """
        Start a git handler in given repository.
        `update_working_copy`: wether also to update the working copy.
            By default, the git handler will only work on the git database.
            Updating the working copy can take a lot of time in
            large repositories.
        """
        self.path = path
        if repo_path is None:
            repo_path = self.path
        self.repo_path = repo_path
        self.update_working_copy = update_working_copy
        self.repo = Repository(self.repo_path)
        self.working_tree = self.get_last_tree()
        self.tree_modifier = TreeModifier(self.repo, self.working_tree)
        self.messages = []
        print("Started libgit2 git handler in ", self.path)

    def get_last_tree(self):
        if self.repo.head_is_unborn:
            tree_id = self.repo.TreeBuilder().write()
            return self.repo[tree_id]
        commit = self.repo[self.getCurrentCommit()]
        return commit.tree

    def insert_into_working_tree(self, blob_id, filename):
        self.tree_modifier.insert_blob(blob_id, filename)

    def remove_from_working_tree(self, filename):
        self.tree_modifier.remove_blob(filename)

    def write_file(self, filename, content):
        # TODO: combine writing many files
        assert isinstance(content, text_type)
        data = content.encode('utf-8')
        existing_entry = get_tree_entry(self.repo, self.working_tree, filename)
        if existing_entry:
            type = 'M'
            if existing_entry.id == git_hash(data):
                return
        else:
            type = 'A'
        blob_id = self.repo.create_blob(data)
        self.insert_into_working_tree(blob_id, filename)

        if not self.repo.is_bare and self.update_working_copy:
            real_filename = os.path.join(self.path, filename)
            mkdir_p(os.path.dirname(real_filename))
            with codecs.open(real_filename, 'w', encoding='utf-8') as outfile:
                outfile.write(content)

        self.messages.append('    {}  {}'.format(type, filename))

    def remove_file(self, filename):
        existing_entry = get_tree_entry(self.repo, self.working_tree, filename)
        if existing_entry:
            self.remove_from_working_tree(filename)

            if not self.repo.is_bare and self.update_working_copy:
                remove_file_with_empty_parents(self.path, filename)

            self.messages.append('    D  {}'.format(filename))

    def move_file(self, old_filename, new_filename):
        self.tree_modifier.move(old_filename, new_filename)

        if not self.repo.is_bare and self.update_working_copy:
            real_old_filename = os.path.join(self.path, old_filename)
            real_new_filename = os.path.join(self.path, new_filename)
            mkdir_p(os.path.dirname(real_new_filename))
            os.rename(real_old_filename, real_new_filename)
            remove_file_with_empty_parents(self.path, old_filename)

        self.messages.append('    R  {} -> {}'.format(old_filename,
                                                      new_filename))

    def commit(self):
        if self.tree_modifier.tree.oid != self.get_last_tree().oid:
            raise Exception("The repository was modified outside of this process. For safety reasons, we cannot commit!")
        self.working_tree = self.tree_modifier.apply()
        self.tree_modifier = TreeModifier(self.repo, self.working_tree)

        if self.repo.head_is_unborn:
            parents = []
        else:
            commit = self.repo[self.getCurrentCommit()]
            if commit.tree.id == self.working_tree.id:
                return
            parents = [commit.id]

        config = self.repo.config
        author = Signature(config['user.name'], config['user.email'])
        committer = Signature(config['user.name'], config['user.email'])
        tree_id = self.working_tree.id
        message = '\n'.join(self.messages)
        self.repo.create_commit('refs/heads/master',
                                author, committer, message,
                                tree_id,
#.........这里部分代码省略.........
开发者ID:matthias-k,项目名称:gitdb2,代码行数:103,代码来源:git_handling.py


注:本文中的pygit2.Repository.create_blob方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。