View Javadoc
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  
12  import java.io.IOException;
13  import java.io.PrintStream;
14  
15  import org.eclipse.jgit.api.errors.AbortedByHookException;
16  import org.eclipse.jgit.lib.Repository;
17  import org.eclipse.jgit.util.ProcessResult;
18  
19  /**
20   * The <code>post-commit</code> hook implementation. This hook is run after the
21   * commit was successfully executed.
22   *
23   * @since 4.5
24   */
25  public class PostCommitHook extends GitHook<Void> {
26  
27  	/** The post-commit hook name. */
28  	public static final String NAME = "post-commit"; //$NON-NLS-1$
29  
30  	/**
31  	 * Constructor for PostCommitHook
32  	 * <p>
33  	 * This constructor will use the default error stream.
34  	 * </p>
35  	 *
36  	 * @param repo
37  	 *            The repository
38  	 * @param outputStream
39  	 *            The output stream the hook must use. {@code null} is allowed,
40  	 *            in which case the hook will use {@code System.out}.
41  	 */
42  	protected PostCommitHook(Repository repo, PrintStream outputStream) {
43  		super(repo, outputStream);
44  	}
45  
46  	/**
47  	 * Constructor for PostCommitHook
48  	 *
49  	 * @param repo
50  	 *            The repository
51  	 * @param outputStream
52  	 *            The output stream the hook must use. {@code null} is allowed,
53  	 *            in which case the hook will use {@code System.out}.
54  	 * @param errorStream
55  	 *            The error stream the hook must use. {@code null} is allowed,
56  	 *            in which case the hook will use {@code System.err}.
57  	 * @since 5.6
58  	 */
59  	protected PostCommitHook(Repository repo, PrintStream outputStream,
60  			PrintStream errorStream) {
61  		super(repo, outputStream, errorStream);
62  	}
63  
64  	/** {@inheritDoc} */
65  	@Override
66  	public Void call() throws IOException, AbortedByHookException {
67  		doRun();
68  		return null;
69  	}
70  
71  	/** {@inheritDoc} */
72  	@Override
73  	public String getHookName() {
74  		return NAME;
75  	}
76  
77  
78  	/**
79  	 * Overwrites the default implementation to never throw an
80  	 * {@link AbortedByHookException}, as the commit has already been done and
81  	 * the exit code of the post-commit hook has no effect.
82  	 */
83  	@Override
84  	protected void handleError(String message, ProcessResult result)
85  			throws AbortedByHookException {
86  		// Do nothing as the exit code of the post-commit hook has no effect.
87  	}
88  
89  }