Auto-update the pre-commit config to the latest versions of repos.
(
config_file: str,
tags_only: bool,
freeze: bool,
repos: Sequence[str] = (),
jobs: int = 1,
)
| 160 | |
| 161 | |
| 162 | def autoupdate( |
| 163 | config_file: str, |
| 164 | tags_only: bool, |
| 165 | freeze: bool, |
| 166 | repos: Sequence[str] = (), |
| 167 | jobs: int = 1, |
| 168 | ) -> int: |
| 169 | """Auto-update the pre-commit config to the latest versions of repos.""" |
| 170 | migrate_config(config_file, quiet=True) |
| 171 | changed = False |
| 172 | retv = 0 |
| 173 | |
| 174 | config_repos = [ |
| 175 | repo for repo in load_config(config_file)['repos'] |
| 176 | if repo['repo'] not in {LOCAL, META} |
| 177 | ] |
| 178 | |
| 179 | rev_infos: list[RevInfo | None] = [None] * len(config_repos) |
| 180 | jobs = jobs or xargs.cpu_count() # 0 => number of cpus |
| 181 | jobs = min(jobs, len(repos) or len(config_repos)) # max 1-per-thread |
| 182 | jobs = max(jobs, 1) # at least one thread |
| 183 | with concurrent.futures.ThreadPoolExecutor(jobs) as exe: |
| 184 | futures = [ |
| 185 | exe.submit( |
| 186 | _update_one, |
| 187 | i, repo, tags_only=tags_only, freeze=freeze, |
| 188 | ) |
| 189 | for i, repo in enumerate(config_repos) |
| 190 | if not repos or repo['repo'] in repos |
| 191 | ] |
| 192 | for future in concurrent.futures.as_completed(futures): |
| 193 | try: |
| 194 | i, old, new = future.result() |
| 195 | except RepositoryCannotBeUpdatedError as e: |
| 196 | output.write_line(str(e)) |
| 197 | retv = 1 |
| 198 | else: |
| 199 | if new.rev != old.rev: |
| 200 | changed = True |
| 201 | if new.frozen: |
| 202 | new_s = f'{new.frozen} (frozen)' |
| 203 | else: |
| 204 | new_s = new.rev |
| 205 | msg = f'updating {old.rev} -> {new_s}' |
| 206 | rev_infos[i] = new |
| 207 | else: |
| 208 | msg = 'already up to date!' |
| 209 | |
| 210 | output.write_line(f'[{old.repo}] {msg}') |
| 211 | |
| 212 | if changed: |
| 213 | _write_new_config(config_file, rev_infos) |
| 214 | |
| 215 | return retv |