Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/git/repo/base.py: 46%

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

593 statements  

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/ 

5 

6from __future__ import annotations 

7 

8__all__ = ["Repo"] 

9 

10import gc 

11import logging 

12import os 

13import os.path as osp 

14from pathlib import Path 

15import re 

16import shlex 

17import sys 

18import warnings 

19 

20import gitdb 

21from gitdb.db.loose import LooseObjectDB 

22from gitdb.exc import BadObject 

23 

24from git.cmd import Git, handle_process_output 

25from git.compat import defenc, safe_decode 

26from git.config import GitConfigParser 

27from git.db import GitCmdObjectDB 

28from git.exc import ( 

29 GitCommandError, 

30 InvalidGitRepositoryError, 

31 NoSuchPathError, 

32) 

33from git.index import IndexFile 

34from git.objects import Submodule, RootModule, Commit 

35from git.refs import HEAD, Head, Reference, TagReference 

36from git.remote import Remote, add_progress, to_progress_instance 

37from git.util import ( 

38 Actor, 

39 cygpath, 

40 expand_path, 

41 finalize_process, 

42 hex_to_bin, 

43 remove_password_if_present, 

44) 

45 

46from .fun import ( 

47 find_submodule_git_dir, 

48 find_worktree_git_dir, 

49 is_git_dir, 

50 rev_parse, 

51 touch, 

52) 

53 

54# typing ------------------------------------------------------ 

55 

56from git.types import ( 

57 CallableProgress, 

58 Commit_ish, 

59 Lit_config_levels, 

60 PathLike, 

61 TBD, 

62 Tree_ish, 

63 assert_never, 

64) 

65from typing import ( 

66 Any, 

67 BinaryIO, 

68 Callable, 

69 Dict, 

70 Iterator, 

71 List, 

72 Mapping, 

73 NamedTuple, 

74 Optional, 

75 Sequence, 

76 TYPE_CHECKING, 

77 TextIO, 

78 Tuple, 

79 Type, 

80 Union, 

81 cast, 

82) 

83 

84from git.types import ConfigLevels_Tup, TypedDict 

85 

86if TYPE_CHECKING: 

87 from git.objects import Tree 

88 from git.objects.submodule.base import UpdateProgress 

89 from git.refs.symbolic import SymbolicReference 

90 from git.remote import RemoteProgress 

91 from git.util import IterableList 

92 

93# ----------------------------------------------------------- 

94 

95_logger = logging.getLogger(__name__) 

96 

97 

98class BlameEntry(NamedTuple): 

99 commit: Dict[str, Commit] 

100 linenos: range 

101 orig_path: Optional[str] 

102 orig_linenos: range 

103 

104 

105class Repo: 

106 """Represents a git repository and allows you to query references, create commit 

107 information, generate diffs, create and clone repositories, and query the log. 

108 

109 The following attributes are worth using: 

110 

111 * :attr:`working_dir` is the working directory of the git command, which is the 

112 working tree directory if available or the ``.git`` directory in case of bare 

113 repositories. 

114 

115 * :attr:`working_tree_dir` is the working tree directory, but will return ``None`` 

116 if we are a bare repository. 

117 

118 * :attr:`git_dir` is the ``.git`` repository directory, which is always set. 

119 """ 

120 

121 DAEMON_EXPORT_FILE = "git-daemon-export-ok" 

122 

123 # Must exist, or __del__ will fail in case we raise on `__init__()`. 

124 git = cast("Git", None) 

125 

126 working_dir: PathLike 

127 """The working directory of the git command.""" 

128 

129 # stored as string for easier processing, but annotated as path for clearer intention 

130 _working_tree_dir: Optional[PathLike] = None 

131 

132 git_dir: PathLike 

133 """The ``.git`` repository directory.""" 

134 

135 _common_dir: PathLike = "" 

136 

137 # Precompiled regex 

138 re_whitespace = re.compile(r"\s+") 

139 re_hexsha_only = re.compile(r"^[0-9A-Fa-f]{40}$") 

140 re_hexsha_shortened = re.compile(r"^[0-9A-Fa-f]{4,40}$") 

141 re_envvars = re.compile(r"(\$(\{\s?)?[a-zA-Z_]\w*(\}\s?)?|%\s?[a-zA-Z_]\w*\s?%)") 

142 re_author_committer_start = re.compile(r"^(author|committer)") 

143 re_tab_full_line = re.compile(r"^\t(.*)$") 

144 

145 unsafe_git_clone_options = [ 

146 # Executes arbitrary commands: 

147 "--upload-pack", 

148 "-u", 

149 # Can override configuration variables that execute arbitrary commands: 

150 "--config", 

151 "-c", 

152 ] 

153 """Options to :manpage:`git-clone(1)` that allow arbitrary commands to be executed. 

154 

155 The ``--upload-pack``/``-u`` option allows users to execute arbitrary commands 

156 directly: 

157 https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---upload-packltupload-packgt 

158 

159 The ``--config``/``-c`` option allows users to override configuration variables like 

160 ``protocol.allow`` and ``core.gitProxy`` to execute arbitrary commands: 

161 https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---configltkeygtltvaluegt 

162 """ 

163 

164 # Invariants 

165 config_level: ConfigLevels_Tup = ("system", "user", "global", "repository") 

166 """Represents the configuration level of a configuration file.""" 

167 

168 # Subclass configuration 

169 GitCommandWrapperType = Git 

170 """Subclasses may easily bring in their own custom types by placing a constructor or 

171 type here.""" 

172 

173 def __init__( 

174 self, 

175 path: Optional[PathLike] = None, 

176 odbt: Type[LooseObjectDB] = GitCmdObjectDB, 

177 search_parent_directories: bool = False, 

178 expand_vars: bool = True, 

179 ) -> None: 

180 R"""Create a new :class:`Repo` instance. 

181 

182 :param path: 

183 The path to either the worktree directory or the .git directory itself:: 

184 

185 repo = Repo("/Users/mtrier/Development/git-python") 

186 repo = Repo("/Users/mtrier/Development/git-python.git") 

187 repo = Repo("~/Development/git-python.git") 

188 repo = Repo("$REPOSITORIES/Development/git-python.git") 

189 repo = Repo(R"C:\Users\mtrier\Development\git-python\.git") 

190 

191 - In *Cygwin*, `path` may be a ``cygdrive/...`` prefixed path. 

192 - If `path` is ``None`` or an empty string, :envvar:`GIT_DIR` is used. If 

193 that environment variable is absent or empty, the current directory is 

194 used. 

195 

196 :param odbt: 

197 Object DataBase type - a type which is constructed by providing the 

198 directory containing the database objects, i.e. ``.git/objects``. It will be 

199 used to access all object data. 

200 

201 :param search_parent_directories: 

202 If ``True``, all parent directories will be searched for a valid repo as 

203 well. 

204 

205 Please note that this was the default behaviour in older versions of 

206 GitPython, which is considered a bug though. 

207 

208 :raise git.exc.InvalidGitRepositoryError: 

209 

210 :raise git.exc.NoSuchPathError: 

211 

212 :return: 

213 :class:`Repo` 

214 """ 

215 

216 epath = path or os.getenv("GIT_DIR") 

217 if not epath: 

218 epath = os.getcwd() 

219 epath = os.fspath(epath) 

220 if Git.is_cygwin(): 

221 # Given how the tests are written, this seems more likely to catch Cygwin 

222 # git used from Windows than Windows git used from Cygwin. Therefore 

223 # changing to Cygwin-style paths is the relevant operation. 

