python - 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
解决方案
此外Etag
,您还可以使用Github API查看分支信息。
curl -i https://api.github.com/repos/Endogen/OpenCryptoBot/branches/master
您可以存储和使用提交哈希,就像Etag
决定是否下载一样:
{
"name": "master",
"commit": {
"sha": "a20434b7d213ff1321b7eaf896246dbf67b9fdbd",
...
但是我想知道是否允许您运行python脚本,也许可以先在脚本中安装git。如果是这样的话,这是最轻松的方式,并且有一个名为GitPython的不错的库来与 git 交互。
推荐阅读
- python - 我在 python 代码中导入 mifs 但我不断收到错误
- laravel - Laravel Horizon 工作人员不断无声地崩溃
- javascript - 游戏结束后js如何显示打错次数?
- php - 查询以在 db 列中搜索特定值
- powerbi - 如何使用 DAX 计算同一 id 的两个不同状态转换之间的天数差异
- python - 如果 setup.py 不在依赖链接的根目录下怎么办?
- javascript - 在 JavaScript 中等待 ajax 调用
- javascript - 在html中更改幻灯片的下一步按钮
- javascript - app与H5页面交互,使用WebViewJavascriptBridge
- performance - 如何使用javascript替换位于本地的文件中的值?