Skip to content

Commit 192cc80

Browse files
committed
Check if branch exists before updating
1 parent a8f35f7 commit 192cc80

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

redbot/cogs/downloader/repo_manager.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class Repo(RepoJSONMixin):
126126
"git -C {path} log --diff-filter=D --pretty=format:%H -n 1 {descendant_rev}"
127127
" -- {module_name}/__init__.py"
128128
)
129+
GIT_VERIFY_BRANCH = "git -C {path} show-ref --quiet --verify refs/heads/{branch}"
129130

130131
PIP_INSTALL = "{python} -m pip install -U -t {target_dir} {reqs}"
131132

@@ -247,6 +248,25 @@ async def is_on_branch(self) -> bool:
247248
"""
248249
return await self.latest_commit() == self.commit
249250

251+
async def is_branch(self) -> bool:
252+
"""
253+
Check if the branch assosiated with the repo is a tag.
254+
255+
Returns
256+
-------
257+
bool
258+
`True` if branch is a tag or `False` otherwise
259+
"""
260+
git_command = ProcessFormatter().format(
261+
self.GIT_VERIFY_BRANCH, branch=self.branch, path=self.folder_path
262+
)
263+
p = await self._run(git_command)
264+
265+
return p.returncode == 0
266+
267+
268+
269+
250270
async def _get_file_update_statuses(
251271
self, old_rev: str, new_rev: Optional[str] = None
252272
) -> Dict[str, str]:
@@ -815,6 +835,9 @@ async def update(self) -> Tuple[str, str]:
815835
-------
816836
`UpdateError` - if git pull results with non-zero exit code
817837
"""
838+
if not await self.is_branch():
839+
return self.commit, self.commit
840+
818841
old_commit = await self.latest_commit()
819842

820843
await self.hard_reset()

tests/cogs/downloader/test_downloader.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ async def test_update(mocker, repo):
324324
new_commit = "a0ccc2390883c85a361f5a90c72e1b07958939fa"
325325
m = _mock_run(mocker, repo, 0)
326326
_mock_setup_repo(mocker, repo, new_commit)
327+
mocker.patch.object(repo, "is_branch", autospec=True, return_value=True)
327328
mocker.patch.object(repo, "latest_commit", autospec=True, return_value=old_commit)
328329
mocker.patch.object(repo, "hard_reset", autospec=True, return_value=None)
329330
ret = await repo.update()

0 commit comments

Comments
 (0)