View Javadoc
1   /*
2    * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.pgm;
44  
45  import java.io.IOException;
46  import java.io.StringWriter;
47  import java.text.MessageFormat;
48  import java.util.ArrayList;
49  import java.util.List;
50  
51  import org.eclipse.jgit.api.Git;
52  import org.eclipse.jgit.api.RemoteAddCommand;
53  import org.eclipse.jgit.api.RemoteListCommand;
54  import org.eclipse.jgit.api.RemoteRemoveCommand;
55  import org.eclipse.jgit.api.RemoteSetUrlCommand;
56  import org.eclipse.jgit.api.errors.JGitInternalException;
57  import org.eclipse.jgit.pgm.internal.CLIText;
58  import org.eclipse.jgit.pgm.opt.CmdLineParser;
59  import org.eclipse.jgit.transport.RemoteConfig;
60  import org.eclipse.jgit.transport.URIish;
61  import org.eclipse.jgit.util.io.ThrowingPrintWriter;
62  import org.kohsuke.args4j.Argument;
63  import org.kohsuke.args4j.Option;
64  
65  @Command(common = false, usage = "usage_Remote")
66  class Remote extends TextBuiltin {
67  
68  	@Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
69  	private boolean verbose = false;
70  
71  	@Option(name = "--prune", aliases = {
72  			"-p" }, usage = "usage_pruneStaleTrackingRefs")
73  	private boolean prune;
74  
75  	@Option(name = "--push", usage = "usage_pushUrls")
76  	private boolean push;
77  
78  	@Argument(index = 0, metaVar = "metaVar_command")
79  	private String command;
80  
81  	@Argument(index = 1, metaVar = "metaVar_remoteName")
82  	private String name;
83  
84  	@Argument(index = 2, metaVar = "metaVar_uriish")
85  	private String uri;
86  
87  	@Override
88  	protected void run() throws Exception {
89  		try (Git git = new Git(db)) {
90  			if (command == null) {
91  				RemoteListCommand cmd = git.remoteList();
92  				List<RemoteConfig> remotes = cmd.call();
93  				print(remotes);
94  			} else if ("add".equals(command)) { //$NON-NLS-1$
95  				RemoteAddCommand cmd = git.remoteAdd();
96  				cmd.setName(name);
97  				cmd.setUri(new URIish(uri));
98  				cmd.call();
99  			} else if ("remove".equals(command) || "rm".equals(command)) { //$NON-NLS-1$ //$NON-NLS-2$
100 				RemoteRemoveCommand cmd = git.remoteRemove();
101 				cmd.setName(name);
102 				cmd.call();
103 			} else if ("set-url".equals(command)) { //$NON-NLS-1$
104 				RemoteSetUrlCommand cmd = git.remoteSetUrl();
105 				cmd.setName(name);
106 				cmd.setUri(new URIish(uri));
107 				cmd.setPush(push);
108 				cmd.call();
109 			} else if ("update".equals(command)) { //$NON-NLS-1$
110 				// reuse fetch command for basic implementation of remote update
111 				Fetch fetch = new Fetch();
112 				fetch.init(db, gitdir);
113 
114 				// redirect the output stream
115 				StringWriter osw = new StringWriter();
116 				fetch.outw = new ThrowingPrintWriter(osw);
117 				// redirect the error stream
118 				StringWriter esw = new StringWriter();
119 				fetch.errw = new ThrowingPrintWriter(esw);
120 
121 				List<String> fetchArgs = new ArrayList<>();
122 				if (verbose) {
123 					fetchArgs.add("--verbose"); //$NON-NLS-1$
124 				}
125 				if (prune) {
126 					fetchArgs.add("--prune"); //$NON-NLS-1$
127 				}
128 				if (name != null) {
129 					fetchArgs.add(name);
130 				}
131 
132 				fetch.execute(fetchArgs.toArray(new String[fetchArgs.size()]));
133 
134 				// flush the streams
135 				fetch.outw.flush();
136 				fetch.errw.flush();
137 				outw.println(osw.toString());
138 				errw.println(esw.toString());
139 			} else {
140 				throw new JGitInternalException(MessageFormat
141 						.format(CLIText.get().unknownSubcommand, command));
142 			}
143 		}
144 	}
145 
146 	@Override
147 	public void printUsage(final String message, final CmdLineParser clp)
148 			throws IOException {
149 		errw.println(message);
150 		errw.println("jgit remote [--verbose (-v)] [--help (-h)]"); //$NON-NLS-1$
151 		errw.println("jgit remote add name uri-ish [--help (-h)]"); //$NON-NLS-1$
152 		errw.println("jgit remote remove name [--help (-h)]"); //$NON-NLS-1$
153 		errw.println("jgit remote rm name [--help (-h)]"); //$NON-NLS-1$
154 		errw.println(
155 				"jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]"); //$NON-NLS-1$
156 		errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]"); //$NON-NLS-1$
157 
158 		errw.println();
159 		clp.printUsage(errw, getResourceBundle());
160 		errw.println();
161 
162 		errw.flush();
163 	}
164 
165 	private void print(List<RemoteConfig> remotes) throws IOException {
166 		for (RemoteConfig remote : remotes) {
167 			String remoteName = remote.getName();
168 			if (verbose) {
169 				List<URIish> fetchURIs = remote.getURIs();
170 				List<URIish> pushURIs = remote.getPushURIs();
171 
172 				String fetchURI = ""; //$NON-NLS-1$
173 				if (!fetchURIs.isEmpty()) {
174 					fetchURI = fetchURIs.get(0).toString();
175 				} else if (!pushURIs.isEmpty()) {
176 					fetchURI = pushURIs.get(0).toString();
177 				}
178 
179 				String pushURI = ""; //$NON-NLS-1$
180 				if (!pushURIs.isEmpty()) {
181 					pushURI = pushURIs.get(0).toString();
182 				} else if (!fetchURIs.isEmpty()) {
183 					pushURI = fetchURIs.get(0).toString();
184 				}
185 
186 				outw.println(
187 						String.format("%s\t%s (fetch)", remoteName, fetchURI)); //$NON-NLS-1$
188 				outw.println(
189 						String.format("%s\t%s (push)", remoteName, pushURI)); //$NON-NLS-1$
190 			} else {
191 				outw.println(remoteName);
192 			}
193 		}
194 	}
195 
196 }