Skip to content

Commit

Permalink
Modules: If something is wrong with the local repo cache, offer to de…
Browse files Browse the repository at this point in the history
…lete it and try again

Closes nf-core#1850
  • Loading branch information
ewels committed Sep 29, 2022
1 parent 7e3c014 commit b097143
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Schema: Remove `allOf` if no definition groups are left.
- Use contextlib to temporarily change working directories ([#1819](https://github.com/nf-core/tools/pull/1819))
- More helpful error messages if `nf-core download` can't parse a singularity image download
- Modules: If something is wrong with the local repo cache, offer to delete it and try again ([#1850](https://github.com/nf-core/tools/pull/1850))

### Modules

Expand Down
104 changes: 57 additions & 47 deletions nf_core/modules/modules_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path

import git
import rich
import rich.progress
from git.exc import GitCommandError

Expand Down Expand Up @@ -150,55 +151,64 @@ def setup_local_repo(self, remote, branch, hide_progress=True):
Sets self.repo
"""
self.local_repo_dir = os.path.join(NFCORE_DIR, self.fullname)
if not os.path.exists(self.local_repo_dir):
try:
pbar = rich.progress.Progress(
"[bold blue]{task.description}",
rich.progress.BarColumn(bar_width=None),
"[bold yellow]{task.fields[state]}",
transient=True,
disable=hide_progress,
)
with pbar:
self.repo = git.Repo.clone_from(
remote,
self.local_repo_dir,
progress=RemoteProgressbar(pbar, self.fullname, self.remote_url, "Cloning"),
try:
if not os.path.exists(self.local_repo_dir):
try:
pbar = rich.progress.Progress(
"[bold blue]{task.description}",
rich.progress.BarColumn(bar_width=None),
"[bold yellow]{task.fields[state]}",
transient=True,
disable=hide_progress,
)
ModulesRepo.update_local_repo_status(self.fullname, True)
except GitCommandError:
raise LookupError(f"Failed to clone from the remote: `{remote}`")
# Verify that the requested branch exists by checking it out
self.setup_branch(branch)
else:
self.repo = git.Repo(self.local_repo_dir)

if ModulesRepo.no_pull_global:
ModulesRepo.update_local_repo_status(self.fullname, True)
# If the repo is already cloned, fetch the latest changes from the remote
if not ModulesRepo.local_repo_synced(self.fullname):
pbar = rich.progress.Progress(
"[bold blue]{task.description}",
rich.progress.BarColumn(bar_width=None),
"[bold yellow]{task.fields[state]}",
transient=True,
disable=hide_progress,
)
with pbar:
self.repo.remotes.origin.fetch(
progress=RemoteProgressbar(pbar, self.fullname, self.remote_url, "Pulling")
with pbar:
self.repo = git.Repo.clone_from(
remote,
self.local_repo_dir,
progress=RemoteProgressbar(pbar, self.fullname, self.remote_url, "Cloning"),
)
ModulesRepo.update_local_repo_status(self.fullname, True)
except GitCommandError:
raise LookupError(f"Failed to clone from the remote: `{remote}`")
# Verify that the requested branch exists by checking it out
self.setup_branch(branch)
else:
self.repo = git.Repo(self.local_repo_dir)

if ModulesRepo.no_pull_global:
ModulesRepo.update_local_repo_status(self.fullname, True)
# If the repo is already cloned, fetch the latest changes from the remote
if not ModulesRepo.local_repo_synced(self.fullname):
pbar = rich.progress.Progress(
"[bold blue]{task.description}",
rich.progress.BarColumn(bar_width=None),
"[bold yellow]{task.fields[state]}",
transient=True,
disable=hide_progress,
)
ModulesRepo.update_local_repo_status(self.fullname, True)

# Before verifying the branch, fetch the changes
# Verify that the requested branch exists by checking it out
self.setup_branch(branch)

# Now merge the changes
tracking_branch = self.repo.active_branch.tracking_branch()
if tracking_branch is None:
raise LookupError(f"There is no remote tracking branch '{self.branch}' in '{self.remote_url}'")
self.repo.git.merge(tracking_branch.name)
with pbar:
self.repo.remotes.origin.fetch(
progress=RemoteProgressbar(pbar, self.fullname, self.remote_url, "Pulling")
)
ModulesRepo.update_local_repo_status(self.fullname, True)

# Before verifying the branch, fetch the changes
# Verify that the requested branch exists by checking it out
self.setup_branch(branch)

# Now merge the changes
tracking_branch = self.repo.active_branch.tracking_branch()
if tracking_branch is None:
raise LookupError(f"There is no remote tracking branch '{self.branch}' in '{self.remote_url}'")
self.repo.git.merge(tracking_branch.name)
except GitCommandError as e:
log.error(f"[red]Could not set up local cache of modules repository:[/]\n{e}\n")
if rich.prompt.Confirm.ask(f"[violet]Delete local cache '{self.local_repo_dir}' and try again?"):
log.info(f"Removing '{self.local_repo_dir}'")
shutil.rmtree(self.local_repo_dir)
self.setup_local_repo(remote, branch, hide_progress)
else:
raise LookupError("Exiting due to error with local modules git repo")

def setup_branch(self, branch):
"""
Expand Down

0 comments on commit b097143

Please sign in to comment.