224 epath = cygpath(epath) 

225 

226 if expand_vars and re.search(self.re_envvars, epath): 

227 warnings.warn( 

228 "The use of environment variables in paths is deprecated" 

229 + "\nfor security reasons and may be removed in the future!!", 

230 stacklevel=1, 

231 ) 

232 epath = expand_path(epath, expand_vars) 

233 if epath is not None: 

234 if not os.path.exists(epath): 

235 raise NoSuchPathError(epath) 

236 

237 # Walk up the path to find the `.git` dir. 

238 curpath = epath 

239 git_dir = None 

240 while curpath: 

241 # ABOUT osp.NORMPATH 

242 # It's important to normalize the paths, as submodules will otherwise 

243 # initialize their repo instances with paths that depend on path-portions 

244 # that will not exist after being removed. It's just cleaner. 

245 if is_git_dir(curpath): 

246 git_dir = curpath 

247 # from man git-config : core.worktree 

248 # Set the path to the root of the working tree. If GIT_COMMON_DIR 

249 # environment variable is set, core.worktree is ignored and not used for 

250 # determining the root of working tree. This can be overridden by the 

251 # GIT_WORK_TREE environment variable. The value can be an absolute path 

252 # or relative to the path to the .git directory, which is either 

253 # specified by GIT_DIR, or automatically discovered. If GIT_DIR is 

254 # specified but none of GIT_WORK_TREE and core.worktree is specified, 

255 # the current working directory is regarded as the top level of your 

256 # working tree. 

257 self._working_tree_dir = os.path.dirname(git_dir) 

258 if os.environ.get("GIT_COMMON_DIR") is None: 

259 gitconf = self._config_reader("repository", git_dir) 

260 if gitconf.has_option("core", "worktree"): 

261 self._working_tree_dir = gitconf.get("core", "worktree") 

262 if "GIT_WORK_TREE" in os.environ: 

263 self._working_tree_dir = os.getenv("GIT_WORK_TREE") 

264 break 

265 

266 dotgit = osp.join(curpath, ".git") 

267 sm_gitpath = find_submodule_git_dir(dotgit) 

268 if sm_gitpath is not None: 

269 git_dir = osp.normpath(sm_gitpath) 

270 

271 sm_gitpath = find_submodule_git_dir(dotgit) 

272 if sm_gitpath is None: 

273 sm_gitpath = find_worktree_git_dir(dotgit) 

274 

275 if sm_gitpath is not None: 

276 git_dir = expand_path(sm_gitpath, expand_vars) 

277 self._working_tree_dir = curpath 

278 break 

279 

280 if not search_parent_directories: 

281 break 

282 curpath, tail = osp.split(curpath) 

283 if not tail: 

284 break 

285 # END while curpath 

286 

287 if git_dir is None: 

288 raise InvalidGitRepositoryError(epath) 

289 self.git_dir = git_dir 

290 

291 self._bare = False 

292 try: 

293 self._bare = self.config_reader("repository").getboolean("core", "bare") 

294 except Exception: 

295 # Let's not assume the option exists, although it should. 

296 pass 

297 

298 try: 

299 common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip() 

300 self._common_dir = osp.join(self.git_dir, common_dir) 

301 except OSError: 

302 self._common_dir = "" 

303 

304 # Adjust the working directory in case we are actually bare - we didn't know 

305 # that in the first place. 

306 if self._bare: 

307 self._working_tree_dir = None 

308 # END working dir handling 

309 

310 self.working_dir: PathLike = self._working_tree_dir or self.common_dir 

311 self.git = self.GitCommandWrapperType(self.working_dir) 

312 

313 # Special handling, in special times. 

314 rootpath = osp.join(self.common_dir, "objects") 

315 if issubclass(odbt, GitCmdObjectDB): 

316 self.odb = odbt(rootpath, self.git) 

317 else: 

318 self.odb = odbt(rootpath) 

319 

320 def __enter__(self) -> "Repo": 

321 return self 

322 

323 def __exit__(self, *args: Any) -> None: 

324 self.close() 

325 

326 def __del__(self) -> None: 

327 try: 

328 self.close() 

329 except Exception: 

330 pass 

331 

332 def close(self) -> None: 

333 if self.git: 

334 self.git.clear_cache() 

335 # Tempfiles objects on Windows are holding references to open files until 

336 # they are collected by the garbage collector, thus preventing deletion. 

337 # TODO: Find these references and ensure they are closed and deleted 

338 # synchronously rather than forcing a gc collection. 

339 if sys.platform == "win32": 

340 gc.collect() 

341 gitdb.util.mman.collect() 

342 if sys.platform == "win32": 

343 gc.collect() 

344 

345 def __eq__(self, rhs: object) -> bool: 

346 if isinstance(rhs, Repo): 

347 return self.git_dir == rhs.git_dir 

348 return False 

349 

350 def __ne__(self, rhs: object) -> bool: 

351 return not self.__eq__(rhs) 

352 

353 def __hash__(self) -> int: 

354 return hash(self.git_dir) 

355 

356 @property 

357 def description(self) -> str: 

358 """The project's description""" 

359 filename = osp.join(self.git_dir, "description") 

360 with open(filename, "rb") as fp: 

361 return fp.read().rstrip().decode(defenc) 

362 

363 @description.setter 

364 def description(self, descr: str) -> None: 

365 filename = osp.join(self.git_dir, "description") 

366 with open(filename, "wb") as fp: 

367 fp.write((descr + "\n").encode(defenc)) 

368 

369 @property 

370 def working_tree_dir(self) -> Optional[PathLike]: 

371 """ 

372 :return: 

373 The working tree directory of our git repository. 

374 If this is a bare repository, ``None`` is returned. 

375 """ 

376 return self._working_tree_dir 

377 

378 @property 

379 def common_dir(self) -> PathLike: 

380 """ 

381 :return: 

382 The git dir that holds everything except possibly HEAD, FETCH_HEAD, 

383 ORIG_HEAD, COMMIT_EDITMSG, index, and logs/. 

384 """ 

385 return self._common_dir or self.git_dir 

386 

387 @property 

388 def bare(self) -> bool: 

389 """:return: ``True`` if the repository is bare""" 

390 return self._bare 

391 

392 @property 

393 def heads(self) -> "IterableList[Head]": 

394 """A list of :class:`~git.refs.head.Head` objects representing the branch heads 

395 in this repo. 

396 

397 :return: 

398 ``git.IterableList(Head, ...)`` 

399 """ 

400 return Head.list_items(self) 

401 

402 @property 

403 def branches(self) -> "IterableList[Head]": 

404 """Alias for heads. 

405 A list of :class:`~git.refs.head.Head` objects representing the branch heads 

406 in this repo. 

407 

408 :return: 

409 ``git.IterableList(Head, ...)`` 

410 """ 

411 return self.heads 

412 

413 @property 

414 def references(self) -> "IterableList[Reference]": 

415 """A list of :class:`~git.refs.reference.Reference` objects representing tags, 

416 heads and remote references. 

417 

418 :return: 

419 ``git.IterableList(Reference, ...)`` 

420 """ 

421 return Reference.list_items(self) 

422 

423 @property 

424 def refs(self) -> "IterableList[Reference]": 

425 """Alias for references. 

426 A list of :class:`~git.refs.reference.Reference` objects representing tags, 

427 heads and remote references. 

428 

429 :return: 

430 ``git.IterableList(Reference, ...)`` 

431 """ 

432 return self.references 

433 

434 @property 

435 def index(self) -> "IndexFile": 

