|
| 1 | +import sys |
| 2 | +import json |
1 | 3 | import click |
| 4 | +import git |
| 5 | +from pathlib import Path |
2 | 6 |
|
3 | 7 |
|
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() |
5 | 72 |
|
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 | | - ) |
|
0 commit comments