Skip to content

Commit 4a88959

Browse files
committed
Add tool for importing webrev patches
Reviewed-by: ehelin
1 parent 4cf872c commit 4a88959

File tree

15 files changed

+488
-11
lines changed

15 files changed

+488
-11
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
public class Command implements Main {
26+
private final String name;
27+
private final String helpText;
28+
private final Main main;
29+
30+
Command(String name, String helpText, Main main) {
31+
this.name = name;
32+
this.helpText = helpText;
33+
this.main = main;
34+
}
35+
36+
public String name() {
37+
return name;
38+
}
39+
40+
public String helpText() {
41+
return helpText;
42+
}
43+
44+
public Main main() {
45+
return main;
46+
}
47+
48+
public static CommandHelpText name(String name) {
49+
return new CommandHelpText<>(Command::new, name);
50+
}
51+
52+
@Override
53+
public void main(String[] args) throws Exception {
54+
main.main(args);
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
public interface CommandCtor<T extends Command> {
26+
T construct(String name, String helpText, Main main);
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
public class CommandHelpText<T extends Command> {
26+
private final CommandCtor<T> ctor;
27+
private final String name;
28+
29+
CommandHelpText(CommandCtor<T> ctor, String name) {
30+
this.ctor = ctor;
31+
this.name = name;
32+
}
33+
34+
public CommandMain<T> helptext(String helpText) {
35+
return new CommandMain<>(ctor, name, helpText);
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
public class CommandMain<T extends Command> {
26+
private final CommandCtor<T> ctor;
27+
private final String name;
28+
private final String helpText;
29+
30+
CommandMain(CommandCtor<T> ctor, String name, String helpText) {
31+
this.ctor = ctor;
32+
this.name = name;
33+
this.helpText = helpText;
34+
}
35+
36+
public T main(Main main) {
37+
return ctor.construct(name, helpText, main);
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
public class Default extends Command {
26+
Default(String name, String helpText, Main main) {
27+
super(name, helpText, main);
28+
}
29+
30+
public static CommandHelpText<Default> name(String name) {
31+
return new CommandHelpText<>(Default::new, name);
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
@FunctionalInterface
26+
public interface Executable {
27+
void execute() throws Exception;
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
@FunctionalInterface
26+
public interface Main {
27+
void main(String[] args) throws Exception;
28+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.skara.args;
24+
25+
import java.io.PrintStream;
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.function.Function;
30+
import java.util.stream.Collectors;
31+
32+
public class MultiCommandParser {
33+
private final String programName;
34+
private final String defaultCommand;
35+
private final Map<String, Command> subCommands;
36+
37+
public MultiCommandParser(String programName, List<Command> commands) {
38+
var defaults = commands.stream().filter(Default.class::isInstance).collect(Collectors.toList());
39+
if (defaults.size() != 1) {
40+
throw new IllegalArgumentException("Expecting exactly one default command");
41+
}
42+
this.defaultCommand = defaults.get(0).name();
43+
44+
this.programName = programName;
45+
this.subCommands = commands.stream()
46+
.collect(Collectors.toMap(
47+
Command::name,
48+
Function.identity()));
49+
this.subCommands.put("help", helpCommand());
50+
}
51+
52+
private Command helpCommand() {
53+
return new Command("help", "print a help message", args -> showUsage());
54+
}
55+
56+
public Executable parse(String[] args) {
57+
if (args.length > 0) {
58+
var p = subCommands.get(args[0]);
59+
if (p != null) {
60+
var forwardedArgs = Arrays.copyOfRange(args, 1, args.length);
61+
return () -> p.main(forwardedArgs);
62+
}
63+
}
64+
return () -> subCommands.get(defaultCommand).main(args);
65+
}
66+
67+
private void showUsage() {
68+
showUsage(System.out);
69+
}
70+
71+
private void showUsage(PrintStream ps) {
72+
ps.print("usage: ");
73+
ps.print(programName);
74+
ps.print(subCommands.keySet().stream().collect(Collectors.joining("|", " <", ">")));
75+
ps.println(" <input>");
76+
77+
int spacing = subCommands.keySet().stream().mapToInt(String::length).max().orElse(0);
78+
spacing += 8; // some room
79+
80+
for (var subCommand : subCommands.values()) {
81+
ps.println(String.format(" %-" + spacing + "s%s", subCommand.name(), subCommand.helpText()));
82+
}
83+
}
84+
}

cli/src/main/java/org/openjdk/skara/cli/GitSkara.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package org.openjdk.skara.cli;
2424

25+
import org.openjdk.skara.args.Main;
2526
import org.openjdk.skara.vcs.Repository;
2627

2728
import java.io.IOException;
@@ -32,15 +33,10 @@
3233
import java.util.List;
3334
import java.util.Map;
3435
import java.util.TreeMap;
35-
import java.util.function.Consumer;
3636

3737
public class GitSkara {
38-
@FunctionalInterface
39-
private interface Command {
40-
void execute(String[] args) throws Exception;
41-
}
4238

43-
private static final Map<String, Command> commands = new TreeMap<>();
39+
private static final Map<String, Main> commands = new TreeMap<>();
4440

4541
private static void usage(String[] args) {
4642
var names = new ArrayList<String>();
@@ -127,7 +123,7 @@ public static void main(String[] args) throws Exception {
127123
var command = isEmpty ? "help" : args[0];
128124
var commandArgs = isEmpty ? new String[0] : Arrays.copyOfRange(args, 1, args.length);
129125
if (commands.containsKey(command)) {
130-
commands.get(command).execute(commandArgs);
126+
commands.get(command).main(commandArgs);
131127
} else {
132128
System.err.println("error: unknown command: " + command);
133129
usage(args);

0 commit comments

Comments
 (0)