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 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.RemoteSetUrlCommand.UriType;
57 import org.eclipse.jgit.api.errors.JGitInternalException;
58 import org.eclipse.jgit.pgm.internal.CLIText;
59 import org.eclipse.jgit.pgm.opt.CmdLineParser;
60 import org.eclipse.jgit.transport.RemoteConfig;
61 import org.eclipse.jgit.transport.URIish;
62 import org.eclipse.jgit.util.io.ThrowingPrintWriter;
63 import org.kohsuke.args4j.Argument;
64 import org.kohsuke.args4j.Option;
65
66 @Command(common = false, usage = "usage_Remote")
67 class Remote extends TextBuiltin {
68
69 @Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
70 private boolean verbose = false;
71
72 @Option(name = "--prune", aliases = {
73 "-p" }, usage = "usage_pruneStaleTrackingRefs")
74 private boolean prune;
75
76 @Option(name = "--push", usage = "usage_pushUrls")
77 private boolean push;
78
79 @Argument(index = 0, metaVar = "metaVar_command")
80 private String command;
81
82 @Argument(index = 1, metaVar = "metaVar_remoteName")
83 private String name;
84
85 @Argument(index = 2, metaVar = "metaVar_uriish")
86 private String uri;
87
88
89 @Override
90 protected void run() {
91 try (Gitit.html#Git">Git git = new Git(db)) {
92 if (command == null) {
93 RemoteListCommand cmd = git.remoteList();
94 List<RemoteConfig> remotes = cmd.call();
95 print(remotes);
96 } else if ("add".equals(command)) {
97 RemoteAddCommand cmd = git.remoteAdd();
98 cmd.setName(name);
99 cmd.setUri(new URIish(uri));
100 cmd.call();
101 } else if ("remove".equals(command) || "rm".equals(command)) {
102 RemoteRemoveCommand cmd = git.remoteRemove();
103 cmd.setRemoteName(name);
104 cmd.call();
105 } else if ("set-url".equals(command)) {
106 RemoteSetUrlCommand cmd = git.remoteSetUrl();
107 cmd.setRemoteName(name);
108 cmd.setRemoteUri(new URIish(uri));
109 cmd.setUriType(push ? UriType.PUSH : UriType.FETCH);
110 cmd.call();
111 } else if ("update".equals(command)) {
112
113 Fetch fetch = new Fetch();
114 fetch.init(db, gitdir);
115
116
117 StringWriter osw = new StringWriter();
118 fetch.outw = new ThrowingPrintWriter(osw);
119
120 StringWriter esw = new StringWriter();
121 fetch.errw = new ThrowingPrintWriter(esw);
122
123 List<String> fetchArgs = new ArrayList<>();
124 if (verbose) {
125 fetchArgs.add("--verbose");
126 }
127 if (prune) {
128 fetchArgs.add("--prune");
129 }
130 if (name != null) {
131 fetchArgs.add(name);
132 }
133
134 fetch.execute(fetchArgs.toArray(new String[0]));
135
136
137 fetch.outw.flush();
138 fetch.errw.flush();
139 outw.println(osw.toString());
140 errw.println(esw.toString());
141 } else {
142 throw new JGitInternalException(MessageFormat
143 .format(CLIText.get().unknownSubcommand, command));
144 }
145 } catch (Exception e) {
146 throw die(e.getMessage(), e);
147 }
148 }
149
150
151 @Override
152 public void printUsage(String message, CmdLineParser clp)
153 throws IOException {
154 errw.println(message);
155 errw.println("jgit remote [--verbose (-v)] [--help (-h)]");
156 errw.println("jgit remote add name uri-ish [--help (-h)]");
157 errw.println("jgit remote remove name [--help (-h)]");
158 errw.println("jgit remote rm name [--help (-h)]");
159 errw.println(
160 "jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]");
161 errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]");
162
163 errw.println();
164 clp.printUsage(errw, getResourceBundle());
165 errw.println();
166
167 errw.flush();
168 }
169
170 private void print(List<RemoteConfig> remotes) throws IOException {
171 for (RemoteConfig remote : remotes) {
172 String remoteName = remote.getName();
173 if (verbose) {
174 List<URIish> fetchURIs = remote.getURIs();
175 List<URIish> pushURIs = remote.getPushURIs();
176
177 String fetchURI = "";
178 if (!fetchURIs.isEmpty()) {
179 fetchURI = fetchURIs.get(0).toString();
180 } else if (!pushURIs.isEmpty()) {
181 fetchURI = pushURIs.get(0).toString();
182 }
183
184 String pushURI = "";
185 if (!pushURIs.isEmpty()) {
186 pushURI = pushURIs.get(0).toString();
187 } else if (!fetchURIs.isEmpty()) {
188 pushURI = fetchURIs.get(0).toString();
189 }
190
191 outw.println(
192 String.format("%s\t%s (fetch)", remoteName, fetchURI));
193 outw.println(
194 String.format("%s\t%s (push)", remoteName, pushURI));
195 } else {
196 outw.println(remoteName);
197 }
198 }
199 }
200
201 }