Write treeish into an archive. If no timestamp is provided and 'treeish' is a commit, its committer timestamp will be used. Otherwise the current time will be used. All path names in the archive are added to 'prefix', which defaults to an empty string.
(
self,
treeish: str | Tree | Object | Oid,
archive: tarfile.TarFile,
timestamp: int | None = None,
prefix: str = '',
)
| 1393 | # Utility for writing a tree into an archive |
| 1394 | # |
| 1395 | def write_archive( |
| 1396 | self, |
| 1397 | treeish: str | Tree | Object | Oid, |
| 1398 | archive: tarfile.TarFile, |
| 1399 | timestamp: int | None = None, |
| 1400 | prefix: str = '', |
| 1401 | ) -> None: |
| 1402 | """ |
| 1403 | Write treeish into an archive. |
| 1404 | |
| 1405 | If no timestamp is provided and 'treeish' is a commit, its committer |
| 1406 | timestamp will be used. Otherwise the current time will be used. |
| 1407 | |
| 1408 | All path names in the archive are added to 'prefix', which defaults to |
| 1409 | an empty string. |
| 1410 | |
| 1411 | Parameters: |
| 1412 | |
| 1413 | treeish |
| 1414 | The treeish to write. |
| 1415 | |
| 1416 | archive |
| 1417 | An archive from the 'tarfile' module. |
| 1418 | |
| 1419 | timestamp |
| 1420 | Timestamp to use for the files in the archive. |
| 1421 | |
| 1422 | prefix |
| 1423 | Extra prefix to add to the path names in the archive. |
| 1424 | |
| 1425 | Example:: |
| 1426 | |
| 1427 | >>> import tarfile, pygit2 |
| 1428 | >>> with tarfile.open('foo.tar', 'w') as archive: |
| 1429 | >>> repo = pygit2.Repository('.') |
| 1430 | >>> repo.write_archive(repo.head.target, archive) |
| 1431 | """ |
| 1432 | |
| 1433 | # Try to get a tree form whatever we got |
| 1434 | if isinstance(treeish, (str, Oid)): |
| 1435 | treeish = self[treeish] |
| 1436 | |
| 1437 | tree = treeish.peel(Tree) |
| 1438 | |
| 1439 | # if we don't have a timestamp, try to get it from a commit |
| 1440 | if not timestamp: |
| 1441 | try: |
| 1442 | commit = treeish.peel(Commit) |
| 1443 | timestamp = commit.committer.time |
| 1444 | except Exception: |
| 1445 | pass |
| 1446 | |
| 1447 | # as a last resort, use the current timestamp |
| 1448 | if not timestamp: |
| 1449 | timestamp = int(time()) |
| 1450 | |
| 1451 | index = Index() |
| 1452 | index.read_tree(tree) |