Skip to content

Commit 8814e4b

Browse files
committed
Instrument git_submodule_set_url.
This commit adds a setter for the url property of the Submodule class. The new setter leverages 'git_submodule_set_url' from libgit2.
1 parent 6caa06d commit 8814e4b

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pygit2/decl/submodule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ const char * git_submodule_branch(git_submodule *submodule);
4141
const git_oid * git_submodule_head_id(git_submodule *submodule);
4242

4343
int git_submodule_status(unsigned int *status, git_repository *repo, const char *name, git_submodule_ignore_t ignore);
44+
45+
int git_submodule_set_url(git_repository *repo, const char *name, const char *url);

pygit2/submodules.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ def url(self) -> Union[str, None]:
152152
url = C.git_submodule_url(self._subm)
153153
return maybe_string(url)
154154

155+
@url.setter
156+
def url(self, url: str) -> None:
157+
crepo = self._repo._repo
158+
cname = ffi.new('char[]', to_bytes(self.name))
159+
curl = ffi.new('char[]', to_bytes(url))
160+
err = C.git_submodule_set_url(crepo, cname, curl)
161+
check_error(err)
162+
155163
@property
156164
def branch(self):
157165
"""Branch that is to be tracked by the submodule."""

test/test_submodule.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ def test_url(repo: Repository) -> None:
123123
s = repo.submodules[SUBM_PATH]
124124
assert SUBM_URL == s.url
125125

126+
def test_set_url(repo: Repository) -> None:
127+
new_url = 'ssh://[email protected]:2222/my_repo'
128+
s = repo.submodules[SUBM_PATH]
129+
s.url = new_url
130+
assert new_url == repo.submodules[SUBM_PATH].url
131+
# Ensure .gitmodules has been correctly altered
132+
with open(Path(repo.workdir, '.gitmodules'), 'r') as fd:
133+
modules = fd.read()
134+
assert new_url in modules
126135

127136
def test_missing_url(repo: Repository) -> None:
128137
# Remove "url" from .gitmodules

0 commit comments

Comments
 (0)