Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / ui / rollback / BackoutAction.java
blobf76e3f8ddfab7dd465827c5d7f0b105ba65605ef
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.netbeans.modules.git.ui.rollback;
45 import java.awt.event.ActionEvent;
46 import java.io.File;
47 import java.util.List;
48 import javax.swing.Action;
49 import org.netbeans.modules.git.Git;
50 import org.netbeans.modules.git.GitException;
51 import org.netbeans.modules.git.GitProgressSupport;
52 import org.netbeans.modules.git.OutputLogger;
53 import org.netbeans.modules.git.ui.actions.ContextAction;
54 import org.netbeans.modules.git.ui.merge.MergeAction;
55 import org.netbeans.modules.git.util.GitCommand;
56 import org.netbeans.modules.git.util.GitUtils;
57 import org.netbeans.modules.versioning.spi.VCSContext;
58 import org.openide.DialogDisplayer;
59 import org.openide.NotifyDescriptor;
60 import org.openide.filesystems.FileObject;
61 import org.openide.filesystems.FileUtil;
62 import org.openide.util.NbBundle;
63 import org.openide.util.RequestProcessor;
66 /**
67 * Pull action for mercurial:
68 * hg pull - pull changes from the specified source
70 * @author John Rice
72 public class BackoutAction extends ContextAction {
74 private final VCSContext context;
75 private static final String HG_BACKOUT_REVISION_REPLACE = "\\{revision}";
76 public static final String HG_BACKOUT_REVISION = " {revision}";
78 public BackoutAction(String name, VCSContext context) {
79 this.context = context;
80 putValue(Action.NAME, name);
83 public void performAction(ActionEvent e) {
84 backout(context);
87 public static void backout(final VCSContext ctx){
88 final File root = GitUtils.getRootFile(ctx);
89 if (root == null) return;
90 String repository = root.getAbsolutePath();
92 String rev = null;
93 String commitMsg = null;
95 final Backout backout = new Backout(root);
96 if (!backout.showDialog()) {
97 return;
99 rev = backout.getSelectionRevision();
100 commitMsg = backout.getCommitMessage();
101 final boolean doMerge = false; // Now handling this using our own merge mechanism, not backout's
102 final String revStr = rev;
103 commitMsg = commitMsg.replaceAll(HG_BACKOUT_REVISION_REPLACE, revStr); //NOI18N
104 final String commitMsgStr = commitMsg;
106 RequestProcessor rp = Git.getInstance().getRequestProcessor(repository);
107 GitProgressSupport support = new GitProgressSupport() {
108 public void perform() {
110 OutputLogger logger = getLogger();
111 try {
112 logger.outputInRed(
113 NbBundle.getMessage(BackoutAction.class,
114 "MSG_BACKOUT_TITLE")); // NOI18N
115 logger.outputInRed(
116 NbBundle.getMessage(BackoutAction.class,
117 "MSG_BACKOUT_TITLE_SEP")); // NOI18N
118 logger.output(
119 NbBundle.getMessage(BackoutAction.class,
120 "MSG_BACKOUT_INFO_SEP", revStr, root.getAbsolutePath())); // NOI18N
121 List<String> list = GitCommand.doBackout(root, revStr, doMerge, commitMsgStr, logger);
123 if(list != null && !list.isEmpty()){
124 boolean bMergeNeededDueToBackout = GitCommand.isBackoutMergeNeededMsg(list.get(list.size() - 1));
125 if(bMergeNeededDueToBackout){
126 list.remove(list.size() - 1);
127 list.remove(list.size() - 1);
129 logger.output(list);
131 if(GitCommand.isUncommittedChangesBackout(list.get(0))){
132 logger.outputInRed(
133 NbBundle.getMessage(BackoutAction.class,
134 "MSG_UNCOMMITTED_CHANGES_BACKOUT")); // NOI18N
135 return;
136 } else if(GitCommand.isMergeChangesetBackout(list.get(0))){
137 logger.outputInRed(
138 NbBundle.getMessage(BackoutAction.class,
139 "MSG_MERGE_CSET_BACKOUT",revStr)); // NOI18N
140 return;
141 } else if(GitCommand.isNoRevStrip(list.get(0))){
142 logger.outputInRed(
143 NbBundle.getMessage(BackoutAction.class,
144 "MSG_NO_REV_BACKOUT",revStr)); // NOI18N
145 return;
148 // Handle Merge - both automatic and merge with conflicts
149 boolean bConfirmMerge = false;
150 if (bMergeNeededDueToBackout) {
151 bConfirmMerge = GitUtils.confirmDialog(
152 BackoutAction.class, "MSG_BACKOUT_MERGE_CONFIRM_TITLE", "MSG_BACKOUT_MERGE_CONFIRM_QUERY"); // NOI18N
154 if (bConfirmMerge) {
155 logger.output(""); // NOI18N
156 logger.outputInRed(NbBundle.getMessage(BackoutAction.class, "MSG_BACKOUT_MERGE_DO")); // NOI18N
157 MergeAction.doMergeAction(root, null, logger);
158 } else {
159 List<String> headRevList = GitCommand.getHeadRevisions(root);
160 if (headRevList != null && headRevList.size() > 1) {
161 MergeAction.printMergeWarning(headRevList, logger);
164 GitUtils.forceStatusRefreshProject(ctx);
165 // refresh filesystem to take account of deleted files.
166 FileObject rootObj = FileUtil.toFileObject(root);
167 try {
168 rootObj.getFileSystem().refresh(true);
169 } catch (java.lang.Exception ex) {
172 } catch (GitException ex) {
173 NotifyDescriptor.Exception e = new NotifyDescriptor.Exception(ex);
174 DialogDisplayer.getDefault().notifyLater(e);
175 } finally {
176 logger.outputInRed(
177 NbBundle.getMessage(BackoutAction.class,
178 "MSG_BACKOUT_DONE")); // NOI18N
179 logger.output(""); // NOI18N
183 support.start(rp, repository,org.openide.util.NbBundle.getMessage(BackoutAction.class, "MSG_BACKOUT_PROGRESS")); // NOI18N
186 @Override
187 public boolean isEnabled() {
188 return GitUtils.getRootFile(context) != null;