|
3 | 3 | import os
|
4 | 4 | from pathlib import Path
|
5 | 5 | import subprocess
|
| 6 | +from typing import Protocol, TypeVar |
6 | 7 | import click
|
7 | 8 | from .config import RECORD_FILE
|
8 | 9 | from .readmes import mkreadmes
|
9 | 10 | from .record import RepoRecord
|
10 | 11 |
|
11 | 12 |
|
| 13 | +class Ided(Protocol): |
| 14 | + id: int |
| 15 | + |
| 16 | + |
| 17 | +D = TypeVar("D", bound="Ided") |
| 18 | + |
| 19 | + |
12 | 20 | @click.command()
|
13 | 21 | @click.option(
|
14 | 22 | "-R",
|
@@ -59,30 +67,23 @@ def main(
|
59 | 67 | to_commit = "HEAD" if to_point is None else dateish2commit(repo, to_point)
|
60 | 68 | from_record = RepoRecord.model_validate_json(read_record(from_commit, repo))
|
61 | 69 | to_record = RepoRecord.model_validate_json(read_record(to_commit, repo))
|
62 |
| - old_github_repos = {r.name for r in from_record.github} |
63 |
| - old_osf_repos = {r.id for r in from_record.osf} |
64 |
| - old_gin_repos = {r.id for r in from_record.gin} |
65 |
| - old_hub_repos = {r.id for r in from_record.hub_datalad_org} |
66 |
| - old_atris_repos = {r.id for r in from_record.atris} |
67 |
| - new_record = RepoRecord() |
68 |
| - for ghr in to_record.github: |
69 |
| - if ghr.name not in old_github_repos: |
70 |
| - new_record.github.append(ghr) |
71 |
| - for osfr in to_record.osf: |
72 |
| - if osfr.id not in old_osf_repos: |
73 |
| - new_record.osf.append(osfr) |
74 |
| - for ginr in to_record.gin: |
75 |
| - if ginr.id not in old_gin_repos: |
76 |
| - new_record.gin.append(ginr) |
77 |
| - for hubr in to_record.hub_datalad_org: |
78 |
| - if hubr.id not in old_hub_repos: |
79 |
| - new_record.hub_datalad_org.append(hubr) |
80 |
| - for atrisr in to_record.atris: |
81 |
| - if atrisr.id not in old_atris_repos: |
82 |
| - new_record.atris.append(atrisr) |
| 70 | + new_record = RepoRecord( |
| 71 | + github=diff_by_id(from_record.github, to_record.github), |
| 72 | + osf=diff_by_id(from_record.osf, to_record.osf), |
| 73 | + gin=diff_by_id(from_record.gin, to_record.gin), |
| 74 | + hub_datalad_org=diff_by_id( |
| 75 | + from_record.hub_datalad_org, to_record.hub_datalad_org |
| 76 | + ), |
| 77 | + atris=diff_by_id(from_record.atris, to_record.atris), |
| 78 | + ) |
83 | 79 | mkreadmes(new_record, filename=readme_file, directory=readme_dir)
|
84 | 80 |
|
85 | 81 |
|
| 82 | +def diff_by_id(old: list[D], new: list[D]) -> list[D]: |
| 83 | + old_ids = {i.id for i in old} |
| 84 | + return [j for j in new if j.id not in old_ids] |
| 85 | + |
| 86 | + |
86 | 87 | def dateish2commit(repo: Path, dateish: str) -> str:
|
87 | 88 | try:
|
88 | 89 | dt = date.fromisoformat(dateish)
|
|
0 commit comments