summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/cherrypick/GitCherryPickAction.java
blob: aa3e1e440277e5805b30594522da7cd72c4f2779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.cherrypick;

import com.intellij.dvcs.DvcsUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
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 com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.*;
import git4idea.GitLocalBranch;
import git4idea.GitPlatformFacade;
import git4idea.GitVcs;
import git4idea.commands.Git;
import git4idea.config.GitVcsSettings;
import git4idea.history.browser.GitHeavyCommit;
import git4idea.history.wholeTree.AbstractHash;
import git4idea.history.wholeTree.GitCommitDetailsProvider;
import git4idea.repo.GitRepository;
import icons.Git4ideaIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author Kirill Likhodedov
 */
public class GitCherryPickAction extends DumbAwareAction {
  private static final Logger LOG = Logger.getInstance(GitCherryPickAction.class);

  @NotNull private final GitPlatformFacade myPlatformFacade;
  @NotNull private final Git myGit;
  @NotNull private final Set<Hash> myIdsInProgress;

  public GitCherryPickAction() {
    super("Cherry-pick", "Cherry-pick", Git4ideaIcons.CherryPick);
    myGit = ServiceManager.getService(Git.class);
    myPlatformFacade = ServiceManager.getService(GitPlatformFacade.class);
    myIdsInProgress = ContainerUtil.newHashSet();
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    final List<? extends VcsFullCommitDetails> commits = getSelectedCommits(e);
    if (project == null || commits == null || commits.isEmpty()) {
      LOG.info(String.format("Cherry-pick action should be disabled. Project: %s, commits: %s", project, commits));
      return;
    }

    for (VcsFullCommitDetails commit : commits) {
      myIdsInProgress.add(commit.getId());
    }

    FileDocumentManager.getInstance().saveAllDocuments();
    myPlatformFacade.getChangeListManager(project).blockModalNotifications();

    new Task.Backgroundable(project, "Cherry-picking", false) {
      public void run(@NotNull ProgressIndicator indicator) {
        try {
          boolean autoCommit = GitVcsSettings.getInstance(myProject).isAutoCommitOnCherryPick();
          Map<GitRepository, List<VcsFullCommitDetails>> commitsInRoots = sortCommits(groupCommitsByRoots(project, commits));
          new GitCherryPicker(myProject, myGit, myPlatformFacade, autoCommit).cherryPick(commitsInRoots);
        }
        finally {
          ApplicationManager.getApplication().invokeLater(new Runnable() {
            public void run() {
              myPlatformFacade.getChangeListManager(project).unblockModalNotifications();
              for (VcsFullCommitDetails commit : commits) {
                myIdsInProgress.remove(commit.getId());
              }
            }
          });
        }
      }
    }.queue();
  }

  /**
   * Sort commits so that earliest ones come first: they need to be cherry-picked first.
   */
  @NotNull
  private static Map<GitRepository, List<VcsFullCommitDetails>> sortCommits(Map<GitRepository, List<VcsFullCommitDetails>> groupedCommits) {
    for (List<VcsFullCommitDetails> gitCommits : groupedCommits.values()) {
      Collections.reverse(gitCommits);
    }
    return groupedCommits;
  }

  @NotNull
  private Map<GitRepository, List<VcsFullCommitDetails>> groupCommitsByRoots(@NotNull Project project,
                                                                       @NotNull List<? extends VcsFullCommitDetails> commits) {
    Map<GitRepository, List<VcsFullCommitDetails>> groupedCommits = ContainerUtil.newHashMap();
    for (VcsFullCommitDetails commit : commits) {
      GitRepository repository = myPlatformFacade.getRepositoryManager(project).getRepositoryForRoot(commit.getRoot());
      if (repository == null) {
        LOG.info("No repository found for commit " + commit);
        continue;
      }
      List<VcsFullCommitDetails> commitsInRoot = groupedCommits.get(repository);
      if (commitsInRoot == null) {
        commitsInRoot = ContainerUtil.newArrayList();
        groupedCommits.put(repository, commitsInRoot);
      }
      commitsInRoot.add(commit);
    }
    return groupedCommits;
  }

  @Override
  public void update(AnActionEvent e) {
    super.update(e);
    final VcsLog log = getVcsLog(e);
    if (log != null && !DvcsUtil.logHasRootForVcs(log, GitVcs.getKey())) {
      e.getPresentation().setEnabledAndVisible(false);
    }
    else {
      e.getPresentation().setEnabled(enabled(e));
    }
  }

