GitReferenceUpdated: Add helper methods to get update type

The type of ref update is already known on the server before the ref
update event is fired. Pass this information into the event, so its
consumers can get it without having to perform operations on the git
repository.

Change-Id: I5d90a157c316ac0c2b65ba3deb2cc2f62628df8a
This commit is contained in:
David Pursehouse
2015-04-13 17:43:51 +09:00
parent c2ac351981
commit 414681fa42
8 changed files with 52 additions and 15 deletions

View File

@@ -25,6 +25,9 @@ public interface GitReferenceUpdatedListener {
String getRefName();
String getOldObjectId();
String getNewObjectId();
boolean isCreate();
boolean isDelete();
boolean isNonFastForward();
}
void onGitReferenceUpdated(Event event);

View File

@@ -457,7 +457,7 @@ public class ChangeUtil {
throw new IOException("Failed to delete ref " + patch.getRefName() +
" in " + repo.getDirectory() + ": " + update.getResult());
}
gitRefUpdated.fire(change.getProject(), update);
gitRefUpdated.fire(change.getProject(), update, ReceiveCommand.Type.DELETE);
} finally {
repo.close();
}

View File

@@ -46,16 +46,22 @@ public class GitReferenceUpdated {
this.listeners = listeners;
}
public void fire(Project.NameKey project, RefUpdate refUpdate) {
public void fire(Project.NameKey project, RefUpdate refUpdate,
ReceiveCommand.Type type) {
fire(project, refUpdate.getName(), refUpdate.getOldObjectId(),
refUpdate.getNewObjectId());
refUpdate.getNewObjectId(), type);
}
public void fire(Project.NameKey project, String ref,
ObjectId oldObjectId, ObjectId newObjectId) {
public void fire(Project.NameKey project, RefUpdate refUpdate) {
fire(project, refUpdate.getName(), refUpdate.getOldObjectId(),
refUpdate.getNewObjectId(), ReceiveCommand.Type.UPDATE);
}
public void fire(Project.NameKey project, String ref, ObjectId oldObjectId,
ObjectId newObjectId, ReceiveCommand.Type type) {
ObjectId o = oldObjectId != null ? oldObjectId : ObjectId.zeroId();
ObjectId n = newObjectId != null ? newObjectId : ObjectId.zeroId();
Event event = new Event(project, ref, o.name(), n.name());
Event event = new Event(project, ref, o.name(), n.name(), type);
for (GitReferenceUpdatedListener l : listeners) {
try {
l.onGitReferenceUpdated(event);
@@ -65,10 +71,19 @@ public class GitReferenceUpdated {
}
}
public void fire(Project.NameKey project, String ref, ObjectId oldObjectId,
ObjectId newObjectId) {
fire(project, ref, oldObjectId, newObjectId, ReceiveCommand.Type.UPDATE);
}
public void fire(Project.NameKey project, ReceiveCommand cmd) {
fire(project, cmd.getRefName(), cmd.getOldId(), cmd.getNewId(), cmd.getType());
}
public void fire(Project.NameKey project, BatchRefUpdate batchRefUpdate) {
for (ReceiveCommand cmd : batchRefUpdate.getCommands()) {
if (cmd.getResult() == ReceiveCommand.Result.OK) {
fire(project, cmd.getRefName(), cmd.getOldId(), cmd.getNewId());
fire(project, cmd);
}
}
}
@@ -78,13 +93,16 @@ public class GitReferenceUpdated {
private final String ref;
private final String oldObjectId;
private final String newObjectId;
private final ReceiveCommand.Type type;
Event(Project.NameKey project, String ref,
String oldObjectId, String newObjectId) {
String oldObjectId, String newObjectId,
ReceiveCommand.Type type) {
this.projectName = project.get();
this.ref = ref;
this.oldObjectId = oldObjectId;
this.newObjectId = newObjectId;
this.type = type;
}
@Override
@@ -107,6 +125,21 @@ public class GitReferenceUpdated {
return newObjectId;
}
@Override
public boolean isCreate() {
return type == ReceiveCommand.Type.CREATE;
}
@Override
public boolean isDelete() {
return type == ReceiveCommand.Type.DELETE;
}
@Override
public boolean isNonFastForward() {
return type == ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
}
@Override
public String toString() {
return String.format("%s[%s,%s: %s -> %s]", getClass().getSimpleName(),

View File

@@ -641,8 +641,7 @@ public class ReceiveCommits {
// We only fire gitRefUpdated for direct refs updates.
// Events for change refs are fired when they are created.
//
gitRefUpdated.fire(project.getNameKey(), c.getRefName(),
c.getOldId(), c.getNewId());
gitRefUpdated.fire(project.getNameKey(), c);
hooks.doRefUpdatedHook(
new Branch.NameKey(project.getNameKey(), c.getRefName()),
c.getOldId(),

View File

@@ -47,6 +47,7 @@ import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.ObjectWalk;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -149,7 +150,7 @@ public class CreateBranch implements RestModifyView<ProjectResource, Input> {
case FAST_FORWARD:
case NEW:
case NO_CHANGE:
referenceUpdated.fire(name.getParentKey(), u);
referenceUpdated.fire(name.getParentKey(), u, ReceiveCommand.Type.CREATE);
hooks.doRefUpdatedHook(name, u, identifiedUser.get().getAccount());
break;
case LOCK_FAILURE:

View File

@@ -69,6 +69,7 @@ import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -384,7 +385,7 @@ public class CreateProject implements RestModifyView<TopLevelResource, ProjectIn
Result result = ru.update();
switch (result) {
case NEW:
referenceUpdated.fire(project, ru);
referenceUpdated.fire(project, ru, ReceiveCommand.Type.CREATE);
break;
default: {
throw new IOException(String.format(

View File

@@ -35,6 +35,7 @@ import com.google.inject.Singleton;
import org.eclipse.jgit.errors.LockFailedException;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -113,7 +114,7 @@ public class DeleteBranch implements RestModifyView<BranchResource, Input>{
case NO_CHANGE:
case FAST_FORWARD:
case FORCED:
referenceUpdated.fire(rsrc.getNameKey(), u);
referenceUpdated.fire(rsrc.getNameKey(), u, ReceiveCommand.Type.DELETE);
hooks.doRefUpdatedHook(rsrc.getBranchKey(), u, identifiedUser.get().getAccount());
ResultSet<SubmoduleSubscription> submoduleSubscriptions =
dbProvider.get().submoduleSubscriptions().bySuperProject(rsrc.getBranchKey());

View File

@@ -165,8 +165,7 @@ class DeleteBranches implements RestModifyView<ProjectResource, Input> {
private void postDeletion(ProjectResource project, ReceiveCommand cmd)
throws OrmException {
referenceUpdated.fire(project.getNameKey(), cmd.getRefName(),
cmd.getOldId(), cmd.getNewId());
referenceUpdated.fire(project.getNameKey(), cmd);
Branch.NameKey branchKey =
new Branch.NameKey(project.getNameKey(), cmd.getRefName());
hooks.doRefUpdatedHook(branchKey, cmd.getOldId(), cmd.getNewId(),