MCPcopy Create free account
hub / github.com/libgit2/pygit2 / hashfile

Method hashfile

pygit2/repository.py:187–238  ·  view source on GitHub ↗

Calculate the hash of a file using repository filtering rules. If you simply want to calculate the hash of a file on disk with no filters, you can just use `pygit2.hashfile()`. However, if you want to hash a file in the repository and you want to apply filtering rules (e.g.

(
        self,
        path: str,
        object_type: ObjectType = ObjectType.BLOB,
        as_path: str | None = None,
    )

Source from the content-addressed store, hash-verified

185 return builder.written_objects_count
186
187 def hashfile(
188 self,
189 path: str,
190 object_type: ObjectType = ObjectType.BLOB,
191 as_path: str | None = None,
192 ) -> Oid:
193 """Calculate the hash of a file using repository filtering rules.
194
195 If you simply want to calculate the hash of a file on disk with no filters,
196 you can just use `pygit2.hashfile()`. However, if you want to hash a file
197 in the repository and you want to apply filtering rules (e.g. crlf filters)
198 before generating the SHA, then use this function.
199
200 Note: if the repository has `core.safecrlf` set to fail and the filtering
201 triggers that failure, then this function will raise an error and not
202 calculate the hash of the file.
203
204 Returns: Output value of calculated SHA (Oid)
205
206 Parameters:
207
208 path
209 Path to file on disk whose contents should be hashed. This may be
210 an absolute path or a relative path, in which case it will be treated
211 as a path within the working directory.
212
213 object_type
214 The object type to hash (e.g. enums.ObjectType.BLOB)
215
216 as_path
217 The path to use to look up filtering rules. If this is an empty string
218 then no filters will be applied when calculating the hash.
219 If this is `None` and the `path` parameter is a file within the
220 repository's working directory, then the `path` will be used.
221 """
222 c_path = to_bytes(path)
223
224 c_as_path: ffi.NULL_TYPE | bytes
225 if as_path is None:
226 c_as_path = ffi.NULL
227 else:
228 c_as_path = to_bytes(as_path)
229
230 c_oid = ffi.new('git_oid *')
231
232 err = C.git_repository_hashfile(
233 c_oid, self._repo, c_path, int(object_type), c_as_path
234 )
235 check_error(err)
236
237 oid = Oid(raw=bytes(ffi.buffer(c_oid.id)[:]))
238 return oid
239
240 def load_filter_list(
241 self, path: str, mode: FilterMode = FilterMode.TO_ODB

Callers 3

test_hashfileFunction · 0.80
test_repository_hashfileFunction · 0.80

Calls 2

to_bytesFunction · 0.85
check_errorFunction · 0.85

Tested by 3

test_hashfileFunction · 0.64
test_repository_hashfileFunction · 0.64