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: show commit messages #441

Closed
wants to merge 3 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
@@ -137,6 +137,46 @@ public Optional<Commit> lookup(Tag t) throws IOException {
return Optional.empty();
}

public List<CommitMetadata> commitMetadata(Hash from, Hash to) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(String range, boolean reverse) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(Hash from, Hash to, boolean reverse) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(List<Path> paths) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(List<Path> paths, boolean reverse) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(String range, List<Path> paths) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(String range, List<Path> paths, boolean reverse) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths, boolean reverse) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(boolean reverse) throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadata(String range) throws IOException {
return List.of();
}
@@ -49,7 +49,17 @@ public interface ReadOnlyRepository {
Optional<Commit> lookup(Branch b) throws IOException;
Optional<Commit> lookup(Tag t) throws IOException;
List<CommitMetadata> commitMetadata() throws IOException;
List<CommitMetadata> commitMetadata(boolean reverse) throws IOException;
List<CommitMetadata> commitMetadata(String range) throws IOException;
List<CommitMetadata> commitMetadata(Hash from, Hash to) throws IOException;
List<CommitMetadata> commitMetadata(String range, boolean reverse) throws IOException;
List<CommitMetadata> commitMetadata(Hash from, Hash to, boolean reverse) throws IOException;
List<CommitMetadata> commitMetadata(List<Path> paths) throws IOException;
List<CommitMetadata> commitMetadata(List<Path> paths, boolean reverse) throws IOException;
List<CommitMetadata> commitMetadata(String range, List<Path> paths) throws IOException;
List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths) throws IOException;
List<CommitMetadata> commitMetadata(String range, List<Path> paths, boolean reverse) throws IOException;
List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths, boolean reverse) throws IOException;
Path root() throws IOException;
boolean exists() throws IOException;
boolean isHealthy() throws IOException;
@@ -200,8 +200,23 @@ public Optional<Commit> lookup(Tag t) throws IOException {
}

