@@ -55,6 +55,8 @@ def _path_to_object(self, path, ref):
55
55
tree = comm .tree
56
56
for part in parts :
57
57
if part and isinstance (tree , pygit2 .Tree ):
58
+ if part not in tree :
59
+ raise FileNotFoundError (path )
58
60
tree = tree [part ]
59
61
return tree
60
62
@@ -69,46 +71,32 @@ def _get_kwargs_from_urls(path):
69
71
out ["ref" ], path = path .split ("@" , 1 )
70
72
return out
71
73
74
+ @staticmethod
75
+ def _object_to_info (obj , path = None ):
76
+ # obj.name and obj.filemode are None for the root tree!
77
+ is_dir = isinstance (obj , pygit2 .Tree )
78
+ return {
79
+ "type" : "directory" if is_dir else "file" ,
80
+ "name" : (
81
+ "/" .join ([path , obj .name or "" ]).lstrip ("/" ) if path else obj .name
82
+ ),
83
+ "hex" : str (obj .id ),
84
+ "mode" : "100644" if obj .filemode is None else f"{ obj .filemode :o} " ,
85
+ "size" : 0 if is_dir else obj .size ,
86
+ }
87
+
72
88
def ls (self , path , detail = True , ref = None , ** kwargs ):
73
- path = self ._strip_protocol (path )
74
- tree = self ._path_to_object (path , ref )
75
- if isinstance (tree , pygit2 .Tree ):
76
- out = []
77
- for obj in tree :
78
- if isinstance (obj , pygit2 .Tree ):
79
- out .append (
80
- {
81
- "type" : "directory" ,
82
- "name" : "/" .join ([path , obj .name ]).lstrip ("/" ),
83
- "hex" : str (obj .id ),
84
- "mode" : f"{ obj .filemode :o} " ,
85
- "size" : 0 ,
86
- }
87
- )
88
- else :
89
- out .append (
90
- {
91
- "type" : "file" ,
92
- "name" : "/" .join ([path , obj .name ]).lstrip ("/" ),
93
- "hex" : str (obj .id ),
94
- "mode" : f"{ obj .filemode :o} " ,
95
- "size" : obj .size ,
96
- }
97
- )
98
- else :
99
- obj = tree
100
- out = [
101
- {
102
- "type" : "file" ,
103
- "name" : obj .name ,
104
- "hex" : str (obj .id ),
105
- "mode" : f"{ obj .filemode :o} " ,
106
- "size" : obj .size ,
107
- }
108
- ]
109
- if detail :
110
- return out
111
- return [o ["name" ] for o in out ]
89
+ tree = self ._path_to_object (self ._strip_protocol (path ), ref )
90
+ return [
91
+ GitFileSystem ._object_to_info (obj , path )
92
+ if detail
93
+ else GitFileSystem ._object_to_info (obj , path )["name" ]
94
+ for obj in (tree if isinstance (tree , pygit2 .Tree ) else [tree ])
95
+ ]
96
+
97
+ def info (self , path , ref = None , ** kwargs ):
98
+ tree = self ._path_to_object (self ._strip_protocol (path ), ref )
99
+ return GitFileSystem ._object_to_info (tree , path )
112
100
113
101
def ukey (self , path , ref = None ):
114
102
return self .info (path , ref = ref )["hex" ]
0 commit comments