PrePushHook.java

  1. /*
  2.  * Copyright (C) 2015, 2022 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.     private boolean dryRun;

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

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

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

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

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

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

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

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

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

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

  130.     /**
  131.      * Sets whether the push is a dry run.
  132.      *
  133.      * @param dryRun
  134.      *            {@code true} if the push is a dry run, {@code false} otherwise
  135.      * @since 6.2
  136.      */
  137.     public void setDryRun(boolean dryRun) {
  138.         this.dryRun = dryRun;
  139.     }

  140.     /**
  141.      * Tells whether the push is a dry run.
  142.      *
  143.      * @return {@code true} if the push is a dry run, {@code false} otherwise
  144.      * @since 6.2
  145.      */
  146.     protected boolean isDryRun() {
  147.         return dryRun;
  148.     }

  149.     /**
  150.      * Set Refs
  151.      *
  152.      * @param toRefs
  153.      *            a collection of {@code RemoteRefUpdate}s
  154.      */
  155.     public void setRefs(Collection<RemoteRefUpdate> toRefs) {
  156.         StringBuilder b = new StringBuilder();
  157.         for (RemoteRefUpdate u : toRefs) {
  158.             b.append(u.getSrcRef());
  159.             b.append(" "); //$NON-NLS-1$
  160.             b.append(u.getNewObjectId().getName());
  161.             b.append(" "); //$NON-NLS-1$
  162.             b.append(u.getRemoteName());
  163.             b.append(" "); //$NON-NLS-1$
  164.             ObjectId ooid = u.getExpectedOldObjectId();
  165.             b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid
  166.                     .getName());
  167.             b.append("\n"); //$NON-NLS-1$
  168.         }
  169.         refs = b.toString();
  170.     }
  171. }