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

Class Index

pygit2/index.py:42–387  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40
41
42class Index:
43 # XXX Implement the basic features in C (_pygit2.Index) and make
44 # pygit2.Index to inherit from _pygit2.Index? This would allow for
45 # a proper implementation in some places: e.g. checking the index type
46 # from C code (see Tree_diff_to_index)
47
48 def __init__(self, path: str | PathLike[str] | None = None) -> None:
49 """Create a new Index
50
51 If path is supplied, the read and write methods will use that path
52 to read from and write to.
53 """
54 cindex = ffi.new('git_index **')
55 err = C.git_index_open(cindex, to_bytes(path))
56 check_error(err)
57
58 self._repo = None
59 self._index = cindex[0]
60 self._cindex = cindex
61
62 @classmethod
63 def from_c(cls, repo, ptr):
64 index = cls.__new__(cls)
65 index._repo = repo
66 index._index = ptr[0]
67 index._cindex = ptr
68
69 return index
70
71 @property
72 def _pointer(self):
73 return bytes(ffi.buffer(self._cindex)[:])
74
75 def __del__(self) -> None:
76 C.git_index_free(self._index)
77
78 def __len__(self) -> int:
79 return C.git_index_entrycount(self._index)
80
81 def __contains__(self, path) -> bool:
82 err = C.git_index_find(ffi.NULL, self._index, to_bytes(path))
83 if err == C.GIT_ENOTFOUND:
84 return False
85
86 check_error(err)
87 return True
88
89 def __getitem__(self, key: str | int | PathLike[str]) -> 'IndexEntry':
90 centry = ffi.NULL
91 if isinstance(key, str) or hasattr(key, '__fspath__'):
92 centry = C.git_index_get_bypath(self._index, to_bytes(key), 0)
93 elif isinstance(key, int):
94 if key >= 0:
95 centry = C.git_index_get_byindex(self._index, key)
96 else:
97 raise ValueError(key)
98 else:
99 raise TypeError(f'Expected str or int, got {type(key)}')

Callers 6

check_writingFunction · 0.90
test_create_emptyFunction · 0.90
test_add_conflictFunction · 0.90
write_archiveMethod · 0.85

Calls

no outgoing calls

Tested by 5

check_writingFunction · 0.72
test_create_emptyFunction · 0.72
test_add_conflictFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…