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.PrintStream;
13 import java.text.MessageFormat;
14
15 import org.eclipse.jgit.internal.JGitText;
16 import org.eclipse.jgit.lib.Repository;
17 import org.eclipse.jgit.util.LfsFactory;
18
19 /**
20 * Factory class for instantiating supported hooks.
21 *
22 * @since 4.0
23 */
24 public class Hooks {
25
26 /**
27 * Create pre-commit hook for the given repository with the default error
28 * stream
29 *
30 * @param repo
31 * a {@link org.eclipse.jgit.lib.Repository} object.
32 * @param outputStream
33 * The output stream, or {@code null} to use {@code System.out}
34 * @return The pre-commit hook for the given repository.
35 */
36 public static PreCommitHook preCommit(Repository repo,
37 PrintStream outputStream) {
38 return new PreCommitHook(repo, outputStream);
39 }
40
41 /**
42 * Create pre-commit hook for the given repository
43 *
44 * @param repo
45 * a {@link org.eclipse.jgit.lib.Repository} object.
46 * @param outputStream
47 * The output stream, or {@code null} to use {@code System.out}
48 * @param errorStream
49 * The error stream, or {@code null} to use {@code System.err}
50 * @return The pre-commit hook for the given repository.
51 * @since 5.6
52 */
53 public static PreCommitHook preCommit(Repository repo,
54 PrintStream outputStream, PrintStream errorStream) {
55 return new PreCommitHook(repo, outputStream, errorStream);
56 }
57
58 /**
59 * Create post-commit hook for the given repository with the default error
60 * stream
61 *
62 * @param repo
63 * a {@link org.eclipse.jgit.lib.Repository} object.
64 * @param outputStream
65 * The output stream, or {@code null} to use {@code System.out}
66 * @return The post-commit hook for the given repository.
67 * @since 4.5
68 */
69 public static PostCommitHook postCommit(Repository repo,
70 PrintStream outputStream) {
71 return new PostCommitHook(repo, outputStream);
72 }
73
74 /**
75 * Create post-commit hook for the given repository
76 *
77 * @param repo
78 * a {@link org.eclipse.jgit.lib.Repository} object.
79 * @param outputStream
80 * The output stream, or {@code null} to use {@code System.out}
81 * @param errorStream
82 * The error stream, or {@code null} to use {@code System.err}
83 * @return The pre-commit hook for the given repository.
84 * @since 5.6
85 */
86 public static PostCommitHook postCommit(Repository repo,
87 PrintStream outputStream, PrintStream errorStream) {
88 return new PostCommitHook(repo, outputStream, errorStream);
89 }
90
91 /**
92 * Create commit-msg hook for the given repository with the default error
93 * stream
94 *
95 * @param repo
96 * a {@link org.eclipse.jgit.lib.Repository} object.
97 * @param outputStream
98 * The output stream, or {@code null} to use {@code System.out}
99 * @return The commit-msg hook for the given repository.
100 */
101 public static CommitMsgHook commitMsg(Repository repo,
102 PrintStream outputStream) {
103 return new CommitMsgHook(repo, outputStream);
104 }
105
106 /**
107 * Create commit-msg hook for the given repository
108 *
109 * @param repo
110 * a {@link org.eclipse.jgit.lib.Repository} object.
111 * @param outputStream
112 * The output stream, or {@code null} to use {@code System.out}
113 * @param errorStream
114 * The error stream, or {@code null} to use {@code System.err}
115 * @return The pre-commit hook for the given repository.
116 * @since 5.6
117 */
118 public static CommitMsgHook commitMsg(Repository repo,
119 PrintStream outputStream, PrintStream errorStream) {
120 return new CommitMsgHook(repo, outputStream, errorStream);
121 }
122
123 /**
124 * Create pre-push hook for the given repository with the default error
125 * stream
126 *
127 * @param repo
128 * a {@link org.eclipse.jgit.lib.Repository} object.
129 * @param outputStream
130 * The output stream, or {@code null} to use {@code System.out}
131 * @return The pre-push hook for the given repository.
132 * @since 4.2
133 */
134 public static PrePushHook prePush(Repository repo, PrintStream outputStream) {
135 if (LfsFactory.getInstance().isAvailable()) {
136 PrePushHook hook = LfsFactory.getInstance().getPrePushHook(repo,
137 outputStream);
138 if (hook != null) {
139 if (hook.isNativeHookPresent()) {
140 PrintStream ps = outputStream;
141 if (ps == null) {
142 ps = System.out;
143 }
144 ps.println(MessageFormat
145 .format(JGitText.get().lfsHookConflict, repo));
146 }
147 return hook;
148 }
149 }
150 return new PrePushHook(repo, outputStream);
151 }
152
153 /**
154 * Create pre-push hook for the given repository
155 *
156 * @param repo
157 * a {@link org.eclipse.jgit.lib.Repository} object.
158 * @param outputStream
159 * The output stream, or {@code null} to use {@code System.out}
160 * @param errorStream
161 * The error stream, or {@code null} to use {@code System.err}
162 * @return The pre-push hook for the given repository.
163 * @since 5.6
164 */
165 public static PrePushHook prePush(Repository repo, PrintStream outputStream,
166 PrintStream errorStream) {
167 if (LfsFactory.getInstance().isAvailable()) {
168 PrePushHook hook = LfsFactory.getInstance().getPrePushHook(repo,
169 outputStream, errorStream);
170 if (hook != null) {
171 if (hook.isNativeHookPresent()) {
172 PrintStream ps = outputStream;
173 if (ps == null) {
174 ps = System.out;
175 }
176 ps.println(MessageFormat
177 .format(JGitText.get().lfsHookConflict, repo));
178 }
179 return hook;
180 }
181 }
182 return new PrePushHook(repo, outputStream, errorStream);
183 }
184 }