我正在尝试获取所有合并的分支以及过时的分支(3个月内未使用) 我已经为LinuxShell编写了命令。但是,我需要用Gitpyhon编写一个替代命令。有人能帮我吗
self.cmd("git branch --merged master") # <-- Display, merged branches
self.cmd( # <-- Display stale branches
"for branch in `git branch -r | grep -v HEAD`;"
"do echo -e `git log --before=\"3 months\" --no-merges"
" -n 1 --format=\"%cr | %an | %ae | \""
" $branch | head -n 1` \\t$branch; done | sort -M"
)
下面是关于如何在GitPython中直接翻译git命令的提示
import git
李>g = git.Git()
创建git处理程序李>env = {'GIT_DIR': '/path/to/repo/.git', 'GIT_WORK_TREE': '/path/to/repo'}
传递这两个变量,这样我们就可以在任何地方调用git命令。如果脚本涉及多个存储库,请记住切换变量值李>git foo
转换为g.foo()
,将git foo-bar
转换为g.foo_bar()
。例如,git commit
等于g.commit()
,git cherry-pick
等于g.cherry_pick()
李>with_extended_output=True
传递给命令,以便捕获status、stdout和stderr李>try...catch...
捕获命令错误。请注意,当stderr不是空的时,可能根本没有问题。即使命令成功,某些命令也会将结果打印到stderr李>g.execute()
李>现在试试你的命令。假设非裸存储库是
/path/to/repo
对于某些命令,如
git worktree add no-checkout foo HEAD
,g.worktree('add', 'foo', 'HEAD', no_checkout=True)
总是失败,因为它调用了无效的命令git worktree no-checkout add foo HEAD
。在这种情况下,我们可以使用g.execute()
。将整个命令转换为参数列表['git', 'worktree', 'add', ' no-checkout', 'foo', 'HEAD']
,并将其传递给g.execute()
相关问题 更多 >
编程相关推荐