标签: gitpython

Python Git diff解析器

我想用Python代码解析git diff,我有兴趣从diff解析器获取以下信息:

  1. 已删除/添加的行的内容以及行号.
  2. 文件名.
  3. 文件的状态,无论是删除,重命名还是添加.

我为此目的使用unidiff 0.5.2并且我编写了以下代码:

    from unidiff import PatchSet
    import git
    import os

    commit_sha1 = 'b4defafcb26ab86843bbe3464a4cf54cdc978696'
    repo_directory_address = '/my/git/repo'
    repository = git.Repo(repo_directory_address)
    commit = repository.commit(commit_sha1)
    diff_index = commit.diff(commit_sha1+'~1', create_patch=True)
    diff_text = reduce(lambda x, y: str(x)+os.linesep+str(y), diff_index).split(os.linesep)
    patch = PatchSet(diff_text)
    print patch[0].is_added_file
Run Code Online (Sandbox Code Playgroud)

我正在使用GitPython来生成Git diff.我收到以下代码的以下错误:

    current_file = PatchedFile(source_file, target_file,
    UnboundLocalError: local variable 'source_file' referenced before assignment
Run Code Online (Sandbox Code Playgroud)

如果你能帮助我解决这个错误,我将不胜感激.

python git parsing gitpython

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

使用 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 中迭代提交黑白 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 的 repo 中

我正在开发一个程序,该程序将在 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)

gitpython

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

在gitpython中获取特定文件的所有修订

我正在使用gitpython库执行git操作,从python代码中检索git信息。我想检索特定文件的所有修订。但是在文档中找不到对此的具体参考。

谁能在这方面提供些帮助的线索?谢谢。

python git gitpython

5
推荐指数
2
解决办法
1905
查看次数

GitPython是否可以在没有签出的情况下从指定的提交中获取文件

我想用GitPython从指定的提交中复制文件.我到目前为止来到这里:

import git
git = git.Git(REPO_PATH)
git.checkout(COMMIT_HEX_SHA)
fo = open(REPO_PATH + "/foo.txt", "r")
str = fo.read(10);
fo.close()
Run Code Online (Sandbox Code Playgroud)

有用.但是checkout更改HEAD和更改文件.是否可以在没有指定提交的情况下复制文件或读取文件checkout

python gitpython

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

如何处理gitpython clone异常?

我正在尝试使用GitPython编写批处理克隆脚本,但是我找不到有效的例子,例如git url不存在,下载interupt等。

我实际上该怎么做?

我现有的代码:

giturl = 'https://github.com/'+username+'/'+hwName+'.git'
targeturl = os.path.join(hwfolder,username+'-'+hwName)
try:
    repo = Repo.clone_from(giturl, targeturl, branch='master')
except:
    #git url not reachable
    #download interupt
    #target local path problem
Run Code Online (Sandbox Code Playgroud)

python git gitpython

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

使用HTTPS和gitpython克隆私人仓库

我正在使用gitpython通过HTTPS克隆git存储库.如果项目是私人仓库,它将提示输入用户名和密码.如何通过pythonically与提示进行交互以传递用户名和密码变量?

from git import Repo

HTTPS_REMOTE_URL = 'https://github.com/username/private-project'
DEST_NAME = 'https-cloned-private-project'
cloned_repo = Repo.clone_from(HTTPS_REMOTE_URL, DEST_NAME)
Run Code Online (Sandbox Code Playgroud)

运行此代码的输出:

$ python example.py
Username for 'https://github.com': example
Password for 'https://example@github.com': 
Run Code Online (Sandbox Code Playgroud)

我知道可以在URL中包含用户名和密码:

HTTPS_REMOTE_URL = 'https://username:password@github.com/username/private-project'
Run Code Online (Sandbox Code Playgroud)

但是,如果这是一个私人回购,我无法提前知道.

python git gitpython

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

如何使用python库GitPython提交和推送文件

需求:

  • 从python脚本提交文件并将其推送到GitHub存储库。
  • 凭据应包含在脚本中。

问题:

  • 如果脚本中提供了凭据,则提交操作正在执行并引发以下错误,
Traceback (most recent call last):
  File "/home/amith/example.py", line 14, in <module>
    repo.index.add(folder_path)
AttributeError: 'Repository' object has no attribute 'index'
Run Code Online (Sandbox Code Playgroud)
  • 如果脚本中提供凭据,则通过在终端上提供提交操作,提交操作将正常工作。

我需要将此脚本集成到应该接受配置文件中凭据的Django应用程序中。

我已经尝试了以下链接,但还没有任何工作。- 链接1 - 链接2 - LINK3

from git import Repo
from github import Github
from pdb import set_trace as bp

repo_dir = '--------'
repo = Repo(repo_dir)

# using username and password

g = Github("-----", "------")
folder_path = '----------'
commit_message = 'Add New file'
repo.index.add(folder_path)
repo.index.commit(commit_message)
origin = repo.remote('origin')
origin.push()
Run Code Online (Sandbox Code Playgroud)

因此,我收到此错误“ AttributeError:'Repository'对象没有属性'index'”。

完成错误- …

python github gitpython

5
推荐指数
0
解决办法
200
查看次数

Python:模拟本地 gitpython 存储库

我目前正在开发一个项目,使用 gitpython 检查 git 存储库中的一些内容。我现在开始为我的项目编写测试,通过这样做,我意识到我需要模拟一些东西。

在这个项目中,我通过克隆存储库以及使用本地存储库来创建 git.Repo 类。我可以在我的计算机上本地运行这些测试,但不可能假设这些测试将在其他计算机上运行。

本质上,问题是,如何在 gitpython 中模拟存储库?如何“假装”存储库存在于当前计算机上的指定路径上?

您可以在下面看到需要模拟的内容:

import git
repository = git.Repo('./local_repo_path')
Run Code Online (Sandbox Code Playgroud)

mocking pytest python-3.x gitpython

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

标签 统计

gitpython ×10

python ×8

git ×6

commit ×1

github ×1

loops ×1

mocking ×1

parsing ×1

pytest ×1

python-3.x ×1