标签: gitpython

How to set default branch for GitPython

With GitPython, I can create a new repo with the following:

from git.repo.base import Repo

Repo.init('/tmp/some-repo/')
Run Code Online (Sandbox Code Playgroud)

The repo is created with the default branch master.

How can I modify this default branch?

Update: As suggested in the answers below, I have tried using Repo.init('/tmp/some-repo', initial_branch="main"), however it renders this exception:

Traceback (most recent call last):
  File "/app/checker/tests.py", line 280, in test_alternative_compare_branch
    comp_repo_main = Repo.init(
  File "/usr/local/lib/python3.9/site-packages/git/repo/base.py", line 937, in init
    git.init(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/git/cmd.py", line 542, in …
Run Code Online (Sandbox Code Playgroud)

python git gitpython

5
推荐指数
1
解决办法
2380
查看次数

如何通过 git-python 添加/提交和推送存储库?

我正在尝试使用 git-python 添加、提交并推送到存储库。根据不完整的文档和此处的示例,我尝试了以下方式:

myrepo = Repo('Repos/hello-world/.git')
# make changes to README.md
myrepo.index.add('README.md')
myrepo.index.commit("Updating copyright year")
myrepo.git.push("origin", "copyright_updater")   ###
Run Code Online (Sandbox Code Playgroud)

我检查了存储库hello_world并将其放在一个文件夹下Repos。我确实更改了一个文件,即README.md. 但是使用该代码我得到一个错误

git.exc.GitCommandError: Cmd('git') failed due to: exit code(1)
   cmdline: git push origin copyright_updater
   stderr: 'error: src refspec copyright_updater does not match any.
 error: failed to push some refs to 'git@github.com:alex4200/hello-world.git''
Run Code Online (Sandbox Code Playgroud)

在标记线上。

我该如何修复它,以便将更改推送到新分支并在 GitHub 上创建拉取请求?

python git gitpython

5
推荐指数
1
解决办法
7406
查看次数

gitpython创建zip存档

如何使用gitpython创建存档,我尝试了以下创建文件,但我无法打开它告诉我读取存档时出错,存档似乎无效或损坏

from git import *
repo = Repo(repo_path)
assert repo.bare == False
repo.archive(open("repo.tar",'w'))
Run Code Online (Sandbox Code Playgroud)

我想创建一个zip文件,然后我尝试了这个但是在这里它创建了一个空的zip文件(repo的路径是正确的,因为当我使用repo.clone时它会克隆一切)

repo.archive(open("repo.zip",'w'), format="zip") 
Run Code Online (Sandbox Code Playgroud)

python git gitpython

4
推荐指数
1
解决办法
1132
查看次数

使用GitPython查找分支上的第一次提交

我正在使用Python和Git-Python编写一个git post-receive钩子,该钩子收集有关推送中包含的提交的信息,然后用摘要更新我们的错误跟踪器和IM。在推送创建分支的情况下,我遇到了麻烦(即fromrev,后接收的参数全为零),并且在该分支上还跨越了几次提交。我正在从torev提交中倒退父母列表,但是我不知道如何确定哪个提交是分支中的第一个提交,即何时停止查找。

在命令行上我可以做

git rev-list this-branch ^not-that-branch ^master
Run Code Online (Sandbox Code Playgroud)

这将给我确切的提交清单this-branch,而没有其他人。我尝试使用记录的Commit.iter_parents方法复制此方法,该方法记录为采用与git-rev-list相同的参数,但据我所知,它不喜欢位置参数,而且我找不到一组关键字参数那个工作。

我读了Dulwich的doco,但尚不清楚它是否会做与Git-Python截然不同的事情。

我的(简化)代码如下所示。推送开始新分支时,当前仅查看第一次提交,然后停止:

import git
repo = git.Repo('.')
for line in input:
    (fromrev, torev, refname) = line.rstrip().split(' ')
    commit = repo.commit(torev)
    maxdepth = 25    # just so we don't go too far back in the tree
    if fromrev == ('0' * 40):
        maxdepth = 1
    depth = 0
    while depth < maxdepth:
        if commit.hexsha == fromrev:
            # Reached the start of the push
            break …
Run Code Online (Sandbox Code Playgroud)

python git git-post-receive gitpython dulwich

4
推荐指数
2
解决办法
3544
查看次数

如何在 GitPython 中使用 git blame ?

我正在尝试在我的脚本中使用 GitPython 模块......但我不能。这不是很有记录:GitPython Blame

我想我还没有那么远,因为我想要重现的通常 git 责备如下: git blame -L127,+1 ../../core/src/filepath.cpp -e

这是我的脚本:

from git import *
    repo = Repo("C:\\Path\\to\\my\\repos\\")
    assert not repo.bare
    # log_line = open("lineDeb.txt")
    # for line in log_line:
    repo.git.blame(L='127,+1' '../../core/src/filepath.cpp', e=True)
Run Code Online (Sandbox Code Playgroud)

注释的两行是为了在我的“lineDeb.txt”文件中的每个数字行上进行 git blame 的最终目标。

我有以下输出:

...
git.exc.GitCommandError: 'git blame -L127,+1../../core/src/filepath.cpp -e' returned with exit code 129
stderr: 'usage: git blame [options] [rev-opts] [rev] [--] file
...
Run Code Online (Sandbox Code Playgroud)

我知道我可以用 os 模块制作它,但我想保留在 python 中。

如果这个模块或 python 的专家可以帮助我?

也许我没有正确使用 GitPython?

目标是获取行提交者的电子邮件......

提前致谢。

python gitpython

4
推荐指数
1
解决办法
3708
查看次数

如何使用GitPython将master分支合并到功能分支?

我正在尝试使一些标准工作流程自动化,而我发现自己经常做的一件事是将对远程master分支的更改合并到我自己的本地分支中并推送结果。

因此,步骤如下:

  1. 切换至主人
  2. 从远程拉出更改
  3. 切换到原始功能分支
  4. 从母版合并到功能分支
  5. 将功能分支推送到远程

我一直在尝试编写一个简短的python脚本来通过一次调用为我完成此操作,但我陷入了第4步。我也无法理解文档来确定如何执行此操作。

使用git.exe本身,我可以简单地做到这一点: git.exe merge master

使用GitPython模块是否有可能,如果可以,应该怎么做?

python git gitpython

4
推荐指数
1
解决办法
3479
查看次数

gitpython:git commit的命令语法

使用gitpython模块,我正在编写python脚本来检查所有修改过的文件的git diff - > git add.最后我想提交所有这些更改,但我没有找到命令的确切语法.

我正在尝试使用下面的代码,'git add'工作正常,但'git commit'会出错.

import git

repo = git.Repo(os.getcwd())
files = repo.git.diff(None, name_only=True)
for f in files.split('\n'):
    show_diff(f)
    repo.git.add(f)

repo.git.commit('test commit', author='sunilt@xxx.com')
Run Code Online (Sandbox Code Playgroud)

这是我看到的错误,似乎cmd参数中缺少某些东西.

In [10]: repo.git.commit("test commit", author="sunilt@xxx.com")
---------------------------------------------------------------------------
GitCommandError                           Traceback (most recent call last)
<ipython-input-10-b4505b7c53c2> in <module>()
----> 1 repo.git.commit("test commit", author="sunil.thorat@nuance.com")

c:\python27\lib\site-packages\git\cmd.pyc in <lambda>(*args, **kwargs)
    421         if name[0] == '_':
    422             return LazyMixin.__getattr__(self, name)
--> 423         return lambda *args, **kwargs: self._call_process(name,    *args, **kwargs)
    424
    425     def set_persistent_git_options(self, **kwargs):

c:\python27\lib\site-packages\git\cmd.pyc …
Run Code Online (Sandbox Code Playgroud)

python git python-2.7 gitpython

4
推荐指数
1
解决办法
5259
查看次数

如何使用 GitPython 推送到远程仓库

我必须从一个存储库克隆一组项目,然后自动将其推送到远程存储库。因此我使用 python 和特定模块GitPython。到目前为止,我可以像这样使用 gitpython 克隆项目:

def main():
  Repo.clone_from(cloneUrl, localRepoPath)
  # Missing: Push the cloned repo to a remote repo.
Run Code Online (Sandbox Code Playgroud)

如何使用 GitPython 将克隆的存储库推送到远程存储库?

python git gitpython

4
推荐指数
2
解决办法
1万
查看次数

你如何用 gitPython 删除文件

你如何用 gitPython 删除文件?

   repo.delete([file_to_delete])
Run Code Online (Sandbox Code Playgroud)

说没有删除方法。

gitpython

4
推荐指数
1
解决办法
1518
查看次数

如何在 Windows 上的 python 中删除 git 存储库

正如标题所描述的,我需要使用 python 删除一个 git 存储库。我已经看到了关于这个主题的其他问题,但似乎没有一个解决方案对我有用。

我的工作: 我需要使用 gitpython 下载一个存储库,然后检查一些不相关的东西。该过程完成后,我需要删除存储库以节省使用我脚本的人的空间。

问题: 克隆 git 存储库时,将创建一个 .git 文件。该文件隐藏在 Windows 中,我一直使用的模块无权删除 .git 文件夹中的任何文件。

我试过的:

import shutil
shutil.rmtree('./cloned_repo')

PermissionError: [WinError 5] Access is denied:
Run Code Online (Sandbox Code Playgroud)

任何有关此问题的帮助将不胜感激。

python windows git gitpython

4
推荐指数
1
解决办法
2277
查看次数

标签 统计

gitpython ×10

python ×9

git ×8

dulwich ×1

git-post-receive ×1

python-2.7 ×1

windows ×1