| 1360 | return meta, new |
| 1361 | |
| 1362 | def add( # noqa: C901 |
| 1363 | self, path: Optional[str] = None, no_commit: bool = False, relink: bool = True |
| 1364 | ) -> Optional["HashFile"]: |
| 1365 | path = path or self.fs_path |
| 1366 | if self.hash_info and not self.is_dir_checksum and self.fs_path != path: |
| 1367 | raise DvcException( |
| 1368 | f"Cannot modify '{self}' which is being tracked as a file" |
| 1369 | ) |
| 1370 | |
| 1371 | assert self.repo |
| 1372 | self.update_legacy_hash_name() |
| 1373 | cache = self.cache if self.use_cache else self.local_cache |
| 1374 | assert isinstance(cache, HashFileDB) |
| 1375 | |
| 1376 | new: HashFile |
| 1377 | try: |
| 1378 | assert self.hash_name |
| 1379 | staging, meta, obj = self._build( |
| 1380 | cache, |
| 1381 | path, |
| 1382 | self.fs, |
| 1383 | self.hash_name, |
| 1384 | ignore=self.dvcignore, |
| 1385 | dry_run=not self.use_cache, |
| 1386 | ) |
| 1387 | except FileNotFoundError as exc: |
| 1388 | if not self.exists: |
| 1389 | raise self.DoesNotExistError(self) from exc |
| 1390 | if not self.is_dir_checksum: |
| 1391 | raise |
| 1392 | |
| 1393 | meta, new = self.unstage(path) |
| 1394 | staging, obj = None, None |
| 1395 | else: |
| 1396 | assert obj |
| 1397 | assert staging |
| 1398 | if self.fs_path != path: |
| 1399 | meta, new = self.apply(path, obj, meta) |
| 1400 | add_update_tree(staging, new) |
| 1401 | else: |
| 1402 | new = obj |
| 1403 | |
| 1404 | self.obj = new |
| 1405 | self.hash_info = self.obj.hash_info |
| 1406 | self.meta = meta |
| 1407 | self.files = None |
| 1408 | self.ignore() |
| 1409 | |
| 1410 | if no_commit or not self.use_cache: |
| 1411 | return obj |
| 1412 | |
| 1413 | if isinstance(new, Tree): |
| 1414 | add_update_tree(cache, new) |
| 1415 | |
| 1416 | if not obj: |
| 1417 | return obj |
| 1418 | |
| 1419 | assert staging |