Skip to content

Commit

Permalink
git-webrev: add --similarity flag
Browse files Browse the repository at this point in the history
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Aug 15, 2020
1 parent 49868fe commit 1aa66a5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
22 changes: 22 additions & 0 deletions cli/src/main/java/org/openjdk/skara/cli/GitWebrev.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ private static void generate(String[] args) throws IOException {
.describe("NAME")
.helptext("Use remote to calculate outgoing changes")
.optional(),
Option.shortcut("s")
.fullname("similarity")
.describe("SIMILARITY")
.helptext("Guess renamed files by similarity (0 - 100)")
.optional(),
Switch.shortcut("b")
.fullname("")
.helptext("Do not ignore changes in whitespace (always true)")
Expand Down Expand Up @@ -352,6 +357,22 @@ private static void generate(String[] args) throws IOException {
}
}

var similarity = 90;
try {
var similarityArg = arg("similarity", arguments, repo);
if (similarityArg != null) {
var value = Integer.parseInt(similarityArg);
if (value < 0 || value > 100) {
System.err.println("error: --similarity must be a number between 0 and 100");
System.exit(1);
}
similarity = value;
}
} catch (NumberFormatException e) {
System.err.println("error: --similarity must be a number between 0 and 100");
System.exit(1);
}

var jbs = "https://bugs.openjdk.java.net/browse/";
var issueParts = issue != null ? issue.split("-") : new String[0];
var jbsProject = issueParts.length == 2 && KNOWN_JBS_PROJECTS.contains(issueParts[0])?
Expand All @@ -366,6 +387,7 @@ private static void generate(String[] args) throws IOException {
.issue(issue)
.version(version)
.files(files)
.similarity(similarity)
.generate(rev);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,19 @@ public List<FileEntry> files(Hash h, List<Path> paths) throws IOException {
public void dump(FileEntry entry, Path to) throws IOException {
}

public Diff diff(Hash base, Hash head) throws IOException {
public Diff diff(Hash base, Hash head, int similarity) throws IOException {
return null;
}

public Diff diff(Hash base, Hash head, List<Path> files) throws IOException {
public Diff diff(Hash base, Hash head, List<Path> files, int similarity) throws IOException {
return null;
}

public Diff diff(Hash head) throws IOException {
public Diff diff(Hash head, int similarity) throws IOException {
return null;
}

public Diff diff(Hash head, List<Path> files) throws IOException {
public Diff diff(Hash head, List<Path> files, int similarity) throws IOException {
return null;
}

Expand Down
23 changes: 19 additions & 4 deletions vcs/src/main/java/org/openjdk/skara/vcs/ReadOnlyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,25 @@ default List<FileEntry> files(Hash h, Path... paths) throws IOException {
void dump(FileEntry entry, Path to) throws IOException;
List<StatusEntry> status(Hash from, Hash to) throws IOException;
List<StatusEntry> status() throws IOException;
Diff diff(Hash base, Hash head) throws IOException;
Diff diff(Hash base, Hash head, List<Path> files) throws IOException;
Diff diff(Hash head) throws IOException;
Diff diff(Hash head, List<Path> files) throws IOException;

static final int DEFAULT_SIMILARITY = 90;
default Diff diff(Hash base, Hash head) throws IOException {
return diff(base, head, DEFAULT_SIMILARITY);
}
Diff diff(Hash base, Hash head, int similarity) throws IOException;
default Diff diff(Hash base, Hash head, List<Path> files) throws IOException {
return diff(base, head, files, DEFAULT_SIMILARITY);
}
Diff diff(Hash base, Hash head, List<Path> files, int similarity) throws IOException;
default Diff diff(Hash head) throws IOException {
return diff(head, DEFAULT_SIMILARITY);
}
Diff diff(Hash head, int similarity) throws IOException;
default Diff diff(Hash head, List<Path> files) throws IOException {
return diff(head, files, DEFAULT_SIMILARITY);
}

Diff diff(Hash head, List<Path> files, int similarity) throws IOException;
List<String> config(String key) throws IOException;
Repository copyTo(Path destination) throws IOException;
String pullPath(String remote) throws IOException;
Expand Down
21 changes: 12 additions & 9 deletions vcs/src/main/java/org/openjdk/skara/vcs/git/GitRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -998,25 +998,28 @@ public List<StatusEntry> status() throws IOException {
}

@Override
public Diff diff(Hash from) throws IOException {
return diff(from, List.of());
public Diff diff(Hash from, int similarity) throws IOException {
return diff(from, List.of(), similarity);
}

@Override
public Diff diff(Hash from, List<Path> files) throws IOException {
return diff(from, null, files);
public Diff diff(Hash from, List<Path> files, int similarity) throws IOException {
return diff(from, null, files, similarity);
}

@Override
public Diff diff(Hash from, Hash to) throws IOException {
return diff(from, to, List.of());
public Diff diff(Hash from, Hash to, int similarity) throws IOException {
return diff(from, to, List.of(), similarity);
}

@Override
public Diff diff(Hash from, Hash to, List<Path> files) throws IOException {
public Diff diff(Hash from, Hash to, List<Path> files, int similarity) throws IOException {
if (similarity < 0 || similarity > 100) {
throw new IllegalArgumentException("similarity must be between 0 and 100, is: " + similarity);
}
var cmd = new ArrayList<>(List.of("git", "diff", "--patch",
"--find-renames=90%",
"--find-copies=90%",
"--find-renames=" + similarity + "%",
"--find-copies=" + similarity + "%",
"--find-copies-harder",
"--binary",
"--raw",
Expand Down
8 changes: 4 additions & 4 deletions vcs/src/main/java/org/openjdk/skara/vcs/hg/HgRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -846,22 +846,22 @@ public void revert(Hash parent) throws IOException {
}

@Override
public Diff diff(Hash from) throws IOException {
public Diff diff(Hash from, int similarity) throws IOException {
return diff(from, List.of());
}

@Override
public Diff diff(Hash from, List<Path> files) throws IOException {
public Diff diff(Hash from, List<Path> files, int similarity) throws IOException {
return diff(from, null, files);
}

@Override
public Diff diff(Hash from, Hash to) throws IOException {
public Diff diff(Hash from, Hash to, int similarity) throws IOException {
return diff(from, to, List.of());
}

@Override
public Diff diff(Hash from, Hash to, List<Path> files) throws IOException {
public Diff diff(Hash from, Hash to, List<Path> files, int similarity) throws IOException {
var ext = Files.createTempFile("ext", ".py");
copyResource(EXT_PY, ext);

Expand Down
10 changes: 8 additions & 2 deletions webrev/src/main/java/org/openjdk/skara/webrev/Webrev.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static class Builder {
private Function<String, String> commitLinker;
private String version;
private List<Path> files = List.of();
private int similarity = 90;

Builder(ReadOnlyRepository repository, Path output) {
this.repository = repository;
Expand Down Expand Up @@ -130,14 +131,19 @@ public Builder files(List<Path> files) {
return this;
}

public Builder similarity(int similarity) {
this.similarity = similarity;
return this;
}

public void generate(Hash tailEnd) throws IOException {
generate(tailEnd, null);
}

public void generate(Hash tailEnd, Hash head) throws IOException {
var diff = head == null ?
repository.diff(tailEnd, files) :
repository.diff(tailEnd, head, files);
repository.diff(tailEnd, files, similarity) :
repository.diff(tailEnd, head, files, similarity);
generate(diff, tailEnd, head);
}

Expand Down

1 comment on commit 1aa66a5

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 1aa66a5 Aug 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.