1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.pgm;
45
46 import java.io.File;
47 import java.net.InetSocketAddress;
48 import java.net.URISyntaxException;
49 import java.text.MessageFormat;
50 import java.util.ArrayList;
51 import java.util.List;
52 import java.util.concurrent.Executors;
53
54 import org.eclipse.jgit.internal.ketch.KetchLeader;
55 import org.eclipse.jgit.internal.ketch.KetchLeaderCache;
56 import org.eclipse.jgit.internal.ketch.KetchPreReceive;
57 import org.eclipse.jgit.internal.ketch.KetchSystem;
58 import org.eclipse.jgit.internal.ketch.KetchText;
59 import org.eclipse.jgit.lib.Repository;
60 import org.eclipse.jgit.pgm.internal.CLIText;
61 import org.eclipse.jgit.storage.file.FileBasedConfig;
62 import org.eclipse.jgit.storage.file.WindowCacheConfig;
63 import org.eclipse.jgit.storage.pack.PackConfig;
64 import org.eclipse.jgit.transport.DaemonClient;
65 import org.eclipse.jgit.transport.DaemonService;
66 import org.eclipse.jgit.transport.ReceivePack;
67 import org.eclipse.jgit.transport.resolver.FileResolver;
68 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
69 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
70 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
71 import org.eclipse.jgit.util.FS;
72 import org.kohsuke.args4j.Argument;
73 import org.kohsuke.args4j.Option;
74
75 @Command(common = true, usage = "usage_exportRepositoriesOverGit")
76 class Daemon extends TextBuiltin {
77 @Option(name = "--config-file", metaVar = "metaVar_configFile", usage = "usage_configFile")
78 File configFile;
79
80 @Option(name = "--port", metaVar = "metaVar_port", usage = "usage_portNumberToListenOn")
81 int port = org.eclipse.jgit.transport.Daemon.DEFAULT_PORT;
82
83 @Option(name = "--listen", metaVar = "metaVar_hostName", usage = "usage_hostnameOrIpToListenOn")
84 String host;
85
86 @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
87 int timeout = -1;
88
89 @Option(name = "--enable", metaVar = "metaVar_service", usage = "usage_enableTheServiceInAllRepositories")
90 List<String> enable = new ArrayList<>();
91
92 @Option(name = "--disable", metaVar = "metaVar_service", usage = "usage_disableTheServiceInAllRepositories")
93 List<String> disable = new ArrayList<>();
94
95 @Option(name = "--allow-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename")
96 List<String> canOverride = new ArrayList<>();
97
98 @Option(name = "--forbid-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename")
99 List<String> forbidOverride = new ArrayList<>();
100
101 @Option(name = "--export-all", usage = "usage_exportWithoutGitDaemonExportOk")
102 boolean exportAll;
103
104 @Option(name = "--ketch", metaVar = "metaVar_ketchServerType", usage = "usage_ketchServerType")
105 KetchServerType ketchServerType;
106
107 enum KetchServerType {
108 LEADER;
109 }
110
111 @Argument(required = true, metaVar = "metaVar_directory", usage = "usage_directoriesToExport")
112 List<File> directory = new ArrayList<>();
113
114
115 @Override
116 protected boolean requiresRepository() {
117 return false;
118 }
119
120
121 @Override
122 protected void run() throws Exception {
123 PackConfig packConfig = new PackConfig();
124
125 if (configFile != null) {
126 if (!configFile.exists()) {
127 throw die(MessageFormat.format(
128 CLIText.get().configFileNotFound,
129 configFile.getAbsolutePath()));
130 }
131
132 FileBasedConfig cfg = new FileBasedConfig(configFile, FS.DETECTED);
133 cfg.load();
134 new WindowCacheConfig().fromConfig(cfg).install();
135 packConfig.fromConfig(cfg);
136 }
137
138 int threads = packConfig.getThreads();
139 if (threads <= 0)
140 threads = Runtime.getRuntime().availableProcessors();
141 if (1 < threads)
142 packConfig.setExecutor(Executors.newFixedThreadPool(threads));
143
144 final FileResolver<DaemonClient> resolver = new FileResolver<>();
145 for (File f : directory) {
146 outw.println(MessageFormat.format(CLIText.get().exporting, f.getAbsolutePath()));
147 resolver.exportDirectory(f);
148 }
149 resolver.setExportAll(exportAll);
150
151 final org.eclipse.jgit.transport.Daemon d;
152 d = new org.eclipse.jgit.transport.Daemon(
153 host != null ? new InetSocketAddress(host, port)
154 : new InetSocketAddress(port));
155 d.setPackConfig(packConfig);
156 d.setRepositoryResolver(resolver);
157 if (0 <= timeout)
158 d.setTimeout(timeout);
159
160 for (String n : enable)
161 service(d, n).setEnabled(true);
162 for (String n : disable)
163 service(d, n).setEnabled(false);
164
165 for (String n : canOverride)
166 service(d, n).setOverridable(true);
167 for (String n : forbidOverride)
168 service(d, n).setOverridable(false);
169 if (ketchServerType == KetchServerType.LEADER) {
170 startKetchLeader(d);
171 }
172 d.start();
173 outw.println(MessageFormat.format(CLIText.get().listeningOn, d.getAddress()));
174 }
175
176 private static DaemonService service(
177 final org.eclipse.jgit.transport.Daemon d,
178 final String n) {
179 final DaemonService svc = d.getService(n);
180 if (svc == null)
181 throw die(MessageFormat.format(CLIText.get().serviceNotSupported, n));
182 return svc;
183 }
184
185 private void startKetchLeader(org.eclipse.jgit.transport.Daemon daemon) {
186 KetchSystem system = new KetchSystem();
187 final KetchLeaderCache leaders = new KetchLeaderCache(system);
188 final ReceivePackFactory<DaemonClient> factory;
189
190 factory = daemon.getReceivePackFactory();
191 daemon.setReceivePackFactory(new ReceivePackFactory<DaemonClient>() {
192 @Override
193 public ReceivePack create(DaemonClient req, Repository repo)
194 throws ServiceNotEnabledException,
195 ServiceNotAuthorizedException {
196 ReceivePack rp = factory.create(req, repo);
197 KetchLeader leader;
198 try {
199 leader = leaders.get(repo);
200 } catch (URISyntaxException err) {
201 throw new ServiceNotEnabledException(
202 KetchText.get().invalidFollowerUri, err);
203 }
204 rp.setPreReceiveHook(new KetchPreReceive(leader));
205 return rp;
206 }
207 });
208 }
209 }