Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase performance of typer autocompletion by breaking out subcommand functions into their own module #77

Merged
merged 2 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 24 additions & 41 deletions git_sim/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,10 @@
import sys
import datetime
import time
import git

import git_sim.add
import git_sim.branch
import git_sim.cherrypick
import git_sim.commit
import git_sim.log
import git_sim.merge
import git_sim.rebase
import git_sim.reset
import git_sim.restore
import git_sim.revert
import git_sim.stash
import git_sim.status
import git_sim.tag
import git_sim.switch
import git_sim.checkout
import git_sim.fetch
import git_sim.pull
import git_sim.push
import git_sim.clone

import git_sim.commands

from git_sim.settings import ImgFormat, VideoFormat, settings
from manim import config, WHITE

app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})

Expand Down Expand Up @@ -146,6 +126,9 @@ def main(
help="Color commits by parameter, such as author",
),
):
import git
from manim import config, WHITE

settings.animate = animate
settings.n = n
settings.auto_open = auto_open
Expand Down Expand Up @@ -199,25 +182,25 @@ def main(
config.output_file = "git-sim-" + ctx.invoked_subcommand + "_" + t + ".mp4"


app.command()(git_sim.add.add)
app.command()(git_sim.branch.branch)
app.command()(git_sim.cherrypick.cherry_pick)
app.command()(git_sim.commit.commit)
app.command()(git_sim.log.log)
app.command()(git_sim.merge.merge)
app.command()(git_sim.rebase.rebase)
app.command()(git_sim.reset.reset)
app.command()(git_sim.restore.restore)
app.command()(git_sim.revert.revert)
app.command()(git_sim.stash.stash)
app.command()(git_sim.status.status)
app.command()(git_sim.tag.tag)
app.command()(git_sim.switch.switch)
app.command()(git_sim.checkout.checkout)
app.command()(git_sim.fetch.fetch)
app.command()(git_sim.pull.pull)
app.command()(git_sim.push.push)
app.command()(git_sim.clone.clone)
app.command()(git_sim.commands.add)
app.command()(git_sim.commands.branch)
app.command()(git_sim.commands.checkout)
app.command()(git_sim.commands.cherry_pick)
app.command()(git_sim.commands.clone)
app.command()(git_sim.commands.commit)
app.command()(git_sim.commands.fetch)
app.command()(git_sim.commands.log)
app.command()(git_sim.commands.merge)
app.command()(git_sim.commands.pull)
app.command()(git_sim.commands.push)
app.command()(git_sim.commands.rebase)
app.command()(git_sim.commands.reset)
app.command()(git_sim.commands.restore)
app.command()(git_sim.commands.revert)
app.command()(git_sim.commands.stash)
app.command()(git_sim.commands.status)
app.command()(git_sim.commands.switch)
app.command()(git_sim.commands.tag)


if __name__ == "__main__":
Expand Down
13 changes: 0 additions & 13 deletions git_sim/add.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import sys
import git
import manim as m
import typer

from typing import List

from git_sim.animations import handle_animations
from git_sim.git_sim_base_command import GitSimBaseCommand
from git_sim.settings import settings

Expand Down Expand Up @@ -82,14 +80,3 @@ def populate_zones(
firstColumnArrowMap[z] = m.Arrow(
stroke_width=3, color=self.fontColor
)


def add(
files: List[str] = typer.Argument(
default=None,
help="The names of one or more files to add to Git's staging area",
)
):
settings.hide_first_tag = True
scene = Add(files=files)
handle_animations(scene=scene)
12 changes: 0 additions & 12 deletions git_sim/branch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import manim as m
import typer

from git_sim.animations import handle_animations
from git_sim.git_sim_base_command import GitSimBaseCommand
from git_sim.settings import settings

Expand Down Expand Up @@ -52,13 +50,3 @@ def construct(self):
self.color_by()
self.fadeout()
self.show_outro()


def branch(
name: str = typer.Argument(
...,
help="The name of the new branch",
)
):
scene = Branch(name=name)
handle_animations(scene=scene)
25 changes: 6 additions & 19 deletions git_sim/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import git
import manim as m
import numpy
import typer

from git_sim.animations import handle_animations
from git_sim.git_sim_base_command import GitSimBaseCommand
from git_sim.settings import settings

Expand All @@ -19,7 +17,11 @@ def __init__(self, branch: str, b: bool):

if self.b:
if self.branch in self.repo.heads:
print("git-sim error: can't create new branch '" + self.branch + "', it already exists")
print(
"git-sim error: can't create new branch '"
+ self.branch
+ "', it already exists"
)
sys.exit(1)
else:
try:
Expand Down Expand Up @@ -75,7 +77,7 @@ def construct(self):
branch_commit = self.get_commit(self.branch)

if self.is_ancestor:
commits_in_range = list(self.repo.iter_commits(self.branch + '..HEAD'))
commits_in_range = list(self.repo.iter_commits(self.branch + "..HEAD"))

# branch is reached from HEAD, so draw everything
if len(commits_in_range) <= self.n:
Expand Down Expand Up @@ -113,18 +115,3 @@ def construct(self):
self.color_by()
self.fadeout()
self.show_outro()


def checkout(
branch: str = typer.Argument(
...,
help="The name of the branch to checkout",
),
b: bool = typer.Option(
False,
"-b",
help="Create the specified branch if it doesn't already exist",
),
):
scene = Checkout(branch=branch, b=b)
handle_animations(scene=scene)
18 changes: 0 additions & 18 deletions git_sim/cherrypick.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import git
import manim as m
import typer

from git_sim.animations import handle_animations
from git_sim.git_sim_base_command import GitSimBaseCommand
from git_sim.settings import settings

Expand Down Expand Up @@ -70,19 +68,3 @@ def construct(self):
self.color_by(offset=2)
self.fadeout()
self.show_outro()


def cherry_pick(
commit: str = typer.Argument(
...,
help="The ref (branch/tag), or commit ID to simulate cherry-pick onto active branch",
),
edit: str = typer.Option(
None,
"--edit",
"-e",
help="Specify a new commit message for the cherry-picked commit",
),
):
scene = CherryPick(commit=commit, edit=edit)
handle_animations(scene=scene)
27 changes: 9 additions & 18 deletions git_sim/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import git
import manim as m
import numpy
import typer
import tempfile
import shutil
import stat
import re

from git_sim.animations import handle_animations
from git_sim.git_sim_base_command import GitSimBaseCommand
from git_sim.settings import settings

Expand All @@ -28,28 +26,30 @@ def __init__(self, url: str):

def construct(self):
if not settings.stdout and not settings.output_only_path and not settings.quiet:
print(
f"{settings.INFO_STRING } {type(self).__name__.lower()} {self.url}"
)
print(f"{settings.INFO_STRING } {type(self).__name__.lower()} {self.url}")

self.show_intro()

# Configure paths to make local clone to run networked commands in
repo_name = re.search(r"/([^/]+)/?$", self.url)
if repo_name:
repo_name = repo_name.group(1)
if repo_name.endswith(".git"):
repo_name = repo_name[:-4]
else:
print(f"git-sim error: Invalid repo URL, please confirm repo URL and try again")
print(
f"git-sim error: Invalid repo URL, please confirm repo URL and try again"
)
sys.exit(1)
new_dir = os.path.join(tempfile.gettempdir(), "git_sim", repo_name)

# Create local clone of local repo
try:
self.repo = git.Repo.clone_from(self.url, new_dir, no_hardlinks=True)
except git.GitCommandError as e:
print(f"git-sim error: Invalid repo URL, please confirm repo URL and try again")
print(
f"git-sim error: Invalid repo URL, please confirm repo URL and try again"
)
sys.exit(1)

head_commit = self.get_commit()
Expand All @@ -69,7 +69,7 @@ def construct(self):

def add_details(self, repo_name):
text1 = m.Text(
f"Successfully cloned from {self.url} into ./{repo_name}",
f"Successfully cloned from {self.url} into ./{repo_name}",
font="Monospace",
font_size=20,
color=self.fontColor,
Expand Down Expand Up @@ -100,12 +100,3 @@ def add_details(self, repo_name):
def del_rw(action, name, exc):
os.chmod(name, stat.S_IWRITE)
os.remove(name)

def clone(
url: str = typer.Argument(
...,
help="The web URL or filesystem path of the Git repo to clone",
),
):
scene = Clone(url=url)
handle_animations(scene=scene)
Loading