Skip to content

Commit 8716ca7

Browse files
ryan-williamsclaude
andcommitted
Add dvx import-url --git — git-tracked imports with URL provenance
Downloads files via `urllib`, computes MD5, writes `.dvc` with `meta.git_tracked: true` and URL dep metadata (ETag, Last-Modified, size, fetch date). Bypasses DVC cache and `.gitignore`. - `dvx update` detects and handles git-tracked imports separately - `dvx push`/`pull` skip git-tracked files in `get_transfer_status()` - `DVCFileInfo.git_tracked` field for downstream consumers (audit, etc.) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 940cdbd commit 8716ca7

File tree

5 files changed

+407
-12
lines changed

5 files changed

+407
-12
lines changed

specs/done/git-tracked-imports.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Git-tracked imports with URL provenance
2+
3+
## Context
4+
5+
Some projects import small files (< 1MB) from HTTP URLs that should be:
6+
1. **Committed to Git** (small enough, useful for diffing/auditing)
7+
2. **Tracked by DVX** for provenance: source URL, download date, hash
8+
9+
Current `dvx import-url` pushes files to the DVC remote (S3) and `.gitignore`s them. For small files this is overkill — Git is the better storage backend, and having the file directly in the repo makes diffs, CI, and collaboration simpler.
10+
11+
### Motivating use case
12+
13+
The [NJ crashes project][crashes] imports annual UCR (Uniform Crime Report) Excel files from NJSP:
14+
15+
```
16+
crime/2018_Uniform_Crime_Report.xlsx (~150KB)
17+
crime/2019_Uniform_Crime_Report.xlsx (~130KB)
18+
...
19+
crime/2024-AGENCY-GROUPS-OFFENSE-SUMMARY-REPORT.xlsx (~200KB)
20+
```
21+
22+
These are small, rarely-changing files downloaded from government URLs. They should be Git-tracked for easy diffing (e.g. verifying quarterly vs annual 2023 data), but we want DVX to record:
23+
- **Source URL** (changes across years/site migrations, e.g. `nj.gov/njsp/ucr/``njsp.njoag.gov/crime-reports/`)
24+
- **Download date** (when we fetched it)
25+
- **Hash** (detect if upstream republishes/corrects data)
26+
27+
[crashes]: https://github.com/hudcostreets/crashes
28+
29+
## Implemented behavior
30+
31+
### `dvx import-url --git` / `-G`
32+
33+
A flag that bypasses DVC's cache/gitignore machinery:
34+
1. Downloads the file to the specified path (via `urllib`)
35+
2. Computes MD5, captures HTTP headers (ETag, Last-Modified, Content-Length)
36+
3. Creates a `.dvc` file with URL provenance and `meta.git_tracked: true`
37+
4. **Does NOT** add the file to `.gitignore`
38+
5. **Does NOT** cache the file in DVC
39+
40+
```bash
41+
dvx import-url --git \
42+
https://njsp.njoag.gov/wp/wp-content/uploads/2023%20Uniform%20Crime%20Report.xlsx \
43+
-o crime/2023_Uniform_Crime_Report.xlsx
44+
```
45+
46+
Resulting `.dvc` file:
47+
48+
```yaml
49+
deps:
50+
- path: https://njsp.njoag.gov/wp/wp-content/uploads/2023%20Uniform%20Crime%20Report.xlsx
51+
checksum: '"etag-value"'
52+
size: 153600
53+
mtime: '2025-01-30T00:00:00+00:00' # Last-Modified as ISO 8601
54+
outs:
55+
- md5: a1b2c3d4...
56+
size: 153600
57+
hash: md5
58+
path: 2023_Uniform_Crime_Report.xlsx
59+
meta:
60+
git_tracked: true
61+
import:
62+
fetched: '2026-03-09'
63+
```
64+
65+
`--no-download` is supported: creates `.dvc` from HEAD request metadata only (no md5 in outs).
66+
67+
### `dvx update` behavior
68+
69+
`dvx update crime/2023_Uniform_Crime_Report.xlsx.dvc`:
70+
- Detects `meta.git_tracked: true` and uses `update_git_import()` instead of DVC
71+
- HEAD request to check ETag/Last-Modified
72+
- If changed: re-download, update `.dvc` hash
73+
- If unchanged: no-op
74+
- `--no-download` supported: updates metadata only
75+
76+
### `dvx push` / `dvx pull` behavior
77+
78+
Files with `meta.git_tracked: true` are **skipped** by `get_transfer_status()` — they're in Git, not DVC cache.
79+
80+
## Implementation
81+
82+
### New files
83+
84+
- **`src/dvx/git_import.py`**: Core logic
85+
- `git_import_url(url, out, no_download)` — download + create `.dvc`
86+
- `update_git_import(dvc_path, no_download)` — re-check and update
87+
- `is_git_tracked_import(dvc_path)` — check if `.dvc` is git-tracked
88+
- `_ensure_not_gitignored(path)` — remove from `.gitignore` if present
89+
90+
### Modified files
91+
92+
- **`src/dvx/cli/external.py`**: Added `-G`/`--git` flag to `import-url`, git-tracked handling in `update`
93+
- **`src/dvx/cache.py`**: `get_transfer_status()` skips git-tracked imports
94+
- **`src/dvx/run/dvc_files.py`**: Added `git_tracked: bool` field to `DVCFileInfo`, read from `meta.git_tracked`
95+
96+
### Design decisions
97+
98+
- **`meta.git_tracked` not `outs[].git_tracked`**: DVC allows arbitrary data in `meta` but rejects unknown keys in `outs`. Putting the flag in `meta` ensures DVC compatibility.
99+
- **No auto-`git add`**: DVX doesn't generally auto-stage files. The user runs `git add` themselves.
100+
- **`urllib` not DVC's downloader**: Keeps the git-tracked path completely independent of DVC's stage system, avoiding `.gitignore` and cache side effects.
101+
102+
## Alternatives considered
103+
104+
### Just use a manifest file
105+
106+
A simpler approach: a `crime/sources.yaml` or similar that lists URLs and expected hashes. No DVX integration, just a convention.
107+
108+
**Downside**: No automated freshness checking, no integration with `dvx status`/`dvx update` workflows, duplicates hash tracking that DVX already does.
109+
110+
### `dvx add --import-url`
111+
112+
Instead of a new flag on `import-url`, extend `dvx add` (which already handles git-tracked files) with an optional `--import-url` to record provenance. This might be a cleaner API since `dvx add` already means "track this file" and the URL is just metadata.
113+
114+
```bash
115+
dvx add crime/2023_Uniform_Crime_Report.xlsx \
116+
--import-url https://njsp.njoag.gov/wp/wp-content/uploads/2023%20Uniform%20Crime%20Report.xlsx
117+
```
118+
119+
This is arguably better since `import-url` currently implies "DVC-managed remote storage". Could be added later as an alias.
120+
121+
## Future work
122+
123+
- `dvx status` upstream freshness checks (ETag/Last-Modified comparison via `--cloud`)
124+
- Batch import from manifest YAML
125+
- `dvx add --import-url` alias
126+
127+
## Relationship to other specs
128+
129+
- [http-import-last-modified]: Captures `Last-Modified` in `.dvc` deps — prerequisite for meaningful freshness checks
130+
- [blob-audit-lineage]: Audit trail for tracked files — `git_tracked` imports participate in lineage tracking
131+
132+
[http-import-last-modified]: ./http-import-last-modified.md
133+
[blob-audit-lineage]: ../blob-audit-lineage.md

