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.text.MessageFormat;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.concurrent.Executors;
52
53 import org.eclipse.jgit.pgm.internal.CLIText;
54 import org.eclipse.jgit.storage.file.FileBasedConfig;
55 import org.eclipse.jgit.storage.file.WindowCacheConfig;
56 import org.eclipse.jgit.storage.pack.PackConfig;
57 import org.eclipse.jgit.transport.DaemonClient;
58 import org.eclipse.jgit.transport.DaemonService;
59 import org.eclipse.jgit.transport.resolver.FileResolver;
60 import org.eclipse.jgit.util.FS;
61 import org.kohsuke.args4j.Argument;
62 import org.kohsuke.args4j.Option;
63
64 @Command(common = true, usage = "usage_exportRepositoriesOverGit")
65 class Daemon extends TextBuiltin {
66 @Option(name = "--config-file", metaVar = "metaVar_configFile", usage = "usage_configFile")
67 File configFile;
68
69 @Option(name = "--port", metaVar = "metaVar_port", usage = "usage_portNumberToListenOn")
70 int port = org.eclipse.jgit.transport.Daemon.DEFAULT_PORT;
71
72 @Option(name = "--listen", metaVar = "metaVar_hostName", usage = "usage_hostnameOrIpToListenOn")
73 String host;
74
75 @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
76 int timeout = -1;
77
78 @Option(name = "--enable", metaVar = "metaVar_service", usage = "usage_enableTheServiceInAllRepositories", multiValued = true)
79 final List<String> enable = new ArrayList<String>();
80
81 @Option(name = "--disable", metaVar = "metaVar_service", usage = "usage_disableTheServiceInAllRepositories", multiValued = true)
82 final List<String> disable = new ArrayList<String>();
83
84 @Option(name = "--allow-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename", multiValued = true)
85 final List<String> canOverride = new ArrayList<String>();
86
87 @Option(name = "--forbid-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename", multiValued = true)
88 final List<String> forbidOverride = new ArrayList<String>();
89
90 @Option(name = "--export-all", usage = "usage_exportWithoutGitDaemonExportOk")
91 boolean exportAll;
92
93 @Argument(required = true, metaVar = "metaVar_directory", usage = "usage_directoriesToExport")
94 final List<File> directory = new ArrayList<File>();
95
96 @Override
97 protected boolean requiresRepository() {
98 return false;
99 }
100
101 @Override
102 protected void run() throws Exception {
103 PackConfig packConfig = new PackConfig();
104
105 if (configFile != null) {
106 if (!configFile.exists()) {
107 throw die(MessageFormat.format(
108 CLIText.get().configFileNotFound,
109 configFile.getAbsolutePath()));
110 }
111
112 FileBasedConfig cfg = new FileBasedConfig(configFile, FS.DETECTED);
113 cfg.load();
114 new WindowCacheConfig().fromConfig(cfg).install();
115 packConfig.fromConfig(cfg);
116 }
117
118 int threads = packConfig.getThreads();
119 if (threads <= 0)
120 threads = Runtime.getRuntime().availableProcessors();
121 if (1 < threads)
122 packConfig.setExecutor(Executors.newFixedThreadPool(threads));
123
124 final FileResolver<DaemonClient> resolver = new FileResolver<DaemonClient>();
125 for (final File f : directory) {
126 outw.println(MessageFormat.format(CLIText.get().exporting, f.getAbsolutePath()));
127 resolver.exportDirectory(f);
128 }
129 resolver.setExportAll(exportAll);
130
131 final org.eclipse.jgit.transport.Daemon d;
132 d = new org.eclipse.jgit.transport.Daemon(
133 host != null ? new InetSocketAddress(host, port)
134 : new InetSocketAddress(port));
135 d.setPackConfig(packConfig);
136 d.setRepositoryResolver(resolver);
137 if (0 <= timeout)
138 d.setTimeout(timeout);
139
140 for (final String n : enable)
141 service(d, n).setEnabled(true);
142 for (final String n : disable)
143 service(d, n).setEnabled(false);
144
145 for (final String n : canOverride)
146 service(d, n).setOverridable(true);
147 for (final String n : forbidOverride)
148 service(d, n).setOverridable(false);
149
150 d.start();
151 outw.println(MessageFormat.format(CLIText.get().listeningOn, d.getAddress()));
152 }
153
154 private static DaemonService service(
155 final org.eclipse.jgit.transport.Daemon d,
156 final String n) {
157 final DaemonService svc = d.getService(n);
158 if (svc == null)
159 throw die(MessageFormat.format(CLIText.get().serviceNotSupported, n));
160 return svc;
161 }
162 }