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