Andreas Hubert
Andreas Hubert

Reputation: 391

GitPython with remotes

I'm trying to solve an easy task with git in python. Actually I just want something like this

git clome remote-repo
git pull
edit file
git commit file
git push
rm -rf remote-repo

So just change a file and commit it and push it right away. But I have my problems get my head arround the documentation http://gitpython.readthedocs.org/en/stable/tutorial.html This is what I got so far

import git
import shutil
import os

repo_url = 'git@git.internal.server:users/me/mytest'
repo_dir = '/tmp/test/repo'
work_file_name = 'testFile'
work_file = os.path.join(repo_dir, work_file_name)
if os.path.isdir(repo_dir):
   shutil.rmtree(repo_dir)
repo = git.Repo.clone_from(repo_url, repo_dir)

git = repo.git
git.pull()
new_file_path = os.path.join(repo.working_tree_dir, work_file_name)
f = open(new_file_path, 'w')
f.write("just some test")
f.close()
repo.index.commit("test commit")

git.push()
shutil.rmtree(repo_dir)

It actually creates a commit and pushes it to the server, but it contains no changes, so the file I tried to write is empty. Any idea what I'm missing?

Upvotes: 0

Views: 450

Answers (1)

Schore
Schore

Reputation: 905

You have to stage the file/change before the commit:

repo.index.add([new_file_path])

As a side note: The following line overrides your import and might lead to errors if you ever expand your script:

git = repo.git

Upvotes: 3

Related Questions