< prev index next >

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

Print this page

195                                                    "--find-renames=50%",
196                                                    "--find-copies=50%",
197                                                    "--find-copies-harder",
198                                                    "--abbrev",
199                                                    ref + "..." + hash.hex());
200         if (dir != null) {
201             pb.directory(dir.toFile());
202         }
203         pb.redirectOutput(patch.toFile());
204         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
205         await(pb.start());
206         return patch;
207     }
208 
209     private static void apply(Path patch) throws IOException {
210         var pb = new ProcessBuilder("git", "apply", "--no-commit", patch.toString());
211         pb.inheritIO();
212         await(pb.start());
213     }
214 
215     private static URI toURI(String remotePath) throws IOException {
216         if (remotePath.startsWith("git+")) {
217             remotePath = remotePath.substring("git+".length());
218         }
219         if (remotePath.startsWith("http")) {
220             return URI.create(remotePath);
221         } else {
222             if (remotePath.startsWith("ssh://")) {
223                 remotePath = remotePath.substring("ssh://".length()).replaceFirst("/", ":");
224             }
225             var indexOfColon = remotePath.indexOf(':');
226             var indexOfSlash = remotePath.indexOf('/');
227             if (indexOfColon != -1) {
228                 if (indexOfSlash == -1 || indexOfColon < indexOfSlash) {
229                     var path = remotePath.contains("@") ? remotePath.split("@")[1] : remotePath;
230                     var name = path.split(":")[0];
231 
232                     // Could be a Host in the ~/.ssh/config file
233                     var sshConfig = Path.of(System.getProperty("user.home"), ".ssh", "config");
234                     if (Files.exists(sshConfig)) {
235                         for (var host : SSHConfig.parse(sshConfig).hosts()) {
236                             if (host.name().equals(name)) {
237                                 var hostName = host.hostName();
238                                 if (hostName != null) {
239                                     return URI.create("https://" + hostName + "/" + path.split(":")[1]);
240                                 }
241                             }
242                         }
243                     }
244 
245                     // Otherwise is must be a domain
246                     return URI.create("https://" + path.replace(":", "/"));
247                 }
248             }
249         }
250 
251         exit("error: cannot find remote repository for " + remotePath);
252         return null; // will never reach here
253     }
254 
255     private static int longest(List<String> strings) {
256         return strings.stream().mapToInt(String::length).max().orElse(0);
257     }
258 
259     public static void main(String[] args) throws IOException, InterruptedException {
260         var flags = List.of(
261             Option.shortcut("u")
262                   .fullname("username")
263                   .describe("NAME")
264                   .helptext("Username on host")
265                   .optional(),
266             Option.shortcut("r")
267                   .fullname("remote")
268                   .describe("NAME")
269                   .helptext("Name of remote, defaults to 'origin'")
270                   .optional(),
271             Option.shortcut("b")
272                   .fullname("branch")
273                   .describe("NAME")
274                   .helptext("Name of target branch, defaults to 'master'")

330 
331         if (arguments.contains("version")) {
332             System.out.println("git-pr version: " + Version.fromManifest().orElse("unknown"));
333             System.exit(0);
334         }
335 
336         if (arguments.contains("verbose") || arguments.contains("debug")) {
337             var level = arguments.contains("debug") ? Level.FINER : Level.FINE;
338             Logging.setup(level);
339         }
340 
341         HttpProxy.setup();
342 
343         var isMercurial = arguments.contains("mercurial");
344         var cwd = Path.of("").toAbsolutePath();
345         var repo = Repository.get(cwd).orElseThrow(() -> new IOException("no git repository found at " + cwd.toString()));
346         var remote = arguments.get("remote").orString(isMercurial ? "default" : "origin");
347         var remotePullPath = repo.pullPath(remote);
348         var username = arguments.contains("username") ? arguments.get("username").asString() : null;
349         var token = isMercurial ? System.getenv("HG_TOKEN") :  System.getenv("GIT_TOKEN");
350         var uri = toURI(remotePullPath);
351         var credentials = GitCredentials.fill(uri.getHost(), uri.getPath(), username, token, uri.getScheme());
352         var host = Host.from(uri, new PersonalAccessToken(credentials.username(), credentials.password()));
353 
354         var action = arguments.at(0).asString();
355         if (action.equals("create")) {
356             if (isMercurial) {
357                 var currentBookmark = repo.currentBookmark();
358                 if (!currentBookmark.isPresent()) {
359                     System.err.println("error: no bookmark is active, you must be on an active bookmark");
360                     System.err.println("");
361                     System.err.println("To create a bookmark and activate it, run:");
362                     System.err.println("");
363                     System.err.println("    hg bookmark NAME-FOR-YOUR-BOOKMARK");
364                     System.err.println("");
365                     System.exit(1);
366                 }
367 
368                 var bookmark = currentBookmark.get();
369                 if (bookmark.equals(new Bookmark("master"))) {
370                     System.err.println("error: you should not create pull requests from the 'master' bookmark");

195                                                    "--find-renames=50%",
196                                                    "--find-copies=50%",
197                                                    "--find-copies-harder",
198                                                    "--abbrev",
199                                                    ref + "..." + hash.hex());
200         if (dir != null) {
201             pb.directory(dir.toFile());
202         }
203         pb.redirectOutput(patch.toFile());
204         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
205         await(pb.start());
206         return patch;
207     }
208 
209     private static void apply(Path patch) throws IOException {
210         var pb = new ProcessBuilder("git", "apply", "--no-commit", patch.toString());
211         pb.inheritIO();
212         await(pb.start());
213     }
214 








































215     private static int longest(List<String> strings) {
216         return strings.stream().mapToInt(String::length).max().orElse(0);
217     }
218 
219     public static void main(String[] args) throws IOException, InterruptedException {
220         var flags = List.of(
221             Option.shortcut("u")
222                   .fullname("username")
223                   .describe("NAME")
224                   .helptext("Username on host")
225                   .optional(),
226             Option.shortcut("r")
227                   .fullname("remote")
228                   .describe("NAME")
229                   .helptext("Name of remote, defaults to 'origin'")
230                   .optional(),
231             Option.shortcut("b")
232                   .fullname("branch")
233                   .describe("NAME")
234                   .helptext("Name of target branch, defaults to 'master'")

290 
291         if (arguments.contains("version")) {
292             System.out.println("git-pr version: " + Version.fromManifest().orElse("unknown"));
293             System.exit(0);
294         }
295 
296         if (arguments.contains("verbose") || arguments.contains("debug")) {
297             var level = arguments.contains("debug") ? Level.FINER : Level.FINE;
298             Logging.setup(level);
299         }
300 
301         HttpProxy.setup();
302 
303         var isMercurial = arguments.contains("mercurial");
304         var cwd = Path.of("").toAbsolutePath();
305         var repo = Repository.get(cwd).orElseThrow(() -> new IOException("no git repository found at " + cwd.toString()));
306         var remote = arguments.get("remote").orString(isMercurial ? "default" : "origin");
307         var remotePullPath = repo.pullPath(remote);
308         var username = arguments.contains("username") ? arguments.get("username").asString() : null;
309         var token = isMercurial ? System.getenv("HG_TOKEN") :  System.getenv("GIT_TOKEN");
310         var uri = Remote.toWebURI(remotePullPath);
311         var credentials = GitCredentials.fill(uri.getHost(), uri.getPath(), username, token, uri.getScheme());
312         var host = Host.from(uri, new PersonalAccessToken(credentials.username(), credentials.password()));
313 
314         var action = arguments.at(0).asString();
315         if (action.equals("create")) {
316             if (isMercurial) {
317                 var currentBookmark = repo.currentBookmark();
318                 if (!currentBookmark.isPresent()) {
319                     System.err.println("error: no bookmark is active, you must be on an active bookmark");
320                     System.err.println("");
321                     System.err.println("To create a bookmark and activate it, run:");
322                     System.err.println("");
323                     System.err.println("    hg bookmark NAME-FOR-YOUR-BOOKMARK");
324                     System.err.println("");
325                     System.exit(1);
326                 }
327 
328                 var bookmark = currentBookmark.get();
329                 if (bookmark.equals(new Bookmark("master"))) {
330                     System.err.println("error: you should not create pull requests from the 'master' bookmark");
< prev index next >