Skip to content

Commit a40687b

Browse files
author
Douglas Lassance
committed
Add profile argument to update and claim commands
1 parent 644cd30 commit a40687b

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

gitalong/batch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ async def update_tracked_commits(
413413
)
414414
absolute_filenames = []
415415
if repository.config.get("modify_permissions"):
416-
print("Updating permissions")
417416
for filename in repository.files:
418417
absolute_filenames.append(repository.get_absolute_path(filename))
419418
await repository.batch.update_files_permissions(absolute_filenames)

gitalong/cli.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .__info__ import __version__
88
from .enums import CommitSpread
99
from .repository import Repository
10+
from .contexts import ProfileContext
1011
from .batch import get_files_last_commits, claim_files, update_tracked_commits
1112

1213

@@ -66,9 +67,25 @@ def config(ctx, prop): # pylint: disable=missing-function-docstring
6667
"files their permissions changed."
6768
)
6869
)
70+
@click.option(
71+
"-p",
72+
"--profile",
73+
is_flag=True,
74+
help=(
75+
"Will generate a profile file in the current working directory."
76+
"The file can be opened in a profiler like snakeviz."
77+
),
78+
)
6979
@click.pass_context
70-
def update(ctx):
71-
"""Update tracked commits with local changes."""
80+
def update(ctx, profile=False): # pylint: disable=missing-function-docstring
81+
if profile:
82+
with ProfileContext():
83+
run_update(ctx)
84+
return
85+
run_update(ctx)
86+
87+
88+
def run_update(ctx): # pylint: disable=missing-function-docstring
7289
repository = Repository.from_filename(ctx.obj.get("REPOSITORY", ""))
7390
if repository:
7491
asyncio.run(update_tracked_commits(repository))
@@ -94,13 +111,8 @@ def update(ctx):
94111
@click.pass_context
95112
def status(ctx, filename, profile=False): # pylint: disable=missing-function-docstring
96113
if profile:
97-
import cProfile # pylint: disable=import-outside-toplevel
98-
import pstats # pylint: disable=import-outside-toplevel
99-
100-
with cProfile.Profile() as pr:
114+
with ProfileContext():
101115
run_status(ctx, filename)
102-
results = pstats.Stats(pr)
103-
results.dump_stats("gitalong.prof")
104116
return
105117
run_status(ctx, filename)
106118

@@ -132,8 +144,25 @@ def run_status(ctx, filename): # pylint: disable=missing-function-docstring
132144
nargs=-1,
133145
# help="The path to the file that should be made writable."
134146
)
147+
@click.option(
148+
"-p",
149+
"--profile",
150+
is_flag=True,
151+
help=(
152+
"Will generate a profile file in the current working directory."
153+
"The file can be opened in a profiler like snakeviz."
154+
),
155+
)
135156
@click.pass_context
136-
def claim(ctx, filename): # pylint: disable=missing-function-docstring
157+
def claim(ctx, filename, profile=False): # pylint: disable=missing-function-docstring
158+
if profile:
159+
with ProfileContext():
160+
run_claim(ctx, filename)
161+
return
162+
run_claim(ctx, filename)
163+
164+
165+
def run_claim(ctx, filename): # pylint: disable=missing-function-docstring
137166
absolute_filenames = []
138167
for filename_ in filename:
139168
repository = Repository.from_filename(ctx.obj.get("REPOSITORY", filename_))

gitalong/commit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def commit_spread(self) -> int:
148148
information about where this commit lives across branches and clones.
149149
"""
150150
commit_spread = 0
151-
active_branch = self._repository.active_branch_name
151+
active_branch = self._repository.active_branch_name if self._repository else ""
152152
if self.get("user", ""):
153153
is_issued = self.is_issued_commit()
154154
if "sha" in self:

gitalong/contexts.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cProfile
2+
import pstats
3+
4+
5+
class ProfileContext:
6+
"""Profile context manager."""
7+
8+
def __init__(self):
9+
self._profile = None
10+
11+
def __enter__(self):
12+
self._profile = cProfile.Profile()
13+
self._profile.enable()
14+
15+
def __exit__(self, exc_type, exc_val, exc_tb):
16+
if self._profile:
17+
self._profile.disable()
18+
results = pstats.Stats(self._profile)
19+
results.dump_stats("gitalong.prof")

0 commit comments

Comments
 (0)