Skip to content

Commit b5954dc

Browse files
author
dualc
committed
优化git status逻辑
1 parent 81d90f1 commit b5954dc

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

jupyterlab_git/git.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,13 @@ 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+
489496
async def status(self, path: str) -> dict:
490497
"""
491498
Execute git status command & return the result.
@@ -501,14 +508,19 @@ async def status(self, path: str) -> dict:
501508
}
502509

503510
# 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-
]
511+
first_commit = await self._is_first_commit(path)
512+
if first_commit:
513+
# only first commit has to compare to an empty tree
514+
command = [ # Compare stage to an empty tree see `_is_binary`
515+
"git",
516+
"diff",
517+
"--numstat",
518+
"-z",
519+
"--cached",
520+
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
521+
]
522+
else:
523+
command = ["git", "diff", "--numstat", "-z", "--cached"]
512524
text_code, text_output, _ = await self.__execute(command, cwd=path)
513525

514526
are_binary = dict()

0 commit comments

Comments
 (0)