PrePushHook.java

  1. /*
  2.  * Copyright (C) 2015 Obeo. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.hooks;

  11. import java.io.IOException;
  12. import java.io.PrintStream;
  13. import java.util.Collection;

  14. import org.eclipse.jgit.api.errors.AbortedByHookException;
  15. import org.eclipse.jgit.lib.ObjectId;
  16. import org.eclipse.jgit.lib.Repository;
  17. import org.eclipse.jgit.transport.RemoteRefUpdate;

  18. /**
  19.  * The <code>pre-push</code> hook implementation. The pre-push hook runs during
  20.  * git push, after the remote refs have been updated but before any objects have
  21.  * been transferred.
  22.  *
  23.  * @since 4.2
  24.  */
  25. public class PrePushHook extends GitHook<String> {

  26.     /**
  27.      * Constant indicating the name of the pre-push hook.
  28.      */
  29.     public static final String NAME = "pre-push"; //$NON-NLS-1$

  30.     private String remoteName;

  31.     private String remoteLocation;

  32.     private String refs;

  33.     /**
  34.      * Constructor for PrePushHook
  35.      * <p>
  36.      * This constructor will use the default error stream.
  37.      * </p>
  38.      *
  39.      * @param repo
  40.      *            The repository
  41.      * @param outputStream
  42.      *            The output stream the hook must use. {@code null} is allowed,
  43.      *            in which case the hook will use {@code System.out}.
  44.      */
  45.     protected PrePushHook(Repository repo, PrintStream outputStream) {
  46.         super(repo, outputStream);
  47.     }

  48.     /**
  49.      * Constructor for PrePushHook
  50.      *
  51.      * @param repo
  52.      *            The repository
  53.      * @param outputStream
  54.      *            The output stream the hook must use. {@code null} is allowed,
  55.      *            in which case the hook will use {@code System.out}.
  56.      * @param errorStream
  57.      *            The error stream the hook must use. {@code null} is allowed,
  58.      *            in which case the hook will use {@code System.err}.
  59.      * @since 5.6
  60.      */
  61.     protected PrePushHook(Repository repo, PrintStream outputStream,
  62.             PrintStream errorStream) {
  63.         super(repo, outputStream, errorStream);
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     protected String getStdinArgs() {
  68.         return refs;
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public String call() throws IOException, AbortedByHookException {
  73.         if (canRun()) {
  74.             doRun();
  75.         }
  76.         return ""; //$NON-NLS-1$
  77.     }

  78.     /**
  79.      * @return {@code true}
  80.      */
  81.     private boolean canRun() {
  82.         return true;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public String getHookName() {
  87.         return NAME;
  88.     }

  89.     /**
  90.      * {@inheritDoc}
  91.      * <p>
  92.      * This hook receives two parameters, which is the name and the location of
  93.      * the remote repository.
  94.      */
  95.     @Override
  96.     protected String[] getParameters() {
  97.         if (remoteName == null) {
  98.             remoteName = remoteLocation;
  99.         }
  100.         return new String[] { remoteName, remoteLocation };
  101.     }

  102.     /**
  103.      * Set remote name
  104.      *
  105.      * @param name
  106.      *            remote name
  107.      */
  108.     public void setRemoteName(String name) {
  109.         remoteName = name;
  110.     }

  111.     /**
  112.      * Get remote name
  113.      *
  114.      * @return remote name or null
  115.      * @since 4.11
  116.      */
  117.     protected String getRemoteName() {
  118.         return remoteName;
  119.     }

  120.     /**
  121.      * Set remote location
  122.      *
  123.      * @param location
  124.      *            a remote location
  125.      */
  126.     public void setRemoteLocation(String location) {
  127.         remoteLocation = location;
  128.     }

  129.     /**
  130.      * Set Refs
  131.      *
  132.      * @param toRefs
  133.      *            a collection of {@code RemoteRefUpdate}s
  134.      */
  135.     public void setRefs(Collection<RemoteRefUpdate> toRefs) {
  136.         StringBuilder b = new StringBuilder();
  137.         for (RemoteRefUpdate u : toRefs) {
  138.             b.append(u.getSrcRef());
  139.             b.append(" "); //$NON-NLS-1$
  140.             b.append(u.getNewObjectId().getName());
  141.             b.append(" "); //$NON-NLS-1$
  142.             b.append(u.getRemoteName());
  143.             b.append(" "); //$NON-NLS-1$
  144.             ObjectId ooid = u.getExpectedOldObjectId();
  145.             b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid
  146.                     .getName());
  147.             b.append("\n"); //$NON-NLS-1$
  148.         }
  149.         refs = b.toString();
  150.     }
  151. }