remove known collisions

This commit is contained in:
Markus Hofbauer 2024-02-19 15:19:49 +01:00
parent 7875e78af7
commit c9282c5613
4 changed files with 59 additions and 432 deletions

View file

@ -47,8 +47,6 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Check for alias collisions
run: python tools/alias_collision/check_alias_collision.py plugins
- name: Install dependencies
run: |
python -m pip install --upgrade pip
@ -57,3 +55,11 @@ jobs:
run: |
cd tools/alias_collision/
python -m pytest
- name: Check for alias collisions on target branch
run: |
git checkout ${GITHUB_BASE_REF}
python tools/alias_collision/check_alias_collision.py plugins --known-collisions-output-path known_alias_collisions.json
- name: Compare known collisions to new collisions on source branch
run: |
git checkout ${GITHUB_HEAD_REF}
python tools/alias_collision/check_alias_collision.py plugins --known-collisions known_alias_collisions.json

View file

@ -158,3 +158,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# default known collisions file
known_collisions.json

View file

@ -2,7 +2,7 @@
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
from pathlib import Path
from dataclasses import dataclass, asdict
from dataclasses import dataclass
from typing import List, Dict
import itertools
import re
@ -13,8 +13,7 @@ ERROR_MESSAGE_TEMPLATE = (
"Alias `%s` defined in `%s` already exists as alias `%s` in `%s`."
)
# TODO: We want that list to be empty and get rid of this file
KNOWN_COLLISIONS = Path(__file__).resolve().parent / "known_collisions.json"
KNOWN_COLLISIONS_PATH = Path(__file__).resolve().parent / "known_collisions.json"
def dir_path(path_string: str) -> Path:
@ -34,26 +33,38 @@ def parse_arguments():
type=dir_path,
help="Folder to check",
)
parser.add_argument(
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--known-collisions",
type=Path,
default=KNOWN_COLLISIONS,
help="Json-serialized list of known collisions",
default=None,
help="Json-serialized list of known collision to compare to",
)
group.add_argument(
"--known-collisions-output-path",
type=Path,
default=KNOWN_COLLISIONS_PATH,
help="Output path for a json-serialized list of known collisions",
)
return parser.parse_args()
@dataclass(frozen=True)
class Alias:
alias: str
value: str
module: Path
def to_dict(self) -> Dict:
return {
"alias": self.alias,
"value": self.value,
"module": str(self.module),
}
@dataclass(frozen=True)
class Collision:
existing_alias: Alias
new_alias: Alias
@ -67,6 +78,12 @@ class Collision:
Alias(**collision_dict["new_alias"]),
)
def to_dict(self) -> Dict:
return {
"existing_alias": self.existing_alias.to_dict(),
"new_alias": self.new_alias.to_dict(),
}
def find_aliases_in_file(file: Path) -> List[Alias]:
matches = re.findall(r"^alias (.*)='(.*)'", file.read_text(), re.M)
@ -113,25 +130,36 @@ def print_collisions(collisions: Dict[Alias, Alias]) -> None:
print("Found no collisions")
def check_for_new_collisions(
known_collisions: Path, collisions: List[Collision]
) -> List[Collision]:
known_collisions = load_known_collisions(known_collisions)
known_collision_aliases = [
collision.new_alias.alias for collision in known_collisions
]
return [
collision
for collision in collisions
if collision.is_new_collision(known_collision_aliases)
]
def main() -> int:
"""main"""
args = parse_arguments()
aliases = find_all_aliases(args.folder)
collisions = check_for_duplicates(aliases)
known_collisions = load_known_collisions(args.known_collisions)
known_collision_aliases = [
collision.new_alias.alias for collision in known_collisions
]
if args.known_collisions is not None:
new_collisions = check_for_new_collisions(args.known_collisions, collisions)
print_collisions(new_collisions)
return -1 if new_collisions else 0
new_collisions = [
collision
for collision in collisions
if collision.is_new_collision(known_collision_aliases)
]
print_collisions(new_collisions)
return -1 if new_collisions else 0
args.known_collisions_output_path.write_text(
json.dumps([collision.to_dict() for collision in collisions])
)
return 0
if __name__ == "__main__":

