001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mercurial.ui.merge;
042:
043: import java.io.File;
044: import java.util.List;
045: import org.netbeans.modules.mercurial.HgException;
046: import org.netbeans.modules.mercurial.Mercurial;
047: import org.netbeans.modules.mercurial.OutputLogger;
048: import org.netbeans.modules.versioning.spi.VCSContext;
049: import javax.swing.*;
050: import java.awt.event.ActionEvent;
051: import java.util.Set;
052: import org.netbeans.modules.mercurial.util.HgCommand;
053: import org.netbeans.modules.mercurial.util.HgUtils;
054: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
055: import org.openide.DialogDisplayer;
056: import org.openide.NotifyDescriptor;
057: import org.openide.util.NbBundle;
058: import org.openide.util.RequestProcessor;
059: import org.netbeans.modules.mercurial.HgProgressSupport;
060: import org.openide.util.Utilities;
061: import org.openide.windows.OutputWriter;
062:
063: /**
064: * Merge action for mercurial:
065: * hg merge - attempts to merge changes when the repository has 2 heads
066: *
067: * @author John Rice
068: */
069: public class MergeAction extends ContextAction {
070:
071: private final VCSContext context;
072: private final static int MULTIPLE_AUTOMERGE_HEAD_LIMIT = 2;
073:
074: public MergeAction(String name, VCSContext context) {
075: this .context = context;
076: putValue(Action.NAME, name);
077: }
078:
079: public boolean isEnabled() {
080: Set<File> ctxFiles = context != null ? context.getRootFiles()
081: : null;
082: if (HgUtils.getRootFile(context) == null || ctxFiles == null
083: || ctxFiles.size() == 0)
084: return false;
085: return true; // #121293: Speed up menu display, warn user if nothing to merge when Merge selected
086: }
087:
088: public void performAction(ActionEvent ev) {
089: final File root = HgUtils.getRootFile(context);
090: if (root == null) {
091: OutputLogger logger = OutputLogger
092: .getLogger(Mercurial.MERCURIAL_OUTPUT_TAB_TITLE);
093: logger.outputInRed(NbBundle.getMessage(MergeAction.class,
094: "MSG_MERGE_TITLE")); // NOI18N
095: logger.outputInRed(NbBundle.getMessage(MergeAction.class,
096: "MSG_MERGE_TITLE_SEP")); // NOI18N
097: logger.outputInRed(NbBundle.getMessage(MergeAction.class,
098: "MSG_MERGE_NOT_SUPPORTED_INVIEW_INFO")); // NOI18N
099: logger.output(""); // NOI18N
100: logger.closeLog();
101: JOptionPane.showMessageDialog(null, NbBundle
102: .getMessage(MergeAction.class,
103: "MSG_MERGE_NOT_SUPPORTED_INVIEW"),// NOI18N
104: NbBundle.getMessage(MergeAction.class,
105: "MSG_MERGE_NOT_SUPPORTED_INVIEW_TITLE"),// NOI18N
106: JOptionPane.INFORMATION_MESSAGE);
107: return;
108: }
109: String repository = root.getAbsolutePath();
110: RequestProcessor rp = Mercurial.getInstance()
111: .getRequestProcessor(repository);
112: HgProgressSupport support = new HgProgressSupport() {
113: public void perform() {
114: OutputLogger logger = getLogger();
115: try {
116: List<String> headList = HgCommand
117: .getHeadRevisions(root);
118: String revStr = null;
119: if (headList.size() <= 1) {
120: logger.outputInRed(NbBundle.getMessage(
121: MergeAction.class, "MSG_MERGE_TITLE")); // NOI18N
122: logger.outputInRed(NbBundle.getMessage(
123: MergeAction.class,
124: "MSG_MERGE_TITLE_SEP")); // NOI18N
125: logger.output(NbBundle.getMessage(
126: MergeAction.class,
127: "MSG_NOTHING_TO_MERGE")); // NOI18N
128: logger.outputInRed(NbBundle.getMessage(
129: MergeAction.class, "MSG_MERGE_DONE")); // NOI18N
130: logger.output(""); // NOI18N
131: JOptionPane.showMessageDialog(null, NbBundle
132: .getMessage(MergeAction.class,
133: "MSG_NOTHING_TO_MERGE"),// NOI18N
134: NbBundle.getMessage(MergeAction.class,
135: "MSG_MERGE_TITLE"),// NOI18N
136: JOptionPane.INFORMATION_MESSAGE);
137: return;
138: } else if (headList.size() > MULTIPLE_AUTOMERGE_HEAD_LIMIT) {
139: final MergeRevisions mergeDlg = new MergeRevisions(
140: root);
141: if (!mergeDlg.showDialog()) {
142: return;
143: }
144: revStr = mergeDlg.getSelectionRevision();
145: }
146: logger.outputInRed(NbBundle.getMessage(
147: MergeAction.class, "MSG_MERGE_TITLE")); // NOI18N
148: logger.outputInRed(NbBundle.getMessage(
149: MergeAction.class, "MSG_MERGE_TITLE_SEP")); // NOI18N
150: doMergeAction(root, revStr, logger);
151: HgUtils.forceStatusRefreshProject(context);
152: logger.output(""); // NOI18N
153: } catch (HgException ex) {
154: NotifyDescriptor.Exception e = new NotifyDescriptor.Exception(
155: ex);
156: DialogDisplayer.getDefault().notifyLater(e);
157: }
158: }
159: };
160: support.start(rp, repository, NbBundle.getMessage(
161: MergeAction.class, "MSG_MERGE_PROGRESS")); // NOI18N
162: }
163:
164: public static boolean doMergeAction(File root, String revStr,
165: OutputLogger logger) throws HgException {
166: List<String> listMerge = HgCommand.doMerge(root, revStr);
167: Boolean bConflicts = false;
168: Boolean bMergeFailed = false;
169:
170: if (listMerge != null && !listMerge.isEmpty()) {
171: logger.output(listMerge);
172: for (String line : listMerge) {
173: if (HgCommand.isMergeAbortUncommittedMsg(line)) {
174: bMergeFailed = true;
175: logger.outputInRed(NbBundle.getMessage(
176: MergeAction.class, "MSG_MERGE_FAILED")); // NOI18N
177: JOptionPane.showMessageDialog(null, NbBundle
178: .getMessage(MergeAction.class,
179: "MSG_MERGE_UNCOMMITTED"), // NOI18N
180: NbBundle.getMessage(MergeAction.class,
181: "MSG_MERGE_TITLE"), // NOI18N
182: JOptionPane.WARNING_MESSAGE);
183: break;
184: }
185:
186: if (HgCommand.isMergeAbortMultipleHeadsMsg(line)) {
187: bMergeFailed = true;
188: logger.outputInRed(NbBundle.getMessage(
189: MergeAction.class, "MSG_MERGE_FAILED")); // NOI18N
190: break;
191: }
192: if (HgCommand.isMergeConflictMsg(line)) {
193: bConflicts = true;
194: String filepath = null;
195: if (Utilities.isWindows()) {
196: filepath = line
197: .substring(
198: HgCommand.HG_MERGE_CONFLICT_WIN1_ERR
199: .length(),
200: line.length()
201: - HgCommand.HG_MERGE_CONFLICT_WIN2_ERR
202: .length())
203: .trim().replace("/", "\\"); // NOI18N
204: filepath = root.getAbsolutePath()
205: + File.separator + filepath;
206: } else {
207: filepath = line
208: .substring(HgCommand.HG_MERGE_CONFLICT_ERR
209: .length());
210: }
211: logger.outputInRed(NbBundle.getMessage(
212: MergeAction.class, "MSG_MERGE_CONFLICT",
213: filepath)); // NOI18N
214: HgCommand.createConflictFile(filepath);
215: }
216:
217: if (HgCommand.isMergeUnavailableMsg(line)) {
218: JOptionPane.showMessageDialog(null, NbBundle
219: .getMessage(MergeAction.class,
220: "MSG_MERGE_UNAVAILABLE"), // NOI18N
221: NbBundle.getMessage(MergeAction.class,
222: "MSG_MERGE_TITLE"), // NOI18N
223: JOptionPane.WARNING_MESSAGE);
224: logger.outputInRed(NbBundle.getMessage(
225: MergeAction.class, "MSG_MERGE_INFO"));// NOI18N
226: logger.outputLink(NbBundle.getMessage(
227: MergeAction.class, "MSG_MERGE_INFO_URL")); // NOI18N
228: }
229: }
230:
231: if (bConflicts) {
232: logger.outputInRed(NbBundle.getMessage(
233: MergeAction.class, "MSG_MERGE_DONE_CONFLICTS")); // NOI18N
234: }
235: if (!bMergeFailed && !bConflicts) {
236: logger.outputInRed(NbBundle.getMessage(
237: MergeAction.class, "MSG_MERGE_DONE")); // NOI18N
238: }
239: }
240: return true;
241: }
242:
243: public static void printMergeWarning(List<String> list,
244: OutputLogger logger) {
245: if (list == null || list.isEmpty() || list.size() <= 1)
246: return;
247:
248: if (list.size() == 2) {
249: logger.outputInRed(NbBundle.getMessage(MergeAction.class,
250: "MSG_MERGE_WARN_NEEDED", list)); // NOI18N
251: logger.outputInRed(NbBundle.getMessage(MergeAction.class,
252: "MSG_MERGE_DO_NEEDED")); // NOI18N
253: } else {
254: logger
255: .outputInRed(NbBundle.getMessage(MergeAction.class,
256: "MSG_MERGE_WARN_MULTIPLE_HEADS", list
257: .size(), list)); // NOI18N
258: logger.outputInRed(NbBundle.getMessage(MergeAction.class,
259: "MSG_MERGE_DONE_MULTIPLE_HEADS")); // NOI18N
260: }
261: }
262:
263: }
|