View Javadoc
1   /*
2    * Copyright (C) 2011-2012, IBM Corporation and others.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.pgm;
44  
45  import static java.nio.charset.StandardCharsets.UTF_8;
46  import static org.junit.Assert.assertNull;
47  
48  import java.io.ByteArrayOutputStream;
49  import java.io.File;
50  import java.io.IOException;
51  import java.io.OutputStreamWriter;
52  import java.io.PrintWriter;
53  import java.util.ArrayList;
54  import java.util.List;
55  
56  import org.eclipse.jgit.internal.storage.file.FileRepository;
57  import org.eclipse.jgit.lib.Repository;
58  import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException;
59  import org.eclipse.jgit.util.IO;
60  
61  public class CLIGitCommand extends Main {
62  
63  	private final Result result;
64  
65  	private final Repository db;
66  
67  	public CLIGitCommand(Repository db) {
68  		super();
69  		this.db = db;
70  		result = new Result();
71  	}
72  
73  	/**
74  	 * Executes git commands (with arguments) specified on the command line. The
75  	 * git repository (same for all commands) can be specified via system
76  	 * property "-Dgit_work_tree=path_to_work_tree". If the property is not set,
77  	 * current directory is used.
78  	 *
79  	 * @param args
80  	 *            each element in the array must be a valid git command line,
81  	 *            e.g. "git branch -h"
82  	 * @throws Exception
83  	 */
84  	public static void main(String[] args) throws Exception {
85  		String workDir = System.getProperty("git_work_tree");
86  		if (workDir == null) {
87  			workDir = ".";
88  			System.out.println(
89  					"System property 'git_work_tree' not specified, using current directory: "
90  							+ new File(workDir).getAbsolutePath());
91  		}
92  		try (Repository db = new FileRepository(workDir + "/.git")) {
93  			for (String cmd : args) {
94  				List<String> result = execute(cmd, db);
95  				for (String line : result) {
96  					System.out.println(line);
97  				}
98  			}
99  		}
100 	}
101 
102 	public static List<String> execute(String str, Repository db)
103 			throws Exception {
104 		Result result = executeRaw(str, db);
105 		return getOutput(result);
106 	}
107 
108 	public static Result executeRaw(String str, Repository db)
109 			throws Exception {
110 		CLIGitCommand cmd = new CLIGitCommand(db);
111 		cmd.run(str);
112 		return cmd.result;
113 	}
114 
115 	public static List<String> executeUnchecked(String str, Repository db)
116 			throws Exception {
117 		CLIGitCommand cmd = new CLIGitCommand(db);
118 		try {
119 			cmd.run(str);
120 			return getOutput(cmd.result);
121 		} catch (Throwable e) {
122 			return cmd.result.errLines();
123 		}
124 	}
125 
126 	private static List<String> getOutput(Result result) {
127 		if (result.ex instanceof TerminatedByHelpException) {
128 			return result.errLines();
129 		}
130 		return result.outLines();
131 	}
132 
133 	private void run(String commandLine) throws Exception {
134 		String[] argv = convertToMainArgs(commandLine);
135 		try {
136 			super.run(argv);
137 		} catch (TerminatedByHelpException e) {
138 			// this is not a failure, super called exit() on help
139 		} finally {
140 			writer.flush();
141 		}
142 	}
143 
144 	private static String[] convertToMainArgs(String str)
145 			throws Exception {
146 		String[] args = split(str);
147 		if (!args[0].equalsIgnoreCase("git") || args.length < 2) {
148 			throw new IllegalArgumentException(
149 					"Expected 'git <command> [<args>]', was:" + str);
150 		}
151 		String[] argv = new String[args.length - 1];
152 		System.arraycopy(args, 1, argv, 0, args.length - 1);
153 		return argv;
154 	}
155 
156 	@Override
157 	PrintWriter createErrorWriter() {
158 		return new PrintWriter(new OutputStreamWriter(
159 				result.err, UTF_8));
160 	}
161 
162 	@Override
163 	void init(TextBuiltin cmd) throws IOException {
164 		cmd.outs = result.out;
165 		cmd.errs = result.err;
166 		super.init(cmd);
167 	}
168 
169 	@Override
170 	protected Repository openGitDir(String aGitdir) throws IOException {
171 		assertNull(aGitdir);
172 		return db;
173 	}
174 
175 	@Override
176 	void exit(int status, Exception t) throws Exception {
177 		if (t == null) {
178 			t = new IllegalStateException(Integer.toString(status));
179 		}
180 		result.ex = t;
181 		throw t;
182 	}
183 
184 	/**
185 	 * Split a command line into a string array.
186 	 *
187 	 * A copy of Gerrit's
188 	 * com.google.gerrit.sshd.CommandFactoryProvider#split(String)
189 	 *
190 	 * @param commandLine
191 	 *            a command line
192 	 * @return the array
193 	 */
194 	static String[] split(String commandLine) {
195 		final List<String> list = new ArrayList<>();
196 		boolean inquote = false;
197 		boolean inDblQuote = false;
198 		StringBuilder r = new StringBuilder();
199 		for (int ip = 0; ip < commandLine.length();) {
200 			final char b = commandLine.charAt(ip++);
201 			switch (b) {
202 			case '\t':
203 			case ' ':
204 				if (inquote || inDblQuote)
205 					r.append(b);
206 				else if (r.length() > 0) {
207 					list.add(r.toString());
208 					r = new StringBuilder();
209 				}
210 				continue;
211 			case '\"':
212 				if (inquote)
213 					r.append(b);
214 				else
215 					inDblQuote = !inDblQuote;
216 				continue;
217 			case '\'':
218 				if (inDblQuote)
219 					r.append(b);
220 				else
221 					inquote = !inquote;
222 				continue;
223 			case '\\':
224 				if (inDblQuote || inquote || ip == commandLine.length())
225 					r.append(b); // literal within a quote
226 				else
227 					r.append(commandLine.charAt(ip++));
228 				continue;
229 			default:
230 				r.append(b);
231 				continue;
232 			}
233 		}
234 		if (r.length() > 0)
235 			list.add(r.toString());
236 		return list.toArray(new String[0]);
237 	}
238 
239 	public static class Result {
240 		public final ByteArrayOutputStream out = new ByteArrayOutputStream();
241 
242 		public final ByteArrayOutputStream err = new ByteArrayOutputStream();
243 
244 		public Exception ex;
245 
246 		public byte[] outBytes() {
247 			return out.toByteArray();
248 		}
249 
250 		public byte[] errBytes() {
251 			return err.toByteArray();
252 		}
253 
254 		public String outString() {
255 			return new String(out.toByteArray(), UTF_8);
256 		}
257 
258 		public List<String> outLines() {
259 			return IO.readLines(outString());
260 		}
261 
262 		public String errString() {
263 			return new String(err.toByteArray(), UTF_8);
264 		}
265 
266 		public List<String> errLines() {
267 			return IO.readLines(errString());
268 		}
269 	}
270 
271 }