3

Is there a way to apply a patch (in the form of a diff file) to a repo using only the gitpython library?

In other words, is there a gitpython equivalent of the git-apply command?

2 Answers 2

4

Solution is to do

r = Repo('path-to-repo')
r.git.execute(['git','apply','patch.diff'])

I had tried this before but I had omitted the 'git' at the beginning of the argument list, which gave an error about the command not existing.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there no API in gitpython to do this directly? I am asking this because, most of the time we have a string which contains diff. In that case we will have to first write the diff to a file - then run r.git.execute and then clean up the file.
as far as i know there is nothing in the API. are you familiar with tempfile, I think that will make your scenario fairly painless
2

You can use git directly, like this:

 repo = git.Repo('repository_path') 

 repo.git.apply(['-3', 'patch-file'])

this will perform the git command:

git apply -3 patch-file

1 Comment

Don't use links directly, try to paste some of the content to your answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.