Reputation: 49
How can I use command (git archive --remote) using GitPython? As per the GitPython docs we can use git directly. I am doing something like:
git = repo.git git.archive(remote= 'http://path')
But getting an error "Exception is : Cmd('git') failed due to: exit code(1)"
Is there any sample that I can look at to execute git archive --remote in a python script?
Thanks
Upvotes: 4
Views: 740
Reputation: 4679
This question is quite old, but I came across the same problem, so here's my solution:
import git
import shutil
url = 'ssh://url-to.my/repo.git'
remote_ref = 'master'
tmprepo = 'temprepo'
tarball = 'contents.tar'
try:
repo = git.Repo.init(tmprepo)
repo.create_remote('origin', url)
repo.remote().fetch(remote_ref)
with open(tarball, 'wb') as f:
repo.archive(f, f'remotes/origin/{remote_ref}', path=None)
print('Success')
finally:
shutil.rmtree(tmprepo)
A few notes:
path
parameter to something meaningful in case you only want to include a subset of the directoryfetch()
call can probably be optimized. The **kwargs
taken by the functions may help here (see man git-fetch
)Upvotes: 0