Skip to content

Commit 7a97d17

Browse files
authored
Merge pull request #32 from lutraconsulting/show_list_of_changes
#61 show list of changes
2 parents d14b17c + 0436676 commit 7a97d17

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

cli.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def get_changes_count(diff):
2222
attrs = ["added", "removed", "updated", "renamed"]
2323
return sum([len(diff[attr]) for attr in attrs])
2424

25+
2526
def pretty_diff(diff):
2627
added = diff["added"]
2728
removed = diff["removed"]
@@ -46,6 +47,13 @@ def pretty_diff(diff):
4647
click.secho("\n".join("M " + f["path"] for f in updated), fg="yellow")
4748

4849

50+
def pretty_summary(summary):
51+
for k, v in summary.items():
52+
click.secho("Details " + k)
53+
click.secho("".join("layer name - " + d["table"] + ": inserted: " + str(d["insert"]) + ", modified: " +
54+
str(d["update"]) + ", deleted: " + str(d["delete"]) + "\n" for d in v['geodiff_summary'] if d["table"] != "gpkg_contents"))
55+
56+
4957
def _init_client():
5058
url = os.environ.get('MERGIN_URL')
5159
auth_token = os.environ.get('MERGIN_AUTH')
@@ -122,11 +130,13 @@ def status():
122130
return
123131

124132
c = _init_client()
125-
pull_changes, push_changes = c.project_status(os.getcwd())
133+
pull_changes, push_changes, push_changes_summary = c.project_status(os.getcwd())
126134
click.secho("### Server changes:", fg="magenta")
127135
pretty_diff(pull_changes)
128136
click.secho("### Local changes:", fg="magenta")
129137
pretty_diff(push_changes)
138+
click.secho("### Local changes summary ###")
139+
pretty_summary(push_changes_summary)
130140

131141

132142
@cli.command()

mergin/client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,28 @@ def get_push_changes(self):
321321
}
322322
else:
323323
not_updated.append(file)
324-
except (pygeodiff.GeoDiffLibError, pygeodiff.GeoDiffLibConflictError):
324+
except (pygeodiff.GeoDiffLibError, pygeodiff.GeoDiffLibConflictError) as e:
325325
pass # we do force update
326326

327327
changes['updated'] = [f for f in changes['updated'] if f not in not_updated]
328328
return changes
329329

330+
def get_list_of_push_changes(self, push_changes):
331+
changes = {}
332+
for idx, file in enumerate(push_changes["updated"]):
333+
if "diff" in file:
334+
changeset_path = file["diff"]["path"]
335+
changeset = self.fpath_meta(changeset_path)
336+
result_file = self.fpath("change_list" + str(idx), self.meta_dir)
337+
try:
338+
self.geodiff.list_changes_summary(changeset, result_file)
339+
change = open(result_file, "r").read()
340+
changes[file["path"]] = json.loads(change)
341+
os.remove(result_file)
342+
except (pygeodiff.GeoDiffLibError, pygeodiff.GeoDiffLibConflictError):
343+
pass
344+
return changes
345+
330346
def apply_pull_changes(self, changes, temp_dir):
331347
"""
332348
Apply changes pulled from server.
@@ -1085,5 +1101,8 @@ def project_status(self, directory):
10851101
local_version = mp.metadata["version"]
10861102
server_info = self.project_info(project_path, since=local_version)
10871103
pull_changes = mp.get_pull_changes(server_info["files"])
1104+
10881105
push_changes = mp.get_push_changes()
1089-
return pull_changes, push_changes
1106+
push_changes_summary = mp.get_list_of_push_changes(push_changes)
1107+
1108+
return pull_changes, push_changes, push_changes_summary

mergin/test/test_client.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_data')
1313

1414

15+
def toggle_geodiff(enabled):
16+
os.environ['GEODIFF_ENABLED'] = str(enabled)
17+
18+
1519
@pytest.fixture(scope='function')
1620
def mc():
1721
assert SERVER_URL and SERVER_URL.rstrip('/') != 'https://public.cloudmergin.com' and API_USER and USER_PWD
@@ -130,7 +134,7 @@ def test_push_pull_changes(mc, parallel):
130134
f.write('Modified')
131135

132136
# check changes before applied
133-
pull_changes, push_changes = mc.project_status(project_dir)
137+
pull_changes, push_changes, _ = mc.project_status(project_dir)
134138
assert not sum(len(v) for v in pull_changes.values())
135139
assert next((f for f in push_changes['added'] if f['path'] == f_added), None)
136140
assert next((f for f in push_changes['removed'] if f['path'] == f_removed), None)
@@ -163,7 +167,7 @@ def test_push_pull_changes(mc, parallel):
163167
mc.push_project(project_dir_2)
164168

165169
# check changes in project_dir_2 before applied
166-
pull_changes, push_changes = mc.project_status(project_dir_2)
170+
pull_changes, push_changes, _ = mc.project_status(project_dir_2)
167171
assert next((f for f in pull_changes['added'] if f['path'] == f_added), None)
168172
assert next((f for f in pull_changes['removed'] if f['path'] == f_removed), None)
169173
assert next((f for f in pull_changes['updated'] if f['path'] == f_updated), None)
@@ -211,8 +215,6 @@ def test_ignore_files(mc):
211215

212216
@pytest.mark.parametrize("diffs_limit, push_geodiff_enabled, pull_geodiff_enabled", diff_test_scenarios)
213217
def test_sync_diff(mc, diffs_limit, push_geodiff_enabled, pull_geodiff_enabled):
214-
def toggle_geodiff(enabled):
215-
os.environ['GEODIFF_ENABLED'] = str(enabled)
216218

217219
test_project = f'test_sync_diff_{diffs_limit}_{int(push_geodiff_enabled)}_{int(pull_geodiff_enabled)}'
218220
project = API_USER + '/' + test_project
@@ -303,3 +305,26 @@ def toggle_geodiff(enabled):
303305
assert not mp3.geodiff.has_changes(mp.fpath_meta('diff'))
304306
else:
305307
assert os.path.exists(mp.fpath('base.gpkg_conflict_copy'))
308+
309+
310+
def test_list_of_push_changes(mc):
311+
PUSH_CHANGES_SUMMARY = "{'base.gpkg': {'geodiff_summary': [{'table': 'gpkg_contents', 'insert': 0, 'update': 1, 'delete': 0}, {'table': 'simple', 'insert': 1, 'update': 0, 'delete': 0}]}}"
312+
313+
test_project = 'test_list_of_push_changes'
314+
project = API_USER + '/' + test_project
315+
project_dir = os.path.join(TMP_DIR, test_project) # primary project dir for updates
316+
317+
cleanup(mc, project, [project_dir])
318+
shutil.copytree(TEST_DATA_DIR, project_dir)
319+
toggle_geodiff(True)
320+
mc.create_project(test_project, project_dir)
321+
322+
f_updated = 'base.gpkg'
323+
mp = MerginProject(project_dir)
324+
325+
shutil.copy(mp.fpath('inserted_1_A.gpkg'), mp.fpath(f_updated))
326+
327+
pull_changes, push_changes, push_changes_summary = mc.project_status(project_dir)
328+
assert str(push_changes_summary) == PUSH_CHANGES_SUMMARY
329+
330+

0 commit comments

Comments
 (0)