Cleaned up whitespace, fix DOS line endings
[nbgit.git] / src / org / nbgit / ui / custom / CustomAction.java
blob2b527c458003df787196b24f953d132e35542b41
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2008 Jonas Fonseca <fonseca@diku.dk>
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. See the License for the
12 * specific language governing permissions and limitations under the
13 * License. When distributing the software, include this License Header
14 * Notice in each file.
16 * This particular file is subject to the "Classpath" exception as provided
17 * by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the
19 * License Header, with the fields enclosed by brackets [] replaced by
20 * your own identifying information:
21 * "Portions Copyrighted [year] [name of copyright owner]"
23 * Contributor(s):
25 * If you wish your version of this file to be governed by only the CDDL
26 * or only the GPL Version 2, indicate your decision by adding
27 * "[Contributor] elects to include this software in this distribution
28 * under the [CDDL or GPL Version 2] license." If you do not indicate a
29 * single choice of license, a recipient has the option to distribute
30 * your version of this file under either the CDDL, the GPL Version 2 or
31 * to extend the choice of license to its licensees as provided above.
32 * However, if you add GPL Version 2 code and therefore, elected the GPL
33 * Version 2 license, then the option applies only if the new code is
34 * made subject to such option by the copyright holder.
36 package org.nbgit.ui.custom;
38 import java.awt.event.ActionEvent;
39 import java.io.BufferedReader;
40 import java.io.File;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.io.InterruptedIOException;
44 import java.util.ArrayList;
45 import java.util.List;
46 import org.nbgit.Git;
47 import org.nbgit.GitProgressSupport;
48 import org.nbgit.OutputLogger;
49 import org.nbgit.StatusCache;
50 import org.nbgit.StatusInfo;
51 import org.nbgit.ui.ContextAction;
52 import org.nbgit.util.GitUtils;
53 import org.openide.util.Exceptions;
54 import org.openide.util.NbBundle;
55 import org.openide.util.RequestProcessor;
56 import org.spearce.jgit.lib.Repository;
58 /**
59 * Custom action container.
61 public class CustomAction extends ContextAction {
63 private final File path;
64 private final String args;
65 private final boolean showOutput;
66 private final boolean showDirty;
67 private final boolean workDirRoot;
69 CustomAction(CustomActionBuilder builder) {
70 super(builder.getName(), builder.getContext());
71 this.path = new File(builder.getPath());
72 this.args = builder.getArgs();
73 this.showOutput = builder.isShowOutput();
74 this.showDirty = builder.isShowDirty();
75 this.workDirRoot = builder.isWorkDirRoot();
78 @Override
79 public boolean isEnabled() {
80 final File root = GitUtils.getProjectFile(context);
81 if (root == null) {
82 return false;
84 if (showDirty) {
85 return true;
87 StatusCache cache = Git.getInstance().getStatusCache();
88 return !cache.containsFileOfStatus(context, StatusInfo.STATUS_LOCAL_CHANGE);
91 @Override
92 protected void performAction(ActionEvent event) {
93 File root = GitUtils.getProjectFile(context);
94 RequestProcessor rp = Git.getInstance().getRequestProcessor(root.getAbsolutePath());
95 Repository repo = Git.getInstance().getRepository(root);
96 final List<String> command = new ArrayList<String>();
98 command.add(path.getAbsolutePath());
99 for (String arg : args.split(" ")) {
100 if (arg.equals("{head}")) {
101 try {
102 command.add(repo.getFullBranch());
103 } catch (IOException ex) {
104 Exceptions.printStackTrace(ex);
106 } else if (arg.equals("{files}")) {
107 for (File file : context.getFiles()) {
108 command.add(file.getAbsolutePath());
110 } else {
111 command.add(arg);
115 final File execDir;
116 if (context.getRootFiles().size() == 1 && !workDirRoot) {
117 execDir = context.getRootFiles().iterator().next();
118 } else {
119 execDir = root;
122 GitProgressSupport support = new GitProgressSupport() {
124 public void perform() {
125 execute(command, execDir, showOutput ? getLogger() : null);
130 /* TODO: Use MessageFormat */
131 support.start(rp, root.getAbsolutePath(),
132 NbBundle.getMessage(CustomAction.class, "CustomActionProgress") + " " + path.getName()); // NOI18N
135 private static void execute(List<String> command, File dir, OutputLogger logger) {
136 BufferedReader input = null;
137 Process proc = null;
138 try {
139 ProcessBuilder builder = new ProcessBuilder(command);
140 builder.directory(dir);
141 proc = builder.start();
143 if (logger != null) {
144 input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
145 String line;
146 while ((line = input.readLine()) != null) {
147 logger.output(line);
149 input.close();
150 input = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
151 while ((line = input.readLine()) != null) {
152 logger.output(line);
154 input.close();
155 input = null;
158 try {
159 proc.waitFor();
160 } catch (InterruptedException e) {
162 } catch (InterruptedIOException e) {
163 if (proc != null) {
164 try {
165 proc.getInputStream().close();
166 proc.getOutputStream().close();
167 proc.getErrorStream().close();
168 } catch (IOException ioex) {
169 // Just ignore. Closing streams.
171 proc.destroy();
173 } catch (IOException e) {
174 } finally {
175 if (input != null) {
176 try {
177 input.close();
178 } catch (IOException ioex) {
179 // Just ignore. Closing streams.
181 input = null;