- 1);
int pnum;
try {
pnum = Integer.parseInt(parentnum);
} catch (NumberFormatException e) {
throw new RevisionSyntaxException(
JGitText.get().invalidCommitParentNumber,
revstr);
}
if (pnum != 0) {
RevCommit commit = (RevCommit) rev;
if (pnum > commit.getParentCount())
rev = null;
else
rev = commit.getParent(pnum - 1);
}
i = j - 1;
done = j;
break;
case '{':
int k;
String item = null;
for (k = i + 2; k < revChars.length; ++k) {
if (revChars[k] == '}') {
item = new String(revChars, i + 2, k - i - 2);
break;
}
}
i = k;
if (item != null)
if (item.equals("tree")) { //$NON-NLS-1$
rev = rw.parseTree(rev);
} else if (item.equals("commit")) { //$NON-NLS-1$
rev = rw.parseCommit(rev);
} else if (item.equals("blob")) { //$NON-NLS-1$
rev = rw.peel(rev);
if (!(rev instanceof RevBlob))
throw new IncorrectObjectTypeException(rev,
Constants.TYPE_BLOB);
} else if (item.equals("")) { //$NON-NLS-1$
rev = rw.peel(rev);
} else
throw new RevisionSyntaxException(revstr);
else
throw new RevisionSyntaxException(revstr);
done = k;
break;
default:
rev = rw.peel(rev);
if (rev instanceof RevCommit) {
RevCommit commit = ((RevCommit) rev);
if (commit.getParentCount() == 0)
rev = null;
else
rev = commit.getParent(0);
} else
throw new IncorrectObjectTypeException(rev,
Constants.TYPE_COMMIT);
}
} else {
rev = rw.peel(rev);
if (rev instanceof RevCommit) {
RevCommit commit = ((RevCommit) rev);
if (commit.getParentCount() == 0)
rev = null;
else
rev = commit.getParent(0);
} else
throw new IncorrectObjectTypeException(rev,
Constants.TYPE_COMMIT);
}
done = i + 1;
break;
case '~':
if (rev == null) {
if (name == null)
if (done == 0)
name = new String(revChars, done, i);
else {
done = i + 1;
break;
}
rev = parseSimple(rw, name);
name = null;
if (rev == null)
return null;
}
rev = rw.peel(rev);
if (!(rev instanceof RevCommit))
throw new IncorrectObjectTypeException(rev,
Constants.TYPE_COMMIT);
int l;
for (l = i + 1; l < revChars.length; ++l) {
if (!Character.isDigit(revChars[l]))
break;
}
int dist;
if (l - i > 1) {
String distnum = new String(revChars, 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) rev;
if (commit.getParentCount() == 0) {
rev = null;
break;
}
commit = commit.getParent(0);
rw.parseHeaders(commit);
rev = commit;
--dist;
}
i = l - 1;
done = l;
break;
case '@':
if (rev != null)
throw new RevisionSyntaxException(revstr);
if (i + 1 < revChars.length && revChars[i + 1] != '{')
continue;
int m;
String time = null;
for (m = i + 2; m < revChars.length; ++m) {
if (revChars[m] == '}') {
time = new String(revChars, i + 2, m - i - 2);
break;
}
}
if (time != null) {
if (time.equals("upstream")) { //$NON-NLS-1$
if (name == null)
name = new String(revChars, done, i);
if (name.equals("")) //$NON-NLS-1$
// Currently checked out branch, HEAD if
// detached
name = Constants.HEAD;
if (!Repository.isValidRefName("x/" + name)) //$NON-NLS-1$
throw new RevisionSyntaxException(revstr);
Ref ref = getRef(name);
name = null;
if (ref == null)
return null;
if (ref.isSymbolic())
ref = ref.getLeaf();
name = ref.getName();
RemoteConfig remoteConfig;
try {
remoteConfig = new RemoteConfig(getConfig(),
"origin"); //$NON-NLS-1$
} catch (URISyntaxException e) {
throw new RevisionSyntaxException(revstr);
}
String remoteBranchName = getConfig()
.getString(
ConfigConstants.CONFIG_BRANCH_SECTION,
Repository.shortenRefName(ref.getName()),
ConfigConstants.CONFIG_KEY_MERGE);
List<RefSpec> fetchRefSpecs = remoteConfig
.getFetchRefSpecs();
for (RefSpec refSpec : fetchRefSpecs) {
if (refSpec.matchSource(remoteBranchName)) {
RefSpec expandFromSource = refSpec
.expandFromSource(remoteBranchName);
name = expandFromSource.getDestination();
break;
}
}
if (name == null)
throw new RevisionSyntaxException(revstr);
} else if (time.matches("^-\\d+$")) { //$NON-NLS-1$
if (name != null)
throw new RevisionSyntaxException(revstr);
else {
String previousCheckout = resolveReflogCheckout(-Integer
.parseInt(time));
if (ObjectId.isId(previousCheckout))
rev = parseSimple(rw, previousCheckout);
else
name = previousCheckout;
}
} else {
if (name == null)
name = new String(revChars, done, i);
if (name.equals("")) //$NON-NLS-1$
name = Constants.HEAD;
if (!Repository.isValidRefName("x/" + name)) //$NON-NLS-1$
throw new RevisionSyntaxException(revstr);
Ref ref = getRef(name);
name = null;
if (ref == null)
return null;
// @{n} means current branch, not HEAD@{1} unless
// detached
if (ref.isSymbolic())
ref = ref.getLeaf();
rev = resolveReflog(rw, ref, time);
}
i = m;
} else
throw new RevisionSyntaxException(revstr);
break;
case ':': {
RevTree tree;
if (rev == null) {
if (name == null)
name = new String(revChars, done, i);
if (name.equals("")) //$NON-NLS-1$
name = Constants.HEAD;
rev = parseSimple(rw, name);
name = null;
}
if (rev == null)
return null;
tree = rw.parseTree(rev);
if (i == revChars.length - 1)
return tree.copy();
TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(),
new String(revChars, i + 1, revChars.length - i - 1),
tree);
return tw != null ? tw.getObjectId(0) : null;
}
default:
if (rev != null)
throw new RevisionSyntaxException(revstr);
}
}
if (rev != null)
return rev.copy();
if (name != null)
return name;
if (done == revstr.length())
return null;
name = revstr.substring(done);
if (!Repository.isValidRefName("x/" + name)) //$NON-NLS-1$
throw new RevisionSyntaxException(revstr);
if (getRef(name) != null)
return name;
return resolveSimple(name);
}