Last active
April 29, 2022 15:06
-
-
Save asottile-sentry/dd54c4478c6011ec74188f7772566ddc to your computer and use it in GitHub Desktop.
all-repos autofixer for git.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import argparse | |
import functools | |
import re | |
import urllib.request | |
from typing import Match | |
from typing import Sequence | |
from all_repos import autofix_lib | |
from all_repos.config import Config | |
GIT_IO_RE = re.compile(rb'https://git\.io/.+?\b') | |
def find_repos(config: Config) -> set[str]: | |
raise NotImplementedError('need to specify --repos') | |
@functools.lru_cache | |
def _git_io_replacement(url: bytes) -> bytes: | |
req = urllib.request.Request(url.decode(), method='HEAD') | |
resp = urllib.request.urlopen(req) | |
return resp.url.encode() | |
def _replace_cb(match: Match[bytes]) -> str: | |
return _git_io_replacement(match[0]) | |
def apply_fix() -> None: | |
ret = autofix_lib.run( | |
'git', 'grep', '-El', GIT_IO_RE.pattern.decode(), | |
capture_output=True, | |
text=True, | |
) | |
for line in ret.stdout.splitlines(): | |
with open(line, 'rb') as f: | |
contents = f.read() | |
new_contents = GIT_IO_RE.sub(_replace_cb, contents) | |
if new_contents != contents: | |
with open(line, 'wb') as f: | |
f.write(new_contents) | |
def main(argv: Sequence[str] | None = None) -> int: | |
parser = argparse.ArgumentParser() | |
autofix_lib.add_fixer_args(parser) | |
args = parser.parse_args(argv) | |
msg = '''\ | |
replace git.io links with redirect targets | |
see: https://github.blog/changelog/2022-04-25-git-io-deprecation/ | |
''' | |
repos, config, commit, autofix_settings = autofix_lib.from_cli( | |
args, | |
find_repos=find_repos, | |
msg=msg, | |
branch_name='git-io-deprecated', | |
) | |
autofix_lib.fix( | |
repos, | |
apply_fix=apply_fix, | |
config=config, | |
commit=commit, | |
autofix_settings=autofix_settings, | |
) | |
return 0 | |
if __name__ == '__main__': | |
raise SystemExit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that a few years ago, git.io was accessible in http://, I have a few http://git.io/ links in my codebase.