Skip to content

Commit 7cfc651

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 7cfc651

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ def test_url(repo: Repository) -> None:
124124
assert SUBM_URL == s.url
125125

126126

127+
def test_set_url(repo: Repository) -> None:
128+
new_url = 'ssh://[email protected]:2222/my_repo'
129+
s = repo.submodules[SUBM_PATH]
130+
s.url = new_url
131+
assert new_url == repo.submodules[SUBM_PATH].url
132+
# Ensure .gitmodules has been correctly altered
133+
with open(Path(repo.workdir, '.gitmodules'), 'r') as fd:
134+
modules = fd.read()
135+
assert new_url in modules
136+
137+
127138
def test_missing_url(repo: Repository) -> None:
128139
# Remove "url" from .gitmodules
129140
with open(Path(repo.workdir, '.gitmodules'), 'wt') as f:

0 commit comments

Comments
 (0)