Skip to content

Commit 4d57159

Browse files
committed
perf: optimize status method with first commit check
Add _is_first_commit helper and update tests accordingly
1 parent 861374f commit 4d57159

File tree

2 files changed

+78
-19
lines changed

2 files changed

+78
-19
lines changed

jupyterlab_git/git.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ def remove_cell_ids(nb):
486486

487487
return {"base": prev_nb, "diff": thediff}
488488

489+
async def _is_first_commit(self, path: str) -> bool:
490+
"""Return True if repo has no commits yet."""
491+
code, _, _ = await self.__execute(
492+
["git", "rev-parse", "--verify", "HEAD"], cwd=path
493+
)
494+
return code != 0
495+
496+
489497
async def status(self, path: str) -> dict:
490498
"""
491499
Execute git status command & return the result.
@@ -501,14 +509,20 @@ async def status(self, path: str) -> dict:
501509
}
502510

503511
# Add attribute `is_binary`
504-
command = [ # Compare stage to an empty tree see `_is_binary`
505-
"git",
506-
"diff",
507-
"--numstat",
508-
"-z",
509-
"--cached",
510-
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
511-
]
512+
first_commit = await self._is_first_commit(path)
513+
if first_commit:
514+
# only first commit has to compare to an empty tree
515+
command = [ # Compare stage to an empty tree see `_is_binary`
516+
"git",
517+
"diff",
518+
"--numstat",
519+
"-z",
520+
"--cached",
521+
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
522+
]
523+
else:
524+
command = ["git", "diff", "--numstat", "-z", "--cached"]
525+
512526
text_code, text_output, _ = await self.__execute(command, cwd=path)
513527

514528
are_binary = dict()

jupyterlab_git/tests/test_status.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,18 @@ async def test_status(tmp_path, output, diff_output, expected):
353353
repository = tmp_path / "test_curr_path"
354354
(repository / ".git" / "rebase-merge").mkdir(parents=True)
355355

356+
# Determine if this is a first commit scenario
357+
is_first_commit = expected.get("branch") == "(initial)"
358+
359+
# Build mock_execute.side_effect list
360+
# First: git status call
361+
# Second: git rev-parse --verify HEAD (for _is_first_commit)
362+
# Third: git diff call (with or without empty tree SHA based on is_first_commit)
363+
# Then: state detection calls (cherry pick, merge, rebase)
356364
mock_execute.side_effect = [
357-
maybe_future((0, "\x00".join(output) + "\x00", "")),
358-
maybe_future((0, "\x00".join(diff_output) + "\x00", "")),
365+
maybe_future((0, "\x00".join(output) + "\x00", "")), # git status
366+
maybe_future((128 if is_first_commit else 0, "", "")), # git rev-parse --verify HEAD
367+
maybe_future((0, "\x00".join(diff_output) + "\x00", "")), # git diff
359368
maybe_future((0 if expected["state"] == 4 else 128, "", "cherry pick")),
360369
maybe_future((0 if expected["state"] == 2 else 128, "", "merge")),
361370
maybe_future(
@@ -381,21 +390,57 @@ async def test_status(tmp_path, output, diff_output, expected):
381390
is_binary=False,
382391
),
383392
call(
384-
[
385-
"git",
386-
"diff",
387-
"--numstat",
388-
"-z",
389-
"--cached",
390-
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
391-
],
393+
["git", "rev-parse", "--verify", "HEAD"],
392394
cwd=str(repository),
393395
timeout=20,
394396
env=None,
395397
username=None,
396398
password=None,
397399
is_binary=False,
398400
),
401+
]
402+
403+
# Add diff command based on whether it's first commit
404+
if is_first_commit:
405+
expected_calls.append(
406+
call(
407+
[
408+
"git",
409+
"diff",
410+
"--numstat",
411+
"-z",
412+
"--cached",
413+
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
414+
],
415+
cwd=str(repository),
416+
timeout=20,
417+
env=None,
418+
username=None,
419+
password=None,
420+
is_binary=False,
421+
)
422+
)
423+
else:
424+
expected_calls.append(
425+
call(
426+
[
427+
"git",
428+
"diff",
429+
"--numstat",
430+
"-z",
431+
"--cached",
432+
],
433+
cwd=str(repository),
434+
timeout=20,
435+
env=None,
436+
username=None,
437+
password=None,
438+
is_binary=False,
439+
)
440+
)
441+
442+
# Add state detection calls
443+
expected_calls.extend([
399444
call(
400445
["git", "show", "--quiet", "CHERRY_PICK_HEAD"],
401446
cwd=str(repository),
@@ -432,7 +477,7 @@ async def test_status(tmp_path, output, diff_output, expected):
432477
password=None,
433478
is_binary=False,
434479
),
435-
]
480+
])
436481

437482
if expected["state"] == 4:
438483
expected_calls = expected_calls[:-3]

0 commit comments

Comments
 (0)