首页 > 解决方案 > GitHub:如何检查我是否已经拥有当前的主分支 zip?

问题描述

我尝试检查我是否已经在 GitHub 上拥有存储库主分支的当前版本。我希望这适用于,ETag因此如果我提供最后保存ETag到我的请求并且与ETag远程相同,那么我不需要再次下载它。

我使用此代码来检查:

import requests


etag_old = '"8ec0bb526b1b281a450669d79ca9ed0c7ff6b4f2"'
headers = {"If-None-Match": etag_old}

# Also doesn't work: https://codeload.github.com/Endogen/OpenCryptoBot/zip/master
response = requests.get("https://github.com/Endogen/OpenCryptoBot/archive/master.zip", headers=headers)

if response.status_code == 200:
    print(f"Status-Code: {response.status_code}")
    print(f"New ETag: {response.headers.get('ETag')}")
    print(f"History: {response.history}")
    print(f"URL: {response.url}")
else:
    # If we get status code 304, it's working
    print(f"Status-Code: {response.status_code}")

奇怪的是,这不起作用。中的 URLresponse.url也不起作用。它与 URL 有关,因为它有效:

import requests


etag_old = '"29b127a376b492572f7e332ba5dd38ea89d4d37c"'
headers = {"If-None-Match": etag_old}

response = requests.get("https://raw.githubusercontent.com/endogen/Telegram-Kraken-Bot/master/telegram_kraken_bot.py", headers=headers)

if response.status_code == 200:
    print(f"Status-Code: {response.status_code}")
    print(f"New ETag: {response.headers.get('ETag')}")
    print(f"History: {response.history}")
    print(f"URL: {response.url}")
else:
    # If we get status code 304, it's working
    print(f"Status-Code: {response.status_code}")

我的问题是:如何可靠地检查我是否已经拥有当前的主分支版本?

我正在使用 Python 3.7

标签: pythonpython-3.xgithubpython-requests

解决方案


此外Etag,您还可以使用Github API查看分支信息。

curl -i https://api.github.com/repos/Endogen/OpenCryptoBot/branches/master

您可以存储和使用提交哈希,就像Etag决定是否下载一样:

{
  "name": "master",
  "commit": {
    "sha": "a20434b7d213ff1321b7eaf896246dbf67b9fdbd",
    ...

但是我想知道是否允许您运行python脚本,也许可以先在脚本中安装git。如果是这样的话,这是最轻松的方式,并且有一个名为GitPython的不错的库来与 git 交互。


推荐阅读