1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.hooks;
11
12 import java.io.IOException;
13 import java.io.PrintStream;
14 import java.util.Collection;
15
16 import org.eclipse.jgit.api.errors.AbortedByHookException;
17 import org.eclipse.jgit.lib.ObjectId;
18 import org.eclipse.jgit.lib.Repository;
19 import org.eclipse.jgit.transport.RemoteRefUpdate;
20
21
22
23
24
25
26
27
28 public class PrePushHook extends GitHook<String> {
29
30
31
32
33 public static final String NAME = "pre-push";
34
35 private String remoteName;
36
37 private String remoteLocation;
38
39 private String refs;
40
41
42
43
44
45
46
47
48
49
50
51
52
53 protected PrePushHook(Repository repo, PrintStream outputStream) {
54 super(repo, outputStream);
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 protected PrePushHook(Repository repo, PrintStream outputStream,
71 PrintStream errorStream) {
72 super(repo, outputStream, errorStream);
73 }
74
75
76 @Override
77 protected String getStdinArgs() {
78 return refs;
79 }
80
81
82 @Override
83 public String call() throws IOException, AbortedByHookException {
84 if (canRun()) {
85 doRun();
86 }
87 return "";
88 }
89
90
91
92
93 private boolean canRun() {
94 return true;
95 }
96
97
98 @Override
99 public String getHookName() {
100 return NAME;
101 }
102
103
104
105
106
107
108
109 @Override
110 protected String[] getParameters() {
111 if (remoteName == null) {
112 remoteName = remoteLocation;
113 }
114 return new String[] { remoteName, remoteLocation };
115 }
116
117
118
119
120
121
122
123 public void setRemoteName(String name) {
124 remoteName = name;
125 }
126
127
128
129
130
131
132
133 protected String getRemoteName() {
134 return remoteName;
135 }
136
137
138
139
140
141
142
143 public void setRemoteLocation(String location) {
144 remoteLocation = location;
145 }
146
147
148
149
150
151
152
153 public void setRefs(Collection<RemoteRefUpdate> toRefs) {
154 StringBuilder b = new StringBuilder();
155 for (RemoteRefUpdate u : toRefs) {
156 b.append(u.getSrcRef());
157 b.append(" ");
158 b.append(u.getNewObjectId().getName());
159 b.append(" ");
160 b.append(u.getRemoteName());
161 b.append(" ");
162 ObjectId ooid = u.getExpectedOldObjectId();
163 b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid
164 .getName());
165 b.append("\n");
166 }
167 refs = b.toString();
168 }
169 }