Skip to content

Commit eedae02

Browse files
committed
adjust upload
1 parent 36a539d commit eedae02

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

dataherb/cmd/sync_git.py

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,72 @@
1+
import sys
2+
import json
13
import click
4+
import git
5+
from pathlib import Path
26

37

4-
def upload_dataset_to_git(source, target):
8+
def is_git_repo(path):
9+
try:
10+
_ = git.Repo(path).git_dir
11+
return True
12+
except git.exc.InvalidGitRepositoryError:
13+
return False
14+
15+
16+
def get_dataherb(source):
17+
18+
if not (Path(source) / "dataherb.json").exists():
19+
click.echo(
20+
f"No dataherb.json found in {source}. Please run `dataherb create` first."
21+
)
22+
sys.exit()
23+
24+
with open(Path(source) / "dataherb.json", "r") as f:
25+
data = json.load(f)
26+
27+
return data
28+
29+
30+
def upload_dataset_to_git(source, target, experimental=False):
31+
"""uploads local folder to remote"""
32+
33+
is_git_initialized = is_git_repo(source)
34+
35+
if not experimental:
36+
text = (
37+
f"git sync is still a WIP.\n"
38+
f"Please go to {source} and sync your git repository to {target} manually.\n"
39+
)
40+
if is_git_initialized:
41+
text += (
42+
f"Note: simply add, commit and push."
43+
)
44+
else:
45+
text += (
46+
f"Note: git init your repo, commit, add remote {target}, and push."
47+
)
48+
click.echo(text)
49+
else:
50+
if is_git_initialized:
51+
repo = git.Repo(source)
52+
repo.index.add(["*"])
53+
repo.index.commit("created datset: added dataherb.json")
54+
55+
if len(repo.remotes) == 0:
56+
origin = repo.create_remote('origin', target)
57+
assert origin.exists()
58+
origin.fetch()
59+
repo.create_head('master', origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
60+
origin.push()
61+
else:
62+
repo.git.push()
63+
else:
64+
repo = git.Repo.init(source)
65+
repo.git.add(["*"])
66+
repo.index.commit("initial commit")
67+
origin = repo.create_remote('origin', target)
68+
assert origin.exists()
69+
origin.fetch()
70+
repo.create_head('master', origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
71+
origin.push()
572

6-
click.echo(
7-
f"git sync has not yet been implemented.\n"
8-
f"Please go to {source} and sync your git repository to {target} manually."
9-
)

dataherb/command.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ def remove(flora, herb_id):
287287
"All contents in this folder will be uploaded.\n"
288288
"Are you sure this is the correct path?"
289289
)
290-
def upload():
290+
@click.option("--experimental", "-e", default=False)
291+
def upload(experimental):
291292
"""
292293
upload dataset in the current folder to the remote destination
293294
"""
@@ -312,7 +313,7 @@ def upload():
312313
if md.metadata.get("source") == "s3":
313314
upload_dataset_to_s3(__CWD__, md_uri)
314315
elif md.metadata.get("source") == "git":
315-
upload_dataset_to_git(__CWD__, md_uri)
316+
upload_dataset_to_git(__CWD__, md_uri, experimental=experimental)
316317

317318

318319
@dataherb.command()

0 commit comments

Comments
 (0)