aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Borowitz2012-02-07 18:16:40 (EST)
committerShawn O. Pearce2012-02-29 22:09:22 (EST)
commit4bf22ff6e840b190e0a019e86cce19001dc587f4 (patch)
tree4873d77308b48f929e4f0007ce0b986074c115f0
parent4de8a84671a16a3d70ed074b6f02326fb85af1d1 (diff)
downloadjgit-4bf22ff6e840b190e0a019e86cce19001dc587f4.zip
jgit-4bf22ff6e840b190e0a019e86cce19001dc587f4.tar.gz
jgit-4bf22ff6e840b190e0a019e86cce19001dc587f4.tar.bz2
Execute ReceiveCommands via a method rather than in ReceivePackrefs/changes/81/5081/7
This allows a PreReceiveHook to easily "take over" all of the ReceiveCommands passed to it, preventing any of them from being handled within the ReceivePack core. Change-Id: I2a8c1fc44e8dcadf22cd97a8ec4ee79d4d9d08f1
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceiveCommand.java78
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java68
2 files changed, 79 insertions, 67 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceiveCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceiveCommand.java
index 60ebeab..da19f4c 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceiveCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceiveCommand.java
@@ -43,8 +43,13 @@
package org.eclipse.jgit.transport;
+import java.io.IOException;
+import java.text.MessageFormat;
+
+import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.RefUpdate;
/**
* A command being processed by {@link ReceivePack}.
@@ -213,6 +218,47 @@ public class ReceiveCommand {
message = m;
}
+ /**
+ * Execute this command during a receive-pack session.
+ * <p>
+ * Sets the status of the command as a side effect.
+ *
+ * @param rp
+ * receive-pack session.
+ */
+ public void execute(final ReceivePack rp) {
+ try {
+ final RefUpdate ru = rp.getRepository().updateRef(getRefName());
+ ru.setRefLogIdent(rp.getRefLogIdent());
+ switch (getType()) {
+ case DELETE:
+ if (!ObjectId.zeroId().equals(getOldId())) {
+ // We can only do a CAS style delete if the client
+ // didn't bork its delete request by sending the
+ // wrong zero id rather than the advertised one.
+ //
+ ru.setExpectedOldObjectId(getOldId());
+ }
+ ru.setForceUpdate(true);
+ setResult(ru.delete(rp.getRevWalk()));
+ break;
+
+ case CREATE:
+ case UPDATE:
+ case UPDATE_NONFASTFORWARD:
+ ru.setForceUpdate(rp.isAllowNonFastForwards());
+ ru.setExpectedOldObjectId(getOldId());
+ ru.setNewObjectId(getNewId());
+ ru.setRefLogMessage("push", true);
+ setResult(ru.update(rp.getRevWalk()));
+ break;
+ }
+ } catch (IOException err) {
+ setResult(Result.REJECTED_OTHER_REASON, MessageFormat.format(
+ JGitText.get().lockError, err.getMessage()));
+ }
+ }
+
void setRef(final Ref r) {
ref = r;
}
@@ -221,6 +267,38 @@ public class ReceiveCommand {
type = t;
}
+ private void setResult(final RefUpdate.Result r) {
+ switch (r) {
+ case NOT_ATTEMPTED:
+ setResult(Result.NOT_ATTEMPTED);
+ break;
+
+ case LOCK_FAILURE:
+ case IO_FAILURE:
+ setResult(Result.LOCK_FAILURE);
+ break;
+
+ case NO_CHANGE:
+ case NEW:
+ case FORCED:
+ case FAST_FORWARD:
+ setResult(Result.OK);
+ break;
+
+ case REJECTED:
+ setResult(Result.REJECTED_NONFASTFORWARD);
+ break;
+
+ case REJECTED_CURRENT_BRANCH:
+ setResult(Result.REJECTED_CURRENT_BRANCH);
+ break;
+
+ default:
+ setResult(Result.REJECTED_OTHER_REASON, r.name());
+ break;
+ }
+ }
+
@Override
public String toString() {
return getType().name() + ": " + getOldId().name() + " "
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java
index 04f8946..0bf84ef 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java
@@ -77,7 +77,6 @@ import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Ref;
-import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Config.SectionParser;
import org.eclipse.jgit.revwalk.ObjectWalk;
@@ -1056,76 +1055,11 @@ public class ReceivePack {
updating.beginTask(JGitText.get().updatingReferences, toApply.size());
for (ReceiveCommand cmd : toApply) {
updating.update(1);
- execute(cmd);
+ cmd.execute(this);
}
updating.endTask();
}
- private void execute(final ReceiveCommand cmd) {
- try {
- final RefUpdate ru = db.updateRef(cmd.getRefName());
- ru.setRefLogIdent(getRefLogIdent());
- switch (cmd.getType()) {
- case DELETE:
- if (!ObjectId.zeroId().equals(cmd.getOldId())) {
- // We can only do a CAS style delete if the client
- // didn't bork its delete request by sending the
- // wrong zero id rather than the advertised one.
- //
- ru.setExpectedOldObjectId(cmd.getOldId());
- }
- ru.setForceUpdate(true);
- status(cmd, ru.delete(walk));
- break;
-
- case CREATE:
- case UPDATE:
- case UPDATE_NONFASTFORWARD:
- ru.setForceUpdate(isAllowNonFastForwards());
- ru.setExpectedOldObjectId(cmd.getOldId());
- ru.setNewObjectId(cmd.getNewId());
- ru.setRefLogMessage("push", true);
- status(cmd, ru.update(walk));
- break;
- }
- } catch (IOException err) {
- cmd.setResult(Result.REJECTED_OTHER_REASON, MessageFormat.format(
- JGitText.get().lockError, err.getMessage()));
- }
- }
-
- private void status(final ReceiveCommand cmd, final RefUpdate.Result result) {
- switch (result) {
- case NOT_ATTEMPTED:
- cmd.setResult(Result.NOT_ATTEMPTED);
- break;
-
- case LOCK_FAILURE:
- case IO_FAILURE:
- cmd.setResult(Result.LOCK_FAILURE);
- break;
-
- case NO_CHANGE:
- case NEW:
- case FORCED:
- case FAST_FORWARD:
- cmd.setResult(Result.OK);
- break;
-
- case REJECTED:
- cmd.setResult(Result.REJECTED_NONFASTFORWARD);
- break;
-
- case REJECTED_CURRENT_BRANCH:
- cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
- break;
-
- default:
- cmd.setResult(Result.REJECTED_OTHER_REASON, result.name());
- break;
- }
- }
-
private List<ReceiveCommand> filterCommands(final Result want) {
final List<ReceiveCommand> r = new ArrayList<ReceiveCommand>(commands
.size());