Initial stub implementation
Change-Id: I29f3e18820d43f028c7b925d54b239a5df00697d
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..68892bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+/target
+/.classpath
+/.project
+/.settings/org.maven.ide.eclipse.prefs
+/.settings/org.eclipse.m2e.core.prefs
+/.idea
diff --git a/BUCK b/BUCK
new file mode 100644
index 0000000..e9575ac
--- /dev/null
+++ b/BUCK
@@ -0,0 +1,8 @@
+gerrit_plugin(
+ name = 'ref-protection',
+ srcs = glob(['src/main/java/**/*.java']),
+ resources = glob(['src/main/resources/**/*']),
+ manifest_entries = [
+ 'Gerrit-Module: com.googlesource.gerrit.plugins.refprotection.RefProtectionModule'
+ ],
+)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefProtectionModule.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefProtectionModule.java
new file mode 100644
index 0000000..d8b3f14
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefProtectionModule.java
@@ -0,0 +1,13 @@
+package com.googlesource.gerrit.plugins.refprotection;
+
+import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
+import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.inject.AbstractModule;
+
+public class RefProtectionModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ DynamicSet.bind(binder(), GitReferenceUpdatedListener.class).to(
+ RefUpdateListener.class);
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
new file mode 100644
index 0000000..92b6527
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
@@ -0,0 +1,82 @@
+package com.googlesource.gerrit.plugins.refprotection;
+
+import static org.eclipse.jgit.lib.Constants.R_HEADS;
+import static org.eclipse.jgit.lib.Constants.R_TAGS;
+
+import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
+
+import org.eclipse.jgit.lib.ObjectId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class RefUpdateListener implements GitReferenceUpdatedListener {
+
+ private static final Logger log = LoggerFactory
+ .getLogger(RefUpdateListener.class);
+
+ @Override
+ public void onGitReferenceUpdated(final Event event) {
+ if (!isNewRef(event) && isRelevantRef(event)) {
+ if (isRefDeleted(event) || isNonFastForwardUpdate(event)) {
+ createBackupBranch(event);
+ }
+ }
+ }
+
+ /**
+ * Create a backup branch for the given ref.
+ *
+ * @param event the Event
+ */
+ private void createBackupBranch(Event event) {
+ // TODO
+ }
+
+ /**
+ * Is the event on a relevant ref?
+ *
+ * @param event the Event
+ * @return True if relevant, otherwise False.
+ */
+ private boolean isRelevantRef(Event event) {
+ return event.getRefName().startsWith(R_HEADS)
+ || event.getRefName().startsWith(R_TAGS);
+ }
+
+ /**
+ * Is the event a new ref?
+ *
+ * @param event the Event
+ * @return True if a new ref, otherwise False.
+ */
+ private boolean isNewRef(Event event) {
+ return event.getOldObjectId().equals(ObjectId.zeroId().getName());
+ }
+
+ /**
+ * Is the event a ref deletion?
+ *
+ * @param event the Event
+ * @return True if a ref deletion, otherwise False.
+ */
+ private boolean isRefDeleted(Event event) {
+ if (event.getNewObjectId().equals(ObjectId.zeroId().getName())) {
+ log.info(String.format(
+ "Ref Deleted: project [%s] refname [%s] old object id [%s]",
+ event.getProjectName(), event.getRefName(), event.getOldObjectId()));
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Is the event a non-fast-forward update?
+ *
+ * @param event the Event
+ * @return True if a non-fast-forward update, otherwise False.
+ */
+ private boolean isNonFastForwardUpdate(Event event) {
+ // TODO
+ return false;
+ }
+}
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
new file mode 100644
index 0000000..b917002
--- /dev/null
+++ b/src/main/resources/Documentation/about.md
@@ -0,0 +1,2 @@
+Makes backups of deleted refs.
+