Skip to content

Commit c12b307

Browse files
committed
Deduplicate some code
1 parent 6a76139 commit c12b307

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/find_datalad_repos/diff.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
import os
44
from pathlib import Path
55
import subprocess
6+
from typing import Protocol, TypeVar
67
import click
78
from .config import RECORD_FILE
89
from .readmes import mkreadmes
910
from .record import RepoRecord
1011

1112

13+
class Ided(Protocol):
14+
id: int
15+
16+
17+
D = TypeVar("D", bound="Ided")
18+
19+
1220
@click.command()
1321
@click.option(
1422
"-R",
@@ -59,30 +67,23 @@ def main(
5967
to_commit = "HEAD" if to_point is None else dateish2commit(repo, to_point)
6068
from_record = RepoRecord.model_validate_json(read_record(from_commit, repo))
6169
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+
)
8379
mkreadmes(new_record, filename=readme_file, directory=readme_dir)
8480

8581

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+
8687
def dateish2commit(repo: Path, dateish: str) -> str:
8788
try:
8889
dt = date.fromisoformat(dateish)

0 commit comments

Comments
 (0)