Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/git/objects/submodule/util.py: 34%
51 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import git
2from git.exc import InvalidGitRepositoryError
3from git.config import GitConfigParser
4from io import BytesIO
5import weakref
8# typing -----------------------------------------------------------------------
10from typing import Any, Sequence, TYPE_CHECKING, Union
12from git.types import PathLike
14if TYPE_CHECKING: 14 ↛ 15line 14 didn't jump to line 15, because the condition on line 14 was never true
15 from .base import Submodule
16 from weakref import ReferenceType
17 from git.repo import Repo
18 from git.refs import Head
19 from git import Remote
20 from git.refs import RemoteReference
23__all__ = (
24 "sm_section",
25 "sm_name",
26 "mkhead",
27 "find_first_remote_branch",
28 "SubmoduleConfigParser",
29)
31# { Utilities
34def sm_section(name: str) -> str:
35 """:return: section title used in .gitmodules configuration file"""
36 return f'submodule "{name}"'
39def sm_name(section: str) -> str:
40 """:return: name of the submodule as parsed from the section name"""
41 section = section.strip()
42 return section[11:-1]
45def mkhead(repo: "Repo", path: PathLike) -> "Head":
46 """:return: New branch/head instance"""
47 return git.Head(repo, git.Head.to_full_path(path))
50def find_first_remote_branch(remotes: Sequence["Remote"], branch_name: str) -> "RemoteReference":
51 """Find the remote branch matching the name of the given branch or raise InvalidGitRepositoryError"""
52 for remote in remotes:
53 try:
54 return remote.refs[branch_name]
55 except IndexError:
56 continue
57 # END exception handling
58 # END for remote
59 raise InvalidGitRepositoryError("Didn't find remote branch '%r' in any of the given remotes" % branch_name)
62# } END utilities
65# { Classes
68class SubmoduleConfigParser(GitConfigParser):
70 """
71 Catches calls to _write, and updates the .gitmodules blob in the index
72 with the new data, if we have written into a stream. Otherwise it will
73 add the local file to the index to make it correspond with the working tree.
74 Additionally, the cache must be cleared
76 Please note that no mutating method will work in bare mode
77 """
79 def __init__(self, *args: Any, **kwargs: Any) -> None:
80 self._smref: Union["ReferenceType[Submodule]", None] = None
81 self._index = None
82 self._auto_write = True
83 super(SubmoduleConfigParser, self).__init__(*args, **kwargs)
85 # { Interface
86 def set_submodule(self, submodule: "Submodule") -> None:
87 """Set this instance's submodule. It must be called before
88 the first write operation begins"""
89 self._smref = weakref.ref(submodule)
91 def flush_to_index(self) -> None:
92 """Flush changes in our configuration file to the index"""
93 assert self._smref is not None
94 # should always have a file here
95 assert not isinstance(self._file_or_files, BytesIO)
97 sm = self._smref()
98 if sm is not None:
99 index = self._index
100 if index is None:
101 index = sm.repo.index
102 # END handle index
103 index.add([sm.k_modules_file], write=self._auto_write)
104 sm._clear_cache()
105 # END handle weakref
107 # } END interface
109 # { Overridden Methods
110 def write(self) -> None: # type: ignore[override]
111 rval: None = super(SubmoduleConfigParser, self).write()
112 self.flush_to_index()
113 return rval
115 # END overridden methods
118# } END classes