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

596 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 :raise ValueError: 

1046 If HEAD points to the ``.invalid`` ref Git uses to mark refs as 

1047 incompatible with older clients. 

1048 

1049 :return: 

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

1051 """ 

1052 active_branch = self.head.reference 

1053 if active_branch.name == ".invalid": 

1054 raise ValueError( 

1055 "HEAD points to 'refs/heads/.invalid', which Git uses to mark refs as incompatible with older clients" 

1056 ) 

1057 return active_branch 

1058 

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

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

1061 

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

1063 stream of :class:`BlameEntry` tuples. 

1064 

1065 :param rev: 

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

1067 uncommitted changes. Otherwise, anything successfully parsed by 

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

1069 

1070 :return: 

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

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

1073 the resulting file. 

1074 

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

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

1077 """ 

1078 

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

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

1081 

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

1083 while True: 

1084 try: 

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

1086 line = next(stream) 

1087 except StopIteration: 

1088 return 

1089 split_line = line.split() 

1090 hexsha, orig_lineno_b, lineno_b, num_lines_b = split_line 

1091 lineno = int(lineno_b) 

1092 num_lines = int(num_lines_b) 

1093 orig_lineno = int(orig_lineno_b) 

1094 if hexsha not in commits: 

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

1096 # commit. 

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

1098 while True: 

1099 try: 

1100 line = next(stream) 

1101 except StopIteration: 

1102 return 

1103 if line == b"boundary": 

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

1105 # "previous" tag. 

1106 continue 

1107 

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

1109 props[tag] = value 

1110 if tag == b"filename": 

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

1112 orig_filename = value 

1113 break 

1114 

1115 c = Commit( 

1116 self, 

1117 hex_to_bin(hexsha), 

1118 author=Actor( 

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

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

1121 ), 

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

1123 committer=Actor( 

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

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

1126 ), 

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

1128 ) 

1129 commits[hexsha] = c 

1130 else: 

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

1132 # the last line. 

1133 while True: 

1134 try: 

1135 # Will fail if we reach the EOF unexpectedly. 

1136 line = next(stream) 

1137 except StopIteration: 

1138 return 

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

1140 if tag == b"filename": 

1141 orig_filename = value 

1142 break 

1143 

1144 yield BlameEntry( 

1145 commits[hexsha], 

1146 range(lineno, lineno + num_lines), 

1147 safe_decode(orig_filename), 

1148 range(orig_lineno, orig_lineno + num_lines), 

1149 ) 

1150 

