Fix PatchListLoader for submodules

Submodule SHA-1s don't exist in the repo, so this was throwing
MissingObjectExceptions trying to load the PatchListEntry. Ensure the
object is blob before attempting to read its size, and treat non-blobs
as size 0 for the purposes of computing a size delta.

Change-Id: I239329e1bdede7db2b436628cd78967392dca164
This commit is contained in:
Dave Borowitz
2015-10-14 13:39:07 -04:00
parent 99af58e9d1
commit 0e96e8f1f4

View File

@@ -196,15 +196,16 @@ public class PatchListLoader implements Callable<PatchList> {
entries.add(newCommitMessage(cmp, reader, entries.add(newCommitMessage(cmp, reader,
againstParent ? null : aCommit, b)); againstParent ? null : aCommit, b));
for (int i = 0; i < cnt; i++) { for (int i = 0; i < cnt; i++) {
DiffEntry diffEntry = diffEntries.get(i); DiffEntry e = diffEntries.get(i);
if (paths == null || paths.contains(diffEntry.getNewPath()) if (paths == null || paths.contains(e.getNewPath())
|| paths.contains(diffEntry.getOldPath())) { || paths.contains(e.getOldPath())) {
FileHeader fh = toFileHeader(key, df, diffEntry); FileHeader fh = toFileHeader(key, df, e);
long sizeDelta = long oldSize =
getFileSize(repo, reader, diffEntry.getNewPath(), bTree) getFileSize(repo, reader, e.getOldMode(), e.getOldPath(), aTree);
- getFileSize(repo, reader, diffEntry.getOldPath(), aTree); long newSize =
entries.add(newEntry(aTree, fh, sizeDelta)); getFileSize(repo, reader, e.getNewMode(), e.getNewPath(), bTree);
entries.add(newEntry(aTree, fh, newSize - oldSize));
} }
} }
return new PatchList(a, b, againstParent, return new PatchList(a, b, againstParent,
@@ -213,7 +214,10 @@ public class PatchListLoader implements Callable<PatchList> {
} }
private static long getFileSize(Repository repo, ObjectReader reader, private static long getFileSize(Repository repo, ObjectReader reader,
String path, RevTree t) throws IOException { FileMode mode, String path, RevTree t) throws IOException {
if (!isBlob(mode)) {
return 0;
}
try (TreeWalk tw = TreeWalk.forPath(reader, path, t)) { try (TreeWalk tw = TreeWalk.forPath(reader, path, t)) {
return tw != null return tw != null
? repo.open(tw.getObjectId(0), OBJ_BLOB).getSize() ? repo.open(tw.getObjectId(0), OBJ_BLOB).getSize()
@@ -221,6 +225,11 @@ public class PatchListLoader implements Callable<PatchList> {
} }
} }
private static boolean isBlob(FileMode mode) {
int t = mode.getBits() & FileMode.TYPE_MASK;
return t == FileMode.TYPE_FILE || t == FileMode.TYPE_SYMLINK;
}
private FileHeader toFileHeader(PatchListKey key, private FileHeader toFileHeader(PatchListKey key,
final DiffFormatter diffFormatter, final DiffEntry diffEntry) final DiffFormatter diffFormatter, final DiffEntry diffEntry)
throws IOException { throws IOException {