View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc. 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  
11  package org.eclipse.jgit.pgm;
12  
13  import java.io.File;
14  import java.io.IOException;
15  import java.net.InetSocketAddress;
16  import java.text.MessageFormat;
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.concurrent.Executors;
20  
21  import org.eclipse.jgit.errors.ConfigInvalidException;
22  import org.eclipse.jgit.lib.StoredConfig;
23  import org.eclipse.jgit.pgm.internal.CLIText;
24  import org.eclipse.jgit.storage.file.FileBasedConfig;
25  import org.eclipse.jgit.storage.file.WindowCacheConfig;
26  import org.eclipse.jgit.storage.pack.PackConfig;
27  import org.eclipse.jgit.transport.DaemonClient;
28  import org.eclipse.jgit.transport.DaemonService;
29  import org.eclipse.jgit.transport.resolver.FileResolver;
30  import org.eclipse.jgit.util.FS;
31  import org.eclipse.jgit.util.SystemReader;
32  import org.kohsuke.args4j.Argument;
33  import org.kohsuke.args4j.Option;
34  
35  @Command(common = true, usage = "usage_exportRepositoriesOverGit")
36  class Daemon extends TextBuiltin {
37  	@Option(name = "--config-file", metaVar = "metaVar_configFile", usage = "usage_configFile")
38  	File configFile;
39  
40  	@Option(name = "--port", metaVar = "metaVar_port", usage = "usage_portNumberToListenOn")
41  	int port = org.eclipse.jgit.transport.Daemon.DEFAULT_PORT;
42  
43  	@Option(name = "--listen", metaVar = "metaVar_hostName", usage = "usage_hostnameOrIpToListenOn")
44  	String host;
45  
46  	@Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
47  	int timeout = -1;
48  
49  	@Option(name = "--enable", metaVar = "metaVar_service", usage = "usage_enableTheServiceInAllRepositories")
50  	List<String> enable = new ArrayList<>();
51  
52  	@Option(name = "--disable", metaVar = "metaVar_service", usage = "usage_disableTheServiceInAllRepositories")
53  	List<String> disable = new ArrayList<>();
54  
55  	@Option(name = "--allow-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename")
56  	List<String> canOverride = new ArrayList<>();
57  
58  	@Option(name = "--forbid-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename")
59  	List<String> forbidOverride = new ArrayList<>();
60  
61  	@Option(name = "--export-all", usage = "usage_exportWithoutGitDaemonExportOk")
62  	boolean exportAll;
63  
64  	@Argument(required = true, metaVar = "metaVar_directory", usage = "usage_directoriesToExport")
65  	List<File> directory = new ArrayList<>();
66  
67  	/** {@inheritDoc} */
68  	@Override
69  	protected boolean requiresRepository() {
70  		return false;
71  	}
72  
73  	/** {@inheritDoc} */
74  	@Override
75  	protected void run() throws Exception {
76  		PackConfig packConfig = new PackConfig();
77  		StoredConfig cfg;
78  		if (configFile == null) {
79  			cfg = getUserConfig();
80  		} else {
81  			if (!configFile.exists()) {
82  				throw die(MessageFormat.format(
83  						CLIText.get().configFileNotFound, //
84  						configFile.getAbsolutePath()));
85  			}
86  			cfg = new FileBasedConfig(configFile, FS.DETECTED);
87  		}
88  			cfg.load();
89  			new WindowCacheConfig().fromConfig(cfg).install();
90  			packConfig.fromConfig(cfg);
91  
92  		int threads = packConfig.getThreads();
93  		if (threads <= 0)
94  			threads = Runtime.getRuntime().availableProcessors();
95  		if (1 < threads)
96  			packConfig.setExecutor(Executors.newFixedThreadPool(threads));
97  
98  		final FileResolver<DaemonClient> resolver = new FileResolver<>();
99  		for (File f : directory) {
100 			outw.println(MessageFormat.format(CLIText.get().exporting, f.getAbsolutePath()));
101 			resolver.exportDirectory(f);
102 		}
103 		resolver.setExportAll(exportAll);
104 
105 		final org.eclipse.jgit.transport.Daemon d;
106 		d = new org.eclipse.jgit.transport.Daemon(
107 				host != null ? new InetSocketAddress(host, port)
108 						: new InetSocketAddress(port));
109 		d.setPackConfig(packConfig);
110 		d.setRepositoryResolver(resolver);
111 		if (0 <= timeout)
112 			d.setTimeout(timeout);
113 
114 		for (String n : enable)
115 			service(d, n).setEnabled(true);
116 		for (String n : disable)
117 			service(d, n).setEnabled(false);
118 
119 		for (String n : canOverride)
120 			service(d, n).setOverridable(true);
121 		for (String n : forbidOverride)
122 			service(d, n).setOverridable(false);
123 		d.start();
124 		outw.println(MessageFormat.format(CLIText.get().listeningOn, d.getAddress()));
125 	}
126 
127 	private StoredConfig getUserConfig() throws IOException {
128 		StoredConfig userConfig = null;
129 		try {
130 			userConfig = SystemReader.getInstance().getUserConfig();
131 		} catch (ConfigInvalidException e) {
132 			throw die(e.getMessage());
133 		}
134 		return userConfig;
135 	}
136 
137 	private static DaemonService service(
138 			final org.eclipse.jgit.transport.Daemon d,
139 			final String n) {
140 		final DaemonService svc = d.getService(n);
141 		if (svc == null)
142 			throw die(MessageFormat.format(CLIText.get().serviceNotSupported, n));
143 		return svc;
144 	}
145 }