From 957a1fd9b8b07d594aca94c32802d011d0cf5517 Mon Sep 17 00:00:00 2001 From: Amber Date: Tue, 29 Oct 2024 16:58:03 +0100 Subject: [PATCH] sync_manager --- src/iface/sync_manager.py | 132 ++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 69 deletions(-) diff --git a/src/iface/sync_manager.py b/src/iface/sync_manager.py index c2a58c3..27d622a 100644 --- a/src/iface/sync_manager.py +++ b/src/iface/sync_manager.py @@ -292,54 +292,54 @@ class Manager(): return agent.copy_dir(absolute_local_path, remote_mount_point) #maybe useless - def precheck_remote_unode(self, node): - ''' - Check the unmerged node on remote before solve conflict - Consideration about unode: - 1. We have generated `cur_hash` starting from `last_hash` version of node on local tree - 2. We are aware on remote the hash is changed at the moment we `sync`, the value is stored in `remote_hash` - - we expect - `remote_hash` is unchanged, then we can proceed with solving the conflict - `remote_hash` is changed, then we can generate a new conflict with `remote_hash` equals to the new remote hash - - ''' - rstatus = self.get_rnode_status(node) - rfile_buf, rhash, rexists = map(rstatus.get, ['iobuffer', 'hash', 'exists']) - - remote_hash = node['remote_hash'] - - if not (remote_hash == rhash): - unmerged = self.load_unmerged_diff() - name = node['name'] - path = node['rel_path'] - - unmerged = self.load_unmerged_diff() - - found = {} - for n, unode in enumerate(unmerged): - if unode['name'] == name and unode['rel_path'] == path: - found = unode - break - else: - n = -1 - - if n == -1: - raise Exception('No node conflicting found!!') - - print(f'remote hash changed you...generate a new conflict') - ## update the conflicting node by index - # (cur_hash, cur_buf) = _genlocal.generate_file_hash(self.get_absolute_local_path(found)) - found['cur_hash'] = cur_hash - found['last_hash'] = found['remote_hash'] - found['remote_hash'] = rhash - found['remote_file_buf'] = rfile_buf - self.store_unmerged_diff(unmerged) - ## store diff in diffs path - self.store_fdiff(found) - return False, 'Generate conflict' - - return True, 'You can proceed' + # def precheck_remote_unode(self, node): + # ''' + # Check the unmerged node on remote before solve conflict + # Consideration about unode: + # 1. We have generated `cur_hash` starting from `last_hash` version of node on local tree + # 2. We are aware on remote the hash is changed at the moment we `sync`, the value is stored in `remote_hash` + # + # we expect + # `remote_hash` is unchanged, then we can proceed with solving the conflict + # `remote_hash` is changed, then we can generate a new conflict with `remote_hash` equals to the new remote hash + # + # ''' + # rstatus = self.get_rnode_status(node) + # rfile_buf, rhash, rexists = map(rstatus.get, ['iobuffer', 'hash', 'exists']) + # + # remote_hash = node['remote_hash'] + # + # if not (remote_hash == rhash): + # unmerged = self.load_unmerged_diff() + # name = node['name'] + # path = node['rel_path'] + # + # unmerged = self.load_unmerged_diff() + # + # found = {} + # for n, unode in enumerate(unmerged): + # if unode['name'] == name and unode['rel_path'] == path: + # found = unode + # break + # else: + # n = -1 + # + # if n == -1: + # raise Exception('No node conflicting found!!') + # + # print(f'remote hash changed you...generate a new conflict') + # ## update the conflicting node by index + # # (cur_hash, cur_buf) = _genlocal.generate_file_hash(self.get_absolute_local_path(found)) + # found['cur_hash'] = cur_hash + # found['last_hash'] = found['remote_hash'] + # found['remote_hash'] = rhash + # found['remote_file_buf'] = rfile_buf + # self.store_unmerged_diff(unmerged) + # ## store diff in diffs path + # self.store_fdiff(found) + # return False, 'Generate conflict' + # + # return True, 'You can proceed' @@ -392,33 +392,32 @@ class Manager(): # raise Exception pass - def check_for_conflict(self, node, onlyprint=False): + def check_remote_for_conflict(self, node): ''' i receive a node i'll check on remote the hash for verify if equal to `last_hash` and eventually i generate a conflict + i return if there is a conflict and the unmerged node ''' conflicting = False rstatus = self.get_rnode_status(node) rfile_buf, rhash, rexists = map(rstatus.get, ['iobuffer', 'hash', 'exists']) + print(f'Remote status file exists {rexists}, {rhash}') remote_hash = node['remote_hash'] if rhash == remote_hash: - return conflicting + return conflicting, {} conflicting = True + unode = node.copy() # (cur_hash, cur_buf) = _genlocal.generate_file_hash(self.get_absolute_local_path(node)) # node['cur_hash'] = cur_hash - # node['last_hash'] = found['remote_hash'] - node['remote_hash'] = rhash - node['remote_file_buf'] = rfile_buf - if onlyprint: - return conflicting - self.store_unmerged_diff(unmerged) - ## store diff in diffs path - self.store_fdiff(found) - return conflicting + unode['last_hash'] = node['remote_hash'] + unode['remote_hash'] = rhash + unode['remote_file_buf'] = rfile_buf + + return conflicting, unode def mark_conflicting_node_as_solved(self, node): name = node['name'] @@ -441,21 +440,16 @@ class Manager(): ## if you mark the node as conflict solved ## you have incorporated the `remote_hash` ## you must verify this is the actual hash in the server side - rstatus = self.get_rnode_status(node) - rfile_buf, rhash, rexists = map(rstatus.get, ['iobuffer', 'hash', 'exists']) - if not (remote_hash == rhash): + conflicting, unode = self.check_remote_for_conflict(node) + if conflicting: print(f'remote hash changed you...generate a new conflict') - ## update the conflicting node by index - (cur_hash, cur_buf) = _genlocal.generate_file_hash(self.get_absolute_local_path(found)) - found['cur_hash'] = cur_hash - found['last_hash'] = found['remote_hash'] - found['remote_hash'] = rhash - found['remote_file_buf'] = rfile_buf + unmerged[n] = unode self.store_unmerged_diff(unmerged) ## store diff in diffs path - self.store_fdiff(found) + self.store_fdiff(unode) return + ## found the node in unmerged path # unmerged_local_path = self.get_unmerged_local_path(node) # absolute_remote_path = self.get_absolute_remote_path(node)