436 """ 

437 :return: 

438 A :class:`~git.index.base.IndexFile` representing this repository's index. 

439 

440 :note: 

441 This property can be expensive, as the returned 

442 :class:`~git.index.base.IndexFile` will be reinitialized. 

443 It is recommended to reuse the object. 

444 """ 

445 return IndexFile(self) 

446 

447 @property 

448 def head(self) -> "HEAD": 

449 """ 

450 :return: 

451 :class:`~git.refs.head.HEAD` object pointing to the current head reference 

452 """ 

453 return HEAD(self, "HEAD") 

454 

455 @property 

456 def remotes(self) -> "IterableList[Remote]": 

457 """A list of :class:`~git.remote.Remote` objects allowing to access and 

458 manipulate remotes. 

459 

460 :return: 

461 ``git.IterableList(Remote, ...)`` 

462 """ 

463 return Remote.list_items(self) 

464 

465 def remote(self, name: str = "origin") -> "Remote": 

466 """:return: The remote with the specified name 

467 

468 :raise ValueError: 

469 If no remote with such a name exists. 

470 """ 

471 r = Remote(self, name) 

472 if not r.exists(): 

473 raise ValueError("Remote named '%s' didn't exist" % name) 

474 return r 

475 

476 # { Submodules 

477 

478 @property 

479 def submodules(self) -> "IterableList[Submodule]": 

480 """ 

481 :return: 

482 git.IterableList(Submodule, ...) of direct submodules available from the 

483 current head 

484 """ 

485 return Submodule.list_items(self) 

486 

487 def submodule(self, name: str) -> "Submodule": 

488 """:return: The submodule with the given name 

489 

490 :raise ValueError: 

491 If no such submodule exists. 

492 """ 

493 try: 

494 return self.submodules[name] 

495 except IndexError as e: 

496 raise ValueError("Didn't find submodule named %r" % name) from e 

497 # END exception handling 

498 

499 def create_submodule(self, *args: Any, **kwargs: Any) -> Submodule: 

500 """Create a new submodule. 

501 

502 :note: 

503 For a description of the applicable parameters, see the documentation of 

504 :meth:`Submodule.add <git.objects.submodule.base.Submodule.add>`. 

505 

506 :return: 

507 The created submodule. 

508 """ 

509 return Submodule.add(self, *args, **kwargs) 

510 

511 def iter_submodules(self, *args: Any, **kwargs: Any) -> Iterator[Submodule]: 

512 """An iterator yielding Submodule instances. 

513 

514 See the :class:`~git.objects.util.Traversable` interface for a description of `args` 

515 and `kwargs`. 

516 

517 :return: 

518 Iterator 

519 """ 

520 return RootModule(self).traverse(*args, **kwargs) 

521 

522 def submodule_update(self, *args: Any, **kwargs: Any) -> RootModule: 

523 """Update the submodules, keeping the repository consistent as it will 

524 take the previous state into consideration. 

525 

526 :note: 

527 For more information, please see the documentation of 

528 :meth:`RootModule.update <git.objects.submodule.root.RootModule.update>`. 

529 """ 

530 return RootModule(self).update(*args, **kwargs) 

531 

532 # }END submodules 

533 

534 @property 

535 def tags(self) -> "IterableList[TagReference]": 

536 """A list of :class:`~git.refs.tag.TagReference` objects that are available in 

537 this repo. 

538 

539 :return: 

540 ``git.IterableList(TagReference, ...)`` 

541 """ 

542 return TagReference.list_items(self) 

543 

544 def tag(self, path: PathLike) -> TagReference: 

545 """ 

546 :return: 

547 :class:`~git.refs.tag.TagReference` object, reference pointing to a 

548 :class:`~git.objects.commit.Commit` or tag 

549 

550 :param path: 

551 Path to the tag reference, e.g. ``0.1.5`` or ``tags/0.1.5``. 

552 """ 

553 full_path = self._to_full_tag_path(path) 

554 return TagReference(self, full_path) 

555 

556 @staticmethod 

557 def _to_full_tag_path(path: PathLike) -> str: 

558 path_str = str(path) 

559 if path_str.startswith(TagReference._common_path_default + "/"): 

560 return path_str 

561 if path_str.startswith(TagReference._common_default + "/"): 

562 return Reference._common_path_default + "/" + path_str 

563 else: 

564 return TagReference._common_path_default + "/" + path_str 

565 

566 def create_head( 

567 self, 

568 path: PathLike, 

569 commit: Union["SymbolicReference", "str"] = "HEAD", 

570 force: bool = False, 

571 logmsg: Optional[str] = None, 

572 ) -> "Head": 

573 """Create a new head within the repository. 

574 

575 :note: 

576 For more documentation, please see the 

577 :meth:`Head.create <git.refs.head.Head.create>` method. 

578 

579 :return: 

580 Newly created :class:`~git.refs.head.Head` Reference. 

581 """ 

582 return Head.create(self, path, commit, logmsg, force) 

583 

584 def delete_head(self, *heads: "Union[str, Head]", **kwargs: Any) -> None: 

585 """Delete the given heads. 

586 

587 :param kwargs: 

588 Additional keyword arguments to be passed to :manpage:`git-branch(1)`. 

589 """ 

590 return Head.delete(self, *heads, **kwargs) 

591 

592 def create_tag( 

593 self, 

594 path: PathLike, 

595 ref: Union[str, "SymbolicReference"] = "HEAD", 

596 message: Optional[str] = None, 

597 force: bool = False, 

598 **kwargs: Any, 

599 ) -> TagReference: 

600 """Create a new tag reference. 

601 

602 :note: 

603 For more documentation, please see the 

604 :meth:`TagReference.create <git.refs.tag.TagReference.create>` method. 

605 

606 :return: 

607 :class:`~git.refs.tag.TagReference` object 

608 """ 

609 return TagReference.create(self, path, ref, message, force, **kwargs) 

610 

611 def delete_tag(self, *tags: TagReference) -> None: 

612 """Delete the given tag references.""" 

613 return TagReference.delete(self, *tags) 

614 

615 def create_remote(self, name: str, url: str, **kwargs: Any) -> Remote: 

616 """Create a new remote. 

617 

618 For more information, please see the documentation of the 

619 :meth:`Remote.create <git.remote.Remote.create>` method. 

620 

621 :return: 

622 :class:`~git.remote.Remote` reference 

623 """ 

624 return Remote.create(self, name, url, **kwargs) 

625 

626 def delete_remote(self, remote: "Remote") -> str: 

627 """Delete the given remote.""" 

628 return Remote.remove(self, remote) 

629 

630 def _get_config_path(self, config_level: Lit_config_levels, git_dir: Optional[PathLike] = None) -> str: 

631 if git_dir is None: 

632 git_dir = self.git_dir 

633 # We do not support an absolute path of the gitconfig on Windows. 

634 # Use the global config instead. 

635 if sys.platform == "win32" and config_level == "system": 

636 config_level = "global" 

637 

638 if config_level == "system": 

639 return "/etc/gitconfig" 

640 elif config_level == "user": 

641 config_home = os.environ.get("XDG_CONFIG_HOME") or osp.join(os.environ.get("HOME", "~"), ".config") 

642 return osp.normpath(osp.expanduser(osp.join(config_home, "git", "config"))) 

643 elif config_level == "global": 

644 return osp.normpath(osp.expanduser("~/.gitconfig")) 

645 elif config_level == "repository": 

646 repo_dir = self._common_dir or git_dir 

647 if not repo_dir: 

648 raise NotADirectoryError 