src/dvx/cache.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,12 @@ def get_transfer_status(
900900
file_info = [] # [(dvc_file, data_path, md5, size), ...]
901901
errors = []
902902

903+
from dvx.git_import import is_git_tracked_import
904+
903905
for dvc_file in dvc_files:
904906
try:
907+
if is_git_tracked_import(dvc_file):
908+
continue # Git-tracked imports skip DVC cache
905909
md5, size, _is_dir = _get_output_info(dvc_file)
906910
data_path = dvc_file[:-4] if dvc_file.endswith(".dvc") else dvc_file
907911
file_info.append((dvc_file, data_path, md5, size))

src/dvx/cli/external.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,31 @@ def import_cmd(url, path, out, rev):
2828
@click.command("import-url")
2929
@click.argument("url")
3030
@click.option("-F", "--fs-config", multiple=True, help="Filesystem config (key=value).")
31+
@click.option("-G", "--git", is_flag=True, help="Track in Git (not DVC cache). For small files.")
3132
@click.option("-N", "--no-download", is_flag=True, help="Track metadata only (no download).")
3233
@click.option("-o", "--out", help="Output path.")
3334
@click.option("-V", "--version-aware", is_flag=True, help="Track S3 version IDs.")
34-
def import_url(url, fs_config, no_download, out, version_aware):
35+
def import_url(url, fs_config, git, no_download, out, version_aware):
3536
"""Import a file from a URL.
3637
38+
Use --git to commit the file to Git (instead of DVC cache) with URL
39+
provenance. Good for small files (< 1MB) you want in the repo.
40+
3741
Use --no-download to track metadata (ETag, size) without downloading.
3842
Use --fs-config allow_anonymous_login=true for public buckets.
3943
"""
44+
if git:
45+
from dvx.git_import import git_import_url
46+
47+
try:
48+
dvc_path = git_import_url(url=url, out=out, no_download=no_download)
49+
action = "Tracked" if no_download else "Imported"
50+
click.echo(f"{action} {url} (git-tracked)")
51+
click.echo(f" {dvc_path}")
52+
except Exception as e:
53+
raise click.ClickException(str(e)) from e
54+
return
55+
4056
fs_config_dict = dict(kv.split("=", 1) for kv in fs_config) if fs_config else None
4157
try:
4258
with Repo() as repo:
@@ -67,17 +83,37 @@ def update(no_download, recursive, targets):
6783
6884
Re-checks source ETags and optionally re-downloads if changed.
6985
"""
70-
try:
71-
with Repo() as repo:
72-
repo.update(
73-
targets=list(targets),
74-
no_download=no_download,
75-
recursive=recursive,
76-
)
77-
for target in targets:
78-
click.echo(f"Updated {target}")
79-
except Exception as e:
80-
raise click.ClickException(str(e)) from e
86+
from pathlib import Path
87+
88+
from dvx.git_import import is_git_tracked_import, update_git_import
89+
90+
dvc_targets = []
91+
for target in targets:
92+
dvc_path = Path(target)
93+
if not dvc_path.suffix == ".dvc":
94+
dvc_path = Path(f"{target}.dvc")
95+
if is_git_tracked_import(dvc_path):
96+
try:
97+
changed = update_git_import(dvc_path, no_download=no_download)
98+
status = "updated" if changed else "up to date"
99+
click.echo(f"{target}: {status} (git-tracked)")
100+
except Exception as e:
101+
raise click.ClickException(f"{target}: {e}") from e
102+
else:
103+
dvc_targets.append(target)
104+
105+
if dvc_targets:
106+
try:
107+
with Repo() as repo:
108+
repo.update(
109+
targets=dvc_targets,
110+
no_download=no_download,
111+
recursive=recursive,
112+
)
113+
for target in dvc_targets:
114+
click.echo(f"Updated {target}")
115+
except Exception as e:
116+
raise click.ClickException(str(e)) from e
81117

82118

83119
# =============================================================================

0 commit comments

Comments
 (0)