Hooks.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.PrintStream;
  12. import java.text.MessageFormat;

  13. import org.eclipse.jgit.internal.JGitText;
  14. import org.eclipse.jgit.lib.Repository;
  15. import org.eclipse.jgit.util.LfsFactory;

  16. /**
  17.  * Factory class for instantiating supported hooks.
  18.  *
  19.  * @since 4.0
  20.  */
  21. public class Hooks {

  22.     /**
  23.      * Create pre-commit hook for the given repository with the default error
  24.      * stream
  25.      *
  26.      * @param repo
  27.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  28.      * @param outputStream
  29.      *            The output stream, or {@code null} to use {@code System.out}
  30.      * @return The pre-commit hook for the given repository.
  31.      */
  32.     public static PreCommitHook preCommit(Repository repo,
  33.             PrintStream outputStream) {
  34.         return new PreCommitHook(repo, outputStream);
  35.     }

  36.     /**
  37.      * Create pre-commit hook for the given repository
  38.      *
  39.      * @param repo
  40.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  41.      * @param outputStream
  42.      *            The output stream, or {@code null} to use {@code System.out}
  43.      * @param errorStream
  44.      *            The error stream, or {@code null} to use {@code System.err}
  45.      * @return The pre-commit hook for the given repository.
  46.      * @since 5.6
  47.      */
  48.     public static PreCommitHook preCommit(Repository repo,
  49.             PrintStream outputStream, PrintStream errorStream) {
  50.         return new PreCommitHook(repo, outputStream, errorStream);
  51.     }

  52.     /**
  53.      * Create post-commit hook for the given repository with the default error
  54.      * stream
  55.      *
  56.      * @param repo
  57.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  58.      * @param outputStream
  59.      *            The output stream, or {@code null} to use {@code System.out}
  60.      * @return The post-commit hook for the given repository.
  61.      * @since 4.5
  62.      */
  63.     public static PostCommitHook postCommit(Repository repo,
  64.             PrintStream outputStream) {
  65.         return new PostCommitHook(repo, outputStream);
  66.     }

  67.     /**
  68.      * Create post-commit hook for the given repository
  69.      *
  70.      * @param repo
  71.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  72.      * @param outputStream
  73.      *            The output stream, or {@code null} to use {@code System.out}
  74.      * @param errorStream
  75.      *            The error stream, or {@code null} to use {@code System.err}
  76.      * @return The pre-commit hook for the given repository.
  77.      * @since 5.6
  78.      */
  79.     public static PostCommitHook postCommit(Repository repo,
  80.             PrintStream outputStream, PrintStream errorStream) {
  81.         return new PostCommitHook(repo, outputStream, errorStream);
  82.     }

  83.     /**
  84.      * Create commit-msg hook for the given repository with the default error
  85.      * stream
  86.      *
  87.      * @param repo
  88.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  89.      * @param outputStream
  90.      *            The output stream, or {@code null} to use {@code System.out}
  91.      * @return The commit-msg hook for the given repository.
  92.      */
  93.     public static CommitMsgHook commitMsg(Repository repo,
  94.             PrintStream outputStream) {
  95.         return new CommitMsgHook(repo, outputStream);
  96.     }

  97.     /**
  98.      * Create commit-msg hook for the given repository
  99.      *
  100.      * @param repo
  101.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  102.      * @param outputStream
  103.      *            The output stream, or {@code null} to use {@code System.out}
  104.      * @param errorStream
  105.      *            The error stream, or {@code null} to use {@code System.err}
  106.      * @return The pre-commit hook for the given repository.
  107.      * @since 5.6
  108.      */
  109.     public static CommitMsgHook commitMsg(Repository repo,
  110.             PrintStream outputStream, PrintStream errorStream) {
  111.         return new CommitMsgHook(repo, outputStream, errorStream);
  112.     }

  113.     /**
  114.      * Create pre-push hook for the given repository with the default error
  115.      * stream
  116.      *
  117.      * @param repo
  118.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  119.      * @param outputStream
  120.      *            The output stream, or {@code null} to use {@code System.out}
  121.      * @return The pre-push hook for the given repository.
  122.      * @since 4.2
  123.      */
  124.     public static PrePushHook prePush(Repository repo, PrintStream outputStream) {
  125.         if (LfsFactory.getInstance().isAvailable()) {
  126.             PrePushHook hook = LfsFactory.getInstance().getPrePushHook(repo,
  127.                     outputStream);
  128.             if (hook != null) {
  129.                 if (hook.isNativeHookPresent()) {
  130.                     PrintStream ps = outputStream;
  131.                     if (ps == null) {
  132.                         ps = System.out;
  133.                     }
  134.                     ps.println(MessageFormat
  135.                             .format(JGitText.get().lfsHookConflict, repo));
  136.                 }
  137.                 return hook;
  138.             }
  139.         }
  140.         return new PrePushHook(repo, outputStream);
  141.     }

  142.     /**
  143.      * Create pre-push hook for the given repository
  144.      *
  145.      * @param repo
  146.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  147.      * @param outputStream
  148.      *            The output stream, or {@code null} to use {@code System.out}
  149.      * @param errorStream
  150.      *            The error stream, or {@code null} to use {@code System.err}
  151.      * @return The pre-push hook for the given repository.
  152.      * @since 5.6
  153.      */
  154.     public static PrePushHook prePush(Repository repo, PrintStream outputStream,
  155.             PrintStream errorStream) {
  156.         if (LfsFactory.getInstance().isAvailable()) {
  157.             PrePushHook hook = LfsFactory.getInstance().getPrePushHook(repo,
  158.                     outputStream, errorStream);
  159.             if (hook != null) {
  160.                 if (hook.isNativeHookPresent()) {
  161.                     PrintStream ps = outputStream;
  162.                     if (ps == null) {
  163.                         ps = System.out;
  164.                     }
  165.                     ps.println(MessageFormat
  166.                             .format(JGitText.get().lfsHookConflict, repo));
  167.                 }
  168.                 return hook;
  169.             }
  170.         }
  171.         return new PrePushHook(repo, outputStream, errorStream);
  172.     }
  173. }