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