我想用Python代码解析git diff,我有兴趣从diff解析器获取以下信息:
我为此目的使用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)
如果你能帮助我解决这个错误,我将不胜感激.
使用 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,但我似乎没有取得任何进展。
有任何想法吗?
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() 方法执行相同的操作。
我正在开发一个程序,该程序将在 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从指定的提交中复制文件.我到目前为止来到这里:
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?
我正在尝试使用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) 我正在使用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)
但是,如果这是一个私人回购,我无法提前知道.
需求:
问题:
Run Code Online (Sandbox Code Playgroud)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'
我需要将此脚本集成到应该接受配置文件中凭据的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'”。
完成错误- …
我目前正在开发一个项目,使用 gitpython 检查 git 存储库中的一些内容。我现在开始为我的项目编写测试,通过这样做,我意识到我需要模拟一些东西。
在这个项目中,我通过克隆存储库以及使用本地存储库来创建 git.Repo 类。我可以在我的计算机上本地运行这些测试,但不可能假设这些测试将在其他计算机上运行。
本质上,问题是,如何在 gitpython 中模拟存储库?如何“假装”存储库存在于当前计算机上的指定路径上?
您可以在下面看到需要模拟的内容:
import git
repository = git.Repo('./local_repo_path')
Run Code Online (Sandbox Code Playgroud)