1151 def blame( 

1152 self, 

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

1154 file: str, 

1155 incremental: bool = False, 

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

1157 **kwargs: Any, 

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

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

1160 

1161 :param rev: 

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

1163 uncommitted changes. Otherwise, anything successfully parsed by 

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

1165 

1166 :return: 

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

1168 

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

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

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

1172 appearance. 

1173 """ 

1174 if incremental: 

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

1176 rev_opts = rev_opts or [] 

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

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

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

1180 

1181 class InfoTD(TypedDict, total=False): 

1182 sha: str 

1183 id: str 

1184 filename: str 

1185 summary: str 

1186 author: str 

1187 author_email: str 

1188 author_date: int 

1189 committer: str 

1190 committer_email: str 

1191 committer_date: int 

1192 

1193 info: InfoTD = {} 

1194 

1195 keepends = True 

1196 for line_bytes in data.splitlines(keepends): 

1197 try: 

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

1199 except UnicodeDecodeError: 

1200 firstpart = "" 

1201 parts = [] 

1202 is_binary = True 

1203 else: 

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

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

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

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

1208 # instead. 

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

1210 firstpart = parts[0] 

1211 is_binary = False 

1212 # END handle decode of line 

1213 

1214 if self.re_hexsha_only.search(firstpart): 

1215 # handles 

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

1217 # 634396b2f541a9f2d58b00be1a07f0c358b999b3 2 2 - indicates 

1218 # another line of blame with the same data 

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

1220 if len(digits) == 3: 

1221 info = {"id": firstpart} 

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

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

1224 info = {"id": firstpart} 

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

1226 # END blame data initialization 

1227 else: 

1228 m = self.re_author_committer_start.search(firstpart) 

1229 if m: 

1230 # handles: 

1231 # author Tom Preston-Werner 

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

1233 # author-time 1192271832 

1234 # author-tz -0700 

1235 # committer Tom Preston-Werner 

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

1237 # committer-time 1192271832 

1238 # committer-tz -0700 - IGNORED BY US 

1239 role = m.group(0) 

1240 if role == "author": 

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

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

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

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

1245 elif role == firstpart: 

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

1247 elif role == "committer": 

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

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

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

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

1252 elif role == firstpart: 

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

1254 # END distinguish mail,time,name 

1255 else: 

1256 # handle 

1257 # filename lib/grit.rb 

1258 # summary add Blob 

1259 # <and rest> 

1260 if firstpart.startswith("filename"): 

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

1262 elif firstpart.startswith("summary"): 

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

1264 elif firstpart == "": 

1265 if info: 

1266 sha = info["id"] 

1267 c = commits.get(sha) 

1268 if c is None: 

1269 c = Commit( 

1270 self, 

1271 hex_to_bin(sha), 

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

1273 authored_date=info["author_date"], 

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

1275 committed_date=info["committer_date"], 

1276 ) 

1277 commits[sha] = c 

1278 blames[-1][0] = c 

1279 # END if commit objects needs initial creation 

1280 

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

1282 line: str | bytes 

1283 if not is_binary: 

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

1285 line_str = line_str[1:] 

1286 line = line_str 

1287 else: 

1288 line = line_bytes 

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

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

1291 # along the newline separator. We will append this 

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

1293 # though it should be concatenated with the last 

1294 # line we have seen. 

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

1296 

1297 info = {"id": sha} 

1298 # END if we collected commit info 

1299 # END distinguish filename,summary,rest 

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

1301 # END distinguish hexsha vs other information 

1302 return blames 

1303 

1304 @classmethod 

1305 def init( 

1306 cls, 

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

1308 mkdir: bool = True, 

1309 odbt: Type[GitCmdObjectDB] = GitCmdObjectDB, 

1310 expand_vars: bool = True, 

1311 **kwargs: Any, 

1312 ) -> "Repo": 

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

1314 

1315 :param path: 

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

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

1318 working directory. 

1319 

1320 :param mkdir: 

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

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

1323 Only effective if a path is explicitly given. 

1324 

1325 :param odbt: 

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

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

1328 used to access all object data. 

1329 

1330 :param expand_vars: 

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

1332 information disclosure, allowing attackers to access the contents of 

1333 environment variables. 

1334 

1335 :param kwargs: 

1336 Keyword arguments serving as additional options to the 

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

1338 

1339 :return: 

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

1341 """ 

1342 if path: 

1343 path = expand_path(path, expand_vars) 

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

1345 os.makedirs(path, 0o755) 

1346 

1347 # git command automatically chdir into the directory 

1348 git = cls.GitCommandWrapperType(path) 

1349 git.init(**kwargs) 

1350 return cls(path, odbt=odbt) 

1351 

1352 @classmethod 

1353 def _clone( 

1354 cls, 

1355 git: "Git", 

1356 url: PathLike, 

1357 path: PathLike, 

1358 odb_default_type: Type[GitCmdObjectDB], 

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

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

1361 allow_unsafe_protocols: bool = False, 

1362 allow_unsafe_options: bool = False, 

1363 **kwargs: Any, 

1364 ) -> "Repo": 

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

1366 

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

1368 url = os.fspath(url) 

1369 path = os.fspath(path) 

1370 

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

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

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

1374 # becomes:: 

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

1376 # 

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

1378 sep_dir = kwargs.get("separate_git_dir") 

1379 if sep_dir: 

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

1381 multi = None 

1382 if multi_options: 

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

1384 

1385 if not allow_unsafe_protocols: 

1386 Git.check_unsafe_protocols(url) 

1387 if not allow_unsafe_options: 

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

1389 if not allow_unsafe_options and multi: 

1390 Git.check_unsafe_options(options=multi, unsafe_options=cls.unsafe_git_clone_options) 

1391 

1392 proc = git.clone( 

1393 multi, 

1394 "--", 

1395 Git.polish_url(url), 

1396 clone_path, 

1397 with_extended_output=True, 

1398 as_process=True, 

1399 v=True, 

1400 universal_newlines=True, 

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

1402 ) 

1403 if progress: 

1404 handle_process_output( 

1405 proc, 

1406 None, 

1407 to_progress_instance(progress).new_message_handler(), 

1408 finalize_process, 

1409 decode_streams=False, 

1410 ) 

1411 else: 

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

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

1414 cmdline = remove_password_if_present(cmdline) 

1415 

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

1417 finalize_process(proc, stderr=stderr) 

1418 

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

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

1421 if not osp.isabs(path): 

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

1423 

1424 repo = cls(path, odbt=odbt) 

1425 

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

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

1428 

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

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

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

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

1433 if repo.remotes: 

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

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

1436 # END handle remote repo 

1437 return repo 

1438 

1439 def clone( 

1440 self, 

1441 path: PathLike, 

1442 progress: Optional[CallableProgress] = None, 

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

1444 allow_unsafe_protocols: bool = False, 

1445 allow_unsafe_options: bool = False, 

1446 **kwargs: Any, 

1447 ) -> "Repo": 

1448 """Create a clone from this repository. 

1449 

1450 :param path: 

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

1452 

1453 :param progress: 

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

1455 

1456 :param multi_options: 

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

1458 times. 

1459 

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

1461 For example:: 

1462 

1463 [ 

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

1465 "--config core.ignorecase", 

1466 "--recurse-submodule=repo1_path", 

1467 "--recurse-submodule=repo2_path", 

1468 ] 

1469 

1470 :param allow_unsafe_protocols: 

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

1472 

1473 :param allow_unsafe_options: 

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

1475 

1476 :param kwargs: 

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

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

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

1480 command. 

1481 

1482 :return: 

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

1484 """ 

1485 return self._clone( 

1486 self.git, 

1487 self.common_dir, 

1488 path, 

1489 type(self.odb), 

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

1491 multi_options, 

1492 allow_unsafe_protocols=allow_unsafe_protocols, 

1493 allow_unsafe_options=allow_unsafe_options, 

1494 **kwargs, 

1495 ) 

1496 

1497 @classmethod 

1498 def clone_from( 

1499 cls, 

1500 url: PathLike, 

1501 to_path: PathLike, 

1502 progress: CallableProgress = None, 

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

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

1505 allow_unsafe_protocols: bool = False, 

1506 allow_unsafe_options: bool = False, 

1507 **kwargs: Any, 

1508 ) -> "Repo": 

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

1510 

1511 :param url: 

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

1513 

1514 :param to_path: 

1515 Path to which the repository should be cloned to. 

1516 

1517 :param progress: 

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

1519 

1520 :param env: 

1521 Optional dictionary containing the desired environment variables. 

1522 

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

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

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

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

1527 

1528 :param multi_options: 

1529 See the :meth:`clone` method. 

1530 

1531 :param allow_unsafe_protocols: 

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

1533 

1534 :param allow_unsafe_options: 

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

1536 

1537 :param kwargs: 

1538 See the :meth:`clone` method. 

1539 

1540 :return: 

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

1542 """ 

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

1544 if env is not None: 

1545 git.update_environment(**env) 

1546 return cls._clone( 

1547 git, 

1548 url, 

1549 to_path, 

1550 GitCmdObjectDB, 

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

1552 multi_options, 

1553 allow_unsafe_protocols=allow_unsafe_protocols, 

1554 allow_unsafe_options=allow_unsafe_options, 

1555 **kwargs, 

1556 ) 

1557 

1558 def archive( 

1559 self, 

1560 ostream: Union[TextIO, BinaryIO], 

1561 treeish: Optional[str] = None, 

1562 prefix: Optional[str] = None, 

1563 **kwargs: Any, 

1564 ) -> Repo: 

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

1566 

1567 :param ostream: 

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

1569 

1570 :param treeish: 

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

1572 

1573 :param prefix: 

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

1575 

1576 :param kwargs: 

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

1578 

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

1580 ostreams to write any format supported by Python. 

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

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

1583 or a list or tuple of multiple paths. 

1584 

1585 :raise git.exc.GitCommandError: 

1586 If something went wrong. 

1587 

1588 :return: 

1589 self 

1590 """ 

1591 if treeish is None: 

1592 treeish = self.head.commit 

1593 if prefix and "prefix" not in kwargs: 

1594 kwargs["prefix"] = prefix 

1595 kwargs["output_stream"] = ostream 

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

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

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

1599 path = [path] 

1600 # END ensure paths is list (or tuple) 

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

1602 return self 

1603 

1604 def has_separate_working_tree(self) -> bool: 

1605 """ 

1606 :return: 

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

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

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

1610 to. 

1611 

1612 :note: 

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

1614 """ 

1615 if self.bare: 

1616 return False 

1617 if self.working_tree_dir: 

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

1619 else: 

1620 return False # Or raise Error? 

1621 

1622 rev_parse = rev_parse 

1623 

1624 def __repr__(self) -> str: 

1625 clazz = self.__class__ 

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

1627 

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

1629 """ 

1630 :return: 

1631 The commit which is currently being replayed while rebasing. 

1632 

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

1634 """ 

1635 if self.git_dir: 

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

1637 if not osp.isfile(rebase_head_file): 

1638 return None 

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

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

1641 return self.commit(content)