Skip to content

Commit

Permalink
Merge pull request #894 from koordinates/attachments
Browse files Browse the repository at this point in the history
Fix up X_KART_ATTACHMENTS checkout
  • Loading branch information
olsen232 committed Jul 31, 2023
2 parents 2ea2d07 + 4380b85 commit 8458fd5
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions kart/workdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
from enum import Enum, auto
import functools
from pathlib import Path
import shutil
import sys
from kart.structure import RepoStructure
Expand All @@ -20,7 +19,6 @@
from kart.diff_structs import Delta, DatasetDiff
from kart.exceptions import (
NotFound,
SubprocessError,
NO_WORKING_COPY,
translate_subprocess_exit_code,
)
Expand All @@ -35,7 +33,6 @@
from kart.tile import ALL_TILE_DATASET_TYPES
from kart.tile.tile_dataset import TileDataset
from kart.tile.tilename_util import (
remove_any_tile_extension,
case_insensitive,
PAM_SUFFIX,
)
Expand Down Expand Up @@ -423,7 +420,9 @@ def reset(
kart_attachments = os.environ.get("X_KART_ATTACHMENTS")
if kart_attachments:
if base_tree and target_tree:
self.update_file_diffs(base_tree, target_tree, workdir_index)
self.write_attached_files_to_workdir(
base_tree, target_tree, workdir_index, track_changes_as_dirty
)

for ds_path in ds_updates:
self._update_dataset_in_workdir(
Expand All @@ -441,26 +440,36 @@ def reset(
sess, self.repo.spatial_filter.hexhash
)

def update_file_diffs(self, base_tree, target_tree, workdir_index):
def write_attached_files_to_workdir(
self, base_tree, target_tree, workdir_index, track_changes_as_dirty=False
):
"""Get the deltas for attachment files and write them to the working copy."""
write_to_index = not track_changes_as_dirty

repo = self.repo
base_rs = RepoStructure(repo, base_tree)
target_rs = RepoStructure(repo, target_tree)
attachment_deltas = get_file_diff(base_rs, target_rs)

for filename, file_delta in attachment_deltas.items():
new_path = self.path / filename

# Delete the old file
if file_delta.old and new_path.is_file():
new_path.unlink()
for file_delta in attachment_deltas.values():
for filename in set(filter(None, (file_delta.old_key, file_delta.new_key))):
workdir_path = self.path / filename
if workdir_path.is_file():
workdir_path.unlink()
if write_to_index:
workdir_index.remove_all([filename])

# Create a new file
if file_delta.new:
blob_data = repo[file_delta.new.value].data
new_path.write_bytes(blob_data)

# TODO: update workdir_index.
workdir_path = self.path / file_delta.new_key
blob_data = repo[file_delta.new_value].data
workdir_path.write_bytes(blob_data)
if write_to_index:
workdir_index.add_entry_with_custom_stat(
pygit2.IndexEntry(
filename, pygit2.hash(blob_data), pygit2.GIT_FILEMODE_BLOB
),
workdir_path,
)

def _diff_to_reset(
self, ds_path, base_datasets, target_datasets, workdir_diff_cache, ds_filter
Expand Down

0 comments on commit 8458fd5

Please sign in to comment.