Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4fcdcf92a1ab6c0faf357c77926eb1bd9d128383 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2014, 2017 Eike Stepper (Loehne, 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.oomph.gitbash;

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.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
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;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Eike Stepper
 */
public class GitApplyAction extends BaseSelectionListenerAction implements IObjectActionDelegate, IMenuCreator
{
  private IWorkbenchPart targetPart;

  private ISelection currentSelection;

  private Menu dropDownMenu;

  public GitApplyAction()
  {
    super("Adjust and Apply Patch");
  }

  protected GitApplyAction(String text)
  {
    super(text);
  }

  public void setActivePart(IAction action, IWorkbenchPart targetPart)
  {
    this.targetPart = targetPart;
  }

  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 = getRepositories();
    List<Repository> sorted = Arrays.asList(repositories);
    Collections.sort(sorted, new Comparator<Repository>()
    {
      public int compare(Repository r1, Repository r2)
      {
        String n1 = r1.getWorkTree().getName();
        String n2 = r2.getWorkTree().getName();
        return n1.compareTo(n2);
      }
    });

    for (Repository repository : sorted)
    {
      RepositorySelectionAction action = new RepositorySelectionAction(attachment, repository);
      ActionContributionItem item = new ActionContributionItem(action);
      item.fill(dropDownMenu, -1);
    }
  }

  @SuppressWarnings("all")
  private Repository[] getRepositories()
  {
    return org.eclipse.egit.core.Activator.getDefault().getRepositoryCache().getAllRepositories();
  }

  /**
   * @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);

        applyPatch(file);
      }
      catch (Exception ex)
      {
        Activator.log(ex);
      }
    }

    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)
          {
          }
        }
      }
    }

    protected void applyPatch(File file)
    {
      if (repository != null)
      {
        Shell shell = targetPart.getSite().getShell();
        File workTree = repository.getWorkTree();
        String command = "git apply " + file.getAbsolutePath().replace('\\', '/');

        GitBash.executeCommand(shell, workTree, command);
      }
    }
  }
}

Back to the top