aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZhen Chen <czhen@google.com>2017-07-20 17:20:55 -0700
committerZhen Chen <czhen@google.com>2017-07-26 10:12:37 -0700
commitb0695e5b7b639797280916201e49076784377bd5 (patch)
treec3404890d8a533fe218b6d5a16ac60fc221a517e
parent673acfc6bdff0e339e47a7f950a44580fa74a637 (diff)
downloadjgit-b0695e5b7b639797280916201e49076784377bd5.tar.gz
jgit-b0695e5b7b639797280916201e49076784377bd5.zip
Add commit check for head references
Make sure all refs/heads/* point to a commit object. Change-Id: I9c7cf347aaf63d5ef604d520c2383c6cf3043890 Signed-off-by: Zhen Chen <czhen@google.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsFsckTest.java26
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/fsck/FsckError.java7
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsFsck.java8
3 files changed, 41 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsFsckTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsFsckTest.java
index 8d223c80d9..804d744ae2 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsFsckTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsFsckTest.java
@@ -240,4 +240,30 @@ public class DfsFsckTest {
assertEquals(errors.getMissingObjects().size(), 1);
assertEquals(errors.getMissingObjects().iterator().next(), blobId);
}
+
+ @Test
+ public void testNonCommitHead() throws Exception {
+ RevCommit commit0 = git.commit().message("0").create();
+ StringBuilder b = new StringBuilder();
+ b.append("object ");
+ b.append(commit0.getName());
+ b.append('\n');
+ b.append("type commit\n");
+ b.append("tag test-tag\n");
+ b.append("tagger A. U. Thor <author@localhost> 1 +0000\n");
+
+ byte[] data = encodeASCII(b.toString());
+ ObjectId tagId = ins.insert(Constants.OBJ_TAG, data);
+ ins.flush();
+
+ git.update("master", tagId);
+
+ DfsFsck fsck = new DfsFsck(repo);
+ FsckError errors = fsck.check(null);
+ assertEquals(errors.getCorruptObjects().size(), 0);
+ assertEquals(errors.getNonCommitHeads().size(), 1);
+ assertEquals(errors.getNonCommitHeads().iterator().next(),
+ "refs/heads/master");
+ }
+
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/fsck/FsckError.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/fsck/FsckError.java
index b7aac1630b..588ed9bf8a 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/fsck/FsckError.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/fsck/FsckError.java
@@ -128,6 +128,8 @@ public class FsckError {
private final Set<CorruptIndex> corruptIndices = new HashSet<>();
+ private final Set<String> nonCommitHeads = new HashSet<>();
+
/** @return corrupt objects from all pack files. */
public Set<CorruptObject> getCorruptObjects() {
return corruptObjects;
@@ -142,4 +144,9 @@ public class FsckError {
public Set<CorruptIndex> getCorruptIndices() {
return corruptIndices;
}
+
+ /** @return refs/heads/* point to non-commit object. */
+ public Set<String> getNonCommitHeads() {
+ return nonCommitHeads;
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsFsck.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsFsck.java
index 9549dbb2ea..f90ba7d986 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsFsck.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsFsck.java
@@ -52,6 +52,7 @@ import org.eclipse.jgit.internal.fsck.FsckError;
import org.eclipse.jgit.internal.fsck.FsckError.CorruptIndex;
import org.eclipse.jgit.internal.fsck.FsckPackParser;
import org.eclipse.jgit.internal.storage.pack.PackExt;
+import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectChecker;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Ref;
@@ -123,6 +124,13 @@ public class DfsFsck {
for (Ref r : repo.getAllRefs().values()) {
try {
RevObject tip = ow.parseAny(r.getObjectId());
+ if (r.getLeaf().getName().startsWith(Constants.R_HEADS)) {
+ // check if heads point to a commit object
+ if (tip.getType() != Constants.OBJ_COMMIT) {
+ errors.getNonCommitHeads()
+ .add(r.getLeaf().getName());
+ }
+ }
ow.markStart(tip);
ow.checkConnectivity();
ow.markUninteresting(tip);