加入收藏 | 设为首页 | 会员中心 | 我要投稿 大庆站长网 (https://www.0459zz.com/)- 科技、智能边缘云、事件网格、云计算、站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Git通过python子进程添加

发布时间:2020-09-18 13:21:54 所属栏目: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

(编辑:大庆站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读