Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/git/index/base.py: 39%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
2#
3# This module is part of GitPython and is released under the
4# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
6"""Module containing :class:`IndexFile`, an Index implementation facilitating all kinds
7of index manipulations such as querying and merging."""
9__all__ = ["IndexFile", "CheckoutError", "StageType"]
11import contextlib
12import datetime
13import glob
14from io import BytesIO
15import os
16import os.path as osp
17from stat import S_ISLNK, S_ISREG
18import subprocess
19import sys
20import tempfile
22from gitdb.base import IStream
23from gitdb.db import MemoryDB
25from git.compat import defenc, force_bytes
26from git.cmd import Git
27import git.diff as git_diff
28from git.exc import CheckoutError, GitCommandError, GitError, InvalidGitRepositoryError
29from git.objects import Blob, Commit, Object, Submodule, Tree
30from git.objects.util import Serializable
31from git.util import (
32 Actor,
33 LazyMixin,
34 LockedFD,
35 join_path_native,
36 file_contents_ro,
37 _is_path_rooted,
38 _to_relative_path,
39 _validate_repo_path,
40 to_native_path_linux,
41 unbare_repo,
42 to_bin_sha,
43)
45from .fun import (
46 S_IFGITLINK,
47 aggressive_tree_merge,
48 entry_key,
49 read_cache,
50 run_commit_hook,
51 stat_mode_to_index_mode,
52 write_cache,
53 write_tree_from_cache,
54)
55from .typ import BaseIndexEntry, IndexEntry, StageType
56from .util import TemporaryFileSwap, post_clear_cache, default_index, git_working_dir
58# typing -----------------------------------------------------------------------------
60from typing import (
61 Any,
62 BinaryIO,
63 Callable,
64 cast,
65 Dict,
66 Generator,
67 IO,
68 Iterable,
69 Iterator,
70 List,
71 NoReturn,
72 Sequence,
73 TYPE_CHECKING,
74 Tuple,
75 Union,
76)
78from git.types import Literal, PathLike
80if TYPE_CHECKING:
81 from subprocess import Popen
83 from git.refs.reference import Reference
84 from git.repo import Repo
87Treeish = Union[Tree, Commit, str, bytes]
89# ------------------------------------------------------------------------------------
92@contextlib.contextmanager
93def _named_temporary_file_for_subprocess(directory: PathLike) -> Generator[str, None, None]:
94 """Create a named temporary file git subprocesses can open, deleting it afterward.
96 :param directory:
97 The directory in which the file is created.
99 :return:
100 A context manager object that creates the file and provides its name on entry,
101 and deletes it on exit.
102 """
103 if sys.platform == "win32":
104 fd, name = tempfile.mkstemp(dir=directory)
105 os.close(fd)
106 try:
107 yield name
108 finally:
109 os.remove(name)
110 else:
111 with tempfile.NamedTemporaryFile(dir=directory) as ctx:
112 yield ctx.name
115class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
116 """An Index that can be manipulated using a native implementation in order to save
117 git command function calls wherever possible.
119 This provides custom merging facilities allowing to merge without actually changing
120 your index or your working tree. This way you can perform your own test merges based
121 on the index only without having to deal with the working copy. This is useful in
122 case of partial working trees.
124 Entries:
126 The index contains an entries dict whose keys are tuples of type
127 :class:`~git.index.typ.IndexEntry` to facilitate access.
129 You may read the entries dict or manipulate it using IndexEntry instance, i.e.::
131 index.entries[index.entry_key(index_entry_instance)] = index_entry_instance
133 Make sure you use :meth:`index.write() <write>` once you are done manipulating the
134 index directly before operating on it using the git command.
135 """
137 unsafe_git_checkout_index_options = ["--prefix"]
138 unsafe_git_read_tree_options = ["--index-output"]
140 __slots__ = ("repo", "version", "entries", "_extension_data", "_file_path")
142 _VERSION = 2
143 """The latest version we support."""
145 S_IFGITLINK = S_IFGITLINK
146 """Flags for a submodule."""
148 def __init__(self, repo: "Repo", file_path: Union[PathLike, None] = None) -> None:
149 """Initialize this Index instance, optionally from the given `file_path`.
151 If no `file_path` is given, we will be created from the current index file.
153 If a stream is not given, the stream will be initialized from the current
154 repository's index on demand.
155 """
156 self.repo = repo
157 self.version = self._VERSION
158 self._extension_data = b""
159 self._file_path: PathLike = file_path or self._index_path()
161 def _set_cache_(self, attr: str) -> None:
162 if attr == "entries":
163 try:
164 fd = os.open(self._file_path, os.O_RDONLY)
165 except OSError:
166 # In new repositories, there may be no index, which means we are empty.
167 self.entries: Dict[Tuple[PathLike, StageType], IndexEntry] = {}
168 return
169 # END exception handling
171 try:
172 stream = file_contents_ro(fd, stream=True, allow_mmap=True)
173 finally:
174 os.close(fd)
176 self._deserialize(stream)
177 else:
178 super()._set_cache_(attr)
180 def _index_path(self) -> PathLike:
181 if self.repo.git_dir:
182 return join_path_native(self.repo.git_dir, "index")
183 else:
184 raise GitCommandError("No git directory given to join index path")
186 @property
187 def path(self) -> PathLike:
188 """:return: Path to the index file we are representing"""
189 return self._file_path
191 def _delete_entries_cache(self) -> None:
192 """Safely clear the entries cache so it can be recreated."""
193 try:
194 del self.entries
195 except AttributeError:
196 # It failed in Python 2.6.5 with AttributeError.
197 # FIXME: Look into whether we can just remove this except clause now.
198 pass
199 # END exception handling
201 # { Serializable Interface
203 def _deserialize(self, stream: IO) -> "IndexFile":
204 """Initialize this instance with index values read from the given stream."""
205 self.version, self.entries, self._extension_data, _conten_sha = read_cache(stream)
206 return self
208 def _entries_sorted(self) -> List[IndexEntry]:
209 """:return: List of entries, in a sorted fashion, first by path, then by stage"""
210 return sorted(self.entries.values(), key=lambda e: (e.path, e.stage))
212 def _serialize(self, stream: IO, ignore_extension_data: bool = False) -> "IndexFile":
213 entries = self._entries_sorted()
214 extension_data = self._extension_data # type: Union[None, bytes]
215 if ignore_extension_data:
216 extension_data = None
217 write_cache(entries, stream, extension_data)
218 return self
220 # } END serializable interface
222 def write(
223 self,
224 file_path: Union[None, PathLike] = None,
225 ignore_extension_data: bool = False,
226 ) -> None:
227 """Write the current state to our file path or to the given one.
229 :param file_path:
230 If ``None``, we will write to our stored file path from which we have been
231 initialized. Otherwise we write to the given file path. Please note that
232 this will change the `file_path` of this index to the one you gave.
234 :param ignore_extension_data:
235 If ``True``, the TREE type extension data read in the index will not be
236 written to disk. NOTE that no extension data is actually written. Use this
237 if you have altered the index and would like to use
238 :manpage:`git-write-tree(1)` afterwards to create a tree representing your
239 written changes. If this data is present in the written index,
240 :manpage:`git-write-tree(1)` will instead write the stored/cached tree.
241 Alternatively, use :meth:`write_tree` to handle this case automatically.
242 """
243 # Make sure we have our entries read before getting a write lock.
244 # Otherwise it would be done when streaming.
245 # This can happen if one doesn't change the index, but writes it right away.
246 self.entries # noqa: B018
247 lfd = LockedFD(file_path or self._file_path)
248 stream = lfd.open(write=True, stream=True)
250 try:
251 self._serialize(stream, ignore_extension_data)
252 except BaseException:
253 lfd.rollback()
254 raise
256 lfd.commit()
258 # Make sure we represent what we have written.
259 if file_path is not None:
260 self._file_path = file_path
262 @post_clear_cache
263 @default_index
264 def merge_tree(
265 self,
266 rhs: Treeish,
267 base: Union[None, Treeish] = None,
268 allow_unsafe_options: bool = False,
269 ) -> "IndexFile":
270 """Merge the given `rhs` treeish into the current index, possibly taking
271 a common base treeish into account.
273 As opposed to the :func:`from_tree` method, this allows you to use an already
274 existing tree as the left side of the merge.
276 :param rhs:
277 Treeish reference pointing to the 'other' side of the merge.
279 :param base:
280 Optional treeish reference pointing to the common base of `rhs` and this
281 index which equals lhs.
283 :param allow_unsafe_options:
284 Allow options that may write to arbitrary paths.
286 :return:
287 self (containing the merge and possibly unmerged entries in case of
288 conflicts)
290 :raise git.exc.GitCommandError:
291 If there is a merge conflict. The error will be raised at the first
292 conflicting path. If you want to have proper merge resolution to be done by
293 yourself, you have to commit the changed index (or make a valid tree from
294 it) and retry with a three-way :meth:`index.from_tree <from_tree>` call.
295 """
296 if not allow_unsafe_options:
297 Git.check_unsafe_options(
298 options=Git._option_candidates([base, rhs]),
299 unsafe_options=self.unsafe_git_read_tree_options,
300 )
302 # -i : ignore working tree status
303 # --aggressive : handle more merge cases
304 # -m : do an actual merge
305 args: List[Union[Treeish, str]] = ["--aggressive", "-i", "-m"]
306 if base is not None:
307 args.append(base)
308 args.append(rhs)
310 self.repo.git.read_tree(args)
311 return self
313 @classmethod
314 def new(cls, repo: "Repo", *tree_sha: Union[str, bytes, Tree]) -> "IndexFile":
315 """Merge the given treeish revisions into a new index which is returned.
317 This method behaves like ``git-read-tree --aggressive`` when doing the merge.
319 :param repo:
320 The repository treeish are located in.
322 :param tree_sha:
323 20 byte or 40 byte tree sha or tree objects.
325 :return:
326 New :class:`IndexFile` instance. Its path will be undefined.
327 If you intend to write such a merged Index, supply an alternate
328 ``file_path`` to its :meth:`write` method.
329 """
330 tree_sha_bytes: List[bytes] = [
331 to_bin_sha(t if isinstance(t, bytes) else str(t).encode("ascii")) for t in tree_sha
332 ]
333 base_entries = aggressive_tree_merge(repo.odb, tree_sha_bytes)
335 inst = cls(repo)
336 # Convert to entries dict.
337 entries: Dict[Tuple[PathLike, int], IndexEntry] = dict(
338 zip(
339 ((e.path, e.stage) for e in base_entries),
340 (IndexEntry.from_base(e) for e in base_entries),
341 )
342 )
344 inst.entries = entries
345 return inst
347 @classmethod
348 def from_tree(
349 cls,
350 repo: "Repo",
351 *treeish: Treeish,
352 allow_unsafe_options: bool = False,
353 **kwargs: Any,
354 ) -> "IndexFile":
355 R"""Merge the given treeish revisions into a new index which is returned.
356 The original index will remain unaltered.
358 :param repo:
359 The repository treeish are located in.
361 :param treeish:
362 One, two or three :class:`~git.objects.tree.Tree` objects,
363 :class:`~git.objects.commit.Commit`\s or 40 byte hexshas.
365 The result changes according to the amount of trees:
367 1. If 1 Tree is given, it will just be read into a new index.
368 2. If 2 Trees are given, they will be merged into a new index using a two
369 way merge algorithm. Tree 1 is the 'current' tree, tree 2 is the 'other'
370 one. It behaves like a fast-forward.
371 3. If 3 Trees are given, a 3-way merge will be performed with the first tree
372 being the common ancestor of tree 2 and tree 3. Tree 2 is the 'current'
373 tree, tree 3 is the 'other' one.
375 :param kwargs:
376 Additional arguments passed to :manpage:`git-read-tree(1)`.
378 :param allow_unsafe_options:
379 Allow options that may write to arbitrary paths.
381 :return:
382 New :class:`IndexFile` instance. It will point to a temporary index location
383 which does not exist anymore. If you intend to write such a merged Index,
384 supply an alternate ``file_path`` to its :meth:`write` method.
386 :note:
387 In the three-way merge case, ``--aggressive`` will be specified to
388 automatically resolve more cases in a commonly correct manner. Specify
389 ``trivial=True`` as a keyword argument to override that.
391 As the underlying :manpage:`git-read-tree(1)` command takes into account the
392 current index, it will be temporarily moved out of the way to prevent any
393 unexpected interference.
394 """
395 if len(treeish) == 0 or len(treeish) > 3:
396 raise ValueError("Please specify between 1 and 3 treeish, got %i" % len(treeish))
398 if not allow_unsafe_options:
399 Git.check_unsafe_options(
400 options=Git._option_candidates(treeish, kwargs),
401 unsafe_options=cls.unsafe_git_read_tree_options,
402 )
404 arg_list: List[Union[Treeish, str]] = []
405 # Ignore that the working tree and index possibly are out of date.
406 if len(treeish) > 1:
407 # Drop unmerged entries when reading our index and merging.
408 arg_list.append("--reset")
409 # Handle non-trivial cases the way a real merge does.
410 arg_list.append("--aggressive")
411 # END merge handling
413 # Create the temporary file in the .git directory to be sure renaming
414 # works - /tmp/ directories could be on another device.
415 with _named_temporary_file_for_subprocess(repo.git_dir) as tmp_index:
416 arg_list.append("--index-output=%s" % tmp_index)
417 arg_list.extend(treeish)
419 # Move the current index out of the way - otherwise the merge may fail as it
420 # considers existing entries. Moving it essentially clears the index.
421 # Unfortunately there is no 'soft' way to do it.
422 # The TemporaryFileSwap ensures the original file gets put back.
423 with TemporaryFileSwap(join_path_native(repo.git_dir, "index")):
424 repo.git.read_tree(*arg_list, **kwargs)
425 index = cls(repo, tmp_index)
426 index.entries # noqa: B018 # Force it to read the file as we will delete the temp-file.
427 return index
428 # END index merge handling
430 # UTILITIES
432 @unbare_repo
433 def _iter_expand_paths(self: "IndexFile", paths: Sequence[PathLike]) -> Iterator[PathLike]:
434 """Expand the directories in list of paths to the corresponding paths
435 accordingly.
437 :note:
438 git will add items multiple times even if a glob overlapped with manually
439 specified paths or if paths where specified multiple times - we respect that
440 and do not prune.
441 """
443 def raise_exc(e: Exception) -> NoReturn:
444 raise e
446 r = str(self.repo.working_tree_dir)
447 rs = r + os.sep
448 for path in paths:
449 abs_path = os.fspath(path)
450 if not osp.isabs(abs_path):
451 abs_path = osp.join(r, path)
452 # END make absolute path
454 try:
455 st = os.lstat(abs_path) # Handles non-symlinks as well.
456 except OSError:
457 # The lstat call may fail as the path may contain globs as well.
458 pass
459 else:
460 if S_ISLNK(st.st_mode):
461 yield abs_path.replace(rs, "")
462 continue
463 # END check symlink
465 # If the path is not already pointing to an existing file, resolve globs if possible.
466 if not os.path.exists(abs_path) and ("?" in abs_path or "*" in abs_path or "[" in abs_path):
467 resolved_paths = glob.glob(abs_path)
468 # not abs_path in resolved_paths:
469 # A glob() resolving to the same path we are feeding it with is a
470 # glob() that failed to resolve. If we continued calling ourselves
471 # we'd endlessly recurse. If the condition below evaluates to true
472 # then we are likely dealing with a file whose name contains wildcard
473 # characters.
474 if abs_path not in resolved_paths:
475 for f in self._iter_expand_paths(glob.glob(abs_path)):
476 yield str(f).replace(rs, "")
477 continue
478 # END glob handling
479 try:
480 for root, dirs, files in os.walk(abs_path, onerror=raise_exc):
481 for dirname in dirs[:]:
482 directory = osp.join(root, dirname)
483 try:
484 _validate_repo_path(to_native_path_linux(osp.relpath(directory, r)))
485 except ValueError:
486 dirs.remove(dirname)
487 continue
488 if osp.islink(directory):
489 dirs.remove(dirname)
490 yield osp.relpath(directory, r)
491 for rela_file in files:
492 # Add relative paths only.
493 yield osp.join(root.replace(rs, ""), rela_file)
494 # END for each file in subdir
495 # END for each subdirectory
496 except OSError:
497 # It was a file or something that could not be iterated.
498 yield abs_path.replace(rs, "")
499 # END path exception handling
500 # END for each path
502 def _write_path_to_stdin(
503 self,
504 proc: "Popen",
505 filepath: PathLike,
506 item: PathLike,
507 fmakeexc: Callable[..., GitError],
508 fprogress: Callable[[PathLike, bool, PathLike], None],
509 read_from_stdout: bool = True,
510 ) -> Union[None, str]:
511 """Write path to ``proc.stdin`` and make sure it processes the item, including
512 progress.
514 :return:
515 stdout string
517 :param read_from_stdout:
518 If ``True``, ``proc.stdout`` will be read after the item was sent to stdin.
519 In that case, it will return ``None``.
521 :note:
522 There is a bug in :manpage:`git-update-index(1)` that prevents it from
523 sending reports just in time. This is why we have a version that tries to
524 read stdout and one which doesn't. In fact, the stdout is not important as
525 the piped-in files are processed anyway and just in time.
527 :note:
528 Newlines are essential here, git's behaviour is somewhat inconsistent on
529 this depending on the version, hence we try our best to deal with newlines
530 carefully. Usually the last newline will not be sent, instead we will close
531 stdin to break the pipe.
532 """
533 fprogress(filepath, False, item)
534 rval: Union[None, str] = None
536 if proc.stdin is not None:
537 try:
538 proc.stdin.write(("%s\n" % filepath).encode(defenc))
539 except OSError as e:
540 # Pipe broke, usually because some error happened.
541 raise fmakeexc() from e
542 # END write exception handling
543 proc.stdin.flush()
545 if read_from_stdout and proc.stdout is not None:
546 rval = proc.stdout.readline().strip()
547 fprogress(filepath, True, item)
548 return rval
550 def iter_blobs(
551 self, predicate: Callable[[Tuple[StageType, Blob]], bool] = lambda t: True
552 ) -> Iterator[Tuple[StageType, Blob]]:
553 """
554 :return:
555 Iterator yielding tuples of :class:`~git.objects.blob.Blob` objects and
556 stages, tuple(stage, Blob).
558 :param predicate:
559 Function(t) returning ``True`` if tuple(stage, Blob) should be yielded by
560 the iterator. A default filter, the :class:`~git.index.typ.BlobFilter`, allows you
561 to yield blobs only if they match a given list of paths.
562 """
563 for entry in self.entries.values():
564 blob = entry.to_blob(self.repo)
565 blob.size = entry.size
566 output = (entry.stage, blob)
567 if predicate(output):
568 yield output
569 # END for each entry
571 def unmerged_blobs(self) -> Dict[PathLike, List[Tuple[StageType, Blob]]]:
572 """
573 :return:
574 Dict(path : list(tuple(stage, Blob, ...))), being a dictionary associating a
575 path in the index with a list containing sorted stage/blob pairs.
577 :note:
578 Blobs that have been removed in one side simply do not exist in the given
579 stage. That is, a file removed on the 'other' branch whose entries are at
580 stage 3 will not have a stage 3 entry.
581 """
583 def is_unmerged_blob(t: Tuple[StageType, Blob]) -> bool:
584 return t[0] != 0
586 path_map: Dict[PathLike, List[Tuple[StageType, Blob]]] = {}
587 for stage, blob in self.iter_blobs(is_unmerged_blob):
588 path_map.setdefault(blob.path, []).append((stage, blob))
589 # END for each unmerged blob
590 for line in path_map.values():
591 line.sort()
593 return path_map
595 @classmethod
596 def entry_key(cls, *entry: Union[BaseIndexEntry, PathLike, StageType]) -> Tuple[PathLike, StageType]:
597 return entry_key(*entry)
599 def resolve_blobs(self, iter_blobs: Iterator[Blob]) -> "IndexFile":
600 """Resolve the blobs given in blob iterator.
602 This will effectively remove the index entries of the respective path at all
603 non-null stages and add the given blob as new stage null blob.
605 For each path there may only be one blob, otherwise a :exc:`ValueError` will be
606 raised claiming the path is already at stage 0.
608 :raise ValueError:
609 If one of the blobs already existed at stage 0.
611 :return:
612 self
614 :note:
615 You will have to write the index manually once you are done, i.e.
616 ``index.resolve_blobs(blobs).write()``.
617 """
618 for blob in iter_blobs:
619 stage_null_key = (blob.path, 0)
620 if stage_null_key in self.entries:
621 raise ValueError("Path %r already exists at stage 0" % str(blob.path))
622 # END assert blob is not stage 0 already
624 # Delete all possible stages.
625 for stage in (1, 2, 3):
626 try:
627 del self.entries[(blob.path, stage)]
628 except KeyError:
629 pass
630 # END ignore key errors
631 # END for each possible stage
633 self.entries[stage_null_key] = IndexEntry.from_blob(blob)
634 # END for each blob
636 return self
638 def update(self) -> "IndexFile":
639 """Reread the contents of our index file, discarding all cached information
640 we might have.
642 :note:
643 This is a possibly dangerous operations as it will discard your changes to
644 :attr:`index.entries <entries>`.
646 :return:
647 self
648 """
649 self._delete_entries_cache()
650 # Allows to lazily reread on demand.
651 return self
653 def write_tree(self) -> Tree:
654 """Write this index to a corresponding :class:`~git.objects.tree.Tree` object
655 into the repository's object database and return it.
657 :return:
658 :class:`~git.objects.tree.Tree` object representing this index.
660 :note:
661 The tree will be written even if one or more objects the tree refers to does
662 not yet exist in the object database. This could happen if you added entries
663 to the index directly.
665 :raise ValueError:
666 If there are no entries in the cache.
668 :raise git.exc.UnmergedEntriesError:
669 """
670 # We obtain no lock as we just flush our contents to disk as tree.
671 # If we are a new index, the entries access will load our data accordingly.
672 mdb = MemoryDB()
673 entries = self._entries_sorted()
674 binsha, tree_items = write_tree_from_cache(entries, mdb, slice(0, len(entries)))
676 # Copy changed trees only.
677 mdb.stream_copy(mdb.sha_iter(), self.repo.odb)
679 # Note: Additional deserialization could be saved if write_tree_from_cache would
680 # return sorted tree entries.
681 root_tree = Tree(self.repo, binsha, path="")
682 root_tree._cache = tree_items
683 return root_tree
685 def _process_diff_args(
686 self,
687 args: List[Union[PathLike, "git_diff.Diffable"]],
688 ) -> List[Union[PathLike, "git_diff.Diffable"]]:
689 try:
690 args.pop(args.index(self))
691 except IndexError:
692 pass
693 # END remove self
694 return args
696 def _to_relative_path(self, path: PathLike) -> PathLike:
697 """
698 :return:
699 Version of path relative to our git directory or raise :exc:`ValueError` if
700 it is not within our git directory.
702 :raise ValueError:
703 """
704 if self.repo.bare:
705 drive, _tail = osp.splitdrive(os.fspath(path))
706 if drive or _is_path_rooted(path):
707 raise InvalidGitRepositoryError("paths with a drive or root require a non-bare repository")
708 return path
709 return _to_relative_path(cast(PathLike, self.repo.working_tree_dir), path)
711 def _preprocess_add_items(
712 self, items: Union[PathLike, Sequence[Union[PathLike, Blob, BaseIndexEntry, "Submodule"]]]
713 ) -> Tuple[List[PathLike], List[BaseIndexEntry]]:
714 """Split the items into two lists of path strings and BaseEntries."""
715 paths = []
716 entries = []
717 # if it is a string put in list
718 if isinstance(items, (str, os.PathLike)):
719 items = [items]
721 for item in items:
722 if isinstance(item, (str, os.PathLike)):
723 paths.append(self._to_relative_path(item))
724 elif isinstance(item, (Blob, Submodule)):
725 entries.append(BaseIndexEntry.from_blob(item))
726 elif isinstance(item, BaseIndexEntry):
727 entries.append(item)
728 else:
729 raise TypeError("Invalid Type: %r" % item)
730 # END for each item
731 for entry in entries:
732 _validate_repo_path(entry.path)
733 return paths, entries
735 def _store_path(self, filepath: PathLike, fprogress: Callable) -> BaseIndexEntry:
736 """Store file at filepath in the database and return the base index entry.
738 :note:
739 This needs the :func:`~git.index.util.git_working_dir` decorator active!
740 This must be ensured in the calling code.
741 """
742 filepath = self._to_relative_path(filepath)
743 _validate_repo_path(filepath)
744 parent = osp.realpath(self.repo.working_dir)
745 for component in os.fspath(filepath).split("/")[:-1]:
746 parent = osp.join(parent, component)
747 if osp.islink(parent) or osp.normcase(osp.realpath(parent)) != osp.normcase(osp.abspath(parent)):
748 raise ValueError("Cannot stage a path beyond a symbolic link: %r" % filepath)
749 st = os.lstat(filepath)
750 if not S_ISLNK(st.st_mode) and not S_ISREG(st.st_mode):
751 raise ValueError("Can only stage a regular file or symbolic link: %r" % filepath)
753 stream_size = st.st_size
754 if S_ISLNK(st.st_mode):
755 # readlink is a string, but we need bytes.
756 target = force_bytes(os.readlink(filepath), encoding=defenc)
757 stream_size = len(target)
759 def open_stream() -> BinaryIO:
760 return BytesIO(target)
761 else:
763 def open_stream() -> BinaryIO:
764 # Do not follow a final symlink or block on a FIFO substituted
765 # between lstat and open on platforms supporting these flags.
766 def opener(path: str, flags: int) -> int:
767 return os.open(path, flags | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0))
769 return open(filepath, "rb", opener=opener)
771 with open_stream() as stream:
772 if not S_ISLNK(st.st_mode):
773 st = os.fstat(stream.fileno())
774 if not S_ISREG(st.st_mode):
775 raise ValueError("Can only stage a regular file: %r" % filepath)
776 stream_size = st.st_size
777 fprogress(filepath, False, filepath)
778 istream = self.repo.odb.store(IStream(Blob.type, stream_size, stream))
779 fprogress(filepath, True, filepath)
780 return BaseIndexEntry(
781 (
782 stat_mode_to_index_mode(st.st_mode),
783 istream.binsha,
784 0,
785 to_native_path_linux(filepath),
786 )
787 )
789 @unbare_repo
790 @git_working_dir
791 def _entries_for_paths(
792 self,
793 paths: List[str],
794 path_rewriter: Union[Callable, None],
795 fprogress: Callable,
796 entries: List[BaseIndexEntry],
797 ) -> List[BaseIndexEntry]:
798 entries_added: List[BaseIndexEntry] = []
799 if path_rewriter:
800 working_tree_dir = self.repo.working_tree_dir
801 if working_tree_dir is None:
802 raise InvalidGitRepositoryError("Cannot rewrite paths without a working tree")
803 working_tree_dir = str(working_tree_dir)
804 for path in paths:
805 if osp.isabs(path):
806 abspath = path
807 gitrelative_path = path[len(working_tree_dir) + 1 :]
808 else:
809 gitrelative_path = path
810 abspath = osp.join(working_tree_dir, gitrelative_path)
811 # END obtain relative and absolute paths
813 blob = Blob(
814 self.repo,
815 Blob.NULL_BIN_SHA,
816 stat_mode_to_index_mode(os.lstat(abspath).st_mode),
817 to_native_path_linux(gitrelative_path),
818 )
819 # TODO: variable undefined
820 entries.append(BaseIndexEntry.from_blob(blob))
821 # END for each path
822 del paths[:]
823 # END rewrite paths
825 # HANDLE PATHS
826 assert len(entries_added) == 0
827 for filepath in self._iter_expand_paths(paths):
828 entries_added.append(self._store_path(filepath, fprogress))
829 # END for each filepath
830 # END path handling
831 return entries_added
833 def add(
834 self,
835 items: Union[PathLike, Sequence[Union[PathLike, Blob, BaseIndexEntry, "Submodule"]]],
836 force: bool = True,
837 fprogress: Callable = lambda *args: None,
838 path_rewriter: Union[Callable[..., PathLike], None] = None,
839 write: bool = True,
840 write_extension_data: bool = False,
841 ) -> List[BaseIndexEntry]:
842 R"""Add files from the working tree, specific blobs, or
843 :class:`~git.index.typ.BaseIndexEntry`\s to the index.
845 :param items:
846 Multiple types of items are supported, types can be mixed within one call.
847 Different types imply a different handling. File paths may generally be
848 relative or absolute.
850 - path string
852 Strings denote a relative or absolute path into the repository pointing
853 to an existing file, e.g., ``CHANGES``, ``lib/myfile.ext``,
854 ``/home/gitrepo/lib/myfile.ext``.
856 Absolute paths must start with working tree directory of this index's
857 repository to be considered valid. For example, if it was initialized
858 with a non-normalized path, like ``/root/repo/../repo``, absolute paths
859 to be added must start with ``/root/repo/../repo``.
861 Paths provided like this must exist. When added, they will be written
862 into the object database.
864 PathStrings may contain globs, such as ``lib/__init__*``. Or they can be
865 directories like ``lib``, which will add all the files within the
866 directory and subdirectories.
868 This equals a straight :manpage:`git-add(1)`.
870 They are added at stage 0.
872 - :class:`~git.objects.blob.Blob` or
873 :class:`~git.objects.submodule.base.Submodule` object
875 Blobs are added as they are assuming a valid mode is set.
877 The file they refer to may or may not exist in the file system, but must
878 be a path relative to our repository.
880 If their sha is null (40*0), their path must exist in the file system
881 relative to the git repository as an object will be created from the
882 data at the path.
884 The handling now very much equals the way string paths are processed,
885 except that the mode you have set will be kept. This allows you to
886 create symlinks by settings the mode respectively and writing the target
887 of the symlink directly into the file. This equals a default Linux
888 symlink which is not dereferenced automatically, except that it can be
889 created on filesystems not supporting it as well.
891 Please note that globs or directories are not allowed in
892 :class:`~git.objects.blob.Blob` objects.
894 They are added at stage 0.
896 - :class:`~git.index.typ.BaseIndexEntry` or type
898 Handling equals the one of :class:`~git.objects.blob.Blob` objects, but
899 the stage may be explicitly set. Please note that Index Entries require
900 binary sha's.
902 :param force:
903 **CURRENTLY INEFFECTIVE**
904 If ``True``, otherwise ignored or excluded files will be added anyway. As
905 opposed to the :manpage:`git-add(1)` command, we enable this flag by default
906 as the API user usually wants the item to be added even though they might be
907 excluded.
909 :param fprogress:
910 Function with signature ``f(path, done=False, item=item)`` called for each
911 path to be added, one time once it is about to be added where ``done=False``
912 and once after it was added where ``done=True``.
914 ``item`` is set to the actual item we handle, either a path or a
915 :class:`~git.index.typ.BaseIndexEntry`.
917 Please note that the processed path is not guaranteed to be present in the
918 index already as the index is currently being processed.
920 :param path_rewriter:
921 Function, with signature ``(string) func(BaseIndexEntry)``, returning a path
922 for each passed entry which is the path to be actually recorded for the
923 object created from :attr:`entry.path <git.index.typ.BaseIndexEntry.path>`.
924 This allows you to write an index which is not identical to the layout of
925 the actual files on your hard-disk. If not ``None`` and `items` contain
926 plain paths, these paths will be converted to Entries beforehand and passed
927 to the path_rewriter. Please note that ``entry.path`` is relative to the git
928 repository.
930 :param write:
931 If ``True``, the index will be written once it was altered. Otherwise the
932 changes only exist in memory and are not available to git commands.
934 :param write_extension_data:
935 If ``True``, extension data will be written back to the index. This can lead
936 to issues in case it is containing the 'TREE' extension, which will cause
937 the :manpage:`git-commit(1)` command to write an old tree, instead of a new
938 one representing the now changed index.
940 This doesn't matter if you use :meth:`IndexFile.commit`, which ignores the
941 'TREE' extension altogether. You should set it to ``True`` if you intend to
942 use :meth:`IndexFile.commit` exclusively while maintaining support for
943 third-party extensions. Besides that, you can usually safely ignore the
944 built-in extensions when using GitPython on repositories that are not
945 handled manually at all.
947 All current built-in extensions are listed here:
948 https://git-scm.com/docs/index-format
950 :return:
951 List of :class:`~git.index.typ.BaseIndexEntry`\s representing the entries
952 just actually added.
954 :raise OSError:
955 If a supplied path did not exist. Please note that
956 :class:`~git.index.typ.BaseIndexEntry` objects that do not have a null sha
957 will be added even if their paths do not exist.
958 """
959 # Sort the entries into strings and Entries.
960 # Blobs are converted to entries automatically.
961 # Paths can be git-added. For everything else we use git-update-index.
962 paths, entries = self._preprocess_add_items(items)
963 entries_added: List[BaseIndexEntry] = []
964 # This code needs a working tree, so we try not to run it unless required.
965 # That way, we are OK on a bare repository as well.
966 # If there are no paths, the rewriter has nothing to do either.
967 if paths:
968 entries_added.extend(self._entries_for_paths(paths, path_rewriter, fprogress, entries))
970 # HANDLE ENTRIES
971 if entries:
972 null_mode_entries = [e for e in entries if e.mode == 0]
973 if null_mode_entries:
974 raise ValueError(
975 "At least one Entry has a null-mode - please use index.remove to remove files for clarity"
976 )
977 # END null mode should be remove
979 # HANDLE ENTRY OBJECT CREATION
980 # Create objects if required, otherwise go with the existing shas.
981 null_entries_indices = [i for i, e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA]
982 if null_entries_indices:
984 @git_working_dir
985 def handle_null_entries(self: "IndexFile") -> None:
986 for ei in null_entries_indices:
987 null_entry = entries[ei]
988 new_entry = self._store_path(null_entry.path, fprogress)
990 # Update null entry.
991 entries[ei] = BaseIndexEntry(
992 (
993 null_entry.mode,
994 new_entry.binsha,
995 null_entry.stage,
996 null_entry.path,
997 )
998 )
999 # END for each entry index
1001 # END closure
1003 handle_null_entries(self)
1004 # END null_entry handling
1006 # REWRITE PATHS
1007 # If we have to rewrite the entries, do so now, after we have generated all
1008 # object sha's.
1009 if path_rewriter:
1010 for i, e in enumerate(entries):
1011 entries[i] = BaseIndexEntry((e.mode, e.binsha, e.stage, path_rewriter(e)))
1012 # END for each entry
1013 # END handle path rewriting
1015 # Just go through the remaining entries and provide progress info.
1016 for i, entry in enumerate(entries):
1017 progress_sent = i in null_entries_indices
1018 if not progress_sent:
1019 fprogress(entry.path, False, entry)
1020 fprogress(entry.path, True, entry)
1021 # END handle progress
1022 # END for each entry
1023 entries_added.extend(entries)
1024 # END if there are base entries
1026 # FINALIZE
1027 # Add the new entries to this instance.
1028 for entry in entries_added:
1029 _validate_repo_path(entry.path)
1030 for entry in entries_added:
1031 self.entries[(entry.path, 0)] = IndexEntry.from_base(entry)
1033 if write:
1034 self.write(ignore_extension_data=not write_extension_data)
1035 # END handle write
1037 return entries_added
1039 def _items_to_rela_paths(
1040 self,
1041 items: Union[PathLike, Sequence[Union[PathLike, BaseIndexEntry, Blob, Submodule]]],
1042 ) -> List[PathLike]:
1043 """Returns a list of repo-relative paths from the given items which
1044 may be absolute or relative paths, entries or blobs."""
1045 paths = []
1046 # If string, put in list.
1047 if isinstance(items, (str, os.PathLike)):
1048 items = [items]
1050 for item in items:
1051 if isinstance(item, (BaseIndexEntry, (Blob, Submodule))):
1052 paths.append(self._to_relative_path(item.path))
1053 elif isinstance(item, (str, os.PathLike)):
1054 paths.append(self._to_relative_path(item))
1055 else:
1056 raise TypeError("Invalid item type: %r" % item)
1057 # END for each item
1058 return paths
1060 @post_clear_cache
1061 @default_index
1062 def remove(
1063 self,
1064 items: Union[PathLike, Sequence[Union[PathLike, Blob, BaseIndexEntry, "Submodule"]]],
1065 working_tree: bool = False,
1066 allow_unsafe_options: bool = False,
1067 **kwargs: Any,
1068 ) -> List[str]:
1069 R"""Remove the given items from the index and optionally from the working tree
1070 as well.
1072 :param items:
1073 Multiple types of items are supported which may be be freely mixed.
1075 - path string
1077 Remove the given path at all stages. If it is a directory, you must
1078 specify the ``r=True`` keyword argument to remove all file entries below
1079 it. If absolute paths are given, they will be converted to a path
1080 relative to the git repository directory containing the working tree
1082 The path string may include globs, such as ``*.c``.
1084 - :class:`~git.objects.blob.Blob` object
1086 Only the path portion is used in this case.
1088 - :class:`~git.index.typ.BaseIndexEntry` or compatible type
1090 The only relevant information here is the path. The stage is ignored.
1092 :param working_tree:
1093 If ``True``, the entry will also be removed from the working tree,
1094 physically removing the respective file. This may fail if there are
1095 uncommitted changes in it.
1097 :param allow_unsafe_options:
1098 Allow unsafe options such as ``--pathspec-from-file`` to be passed to
1099 :manpage:`git-rm(1)`.
1101 :param kwargs:
1102 Additional keyword arguments to be passed to :manpage:`git-rm(1)`, such as
1103 ``r`` to allow recursive removal.
1105 :return:
1106 List(path_string, ...) list of repository relative paths that have been
1107 removed effectively.
1109 This is interesting to know in case you have provided a directory or globs.
1110 Paths are relative to the repository.
1111 """
1112 if not allow_unsafe_options:
1113 Git.check_unsafe_options(
1114 options=Git._option_candidates([], kwargs),
1115 unsafe_options=Git.unsafe_git_pathspec_from_file_options,
1116 )
1117 args = []
1118 if not working_tree:
1119 args.append("--cached")
1120 args.append("--")
1122 # Preprocess paths.
1123 paths = list(map(os.fspath, self._items_to_rela_paths(items))) # type: ignore[arg-type]
1124 removed_paths = self.repo.git.rm(args, paths, **kwargs).splitlines()
1126 # Process output to gain proper paths.
1127 # rm 'path'
1128 return [p[4:-1] for p in removed_paths]
1130 @post_clear_cache
1131 @default_index
1132 def move(
1133 self,
1134 items: Union[PathLike, Sequence[Union[PathLike, Blob, BaseIndexEntry, "Submodule"]]],
1135 skip_errors: bool = False,
1136 allow_unsafe_options: bool = False,
1137 **kwargs: Any,
1138 ) -> List[Tuple[str, str]]:
1139 """Rename/move the items, whereas the last item is considered the destination of
1140 the move operation.
1142 If the destination is a file, the first item (of two) must be a file as well.
1144 If the destination is a directory, it may be preceded by one or more directories
1145 or files.
1147 The working tree will be affected in non-bare repositories.
1149 :param items:
1150 Multiple types of items are supported, please see the :meth:`remove` method
1151 for reference.
1153 :param skip_errors:
1154 If ``True``, errors such as ones resulting from missing source files will be
1155 skipped.
1157 :param allow_unsafe_options:
1158 Allow unsafe options such as ``--pathspec-from-file`` to be passed to
1159 :manpage:`git-mv(1)`.
1161 :param kwargs:
1162 Additional arguments you would like to pass to :manpage:`git-mv(1)`, such as
1163 ``dry_run`` or ``force``.
1165 :return:
1166 List(tuple(source_path_string, destination_path_string), ...)
1168 A list of pairs, containing the source file moved as well as its actual
1169 destination. Relative to the repository root.
1171 :raise ValueError:
1172 If only one item was given.
1174 :raise git.exc.GitCommandError:
1175 If git could not handle your request.
1176 """
1177 if not allow_unsafe_options:
1178 Git.check_unsafe_options(
1179 options=Git._option_candidates([], kwargs),
1180 unsafe_options=Git.unsafe_git_pathspec_from_file_options,
1181 )
1182 args = []
1183 if skip_errors:
1184 args.append("-k")
1185 args.append("--")
1187 paths = self._items_to_rela_paths(items)
1188 if len(paths) < 2:
1189 raise ValueError("Please provide at least one source and one destination of the move operation")
1191 was_dry_run = kwargs.pop("dry_run", kwargs.pop("n", None))
1192 kwargs["dry_run"] = True
1194 # First execute rename in dry run so the command tells us what it actually does
1195 # (for later output).
1196 out = []
1197 mvlines = self.repo.git.mv(args, paths, **kwargs).splitlines()
1199 # Parse result - first 0:n/2 lines are 'checking ', the remaining ones are the
1200 # 'renaming' ones which we parse.
1201 for ln in range(int(len(mvlines) / 2), len(mvlines)):
1202 tokens = mvlines[ln].split(" to ")
1203 assert len(tokens) == 2, "Too many tokens in %s" % mvlines[ln]
1205 # [0] = Renaming x
1206 # [1] = y
1207 out.append((tokens[0][9:], tokens[1]))
1208 # END for each line to parse
1210 # Either prepare for the real run, or output the dry-run result.
1211 if was_dry_run:
1212 return out
1213 # END handle dry run
1215 # Now apply the actual operation.
1216 kwargs.pop("dry_run")
1217 self.repo.git.mv(args, paths, **kwargs)
1219 return out
1221 def commit(
1222 self,
1223 message: str,
1224 parent_commits: Union[List[Commit], None] = None,
1225 head: bool = True,
1226 author: Union[None, Actor] = None,
1227 committer: Union[None, Actor] = None,
1228 author_date: Union[datetime.datetime, str, None] = None,
1229 commit_date: Union[datetime.datetime, str, None] = None,
1230 skip_hooks: bool = False,
1231 trailers: Union[None, "Dict[str, str]", "List[Tuple[str, str]]"] = None,
1232 ) -> Commit:
1233 """Commit the current default index file, creating a
1234 :class:`~git.objects.commit.Commit` object.
1236 For more information on the arguments, see
1237 :meth:`Commit.create_from_tree <git.objects.commit.Commit.create_from_tree>`.
1239 :note:
1240 If you have manually altered the :attr:`entries` member of this instance,
1241 don't forget to :meth:`write` your changes to disk beforehand.
1243 :note:
1244 Passing ``skip_hooks=True`` is the equivalent of using ``-n`` or
1245 ``--no-verify`` on the command line.
1247 :return:
1248 :class:`~git.objects.commit.Commit` object representing the new commit
1249 """
1250 if not skip_hooks:
1251 run_commit_hook("pre-commit", self)
1253 self._write_commit_editmsg(message)
1254 run_commit_hook("commit-msg", self, self._commit_editmsg_filepath())
1255 message = self._read_commit_editmsg()
1256 self._remove_commit_editmsg()
1257 tree = self.write_tree()
1258 rval = Commit.create_from_tree(
1259 self.repo,
1260 tree,
1261 message,
1262 parent_commits,
1263 head,
1264 author=author,
1265 committer=committer,
1266 author_date=author_date,
1267 commit_date=commit_date,
1268 trailers=trailers,
1269 )
1270 if not skip_hooks:
1271 run_commit_hook("post-commit", self)
1272 return rval
1274 def _write_commit_editmsg(self, message: str) -> None:
1275 with open(self._commit_editmsg_filepath(), "wb") as commit_editmsg_file:
1276 commit_editmsg_file.write(message.encode(defenc))
1278 def _remove_commit_editmsg(self) -> None:
1279 os.remove(self._commit_editmsg_filepath())
1281 def _read_commit_editmsg(self) -> str:
1282 with open(self._commit_editmsg_filepath(), "rb") as commit_editmsg_file:
1283 return commit_editmsg_file.read().decode(defenc)
1285 def _commit_editmsg_filepath(self) -> str:
1286 return osp.join(self.repo.common_dir, "COMMIT_EDITMSG")
1288 def _flush_stdin_and_wait(self, proc: "Popen[bytes]", ignore_stdout: bool = False) -> bytes:
1289 stdin_IO = proc.stdin
1290 if stdin_IO:
1291 stdin_IO.flush()
1292 stdin_IO.close()
1294 stdout = b""
1295 if not ignore_stdout and proc.stdout:
1296 stdout = proc.stdout.read()
1298 if proc.stdout:
1299 proc.stdout.close()
1300 proc.wait()
1301 return stdout
1303 @default_index
1304 def checkout(
1305 self,
1306 paths: Union[None, PathLike, Iterable[PathLike]] = None,
1307 force: bool = False,
1308 fprogress: Callable = lambda *args: None,
1309 allow_unsafe_options: bool = False,
1310 **kwargs: Any,
1311 ) -> Union[None, Iterator[PathLike], Sequence[PathLike]]:
1312 """Check out the given paths or all files from the version known to the index
1313 into the working tree.
1315 :note:
1316 Be sure you have written pending changes using the :meth:`write` method in
1317 case you have altered the entries dictionary directly.
1319 :param paths:
1320 If ``None``, all paths in the index will be checked out.
1321 Otherwise an iterable of relative or absolute paths or a single path
1322 pointing to files or directories in the index is expected.
1324 :param force:
1325 If ``True``, existing files will be overwritten even if they contain local
1326 modifications.
1327 If ``False``, these will trigger a :exc:`~git.exc.CheckoutError`.
1329 :param fprogress:
1330 See :meth:`IndexFile.add` for signature and explanation.
1332 The provided progress information will contain ``None`` as path and item if
1333 no explicit paths are given. Otherwise progress information will be send
1334 prior and after a file has been checked out.
1336 :param allow_unsafe_options:
1337 Allow unsafe options, such as ``--prefix``.
1339 :param kwargs:
1340 Additional arguments to be passed to :manpage:`git-checkout-index(1)`.
1342 :return:
1343 Iterable yielding paths to files which have been checked out and are
1344 guaranteed to match the version stored in the index.
1346 :raise git.exc.CheckoutError:
1347 * If at least one file failed to be checked out. This is a summary, hence it
1348 will checkout as many files as it can anyway.
1349 * If one of files or directories do not exist in the index (as opposed to
1350 the original git command, which ignores them).
1352 :raise git.exc.GitCommandError:
1353 If error lines could not be parsed - this truly is an exceptional state.
1355 :note:
1356 The checkout is limited to checking out the files in the index. Files which
1357 are not in the index anymore and exist in the working tree will not be
1358 deleted. This behaviour is fundamentally different to ``head.checkout``,
1359 i.e. if you want :manpage:`git-checkout(1)`-like behaviour, use
1360 ``head.checkout`` instead of ``index.checkout``.
1361 """
1362 if not allow_unsafe_options:
1363 Git.check_unsafe_options(
1364 options=Git._option_candidates([], kwargs),
1365 unsafe_options=self.unsafe_git_checkout_index_options,
1366 )
1368 args = ["--index"]
1369 if force:
1370 args.append("--force")
1372 failed_files = []
1373 failed_reasons = []
1374 unknown_lines = []
1376 def handle_stderr(proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLike]) -> None:
1377 stderr_IO = proc.stderr
1378 if not stderr_IO:
1379 return # Return early if stderr empty.
1381 stderr_bytes = stderr_IO.read()
1382 # line contents:
1383 stderr = stderr_bytes.decode(defenc)
1384 # git-checkout-index: this already exists
1385 endings = (
1386 " already exists",
1387 " is not in the cache",
1388 " does not exist at stage",
1389 " is unmerged",
1390 )
1391 for line in stderr.splitlines():
1392 if not line.startswith("git checkout-index: ") and not line.startswith("git-checkout-index: "):
1393 is_a_dir = " is a directory"
1394 unlink_issue = "unable to unlink old '"
1395 already_exists_issue = " already exists, no checkout" # created by entry.c:checkout_entry(...)
1396 if line.endswith(is_a_dir):
1397 failed_files.append(line[: -len(is_a_dir)])
1398 failed_reasons.append(is_a_dir)
1399 elif line.startswith(unlink_issue):
1400 failed_files.append(line[len(unlink_issue) : line.rfind("'")])
1401 failed_reasons.append(unlink_issue)
1402 elif line.endswith(already_exists_issue):
1403 failed_files.append(line[: -len(already_exists_issue)])
1404 failed_reasons.append(already_exists_issue)
1405 else:
1406 unknown_lines.append(line)
1407 continue
1408 # END special lines parsing
1410 for e in endings:
1411 if line.endswith(e):
1412 failed_files.append(line[20 : -len(e)])
1413 failed_reasons.append(e)
1414 break
1415 # END if ending matches
1416 # END for each possible ending
1417 # END for each line
1418 if unknown_lines:
1419 raise GitCommandError(("git-checkout-index",), 128, stderr)
1420 if failed_files:
1421 valid_files = list(set(iter_checked_out_files) - set(failed_files))
1422 raise CheckoutError(
1423 "Some files could not be checked out from the index due to local modifications",
1424 failed_files,
1425 valid_files,
1426 failed_reasons,
1427 )
1429 # END stderr handler
1431 # Read and validate the index before Git trusts its paths for checkout.
1432 self._delete_entries_cache()
1433 self.entries # noqa: B018
1434 if paths is None:
1435 args.append("--all")
1436 kwargs["as_process"] = 1
1437 fprogress(None, False, None)
1438 proc = self.repo.git.checkout_index(*args, **kwargs)
1439 proc.wait()
1440 fprogress(None, True, None)
1441 rval_iter = (e.path for e in self.entries.values())
1442 handle_stderr(proc, rval_iter)
1443 return rval_iter
1444 else:
1445 if isinstance(paths, (str, os.PathLike)):
1446 paths = [paths]
1448 # Make sure we have our entries loaded before we start checkout_index, which
1449 # will hold a lock on it. We try to get the lock as well during our entries
1450 # initialization.
1451 self.entries # noqa: B018
1453 args.append("--stdin")
1454 kwargs["as_process"] = True
1455 kwargs["istream"] = subprocess.PIPE
1456 proc = self.repo.git.checkout_index(args, **kwargs)
1458 # FIXME: Reading from GIL!
1459 def make_exc() -> GitCommandError:
1460 return GitCommandError(("git-checkout-index", *args), 128, proc.stderr.read())
1462 checked_out_files: List[PathLike] = []
1464 for path in paths:
1465 co_path = to_native_path_linux(self._to_relative_path(path))
1466 # If the item is not in the index, it could be a directory.
1467 path_is_directory = False
1469 try:
1470 self.entries[(co_path, 0)]
1471 except KeyError:
1472 folder = co_path
1473 if not folder.endswith("/"):
1474 folder += "/"
1475 for entry in self.entries.values():
1476 if os.fspath(entry.path).startswith(folder):
1477 p = entry.path
1478 self._write_path_to_stdin(proc, p, p, make_exc, fprogress, read_from_stdout=False)
1479 checked_out_files.append(p)
1480 path_is_directory = True
1481 # END if entry is in directory
1482 # END for each entry
1483 # END path exception handlnig
1485 if not path_is_directory:
1486 self._write_path_to_stdin(proc, co_path, path, make_exc, fprogress, read_from_stdout=False)
1487 checked_out_files.append(co_path)
1488 # END path is a file
1489 # END for each path
1490 try:
1491 self._flush_stdin_and_wait(proc, ignore_stdout=True)
1492 except GitCommandError:
1493 # Without parsing stdout we don't know what failed.
1494 raise CheckoutError( # noqa: B904
1495 "Some files could not be checked out from the index, probably because they didn't exist.",
1496 failed_files,
1497 [],
1498 failed_reasons,
1499 )
1501 handle_stderr(proc, checked_out_files)
1502 return checked_out_files
1503 # END paths handling
1505 @default_index
1506 def reset(
1507 self,
1508 commit: Union[Commit, "Reference", str] = "HEAD",
1509 working_tree: bool = False,
1510 paths: Union[None, Iterable[PathLike]] = None,
1511 head: bool = False,
1512 allow_unsafe_options: bool = False,
1513 **kwargs: Any,
1514 ) -> "IndexFile":
1515 """Reset the index to reflect the tree at the given commit. This will not adjust
1516 our HEAD reference by default, as opposed to
1517 :meth:`HEAD.reset <git.refs.head.HEAD.reset>`.
1519 :param commit:
1520 Revision, :class:`~git.refs.reference.Reference` or
1521 :class:`~git.objects.commit.Commit` specifying the commit we should
1522 represent.
1524 If you want to specify a tree only, use :meth:`IndexFile.from_tree` and
1525 overwrite the default index.
1527 :param working_tree:
1528 If ``True``, the files in the working tree will reflect the changed index.
1529 If ``False``, the working tree will not be touched.
1530 Please note that changes to the working copy will be discarded without
1531 warning!
1533 :param head:
1534 If ``True``, the head will be set to the given commit. This is ``False`` by
1535 default, but if ``True``, this method behaves like
1536 :meth:`HEAD.reset <git.refs.head.HEAD.reset>`.
1538 :param paths:
1539 If given as an iterable of absolute or repository-relative paths, only these
1540 will be reset to their state at the given commit-ish.
1541 The paths need to exist at the commit, otherwise an exception will be
1542 raised.
1544 :param allow_unsafe_options:
1545 Allow options that may write to arbitrary paths.
1547 :param kwargs:
1548 Additional keyword arguments passed to :manpage:`git-reset(1)`.
1550 :note:
1551 :meth:`IndexFile.reset`, as opposed to
1552 :meth:`HEAD.reset <git.refs.head.HEAD.reset>`, will not delete any files in
1553 order to maintain a consistent working tree. Instead, it will just check out
1554 the files according to their state in the index.
1555 If you want :manpage:`git-reset(1)`-like behaviour, use
1556 :meth:`HEAD.reset <git.refs.head.HEAD.reset>` instead.
1558 :return:
1559 self
1560 """
1561 # What we actually want to do is to merge the tree into our existing index,
1562 # which is what git-read-tree does.
1563 new_inst = type(self).from_tree(self.repo, commit, allow_unsafe_options=allow_unsafe_options)
1564 if not paths:
1565 self.entries = new_inst.entries
1566 else:
1567 nie = new_inst.entries
1568 for path in paths:
1569 path = self._to_relative_path(path)
1570 key = entry_key(path, 0)
1571 try:
1572 self.entries[key] = nie[key]
1573 except KeyError:
1574 # If key is not in theirs, it mustn't be in ours.
1575 try:
1576 del self.entries[key]
1577 except KeyError:
1578 pass
1579 # END handle deletion keyerror
1580 # END handle keyerror
1581 # END for each path
1582 # END handle paths
1583 self.write()
1585 if working_tree:
1586 self.checkout(paths=paths, force=True)
1587 # END handle working tree
1589 if head:
1590 self.repo.head.set_commit(self.repo.commit(commit), logmsg="%s: Updating HEAD" % commit)
1591 # END handle head change
1593 return self
1595 def diff(
1596 self,
1597 other: Union[
1598 Literal[git_diff.DiffConstants.INDEX],
1599 Literal[git_diff.DiffConstants.NULL_TREE],
1600 "Tree",
1601 "Commit",
1602 str,
1603 None,
1604 ] = git_diff.INDEX,
1605 paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None,
1606 create_patch: bool = False,
1607 allow_unsafe_options: bool = False,
1608 **kwargs: Any,
1609 ) -> git_diff.DiffIndex[git_diff.Diff]:
1610 """Diff this index against the working copy or a :class:`~git.objects.tree.Tree`
1611 or :class:`~git.objects.commit.Commit` object.
1613 For documentation of the parameters and return values, see
1614 :meth:`Diffable.diff <git.diff.Diffable.diff>`.
1616 :note:
1617 Will only work with indices that represent the default git index as they
1618 have not been initialized with a stream.
1619 """
1620 if not allow_unsafe_options:
1621 Git.check_unsafe_options(
1622 options=Git._option_candidates([other], kwargs),
1623 unsafe_options=self.repo.unsafe_git_diff_options,
1624 clusterable_short_options="46abceflmnpqrstuvwzBCDMNRW",
1625 )
1627 # Only run if we are the default repository index.
1628 if self._file_path != self._index_path():
1629 raise AssertionError("Cannot call %r on indices that do not represent the default git index" % self.diff())
1630 # Index against index is always empty.
1631 if other is self.INDEX:
1632 return git_diff.DiffIndex()
1634 if other == git_diff.NULL_TREE or other == git_diff.NULL_TREE_SHA:
1635 args: List[Union[PathLike, str]] = [
1636 "--cached",
1637 git_diff.NULL_TREE_SHA,
1638 "--abbrev=40",
1639 "--full-index",
1640 ]
1642 if not any(x in kwargs for x in ("find_renames", "no_renames", "M")):
1643 args.append("-M")
1645 if create_patch:
1646 args.append("-p")
1647 args.append("--no-ext-diff")
1648 else:
1649 args.append("--raw")
1650 args.append("-z")
1652 args.append("--no-color")
1654 if paths is not None and not isinstance(paths, (tuple, list)):
1655 paths = [paths]
1657 if paths:
1658 args.append("--")
1659 args.extend(paths)
1661 kwargs["as_process"] = True
1662 if create_patch:
1663 self.repo.git(c="diff.mnemonicPrefix=false")
1664 proc = self.repo.git.diff(*args, **kwargs)
1666 diff_method = (
1667 git_diff.Diff._index_from_patch_format if create_patch else git_diff.Diff._index_from_raw_format
1668 )
1669 index = diff_method(self.repo, proc)
1671 proc.wait()
1672 return index
1674 # Index against anything but None is a reverse diff with the respective item.
1675 # Handle existing -R flags properly.
1676 # Transform strings to the object so that we can call diff on it.
1677 if isinstance(other, str):
1678 other = self.repo.rev_parse(other)
1679 # END object conversion
1681 if isinstance(other, Object): # For Tree or Commit.
1682 # Invert the existing R flag.
1683 cur_val = kwargs.get("R", False)
1684 kwargs["R"] = not cur_val
1685 return other.diff(
1686 self.INDEX,
1687 paths,
1688 create_patch,
1689 allow_unsafe_options=allow_unsafe_options,
1690 **kwargs,
1691 )
1692 # END diff against other item handling
1694 # If other is not None here, something is wrong.
1695 if other is not None:
1696 raise ValueError("other must be None, Diffable.INDEX, a Tree or Commit, was %r" % other)
1698 # Diff against working copy - can be handled by superclass natively.
1699 return super().diff(other, paths, create_patch, allow_unsafe_options=allow_unsafe_options, **kwargs)