GitPython不显示最新提交

2024-05-23 22:32:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个文档自动化系统上工作。其中一部分要求我检查回购协议的最后两次提交并检查差异,然后根据结果触发多个事件。我使用的是Mac,Catalina和Python 3.7.7

我在比特桶上有一个测试回购。我只是对自述文件做了一些更改,直接在BitBucket中编辑并提交它。我可以看到BitBucket中的变化:

BitBucket Repo

如果我跳上终端,cd到回购并检查状态,我会得到预期的结果:

Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.

但是,我的Python脚本一直返回上一次提交。(应该注意的是,我已经编程了一段时间,但对Python还是新手)

以下是相关代码:

import os, sys, git, time
from pathlib import Path

...

#get the repo
from git import Repo
thisRepo = Repo(thisRepoPath)

#fetch the repo first to make sure we capture any changes
for remote in thisRepo.remotes:
    remote.fetch()

#check the diffs
getGitDiff(thisRepo, thisRepoPath)

getGitDiff

def getGitDiff(thisRepo, thisRepoPath):
    printInCyan("Checking:\n" + thisRepoPath)

    #give us read only access to the repo
    thisRepo.config_reader()

    #check to see if files have been changed in the repo and that change was a modification
    print(getStatus(thisRepo, thisRepoPath))

getStatus

def getStatus(thisRepo, path):

    printInRed(thisRepo.head.commit.message)
    printInYellow(time.strftime("%a, %d %b %Y %H:%M", time.gmtime(thisRepo.head.commit.committed_date)))

    changed = [ item.a_path for item in thisRepo.index.diff(None) ]
    result = {}
    if path in changed:
         result['status'] = 'modified'
         result['hasChange'] = True
         result['item'] = changed
    else:
        result['status'] = 'up-to-date'
        result['hasChange'] = False
        result['item'] = changed

    return result

打印提交消息和提交日期可验证它是否读取了错误的提交。我已经构建了类似于Mac桌面应用程序的东西,我遇到了类似的问题,并通过调用git fetch解决了这个问题。我假设(希望是正确的)这是一个类似的问题,我只是没有打正确的电话或打错误的电话。:)

任何帮助都将不胜感激。(我正在终端中运行脚本)


Tags: thetoinimportgitbitbuckettimerepo