  private boolean enabled(AnActionEvent e) {
    final List<? extends VcsFullCommitDetails> commits = getSelectedCommits(e);
    final Project project = e.getProject();

    if (commits == null || commits.isEmpty() || project == null) {
      return false;
    }

    for (VcsFullCommitDetails commit : commits) {
      if (myIdsInProgress.contains(commit.getId())) {
        return false;
      }
      GitRepository repository = myPlatformFacade.getRepositoryManager(project).getRepositoryForRoot(commit.getRoot());
      if (repository == null) {
        return false;
      }
      GitLocalBranch currentBranch = repository.getCurrentBranch();
      Collection<String> containingBranches = getContainingBranches(e, commit, repository);
      if (currentBranch != null &&  containingBranches != null && containingBranches.contains(currentBranch.getName())) {
        // already is contained in the current branch
        return false;
      }
    }
    return true;
  }

  // TODO remove after removing the old Vcs Log implementation
  @Nullable
  private List<? extends VcsFullCommitDetails> getSelectedCommits(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null) {
      return null;
    }
    List<GitHeavyCommit> commits = e.getData(GitVcs.SELECTED_COMMITS);
    if (commits != null) {
      return convertHeavyCommitToFullDetails(commits, project);
    }
    final VcsLog log = getVcsLog(e);
    if (log == null) {
      return null;
    }

    List<Hash> selectedCommits = log.getSelectedCommits();
    List<VcsFullCommitDetails> selectedDetails = ContainerUtil.newArrayList();
    for (Hash commit : selectedCommits) {
      VcsFullCommitDetails details = log.getDetailsIfAvailable(commit);
      if (details == null) { // let the action be unavailable until all details are loaded
        return null;
      }
      GitRepository root = myPlatformFacade.getRepositoryManager(project).getRepositoryForRoot(details.getRoot());
      // don't allow to cherry-pick if a non-Git commit was selected
      // we could cherry-pick just Git commits filtered from the list, but it might provide confusion
      if (root == null) {
        return null;
      }
      selectedDetails.add(details);
    }
    return selectedDetails;
  }

  private static List<? extends VcsFullCommitDetails> convertHeavyCommitToFullDetails(List<GitHeavyCommit> commits, final Project project) {
    return ContainerUtil.map(commits, new Function<GitHeavyCommit, VcsFullCommitDetails>() {
      @Override
      public VcsFullCommitDetails fun(GitHeavyCommit commit) {
        final VcsLogObjectsFactory factory = ServiceManager.getService(project, VcsLogObjectsFactory.class);
        List<Hash> parents = ContainerUtil.map(commit.getParentsHashes(), new Function<String, Hash>() {
          @Override
          public Hash fun(String hashValue) {
            return factory.createHash(hashValue);
          }
        });
        final List<Change> changes = commit.getChanges();
        return factory.createFullDetails(
          factory.createHash(commit.getHash().getValue()), parents, commit.getAuthorTime(), commit.getRoot(), commit.getSubject(),
          commit.getAuthor(), commit.getAuthorEmail(), commit.getDescription(), commit.getCommitter(), commit.getCommitterEmail(),
          commit.getDate().getTime(), new ThrowableComputable<Collection<Change>, Exception>() {
            @Override
            public Collection<Change> compute() throws Exception {
              return changes;
            }
          }
        );
      }
    });
  }

  private static VcsLog getVcsLog(@NotNull AnActionEvent event) {
    return event.getData(VcsLogDataKeys.VSC_LOG);
  }

  // TODO remove after removing the old Vcs Log implementation
  @Nullable
  private static Collection<String> getContainingBranches(AnActionEvent event, VcsFullCommitDetails commit, GitRepository repository) {
    GitCommitDetailsProvider detailsProvider = event.getData(GitVcs.COMMIT_DETAILS_PROVIDER);
    if (detailsProvider != null) {
      return detailsProvider.getContainingBranches(repository.getRoot(), AbstractHash.create(commit.getId().toShortString()));
    }
    if (event.getProject() == null) {
      return null;
    }
    VcsLog log = getVcsLog(event);
    if (log == null) {
      return null;
    }
    return log.getContainingBranches(commit.getId());
  }

}