@Override
public List<CommitMetadata> commitMetadata(String range) throws IOException {
var p = start("git", "rev-list", "--format=" + GitCommitMetadata.FORMAT, "--no-abbrev", "--reverse", "--no-color", range);
public List<CommitMetadata> commitMetadata(String range, List<Path> paths, boolean reverse) throws IOException {
var args = new ArrayList<String>();
args.addAll(List.of("git", "rev-list",
"--format=" + GitCommitMetadata.FORMAT,
"--no-abbrev",
"--no-color",
range));
if (reverse) {
args.add("--reverse");
}
if (paths != null && !paths.isEmpty()) {
args.add("--");
for (var path : paths) {
args.add(path.toString());
}
}
var p = start(args);
var reader = new UnixStreamReader(p.getInputStream());
var result = new ArrayList<CommitMetadata>();

@@ -219,6 +234,56 @@ public List<CommitMetadata> commitMetadata(String range) throws IOException {
return result;
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths, boolean reverse) throws IOException {
return commitMetadata(from.hex() + ".." + to.hex(), paths, reverse);
}

@Override
public List<CommitMetadata> commitMetadata(String range, List<Path> paths) throws IOException {
return commitMetadata(range, paths, false);
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths) throws IOException {
return commitMetadata(from.hex() + ".." + to.hex(), paths, false);
}

@Override
public List<CommitMetadata> commitMetadata(boolean reverse) throws IOException {
return commitMetadata("--all", List.of(), reverse);
}

@Override
public List<CommitMetadata> commitMetadata(String range) throws IOException {
return commitMetadata(range, List.of(), false);
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to) throws IOException {
return commitMetadata(from.hex() + ".." + to.hex(), List.of(), false);
}

@Override
public List<CommitMetadata> commitMetadata(String range, boolean reverse) throws IOException {
return commitMetadata(range, List.of(), reverse);
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to, boolean reverse) throws IOException {
return commitMetadata(from.hex() + ".." + to.hex(), List.of(), reverse);
}

@Override
public List<CommitMetadata> commitMetadata(List<Path> paths) throws IOException {
return commitMetadata("--all", paths, false);
}

@Override
public List<CommitMetadata> commitMetadata(List<Path> paths, boolean reverse) throws IOException {
return commitMetadata("--all", paths, reverse);
}

@Override
public List<CommitMetadata> commitMetadata() throws IOException {
return commitMetadata("--all");
@@ -257,16 +257,34 @@ public Optional<Commit> lookup(Tag t) throws IOException {
}

@Override
public List<CommitMetadata> commitMetadata(String range) throws IOException {
throw new RuntimeException("not implemented yet");
public List<CommitMetadata> commitMetadata(String range, List<Path> paths) throws IOException {
return commitMetadata(range, paths, false);
}

@Override
public List<CommitMetadata> commitMetadata() throws IOException {
public List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths) throws IOException {
return commitMetadata(from.hex() + ":" + to.hex() + "-" + from.hex(), paths, false);
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths, boolean reverse) throws IOException {
return commitMetadata(from.hex() + ":" + to.hex() + "-" + from.hex(), paths, reverse);
}

@Override
public List<CommitMetadata> commitMetadata(String range, List<Path> paths, boolean reverse) throws IOException {
var ext = Files.createTempFile("ext", ".py");
copyResource(EXT_PY, ext);

var p = start("hg", "--config", "extensions.dump=" + ext.toAbsolutePath().toString(), "metadata");
var args = new ArrayList<String>();
args.addAll(List.of("hg", "--config", "extensions.dump=" + ext.toAbsolutePath().toString(), "metadata"));
range = range == null ? "tip:0" : range;
var revset = reverse ? "reverse(" + range + ")" : range;
args.add(revset);
if (paths != null && !paths.isEmpty()) {
args.add(paths.stream().map(Path::toString).collect(Collectors.joining("\t")));
}
var p = start(args);
var reader = new UnixStreamReader(p.getInputStream());
var result = new ArrayList<CommitMetadata>();

@@ -280,6 +298,46 @@ public List<CommitMetadata> commitMetadata() throws IOException {
return result;
}

@Override
public List<CommitMetadata> commitMetadata(String range) throws IOException {
return commitMetadata(range, List.of(), false);
}

@Override
public List<CommitMetadata> commitMetadata(boolean reverse) throws IOException {
return commitMetadata(null, List.of(), reverse);
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to) throws IOException {
return commitMetadata(from.hex() + ":" + to.hex() + "-" + from.hex(), List.of(), false);
}

@Override
public List<CommitMetadata> commitMetadata(String range, boolean reverse) throws IOException {
return commitMetadata(range, List.of(), reverse);
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to, boolean reverse) throws IOException {
return commitMetadata(from.hex() + ":" + to.hex() + "-" + from.hex(), reverse);
}

@Override
public List<CommitMetadata> commitMetadata(List<Path> paths) throws IOException {
return commitMetadata(null, paths, false);
}

@Override
public List<CommitMetadata> commitMetadata(List<Path> paths, boolean reverse) throws IOException {
return commitMetadata(null, paths, reverse);
}

@Override
public List<CommitMetadata> commitMetadata() throws IOException {
return commitMetadata(null, List.of(), false);
}

@Override
public boolean isEmpty() throws IOException {
var numBranches = branches().size();
@@ -304,13 +304,18 @@ def dump(ui, repo, **opts):
__dump(repo, 0, len(repo))

@command(b'metadata', [], b'hg metadata')
def dump(ui, repo, revs=None, **opts):
if revs == None:
revs = b"0:tip"
def metadata(ui, repo, revs, filenames=None, **opts):
if filenames != None:
fnames = filenames.split(b"\t")

for r in revrange(repo, [revs]):
ctx = repo[r]
__dump_metadata(ctx)
if filenames == None:
__dump_metadata(ctx)
else:
modified, added, removed = tuple(ctx.status(ctx.p1(), _match_exact(repo.root, repo.getcwd(), fnames)))[:3]
if modified or added or removed:
__dump_metadata(ctx)

@command(b'ls-tree', [], b'hg ls-tree')
def ls_tree(ui, repo, rev, **opts):
@@ -1197,11 +1197,75 @@ void testCommitMetadata(VCS vcs) throws IOException {
var metadata = r.commitMetadata();
assertEquals(2, metadata.size());

assertEquals(second, metadata.get(0).hash());
assertEquals(List.of("Modified README"), metadata.get(0).message());

assertEquals(first, metadata.get(1).hash());
assertEquals(List.of("Added README"), metadata.get(1).message());
}
}

@ParameterizedTest
@EnumSource(VCS.class)
void testCommitMetadataWithFiles(VCS vcs) throws IOException {
try (var dir = new TemporaryDirectory()) {
var r = Repository.init(dir.path(), vcs);

var readme1 = dir.path().resolve("README_1");
Files.write(readme1, List.of("1"));
r.add(readme1);
var first = r.commit("Added README_1", "duke", "duke@openjdk.java.net");

var readme2 = dir.path().resolve("README_2");
Files.write(readme2, List.of("2"));
r.add(readme2);
var second = r.commit("Added README_2", "duke", "duke@openjdk.java.net");

Files.write(readme2, List.of("3"), WRITE, APPEND);
r.add(readme2);
var third = r.commit("Modified README_2", "duke", "duke@openjdk.java.net");

var metadata = r.commitMetadata(List.of(Path.of("README_1")));
assertEquals(1, metadata.size());
assertEquals(first, metadata.get(0).hash());
assertEquals(List.of("Added README"), metadata.get(0).message());

metadata = r.commitMetadata(List.of(Path.of("README_2")));
assertEquals(2, metadata.size());
assertEquals(third, metadata.get(0).hash());
assertEquals(second, metadata.get(1).hash());

metadata = r.commitMetadata(List.of(Path.of("README_1"), Path.of("README_2")));
assertEquals(3, metadata.size());
assertEquals(third, metadata.get(0).hash());
assertEquals(second, metadata.get(1).hash());
assertEquals(first, metadata.get(2).hash());
}
}

@ParameterizedTest
@EnumSource(VCS.class)
void testCommitMetadataWithReverse(VCS vcs) throws IOException {
try (var dir = new TemporaryDirectory()) {
var r = Repository.init(dir.path(), vcs);

var readme = dir.path().resolve("README");
Files.write(readme, List.of("Hello, world!"));
r.add(readme);
var first = r.commit("Added README", "duke", "duke@openjdk.java.net");

Files.write(readme, List.of("One more line"), WRITE, APPEND);
r.add(readme);
var second = r.commit("Modified README", "duke", "duke@openjdk.java.net");

var metadata = r.commitMetadata();
assertEquals(2, metadata.size());
assertEquals(second, metadata.get(0).hash());
assertEquals(first, metadata.get(1).hash());

metadata = r.commitMetadata(true);
assertEquals(2, metadata.size());
assertEquals(first, metadata.get(0).hash());
assertEquals(second, metadata.get(1).hash());
assertEquals(List.of("Modified README"), metadata.get(1).message());
}
}

@@ -27,17 +27,22 @@
import java.io.*;
import java.nio.file.*;
import java.util.List;
import java.util.stream.Collectors;

class AddedFileView implements FileView {
private final Patch patch;
private final Path out;
private final List<CommitMetadata> commits;
private final MetadataFormatter formatter;
private final List<String> newContent;
private final byte[] binaryContent;
private final WebrevStats stats;

public AddedFileView(ReadOnlyRepository repo, Hash head, Patch patch, Path out) throws IOException {
public AddedFileView(ReadOnlyRepository repo, Hash base, Hash head, List<CommitMetadata> commits, MetadataFormatter formatter, Patch patch, Path out) throws IOException {
this.patch = patch;
this.out = out;
this.commits = commits;
this.formatter = formatter;
if (patch.isTextual()) {
binaryContent = null;
if (head == null) {
@@ -104,6 +109,11 @@ public void render(Writer w) throws IOException {

if (patch.isTextual()) {
w.write("<blockquote>\n");
w.write(" <pre>\n");
w.write(commits.stream()
.map(formatter::format)
.collect(Collectors.joining("\n")));
w.write(" </pre>\n");
w.write(" <span class=\"stat\">\n");
w.write(stats.toString());
w.write(" </span>");