11import os
22import configparser
3+ from datetime import datetime
34
45from microprojects .ngit .repository import GitRepository , repo_dir , repo_file , ref_list
6+ from microprojects .ngit .repository import GitIndex , GitIndexEntry
57from microprojects .ngit .object import GitObject , GitCommit , GitBlob , GitTag , GitTree
68from microprojects .ngit .object_utils import object_read , object_find , object_write
7- from microprojects .ngit .object_utils import object_pick , shortify_hash
9+ from microprojects .ngit .object_utils import object_pick , shortify_hash , index_read
810
911
1012def repo_default_config () -> configparser .ConfigParser :
@@ -122,15 +124,9 @@ def object_hash(repo: GitRepository | None, file, fmt: bytes) -> str:
122124
123125
124126def ls_tree (
125- repo : GitRepository ,
126- sha1 : str ,
127- only_trees : bool ,
128- recurse_trees : bool ,
129- always_trees : bool ,
130- null_terminator : bool ,
131- format_str : str ,
132- _prefix : str = "" ,
133- ) -> None :
127+ repo : GitRepository , sha1 : str , only_trees : bool , recurse_trees : bool ,
128+ always_trees : bool , null_terminator : bool , format_str : str , _prefix : str = "" ,
129+ ) -> None : # fmt: skip
134130 """List the contents of a tree object
135131
136132 Parameters:
@@ -162,7 +158,7 @@ def prettify(leaf, format_str: str, obj_fmt: str, _prefix: str) -> str:
162158 if type (obj ) is not GitTree :
163159 raise TypeError (f"fatal: { sha1 } do not point to valid GitTree" )
164160
165- if not any ([recurse_trees , always_trees , only_trees ]):
161+ if not any ([only_trees , recurse_trees , always_trees ]):
166162 always_trees = True # set always_trees, if no
167163
168164 for leaf in obj .data :
@@ -183,20 +179,45 @@ def prettify(leaf, format_str: str, obj_fmt: str, _prefix: str) -> str:
183179 if always_trees or only_trees :
184180 print (prettify (leaf , format_str , obj_fmt , _prefix ), end = endl )
185181 if recurse_trees :
186- ls_tree (
187- repo ,
188- leaf .sha1 ,
189- only_trees ,
190- recurse_trees ,
191- always_trees ,
192- null_terminator ,
193- format_str ,
194- _prefix = os .path .join (_prefix , leaf .path ),
195- )
182+ ls_tree (repo , leaf .sha1 , only_trees , recurse_trees , always_trees ,
183+ null_terminator , format_str , _prefix = os .path .join (_prefix , leaf .path )) # fmt: skip
184+
196185 elif not only_trees :
197186 print (prettify (leaf , format_str , obj_fmt , _prefix ), end = endl )
198187
199188
189+ def ls_files (repo : GitRepository , fmt : str , endl : str = "\n " ) -> None :
190+ """"""
191+
192+ def prettify (entry : GitIndexEntry , format_str : str ) -> str :
193+ obj_type : str = {0o10 : "blob" , 0o12 : "symlink" , 0o16 : "commit" }[entry .mode_type ]
194+
195+ format_str = format_str .replace ("%(objectmode)" , f"{ entry .mode_type :02o} { entry .mode_perms :04o} " ) # fmt: skip
196+ format_str = format_str .replace ("%(objecttype)" , f"{ obj_type } " )
197+ format_str = format_str .replace ("%(objectname)" , f"{ entry .sha1 } " )
198+ format_str = format_str .replace ("%(objectsize)" , f"{ entry .file_size } " )
199+ format_str = format_str .replace ("%(stage)" , f"{ entry .flag_stage } " )
200+ format_str = format_str .replace ("%(path)" , f"{ entry .name } " )
201+
202+ # Extras
203+ format_str = format_str .replace ("%(ctime)" , f"{ entry .ctime_s } :{ entry .ctime_n } " )
204+ format_str = format_str .replace ("%(mtime)" , f"{ entry .mtime_s } :{ entry .mtime_n } " )
205+ format_str = format_str .replace ("%(ctime:iso)" , f"{ datetime .fromtimestamp (entry .ctime_s )} " ) # fmt: skip
206+ format_str = format_str .replace ("%(mtime:iso)" , f"{ datetime .fromtimestamp (entry .mtime_s )} " ) # fmt: skip
207+ format_str = format_str .replace ("%(dev)" , f"{ entry .dev } " )
208+ format_str = format_str .replace ("%(ino)" , f"{ entry .ino } " )
209+ format_str = format_str .replace ("%(uid)" , f"{ entry .uid } " )
210+ format_str = format_str .replace ("%(gid)" , f"{ entry .gid } " )
211+ format_str = format_str .replace ("%(gid)" , f"{ entry .gid } " )
212+ format_str = format_str .replace ("%(flags)" , f"{ entry .flag_assume_valid } { entry .flag_stage } " ) # fmt: skip
213+
214+ return format_str
215+
216+ index : GitIndex = index_read (repo )
217+ for entry in index .entries :
218+ print (prettify (entry , fmt ))
219+
220+
200221def checkout (repo : GitRepository , tree : GitTree , path : str , quiet : bool ) -> None :
201222 """Switch branches or restore working tree files
202223
@@ -222,9 +243,7 @@ def checkout(repo: GitRepository, tree: GitTree, path: str, quiet: bool) -> None
222243 file .write (obj .data )
223244
224245
225- def show_ref (
226- repo : GitRepository , refs : list , only_sha1 : bool , deref : bool , prefx = "refs"
227- ) -> None :
246+ def show_ref (repo : GitRepository , refs : list , only_sha1 : bool , deref : bool , prefx = "refs" ) -> None : # fmt: skip
228247 """List references in repo under ref, verify them, and print relevant information
229248
230249 Parameters:
0 commit comments