简体   繁体   中英

gitpython creating zip archive

How can I create an archive with gitpython, I tried the following which creates the file but I can't open it it tells me an error occurred reading archive the archive appears to be invalid or damaged

from git import *
repo = Repo(repo_path)
assert repo.bare == False
repo.archive(open("repo.tar",'w'))

I would like to create a zip file so then I tried this but here it creates an empty zip file (the path to repo is correct as when I use repo.clone it propery clones everything)

repo.archive(open("repo.zip",'w'), format="zip") 

You need to open your file as binary (adding b to the mode parameter) and close it when you finish. The following modifications to your example make it work correctly:

from git import Repo

repo = Repo(repo_path)
assert not repo.bare
with open('repo.zip', 'wb') as archive_file:
    repo.archive(archive_file, format='zip')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM