summaryrefslogtreecommitdiff
path: root/git/index
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2022-05-18 08:01:38 +0800
committerSebastian Thiel <sebastian.thiel@icloud.com>2022-05-18 08:01:38 +0800
commite530544546b2a4e5f00e8d9458bf1b895573ec41 (patch)
tree9b957bd812fe98664d3f1f75615dda8242663097 /git/index
parentf78fc42b90711c81e06699d1ebdbe69e6648b949 (diff)
downloadgitpython-e530544546b2a4e5f00e8d9458bf1b895573ec41.tar.gz
reformat according to 'black' configuration file.
Diffstat (limited to 'git/index')
-rw-r--r--git/index/base.py108
-rw-r--r--git/index/fun.py53
-rw-r--r--git/index/typ.py4
-rw-r--r--git/index/util.py7
4 files changed, 46 insertions, 126 deletions
diff --git a/git/index/base.py b/git/index/base.py
index 48894833..edc64875 100644
--- a/git/index/base.py
+++ b/git/index/base.py
@@ -173,18 +173,14 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
def _deserialize(self, stream: IO) -> "IndexFile":
"""Initialize this instance with index values read from the given stream"""
- self.version, self.entries, self._extension_data, _conten_sha = read_cache(
- stream
- )
+ self.version, self.entries, self._extension_data, _conten_sha = read_cache(stream)
return self
def _entries_sorted(self) -> List[IndexEntry]:
""":return: list of entries, in a sorted fashion, first by path, then by stage"""
return sorted(self.entries.values(), key=lambda e: (e.path, e.stage))
- def _serialize(
- self, stream: IO, ignore_extension_data: bool = False
- ) -> "IndexFile":
+ def _serialize(self, stream: IO, ignore_extension_data: bool = False) -> "IndexFile":
entries = self._entries_sorted()
extension_data = self._extension_data # type: Union[None, bytes]
if ignore_extension_data:
@@ -242,9 +238,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
@post_clear_cache
@default_index
- def merge_tree(
- self, rhs: Treeish, base: Union[None, Treeish] = None
- ) -> "IndexFile":
+ def merge_tree(self, rhs: Treeish, base: Union[None, Treeish] = None) -> "IndexFile":
"""Merge the given rhs treeish into the current index, possibly taking
a common base treeish into account.
@@ -344,9 +338,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
it will be temporarily moved out of the way to assure there are no unsuspected
interferences."""
if len(treeish) == 0 or len(treeish) > 3:
- raise ValueError(
- "Please specify between 1 and 3 treeish, got %i" % len(treeish)
- )
+ raise ValueError("Please specify between 1 and 3 treeish, got %i" % len(treeish))
arg_list: List[Union[Treeish, str]] = []
# ignore that working tree and index possibly are out of date
@@ -383,9 +375,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# UTILITIES
@unbare_repo
- def _iter_expand_paths(
- self: "IndexFile", paths: Sequence[PathLike]
- ) -> Iterator[PathLike]:
+ def _iter_expand_paths(self: "IndexFile", paths: Sequence[PathLike]) -> Iterator[PathLike]:
"""Expand the directories in list of paths to the corresponding paths accordingly,
Note: git will add items multiple times even if a glob overlapped
@@ -415,9 +405,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# end check symlink
# if the path is not already pointing to an existing file, resolve globs if possible
- if not os.path.exists(abs_path) and (
- "?" in abs_path or "*" in abs_path or "[" in abs_path
- ):
+ if not os.path.exists(abs_path) and ("?" in abs_path or "*" in abs_path or "[" in abs_path):
resolved_paths = glob.glob(abs_path)
# not abs_path in resolved_paths:
# a glob() resolving to the same path we are feeding it with
@@ -525,9 +513,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
return path_map
@classmethod
- def entry_key(
- cls, *entry: Union[BaseIndexEntry, PathLike, StageType]
- ) -> Tuple[PathLike, StageType]:
+ def entry_key(cls, *entry: Union[BaseIndexEntry, PathLike, StageType]) -> Tuple[PathLike, StageType]:
return entry_key(*entry)
def resolve_blobs(self, iter_blobs: Iterator[Blob]) -> "IndexFile":
@@ -621,10 +607,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
if self.repo.bare:
raise InvalidGitRepositoryError("require non-bare repository")
if not str(path).startswith(str(self.repo.working_tree_dir)):
- raise ValueError(
- "Absolute path %r is not in git repository at %r"
- % (path, self.repo.working_tree_dir)
- )
+ raise ValueError("Absolute path %r is not in git repository at %r" % (path, self.repo.working_tree_dir))
return os.path.relpath(path, self.repo.working_tree_dir)
def _preprocess_add_items(
@@ -655,9 +638,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
st = os.lstat(filepath) # handles non-symlinks as well
if S_ISLNK(st.st_mode):
# in PY3, readlink is string, but we need bytes. In PY2, it's just OS encoded bytes, we assume UTF-8
- open_stream: Callable[[], BinaryIO] = lambda: BytesIO(
- force_bytes(os.readlink(filepath), encoding=defenc)
- )
+ open_stream: Callable[[], BinaryIO] = lambda: BytesIO(force_bytes(os.readlink(filepath), encoding=defenc))
else:
open_stream = lambda: open(filepath, "rb")
with open_stream() as stream:
@@ -830,9 +811,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# That way, we are OK on a bare repository as well.
# If there are no paths, the rewriter has nothing to do either
if paths:
- entries_added.extend(
- self._entries_for_paths(paths, path_rewriter, fprogress, entries)
- )
+ entries_added.extend(self._entries_for_paths(paths, path_rewriter, fprogress, entries))
# HANDLE ENTRIES
if entries:
@@ -845,9 +824,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# HANDLE ENTRY OBJECT CREATION
# create objects if required, otherwise go with the existing shas
- null_entries_indices = [
- i for i, e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA
- ]
+ null_entries_indices = [i for i, e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA]
if null_entries_indices:
@git_working_dir
@@ -876,9 +853,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# all object sha's
if path_rewriter:
for i, e in enumerate(entries):
- entries[i] = BaseIndexEntry(
- (e.mode, e.binsha, e.stage, path_rewriter(e))
- )
+ entries[i] = BaseIndexEntry((e.mode, e.binsha, e.stage, path_rewriter(e)))
# END for each entry
# END handle path rewriting
@@ -906,9 +881,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
def _items_to_rela_paths(
self,
- items: Union[
- PathLike, Sequence[Union[PathLike, BaseIndexEntry, Blob, Submodule]]
- ],
+ items: Union[PathLike, Sequence[Union[PathLike, BaseIndexEntry, Blob, Submodule]]],
) -> List[PathLike]:
"""Returns a list of repo-relative paths from the given items which
may be absolute or relative paths, entries or blobs"""
@@ -933,7 +906,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
self,
items: Sequence[Union[PathLike, Blob, BaseIndexEntry, "Submodule"]],
working_tree: bool = False,
- **kwargs: Any
+ **kwargs: Any,
) -> List[str]:
"""Remove the given items from the index and optionally from
the working tree as well.
@@ -989,7 +962,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
self,
items: Sequence[Union[PathLike, Blob, BaseIndexEntry, "Submodule"]],
skip_errors: bool = False,
- **kwargs: Any
+ **kwargs: Any,
) -> List[Tuple[str, str]]:
"""Rename/move the items, whereas the last item is considered the destination of
the move operation. If the destination is a file, the first item ( of two )
@@ -1020,9 +993,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
paths = self._items_to_rela_paths(items)
if len(paths) < 2:
- raise ValueError(
- "Please provide at least one source and one destination of the move operation"
- )
+ raise ValueError("Please provide at least one source and one destination of the move operation")
was_dry_run = kwargs.pop("dry_run", kwargs.pop("n", None))
kwargs["dry_run"] = True
@@ -1110,9 +1081,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
def _commit_editmsg_filepath(self) -> str:
return osp.join(self.repo.common_dir, "COMMIT_EDITMSG")
- def _flush_stdin_and_wait(
- cls, proc: "Popen[bytes]", ignore_stdout: bool = False
- ) -> bytes:
+ def _flush_stdin_and_wait(cls, proc: "Popen[bytes]", ignore_stdout: bool = False) -> bytes:
stdin_IO = proc.stdin
if stdin_IO:
stdin_IO.flush()
@@ -1133,7 +1102,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
paths: Union[None, Iterable[PathLike]] = None,
force: bool = False,
fprogress: Callable = lambda *args: None,
- **kwargs: Any
+ **kwargs: Any,
) -> Union[None, Iterator[PathLike], Sequence[PathLike]]:
"""Checkout the given paths or all files from the version known to the index into
the working tree.
@@ -1185,9 +1154,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
failed_reasons = []
unknown_lines = []
- def handle_stderr(
- proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLike]
- ) -> None:
+ def handle_stderr(proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLike]) -> None:
stderr_IO = proc.stderr
if not stderr_IO:
@@ -1204,9 +1171,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
" is unmerged",
)
for line in stderr.splitlines():
- if not line.startswith("git checkout-index: ") and not line.startswith(
- "git-checkout-index: "
- ):
+ if not line.startswith("git checkout-index: ") and not line.startswith("git-checkout-index: "):
is_a_dir = " is a directory"
unlink_issue = "unable to unlink old '"
already_exists_issue = " already exists, no checkout" # created by entry.c:checkout_entry(...)
@@ -1269,9 +1234,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
kwargs["istream"] = subprocess.PIPE
proc = self.repo.git.checkout_index(args, **kwargs)
# FIXME: Reading from GIL!
- make_exc = lambda: GitCommandError(
- ("git-checkout-index",) + tuple(args), 128, proc.stderr.read()
- )
+ make_exc = lambda: GitCommandError(("git-checkout-index",) + tuple(args), 128, proc.stderr.read())
checked_out_files: List[PathLike] = []
for path in paths:
@@ -1288,9 +1251,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
for entry in self.entries.values():
if str(entry.path).startswith(folder):
p = entry.path
- self._write_path_to_stdin(
- proc, p, p, make_exc, fprogress, read_from_stdout=False
- )
+ self._write_path_to_stdin(proc, p, p, make_exc, fprogress, read_from_stdout=False)
checked_out_files.append(p)
path_is_directory = True
# END if entry is in directory
@@ -1298,9 +1259,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# END path exception handlnig
if not path_is_directory:
- self._write_path_to_stdin(
- proc, co_path, path, make_exc, fprogress, read_from_stdout=False
- )
+ self._write_path_to_stdin(proc, co_path, path, make_exc, fprogress, read_from_stdout=False)
checked_out_files.append(co_path)
# END path is a file
# END for each path
@@ -1326,7 +1285,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
working_tree: bool = False,
paths: Union[None, Iterable[PathLike]] = None,
head: bool = False,
- **kwargs: Any
+ **kwargs: Any,
) -> "IndexFile":
"""Reset the index to reflect the tree at the given commit. This will not
adjust our HEAD reference as opposed to HEAD.reset by default.
@@ -1389,9 +1348,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# END handle working tree
if head:
- self.repo.head.set_commit(
- self.repo.commit(commit), logmsg="%s: Updating HEAD" % commit
- )
+ self.repo.head.set_commit(self.repo.commit(commit), logmsg="%s: Updating HEAD" % commit)
# END handle head change
return self
@@ -1399,12 +1356,10 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# @ default_index, breaks typing for some reason, copied into function
def diff(
self, # type: ignore[override]
- other: Union[
- Type["git_diff.Diffable.Index"], "Tree", "Commit", str, None
- ] = git_diff.Diffable.Index,
+ other: Union[Type["git_diff.Diffable.Index"], "Tree", "Commit", str, None] = git_diff.Diffable.Index,
paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None,
create_patch: bool = False,
- **kwargs: Any
+ **kwargs: Any,
) -> git_diff.DiffIndex:
"""Diff this index against the working copy or a Tree or Commit object
@@ -1418,10 +1373,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# only run if we are the default repository index
if self._file_path != self._index_path():
- raise AssertionError(
- "Cannot call %r on indices that do not represent the default git index"
- % self.diff()
- )
+ raise AssertionError("Cannot call %r on indices that do not represent the default git index" % self.diff())
# index against index is always empty
if other is self.Index:
return git_diff.DiffIndex()
@@ -1442,9 +1394,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# if other is not None here, something is wrong
if other is not None:
- raise ValueError(
- "other must be None, Diffable.Index, a Tree or Commit, was %r" % other
- )
+ raise ValueError("other must be None, Diffable.Index, a Tree or Commit, was %r" % other)
# diff against working copy - can be handled by superclass natively
return super(IndexFile, self).diff(other, paths, create_patch, **kwargs)
diff --git a/git/index/fun.py b/git/index/fun.py
index e8dead86..4659ac89 100644
--- a/git/index/fun.py
+++ b/git/index/fun.py
@@ -115,9 +115,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
else:
stdout_list: List[str] = []
stderr_list: List[str] = []
- handle_process_output(
- cmd, stdout_list.append, stderr_list.append, finalize_process
- )
+ handle_process_output(cmd, stdout_list.append, stderr_list.append, finalize_process)
stdout = "".join(stdout_list)
stderr = "".join(stderr_list)
if cmd.returncode != 0:
@@ -134,9 +132,7 @@ def stat_mode_to_index_mode(mode: int) -> int:
return S_IFLNK
if S_ISDIR(mode) or S_IFMT(mode) == S_IFGITLINK: # submodules
return S_IFGITLINK
- return S_IFREG | (
- mode & S_IXUSR and 0o755 or 0o644
- ) # blobs with or without executable bit
+ return S_IFREG | (mode & S_IXUSR and 0o755 or 0o644) # blobs with or without executable bit
def write_cache(
@@ -253,17 +249,13 @@ def read_cache(
beginoffset = tell()
ctime = unpack(">8s", read(8))[0]
mtime = unpack(">8s", read(8))[0]
- (dev, ino, mode, uid, gid, size, sha, flags) = unpack(
- ">LLLLLL20sH", read(20 + 4 * 6 + 2)
- )
+ (dev, ino, mode, uid, gid, size, sha, flags) = unpack(">LLLLLL20sH", read(20 + 4 * 6 + 2))
path_size = flags & CE_NAMEMASK
path = read(path_size).decode(defenc)
real_size = (tell() - beginoffset + 8) & ~7
read((beginoffset + real_size) - tell())
- entry = IndexEntry(
- (mode, sha, flags, path, ctime, mtime, dev, ino, uid, gid, size)
- )
+ entry = IndexEntry((mode, sha, flags, path, ctime, mtime, dev, ino, uid, gid, size))
# entry_key would be the method to use, but we safe the effort
entries[(path, entry.stage)] = entry
count += 1
@@ -276,10 +268,9 @@ def read_cache(
# 4 bytes length of chunk
# repeated 0 - N times
extension_data = stream.read(~0)
- assert len(extension_data) > 19, (
- "Index Footer was not at least a sha on content as it was only %i bytes in size"
- % len(extension_data)
- )
+ assert (
+ len(extension_data) > 19
+ ), "Index Footer was not at least a sha on content as it was only %i bytes in size" % len(extension_data)
content_sha = extension_data[-20:]
@@ -330,9 +321,7 @@ def write_tree_from_cache(
# enter recursion
# ci - 1 as we want to count our current item as well
- sha, _tree_entry_list = write_tree_from_cache(
- entries, odb, slice(ci - 1, xi), rbound + 1
- )
+ sha, _tree_entry_list = write_tree_from_cache(entries, odb, slice(ci - 1, xi), rbound + 1)
tree_items.append((sha, S_IFDIR, base))
# skip ahead
@@ -342,26 +331,18 @@ def write_tree_from_cache(
# finally create the tree
sio = BytesIO()
- tree_to_stream(
- tree_items, sio.write
- ) # writes to stream as bytes, but doesn't change tree_items
+ tree_to_stream(tree_items, sio.write) # writes to stream as bytes, but doesn't change tree_items
sio.seek(0)
istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio))
return (istream.binsha, tree_items)
-def _tree_entry_to_baseindexentry(
- tree_entry: "TreeCacheTup", stage: int
-) -> BaseIndexEntry:
- return BaseIndexEntry(
- (tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2])
- )
+def _tree_entry_to_baseindexentry(tree_entry: "TreeCacheTup", stage: int) -> BaseIndexEntry:
+ return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2]))
-def aggressive_tree_merge(
- odb: "GitCmdObjectDB", tree_shas: Sequence[bytes]
-) -> List[BaseIndexEntry]:
+def aggressive_tree_merge(odb: "GitCmdObjectDB", tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]:
"""
:return: list of BaseIndexEntries representing the aggressive merge of the given
trees. All valid entries are on stage 0, whereas the conflicting ones are left
@@ -394,14 +375,8 @@ def aggressive_tree_merge(
# it exists in all branches, if it was changed in both
# its a conflict, otherwise we take the changed version
# This should be the most common branch, so it comes first
- if (
- base[0] != ours[0]
- and base[0] != theirs[0]
- and ours[0] != theirs[0]
- ) or (
- base[1] != ours[1]
- and base[1] != theirs[1]
- and ours[1] != theirs[1]
+ if (base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or (
+ base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]
):
# changed by both
out.append(_tree_entry_to_baseindexentry(base, 1))
diff --git a/git/index/typ.py b/git/index/typ.py
index cbe26f27..6371953b 100644
--- a/git/index/typ.py
+++ b/git/index/typ.py
@@ -159,9 +159,7 @@ class IndexEntry(BaseIndexEntry):
:param base: Instance of type BaseIndexEntry"""
time = pack(">LL", 0, 0)
- return IndexEntry(
- (base.mode, base.binsha, base.flags, base.path, time, time, 0, 0, 0, 0, 0)
- )
+ return IndexEntry((base.mode, base.binsha, base.flags, base.path, time, time, 0, 0, 0, 0, 0))
@classmethod
def from_blob(cls, blob: Blob, stage: int = 0) -> "IndexEntry":
diff --git a/git/index/util.py b/git/index/util.py
index 7339b147..bfc7fadd 100644
--- a/git/index/util.py
+++ b/git/index/util.py
@@ -69,9 +69,7 @@ def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]:
"""
@wraps(func)
- def post_clear_cache_if_not_raised(
- self: "IndexFile", *args: Any, **kwargs: Any
- ) -> _T:
+ def post_clear_cache_if_not_raised(self: "IndexFile", *args: Any, **kwargs: Any) -> _T:
rval = func(self, *args, **kwargs)
self._delete_entries_cache()
return rval
@@ -90,8 +88,7 @@ def default_index(func: Callable[..., _T]) -> Callable[..., _T]:
def check_default_index(self: "IndexFile", *args: Any, **kwargs: Any) -> _T:
if self._file_path != self._index_path():
raise AssertionError(
- "Cannot call %r on indices that do not represent the default git index"
- % func.__name__
+ "Cannot call %r on indices that do not represent the default git index" % func.__name__
)
return func(self, *args, **kwargs)