summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/reset/GitNewResetDialog.java
blob: 58363b917992536b15931473934d1f0dca6c5107 (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
/*
 * Copyright 2000-2014 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.reset;

import com.intellij.dvcs.DvcsUtil;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBRadioButton;
import com.intellij.util.ui.GridBag;
import com.intellij.util.ui.RadioButtonEnumModel;
import com.intellij.util.ui.UIUtil;
import com.intellij.vcs.log.VcsFullCommitDetails;
import com.intellij.xml.util.XmlStringUtil;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

import static com.intellij.dvcs.DvcsUtil.getShortRepositoryName;

public class GitNewResetDialog extends DialogWrapper {

  private static final String DIALOG_ID = "git.new.reset.dialog";
  
  @NotNull private final Project myProject;
  @NotNull private final Map<GitRepository, VcsFullCommitDetails> myCommits;
  @NotNull private final GitResetMode myDefaultMode;
  @NotNull private final ButtonGroup myButtonGroup;

  private RadioButtonEnumModel<GitResetMode> myEnumModel;

  protected GitNewResetDialog(@NotNull Project project, @NotNull Map<GitRepository, VcsFullCommitDetails> commits,
                              @NotNull GitResetMode defaultMode) {
    super(project);
    myProject = project;
    myCommits = commits;
    myDefaultMode = defaultMode;
    myButtonGroup = new ButtonGroup();

    init();
    setTitle("Git Reset");
    setOKButtonText("Reset");
    setOKButtonMnemonic('R');
    setResizable(false);
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBag gb = new GridBag().
      setDefaultAnchor(GridBagConstraints.LINE_START).
      setDefaultInsets(0, UIUtil.DEFAULT_HGAP, UIUtil.LARGE_VGAP, 0);

    String description = prepareDescription(myProject, myCommits);
    panel.add(new JBLabel(XmlStringUtil.wrapInHtml(description)), gb.nextLine().next().coverLine());

    String explanation = "This will reset the current branch head to the selected commit, <br/>" +
                         "and update the working tree and the index according to the selected mode:";
    panel.add(new JBLabel(XmlStringUtil.wrapInHtml(explanation), UIUtil.ComponentStyle.SMALL), gb.nextLine().next().coverLine());

    for (GitResetMode mode : GitResetMode.values()) {
      JBRadioButton button = new JBRadioButton(mode.getName());
      button.setMnemonic(mode.getName().charAt(0));
      myButtonGroup.add(button);
      panel.add(button, gb.nextLine().next());
      panel.add(new JBLabel(XmlStringUtil.wrapInHtml(mode.getDescription()), UIUtil.ComponentStyle.SMALL), gb.next());
    }

    myEnumModel = RadioButtonEnumModel.bindEnum(GitResetMode.class, myButtonGroup);
    myEnumModel.setSelected(myDefaultMode);
    return panel;
  }

  @Nullable
  @Override
  protected String getHelpId() {
    return DIALOG_ID;
  }

  @NotNull
  private static String prepareDescription(@NotNull Project project, @NotNull Map<GitRepository, VcsFullCommitDetails> commits) {
    if (commits.size() == 1 && !isMultiRepo(project)) {
      Map.Entry<GitRepository, VcsFullCommitDetails> entry = commits.entrySet().iterator().next();
      return String.format("%s -> %s", getSourceText(entry.getKey()), getTargetText(entry.getValue()));
    }

    StringBuilder desc = new StringBuilder("");
    for (Map.Entry<GitRepository, VcsFullCommitDetails> entry : commits.entrySet()) {
      GitRepository repository = entry.getKey();
      VcsFullCommitDetails commit = entry.getValue();
      desc.append(String.format("%s in %s -> %s<br/>", getSourceText(repository),
                                getShortRepositoryName(repository), getTargetText(commit)));
    }
    return desc.toString();
  }

  @NotNull
  private static String getTargetText(@NotNull VcsFullCommitDetails commit) {
    String commitMessage = StringUtil.escapeXml(StringUtil.shortenTextWithEllipsis(commit.getSubject(), 20, 0));
    return String.format("<code><b>%s</b> \"%s\"</code> by <code>%s</code>",
                         commit.getId().toShortString(), commitMessage, commit.getAuthor().getName());
  }

  @NotNull
  private static String getSourceText(@NotNull GitRepository repository) {
    String currentRevision = repository.getCurrentRevision();
    assert currentRevision != null;
    String text = repository.getCurrentBranch() == null ?
                  "HEAD (" + DvcsUtil.getShortHash(currentRevision) + ")" :
                  repository.getCurrentBranch().getName();
    return "<b>" + text + "</b>";
  }

  private static boolean isMultiRepo(@NotNull Project project) {
    return ServiceManager.getService(project, GitRepositoryManager.class).moreThanOneRoot();
  }

  @NotNull
  public GitResetMode getResetMode() {
    return myEnumModel.getSelected();
  }

}