我试图访问单个文件的提交历史记录,如下所示:
git log --follow -- <filename>
Run Code Online (Sandbox Code Playgroud)
我必须使用gitpython,所以我现在正在做的是:
import git
g = git.Git('repo_dir')
hexshas = g.log('--pretty=%H','--follow','--',filename).split('\n')
Run Code Online (Sandbox Code Playgroud)
然后我构建提交对象:
repo = git.Repo('repo_dir')
commits = [repo.rev_parse(c) for c in r]
Run Code Online (Sandbox Code Playgroud)
有没有办法以更加gitpython-ic的方式做到这一点?我都尝试commit.iter_parents()和commit.iter_items(),但他们都依靠git-rev-list,所以他们没有--follow选择.
我需要在自上次Git提交以来已更改的文件中读取Python脚本.使用GitPython,我如何获得与从cli运行相同的输出:
$ git diff --name-only HEAD~1 HEAD
Run Code Online (Sandbox Code Playgroud)
我可以做类似下面的事情,但是,我只需要文件名:
hcommit = repo.head.commit
for diff_added in hcommit.diff('HEAD~1').iter_change_type('A'):
print(diff_added)
Run Code Online (Sandbox Code Playgroud) 如果我创建一个文件,如:
import os
print os.getlogin()
Run Code Online (Sandbox Code Playgroud)
并使用cron运行它,我得到一个例外
print os.getlogin()
OSError: [Errno 22] Invalid argument
Run Code Online (Sandbox Code Playgroud)
如果我在shell中手动运行它 - 它的工作原理.
问题是,commit()中的GitPython 0.3.1使用了这个函数,我需要使用它.
有没有解决方法?
我在Ubuntu10.10/python2.6.6和Debian5.0.6/python2.5.2上测试过它.
我试图找到使用gitPython拉出git存储库的方法.到目前为止,这是我从这里的官方文档中获取的内容.
test_remote = repo.create_remote('test', 'git@server:repo.git')
repo.delete_remote(test_remote) # create and delete remotes
origin = repo.remotes.origin # get default remote by name
origin.refs # local remote references
o = origin.rename('new_origin') # rename remotes
o.fetch() # fetch, pull and push from and to the remote
o.pull()
o.push()
Run Code Online (Sandbox Code Playgroud)
事实是,我想访问repo.remotes.origin做一个拉动而不重命名它的起源(origin.rename)我怎样才能实现这个目标?谢谢.
长SHA可以得到如下:
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
Run Code Online (Sandbox Code Playgroud)
短的怎么样?(简称SHA由回购的规模决定,所以它不应该像sha[:7])
如何使用GitPython和特定的SSH密钥?
关于该主题的文档不是很全面.到目前为止我唯一尝试过的是Repo(path).
我已经使用GitPython克隆了一个存储库,现在我想检查一个分支并使用该分支的内容更新本地存储库的工作树.理想情况下,我还可以在执行此操作之前检查分支是否存在.这是我到目前为止:
import git
repo_clone_url = "git@github.com:mygithubuser/myrepo.git"
local_repo = "mytestproject"
test_branch = "test-branch"
repo = git.Repo.clone_from(repo_clone_url, local_repo)
# Check out branch test_branch somehow
# write to file in working directory
repo.index.add(["test.txt"])
commit = repo.index.commit("Commit test")
Run Code Online (Sandbox Code Playgroud)
我不知道在上述评论的位置放什么.该文件似乎给出了如何拆卸头,但不知道如何检出一个名为分支的例子.
那么标题是自我解释的.相当于git reset --hard使用GitPython模块运行(在终端上)的python代码是什么?
我期待只从git repo改变文件的差异.现在,我正在使用gitpython来实际获取提交对象和git文件的更改,但我想对仅更改的文件部分进行依赖性分析.有没有办法从git python获取git diff?或者我是否必须通过逐行阅读来比较每个文件?
我没有看到在此模块中签出或列出远程/本地分支的选项:https://gitpython.readthedocs.io/en/stable/