}
}
private ObjectId resolve(final RevWalk rw, final String revstr) throws IOException {
char[] rev = revstr.toCharArray();
RevObject ref = null;
for (int i = 0; i < rev.length; ++i) {
switch (rev[i]) {
case '^':
if (ref == null) {
ref = parseSimple(rw, new String(rev, 0, i));
if (ref == null)
return null;
}
if (i + 1 < rev.length) {
switch (rev[i + 1]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
int j;
ref = rw.parseCommit(ref);
for (j = i + 1; j < rev.length; ++j) {
if (!Character.isDigit(rev[j]))
break;
}
String parentnum = new String(rev, i + 1, j - i - 1);
int pnum;
try {
pnum = Integer.parseInt(parentnum);
} catch (NumberFormatException e) {
throw new RevisionSyntaxException(
JGitText.get().invalidCommitParentNumber,
revstr);
}
if (pnum != 0) {
RevCommit commit = (RevCommit) ref;
if (pnum > commit.getParentCount())
ref = null;
else
ref = commit.getParent(pnum - 1);
}
i = j - 1;
break;
case '{':
int k;
String item = null;
for (k = i + 2; k < rev.length; ++k) {
if (rev[k] == '}') {
item = new String(rev, i + 2, k - i - 2);
break;
}
}
i = k;
if (item != null)
if (item.equals("tree")) {
ref = rw.parseTree(ref);
} else if (item.equals("commit")) {
ref = rw.parseCommit(ref);
} else if (item.equals("blob")) {
ref = rw.peel(ref);
if (!(ref instanceof RevBlob))
throw new IncorrectObjectTypeException(ref,
Constants.TYPE_BLOB);
} else if (item.equals("")) {
ref = rw.peel(ref);
} else
throw new RevisionSyntaxException(revstr);
else
throw new RevisionSyntaxException(revstr);
break;
default:
ref = rw.parseAny(ref);
if (ref instanceof RevCommit) {
RevCommit commit = ((RevCommit) ref);
if (commit.getParentCount() == 0)
ref = null;
else
ref = commit.getParent(0);
} else
throw new IncorrectObjectTypeException(ref,
Constants.TYPE_COMMIT);
}
} else {
ref = rw.peel(ref);
if (ref instanceof RevCommit) {
RevCommit commit = ((RevCommit) ref);
if (commit.getParentCount() == 0)
ref = null;
else
ref = commit.getParent(0);
} else
throw new IncorrectObjectTypeException(ref,
Constants.TYPE_COMMIT);
}
break;
case '~':
if (ref == null) {
ref = parseSimple(rw, new String(rev, 0, i));
if (ref == null)
return null;
}
ref = rw.peel(ref);
if (!(ref instanceof RevCommit))
throw new IncorrectObjectTypeException(ref,
Constants.TYPE_COMMIT);
int l;
for (l = i + 1; l < rev.length; ++l) {
if (!Character.isDigit(rev[l]))
break;
}
int dist;
if (l - i > 1) {
String distnum = new String(rev, i + 1, l - i - 1);
try {
dist = Integer.parseInt(distnum);
} catch (NumberFormatException e) {
throw new RevisionSyntaxException(
JGitText.get().invalidAncestryLength, revstr);
}
} else
dist = 1;
while (dist > 0) {
RevCommit commit = (RevCommit) ref;
if (commit.getParentCount() == 0) {
ref = null;
break;
}
commit = commit.getParent(0);
rw.parseHeaders(commit);
ref = commit;
--dist;
}
i = l - 1;
break;
case '@':
int m;
String time = null;
for (m = i + 2; m < rev.length; ++m) {
if (rev[m] == '}') {
time = new String(rev, i + 2, m - i - 2);
break;
}
}
if (time != null) {
String refName = new String(rev, 0, i);
Ref resolved = getRefDatabase().getRef(refName);
if (resolved == null)
return null;
ref = resolveReflog(rw, resolved, time);
i = m;
} else
i = m - 1;
break;
case ':': {
RevTree tree;
if (ref == null) {
// We might not yet have parsed the left hand side.
ObjectId id;
try {
if (i == 0)
id = resolve(rw, Constants.HEAD);
else
id = resolve(rw, new String(rev, 0, i));
} catch (RevisionSyntaxException badSyntax) {
throw new RevisionSyntaxException(revstr);
}
if (id == null)
return null;
tree = rw.parseTree(id);
} else {
tree = rw.parseTree(ref);
}
if (i == rev.length - 1)
return tree.copy();
TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(),
new String(rev, i + 1, rev.length - i - 1), tree);
return tw != null ? tw.getObjectId(0) : null;
}
default:
if (ref != null)
throw new RevisionSyntaxException(revstr);
}
}
return ref != null ? ref.copy() : resolveSimple(revstr);
}