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.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
88 @Override
89 protected void run() throws Exception {
90 try (Git git = new Git(db)) {
91 if (command == null) {
92 RemoteListCommand cmd = git.remoteList();
93 List<RemoteConfig> remotes = cmd.call();
94 print(remotes);
95 } else if ("add".equals(command)) {
96 RemoteAddCommand cmd = git.remoteAdd();
97 cmd.setName(name);
98 cmd.setUri(new URIish(uri));
99 cmd.call();
100 } else if ("remove".equals(command) || "rm".equals(command)) {
101 RemoteRemoveCommand cmd = git.remoteRemove();
102 cmd.setName(name);
103 cmd.call();
104 } else if ("set-url".equals(command)) {
105 RemoteSetUrlCommand cmd = git.remoteSetUrl();
106 cmd.setName(name);
107 cmd.setUri(new URIish(uri));
108 cmd.setPush(push);
109 cmd.call();
110 } else if ("update".equals(command)) {
111
112 Fetch fetch = new Fetch();
113 fetch.init(db, gitdir);
114
115
116 StringWriter osw = new StringWriter();
117 fetch.outw = new ThrowingPrintWriter(osw);
118
119 StringWriter esw = new StringWriter();
120 fetch.errw = new ThrowingPrintWriter(esw);
121
122 List<String> fetchArgs = new ArrayList<>();
123 if (verbose) {
124 fetchArgs.add("--verbose");
125 }
126 if (prune) {
127 fetchArgs.add("--prune");
128 }
129 if (name != null) {
130 fetchArgs.add(name);
131 }
132
133 fetch.execute(fetchArgs.toArray(new String[fetchArgs.size()]));
134
135
136 fetch.outw.flush();
137 fetch.errw.flush();
138 outw.println(osw.toString());
139 errw.println(esw.toString());
140 } else {
141 throw new JGitInternalException(MessageFormat
142 .format(CLIText.get().unknownSubcommand, command));
143 }
144 }
145 }
146
147
148 @Override
149 public void printUsage(String message, CmdLineParser clp)
150 throws IOException {
151 errw.println(message);
152 errw.println("jgit remote [--verbose (-v)] [--help (-h)]");
153 errw.println("jgit remote add name uri-ish [--help (-h)]");
154 errw.println("jgit remote remove name [--help (-h)]");
155 errw.println("jgit remote rm name [--help (-h)]");
156 errw.println(
157 "jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]");
158 errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]");
159
160 errw.println();
161 clp.printUsage(errw, getResourceBundle());
162 errw.println();
163
164 errw.flush();
165 }
166
167 private void print(List<RemoteConfig> remotes) throws IOException {
168 for (RemoteConfig remote : remotes) {
169 String remoteName = remote.getName();
170 if (verbose) {
171 List<URIish> fetchURIs = remote.getURIs();
172 List<URIish> pushURIs = remote.getPushURIs();
173
174 String fetchURI = "";
175 if (!fetchURIs.isEmpty()) {
176 fetchURI = fetchURIs.get(0).toString();
177 } else if (!pushURIs.isEmpty()) {
178 fetchURI = pushURIs.get(0).toString();
179 }
180
181 String pushURI = "";
182 if (!pushURIs.isEmpty()) {
183 pushURI = pushURIs.get(0).toString();
184 } else if (!fetchURIs.isEmpty()) {
185 pushURI = fetchURIs.get(0).toString();
186 }
187
188 outw.println(
189 String.format("%s\t%s (fetch)", remoteName, fetchURI));
190 outw.println(
191 String.format("%s\t%s (push)", remoteName, pushURI));
192 } else {
193 outw.println(remoteName);
194 }
195 }
196 }
197
198 }