Skip to content

Commit

Permalink
[chore][command] implements show-index for reading index easily
Browse files Browse the repository at this point in the history
  • Loading branch information
smd1121 committed Jan 5, 2024
1 parent 8af17de commit 3dfb5d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion xgit/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typer

from xgit.commands import init, cat_file, ls_files, hash_object
from xgit.commands import init, cat_file, ls_files, show_index, hash_object

app = typer.Typer(add_completion=False, rich_markup_mode="markdown")

Expand All @@ -9,6 +9,7 @@
app.command()(init.init)
app.command()(cat_file.cat_file)
app.command()(ls_files.ls_files)
app.command(hidden=True)(show_index.show_index)


def main():
Expand Down
12 changes: 12 additions & 0 deletions xgit/commands/show_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rich.pretty import pprint

from xgit.types.index import get_index


def show_index():
"""
以可读的方式输出 index。
这并非 git 本身支持的功能,只是为了方便调试和展示结果。
"""
index = get_index()
pprint(index)
28 changes: 28 additions & 0 deletions xgit/types/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def to_bytes(self) -> bytes:
data |= self.name_length
return data.to_bytes(2, "big")

def __rich_repr__(self):
yield "assume_valid", self.assume_valid
yield "extended", self.extended
yield "stage", self.stage
yield "name_length", self.name_length

ctime_s: int
ctime_ns: int
mtime_s: int
Expand Down Expand Up @@ -186,6 +192,22 @@ def to_bytes(self) -> bytes:

return entry

def __rich_repr__(self):
yield "ctime_s", self.ctime_s
yield "ctime_ns", self.ctime_ns
yield "mtime_s", self.mtime_s
yield "mtime_ns", self.mtime_ns
yield "dev", self.dev
yield "inode", self.inode
yield "mode", self.mode
yield "uid", self.uid
yield "gid", self.gid
yield "file_size", self.file_size
yield "sha", self.sha
yield "flags", self.flags
yield "extended_flags", self.extended_flags
yield "file_name", self.file_name


class Index:
version: int
Expand Down Expand Up @@ -217,6 +239,12 @@ def to_bytes(self) -> bytes:
index += hashlib.sha1(index).digest()
return index

def __rich_repr__(self):
yield "version", self.version
yield "entry_count", self.entry_count
yield "entries", self.entries
yield "extensions", self.extensions


def get_index() -> Index:
index_path = find_repo() / GIT_DIR / "index"
Expand Down

0 comments on commit 3dfb5d3

Please sign in to comment.