Skip to content

Instantly share code, notes, and snippets.

@asottile-sentry
Last active April 29, 2022 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asottile-sentry/dd54c4478c6011ec74188f7772566ddc to your computer and use it in GitHub Desktop.
Save asottile-sentry/dd54c4478c6011ec74188f7772566ddc to your computer and use it in GitHub Desktop.
all-repos autofixer for git.io
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())
@asottile-sentry
Copy link
Author

asottile-sentry commented Apr 26, 2022

an all-repos autofixer for git.io urls

usage is something like:

python -m git_io_fix --repos $(all-repos-grep --repos -E 'https://git\.io/.+?\b')

an example pull request: getsentry/develop#565

@KangOl
Copy link

KangOl commented Apr 29, 2022

Note that a few years ago, git.io was accessible in http://, I have a few http://git.io/ links in my codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment