Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish: add flag --follow #638

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -27,9 +27,11 @@
import org.openjdk.skara.version.Version;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import java.util.function.Supplier;
import java.util.logging.Level;

@@ -42,46 +44,108 @@ private static <T> Supplier<T> die(String fmt, Object... args) {
};
}

private static int pushAndTrack(String remote, Branch b, boolean isQuiet) throws IOException, InterruptedException {
var cmd = new ArrayList<String>();
cmd.addAll(List.of("git", "push"));
if (isQuiet) {
cmd.add("--quiet");
private static class RecordingOutputStream extends OutputStream {
private final OutputStream target;
private final boolean shouldForward;
private byte[] output;
private int index;

RecordingOutputStream(OutputStream target, boolean shouldForward) {
this.target = target;
this.shouldForward = shouldForward;
this.output = new byte[1024];
this.index = 0;
}
cmd.addAll(List.of("--set-upstream", remote, b.name()));
var pb = new ProcessBuilder(cmd);

@Override
public void write(int b) throws IOException {
if (index == output.length) {
output = Arrays.copyOf(output, output.length * 2);
}
output[index] = (byte) b;
index++;

if (shouldForward) {
target.write(b);
target.flush();
}
}

String output() {
return new String(output, 0, index + 1, StandardCharsets.UTF_8);
}
}

private static int pushAndFollow(String remote, Branch b, boolean isQuiet, String browser) throws IOException, InterruptedException {
var pb = new ProcessBuilder("git", "push", "--set-upstream", "--progress", remote, b.name());
if (isQuiet) {
pb.redirectOutput(ProcessBuilder.Redirect.DISCARD);
pb.redirectError(ProcessBuilder.Redirect.PIPE);
} else {
pb.inheritIO();
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
}
pb.redirectError(ProcessBuilder.Redirect.PIPE);
var p = pb.start();
var errorOutput = p.getErrorStream().readAllBytes();
var recording = new RecordingOutputStream(System.err, !isQuiet);
p.getErrorStream().transferTo(recording);
int err = p.waitFor();
if (err != 0) {
System.out.write(errorOutput, 0, errorOutput.length);
System.out.flush();
if (err == 0) {
var lines = recording.output().lines().collect(Collectors.toList());
for (var line : lines) {
if (line.startsWith("remote:")) {
var parts = line.split("\\s");
for (var part : parts) {
if (part.startsWith("https://")) {
var browserPB = new ProcessBuilder(browser, part);
browserPB.start().waitFor(); // don't care about status
break;
}
}
}
}
}
return err;
}

private static int push(String remote, Branch b, boolean isQuiet) throws IOException, InterruptedException {
var cmd = new ArrayList<String>();
cmd.addAll(List.of("git", "push"));
if (isQuiet) {
cmd.add("--quiet");
}
cmd.addAll(List.of("--set-upstream", remote, b.name()));
var pb = new ProcessBuilder(cmd);
pb.inheritIO();
return pb.start().waitFor();
}

private static String getOption(String name, Arguments arguments, ReadOnlyRepository repo) throws IOException {
if (arguments.contains(name)) {
return arguments.get(name).asString();
}

var lines = repo.config("sync." + name);
var lines = repo.config("publish." + name);
return lines.size() == 1 ? lines.get(0) : null;
}

private static boolean getSwitch(String name, Arguments arguments, ReadOnlyRepository repo) throws IOException {
if (arguments.contains(name)) {
return true;
}

var lines = repo.config("publish." + name);
return lines.size() == 1 && lines.get(0).toLowerCase().equals("true");
}

public static void main(String[] args) throws IOException, InterruptedException {
var flags = List.of(
Switch.shortcut("q")
.fullname("quiet")
.helptext("Silence all output")
.optional(),
Switch.shortcut("")
.fullname("follow")
.helptext("Open link provided by remote")
.optional(),
Switch.shortcut("")
.fullname("verbose")
.helptext("Turn on verbose output")
@@ -146,14 +210,27 @@ public static void main(String[] args) throws IOException, InterruptedException
System.exit(1);
}

var isQuiet = arguments.contains("quiet");
if (!isQuiet) {
var lines = repo.config("publish.quiet");
isQuiet = lines.size() == 1 && lines.get(0).toLowerCase().equals("true");
}
var err = pushAndTrack(remote, repo.currentBranch().get(), isQuiet);
if (err != 0) {
System.exit(err);
var branch = repo.currentBranch().get();
var isQuiet = getSwitch("quiet", arguments, repo);
var shouldFollow = getSwitch("follow", arguments, repo);
int err = 0;
if (shouldFollow) {
var browser = getOption("browser", arguments, repo);
if (browser == null) {
var os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
browser = "explorer";
} else if (os.startsWith("mac")) {
browser = "open";
} else {
// Assume GNU/Linux
browser = "xdg-open";
}
}
err = pushAndFollow(remote, branch, isQuiet, browser);
} else {
err = push(remote, branch, isQuiet);
}
System.exit(err);
}
}