标签: gitpython

GitPython相当于"git remote show origin"?

我正在尝试更新一个Python脚本,该脚本检查一些本地存储库的状态,以防止从使用进程到使用GitPython的远程控制器.什么是等效的命令GitPythongit remote show origin,或者有什么更好的方法来检查本地回购是快速转发外的日期(等)?

$ git remote show origin
* remote origin
  Fetch URL: <url>
  Push  URL: <url>
  HEAD branch: master
  Remote branches:
    XYZ    tracked
    master tracked
  Local branches configured for 'git pull':
    XYZ    merges with remote XYZ
    master merges with remote master
  Local refs configured for 'git push':
    XYZ    pushes to XYZ    (up to date)
    master pushes to master (up to date)
Run Code Online (Sandbox Code Playgroud)

最后两行是我主要关注的问题.它看起来像这可能是可能的GitPython通过迭代git.Repo.heads …

python git gitpython

7
推荐指数
1
解决办法
3234
查看次数

使用gitpython时如何设置git用户名和密码?

我计划在我的项目中使用 GitPython。当我测试它时,使用此代码我收到一个错误。

repo.index.add(['*']) 
repo.index.commit(message="Initial Commit")
repo.remotes.origin.push()
Run Code Online (Sandbox Code Playgroud)

错误是:

Traceback (most recent call last):
  File "test.py", line 24, in <module>
    repo.remotes.origin.push()
  File "C:\Python27\lib\site-packages\git\remote.py", line 627, in push
    return self._get_push_info(proc, progress or RemoteProgress())
  File "C:\Python27\lib\site-packages\git\remote.py", line 564, in _get_push_info
    finalize_process(proc)
  File "C:\Python27\lib\site-packages\git\remote.py", line 64, in finalize_process
    proc.wait()
  File "C:\Python27\lib\site-packages\git\cmd.py", line 100, in wait
    raise GitCommandError(self.args, status, self.proc.stderr.read())
git.exc.GitCommandError: 'git push --porcelain origin' returned exit status 128:
Run Code Online (Sandbox Code Playgroud)

在最后一行之后没有消息。但是,如果我git push --porcelain origin从命令行手动运行,则会收到错误消息:

fatal: could not read Username for 'https://github.com': No such file or …
Run Code Online (Sandbox Code Playgroud)

python git gitpython

6
推荐指数
1
解决办法
2253
查看次数

获取GitPython中第一次提交的差异细节

在GitPython中,我可以通过调用diff()不同提交对象之间的方法来单独迭代树中每个更改的diff信息.如果我diff()使用create_patch=True关键字参数调用,则会为每个我可以通过创建的diff对象访问的更改(添加,删除,重命名)创建补丁字符串,并剖析更改.

但是,我没有父级与第一次提交进行比较.

import git
from git.compat import defenc
repo = git.Repo("path_to_my_repo")

commits = list(repo.iter_commits('master'))
commits.reverse()

for i in commits:

    if not i.parents:
        # First commit, don't know what to do
        continue
    else:
        # Has a parent
        diff = i.diff(i.parents[0], create_patch=True)

    for k in diff:
        try:
            # Get the patch message
            msg = k.diff.decode(defenc)
            print(msg)
        except UnicodeDecodeError:
            continue
Run Code Online (Sandbox Code Playgroud)

您可以使用该方法

diff = repo.git.diff_tree(i.hexsha, '--', root=True)
Run Code Online (Sandbox Code Playgroud)

但这会git diff使用给定的参数调用整个树,返回一个字符串,我无法分别获取每个文件的信息.

也许,有一种方法可以创建某种root对象.如何在存储库中获得第一个更改?

编辑

一个肮脏的解决方法似乎是通过直接使用 …

python git gitpython

6
推荐指数
1
解决办法
1126
查看次数

如何使用 GitPython 库 git pull rebase?

