PreCommitHook.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 org.eclipse.jgit.api.errors.AbortedByHookException;
  14. import org.eclipse.jgit.lib.Repository;

  15. /**
  16.  * The <code>pre-commit</code> hook implementation. This hook is run before the
  17.  * commit and can reject the commit.
  18.  *
  19.  * @since 4.0
  20.  */
  21. public class PreCommitHook extends GitHook<Void> {

  22.     /** The pre-commit hook name. */
  23.     public static final String NAME = "pre-commit"; //$NON-NLS-1$

  24.     /**
  25.      * Constructor for PreCommitHook
  26.      * <p>
  27.      * This constructor will use the default error stream.
  28.      * </p>
  29.      *
  30.      * @param repo
  31.      *            The repository
  32.      * @param outputStream
  33.      *            The output stream the hook must use. {@code null} is allowed,
  34.      *            in which case the hook will use {@code System.out}.
  35.      */
  36.     protected PreCommitHook(Repository repo, PrintStream outputStream) {
  37.         super(repo, outputStream);
  38.     }

  39.     /**
  40.      * Constructor for PreCommitHook
  41.      *
  42.      * @param repo
  43.      *            The repository
  44.      * @param outputStream
  45.      *            The output stream the hook must use. {@code null} is allowed,
  46.      *            in which case the hook will use {@code System.out}.
  47.      * @param errorStream
  48.      *            The error stream the hook must use. {@code null} is allowed,
  49.      *            in which case the hook will use {@code System.err}.
  50.      * @since 5.6
  51.      */
  52.     protected PreCommitHook(Repository repo, PrintStream outputStream,
  53.             PrintStream errorStream) {
  54.         super(repo, outputStream, errorStream);
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public Void call() throws IOException, AbortedByHookException {
  59.         doRun();
  60.         return null;
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public String getHookName() {
  65.         return NAME;
  66.     }

  67. }