649 else: 

650 return osp.normpath(osp.join(repo_dir, "config")) 

651 else: 

652 assert_never( # type: ignore[unreachable] 

653 config_level, 

654 ValueError(f"Invalid configuration level: {config_level!r}"), 

655 ) 

656 

657 def config_reader( 

658 self, 

659 config_level: Optional[Lit_config_levels] = None, 

660 ) -> GitConfigParser: 

661 """ 

662 :return: 

663 :class:`~git.config.GitConfigParser` allowing to read the full git 

664 configuration, but not to write it. 

665 

666 The configuration will include values from the system, user and repository 

667 configuration files. 

668 

669 :param config_level: 

670 For possible values, see the :meth:`config_writer` method. If ``None``, all 

671 applicable levels will be used. Specify a level in case you know which file 

672 you wish to read to prevent reading multiple files. 

673 

674 :note: 

675 On Windows, system configuration cannot currently be read as the path is 

676 unknown, instead the global path will be used. 

677 """ 

678 return self._config_reader(config_level=config_level) 

679 

680 def _config_reader( 

681 self, 

682 config_level: Optional[Lit_config_levels] = None, 

683 git_dir: Optional[PathLike] = None, 

684 ) -> GitConfigParser: 

685 if config_level is None: 

686 files = [self._get_config_path(f, git_dir) for f in self.config_level if f] 

687 else: 

688 files = [self._get_config_path(config_level, git_dir)] 

689 return GitConfigParser(files, read_only=True, repo=self) 

690 

691 def config_writer(self, config_level: Lit_config_levels = "repository") -> GitConfigParser: 

692 """ 

693 :return: 

694 A :class:`~git.config.GitConfigParser` allowing to write values of the 

695 specified configuration file level. Config writers should be retrieved, used 

696 to change the configuration, and written right away as they will lock the 

697 configuration file in question and prevent other's to write it. 

698 

699 :param config_level: 

700 One of the following values: 

701 

702 * ``"system"`` = system wide configuration file 

703 * ``"global"`` = user level configuration file 

704 * ``"`repository"`` = configuration file for this repository only 

705 """ 

706 return GitConfigParser(self._get_config_path(config_level), read_only=False, repo=self, merge_includes=False) 

707 

708 def commit(self, rev: Union[str, Commit_ish, None] = None) -> Commit: 

709 """The :class:`~git.objects.commit.Commit` object for the specified revision. 

710 

711 :param rev: 

712 Revision specifier, see :manpage:`git-rev-parse(1)` for viable options. 

713 

714 :return: 

715 :class:`~git.objects.commit.Commit` 

716 """ 

717 if rev is None: 

718 return self.head.commit 

719 return self.rev_parse(str(rev) + "^0") 

720 

721 def iter_trees(self, *args: Any, **kwargs: Any) -> Iterator["Tree"]: 

722 """:return: Iterator yielding :class:`~git.objects.tree.Tree` objects 

723 

724 :note: 

725 Accepts all arguments known to the :meth:`iter_commits` method. 

726 """ 

727 return (c.tree for c in self.iter_commits(*args, **kwargs)) 

728 

729 def tree(self, rev: Union[Tree_ish, str, None] = None) -> "Tree": 

730 """The :class:`~git.objects.tree.Tree` object for the given tree-ish revision. 

731 

732 Examples:: 

733 

734 repo.tree(repo.heads[0]) 

735 

736 :param rev: 

737 A revision pointing to a Treeish (being a commit or tree). 

738 

739 :return: 

740 :class:`~git.objects.tree.Tree` 

741 

742 :note: 

743 If you need a non-root level tree, find it by iterating the root tree. 

744 Otherwise it cannot know about its path relative to the repository root and 

745 subsequent operations might have unexpected results. 

746 """ 

747 if rev is None: 

748 return self.head.commit.tree 

749 return self.rev_parse(str(rev) + "^{tree}") 

750 

751 def iter_commits( 

752 self, 

753 rev: Union[str, Commit, "SymbolicReference", None] = None, 

754 paths: Union[PathLike, Sequence[PathLike]] = "", 

755 **kwargs: Any, 

756 ) -> Iterator[Commit]: 

757 """An iterator of :class:`~git.objects.commit.Commit` objects representing the 

758 history of a given ref/commit. 

759 

760 :param rev: 

761 Revision specifier, see :manpage:`git-rev-parse(1)` for viable options. 

762 If ``None``, the active branch will be used. 

763 

764 :param paths: 

765 An optional path or a list of paths. If set, only commits that include the 

766 path or paths will be returned. 

767 

768 :param kwargs: 

769 Arguments to be passed to :manpage:`git-rev-list(1)`. 

770 Common ones are ``max_count`` and ``skip``. 

771 

772 :note: 

773 To receive only commits between two named revisions, use the 

774 ``"revA...revB"`` revision specifier. 

775 

776 :return: 

777 Iterator of :class:`~git.objects.commit.Commit` objects 

778 """ 

779 if rev is None: 

780 rev = self.head.commit 

781 

782 return Commit.iter_items(self, rev, paths, **kwargs) 

783 

784 def merge_base(self, *rev: TBD, **kwargs: Any) -> List[Commit]: 

785 R"""Find the closest common ancestor for the given revision 

786 (:class:`~git.objects.commit.Commit`\s, :class:`~git.refs.tag.Tag`\s, 

787 :class:`~git.refs.reference.Reference`\s, etc.). 

788 

789 :param rev: 

790 At least two revs to find the common ancestor for. 

791 

792 :param kwargs: 

793 Additional arguments to be passed to the ``repo.git.merge_base()`` command 

794 which does all the work. 

795 

796 :return: 

797 A list of :class:`~git.objects.commit.Commit` objects. If ``--all`` was 

798 not passed as a keyword argument, the list will have at max one 

799 :class:`~git.objects.commit.Commit`, or is empty if no common merge base 

800 exists. 

801 

802 :raise ValueError: 

803 If fewer than two revisions are provided. 

804 """ 

805 if len(rev) < 2: 

806 raise ValueError("Please specify at least two revs, got only %i" % len(rev)) 

807 # END handle input 

808 

809 res: List[Commit] = [] 

810 try: 

811 lines: List[str] = self.git.merge_base(*rev, **kwargs).splitlines() 

812 except GitCommandError as err: 

813 if err.status == 128: 

814 raise 

815 # END handle invalid rev 

816 # Status code 1 is returned if there is no merge-base. 

817 # (See: https://github.com/git/git/blob/v2.44.0/builtin/merge-base.c#L19) 

818 return res 

819 # END exception handling 

820 

821 for line in lines: 

822 res.append(self.commit(line)) 

823 # END for each merge-base 

824 

825 return res 

826 

827 def is_ancestor(self, ancestor_rev: Commit, rev: Commit) -> bool: 

828 """Check if a commit is an ancestor of another. 

829 

830 :param ancestor_rev: 

831 Rev which should be an ancestor. 

832 

833 :param rev: 

834 Rev to test against `ancestor_rev`. 

835 

836 :return: 

837 ``True`` if `ancestor_rev` is an ancestor to `rev`. 

838 """ 

839 try: 

840 self.git.merge_base(ancestor_rev, rev, is_ancestor=True) 

841 except GitCommandError as err: 

842 if err.status == 1: 

843 return False 

844 raise 

845 return True 

846 

847 def is_valid_object(self, sha: str, object_type: Union[str, None] = None) -> bool: 

848 try: 

849 complete_sha = self.odb.partial_to_complete_sha_hex(sha) 

