zoukankan      html  css  js  c++  java
  • 使用metaweblog API实现通用博客发布 之 版本控制

    使用metaweblog API实现通用博客发布 之 版本控制

    接上一篇本地图片自动上传以及替换路径,继续解决使用API发布博客的版本控制问题。
    当本地文档修订更新以后,如何发现版本更新,并自动发布到博客呢?我的解决方案是使用同一的git版本控制系统来实现版本监控。通过对比上一次发布的版本(commit版本),以及当前的版本(commit版本),发现两个版本间的文件差别,提供自动新建博文,或者更新博文的依据。

    # encoding=utf-8
    #!/bin/sh python3
    
    import git  #gitpython
    import inspect
    
    class RepoScanner():
      def __init__(self, repopath, branch):
        self._root = repopath
        self._branch = branch
        try:
          self._repo = git.Repo(self._root)
        except git.exc.NoSuchPathError as error: #如果没有git库,先创建git库
          self._repo = git.Repo.init(path=self._root)
        except:
          raise Exception('Fail to open git repo at: %s' % (repopath))
    
        heads = self._repo.heads
        gotbranch = False
        for head in heads:
          if head.name == branch:
            gotbranch = True
            self.head = head
            break
        if not gotbranch:
          print('create branch:', branch)
          self.head = self._repo.create_head(branch)
    
      def scanFiles(self, begin_hexsha=None):
        all_commits = list(self._repo.iter_commits(self.head.name))
        begin_commit = None
        for commit in all_commits:
          if commit.hexsha == begin_hexsha:
            begin_commit = commit
            break
    
        sourceFiles = {}
        if begin_commit:
          beginIndexFile = git.IndexFile.from_tree(self._repo, begin_commit)
          for (path, stage), entry in beginIndexFile.entries.items():
            sourceFiles[path] = entry.hexsha
    
        indexFile = git.IndexFile.from_tree(self._repo, self.head.commit)
        files = {}
        for (path, stage), entry in indexFile.entries.items():
          if path in sourceFiles:
            if entry.hexsha != sourceFiles[path]:
              files[path] = { "hexsha": entry.hexsha, "from_hexsha": sourceFiles[path], "change": 'modify'}
          else:
            files[path] = { "hexsha": entry.hexsha, "change": 'new'}
        
        return { "head": self.head.commit.hexsha, "files": files }
        
    
  • 相关阅读:
    MySQL 之 数据操作
    MySQL 之 库操作,表操作
    Python之协程
    IO模式和IO多路复用
    python之线程
    Python--多线程、多进程常用概念
    计算机组成原理
    python基础-守护进程、守护线程、守护非守护并行
    win7旗舰版 安装IIS中出现的问题
    sql 2008数据事务日志已满处理方法
  • 原文地址:https://www.cnblogs.com/robert-9/p/11457024.html
Copyright © 2011-2022 走看看