gith 0.1.0__tar.gz

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.
gith-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Reinaldo J. Menéndez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
gith-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.1
2
+ Name: gith
3
+ Version: 0.1.0
4
+ Summary: A Typer-based helper for Git operations
5
+ Author-email: "Reinaldo J. Menéndez" <rejamen@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Reinaldo J. Menéndez
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Classifier: Programming Language :: Python :: 3
29
+ Classifier: License :: OSI Approved :: MIT License
30
+ Classifier: Operating System :: OS Independent
31
+ Requires-Python: >=3.7
32
+ Description-Content-Type: text/markdown
33
+ License-File: LICENSE
34
+ Requires-Dist: typer
35
+ Requires-Dist: rich
36
+
37
+ # gith
38
+
39
+ A Typer-based CLI helper for Git operations.
40
+
41
+ ## Installation
42
+
43
+ ```bash
44
+ pip install gith
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ### List local branches
50
+
51
+ ```bash
52
+ gith branch --list
53
+ ```
54
+
55
+ ### Create a new branch
56
+
57
+ ```bash
58
+ gith branch --create new_branch_name
59
+ ```
60
+
61
+ ### Create a new branch from a specific branch
62
+
63
+ ```bash
64
+ gith branch --create new_branch_name --from 2
65
+ ```
66
+
67
+ ### Delete branches by their indexes
68
+
69
+ ```bash
70
+ gith branch --delete 1,3,5
71
+ ```
72
+
73
+ ### Keep specific branches and delete the rest
74
+
75
+ ```bash
76
+ gith branch --keep 2,4
77
+ ```
78
+
79
+ ### Checkout to a branch by its index
80
+
81
+ ```bash
82
+ gith checkout 2
83
+ ```
gith-0.1.0/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # gith
2
+
3
+ A Typer-based CLI helper for Git operations.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install gith
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### List local branches
14
+
15
+ ```bash
16
+ gith branch --list
17
+ ```
18
+
19
+ ### Create a new branch
20
+
21
+ ```bash
22
+ gith branch --create new_branch_name
23
+ ```
24
+
25
+ ### Create a new branch from a specific branch
26
+
27
+ ```bash
28
+ gith branch --create new_branch_name --from 2
29
+ ```
30
+
31
+ ### Delete branches by their indexes
32
+
33
+ ```bash
34
+ gith branch --delete 1,3,5
35
+ ```
36
+
37
+ ### Keep specific branches and delete the rest
38
+
39
+ ```bash
40
+ gith branch --keep 2,4
41
+ ```
42
+
43
+ ### Checkout to a branch by its index
44
+
45
+ ```bash
46
+ gith checkout 2
47
+ ```
@@ -0,0 +1,3 @@
1
+ """
2
+ Gith: A Typer-based CLI helper for Git operations.
3
+ """
gith-0.1.0/gith/cli.py ADDED
@@ -0,0 +1,189 @@
1
+ import typer
2
+ import subprocess
3
+ from typing import List
4
+ from rich.console import Console
5
+ from rich.table import Table
6
+
7
+ app = typer.Typer()
8
+ console = Console()
9
+
10
+ # TODO: implement better way of printint results, for example ERRORs
11
+
12
+ # TODO: test this outside. It shows the error, but still shows the empty table of branches.
13
+ def validate_git_repo():
14
+ """
15
+ Validate if Git is installed.
16
+ """
17
+ result = subprocess.run(["git", "status"], capture_output=True, text=True)
18
+ if not result.returncode == 0:
19
+ typer.echo("Error: Ensure you are in a Git repository.", err=True)
20
+
21
+ def list_git_branches() -> list:
22
+ """
23
+ Returns a list of local Git branches.
24
+ """
25
+ current = []
26
+ others = []
27
+ result = subprocess.run(["git", "branch"], capture_output=True, text=True)
28
+ for branch in result.stdout.split("\n"):
29
+ if branch.strip():
30
+ name = branch.strip()
31
+ if name.startswith("*"):
32
+ current.append(name.replace('*', '').strip())
33
+ else:
34
+ others.append(name)
35
+ return current + sorted(others)
36
+
37
+ def create_branch(branch_name: str, from_branch: int = 1):
38
+ """
39
+ Create a new branch in the Git repository.
40
+ """
41
+ # TODO: improve this code to always do:
42
+ # 1. checkout to the branch if from_branch is provided, else stay in current branch
43
+ # 2. pull the latest changes
44
+ # 3. create a new branch from the current branch
45
+ # If `from_branch` is None, then asume from_branch to be the current branch.
46
+ branches = list_git_branches()
47
+ if branch_name in branches:
48
+ console.print(f"Sorry my friend, branch [green]{branch_name}[/green] already exists.")
49
+ raise typer.Abort()
50
+
51
+ if from_branch != 1:
52
+ # TODO: try-catch
53
+ # checkout to specified branch, pull the latest changes, and create a new branch from it.
54
+ # TODO: do we stash before checkout?
55
+ checkout_to_branch(branches[from_branch - 1])
56
+ current_branch = branches[0]
57
+ subprocess.run(["git", "pull", "origin", f"{current_branch}"], capture_output=True, text=True)
58
+ result = subprocess.run(["git", "branch", branch_name], capture_output=True, text=True)
59
+ if result.returncode == 0:
60
+ console.print(f"New branch [green]{branch_name}[/green] created.")
61
+
62
+ def checkout_to_branch(branch_name: str):
63
+ """
64
+ Checkout to the specified branch.
65
+ """
66
+ result = subprocess.run(["git", "checkout", branch_name], capture_output=True, text=True)
67
+ if result.returncode == 0:
68
+ console.print(f"Switched to branch [green]{branch_name}[/green].")
69
+ else:
70
+ console.print(f"Error: Unable to switch to branch [red]{branch_name}[/red].")
71
+ # TODO: improve how we show this error
72
+ console.print(f"{result.stderr}")
73
+
74
+ def _process_str_indexes(indexes: str) -> list:
75
+ """
76
+ Process the string of indexes and return a list of integers.
77
+ """
78
+ try:
79
+ return [int(index) for index in indexes.split(",")]
80
+ except Exception as e:
81
+ console.print(f"[red]Error[/red] processing provided indexes: {e}")
82
+ raise typer.Abort()
83
+
84
+ def delete_branches(delete: str):
85
+ """
86
+ Delete branches by their indexes.
87
+ """
88
+ branches = list_git_branches()
89
+ indexes = _process_str_indexes(delete)
90
+ for index, branch_name in enumerate(branches, start=1):
91
+ try:
92
+ if index in indexes:
93
+ result = subprocess.run(["git", "branch", "-D", branch_name], capture_output=True, text=True)
94
+ if result.returncode == 0:
95
+ console.print(f"Branch [green]{branch_name}[/green] [red]deleted.[/red]")
96
+ else:
97
+ console.print(f"Error: Unable to delete branch [red]{branch_name}[/red].")
98
+ except Exception:
99
+ console.print(f"Error: Branch with index [green]{index}[/green] [red]not found.[/red]")
100
+
101
+ def keep_branches(keep: str):
102
+ """
103
+ Keep the branches specified by indexes. Delete the rest.
104
+ """
105
+ branches = list_git_branches()
106
+ indexes = _process_str_indexes(keep)
107
+ for index, branch_name in enumerate(branches, start=1):
108
+ try:
109
+ if index not in indexes:
110
+ result = subprocess.run(["git", "branch", "-D", branch_name], capture_output=True, text=True)
111
+ if result.returncode == 0:
112
+ console.print(f"Branch [green]{branch_name}[/green] [red]deleted.[/red]")
113
+ else:
114
+ console.print(f"Error: Unable to delete branch [red]{branch_name}[/red].")
115
+ except Exception:
116
+ console.print(f"Error: Branch with index [green]{index}[/green] [red]not found.[/red]")
117
+
118
+ @app.command()
119
+ def branch(
120
+ list: bool = typer.Option(False, "--list", "-l", help="List local branches. Default behaviour if you call 'gith branch' without any options."),
121
+ create: bool = typer.Option(False, "--create", "-c", help="Create a new branch."),
122
+ branch_name: List[str] = typer.Argument(None, help="Name of the branch. You can use spaces in the name."),
123
+ name_separator: str = typer.Option("_", help="Separator to use when creating a branch name with spaces. e.g: gith branch -c my new branch --name-separator -"),
124
+ checkout: bool = typer.Option(True, help="Automatically checkout the new branch after creating it."),
125
+ delete: str = typer.Option(False, "--delete", "-d", help="Delete branches by their indexes."),
126
+ keep: str = typer.Option(False, "--keep", "-k", help="Keep the branches specified by indexes. CAUTION: this will delete the other branches."),
127
+ from_branch: int = typer.Option(None, "--from", "-f", help="Index of the branch to create a new branch from. e.g: 'gith branch -c new_branch from 2' will create a new branch from 2."),
128
+ ):
129
+ """
130
+ A helper command to list local Git branches.
131
+ """
132
+ validate_git_repo()
133
+ if delete != 'False' and keep != 'False': # TODO: find the best way to check this.
134
+ console.print("[red]Error:[/red] You can not use 'delete' and 'keep' options together. Please use only one.")
135
+ console.print(
136
+ (
137
+ "[yellow]Reminder:[/yellow] Use gith branch --delete 1,4,5 to delete branches of those indexes.\n"
138
+ "Use gith branch --keep 6,9 to keep branches of those indexes and DELETE the rest."
139
+ )
140
+ )
141
+ raise typer.Abort()
142
+
143
+ # TODO: find the best way to detect the action
144
+ if delete != 'False':
145
+ delete_branches(delete)
146
+ elif keep != 'False':
147
+ keep_branches(keep)
148
+ elif list or not list and not create and not branch_name:
149
+ # then return default, which is list
150
+ # TODO: check best way to do this
151
+ branches = list_git_branches()
152
+ table = Table()
153
+ # Add columns to the table
154
+ table.add_column("Index", justify="right", style="cyan", no_wrap=True)
155
+ table.add_column("Branch Name")
156
+
157
+ # Add rows
158
+ for index, branch in enumerate(branches, start=1):
159
+ if index == 1:
160
+ # Highlight the current branch with green color
161
+ table.add_row(str(index), branch, style="green")
162
+ else:
163
+ table.add_row(str(index), branch)
164
+ # Print the table to the console
165
+ console.print(table)
166
+ elif create:
167
+ if not branch_name:
168
+ typer.echo(
169
+ "Error: Please provide a branch name. e.g: gith branch -c my_new_branch.",
170
+ err=True
171
+ )
172
+ else:
173
+ branch_name = f"{name_separator}".join(branch_name)
174
+ create_branch(branch_name, from_branch)
175
+ if checkout:
176
+ checkout_to_branch(branch_name)
177
+
178
+
179
+ @app.command()
180
+ def checkout(index: int):
181
+ """
182
+ A helper command to checkout to a branch by its index.
183
+ """
184
+ checkout_to_branch(list_git_branches()[index - 1])
185
+
186
+
187
+ if __name__ == "__main__":
188
+ # TODO: explore, test, implement autocompletion
189
+ app()
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.1
2
+ Name: gith
3
+ Version: 0.1.0
4
+ Summary: A Typer-based helper for Git operations
5
+ Author-email: "Reinaldo J. Menéndez" <rejamen@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Reinaldo J. Menéndez
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Classifier: Programming Language :: Python :: 3
29
+ Classifier: License :: OSI Approved :: MIT License
30
+ Classifier: Operating System :: OS Independent
31
+ Requires-Python: >=3.7
32
+ Description-Content-Type: text/markdown
33
+ License-File: LICENSE
34
+ Requires-Dist: typer
35
+ Requires-Dist: rich
36
+
37
+ # gith
38
+
39
+ A Typer-based CLI helper for Git operations.
40
+
41
+ ## Installation
42
+
43
+ ```bash
44
+ pip install gith
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ### List local branches
50
+
51
+ ```bash
52
+ gith branch --list
53
+ ```
54
+
55
+ ### Create a new branch
56
+
57
+ ```bash
58
+ gith branch --create new_branch_name
59
+ ```
60
+
61
+ ### Create a new branch from a specific branch
62
+
63
+ ```bash
64
+ gith branch --create new_branch_name --from 2
65
+ ```
66
+
67
+ ### Delete branches by their indexes
68
+
69
+ ```bash
70
+ gith branch --delete 1,3,5
71
+ ```
72
+
73
+ ### Keep specific branches and delete the rest
74
+
75
+ ```bash
76
+ gith branch --keep 2,4
77
+ ```
78
+
79
+ ### Checkout to a branch by its index
80
+
81
+ ```bash
82
+ gith checkout 2
83
+ ```
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ gith/__init__.py
5
+ gith/cli.py
6
+ gith.egg-info/PKG-INFO
7
+ gith.egg-info/SOURCES.txt
8
+ gith.egg-info/dependency_links.txt
9
+ gith.egg-info/entry_points.txt
10
+ gith.egg-info/requires.txt
11
+ gith.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ gith = gith.cli:app
@@ -0,0 +1,2 @@
1
+ typer
2
+ rich
@@ -0,0 +1 @@
1
+ gith
@@ -0,0 +1,21 @@
1
+ [build-system]
2
+ requires = ["setuptools", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "gith"
7
+ version = "0.1.0"
8
+ description = "A Typer-based helper for Git operations"
9
+ authors = [{ name = "Reinaldo J. Menéndez", email = "rejamen@gmail.com" }]
10
+ license = { file = "LICENSE" }
11
+ readme = "README.md"
12
+ requires-python = ">=3.7"
13
+ dependencies = ["typer", "rich"]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent"
18
+ ]
19
+
20
+ [project.scripts]
21
+ gith = "gith.cli:app"
gith-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+