850 object_info = self.odb.info(complete_sha) 

851 if object_type: 

852 if object_info.type == object_type.encode(): 

853 return True 

854 else: 

855 _logger.debug( 

856 "Commit hash points to an object of type '%s'. Requested were objects of type '%s'", 

857 object_info.type.decode(), 

858 object_type, 

859 ) 

860 return False 

861 else: 

862 return True 

863 except BadObject: 

864 _logger.debug("Commit hash is invalid.") 

865 return False 

866 

867 def _get_daemon_export(self) -> bool: 

868 if self.git_dir: 

869 filename = osp.join(self.git_dir, self.DAEMON_EXPORT_FILE) 

870 return osp.exists(filename) 

871 

872 def _set_daemon_export(self, value: object) -> None: 

873 if self.git_dir: 

874 filename = osp.join(self.git_dir, self.DAEMON_EXPORT_FILE) 

875 fileexists = osp.exists(filename) 

876 if value and not fileexists: 

877 touch(filename) 

878 elif not value and fileexists: 

879 os.unlink(filename) 

880 

881 @property 

882 def daemon_export(self) -> bool: 

883 """If True, git-daemon may export this repository""" 

884 return self._get_daemon_export() 

885 

886 @daemon_export.setter 

887 def daemon_export(self, value: object) -> None: 

888 self._set_daemon_export(value) 

889 

890 def _get_alternates(self) -> List[str]: 

891 """The list of alternates for this repo from which objects can be retrieved. 

892 

893 :return: 

894 List of strings being pathnames of alternates 

895 """ 

896 if self.git_dir: 

897 alternates_path = osp.join(self.git_dir, "objects", "info", "alternates") 

898 

899 if osp.exists(alternates_path): 

900 with open(alternates_path, "rb") as f: 

901 alts = f.read().decode(defenc) 

902 return alts.strip().splitlines() 

903 return [] 

904 

905 def _set_alternates(self, alts: List[str]) -> None: 

906 """Set the alternates. 

907 

908 :param alts: 

909 The array of string paths representing the alternates at which git should 

910 look for objects, i.e. ``/home/user/repo/.git/objects``. 

911 

912 :raise git.exc.NoSuchPathError: 

913 

914 :note: 

915 The method does not check for the existence of the paths in `alts`, as the 

916 caller is responsible. 

917 """ 

918 alternates_path = osp.join(self.common_dir, "objects", "info", "alternates") 

919 if not alts: 

920 if osp.isfile(alternates_path): 

921 os.remove(alternates_path) 

922 else: 

923 with open(alternates_path, "wb") as f: 

924 f.write("\n".join(alts).encode(defenc)) 

925 

926 @property 

927 def alternates(self) -> List[str]: 

928 """Retrieve a list of alternates paths or set a list paths to be used as alternates""" 

929 return self._get_alternates() 

930 

931 @alternates.setter 

932 def alternates(self, alts: List[str]) -> None: 

933 self._set_alternates(alts) 

934 

935 def is_dirty( 

936 self, 

937 index: bool = True, 

938 working_tree: bool = True, 

939 untracked_files: bool = False, 

940 submodules: bool = True, 

941 path: Optional[PathLike] = None, 

942 ) -> bool: 

943 """ 

944 :return: 

945 ``True`` if the repository is considered dirty. By default it will react 

946 like a :manpage:`git-status(1)` without untracked files, hence it is dirty 

947 if the index or the working copy have changes. 

948 """ 

949 if self._bare: 

950 # Bare repositories with no associated working directory are 

951 # always considered to be clean. 

952 return False 

953 

954 # Start from the one which is fastest to evaluate. 

955 default_args = ["--abbrev=40", "--full-index", "--raw"] 

956 if not submodules: 

957 default_args.append("--ignore-submodules") 

958 if path: 

959 default_args.extend(["--", os.fspath(path)]) 

960 if index: 

961 # diff index against HEAD. 

962 if osp.isfile(self.index.path) and len(self.git.diff("--cached", *default_args)): 

963 return True 

964 # END index handling 

965 if working_tree: 

966 # diff index against working tree. 

967 if len(self.git.diff(*default_args)): 

968 return True 

969 # END working tree handling 

970 if untracked_files: 

971 if len(self._get_untracked_files(path, ignore_submodules=not submodules)): 

972 return True 

973 # END untracked files 

974 return False 

975 

976 @property 

977 def untracked_files(self) -> List[str]: 

978 """ 

979 :return: 

980 list(str,...) 

981 

982 Files currently untracked as they have not been staged yet. Paths are 

983 relative to the current working directory of the git command. 

984 

985 :note: 

986 Ignored files will not appear here, i.e. files mentioned in ``.gitignore``. 

987 

988 :note: 

989 This property is expensive, as no cache is involved. To process the result, 

990 please consider caching it yourself. 

991 """ 

992 return self._get_untracked_files() 

993 

994 def _get_untracked_files(self, *args: Any, **kwargs: Any) -> List[str]: 

995 # Make sure we get all files, not only untracked directories. 

996 proc = self.git.status(*args, porcelain=True, untracked_files=True, as_process=True, **kwargs) 

997 # Untracked files prefix in porcelain mode 

998 prefix = "?? " 

999 untracked_files = [] 

1000 for line in proc.stdout: 

1001 line = line.decode(defenc) 

1002 if not line.startswith(prefix): 

1003 continue 

1004 filename = line[len(prefix) :].rstrip("\n") 

1005 # Special characters are escaped 

1006 if filename[0] == filename[-1] == '"': 

1007 filename = filename[1:-1] 

1008 # WHATEVER ... it's a mess, but works for me 

1009 filename = filename.encode("ascii").decode("unicode_escape").encode("latin1").decode(defenc) 

1010 untracked_files.append(filename) 

1011 finalize_process(proc) 

1012 return untracked_files 

1013 

1014 def ignored(self, *paths: PathLike) -> List[str]: 

1015 """Checks if paths are ignored via ``.gitignore``. 

1016 

1017 This does so using the :manpage:`git-check-ignore(1)` method. 

1018 

1019 :param paths: 

1020 List of paths to check whether they are ignored or not. 

1021 

1022 :return: 

1023 Subset of those paths which are ignored 

1024 """ 

1025 try: 

1026 proc: str = self.git.check_ignore(*paths) 

1027 except GitCommandError as err: 

1028 if err.status == 1: 

1029 # If return code is 1, this means none of the items in *paths are 

1030 # ignored by Git, so return an empty list. 

1031 return [] 

1032 else: 

1033 # Raise the exception on all other return codes. 

1034 raise 

1035 

1036 return proc.replace("\\\\", "\\").replace('"', "").split("\n") 

1037 

1038 @property 

1039 def active_branch(self) -> Head: 

1040 """The name of the currently active branch. 

1041 

1042 :raise TypeError: 

1043 If HEAD is detached. 

1044 

1045 :return: 

1046 :class:`~git.refs.head.Head` to the active branch 

1047 """ 

1048 # reveal_type(self.head.reference) # => Reference 

1049 return self.head.reference 

1050 

1051 def blame_incremental(self, rev: str | HEAD | None, file: str, **kwargs: Any) -> Iterator["BlameEntry"]: 

