You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CLIGitCommand.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. import java.io.ByteArrayOutputStream;
  45. import java.text.MessageFormat;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. import org.eclipse.jgit.lib.Repository;
  49. import org.eclipse.jgit.pgm.internal.CLIText;
  50. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  51. import org.eclipse.jgit.pgm.opt.SubcommandHandler;
  52. import org.eclipse.jgit.util.IO;
  53. import org.kohsuke.args4j.Argument;
  54. public class CLIGitCommand {
  55. @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class)
  56. private TextBuiltin subcommand;
  57. @Argument(index = 1, metaVar = "metaVar_arg")
  58. private List<String> arguments = new ArrayList<String>();
  59. public TextBuiltin getSubcommand() {
  60. return subcommand;
  61. }
  62. public List<String> getArguments() {
  63. return arguments;
  64. }
  65. public static List<String> execute(String str, Repository db)
  66. throws Exception {
  67. try {
  68. return IO.readLines(new String(rawExecute(str, db)));
  69. } catch (Die e) {
  70. return IO.readLines(MessageFormat.format(CLIText.get().fatalError,
  71. e.getMessage()));
  72. }
  73. }
  74. public static byte[] rawExecute(String str, Repository db)
  75. throws Exception {
  76. String[] args = split(str);
  77. if (!args[0].equalsIgnoreCase("git") || args.length < 2)
  78. throw new IllegalArgumentException(
  79. "Expected 'git <command> [<args>]', was:" + str);
  80. String[] argv = new String[args.length - 1];
  81. System.arraycopy(args, 1, argv, 0, args.length - 1);
  82. CLIGitCommand bean = new CLIGitCommand();
  83. final CmdLineParser clp = new CmdLineParser(bean);
  84. clp.parseArgument(argv);
  85. final TextBuiltin cmd = bean.getSubcommand();
  86. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  87. cmd.outs = baos;
  88. if (cmd.requiresRepository())
  89. cmd.init(db, null);
  90. else
  91. cmd.init(null, null);
  92. try {
  93. cmd.execute(bean.getArguments().toArray(
  94. new String[bean.getArguments().size()]));
  95. } finally {
  96. if (cmd.outw != null)
  97. cmd.outw.flush();
  98. }
  99. return baos.toByteArray();
  100. }
  101. /**
  102. * Split a command line into a string array.
  103. *
  104. * A copy of Gerrit's
  105. * com.google.gerrit.sshd.CommandFactoryProvider#split(String)
  106. *
  107. * @param commandLine
  108. * a command line
  109. * @return the array
  110. */
  111. static String[] split(String commandLine) {
  112. final List<String> list = new ArrayList<String>();
  113. boolean inquote = false;
  114. boolean inDblQuote = false;
  115. StringBuilder r = new StringBuilder();
  116. for (int ip = 0; ip < commandLine.length();) {
  117. final char b = commandLine.charAt(ip++);
  118. switch (b) {
  119. case '\t':
  120. case ' ':
  121. if (inquote || inDblQuote)
  122. r.append(b);
  123. else if (r.length() > 0) {
  124. list.add(r.toString());
  125. r = new StringBuilder();
  126. }
  127. continue;
  128. case '\"':
  129. if (inquote)
  130. r.append(b);
  131. else
  132. inDblQuote = !inDblQuote;
  133. continue;
  134. case '\'':
  135. if (inDblQuote)
  136. r.append(b);
  137. else
  138. inquote = !inquote;
  139. continue;
  140. case '\\':
  141. if (inquote || ip == commandLine.length())
  142. r.append(b); // literal within a quote
  143. else
  144. r.append(commandLine.charAt(ip++));
  145. continue;
  146. default:
  147. r.append(b);
  148. continue;
  149. }
  150. }
  151. if (r.length() > 0)
  152. list.add(r.toString());
  153. return list.toArray(new String[list.size()]);
  154. }
  155. }