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.cli; 24 25 import org.openjdk.skara.vcs.Repository; 26 27 import java.io.IOException; 28 import java.nio.file.Path; 29 import java.nio.charset.StandardCharsets; 30 import java.util.Arrays; 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.TreeMap; 35 import java.util.function.Consumer; 36 37 public class GitSkara { 38 @FunctionalInterface 39 private interface Command { 40 void execute(String[] args) throws Exception; 41 } 42 43 private static final Map<String, Command> commands = new TreeMap<>(); 44 45 private static void usage(String[] args) { 46 var names = new ArrayList<String>(); 47 names.addAll(commands.keySet()); 48 49 System.out.println("usage: git-skara <" + String.join("|", names) + ">"); 50 System.out.println(""); 51 System.out.println("Additional available git commands"); 52 for (var name : names) { 53 System.out.println("- git-" + name); 54 } 55 System.exit(0); 56 } 57 58 private static List<String> config(String key) throws IOException, InterruptedException { 59 var pb = new ProcessBuilder("git", "config", key); 60 pb.redirectOutput(ProcessBuilder.Redirect.PIPE); 61 pb.redirectError(ProcessBuilder.Redirect.INHERIT); 62 var p = pb.start(); 63 var value = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8); 64 p.waitFor(); 65 return Arrays.asList(value.split("\n")); 66 } 67 68 private static void update(String[] args) throws IOException, InterruptedException { 69 var lines = config("include.path"); 70 var path = lines.stream().filter(l -> l.endsWith("skara.gitconfig")).map(Path::of).findFirst(); 71 if (path.isEmpty()) { 72 System.err.println("error: could not find skara repository"); 73 System.exit(1); 74 } 75 76 var parent = path.get().getParent(); 77 var repo = Repository.get(parent); 78 if (repo.isEmpty()) { 79 System.err.println("error: could not find skara repository"); 80 System.exit(1); 81 } 82 83 var head = repo.get().head(); 84 System.out.print("Checking for updates ..."); 85 repo.get().pull(); 86 var newHead = repo.get().head(); 87 88 if (!head.equals(newHead)) { 89 System.out.println("updates downloaded"); 90 System.out.println("Rebuilding ..."); 91 var cmd = new ArrayList<String>(); 92 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 93 cmd.add("gradlew.bat"); 94 } else { 95 cmd.addAll(List.of("sh", "gradlew")); 96 } 97 98 var pb = new ProcessBuilder(cmd); 99 pb.inheritIO(); 100 pb.directory(parent.toFile()); 101 var p = pb.start(); 102 var res = p.waitFor(); 103 if (res != 0) { 104 System.err.println("error: could not build Skara tooling"); 105 System.exit(1); 106 } 107 } else { 108 System.out.println("no updates found"); 109 } 110 } 111 112 public static void main(String[] args) throws Exception { 113 commands.put("jcheck", GitJCheck::main); 114 commands.put("webrev", GitWebrev::main); 115 commands.put("defpath", GitDefpath::main); 116 commands.put("verify-import", GitVerifyImport::main); 117 commands.put("openjdk-import", GitOpenJDKImport::main); 118 commands.put("fork", GitFork::main); 119 commands.put("pr", GitPr::main); 120 commands.put("token", GitToken::main); 121 commands.put("info", GitInfo::main); 122 commands.put("translate", GitTranslate::main); 123 commands.put("update", GitSkara::update); 124 commands.put("help", GitSkara::usage); 125 126 var isEmpty = args.length == 0; 127 var command = isEmpty ? "help" : args[0]; 128 var commandArgs = isEmpty ? new String[0] : Arrays.copyOfRange(args, 1, args.length); 129 if (commands.containsKey(command)) { 130 commands.get(command).execute(commandArgs); 131 } else { 132 System.err.println("error: unknown command: " + command); 133 usage(args); 134 } 135 } 136 }