Git add through python subprocess

2024/10/15 20:20:53

I am trying to run git commands through python subprocess. I do this by calling the git.exe in the cmd directory of github.

I managed to get most commands working (init, remote, status) but i get an error when calling git add. This is my code so far:

import subprocessgitPath = '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','.']

The error i get is:

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

So i searched for this error, and most solutions i found were about not being in the right directory. So my guess would also be that. However, i do not know why. Git status returns the correct files in the directory, and i have set --git-dir and --work-tree

If i go to git shell i have no problem adding files, but i cannot find out why things go wrong here.

I am not looking for a solution using pythons git library.

Answer

You need to specify the working directory.

Functions Popen, call, check_call, and check_output have a cwd keyword argument to do so, e.g.:

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

See also Specify working directory for popen

https://en.xdnf.cn/q/69244.html

Related Q&A

How to unread a line in python

I am new to Python (2.6), and have a situation where I need to un-read a line I just read from a file. Heres basically what I am doing.for line in file:print linefile.seek(-len(line),1)zz = file.readli…

typeerror bytes object is not callable

My code:import psycopg2 import requests from urllib.request import urlopen import urllib.parse uname = " **** " pwd = " ***** " resp = requests.get("https://api.flipkart.net/se…

How to inverse lemmatization process given a lemma and a token?

Generally, in natural language processing, we want to get the lemma of a token. For example, we can map eaten to eat using wordnet lemmatization.Is there any tools in python that can inverse lemma to a…

NameError: name N_TOKENS is not defined

I am new on Python and just got around to install PyCharm for Windows. Downloaded some sample code from Skype for testing their SkypeKit API. But... As soon as I hit the debug button, I get this: (I ha…

Moving Spark DataFrame from Python to Scala whithn Zeppelin

I created a spark DataFrame in a Python paragraph in Zeppelin.sqlCtx = SQLContext(sc) spDf = sqlCtx.createDataFrame(df)and df is a pandas dataframeprint(type(df)) <class pandas.core.frame.DataFrame&…

How do I efficiently do a bulk insert-or-update with SQLAlchemy?

Im using SQLAlchemy with a Postgres backend to do a bulk insert-or-update. To try to improve performance, Im attempting to commit only once every thousand rows or so:trans = engine.begin()for i, rec in…

How to pass variables from javascript to python in Jupyter?

As I understand it, I should be able to print the variable foo in the snippet below. from IPython.display import HTML HTML(<script type="text/javascript">IPython.notebook.kernel.execute…

SVR Model --Feature Scaling - Expected 2D array, got 1D array instead

I am trying to understand what is wrong with the code below. I know that the Y variable is 1D array and expected to be 2D array and need to reshape the structure but that code was working previously fi…

How to find the version of jupyter notebook from within the notebook

I wish to return the version of Jupyter Notebook from within a cell of a notebook. For example, to get the python version, I run: from platform import python_version python_version()or to get the panda…

Python logging - multiple modules

Im working on a small python project that has the following structure -project -- logs-- project__init.py__classA.pyclassB.pyutils.py-- main.pyIve set up the logging configuration in __init.py__ under …