我没有看到在此模块中签出或列出远程/本地分支的选项:https://gitpython.readthedocs.io/en/stable/
我已经在这几个小时了,虽然我有一种感觉,我很接近我似乎无法解决这个问题.
我正在尝试创建一个带有git存储库的脚本,将该存储库中的子模块更新为指定版本,并提交更改.
我可以找到存储库,获取子模块并检查我想要的提交.
我似乎无法添加更新的子模块哈希,所以我可以提交它.
repos = Repo('path/to/repos')
submodule = repos.submodule('submodule-name')
submodule.module().git.checkout('wanted commit')
diff = repos.index.diff(None)
Run Code Online (Sandbox Code Playgroud)
此时我可以看到子模块的变化.如果我检查sourcetree,我可以在'unstaged files'中看到更改的子模块.问题是,我不知道如何进行更改以便我可以提交.
repos.index.commit(''),它会创建一个空提交.repos.index.add([submodule.path])所有文件都会添加到存储库中,这肯定不是我想要的.repos.index.add([submodule]),似乎没有任何事情发生.我有一个 python 项目,它使用 GitPython 对远程 Git 存储库执行克隆和拉取功能。
举个简单的例子:
import git
from git import Git
from git import Repo
def clone_and_checkout(full_dir, git_url, repo_ver):
repo = Repo.clone_from(
url=git_url,
to_path=full_dir
)
# Trigger re-create if repository is bare
if repo.bare:
raise git.exc.InvalidGitRepositoryError
# Set origin and pull
origin = repo.remotes.origin
origin.pull()
# Check out desired version of repository
g = Git(full_dir)
g.checkout(repo_ver)
Run Code Online (Sandbox Code Playgroud)
我希望能够为这个函数编写一个单元测试,但显然这需要接触当前的外部系统。
我很好奇是否有人有模拟这种外部交互的经验,其方式类似于使用 Mock 来模拟 HTTP 调用。我希望能够以一种可以在测试时模拟的方式执行这些任务,而无需调用实际的 Git 远程。
我应该如何为此编写测试?
编辑:为了更清楚地了解我的要求,我应该提到我是 Mock 的新手,并且正在努力理解如何 Mock 这些类的实例而不是类本身。我的问题应该表述得更好 - 类似于“如何使用 Mock 来设置实例特定的属性,例如裸露?”
从那以后,我对 Mock …
我正在使用GitPython,但没有找到使用用户名和密码推送回购的方法.任何人都可以给我一个工作实例或给我一些关于如何做的指针吗?我需要做的是:将文件添加到存储库,使用提供的用户名和密码推送它.
在python脚本中,我尝试在克隆git存储库后签出标记.我使用GitPython 0.3.2.
#!/usr/bin/env python
import git
g = git.Git()
g.clone("user@host:repos")
g = git.Git(repos)
g.execute(["git", "checkout", "tag_name"])
Run Code Online (Sandbox Code Playgroud)
使用此代码我有一个错误:
g.execute(["git", "checkout", "tag_name"])
File "/usr/lib/python2.6/site-packages/git/cmd.py", line 377, in execute
raise GitCommandError(command, status, stderr_value)
GitCommandError: 'git checkout tag_name' returned exit status 1: error: pathspec 'tag_name' did not match any file(s) known to git.
Run Code Online (Sandbox Code Playgroud)
如果我用分支名称替换标签名称,我没有问题.我没有在GitPython文档中找到信息.如果我尝试在shell中签出相同的标签,我没有问题.
你知道我怎么能在python中签出一个git标签?
我正在尝试使用GitPython lib获取repo中的最新标记.通常我这样做:
repo = Repo(project_root)
last_tag = str(repo.tags[-1])
Run Code Online (Sandbox Code Playgroud)
但是一旦版本1.10发布,我总是得到1.9;(我知道它与输出git tag -l列出相同的订单有关.所以它将是1.1, 1.10, 1.2, ..., 1.9
问题是如何使用GitPython获取最新的标签?(我知道git tag -l | sort -V并且我知道如何解决这个问题而不使用repo对象.但是也许有人知道在这个lib中获取排序标签列表时我缺少什么)
自定义排序功能也总是一个选项,但我仍然想知道是否有办法用GitPython做到这一点?
有谁知道git commit -agitpython 中的等价物是什么?
我试过了repo.index.commit,但不知道如何添加 -a 选项。
repo.index.add仅添加新文件,而不是已修改的现有文件。它似乎并不支持update功能,如git add -u。
我可以做类似的事情repo.git.commit('-a'),甚至
repo.git.add('-u')
repo.index.commit(comment)
Run Code Online (Sandbox Code Playgroud)
但我认为高级接口应该能够做到这一点。我错过了什么吗?
提前致谢,
翻转
我想获得当前git-repo的已更改文件列表.这些文件通常在Changes not staged for commit:调用时列出git status.
到目前为止,我已设法连接到存储库,将其拉出并显示所有未跟踪的文件:
from git import Repo
repo = Repo(pk_repo_path)
o = self.repo.remotes.origin
o.pull()[0]
print(repo.untracked_files)
Run Code Online (Sandbox Code Playgroud)
但现在我想显示所有有变化的文件(未提交).任何人都能把我推向正确的方向吗?我查看repo了一段时间的方法和实验的名称,但我找不到正确的解决方案.
显然我可以调用repo.git.status和解析文件,但这根本不优雅.必须有更好的东西.
编辑:现在我考虑一下.更有用的是一个函数,它告诉我单个文件的状态.喜欢:
print(repo.get_status(path_to_file))
>>untracked
print(repo.get_status(path_to_another_file))
>>not staged
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用GitPython编写一些Python脚本,我可以使用它来简化我的日常任务,因为我管理了很多分支.
在撰写复杂的脚本时,我对Python也很陌生.
这是我使用的API:GitPython API doc
我想在GitPython中编写它,只需执行以下操作并解析显示HEAD远程分支指向的部分.换句话说,我想基本上得到remotes/origin/HEAD
$ git branch -a
master
* branch_to_remove
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/testing
Run Code Online (Sandbox Code Playgroud)
我浏览API文档很多次,起初我无法理解这些API文档的Python的格式,我找不到任何有用的东西用来做这以外remote_head的class git.refs.reference.Reference(repo, path, check_path=True)
但我甚至不知道如何调用/初始化它.
这是我到目前为止所做的,你可以告诉我我想要做什么,只需重置为'无分支'状态并删除我正在使用的当前分支:
import git
from git import *
repo = git.Repo("/some/path/testing")
repo.git.branch()
[some code to get the remotes/origin/HEAD, set it to remoteHeadBranch ]
repo.git.checkout(remoteHeadBranch) # this should reset the Git back to 'no branch' state
repo.git.checkout(D="branch_to_remove")
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢!
谢谢.
我使用库gitpython
如果本地git位于签出的标记上,我想获取标记的名称.
repo=git.Repo(repo_dir)
repo.tag # --> tags. But which is the current?
Run Code Online (Sandbox Code Playgroud)
在命令行上,git工具知道它.例
user@host> git status
HEAD detached at release/1.2.3
Run Code Online (Sandbox Code Playgroud)
我想通过gitpython获取字符串"release/1.2.3".