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: allow diffs against initial revision #726

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
@@ -41,6 +41,7 @@ public class GitRepository implements Repository {
private final Path dir;
private final Logger log = Logger.getLogger("org.openjdk.skara.vcs.git");
private Path cachedRoot = null;
private static final Hash EMPTY_TREE = new Hash("4b825dc642cb6eb9a060e54bf8d69288fbee4904");

private java.lang.Process start(String... cmd) throws IOException {
return start(Arrays.asList(cmd));
@@ -977,7 +978,11 @@ public List<StatusEntry> status(Hash from, Hash to) throws IOException {
"--no-abbrev",
"--no-color"));
if (from != null) {
cmd.add(from.hex());
if (from.equals(Hash.zero())) {
cmd.add(EMPTY_TREE.hex());
} else {
cmd.add(from.hex());
}
}
if (to != null) {
cmd.add(to.hex());
@@ -1025,8 +1030,14 @@ public Diff diff(Hash from, Hash to, List<Path> files, int similarity) throws IO
"--raw",
"--no-abbrev",
"--unified=0",
"--no-color",
from.hex()));
"--no-color"));
if (from != null) {
if (from.equals(Hash.zero())) {
cmd.add(EMPTY_TREE.hex());
} else {
cmd.add(from.hex());
}
}
if (to != null) {
cmd.add(to.hex());
}
@@ -1570,6 +1570,48 @@ void testDiffOnFilenamesWithSpace(VCS vcs) throws IOException {
}
}

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

var readme = dir.path().resolve("README.md");
Files.writeString(readme, "Hello world\n");
r.add(readme);
var hash = r.commit("Added readme", "duke", "duke@openjdk.java.net");
var commit = r.lookup(hash).orElseThrow();
var parent = commit.parents().get(0);

var diff = r.diff(parent, commit.hash());
assertEquals(parent, diff.from());
assertEquals(hash, diff.to());
}
}

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

var readme = dir.path().resolve("README.md");
Files.writeString(readme, "Hello world\n");
r.add(readme);
var hash = r.commit("Added readme", "duke", "duke@openjdk.java.net");
var commit = r.lookup(hash).orElseThrow();
var parent = commit.parents().get(0);

var entries = r.status(parent, commit.hash());
assertEquals(1, entries.size());
var entry = entries.get(0);
assertTrue(entry.status().isAdded());
assertEquals(Path.of("README.md"), entry.target().path().get());
}
}

@Test
void testSingleEmptyCommit() throws IOException, InterruptedException {
try (var dir = new TemporaryDirectory()) {