1052 """Iterator for blame information for the given file at the given revision. 

1053 

1054 Unlike :meth:`blame`, this does not return the actual file's contents, only a 

1055 stream of :class:`BlameEntry` tuples. 

1056 

1057 :param rev: 

1058 Revision specifier. If ``None``, the blame will include all the latest 

1059 uncommitted changes. Otherwise, anything successfully parsed by 

1060 :manpage:`git-rev-parse(1)` is a valid option. 

1061 

1062 :return: 

1063 Lazy iterator of :class:`BlameEntry` tuples, where the commit indicates the 

1064 commit to blame for the line, and range indicates a span of line numbers in 

1065 the resulting file. 

1066 

1067 If you combine all line number ranges outputted by this command, you should get 

1068 a continuous range spanning all line numbers in the file. 

1069 """ 

1070 

1071 data: bytes = self.git.blame(rev, "--", file, p=True, incremental=True, stdout_as_string=False, **kwargs) 

1072 commits: Dict[bytes, Commit] = {} 

1073 

1074 stream = (line for line in data.split(b"\n") if line) 

1075 while True: 

1076 try: 

1077 # When exhausted, causes a StopIteration, terminating this function. 

1078 line = next(stream) 

1079 except StopIteration: 

1080 return 

1081 split_line = line.split() 

1082 hexsha, orig_lineno_b, lineno_b, num_lines_b = split_line 

1083 lineno = int(lineno_b) 

1084 num_lines = int(num_lines_b) 

1085 orig_lineno = int(orig_lineno_b) 

1086 if hexsha not in commits: 

1087 # Now read the next few lines and build up a dict of properties for this 

1088 # commit. 

1089 props: Dict[bytes, bytes] = {} 

1090 while True: 

1091 try: 

1092 line = next(stream) 

1093 except StopIteration: 

1094 return 

1095 if line == b"boundary": 

1096 # "boundary" indicates a root commit and occurs instead of the 

1097 # "previous" tag. 

1098 continue 

1099 

1100 tag, value = line.split(b" ", 1) 

1101 props[tag] = value 

1102 if tag == b"filename": 

1103 # "filename" formally terminates the entry for --incremental. 

1104 orig_filename = value 

1105 break 

1106 

1107 c = Commit( 

1108 self, 

1109 hex_to_bin(hexsha), 

1110 author=Actor( 

1111 safe_decode(props[b"author"]), 

1112 safe_decode(props[b"author-mail"].lstrip(b"<").rstrip(b">")), 

1113 ), 

1114 authored_date=int(props[b"author-time"]), 

1115 committer=Actor( 

1116 safe_decode(props[b"committer"]), 

1117 safe_decode(props[b"committer-mail"].lstrip(b"<").rstrip(b">")), 

1118 ), 

1119 committed_date=int(props[b"committer-time"]), 

1120 ) 

1121 commits[hexsha] = c 

1122 else: 

1123 # Discard all lines until we find "filename" which is guaranteed to be 

1124 # the last line. 

1125 while True: 

1126 try: 

1127 # Will fail if we reach the EOF unexpectedly. 

1128 line = next(stream) 

1129 except StopIteration: 

1130 return 

1131 tag, value = line.split(b" ", 1) 

1132 if tag == b"filename": 

1133 orig_filename = value 

1134 break 

1135 

1136 yield BlameEntry( 

1137 commits[hexsha], 

1138 range(lineno, lineno + num_lines), 

1139 safe_decode(orig_filename), 

1140 range(orig_lineno, orig_lineno + num_lines), 

1141 ) 

1142 

1143 def blame( 

1144 self, 

1145 rev: Union[str, HEAD, None], 

1146 file: str, 

1147 incremental: bool = False, 

1148 rev_opts: Optional[List[str]] = None, 

1149 **kwargs: Any, 

1150 ) -> List[List[Commit | List[str | bytes] | None]] | Iterator[BlameEntry] | None: 

1151 """The blame information for the given file at the given revision. 

1152 

1153 :param rev: 

1154 Revision specifier. If ``None``, the blame will include all the latest 

1155 uncommitted changes. Otherwise, anything successfully parsed by 

1156 :manpage:`git-rev-parse(1)` is a valid option. 

1157 

1158 :return: 

1159 list: [git.Commit, list: [<line>]] 

1160 

1161 A list of lists associating a :class:`~git.objects.commit.Commit` object 

1162 with a list of lines that changed within the given commit. The 

1163 :class:`~git.objects.commit.Commit` objects will be given in order of 

1164 appearance. 

1165 """ 

1166 if incremental: 

1167 return self.blame_incremental(rev, file, **kwargs) 

1168 rev_opts = rev_opts or [] 

1169 data: bytes = self.git.blame(rev, *rev_opts, "--", file, p=True, stdout_as_string=False, **kwargs) 

1170 commits: Dict[str, Commit] = {} 

1171 blames: List[List[Commit | List[str | bytes] | None]] = [] 

1172 

1173 class InfoTD(TypedDict, total=False): 

1174 sha: str 

1175 id: str 

1176 filename: str 

1177 summary: str 

1178 author: str 

1179 author_email: str 

1180 author_date: int 

1181 committer: str 

1182 committer_email: str 

1183 committer_date: int 

1184 

1185 info: InfoTD = {} 

1186 

1187 keepends = True 

1188 for line_bytes in data.splitlines(keepends): 

1189 try: 

1190 line_str = line_bytes.rstrip().decode(defenc) 

1191 except UnicodeDecodeError: 

1192 firstpart = "" 

1193 parts = [] 

1194 is_binary = True 

1195 else: 

1196 # As we don't have an idea when the binary data ends, as it could 

1197 # contain multiple newlines in the process. So we rely on being able to 

1198 # decode to tell us what it is. This can absolutely fail even on text 

1199 # files, but even if it does, we should be fine treating it as binary 

1200 # instead. 

1201 parts = self.re_whitespace.split(line_str, 1) 

1202 firstpart = parts[0] 

1203 is_binary = False 

1204 # END handle decode of line 

1205 

1206 if self.re_hexsha_only.search(firstpart): 

1207 # handles 

1208 # 634396b2f541a9f2d58b00be1a07f0c358b999b3 1 1 7 - indicates blame-data start 

1209 # 634396b2f541a9f2d58b00be1a07f0c358b999b3 2 2 - indicates 

1210 # another line of blame with the same data 

1211 digits = parts[-1].split(" ") 

1212 if len(digits) == 3: 

1213 info = {"id": firstpart} 

1214 blames.append([None, []]) 

1215 elif info["id"] != firstpart: 

1216 info = {"id": firstpart} 

1217 blames.append([commits.get(firstpart), []]) 

1218 # END blame data initialization 

1219 else: 

1220 m = self.re_author_committer_start.search(firstpart) 

1221 if m: 

1222 # handles: 

1223 # author Tom Preston-Werner 

1224 # author-mail <tom@mojombo.com> 

1225 # author-time 1192271832 

1226 # author-tz -0700 

1227 # committer Tom Preston-Werner 

1228 # committer-mail <tom@mojombo.com> 

1229 # committer-time 1192271832 

1230 # committer-tz -0700 - IGNORED BY US 

1231 role = m.group(0) 

1232 if role == "author": 

1233 if firstpart.endswith("-mail"): 

1234 info["author_email"] = parts[-1] 

1235 elif firstpart.endswith("-time"): 

1236 info["author_date"] = int(parts[-1]) 

1237 elif role == firstpart: 

1238 info["author"] = parts[-1] 

1239 elif role == "committer": 

1240 if firstpart.endswith("-mail"): 

1241 info["committer_email"] = parts[-1] 

1242 elif firstpart.endswith("-time"): 

1243 info["committer_date"] = int(parts[-1]) 

1244 elif role == firstpart: 

1245 info["committer"] = parts[-1] 

