1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.hooks;
11
12 import static java.nio.charset.StandardCharsets.UTF_8;
13
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.PrintStream;
17 import java.io.UnsupportedEncodingException;
18 import java.util.concurrent.Callable;
19
20 import org.eclipse.jgit.api.errors.AbortedByHookException;
21 import org.eclipse.jgit.lib.Repository;
22 import org.eclipse.jgit.util.FS;
23 import org.eclipse.jgit.util.ProcessResult;
24 import org.eclipse.jgit.util.io.TeeOutputStream;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 abstract class GitHook<T> implements Callable<T> {
41
42 private final Repository repo;
43
44
45
46
47 protected final PrintStream outputStream;
48
49
50
51
52 protected final PrintStream errorStream;
53
54
55
56
57
58
59
60
61
62
63
64
65
66 protected GitHook(Repository repo, PrintStream outputStream) {
67 this(repo, outputStream, null);
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82 protected GitHook(Repository repo, PrintStream outputStream,
83 PrintStream errorStream) {
84 this.repo = repo;
85 this.outputStream = outputStream;
86 this.errorStream = errorStream;
87 }
88
89
90
91
92
93
94 @Override
95 public abstract T call() throws IOException, AbortedByHookException;
96
97
98
99
100
101
102 public abstract String getHookName();
103
104
105
106
107
108
109 protected Repository getRepository() {
110 return repo;
111 }
112
113
114
115
116
117
118
119
120 protected String[] getParameters() {
121 return new String[0];
122 }
123
124
125
126
127
128
129
130 protected String getStdinArgs() {
131 return null;
132 }
133
134
135
136
137
138
139
140 protected PrintStream getOutputStream() {
141 return outputStream == null ? System.out : outputStream;
142 }
143
144
145
146
147
148
149
150 protected PrintStream getErrorStream() {
151 return errorStream == null ? System.err : errorStream;
152 }
153
154
155
156
157
158
159
160 protected void doRun() throws AbortedByHookException {
161 final ByteArrayOutputStream errorByteArray = new ByteArrayOutputStream();
162 final TeeOutputStreamml#TeeOutputStream">TeeOutputStream stderrStream = new TeeOutputStream(errorByteArray,
163 getErrorStream());
164 PrintStream hookErrRedirect = null;
165 try {
166 hookErrRedirect = new PrintStream(stderrStream, false,
167 UTF_8.name());
168 } catch (UnsupportedEncodingException e) {
169
170 }
171 Repository repository = getRepository();
172 FS fs = repository.getFS();
173 if (fs == null) {
174 fs = FS.DETECTED;
175 }
176 ProcessResult result = fs.runHookIfPresent(repository, getHookName(),
177 getParameters(), getOutputStream(), hookErrRedirect,
178 getStdinArgs());
179 if (result.isExecutedWithError()) {
180 throw new AbortedByHookException(
181 new String(errorByteArray.toByteArray(), UTF_8),
182 getHookName(), result.getExitCode());
183 }
184 }
185
186
187
188
189
190
191
192
193 public boolean isNativeHookPresent() {
194 FS fs = getRepository().getFS();
195 if (fs == null) {
196 fs = FS.DETECTED;
197 }
198 return fs.findHook(getRepository(), getHookName()) != null;
199 }
200
201 }