I get these errors in my python script. It is designed to create a repo and then push the first commit with a readme file. It does not and gives me 3 errors instead.
Initialized empty Git repository in D:/Project/Repos/autoRepo/.git/
error: pathspec 'commit'' did not match any file(s) known to git
error: src refspec 'main' does not match any
error: failed to push some refs to 'origin'
from github import *
from secure import GITHUB_TOKEN
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument("--name", "-n", type=str, dest="name", required=True)
parser.add_argument("--private", "-p", dest="is_private", action="store_true")
args = parser.parse_args()
repo_name = args.name
is_private = args.is_private
g = Github(GITHUB_TOKEN) # safer alternative, if you have an access token
try:
x=0
x=x 1
print(x)
u = g.get_user()
repo = u.create_repo(repo_name, private=is_private)
REPO_PATH="D:\Project\Repos\\"
os.chdir (REPO_PATH)
os.system("mkdir " repo_name)
os.chdir(REPO_PATH repo_name)
os.system("git remote add origin [email protected]:TallKid420/" repo_name ".git")
os.system("echo '# " repo_name "e' >> README.md")
os.system("git init")
os.system("git add README.md")
os.system("git commit -m 'first commit'")
os.system("git branch -M 'main'")
os.system("git push -u origin 'main'")
except FileExistsError as err:
u.delete_repo(repo_name)
raise SystemExit(err)
I run it with this line in terminal
python d:/SCripts/SetupGitProject.py --name autoRepo -p
CodePudding user response:
First, this doesn't make any sense:
os.system("mkdir " repo_name)
os.chdir(REPO_PATH repo_name)
os.system("git remote add origin [email protected]:TallKid420/" repo_name ".git")
os.system("echo '# " repo_name "e' >> README.md")
os.system("git init")
First, you can't git remote add
in an empty directory; you need to initialize a git repository first. You should be seeing the error:
fatal: not a git repository (or any parent up to mount point /)
Second, there's no reason to create the empty directory like this; git init
will do that for you. So the above should become:
os.system(f"git init {repo_name}")
os.chdir(os.path.join(REPO_PATH, repo_name))
os.system(f"git remote add origin [email protected]:TallKid420/{repo_name}.git")
Then the remainder of your script becomes:
os.system(f"echo '# {repo_name}' >> README.md")
os.system("git add README.md")
os.system("git commit -m 'first commit'")
os.system("git branch -M 'main'")
os.system("git push -u origin main")
On my system, this runs without errors and successfully pushes to the remote repository. However, there are a number of things I would change about this script:
Use
subprocess
instead ofos.system
. You don't need to invoke the shell for all of these tasks:subprocess.check_call(['git', 'commit', '-m', 'first commit'])
Etc.
If you need to write text content to a file, just use Python.
with open('README.md', 'w') as fd: print(f'# {repo_name}', file=fd)
Rather than creating the remote repository, trying to create the local directory, then deleting the remote repository if the directory already exists locally...why not just check if the directory exists first, and just exit at that point?
CodePudding user response:
Hello thank you for your reply I applied the changes and I still get the errors not sure why. im so sorry if i did something wrong in the changes
from github import *
from secure import GITHUB_TOKEN
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument("--name", "-n", type=str, dest="name", required=True)
parser.add_argument("--private", "-p", dest="is_private", action="store_true")
args = parser.parse_args()
repo_name = args.name
is_private = args.is_private
g = Github(GITHUB_TOKEN) # safer alternative, if you have an access token
try:
u = g.get_user()
repo = u.create_repo(repo_name, private=is_private)
REPO_PATH="D:\Project\Repos\\"
os.chdir (REPO_PATH)
os.system(f"git init {repo_name}")
os.chdir(os.path.join(REPO_PATH, repo_name))
os.system(f"git remote add origin [email protected]:TallKid420/{repo_name}.git")
with open('README.md', 'w') as fd:
print(f'# {repo_name}', file=fd)
os.system("git add README.md")
os.system("git commit -m 'first commit'")
os.system("git branch -M 'main'")
os.system("git push -u origin main")
except FileExistsError as err:
raise SystemExit(err)