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 4.9KB

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