我正在使用 GitPython 库(GitPython 文档

以下代码对于 git pull 工作正常,但如何使用git pull --rebase?”

import git 

g = git.cmd.Git(git_dir)
g.pull()
Run Code Online (Sandbox Code Playgroud)

我们需要为git pull --rebase添加任何函数或参数吗?

python git gitpython

6
推荐指数
1
解决办法
4746
查看次数

将本地分支推送到远程分支

我在 Github 存储库中创建了新存储库。

使用 gitpython 库,我可以获得这个存储库。然后我创建新分支,添加新文件,提交并尝试推送到新分支。

请检查以下代码:

import git
import random
import os

repo_name = 'test'
branch_name = 'feature4'

remote_repo_addr_git = 'git@repo:DevOps/z_sandbox1.git'

no = random.randint(0,1000)
repo = git.Repo.clone_from(remote_repo_addr_git, repo_name)
new_branch = repo.create_head(branch_name)
repo.head.set_reference(new_branch)
os.chdir(repo_name)
open("parasol" + str(no), "w+").write(str(no)) # this is added
print repo.active_branch
repo.git.add(A=True)
repo.git.commit(m='okej')
repo.git.push(u='origin feature4')
Run Code Online (Sandbox Code Playgroud)

一切正常,直到最后一个推送方法。我收到此错误:

stderr: 'fatal: 'origin feature4' 似乎不是 git 存储库 致命:无法从远程存储库读取。

请确保您拥有正确的访问权限并且存储库存在。

我可以从命令行运行这个方法并且它工作正常:

git puth -u origin feature4
Run Code Online (Sandbox Code Playgroud)

但它在 Python 中不起作用。

python git gitpython

6
推荐指数
1
解决办法
3418
查看次数

Git通过GitPython推送

我在Python中使用此代码(使用"import git"):

repo = git.Repo("my_repository")
repo.git.add("bla.txt")
repo.git.commit("my commit description")
Run Code Online (Sandbox Code Playgroud)

现在我想推动这个提交.我已经尝试了很多但没有成功.Python命令应该与此Bash命令类似:

git push origin HEAD:refs/for/master
Run Code Online (Sandbox Code Playgroud)

python git push gitpython

6
推荐指数
3
解决办法
1万
查看次数

使用 GitPython 列出特定 git 提交的目录内容

使用 GitPython,我尝试列出给定提交时目录的内容(即当时目录的“快照”)。

在终端中,我要做的是:

git ls-tree --name-only 4b645551aa82ec55d1794d0bae039dd28e6c5704
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 GitPyhon 中做同样的事情?

根据我找到的类似问题的答案(GitPython get tree and blob object by sha),我尝试了递归遍历base_commit.tree及其.trees,但我似乎没有取得任何进展。

有任何想法吗?

python git gitpython

6
推荐指数
1
解决办法
3695
查看次数

如何在 GitPython 中创建 Git 拉取请求

我正在尝试将 python 用于我的 jenkins 工作,该工作下载并刷新项目中的一行,然后提交并创建一个拉取请求,我正在尝试尽可能努力阅读 GitPython 的文档,但我的大脑无法阅读从中获得任何意义。

import git
import os
import os.path as osp


path = "banana-post/infrastructure/"
repo = git.Repo.clone_from('https://github.myproject.git',
                           osp.join('/Users/monkeyman/PycharmProjects/projectfolder/', 'monkey-post'), branch='banana-refresh')
os.chdir(path)

latest_banana = '123456'
input_file_name = "banana.yml"
output_file_name = "banana.yml"
with open(input_file_name, 'r') as f_in, open(output_file_name, 'w') as f_out:
    for line in f_in:
        if line.startswith("banana_version:"):
            f_out.write("banana_version: {}".format(latest_banana))
            f_out.write("\n")
        else:
            f_out.write(line)
os.remove("deploy.yml")
os.rename("deploy1.yml", "banana.yml")
files = repo.git.diff(None, name_only=True)
for f in files.split('\n'):
    repo.git.add(f)
repo.git.commit('-m', 'This an Auto banana Refresh, contact bannana@monkey.com',
                author='moneky@banana.com')
Run Code Online (Sandbox Code Playgroud)

提交此更改后,我正在尝试push此更改并创建一个pull request …

python github gitpython

6
推荐指数
2
解决办法
6969
查看次数

使用 GitPython 检索 Github 存储库名称

有没有办法使用 GitPython 获取存储库名称?

repo = git.Repo.clone_from(repoUrl, ".", branch=branch)
Run Code Online (Sandbox Code Playgroud)

我似乎找不到附加到具有此信息的 repo 对象的任何属性。可能是我误解了 github/GitPython 的工作原理。

gitpython python-3.6

6
推荐指数
3
解决办法
4696
查看次数

在 GitPython 中迭代提交黑白 2 个指定提交

import git
repo = git.Repo(repo_dir)
ref_name = 'master'
for commit in repo.iter_commits(rev=ref_name):
     <some code here>
Run Code Online (Sandbox Code Playgroud)

此代码迭代所有提交。我想迭代黑白 2 次提交。就像git log commit1...commit2

我如何使用 GitPython 的 iter_commits() 方法执行相同的操作。

python git loops commit gitpython

6
推荐指数
2
解决办法
3607
查看次数

标签 统计

gitpython ×10

python ×9

git ×8

commit ×1

github ×1

loops ×1

push ×1

python-3.6 ×1