Move parsing of refs/for/.. into MagicBranchInput

Delegate the parsing of the reference string into the inner class.
This will eventually make it easier to write a new version of the
parser that handles a very different syntax than the current way
refs/for/, refs/drafts/, and refs/publish/ are read.

Change-Id: I7ee4aea272b9bc9a806e7eece7590db33c3d4aa1
This commit is contained in:
Shawn Pearce 2013-02-24 15:32:23 -08:00
parent 0795c58a31
commit 7882a0da1a

View File

@ -993,6 +993,55 @@ public class ReceiveCommits {
MailRecipients getMailRecipients() { MailRecipients getMailRecipients() {
return new MailRecipients(reviewer, cc); return new MailRecipients(reviewer, cc);
} }
String parse(Repository repo, Map<String, Ref> advertisedRefs) {
String destBranchName = MagicBranch.getDestBranchName(cmd.getRefName());
if (!destBranchName.startsWith(Constants.R_REFS)) {
destBranchName = Constants.R_HEADS + destBranchName;
}
String head;
try {
head = repo.getFullBranch();
} catch (IOException e) {
log.error("Cannot read HEAD symref", e);
cmd.setResult(REJECTED_OTHER_REASON, "internal error");
return null;
}
// Split the destination branch by branch and topic. The topic
// suffix is entirely optional, so it might not even exist.
int split = destBranchName.length();
for (;;) {
String name = destBranchName.substring(0, split);
if (advertisedRefs.containsKey(name)) {
// We advertised the branch to the client so we know
// the branch exists. Target this branch for the upload.
break;
} else if (head.equals(name)) {
// We didn't advertise the branch, because it doesn't exist yet.
// Allow it anyway as HEAD is a symbolic reference to the name.
break;
}
split = name.lastIndexOf('/', split - 1);
if (split <= Constants.R_REFS.length()) {
String n = destBranchName;
if (n.startsWith(Constants.R_HEADS)) {
n = n.substring(Constants.R_HEADS.length());
}
cmd.setResult(REJECTED_OTHER_REASON, "branch " + n + " not found");
return null;
}
}
if (split < destBranchName.length()) {
String t = destBranchName.substring(split + 1);
topic = Strings.emptyToNull(t);
}
return destBranchName.substring(0, split);
}
} }
private void parseMagicBranch(final ReceiveCommand cmd) { private void parseMagicBranch(final ReceiveCommand cmd) {
@ -1007,60 +1056,17 @@ public class ReceiveCommits {
magicBranch.reviewer.addAll(reviewersFromCommandLine); magicBranch.reviewer.addAll(reviewersFromCommandLine);
magicBranch.cc.addAll(ccFromCommandLine); magicBranch.cc.addAll(ccFromCommandLine);
String destBranchName = MagicBranch.getDestBranchName(cmd.getRefName()); String ref = magicBranch.parse(repo, rp.getAdvertisedRefs());
if (!destBranchName.startsWith(Constants.R_REFS)) { if (ref == null) {
destBranchName = Constants.R_HEADS + destBranchName; // Command was already rejected, but progress needs to update.
} commandProgress.update(1);
final String head;
try {
head = repo.getFullBranch();
} catch (IOException e) {
log.error("Cannot read HEAD symref", e);
reject(cmd, "internal error");
return; return;
} }
// Split the destination branch by branch and topic. The topic magicBranch.dest = new Branch.NameKey(project.getNameKey(), ref);
// suffix is entirely optional, so it might not even exist. magicBranch.ctl = projectControl.controlForRef(ref);
//
int split = destBranchName.length();
for (;;) {
String name = destBranchName.substring(0, split);
if (rp.getAdvertisedRefs().containsKey(name)) {
// We advertised the branch to the client so we know
// the branch exists. Target this branch for the upload.
//
break;
} else if (head.equals(name)) {
// We didn't advertise the branch, because it doesn't exist yet.
// Allow it anyway as HEAD is a symbolic reference to the name.
//
break;
}
split = name.lastIndexOf('/', split - 1);
if (split <= Constants.R_REFS.length()) {
String n = destBranchName;
if (n.startsWith(Constants.R_HEADS))
n = n.substring(Constants.R_HEADS.length());
reject(cmd, "branch " + n + " not found");
return;
}
}
if (split < destBranchName.length()) {
String t = destBranchName.substring(split + 1);
magicBranch.topic = Strings.emptyToNull(t);
} else {
magicBranch.topic = null;
}
magicBranch.dest = new Branch.NameKey(project.getNameKey(), //
destBranchName.substring(0, split));
magicBranch.ctl = projectControl.controlForRef(magicBranch.dest);
if (!magicBranch.ctl.canUpload()) { if (!magicBranch.ctl.canUpload()) {
errors.put(Error.CODE_REVIEW, cmd.getRefName()); errors.put(Error.CODE_REVIEW, ref);
reject(cmd, "cannot upload review"); reject(cmd, "cannot upload review");
return; return;
} }
@ -1072,9 +1078,8 @@ public class ReceiveCommits {
// //
try { try {
final RevWalk walk = rp.getRevWalk(); final RevWalk walk = rp.getRevWalk();
final RevCommit tip = walk.parseCommit(magicBranch.cmd.getNewId()); final RevCommit tip = walk.parseCommit(magicBranch.cmd.getNewId());
Ref targetRef = rp.getAdvertisedRefs().get(destBranchName); Ref targetRef = rp.getAdvertisedRefs().get(magicBranch.ctl.getRefName());
if (targetRef == null || targetRef.getObjectId() == null) { if (targetRef == null || targetRef.getObjectId() == null) {
// The destination branch does not yet exist. Assume the // The destination branch does not yet exist. Assume the
// history being sent for review will start it and thus // history being sent for review will start it and thus
@ -1082,7 +1087,6 @@ public class ReceiveCommits {
return; return;
} }
final RevCommit h = walk.parseCommit(targetRef.getObjectId()); final RevCommit h = walk.parseCommit(targetRef.getObjectId());
final RevFilter oldRevFilter = walk.getRevFilter(); final RevFilter oldRevFilter = walk.getRevFilter();
try { try {
walk.reset(); walk.reset();