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:
@@ -25,6 +25,9 @@ public interface GitReferenceUpdatedListener {
|
|||||||
String getRefName();
|
String getRefName();
|
||||||
String getOldObjectId();
|
String getOldObjectId();
|
||||||
String getNewObjectId();
|
String getNewObjectId();
|
||||||
|
boolean isCreate();
|
||||||
|
boolean isDelete();
|
||||||
|
boolean isNonFastForward();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onGitReferenceUpdated(Event event);
|
void onGitReferenceUpdated(Event event);
|
||||||
|
@@ -457,7 +457,7 @@ public class ChangeUtil {
|
|||||||
throw new IOException("Failed to delete ref " + patch.getRefName() +
|
throw new IOException("Failed to delete ref " + patch.getRefName() +
|
||||||
" in " + repo.getDirectory() + ": " + update.getResult());
|
" in " + repo.getDirectory() + ": " + update.getResult());
|
||||||
}
|
}
|
||||||
gitRefUpdated.fire(change.getProject(), update);
|
gitRefUpdated.fire(change.getProject(), update, ReceiveCommand.Type.DELETE);
|
||||||
} finally {
|
} finally {
|
||||||
repo.close();
|
repo.close();
|
||||||
}
|
}
|
||||||
|
@@ -46,16 +46,22 @@ public class GitReferenceUpdated {
|
|||||||
this.listeners = listeners;
|
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(),
|
fire(project, refUpdate.getName(), refUpdate.getOldObjectId(),
|
||||||
refUpdate.getNewObjectId());
|
refUpdate.getNewObjectId(), type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fire(Project.NameKey project, String ref,
|
public void fire(Project.NameKey project, RefUpdate refUpdate) {
|
||||||
ObjectId oldObjectId, ObjectId newObjectId) {
|
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 o = oldObjectId != null ? oldObjectId : ObjectId.zeroId();
|
||||||
ObjectId n = newObjectId != null ? newObjectId : 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) {
|
for (GitReferenceUpdatedListener l : listeners) {
|
||||||
try {
|
try {
|
||||||
l.onGitReferenceUpdated(event);
|
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) {
|
public void fire(Project.NameKey project, BatchRefUpdate batchRefUpdate) {
|
||||||
for (ReceiveCommand cmd : batchRefUpdate.getCommands()) {
|
for (ReceiveCommand cmd : batchRefUpdate.getCommands()) {
|
||||||
if (cmd.getResult() == ReceiveCommand.Result.OK) {
|
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 ref;
|
||||||
private final String oldObjectId;
|
private final String oldObjectId;
|
||||||
private final String newObjectId;
|
private final String newObjectId;
|
||||||
|
private final ReceiveCommand.Type type;
|
||||||
|
|
||||||
Event(Project.NameKey project, String ref,
|
Event(Project.NameKey project, String ref,
|
||||||
String oldObjectId, String newObjectId) {
|
String oldObjectId, String newObjectId,
|
||||||
|
ReceiveCommand.Type type) {
|
||||||
this.projectName = project.get();
|
this.projectName = project.get();
|
||||||
this.ref = ref;
|
this.ref = ref;
|
||||||
this.oldObjectId = oldObjectId;
|
this.oldObjectId = oldObjectId;
|
||||||
this.newObjectId = newObjectId;
|
this.newObjectId = newObjectId;
|
||||||
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -107,6 +125,21 @@ public class GitReferenceUpdated {
|
|||||||
return newObjectId;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("%s[%s,%s: %s -> %s]", getClass().getSimpleName(),
|
return String.format("%s[%s,%s: %s -> %s]", getClass().getSimpleName(),
|
||||||
|
@@ -641,8 +641,7 @@ public class ReceiveCommits {
|
|||||||
// We only fire gitRefUpdated for direct refs updates.
|
// We only fire gitRefUpdated for direct refs updates.
|
||||||
// Events for change refs are fired when they are created.
|
// Events for change refs are fired when they are created.
|
||||||
//
|
//
|
||||||
gitRefUpdated.fire(project.getNameKey(), c.getRefName(),
|
gitRefUpdated.fire(project.getNameKey(), c);
|
||||||
c.getOldId(), c.getNewId());
|
|
||||||
hooks.doRefUpdatedHook(
|
hooks.doRefUpdatedHook(
|
||||||
new Branch.NameKey(project.getNameKey(), c.getRefName()),
|
new Branch.NameKey(project.getNameKey(), c.getRefName()),
|
||||||
c.getOldId(),
|
c.getOldId(),
|
||||||
|
@@ -47,6 +47,7 @@ import org.eclipse.jgit.lib.Repository;
|
|||||||
import org.eclipse.jgit.revwalk.ObjectWalk;
|
import org.eclipse.jgit.revwalk.ObjectWalk;
|
||||||
import org.eclipse.jgit.revwalk.RevObject;
|
import org.eclipse.jgit.revwalk.RevObject;
|
||||||
import org.eclipse.jgit.revwalk.RevWalk;
|
import org.eclipse.jgit.revwalk.RevWalk;
|
||||||
|
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -149,7 +150,7 @@ public class CreateBranch implements RestModifyView<ProjectResource, Input> {
|
|||||||
case FAST_FORWARD:
|
case FAST_FORWARD:
|
||||||
case NEW:
|
case NEW:
|
||||||
case NO_CHANGE:
|
case NO_CHANGE:
|
||||||
referenceUpdated.fire(name.getParentKey(), u);
|
referenceUpdated.fire(name.getParentKey(), u, ReceiveCommand.Type.CREATE);
|
||||||
hooks.doRefUpdatedHook(name, u, identifiedUser.get().getAccount());
|
hooks.doRefUpdatedHook(name, u, identifiedUser.get().getAccount());
|
||||||
break;
|
break;
|
||||||
case LOCK_FAILURE:
|
case LOCK_FAILURE:
|
||||||
|
@@ -69,6 +69,7 @@ import org.eclipse.jgit.lib.PersonIdent;
|
|||||||
import org.eclipse.jgit.lib.RefUpdate;
|
import org.eclipse.jgit.lib.RefUpdate;
|
||||||
import org.eclipse.jgit.lib.RefUpdate.Result;
|
import org.eclipse.jgit.lib.RefUpdate.Result;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -384,7 +385,7 @@ public class CreateProject implements RestModifyView<TopLevelResource, ProjectIn
|
|||||||
Result result = ru.update();
|
Result result = ru.update();
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case NEW:
|
case NEW:
|
||||||
referenceUpdated.fire(project, ru);
|
referenceUpdated.fire(project, ru, ReceiveCommand.Type.CREATE);
|
||||||
break;
|
break;
|
||||||
default: {
|
default: {
|
||||||
throw new IOException(String.format(
|
throw new IOException(String.format(
|
||||||
|
@@ -35,6 +35,7 @@ import com.google.inject.Singleton;
|
|||||||
import org.eclipse.jgit.errors.LockFailedException;
|
import org.eclipse.jgit.errors.LockFailedException;
|
||||||
import org.eclipse.jgit.lib.RefUpdate;
|
import org.eclipse.jgit.lib.RefUpdate;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -113,7 +114,7 @@ public class DeleteBranch implements RestModifyView<BranchResource, Input>{
|
|||||||
case NO_CHANGE:
|
case NO_CHANGE:
|
||||||
case FAST_FORWARD:
|
case FAST_FORWARD:
|
||||||
case FORCED:
|
case FORCED:
|
||||||
referenceUpdated.fire(rsrc.getNameKey(), u);
|
referenceUpdated.fire(rsrc.getNameKey(), u, ReceiveCommand.Type.DELETE);
|
||||||
hooks.doRefUpdatedHook(rsrc.getBranchKey(), u, identifiedUser.get().getAccount());
|
hooks.doRefUpdatedHook(rsrc.getBranchKey(), u, identifiedUser.get().getAccount());
|
||||||
ResultSet<SubmoduleSubscription> submoduleSubscriptions =
|
ResultSet<SubmoduleSubscription> submoduleSubscriptions =
|
||||||
dbProvider.get().submoduleSubscriptions().bySuperProject(rsrc.getBranchKey());
|
dbProvider.get().submoduleSubscriptions().bySuperProject(rsrc.getBranchKey());
|
||||||
|
@@ -165,8 +165,7 @@ class DeleteBranches implements RestModifyView<ProjectResource, Input> {
|
|||||||
|
|
||||||
private void postDeletion(ProjectResource project, ReceiveCommand cmd)
|
private void postDeletion(ProjectResource project, ReceiveCommand cmd)
|
||||||
throws OrmException {
|
throws OrmException {
|
||||||
referenceUpdated.fire(project.getNameKey(), cmd.getRefName(),
|
referenceUpdated.fire(project.getNameKey(), cmd);
|
||||||
cmd.getOldId(), cmd.getNewId());
|
|
||||||
Branch.NameKey branchKey =
|
Branch.NameKey branchKey =
|
||||||
new Branch.NameKey(project.getNameKey(), cmd.getRefName());
|
new Branch.NameKey(project.getNameKey(), cmd.getRefName());
|
||||||
hooks.doRefUpdatedHook(branchKey, cmd.getOldId(), cmd.getNewId(),
|
hooks.doRefUpdatedHook(branchKey, cmd.getOldId(), cmd.getNewId(),
|
||||||
|
Reference in New Issue
Block a user