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 --similarity flag #715

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
@@ -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)")
@@ -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])?
@@ -366,6 +387,7 @@ private static void generate(String[] args) throws IOException {
.issue(issue)
.version(version)
.files(files)
.similarity(similarity)
.generate(rev);
}

@@ -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;
}

@@ -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;
@@ -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",
@@ -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);

@@ -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;
@@ -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);
}