1
- #! /usr/bin/env python3
2
-
3
1
import enum
4
2
import hashlib
5
3
import json
29
27
from dvc_data .hashfile .hash_info import HashInfo
30
28
from dvc_data .hashfile .state import State
31
29
from dvc_data .objects .tree import Tree , merge
30
+ from dvc_data .repo import NotARepo , Repo
32
31
from dvc_data .transfer import transfer as _transfer
33
32
34
33
file_type = typer .Argument (
59
58
"LinkEnum" , {lt : lt for lt in ["reflink" , "hardlink" , "symlink" , "copy" ]}
60
59
)
61
60
SIZE_HELP = "Human readable size, eg: '1kb', '100Mb', '10GB' etc"
62
- ODB_PATH = typer .Option (
63
- ".dvc/cache" , help = "Path to the root of the odb" , envvar = "ODB_PATH"
64
- )
65
61
66
62
67
63
class Application (typer .Typer ):
@@ -138,21 +134,27 @@ def from_shortoid(odb: HashFileDB, oid: str) -> str:
138
134
raise typer .Exit (1 ) from exc
139
135
140
136
141
- def get_odb (path , ** config ):
142
- state = State (root_dir = os .getcwd (), tmp_dir = os .path .join (path , "tmp" ))
143
- return HashFileDB (LocalFileSystem (), path , state = state , ** config )
137
+ def get_odb (** config ):
138
+ try :
139
+ repo = Repo .discover ()
140
+ except NotARepo as exc :
141
+ typer .echo (exc , err = True )
142
+ raise typer .Abort (1 )
143
+
144
+ state = State (root_dir = repo .root , tmp_dir = repo .tmp_dir )
145
+ return HashFileDB (repo .fs , repo .object_dir , state = state , ** config )
144
146
145
147
146
148
@app .command (help = "Oid to path" )
147
- def o2p (oid : str = typer .Argument (..., allow_dash = True ), db : str = ODB_PATH ):
148
- odb = get_odb (db )
149
+ def o2p (oid : str = typer .Argument (..., allow_dash = True )):
150
+ odb = get_odb ()
149
151
path = odb .oid_to_path (from_shortoid (odb , oid ))
150
152
typer .echo (path )
151
153
152
154
153
155
@app .command (help = "Path to Oid" )
154
- def p2o (path : Path = typer .Argument (..., allow_dash = True ), db : str = ODB_PATH ):
155
- odb = get_odb (db )
156
+ def p2o (path : Path = typer .Argument (..., allow_dash = True )):
157
+ odb = get_odb ()
156
158
fs_path = relpath (path )
157
159
if fs_path == "-" :
158
160
fs_path = sys .stdin .read ().strip ()
@@ -164,10 +166,9 @@ def p2o(path: Path = typer.Argument(..., allow_dash=True), db: str = ODB_PATH):
164
166
@app .command (help = "Provide content of the objects" )
165
167
def cat (
166
168
oid : str = typer .Argument (..., allow_dash = True ),
167
- db : str = ODB_PATH ,
168
169
check : bool = typer .Option (False , "--check" , "-c" ),
169
170
):
170
- odb = get_odb (db )
171
+ odb = get_odb ()
171
172
oid = from_shortoid (odb , oid )
172
173
if check :
173
174
try :
@@ -184,11 +185,10 @@ def cat(
184
185
@app .command (help = "Build and optionally write object to the database" )
185
186
def build (
186
187
path : Path = dir_file_type ,
187
- db : str = ODB_PATH ,
188
188
write : bool = typer .Option (False , "--write" , "-w" ),
189
189
shallow : bool = False ,
190
190
):
191
- odb = get_odb (db )
191
+ odb = get_odb ()
192
192
fs_path = relpath (path )
193
193
194
194
fs = odb .fs
@@ -210,8 +210,8 @@ def build(
210
210
211
211
@app .command ("ls" , help = "List objects in a tree" )
212
212
@app .command ("ls-tree" , help = "List objects in a tree" )
213
- def ls (oid : str = typer .Argument (..., allow_dash = True ), db : str = ODB_PATH ):
214
- odb = get_odb (db )
213
+ def ls (oid : str = typer .Argument (..., allow_dash = True )):
214
+ odb = get_odb ()
215
215
oid = from_shortoid (odb , oid )
216
216
try :
217
217
tree = Tree .load (odb , HashInfo ("md5" , oid ))
@@ -224,8 +224,8 @@ def ls(oid: str = typer.Argument(..., allow_dash=True), db: str = ODB_PATH):
224
224
225
225
226
226
@app .command (help = "Verify objects in the database" )
227
- def fsck (db : str = ODB_PATH ):
228
- odb = get_odb (db )
227
+ def fsck ():
228
+ odb = get_odb ()
229
229
ret = 0
230
230
for oid in odb .all ():
231
231
try :
@@ -237,10 +237,8 @@ def fsck(db: str = ODB_PATH):
237
237
238
238
239
239
@app .command (help = "Diff two objects in the database" )
240
- def diff (
241
- short_oid1 , short_oid2 : str , db : str = ODB_PATH , unchanged : bool = False
242
- ):
243
- odb = get_odb (db )
240
+ def diff (short_oid1 , short_oid2 : str , unchanged : bool = False ):
241
+ odb = get_odb ()
244
242
obj1 = odb .get (from_shortoid (odb , short_oid1 ))
245
243
obj2 = odb .get (from_shortoid (odb , short_oid2 ))
246
244
d = _diff (load (odb , obj1 .hash_info ), load (odb , obj2 .hash_info ), odb )
@@ -271,8 +269,8 @@ def _prepare_info(entry):
271
269
272
270
273
271
@app .command (help = "Merge two trees and optionally write to the database." )
274
- def merge_tree (oid1 : str , oid2 : str , db : str = ODB_PATH , force : bool = False ):
275
- odb = get_odb (db )
272
+ def merge_tree (oid1 : str , oid2 : str , force : bool = False ):
273
+ odb = get_odb ()
276
274
oid1 = from_shortoid (odb , oid1 )
277
275
oid2 = from_shortoid (odb , oid2 )
278
276
obj1 = load (odb , odb .get (oid1 ).hash_info )
@@ -300,7 +298,7 @@ def merge_tree(oid1: str, oid2: str, db: str = ODB_PATH, force: bool = False):
300
298
301
299
302
300
@app .command ()
303
- def update_tree (oid : str , patch_file : Path = file_type , db : str = ODB_PATH ):
301
+ def update_tree (oid : str , patch_file : Path = file_type ):
304
302
"""Update tree contents virtually with a patch file in json format.
305
303
306
304
Example patch file for reference:
@@ -316,7 +314,7 @@ def update_tree(oid: str, patch_file: Path = file_type, db: str = ODB_PATH):
316
314
317
315
Example: ./cli.py update-tree f23d4 patch.json
318
316
"""
319
- odb = get_odb (db )
317
+ odb = get_odb ()
320
318
oid = from_shortoid (odb , oid )
321
319
obj = load (odb , odb .get (oid ).hash_info )
322
320
assert isinstance (obj , Tree )
@@ -374,9 +372,8 @@ def checkout(
374
372
type : List [LinkEnum ] = typer .Option ( # pylint: disable=redefined-builtin
375
373
["copy" ]
376
374
),
377
- db : str = ODB_PATH ,
378
375
):
379
- odb = get_odb (db , type = [t .value for t in type ])
376
+ odb = get_odb (type = [t .value for t in type ])
380
377
oid = from_shortoid (odb , oid )
381
378
obj = load (odb , odb .get (oid ).hash_info )
382
379
with Tqdm (total = len (obj ), desc = "Checking out" , unit = "obj" ) as pbar :
0 commit comments