|
| 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 |
0 commit comments