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

git-webrev: add --json flag #717

Closed
wants to merge 2 commits 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
@@ -150,6 +150,10 @@ private static void generate(String[] args) throws IOException {
.fullname("mercurial")
.helptext("Deprecated: force use of mercurial")
.optional(),
Switch.shortcut("")
.fullname("json")
.helptext("Generate a JSON description instead of HTML")
.optional(),
Switch.shortcut("C")
.fullname("no-comments")
.helptext("Don't show comments")
@@ -200,30 +204,44 @@ private static void generate(String[] args) throws IOException {
var repo = repository.get();
var isMercurial = arguments.contains("mercurial");


URI upstreamPullPath = null;
URI originPullPath = null;
var remotes = repo.remotes();
if (remotes.contains("upstream")) {
upstreamPullPath = Remote.toWebURI(repo.pullPath("upstream"));
}
if (remotes.contains("origin") || remotes.contains("default")) {
var remote = isMercurial ? "default" : "origin";
originPullPath = Remote.toWebURI(repo.pullPath(remote));
}

if (arguments.contains("json") &&
(upstreamPullPath == null || originPullPath == null)) {
System.err.println("error: --json requires remotes 'upstream' and 'origin' to be present");
System.exit(1);
}

var upstream = arg("upstream", arguments, repo);
if (upstream == null) {
var remotes = repo.remotes();
if (remotes.contains("upstream")) {
var pullPath = Remote.toWebURI(repo.pullPath("upstream"));
var host = pullPath.getHost();
if (upstreamPullPath != null) {
var host = upstreamPullPath.getHost();
if (host != null && host.endsWith("openjdk.java.net")) {
upstream = pullPath.toString();
upstream = upstreamPullPath.toString();
} else if (host != null && host.equals("github.com")) {
var path = pullPath.getPath();
var path = upstreamPullPath.getPath();
if (path != null && path.startsWith("/openjdk/")) {
upstream = pullPath.toString();
upstream = upstreamPullPath.toString();
}
}
} else if (remotes.contains("origin") || remotes.contains("default")) {
var remote = isMercurial ? "default" : "origin";
var pullPath = Remote.toWebURI(repo.pullPath(remote));
var host = pullPath.getHost();
} else if (originPullPath != null) {
var host = originPullPath.getHost();
if (host != null && host.endsWith("openjdk.java.net")) {
upstream = pullPath.toString();
upstream = originPullPath.toString();
} else if (host != null && host.equals("github.com")) {
var path = pullPath.getPath();
var path = originPullPath.getPath();
if (path != null && path.startsWith("/openjdk/")) {
upstream = pullPath.toString();
upstream = originPullPath.toString();
}
}
}
@@ -272,7 +290,6 @@ private static void generate(String[] args) throws IOException {
} else {
String remote = arg("remote", arguments, repo);
if (remote == null) {
var remotes = repo.remotes();
if (remotes.size() == 0) {
System.err.println("error: no remotes present, cannot figure out outgoing changes");
System.err.println(" Use --rev to specify revision to compare against");
@@ -403,18 +420,32 @@ private static void generate(String[] args) throws IOException {
var issueParts = issue != null ? issue.split("-") : new String[0];
var jbsProject = issueParts.length == 2 && KNOWN_JBS_PROJECTS.contains(issueParts[0])?
issueParts[0] : "JDK";
Webrev.repository(repo)
.output(output)
.title(title)
.upstream(upstream)
.username(author.name())
.commitLinker(hash -> upstreamURL == null ? null : upstreamURL + "/commit/" + hash)
.issueLinker(id -> jbs + (isDigit(id.charAt(0)) ? jbsProject + "-" : "") + id)
.issue(issue)
.version(version)
.files(files)
.similarity(similarity)
.generate(base, head);
if (arguments.contains("json")) {
if (head == null) {
head = repo.head();
}
var upstreamName = upstreamPullPath.getPath().substring(1);
var originName = originPullPath.getPath().substring(1);
Webrev.repository(repo)
.output(output)
.upstream(upstreamPullPath, upstreamName)
.fork(originPullPath, originName)
.similarity(similarity)
.generateJSON(base, head);
} else {
Webrev.repository(repo)
.output(output)
.title(title)
.upstream(upstream)
.username(author.name())
.commitLinker(hash -> upstreamURL == null ? null : upstreamURL + "/commit/" + hash)
.issueLinker(id -> jbs + (isDigit(id.charAt(0)) ? jbsProject + "-" : "") + id)
.issue(issue)
.version(version)
.files(files)
.similarity(similarity)
.generate(base, head);
}
}

private static void apply(String[] args) throws Exception {
@@ -23,7 +23,9 @@
package org.openjdk.skara.vcs;

import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;

public class Hunk {
@@ -80,7 +82,17 @@ public WebrevStats stats() {
return new WebrevStats(added, removed, modified);
}

public int changes() {
return source.lines().size() + target.lines().size();
}

public int additions() {
return target.lines().size();
}

public int deletions() {
return source.lines().size();
}

public void write(BufferedWriter w) throws IOException {
w.append("@@ -");
@@ -110,4 +122,36 @@ public void write(BufferedWriter w) throws IOException {
w.write("\n");
}
}

@Override
public String toString() {
var sb = new StringBuilder();
sb.append("@@ -");
sb.append(source.range().toString());
sb.append(" +");
sb.append(target.range().toString());
sb.append(" @@");
sb.append("\n");

for (var line : source.lines()) {
sb.append("-");
sb.append(line);
sb.append("\n");
}
if (!source.hasNewlineAtEndOfFile()) {
sb.append("\\ No newline at end of file");
sb.append("\n");
}

for (var line : target.lines()) {
sb.append("+");
sb.append(line);
sb.append("\n");
}
if (!target.hasNewlineAtEndOfFile()) {
sb.append("\\ No newline at end of file");
sb.append("\n");
}
return sb.toString();
}
}
@@ -63,4 +63,17 @@ public WebrevStats stats() {

return new WebrevStats(added, removed, modified);
}

public int additions() {
return hunks.stream().mapToInt(Hunk::additions).sum();
}

public int deletions() {
return hunks.stream().mapToInt(Hunk::deletions).sum();
}

public int changes() {
return additions() + deletions();
}

}
@@ -33,6 +33,7 @@ module {

dependencies {
implementation project(':vcs')
implementation project(':json')

testImplementation project(':test')
}
@@ -22,6 +22,7 @@
*/
module org.openjdk.skara.webrev {
requires org.openjdk.skara.vcs;
requires org.openjdk.skara.json;
requires java.net.http;
requires java.logging;