// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package git4idea.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import git4idea.GitVcs;
import org.jetbrains.annotations.NotNull;

import static git4idea.GitUtil.getRepositories;
import static git4idea.fetch.GitFetchSupport.fetchSupport;

public class GitFetch extends DumbAwareAction {

  @Override
  public void update(@NotNull AnActionEvent e) {
    super.update(e);
    Project project = e.getProject();
    if (project == null) {
      e.getPresentation().setEnabledAndVisible(false);
    }
    else {
      boolean hasRemotes = getRepositories(project).stream().anyMatch(repository -> !repository.getRemotes().isEmpty());
      e.getPresentation().setEnabledAndVisible(hasRemotes);
    }
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    GitVcs.runInBackground(new Task.Backgroundable(project, "Fetching...", true) {
      @Override
      public void run(@NotNull ProgressIndicator indicator) {
        fetchSupport(project).fetchAllRemotes(getRepositories(project)).showNotification();
      }
    });
  }
}

@@ -506,41 +506,168 @@
 ort 
-org.jetbrains.annotations.NotNull
+git4idea.fetch.GitFetchResult;%0Aimport org.jetbrains.annotations.CalledInAwt;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable
 ;%0A%0Ai
@@ -1447,24 +1447,54 @@
 .%22, true) %7B%0A
+      GitFetchResult result;%0A%0A
       @Overr
@@ -1566,16 +1566,25 @@
 %0A       
+ result =
  fetchSu
@@ -1639,16 +1639,251 @@
 roject))
+;%0A      %7D%0A%0A      @Override%0A      public void onFinished() %7B%0A        onFetchFinished(result);%0A      %7D%0A    %7D);%0A  %7D%0A%0A  @CalledInAwt%0A  protected void onFetchFinished(@Nullable GitFetchResult result) %7B%0A    if (result != null) %7B%0A      result
 .showNot
@@ -1895,30 +1895,21 @@
 tion();%0A    
-  %7D%0A    %7D);
+%7D%0A
 %0A  %7D%0A%7D%0A

extend GitFetch action - support custom onFinished callback