您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Git通过python子进程添加

5b51 2022/1/14 8:23:34 python 字数 3420 阅读 592 来源 www.jb51.cc/python

我试图通过python子进程运行git命令.我这样做是通过调用github的cmd目录中的git.exe来实现的.我设法让大多数命令工作(init,remote,status)但是在调用git add时出错了.到目前为止这是我的代码:import subprocess gitPath = 'C:/path/to/git/cmd.exe' repoPath

概述

我试图通过python子进程运行git命令.我这样做是通过调用github的cmd目录中的git.exe来实现的.

我设法让大多数命令工作(init,remote,status)但是在调用git add时出错了.到目前为止这是我的代码

import subprocess

gitPath = 'C:/path/to/git/cmd.exe'
repoPath = 'C:/path/to/my/repo'
repoUrl = 'https://www.github.com/login/repo';

#list to set directory and working tree
dirList = ['--git-dir='+repoPath+'/.git','--work-tree='+repoPath]


#init gitt
subprocess.call([gitPath] + ['init',repoPath]

#add remote
subprocess.call([gitPath] + dirList + ['remote','add','origin',repoUrl])

#Check status,returns files to be commited etc,so a working repo exists there
subprocess.call([gitPath] + dirList + ['status'])

#Adds all files in folder (this returns the error)
subprocess.call([gitPath] + dirList + ['add','.']

我得到的错误是:

fatal: Not a git repository (or any of the parent directories): .git

所以我搜索了这个错误,我找到的大多数解决方案都是关于不在正确的目录中.所以我的猜测也就是这样.但是,我不知道为什么. Git status返回目录中的正确文件,我已经设置了–git-dir和–work-tree

如果我去git shell我没有问题添加文件,但我不知道为什么这里出问题.

我不是在寻找使用pythons git库的解决方案.

函数Popen,call,check_callcheck_output一个cwd关键字参数,例如:

subprocess.call([gitPath] + dirList + ['add','.'],cwd='/home/me/workdir')

另见Specify working directory for popen

总结

以上是编程之家为你收集整理的Git通过python子进程添加全部内容,希望文章能够帮你解决Git通过python子进程添加所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