1246 # END distinguish mail,time,name 

1247 else: 

1248 # handle 

1249 # filename lib/grit.rb 

1250 # summary add Blob 

1251 # <and rest> 

1252 if firstpart.startswith("filename"): 

1253 info["filename"] = parts[-1] 

1254 elif firstpart.startswith("summary"): 

1255 info["summary"] = parts[-1] 

1256 elif firstpart == "": 

1257 if info: 

1258 sha = info["id"] 

1259 c = commits.get(sha) 

1260 if c is None: 

1261 c = Commit( 

1262 self, 

1263 hex_to_bin(sha), 

1264 author=Actor._from_string(f"{info['author']} {info['author_email']}"), 

1265 authored_date=info["author_date"], 

1266 committer=Actor._from_string(f"{info['committer']} {info['committer_email']}"), 

1267 committed_date=info["committer_date"], 

1268 ) 

1269 commits[sha] = c 

1270 blames[-1][0] = c 

1271 # END if commit objects needs initial creation 

1272 

1273 if blames[-1][1] is not None: 

1274 line: str | bytes 

1275 if not is_binary: 

1276 if line_str and line_str[0] == "\t": 

1277 line_str = line_str[1:] 

1278 line = line_str 

1279 else: 

1280 line = line_bytes 

1281 # NOTE: We are actually parsing lines out of binary 

1282 # data, which can lead to the binary being split up 

1283 # along the newline separator. We will append this 

1284 # to the blame we are currently looking at, even 

1285 # though it should be concatenated with the last 

1286 # line we have seen. 

1287 blames[-1][1].append(line) 

1288 

1289 info = {"id": sha} 

1290 # END if we collected commit info 

1291 # END distinguish filename,summary,rest 

1292 # END distinguish author|committer vs filename,summary,rest 

1293 # END distinguish hexsha vs other information 

1294 return blames 

1295 

1296 @classmethod 

1297 def init( 

1298 cls, 

1299 path: Union[PathLike, None] = None, 

1300 mkdir: bool = True, 

1301 odbt: Type[GitCmdObjectDB] = GitCmdObjectDB, 

1302 expand_vars: bool = True, 

1303 **kwargs: Any, 

1304 ) -> "Repo": 

1305 """Initialize a git repository at the given path if specified. 

1306 

1307 :param path: 

1308 The full path to the repo (traditionally ends with ``/<name>.git``). Or 

1309 ``None``, in which case the repository will be created in the current 

1310 working directory. 

1311 

1312 :param mkdir: 

1313 If specified, will create the repository directory if it doesn't already 

1314 exist. Creates the directory with a mode=0755. 

1315 Only effective if a path is explicitly given. 

1316 

1317 :param odbt: 

1318 Object DataBase type - a type which is constructed by providing the 

1319 directory containing the database objects, i.e. ``.git/objects``. It will be 

1320 used to access all object data. 

1321 

1322 :param expand_vars: 

1323 If specified, environment variables will not be escaped. This can lead to 

1324 information disclosure, allowing attackers to access the contents of 

1325 environment variables. 

1326 

1327 :param kwargs: 

1328 Keyword arguments serving as additional options to the 

1329 :manpage:`git-init(1)` command. 

1330 

1331 :return: 

1332 :class:`Repo` (the newly created repo) 

1333 """ 

1334 if path: 

1335 path = expand_path(path, expand_vars) 

1336 if mkdir and path and not osp.exists(path): 

1337 os.makedirs(path, 0o755) 

1338 

1339 # git command automatically chdir into the directory 

1340 git = cls.GitCommandWrapperType(path) 

1341 git.init(**kwargs) 

1342 return cls(path, odbt=odbt) 

1343 

1344 @classmethod 

1345 def _clone( 

1346 cls, 

1347 git: "Git", 

1348 url: PathLike, 

1349 path: PathLike, 

1350 odb_default_type: Type[GitCmdObjectDB], 

1351 progress: Union["RemoteProgress", "UpdateProgress", Callable[..., "RemoteProgress"], None] = None, 

1352 multi_options: Optional[List[str]] = None, 

1353 allow_unsafe_protocols: bool = False, 

1354 allow_unsafe_options: bool = False, 

1355 **kwargs: Any, 

1356 ) -> "Repo": 

1357 odbt = kwargs.pop("odbt", odb_default_type) 

1358 

1359 # url may be a path and this has no effect if it is a string 

1360 url = os.fspath(url) 

1361 path = os.fspath(path) 

1362 

1363 ## A bug win cygwin's Git, when `--bare` or `--separate-git-dir` 

1364 # it prepends the cwd or(?) the `url` into the `path, so:: 

1365 # git clone --bare /cygwin/d/foo.git C:\\Work 

1366 # becomes:: 

1367 # git clone --bare /cygwin/d/foo.git /cygwin/d/C:\\Work 

1368 # 

1369 clone_path = Git.polish_url(path) if Git.is_cygwin() and "bare" in kwargs else path 

1370 sep_dir = kwargs.get("separate_git_dir") 

1371 if sep_dir: 

1372 kwargs["separate_git_dir"] = Git.polish_url(sep_dir) 

1373 multi = None 

1374 if multi_options: 

1375 multi = shlex.split(" ".join(multi_options)) 

1376 

1377 if not allow_unsafe_protocols: 

1378 Git.check_unsafe_protocols(url) 

1379 if not allow_unsafe_options: 

1380 Git.check_unsafe_options(options=list(kwargs.keys()), unsafe_options=cls.unsafe_git_clone_options) 

1381 if not allow_unsafe_options and multi_options: 

1382 Git.check_unsafe_options(options=multi_options, unsafe_options=cls.unsafe_git_clone_options) 

1383 

1384 proc = git.clone( 

1385 multi, 

1386 "--", 

1387 Git.polish_url(url), 

1388 clone_path, 

1389 with_extended_output=True, 

1390 as_process=True, 

1391 v=True, 

1392 universal_newlines=True, 

1393 **add_progress(kwargs, git, progress), 

1394 ) 

1395 if progress: 

1396 handle_process_output( 

1397 proc, 

1398 None, 

1399 to_progress_instance(progress).new_message_handler(), 

1400 finalize_process, 

1401 decode_streams=False, 

1402 ) 

1403 else: 

1404 (stdout, stderr) = proc.communicate() 

1405 cmdline = getattr(proc, "args", "") 

1406 cmdline = remove_password_if_present(cmdline) 

1407 

1408 _logger.debug("Cmd(%s)'s unused stdout: %s", cmdline, stdout) 

1409 finalize_process(proc, stderr=stderr) 

1410 

1411 # Our git command could have a different working dir than our actual 

1412 # environment, hence we prepend its working dir if required. 

1413 if not osp.isabs(path): 

1414 path = osp.join(git._working_dir, path) if git._working_dir is not None else path 

1415 

1416 repo = cls(path, odbt=odbt) 

1417 

1418 # Retain env values that were passed to _clone(). 

1419 repo.git.update_environment(**git.environment()) 

1420 

1421 # Adjust remotes - there may be operating systems which use backslashes, These 

1422 # might be given as initial paths, but when handling the config file that 

1423 # contains the remote from which we were clones, git stops liking it as it will 

1424 # escape the backslashes. Hence we undo the escaping just to be sure. 

1425 if repo.remotes: 

1426 with repo.remotes[0].config_writer as writer: 

1427 writer.set_value("url", Git.polish_url(repo.remotes[0].url)) 

1428 # END handle remote repo 

1429 return repo 

1430 

