/* * Copyright 2000-2020 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 jetbrains.teamcity.ui.actions; import jetbrains.teamcity.Activator; import jetbrains.teamcity.core.Util; import jetbrains.teamcity.core.shelve.IShelvedChangeSet; import jetbrains.teamcity.core.shelve.IShelvedResource; import jetbrains.teamcity.ui.InformationalDialog; import jetbrains.teamcity.ui.UIUtil; import jetbrains.teamcity.vcs.ui.ShelveTreeViewControl; import jetbrains.teamcity.vcs.ui.ShelveTreeViewControl.CompareWithWorkspaceResourceAction; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.WorkspaceModifyOperation; import java.util.*; import java.util.Map.Entry; public class ShelveUnshelveAction extends ShelveAbstractAction { public ShelveUnshelveAction() { } @Override public void run() { if (getSelection() == null || getSelection().isEmpty()) { return; } final WorkspaceModifyOperation unshelveOperation = new WorkspaceModifyOperation() { @Override protected void execute(IProgressMonitor monitor) throws CoreException { final Set>> opScope = createActionScope().entrySet(); final HashMap pathsToValidationMap = new HashMap(); for (Map.Entry> entry : opScope) { for (IShelvedResource sres : entry.getValue()) { //check if there is resource with such destination if (pathsToValidationMap.containsKey(sres.getOrigin())) { throw new CoreException(Util.createStatus(IStatus.ERROR, String.format("Could not 'unshelve' twice into '%s'", sres.getOrigin()))); } pathsToValidationMap.put(sres.getOrigin(), sres); } } final TreeSet changedFileToOverride = new TreeSet(new Comparator() { public int compare(IFile o1, IFile o2) { return o1.getLocation().toOSString().compareTo(o2.getLocation().toOSString()); } }); for (final Map.Entry validationEntry : pathsToValidationMap.entrySet()) { if (validationEntry.getValue().hasMergeConflict()) { final IFile ifile = ResourcesPlugin.getWorkspace().getRoot().getFile(validationEntry.getKey()); changedFileToOverride.add(ifile); } } final boolean[] override = new boolean[]{changedFileToOverride.isEmpty()}; final boolean[] diff = new boolean[]{changedFileToOverride.isEmpty()}; //warn if something changed if (!changedFileToOverride.isEmpty()) { final StringBuffer changedFiles = new StringBuffer(); for (IFile file : changedFileToOverride) { changedFiles.append(file.getLocation()).append("\n"); } UIUtil.execUI(new Runnable() { public void run() { final int answer = InformationalDialog.openQuestion(UIUtil.getWorkbenchShell(), SWT.ICON_WARNING, "Unshelve", Messages.getString("ShelveUnshelveAction.ask_about_conflict_message"), Util.createStatus(IStatus.WARNING, changedFiles.toString()), null, new String[]{"Override", "Show diff", IDialogConstants.CANCEL_LABEL}, new int[]{IDialogConstants.OK_ID, IDialogConstants.PROCEED_ID, IDialogConstants.CANCEL_ID}, IDialogConstants.PROCEED_ID); if (IDialogConstants.PROCEED_ID == answer) { diff[0] = true; } else if (IDialogConstants.OK_ID == answer) { override[0] = true; } } }, false); } if (override[0]) { //checkout/discard RO if required final Set pathsToValidation = pathsToValidationMap.keySet(); if (UIUtil.Resources.makePathsWritable(monitor, pathsToValidation)) { //perform operation if all ok for (Map.Entry> entry : opScope) { final IShelvedChangeSet changeSet = entry.getKey(); changeSet.unshelve(entry.getValue()); if (changeSet.members().isEmpty()) { changeSet.remove(); } } } } else if (diff[0]) { for (final IFile file : changedFileToOverride) { final ArrayList mergeResources = new ArrayList(); for (final Map.Entry entry : pathsToValidationMap.entrySet()) { if (file.getFullPath().equals(entry.getKey())) { mergeResources.add(entry.getValue()); break; } } UIUtil.execUI(new Runnable() { public void run() { try { final CompareWithWorkspaceResourceAction caction = new ShelveTreeViewControl.CompareWithWorkspaceResourceAction(); caction.selectionChanged(caction, new StructuredSelection(mergeResources)); caction.run(); } catch (Exception e) { Activator.getDefault().getLog().log(Util.createStatus(e)); } } }, true); } } } }; UIUtil.execUI(new Runnable() { public void run() { try { PlatformUI.getWorkbench().getProgressService().run(true, true, unshelveOperation); } catch (Exception e) { Activator.getDefault().getLog().log(Util.createStatus(e)); } } }, false); } }