/** * Fetches all specified roots. Once a root has failed, stops and displays the notification. If * needed, displays the successful notification at the end. * * @param roots roots to fetch. * @param errorNotificationTitle if specified, this notification title will be used instead of the * standard "Fetch failed". Use this when fetch is a part of a compound process. * @param notifySuccess if set to {@code true} successful notification will be displayed. * @return true if all fetches were successful, false if at least one fetch failed. */ public boolean fetchRootsAndNotify( @NotNull Collection<GitRepository> roots, @Nullable String errorNotificationTitle, boolean notifySuccess) { Map<VirtualFile, String> additionalInfo = new HashMap<VirtualFile, String>(); for (GitRepository repository : roots) { LOG.info("fetching " + repository); GitFetchResult result = fetch(repository); String ai = result.getAdditionalInfo(); if (!StringUtil.isEmptyOrSpaces(ai)) { additionalInfo.put(repository.getRoot(), ai); } if (!result.isSuccess()) { Collection<Exception> errors = new ArrayList<Exception>(getErrors()); errors.addAll(result.getErrors()); displayFetchResult(myProject, result, errorNotificationTitle, errors); return false; } } if (notifySuccess) { GitUIUtil.notifySuccess(myProject, "", "Fetched successfully"); } String addInfo = makeAdditionalInfoByRoot(additionalInfo); if (!StringUtil.isEmptyOrSpaces(addInfo)) { Notificator.getInstance(myProject) .notify( GitVcs.MINOR_NOTIFICATION, "Fetch details", addInfo, NotificationType.INFORMATION); } return true; }
@NotNull private static FetchParams getFetchParams(@NotNull GitRepository repository) { GitLocalBranch currentBranch = repository.getCurrentBranch(); if (currentBranch == null) { // fetching current branch is called from Update Project and Push, where branch tracking is // pre-checked String message = "Current branch can't be null here. \nRepository: " + repository; LOG.error(message); return new FetchParams(GitFetchResult.error(new Exception(message))); } GitBranchTrackInfo trackInfo = GitBranchUtil.getTrackInfoForBranch(repository, currentBranch); if (trackInfo == null) { String message = "Tracked info is null for branch " + currentBranch + "\n Repository: " + repository; LOG.error(message); return new FetchParams(GitFetchResult.error(new Exception(message))); } GitRemote remote = trackInfo.getRemote(); String url = remote.getFirstUrl(); if (url == null) { String message = "URL is null for remote " + remote.getName(); LOG.error(message); return new FetchParams(GitFetchResult.error(new Exception(message))); } return new FetchParams(remote, trackInfo.getRemoteBranch(), url); }
@NotNull private GitFetchResult fetchAll( @NotNull GitRepository repository, @NotNull GitFetchResult fetchResult) { for (GitRemote remote : repository.getRemotes()) { String url = remote.getFirstUrl(); if (url == null) { LOG.error("URL is null for remote " + remote.getName()); continue; } if (GitHttpAdapter.shouldUseJGit(url)) { GitFetchResult res = GitHttpAdapter.fetch(repository, remote, url, null); res.addPruneInfo(fetchResult.getPrunedRefs()); fetchResult = res; myErrors.addAll(fetchResult.getErrors()); if (!fetchResult.isSuccess()) { break; } } else { GitFetchResult res = fetchNatively(repository.getRoot(), remote, null); res.addPruneInfo(fetchResult.getPrunedRefs()); fetchResult = res; if (!fetchResult.isSuccess()) { break; } } } return fetchResult; }
/** * Invokes 'git fetch'. * * @return true if fetch was successful, false in the case of error. */ public GitFetchResult fetch(@NotNull GitRepository repository) { // TODO need to have a fair compound result here GitFetchResult fetchResult = GitFetchResult.success(); if (myFetchAll) { fetchResult = fetchAll(repository, fetchResult); } else { return fetchCurrentRemote(repository); } repository.update(); return fetchResult; }
public static void displayFetchResult( @NotNull Project project, @NotNull GitFetchResult result, @Nullable String errorNotificationTitle, @NotNull Collection<? extends Exception> errors) { if (result.isSuccess()) { GitVcs.NOTIFICATION_GROUP_ID .createNotification( "Fetched successfully" + result.getAdditionalInfo(), NotificationType.INFORMATION) .notify(project); } else if (result.isCancelled()) { GitVcs.NOTIFICATION_GROUP_ID .createNotification( "Fetch cancelled by user" + result.getAdditionalInfo(), NotificationType.WARNING) .notify(project); } else if (result.isNotAuthorized()) { String title; String description; if (errorNotificationTitle != null) { title = errorNotificationTitle; description = "Fetch failed: couldn't authorize"; } else { title = "Fetch failed"; description = "Couldn't authorize"; } description += result.getAdditionalInfo(); GitUIUtil.notifyMessage(project, title, description, NotificationType.ERROR, true, null); } else { GitVcs instance = GitVcs.getInstance(project); if (instance != null && instance.getExecutableValidator().isExecutableValid()) { GitUIUtil.notifyMessage( project, "Fetch failed", result.getAdditionalInfo(), NotificationType.ERROR, true, errors); } } }
private static GitFetchResult logError(@NotNull String message, @Nullable String additionalInfo) { String addInfo = additionalInfo != null ? "\n" + additionalInfo : ""; LOG.error(message + addInfo); return GitFetchResult.error(message); }