我正在使用GitPython 包从 Python 访问 Git 存储库。这会引入async 包。在 中async/__init__.py,发生以下情况:
def _init_signals():
"""Assure we shutdown our threads correctly when being interrupted"""
import signal
# ...
signal.signal(signal.SIGINT, thread_interrupt_handler)
_init_signals()
Run Code Online (Sandbox Code Playgroud)
如果一切都在主线程中,这可以正常工作。然而,当第一次导入git(和因此async) 发生在另一个线程上时,事情会变得繁荣:
ValueError: signal only works in main thread
Run Code Online (Sandbox Code Playgroud)
由于所有这些都在 Django 框架内运行,因此我无法控制线程。
我发现的一种解决方法是 put import asyncinto settings.py,它(显然)在主线程上导入。然而,这需要在每次安装的基础上完成,所以对我的 Django 应用程序的用户来说不是很好。
我尝试捕获异常,但引发异常的导入未完全完成,因此下一个import async也会失败。
你能想出任何半途而废的方法来解决这个问题吗?
更新:我注意到 Apache 的 mod_wsgi 足够聪明,可以忽略signal调用:
[Tue Sep 07 19:53:11 2010] [warn] mod_wsgi (pid=28595): Callback registration for …Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序,该程序将在 git 存储库中添加和更新文件。由于我无法确定我正在使用的文件当前是否在 repo 中,因此我需要检查它是否存在——这个操作似乎比我想象的要难。
“in”比较似乎不适用于 gitpython 树的非根级别。前任。
>>> repo = Repo(path)
>>> hct = repo.head.commit.tree
>>>> 'A' in hct['documents']
False
>>> hct['documents']['A']
<git.Tree "8c74cba527a814a3700a96d8b168715684013857">
Run Code Online (Sandbox Code Playgroud)
所以我想知道,人们如何在尝试处理之前检查给定的文件是否在 git 树中?尝试访问不在树中的文件的对象将引发 KeyError,因此我可以尝试捕获。但这感觉就像在例行存在检查中使用异常处理很差。
我错过了一些非常明显的东西吗?一次如何使用 gitpython(或 Python 中的任何库/方法)检查提交树中文件的存在?
自我回答
好的,我在Tree 类中四处挖掘,看看 __contains__ 做了什么。事实证明,在子文件夹中搜索时,必须使用来自 repo 根目录的完整相对路径来检查文件是否存在。所以我上面做的检查的一个工作版本是:
>>> 'documents/A' in hct['documents']
True
Run Code Online (Sandbox Code Playgroud) 如何使用python从Github克隆私有存储库?
我找到了一些关于git和python的好信息,但几天前我开始学习python.
我有本地 git 存储库。我正在使用 python 使用 gitpython 库提交本地存储库。我想将提交推送到 github。我如何使用 gitpython 或任何其他库来做到这一点。我在网上看了,但没有可用的解决方案。谁能帮我这个。提前致谢
使用 GitPython 并且我只想在拉取后对本地文件进行更改时才调用函数。例如,如果我在单独的计算机上进行推送。然后拉上第一台计算机,它按预期工作,但不提供任何输出。理想的输出是更改的文件列表。或者只是告诉我拉动是否有错误,没有拉动因为分支是最新的或发生了变化的布尔值。我相信我可以刮掉 repo.git.status() 但它看起来很粗糙。环顾四周,我似乎还可以比较分支的变化,但似乎有很多额外的代码和远程调用。是否有仅使用 pull 调用的正确方法?
while True:
repo = git.Repo()
o = repo.remotes.origin
o.pull()
changed = NOT_SURE
if changed:
do_something()
print(repo.git.status())
time.sleep(POLLING_RATE)
Run Code Online (Sandbox Code Playgroud)
更新:这确实适用于检查是否进行了更改,但不会在没有额外远程调用的情况下更改文件
while True:
print(str(time.ctime())+": Checking for updates")
repo = git.Repo()
current_hash = repo.head.object.hexsha
o = repo.remotes.origin
o.pull()
pull_hash = repo.head.object.hexsha
if current_hash != pull_hash:
print("files have changed")
else:
print("no changes")
time.sleep(config.GIT_POLL_RATE)
Run Code Online (Sandbox Code Playgroud) 我是 gitpython 的新手,无法在任何地方找到对此的引用。我想要做的是这样的:
If remote branch name exists:
do something
else:
do something else
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
我查看了一些参考资料,但仍然遇到问题:
我想克隆一个远程仓库,创建一个新分支,然后使用 GitPython 将新分支推送回远程。
这似乎有效:
import git
import subprocess
nm_brnch = 'new_branch'
# Clone
repo_url = r'my_remote.git'
repo = git.Repo.clone_from(repo_url, dnm_wrk, branch=r'some_branch')
# Create new branch
git = repo.git
git.checkout('HEAD', b=nm_brnch)
# Push new branch to remote
subprocess.call(f'git push -u origin {nm_brnch}')
Run Code Online (Sandbox Code Playgroud)
但它很难看,因为它使用subprocess, 而不是使用 GitPython。
我尝试使用 GitPython,但没有成功:
repo.head.set_reference(nm_brnch)
repo.git.push("origin", nm_brnch)
Run Code Online (Sandbox Code Playgroud)
我查阅了以下参考资料:
我目前正在开发一个项目,使用 gitpython 检查 git 存储库中的一些内容。我现在开始为我的项目编写测试,通过这样做,我意识到我需要模拟一些东西。
在这个项目中,我通过克隆存储库以及使用本地存储库来创建 git.Repo 类。我可以在我的计算机上本地运行这些测试,但不可能假设这些测试将在其他计算机上运行。
本质上,问题是,如何在 gitpython 中模拟存储库?如何“假装”存储库存在于当前计算机上的指定路径上?
您可以在下面看到需要模拟的内容:
import git
repository = git.Repo('./local_repo_path')
Run Code Online (Sandbox Code Playgroud) 我已经编写了一个 Azure Funciton API(使用 python 运行时)来自动化我们正在做的一些工作的 git 签入过程(使用 gitPython)。它在我当地的环境中运行良好。但是,当我尝试将其发布到 azure 函数时,API 失败了。
---> Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: Result: Failure
Exception: ImportError: Failed to initialize: Bad git executable.
The git executable must be specified in one of the following ways:
- be included in your $PATH
- be set via $GIT_PYTHON_GIT_EXECUTABLE
- explicitly set via git.refresh()
All git commands will error until this is rectified.
This initial warning can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one …Run Code Online (Sandbox Code Playgroud) gitpython azure-app-service-plans azure-functions azure-function-app
我希望能够检查给定存储库的最新标签是什么(我在这里使用 CPython 作为示例)。
以下作品:
g = git.cmd.Git()
blob = g.ls_remote('https://github.com/python/cpython', sort='-v:refname', tags=True)
blob.split('\n')[0].split('/')[-1] # 'v3.9.0a6'
Run Code Online (Sandbox Code Playgroud)
因为blob看起来像这样:
'bc1c8af8ef2563802767404c78c8ec6d6a967897\trefs/tags/v3.9.0a6\ndcd4 (...)'
Run Code Online (Sandbox Code Playgroud)
但是:有没有更干净的方法来获取最新标签?
最好使用gitpython但任何其他包也可以。
gitpython ×10
python ×6
git ×3
python-3.x ×3
asynchronous ×1
git-push ×1
git-tag ×1
mocking ×1
pytest ×1
signals ×1