diff options
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf')
3 files changed, 361 insertions, 113 deletions
diff --git a/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/ApplyPatchToGitAction.java b/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/ApplyPatchToGitAction.java new file mode 100644 index 0000000000..ddba36c44f --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/ApplyPatchToGitAction.java @@ -0,0 +1,222 @@ +/**
+ * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.emf.cdo.releng.wingit;
+
+import org.eclipse.core.resources.IStorage;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuCreator;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.mylyn.tasks.core.ITaskAttachment;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.ui.IViewActionDelegate;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.actions.BaseSelectionListenerAction;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+
+/**
+ * @author Eike Stepper
+ */
+public class ApplyPatchToGitAction extends BaseSelectionListenerAction implements IViewActionDelegate, IMenuCreator
+{
+ private ISelection currentSelection;
+
+ private Menu dropDownMenu;
+
+ public ApplyPatchToGitAction()
+ {
+ super("Adjust and Apply Patch");
+ }
+
+ protected ApplyPatchToGitAction(String text)
+ {
+ super(text);
+ }
+
+ public void init(IViewPart view)
+ {
+ // ignore
+ }
+
+ public void run(IAction action)
+ {
+ action.setMenuCreator(this);
+ }
+
+ public void selectionChanged(IAction action, ISelection selection)
+ {
+ action.setMenuCreator(this);
+ currentSelection = selection;
+ }
+
+ private ITaskAttachment getSelectedAttachment()
+ {
+ if (currentSelection instanceof StructuredSelection)
+ {
+ Object object = ((StructuredSelection)currentSelection).getFirstElement();
+ if (object instanceof ITaskAttachment)
+ {
+ return (ITaskAttachment)object;
+ }
+ }
+
+ return null;
+ }
+
+ public void dispose()
+ {
+ if (dropDownMenu != null)
+ {
+ dropDownMenu.dispose();
+ dropDownMenu = null;
+ }
+ }
+
+ public Menu getMenu(Control parent)
+ {
+ dispose();
+ dropDownMenu = new Menu(parent);
+ addActionsToMenu();
+ return dropDownMenu;
+ }
+
+ public Menu getMenu(Menu parent)
+ {
+ dispose();
+ dropDownMenu = new Menu(parent);
+ addActionsToMenu();
+ return dropDownMenu;
+ }
+
+ private void addActionsToMenu()
+ {
+ ITaskAttachment attachment = getSelectedAttachment();
+ if (attachment == null)
+ {
+ return;
+ }
+
+ Repository[] repositories = org.eclipse.egit.core.Activator.getDefault().getRepositoryCache().getAllRepositories();
+ for (Repository repository : repositories)
+ {
+ RepositorySelectionAction action = new RepositorySelectionAction(attachment, repository);
+ ActionContributionItem item = new ActionContributionItem(action);
+ item.fill(dropDownMenu, -1);
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private class RepositorySelectionAction extends Action
+ {
+ private final ITaskAttachment attachment;
+
+ private final Repository repository;
+
+ public RepositorySelectionAction(ITaskAttachment attachment, Repository repository)
+ {
+ this.attachment = attachment;
+ this.repository = repository;
+ setText(repository.getWorkTree().getName());
+ setImageDescriptor(Activator.getImageDescriptor("icons/repository.gif"));
+ }
+
+ @Override
+ @SuppressWarnings({ "restriction", "deprecation" })
+ public void run()
+ {
+ try
+ {
+ IStorage storage = org.eclipse.mylyn.internal.tasks.ui.editors.TaskAttachmentStorage.create(attachment);
+ File file = savePatch(storage);
+
+ // TODO Do apply patch here:
+ System.out.println("Applying " + file.getAbsolutePath() + " to " + repository.getWorkTree());
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+
+ @SuppressWarnings("resource")
+ private File savePatch(IStorage storage) throws CoreException
+ {
+ InputStream in = null;
+ BufferedWriter writer = null;
+
+ try
+ {
+ File tempFile = File.createTempFile("~attachment-", ".patch");
+ writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile)));
+
+ in = storage.getContents();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+
+ String line;
+ while ((line = reader.readLine()) != null)
+ {
+ writer.write(line);
+ writer.write("\n");
+ }
+
+ return tempFile;
+ }
+ catch (CoreException ex)
+ {
+ throw ex;
+ }
+ catch (Exception ex)
+ {
+ throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Problem while saving patch", ex));
+ }
+ finally
+ {
+ if (in != null)
+ {
+ try
+ {
+ in.close();
+ }
+ catch (Exception ex)
+ {
+ }
+ }
+
+ if (writer != null)
+ {
+ try
+ {
+ writer.close();
+ }
+ catch (Exception ex)
+ {
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/GitAction.java b/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/GitAction.java index 54fe922c0d..cef7cf6a99 100644 --- a/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/GitAction.java +++ b/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/GitAction.java @@ -12,31 +12,21 @@ package org.eclipse.emf.cdo.releng.wingit; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jface.action.IAction; -import org.eclipse.jface.dialogs.IInputValidator; -import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jgit.lib.Repository; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; /** * @author Eike Stepper */ public class GitAction implements IObjectActionDelegate { - private static final String GIT_BASH = "C:\\Program Files (x86)\\Git\\bin\\sh.exe"; - private IWorkbenchPart targetPart; - private String gitBash; - private Repository repository; public GitAction() @@ -67,13 +57,13 @@ public class GitAction implements IObjectActionDelegate { if (repository != null) { - initGitBash(); + String gitBash = WinGit.getGitBash(targetPart.getSite().getShell()); try { File workTree = repository.getWorkTree(); Runtime.getRuntime().exec( - "cmd /c cd \"" + workTree.getAbsolutePath() + "\" && start cmd.exe /c \"" + GIT_BASH + "\" --login -i"); + "cmd /c cd \"" + workTree.getAbsolutePath() + "\" && start cmd.exe /c \"" + gitBash + "\" --login -i"); } catch (Exception ex) { @@ -81,105 +71,4 @@ public class GitAction implements IObjectActionDelegate } } } - - protected void initGitBash() - { - if (gitBash == null) - { - File stateFile = Activator.getDefault().getStateLocation().append("git-bash.txt").toFile(); - if (stateFile.isFile()) - { - gitBash = loadFile(stateFile); - } - - if (gitBash == null) - { - gitBash = getPath(GIT_BASH); - } - - if (!new File(gitBash).isFile()) - { - gitBash = getPath(gitBash); - } - - saveFile(stateFile, gitBash); - } - } - - private String getPath(String initial) - { - InputDialog dialog = new InputDialog(targetPart.getSite().getShell(), "Git Bash", "Location:", initial, - new IInputValidator() - { - public String isValid(String newText) - { - return new File(newText).isFile() ? null : "Not a file!"; - } - }); - - if (dialog.open() != InputDialog.OK) - { - throw new IllegalStateException("Git bash not found at " + gitBash); - } - - return dialog.getValue(); - } - - private String loadFile(File file) - { - FileReader in = null; - - try - { - in = new FileReader(file); - return new BufferedReader(in).readLine(); - } - catch (IOException ex) - { - return null; - } - finally - { - if (in != null) - { - try - { - in.close(); - } - catch (IOException ex) - { - // Ignore - } - } - } - } - - private void saveFile(File file, String content) - { - FileWriter out = null; - - try - { - out = new FileWriter(file); - out.write(content); - } - catch (IOException ex) - { - throw new IllegalStateException("Could not write to " + file, ex); - } - finally - { - if (out != null) - { - try - { - out.close(); - } - catch (IOException ex) - { - // Ignore - } - } - } - } } diff --git a/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/WinGit.java b/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/WinGit.java new file mode 100644 index 0000000000..c0a0b7251a --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.releng.wingit/src/org/eclipse/emf/cdo/releng/wingit/WinGit.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.emf.cdo.releng.wingit; + +import org.eclipse.jface.dialogs.IInputValidator; +import org.eclipse.jface.dialogs.InputDialog; +import org.eclipse.swt.widgets.Shell; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +/** + * @author Eike Stepper + */ +public class WinGit +{ + private static final String GIT_BASH = "C:\\Program Files (x86)\\Git\\bin\\sh.exe"; + + private static String gitBash; + + public WinGit() + { + } + + public static synchronized String getGitBash(Shell shell) + { + if (gitBash == null) + { + File stateFile = Activator.getDefault().getStateLocation().append("git-bash.txt").toFile(); + if (stateFile.isFile()) + { + gitBash = loadFile(stateFile); + } + + if (gitBash == null) + { + gitBash = getPath(GIT_BASH, shell); + } + + if (!new File(gitBash).isFile()) + { + gitBash = getPath(gitBash, shell); + } + + saveFile(stateFile, gitBash); + } + + return gitBash; + } + + private static String getPath(String initial, Shell shell) + { + InputDialog dialog = new InputDialog(shell, "Git Bash", "Location:", initial, new IInputValidator() + { + public String isValid(String newText) + { + return new File(newText).isFile() ? null : "Not a file!"; + } + }); + + if (dialog.open() != InputDialog.OK) + { + throw new IllegalStateException("Git bash not found at " + gitBash); + } + + return dialog.getValue(); + } + + private static String loadFile(File file) + { + FileReader in = null; + + try + { + in = new FileReader(file); + return new BufferedReader(in).readLine(); + } + catch (IOException ex) + { + return null; + } + finally + { + if (in != null) + { + try + { + in.close(); + } + catch (IOException ex) + { + // Ignore + } + } + } + } + + private static void saveFile(File file, String content) + { + FileWriter out = null; + + try + { + out = new FileWriter(file); + out.write(content); + } + catch (IOException ex) + { + throw new IllegalStateException("Could not write to " + file, ex); + } + finally + { + if (out != null) + { + try + { + out.close(); + } + catch (IOException ex) + { + // Ignore + } + } + } + } +} |