View file

@ -1,410 +0,0 @@
[
{
"existing_alias": {
"alias": "pbl",
"value": "podman build",
"module": "plugins/podman/podman.plugin.zsh"
},
"new_alias": {
"alias": "pbl",
"value": "perlbrew list",
"module": "plugins/perl/perl.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "gcd",
"value": "git checkout $(git_develop_branch)",
"module": "plugins/git/git.plugin.zsh"
},
"new_alias": {
"alias": "gcd",
"value": "git checkout $(git config gitflow.branch.develop)",
"module": "plugins/git-flow/git-flow.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "h",
"value": "history",
"module": "plugins/history/history.plugin.zsh"
},
"new_alias": {
"alias": "h",
"value": "history",
"module": "plugins/common-aliases/common-aliases.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "brs",
"value": "bin/bridgetown start",
"module": "plugins/bridgetown/bridgetown.plugin.zsh"
},
"new_alias": {
"alias": "brs",
"value": "web_search brave",
"module": "plugins/web-search/web-search.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "github",
"value": "frontend github",
"module": "plugins/frontend-search/frontend-search.plugin.zsh"
},
"new_alias": {
"alias": "github",
"value": "web_search github",
"module": "plugins/web-search/web-search.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "stackoverflow",
"value": "frontend stackoverflow",
"module": "plugins/frontend-search/frontend-search.plugin.zsh"
},
"new_alias": {
"alias": "stackoverflow",
"value": "web_search stackoverflow",
"module": "plugins/web-search/web-search.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "zcl",
"value": "sudo zypper clean",
"module": "plugins/suse/suse.plugin.zsh"
},
"new_alias": {
"alias": "zcl",
"value": "sudo zypper cl",
"module": "plugins/suse/suse.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "afs",
"value": "apt-file search --regexp",
"module": "plugins/ubuntu/ubuntu.plugin.zsh"
},
"new_alias": {
"alias": "afs",
"value": "apt-file search --regexp",
"module": "plugins/debian/debian.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "allpkgs",
"value": "dpkg --get-selections | grep -v deinstall",
"module": "plugins/ubuntu/ubuntu.plugin.zsh"
},
"new_alias": {
"alias": "allpkgs",
"value": "aptitude search -F \"%p\" --disable-columns ~i",
"module": "plugins/debian/debian.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "mydeb",
"value": "time dpkg-buildpackage -rfakeroot -us -uc",
"module": "plugins/ubuntu/ubuntu.plugin.zsh"
},
"new_alias": {
"alias": "mydeb",
"value": "time dpkg-buildpackage -rfakeroot -us -uc",
"module": "plugins/debian/debian.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "jh",
"value": "juju help",
"module": "plugins/juju/juju.plugin.zsh"
},
"new_alias": {
"alias": "jh",
"value": "jhbuild",
"module": "plugins/jhbuild/jhbuild.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "n",
"value": "nanoc",
"module": "plugins/nanoc/nanoc.plugin.zsh"
},
"new_alias": {
"alias": "n",
"value": "\"$GREP\" -Rvi",
"module": "plugins/singlechar/singlechar.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "a",
"value": "ansible ",
"module": "plugins/ansible/ansible.plugin.zsh"
},
"new_alias": {
"alias": "a",
"value": "echo >>",
"module": "plugins/singlechar/singlechar.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "p",
"value": "ps -f",
"module": "plugins/common-aliases/common-aliases.plugin.zsh"
},
"new_alias": {
"alias": "p",
"value": "\"$PAGER\"",
"module": "plugins/singlechar/singlechar.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "sf",
"value": "`_symfony_console`",
"module": "plugins/symfony2/symfony2.plugin.zsh"
},
"new_alias": {
"alias": "sf",
"value": "\"$ROOT\" \"$GREP\" -Rli",
"module": "plugins/singlechar/singlechar.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "sp",
"value": "web_search startpage",
"module": "plugins/web-search/web-search.plugin.zsh"
},
"new_alias": {
"alias": "sp",
"value": "\"$ROOT\" \"$PAGER\"",
"module": "plugins/singlechar/singlechar.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "hs",
"value": "history | grep",
"module": "plugins/history/history.plugin.zsh"
},
"new_alias": {
"alias": "hs",
"value": "hanami server",
"module": "plugins/hanami/hanami.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "db",
"value": "deno bundle",
"module": "plugins/deno/deno.plugin.zsh"
},
"new_alias": {
"alias": "db",
"value": "dotnet build",
"module": "plugins/dotnet/dotnet.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "rn",
"value": "react-native",
"module": "plugins/react-native/react-native.plugin.zsh"
},
"new_alias": {
"alias": "rn",
"value": "rails notes",
"module": "plugins/rails/rails.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "rs",
"value": "repo sync",
"module": "plugins/repo/repo.plugin.zsh"
},
"new_alias": {
"alias": "rs",
"value": "rails server",
"module": "plugins/rails/rails.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "ru",
"value": "repo upload",
"module": "plugins/repo/repo.plugin.zsh"
},
"new_alias": {
"alias": "ru",
"value": "rails runner",
"module": "plugins/rails/rails.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "sc",
"value": "\"$ROOT\" cat",
"module": "plugins/singlechar/singlechar.plugin.zsh"
},
"new_alias": {
"alias": "sc",
"value": "ruby script/console",
"module": "plugins/rails/rails.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "sd",
"value": "\"$ROOT\" \"$WGET\"",
"module": "plugins/singlechar/singlechar.plugin.zsh"
},
"new_alias": {
"alias": "sd",
"value": "ruby script/destroy",
"module": "plugins/rails/rails.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "sd",
"value": "\"$ROOT\" \"$WGET\"",
"module": "plugins/singlechar/singlechar.plugin.zsh"
},
"new_alias": {
"alias": "sd",
"value": "ruby script/server --debugger",
"module": "plugins/rails/rails.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "sp",
"value": "web_search startpage",
"module": "plugins/web-search/web-search.plugin.zsh"
},
"new_alias": {
"alias": "sp",
"value": "ruby script/plugin",
"module": "plugins/rails/rails.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "c",
"value": "cat",
"module": "plugins/singlechar/singlechar.plugin.zsh"
},
"new_alias": {
"alias": "c",
"value": "composer",
"module": "plugins/composer/composer.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "dr",
"value": "dotnet run",
"module": "plugins/dotnet/dotnet.plugin.zsh"
},
"new_alias": {
"alias": "dr",
"value": "docker container run",
"module": "plugins/docker/docker.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "rake",
"value": "_rake_command",
"module": "plugins/rails/rails.plugin.zsh"
},
"new_alias": {
"alias": "rake",
"value": "noglob rake",
"module": "plugins/rake/rake.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "rubies",
"value": "rvm list rubies",
"module": "plugins/rvm/rvm.plugin.zsh"
},
"new_alias": {
"alias": "rubies",
"value": "chruby",
"module": "plugins/chruby/chruby.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "h",
"value": "history",
"module": "plugins/history/history.plugin.zsh"
},
"new_alias": {
"alias": "h",
"value": "helm",
"module": "plugins/helm/helm.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "ma",
"value": "mongocli atlas",
"module": "plugins/mongocli/mongocli.plugin.zsh"
},
"new_alias": {
"alias": "ma",
"value": "meteor add",
"module": "plugins/meteor/meteor.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "map",
"value": "web_search duckduckgo \\!m",
"module": "plugins/web-search/web-search.plugin.zsh"
},
"new_alias": {
"alias": "map",
"value": "meteor add-platform",
"module": "plugins/meteor/meteor.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "mis",
"value": "microk8s.istio",
"module": "plugins/microk8s/microk8s.plugin.zsh"
},
"new_alias": {
"alias": "mis",
"value": "meteor install-sdk",
"module": "plugins/meteor/meteor.plugin.zsh"
}
},
{
"existing_alias": {
"alias": "m",
"value": "man",
"module": "plugins/singlechar/singlechar.plugin.zsh"
},
"new_alias": {
"alias": "m",
"value": "meteor run",
"module": "plugins/meteor/meteor.plugin.zsh"
}
}
]