Skip to content

Commit 84edc2c

Browse files
varmar05wonder-sk
authored andcommitted
added project status function (closes #24)
1 parent 39f3fad commit 84edc2c

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ For running test do:
2222
export TEST_MERGIN_URL=<url> # testing server
2323
export TEST_API_USERNAME=<username>
2424
export TEST_API_PASSWORD=<pwd>
25-
export GEODIFFLIB=<path to libgeodiff.so> # usually in mergin/deps/
2625
pipenv run pytest --cov-report html --cov=mergin test/
2726

2827

cli.py

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -117,50 +117,16 @@ def status():
117117

118118
try:
119119
mp = MerginProject(os.getcwd())
120-
project_info = mp.metadata
121120
except InvalidProject:
122121
click.secho('Invalid project directory', fg='red')
123122
return
124123

125-
project_name = project_info["name"]
126124
c = _init_client()
127-
128-
local_version = num_version(project_info["version"])
129-
130-
try:
131-
versions = c.project_versions(project_name)
132-
except Exception as e:
133-
click.secho(str(e), fg='red')
134-
return
135-
136-
click.echo("Current version: {}".format(project_info["version"]))
137-
new_versions = [v for v in versions if num_version(v["name"]) > local_version]
138-
if new_versions:
139-
click.secho("### Available updates: {}".format(len(new_versions)), fg="magenta")
140-
141-
# TODO: insufficient API, files could be included in versions,
142-
# or we should be able to request project_info at specific version
143-
server_files = c.project_info(project_name, since=local_version)["files"]
144-
145-
# changes between current files and last version on server
146-
# changes = project_changes(local_files, server_files)
147-
148-
# changes between versions on server
149-
changes = mp.get_pull_changes(server_files)
150-
151-
click.echo()
152-
click.secho("### Changes:", fg="magenta")
153-
pretty_diff(changes)
154-
click.echo()
155-
156-
changes = mp.get_push_changes()
157-
changes_count = get_changes_count(changes)
158-
if changes_count:
159-
click.secho("### Local changes: {}".format(changes_count), fg="magenta")
160-
pretty_diff(changes)
161-
else:
162-
click.secho("No local changes!", fg="magenta")
163-
# TODO: show conflicts
125+
pull_changes, push_changes = c.project_status(os.getcwd())
126+
click.secho("### Server changes:", fg="magenta")
127+
pretty_diff(pull_changes)
128+
click.secho("### Local changes:", fg="magenta")
129+
pretty_diff(push_changes)
164130

165131

166132
@cli.command()

mergin/client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,3 +1043,20 @@ def upload_chunk(chunk_id, data):
10431043
for chunk in file_meta["chunks"]:
10441044
data = file.read(UPLOAD_CHUNK_SIZE)
10451045
upload_chunk(chunk, data)
1046+
1047+
def project_status(self, directory):
1048+
"""
1049+
Get project status, e.g. server and local changes.
1050+
1051+
:param directory: Project's directory
1052+
:type directory: String
1053+
:returns: changes metadata for files modified on server, and for those modified locally
1054+
:rtype: dict, dict
1055+
"""
1056+
mp = MerginProject(directory)
1057+
project_path = mp.metadata["name"]
1058+
local_version = mp.metadata["version"]
1059+
server_info = self.project_info(project_path, since=local_version)
1060+
pull_changes = mp.get_pull_changes(server_info["files"])
1061+
push_changes = mp.get_push_changes()
1062+
return pull_changes, push_changes

mergin/test/test_client.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ def test_push_pull_changes(mc, parallel):
129129
with open(os.path.join(project_dir, f_updated), 'w') as f:
130130
f.write('Modified')
131131

132+
# check changes before applied
133+
pull_changes, push_changes = mc.project_status(project_dir)
134+
assert not sum(len(v) for v in pull_changes.values())
135+
assert next((f for f in push_changes['added'] if f['path'] == f_added), None)
136+
assert next((f for f in push_changes['removed'] if f['path'] == f_removed), None)
137+
assert next((f for f in push_changes['updated'] if f['path'] == f_updated), None)
138+
assert next((f for f in push_changes['renamed'] if f['path'] == f_renamed), None)
139+
132140
mc.push_project(project_dir, parallel=parallel)
133141
project_info = mc.project_info(project)
134142
assert project_info['version'] == 'v2'
@@ -154,6 +162,14 @@ def test_push_pull_changes(mc, parallel):
154162
with pytest.raises(ClientError, match='Update your local repository'):
155163
mc.push_project(project_dir_2)
156164

165+
# check changes in project_dir_2 before applied
166+
pull_changes, push_changes = mc.project_status(project_dir_2)
167+
assert next((f for f in pull_changes['added'] if f['path'] == f_added), None)
168+
assert next((f for f in pull_changes['removed'] if f['path'] == f_removed), None)
169+
assert next((f for f in pull_changes['updated'] if f['path'] == f_updated), None)
170+
assert next((f for f in pull_changes['renamed'] if f['path'] == f_renamed), None)
171+
assert next((f for f in push_changes['updated'] if f['path'] == f_updated), None)
172+
157173
mc.pull_project(project_dir_2, parallel=parallel)
158174
assert os.path.exists(os.path.join(project_dir_2, f_added))
159175
assert not os.path.exists(os.path.join(project_dir_2, f_removed))
@@ -222,7 +238,6 @@ def toggle_geodiff(enabled):
222238
# step 1) base.gpkg updated to inserted_1_A (inserted A feature)
223239
if push_geodiff_enabled:
224240
shutil.move(mp.fpath(f_updated), mp.fpath_meta(f_updated)) # make local copy for changeset calculation
225-
pass
226241
shutil.copy(mp.fpath('inserted_1_A.gpkg'), mp.fpath(f_updated))
227242
mc.push_project(project_dir)
228243
if push_geodiff_enabled:

0 commit comments

Comments
 (0)