1431 def clone( 

1432 self, 

1433 path: PathLike, 

1434 progress: Optional[CallableProgress] = None, 

1435 multi_options: Optional[List[str]] = None, 

1436 allow_unsafe_protocols: bool = False, 

1437 allow_unsafe_options: bool = False, 

1438 **kwargs: Any, 

1439 ) -> "Repo": 

1440 """Create a clone from this repository. 

1441 

1442 :param path: 

1443 The full path of the new repo (traditionally ends with ``./<name>.git``). 

1444 

1445 :param progress: 

1446 See :meth:`Remote.push <git.remote.Remote.push>`. 

1447 

1448 :param multi_options: 

1449 A list of :manpage:`git-clone(1)` options that can be provided multiple 

1450 times. 

1451 

1452 One option per list item which is passed exactly as specified to clone. 

1453 For example:: 

1454 

1455 [ 

1456 "--config core.filemode=false", 

1457 "--config core.ignorecase", 

1458 "--recurse-submodule=repo1_path", 

1459 "--recurse-submodule=repo2_path", 

1460 ] 

1461 

1462 :param allow_unsafe_protocols: 

1463 Allow unsafe protocols to be used, like ``ext``. 

1464 

1465 :param allow_unsafe_options: 

1466 Allow unsafe options to be used, like ``--upload-pack``. 

1467 

1468 :param kwargs: 

1469 * ``odbt`` = ObjectDatabase Type, allowing to determine the object database 

1470 implementation used by the returned :class:`Repo` instance. 

1471 * All remaining keyword arguments are given to the :manpage:`git-clone(1)` 

1472 command. 

1473 

1474 :return: 

1475 :class:`Repo` (the newly cloned repo) 

1476 """ 

1477 return self._clone( 

1478 self.git, 

1479 self.common_dir, 

1480 path, 

1481 type(self.odb), 

1482 progress, # type: ignore[arg-type] 

1483 multi_options, 

1484 allow_unsafe_protocols=allow_unsafe_protocols, 

1485 allow_unsafe_options=allow_unsafe_options, 

1486 **kwargs, 

1487 ) 

1488 

1489 @classmethod 

1490 def clone_from( 

1491 cls, 

1492 url: PathLike, 

1493 to_path: PathLike, 

1494 progress: CallableProgress = None, 

1495 env: Optional[Mapping[str, str]] = None, 

1496 multi_options: Optional[List[str]] = None, 

1497 allow_unsafe_protocols: bool = False, 

1498 allow_unsafe_options: bool = False, 

1499 **kwargs: Any, 

1500 ) -> "Repo": 

1501 """Create a clone from the given URL. 

1502 

1503 :param url: 

1504 Valid git url, see: https://git-scm.com/docs/git-clone#URLS 

1505 

1506 :param to_path: 

1507 Path to which the repository should be cloned to. 

1508 

1509 :param progress: 

1510 See :meth:`Remote.push <git.remote.Remote.push>`. 

1511 

1512 :param env: 

1513 Optional dictionary containing the desired environment variables. 

1514 

1515 Note: Provided variables will be used to update the execution environment 

1516 for ``git``. If some variable is not specified in `env` and is defined in 

1517 :attr:`os.environ`, value from :attr:`os.environ` will be used. If you want 

1518 to unset some variable, consider providing empty string as its value. 

1519 

1520 :param multi_options: 

1521 See the :meth:`clone` method. 

1522 

1523 :param allow_unsafe_protocols: 

1524 Allow unsafe protocols to be used, like ``ext``. 

1525 

1526 :param allow_unsafe_options: 

1527 Allow unsafe options to be used, like ``--upload-pack``. 

1528 

1529 :param kwargs: 

1530 See the :meth:`clone` method. 

1531 

1532 :return: 

1533 :class:`Repo` instance pointing to the cloned directory. 

1534 """ 

1535 git = cls.GitCommandWrapperType(os.getcwd()) 

1536 if env is not None: 

1537 git.update_environment(**env) 

1538 return cls._clone( 

1539 git, 

1540 url, 

1541 to_path, 

1542 GitCmdObjectDB, 

1543 progress, # type: ignore[arg-type] 

1544 multi_options, 

1545 allow_unsafe_protocols=allow_unsafe_protocols, 

1546 allow_unsafe_options=allow_unsafe_options, 

1547 **kwargs, 

1548 ) 

1549 

1550 def archive( 

1551 self, 

1552 ostream: Union[TextIO, BinaryIO], 

1553 treeish: Optional[str] = None, 

1554 prefix: Optional[str] = None, 

1555 **kwargs: Any, 

1556 ) -> Repo: 

1557 """Archive the tree at the given revision. 

1558 

1559 :param ostream: 

1560 File-compatible stream object to which the archive will be written as bytes. 

1561 

1562 :param treeish: 

1563 The treeish name/id, defaults to active branch. 

1564 

1565 :param prefix: 

1566 The optional prefix to prepend to each filename in the archive. 

1567 

1568 :param kwargs: 

1569 Additional arguments passed to :manpage:`git-archive(1)`: 

1570 

1571 * Use the ``format`` argument to define the kind of format. Use specialized 

1572 ostreams to write any format supported by Python. 

1573 * You may specify the special ``path`` keyword, which may either be a 

1574 repository-relative path to a directory or file to place into the archive, 

1575 or a list or tuple of multiple paths. 

1576 

1577 :raise git.exc.GitCommandError: 

1578 If something went wrong. 

1579 

1580 :return: 

1581 self 

1582 """ 

1583 if treeish is None: 

1584 treeish = self.head.commit 

1585 if prefix and "prefix" not in kwargs: 

1586 kwargs["prefix"] = prefix 

1587 kwargs["output_stream"] = ostream 

1588 path = kwargs.pop("path", []) 

1589 path = cast(Union[PathLike, List[PathLike], Tuple[PathLike, ...]], path) 

1590 if not isinstance(path, (tuple, list)): 

1591 path = [path] 

1592 # END ensure paths is list (or tuple) 

1593 self.git.archive("--", treeish, *path, **kwargs) 

1594 return self 

1595 

1596 def has_separate_working_tree(self) -> bool: 

1597 """ 

1598 :return: 

1599 True if our :attr:`git_dir` is not at the root of our 

1600 :attr:`working_tree_dir`, but a ``.git`` file with a platform-agnostic 

1601 symbolic link. Our :attr:`git_dir` will be wherever the ``.git`` file points 

1602 to. 

1603 

1604 :note: 

1605 Bare repositories will always return ``False`` here. 

1606 """ 

1607 if self.bare: 

1608 return False 

1609 if self.working_tree_dir: 

1610 return osp.isfile(osp.join(self.working_tree_dir, ".git")) 

1611 else: 

1612 return False # Or raise Error? 

1613 

1614 rev_parse = rev_parse 

1615 

1616 def __repr__(self) -> str: 

1617 clazz = self.__class__ 

1618 return "<%s.%s %r>" % (clazz.__module__, clazz.__name__, self.git_dir) 

1619 

1620 def currently_rebasing_on(self) -> Commit | None: 

1621 """ 

1622 :return: 

1623 The commit which is currently being replayed while rebasing. 

1624 

1625 ``None`` if we are not currently rebasing. 

1626 """ 

1627 if self.git_dir: 

1628 rebase_head_file = osp.join(self.git_dir, "REBASE_HEAD") 

1629 if not osp.isfile(rebase_head_file): 

1630 return None 

1631 with open(rebase_head_file, "rt") as f: 

1632 content = f.readline().strip() 

1633 return self.commit(content)