@@ -531,49 +531,63 @@ def pull_project_async(mc, directory) -> Optional[PullJob]:
531531 # list of FileToMerge instances, which consists of destination path and list of download items
532532 merge_files = []
533533 diff_files = []
534- # make sure we can update geodiff reference files (aka. basefiles) with diffs or
535- # download their full versions so we have them up-to-date for applying changes
536534 basefiles_to_patch = [] # list of tuples (relative path within project, list of diff files in temp dir to apply)
537535 pull_actions = []
538536 local_delta = mp .get_local_delta (tmp_dir .name )
537+ # Converting local to PullActions
539538 for item in delta .items :
539+ # find corresponding local delta item
540540 local_item = next ((i for i in local_delta if i .path == item .path ), None )
541- pull_action = mp .get_pull_action (item .change , local_item .change if local_item else None )
542- if not pull_action :
541+ local_item_change = local_item .change if local_item else None
542+
543+ # compare server and local changes to decide what to do in pull
544+ pull_action_type = mp .get_pull_action (item .change , local_item_change )
545+ if not pull_action_type :
543546 continue # no action needed
544547
545- pull_actions . append ( PullAction (pull_action , item , local_item ) )
546- if pull_action == PullActionType .APPLY_DIFF or (
547- pull_action == PullActionType .COPY_CONFLICT and item .change == DeltaChangeType .UPDATE_DIFF
548+ pull_action = PullAction (pull_action_type , item , local_item )
549+ if pull_action_type == PullActionType .APPLY_DIFF or (
550+ pull_action_type == PullActionType .COPY_CONFLICT and item .change == DeltaChangeType .UPDATE_DIFF
548551 ):
552+ basefile = mp .fpath_meta (item .path )
553+ if not os .path .exists (basefile ):
554+ # The basefile does not exist for some reason. This should not happen normally (maybe user removed the file
555+ # or we removed it within previous pull because we failed to apply patch the older version for some reason).
556+ # But it's not a problem - we will download the newest version and we're sorted.
557+ mp .log .info (f"missing base file for { item .path } -> going to download it (version { server_version } )" )
558+ items = get_download_items (item .path , item .size , server_version , tmp_dir .name )
559+ dest_file_path = mp .fpath (item .path , tmp_dir .name )
560+ merge_files .append (FileToMerge (dest_file_path , items ))
561+
562+ # Force use COPY action to apply the new version instead of trying to apply diffs
563+ # We are not able to get local changes anyway as base file is missing
564+ pull_action .type = PullActionType .COPY_CONFLICT
565+
549566 # if we have diff to apply, let's download the diff files
550567 # if we have conflict and diff update, download the diff files
551- if v2_pull :
568+ elif v2_pull :
552569 diff_files .extend (
553570 [
554571 DownloadDiffQueueItem (diff_item .id , os .path .join (tmp_dir .name , diff_item .id ))
555572 for diff_item in item .diffs
556573 ]
557574 )
575+ basefiles_to_patch .append ((item .path , [diff .id for diff in item .diffs ]))
576+
558577 else :
578+ # fallback for diff files using v1 endpoint /raw
559579 diff_merge_files = get_diff_merge_files (item , tmp_dir .name )
560580 merge_files .extend (diff_merge_files )
581+ basefiles_to_patch .append ((item .path , [diff .id for diff in item .diffs ]))
582+
561583 # let's check the base file existence
562- basefile = mp .fpath_meta (item .path )
563- if not os .path .exists (basefile ):
564- # The basefile does not exist for some reason. This should not happen normally (maybe user removed the file
565- # or we removed it within previous pull because we failed to apply patch the older version for some reason).
566- # But it's not a problem - we will download the newest version and we're sorted.
567- mp .log .info (f"missing base file for { item .path } -> going to download it (version { server_version } )" )
568- items = get_download_items (item .path , item .size , server_version , tmp_dir .name )
569- dest_file_path = mp .fpath (item .path , tmp_dir .name )
570- merge_files .append (FileToMerge (dest_file_path , items ))
571- basefiles_to_patch .append ((item .path , [diff .id for diff in item .diffs ]))
572- elif pull_action == PullActionType .COPY or pull_action == PullActionType .COPY_CONFLICT :
584+ elif pull_action_type == PullActionType .COPY or pull_action_type == PullActionType .COPY_CONFLICT :
573585 # simply download the server version of the files
574586 dest_file_path = prepare_file_destination (tmp_dir .name , item .path )
575587 download_items = get_download_items (item .path , item .size , server_version , tmp_dir .name )
576588 merge_files .append (FileToMerge (dest_file_path , download_items ))
589+
590+ pull_actions .append (pull_action )
577591 # Do nothing for DELETE actions
578592
579593 # make a single list of items to download
@@ -693,7 +707,7 @@ def pull_project_finalize(job: PullJob):
693707 basefile = job .mp .fpath_meta (file_path )
694708 server_file = job .mp .fpath (file_path , job .tmp_dir .name )
695709
696- shutil . copy (basefile , server_file )
710+ job . mp . geodiff . make_copy_sqlite (basefile , server_file )
697711 diffs = [job .mp .fpath (f , job .tmp_dir .name ) for f in file_diffs ]
698712 patch_error = job .mp .apply_diffs (server_file , diffs )
699713 if patch_error :
@@ -709,6 +723,7 @@ def pull_project_finalize(job: PullJob):
709723 os .remove (basefile )
710724 raise ClientError ("Cannot patch basefile {}! Please try syncing again." .format (basefile ))
711725 conflicts = []
726+ job .mp .log .info (f"--- applying pull actions { job .pull_actions } " )
712727 try :
713728 if job .pull_actions :
714729 conflicts = job .mp .apply_pull_actions (job .pull_actions , job .tmp_dir .name , job .project_info , job .mc )
0 commit comments