git-get 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.1
2
+ Name: git-get
3
+ Version: 0.1.3
4
+ Summary: Automatically clone Git repos.
5
+ Author-email: Daniel Rocha <daniel@rocha.io>
6
+ Requires-Python: >=3.10
7
+ Requires-Dist: typer >= 0.6.1
8
+ Requires-Dist: rich >= 12.6.0
9
+ Project-URL: homepage, https://github.com/danroc/git-get
@@ -0,0 +1,5 @@
1
+ git_get.py,sha256=kZk8n77WCA5it9UJMuZLOdw3Goe1q8imgU-BiMXe6SM,2284
2
+ git_get-0.1.3.dist-info/entry_points.txt,sha256=_WPI8vSIOd7cPrkzukgO5O_SJYcYWtPHJD5e-DBiQJw,38
3
+ git_get-0.1.3.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
4
+ git_get-0.1.3.dist-info/METADATA,sha256=TNfp-qaDiKXs6B00cNkN9tlVuEaY6nzPUwAvWGh0ONk,277
5
+ git_get-0.1.3.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: flit 3.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ git-get=gitget:app
3
+
git_get.py ADDED
@@ -0,0 +1,101 @@
1
+ """Automatically clone Git repos."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import enum
6
+ import os
7
+ import re
8
+ import subprocess # noqa: S404
9
+ import sys
10
+ from dataclasses import dataclass
11
+
12
+ import typer
13
+
14
+ __version__ = "0.1.3"
15
+
16
+ app = typer.Typer()
17
+
18
+
19
+ class Options(str, enum.Enum):
20
+ REPOS_DIR = "GIT_GET_REPOS_DIR"
21
+ SSH_USERS = "GIT_GET_SSH_USERS"
22
+ DEFAULT_PREFIX = "GIT_GET_DEFAULT_PREFIX"
23
+
24
+
25
+ # Default configuration, which can be overridden by environment variables.
26
+ config = {
27
+ Options.REPOS_DIR: "~/repos",
28
+ Options.SSH_USERS: "danroc,metamask",
29
+ Options.DEFAULT_PREFIX: "https://github.com/",
30
+ }
31
+
32
+ config = {k: os.getenv(k, config[k]) for k in config}
33
+
34
+
35
+ class InvalidURL(Exception):
36
+ pass
37
+
38
+
39
+ class Schema(enum.Enum):
40
+ SSH = enum.auto()
41
+ HTTP = enum.auto()
42
+
43
+
44
+ FORMATS = {
45
+ Schema.SSH: re.compile(
46
+ r"^(.*?)@(?P<host>.*?):(?P<user>.*?)/(?P<repo>.*?)\.git$",
47
+ ),
48
+ Schema.HTTP: re.compile(
49
+ r"^https?://(?P<host>.*?)(?P<port>:\d+)?/(?P<user>.*?)/(?P<repo>.*?)\.git$",
50
+ ),
51
+ }
52
+
53
+
54
+ @dataclass(frozen=True)
55
+ class Repo:
56
+ host: str
57
+ user: str
58
+ name: str
59
+
60
+
61
+ def parse(url: str) -> Repo:
62
+ for format in FORMATS.values():
63
+ m = format.match(url)
64
+ if m:
65
+ return Repo(
66
+ host=m.group("host"),
67
+ user=m.group("user"),
68
+ name=m.group("repo"),
69
+ )
70
+ raise InvalidURL
71
+
72
+
73
+ @app.command()
74
+ def main(repo_url: str) -> None:
75
+ clone_url = repo_url.strip()
76
+ if not clone_url.endswith(".git"):
77
+ clone_url += ".git"
78
+
79
+ try:
80
+ # Try to parse the URL
81
+ repo = parse(clone_url)
82
+ except InvalidURL:
83
+ # Retry with the prefix
84
+ clone_url = config[Options.DEFAULT_PREFIX] + clone_url
85
+ repo = parse(clone_url)
86
+
87
+ # Check if should force SSH
88
+ use_ssh_on = config[Options.SSH_USERS].lower().split(",")
89
+ if repo.user.lower() in use_ssh_on:
90
+ clone_url = f"git@{repo.host}:{repo.user}/{repo.name}.git"
91
+
92
+ path = os.path.join(config[Options.REPOS_DIR], repo.host, repo.user, repo.name)
93
+ path = os.path.expanduser(path)
94
+
95
+ print(f"Cloning repo '{clone_url}'...")
96
+ out = subprocess.run(["git", "clone", clone_url, path]) # noqa: S603,S607
97
+ sys.exit(out.returncode)
98
+
99
+
100
+ if __name__ == "__main__":
101
+ app()