1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.pgm;
11
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import java.text.MessageFormat;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jgit.api.Git;
19 import org.eclipse.jgit.api.RemoteAddCommand;
20 import org.eclipse.jgit.api.RemoteListCommand;
21 import org.eclipse.jgit.api.RemoteRemoveCommand;
22 import org.eclipse.jgit.api.RemoteSetUrlCommand;
23 import org.eclipse.jgit.api.RemoteSetUrlCommand.UriType;
24 import org.eclipse.jgit.api.errors.JGitInternalException;
25 import org.eclipse.jgit.pgm.internal.CLIText;
26 import org.eclipse.jgit.pgm.opt.CmdLineParser;
27 import org.eclipse.jgit.transport.RemoteConfig;
28 import org.eclipse.jgit.transport.URIish;
29 import org.eclipse.jgit.util.io.ThrowingPrintWriter;
30 import org.kohsuke.args4j.Argument;
31 import org.kohsuke.args4j.Option;
32
33 @Command(common = false, usage = "usage_Remote")
34 class Remote extends TextBuiltin {
35
36 @Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
37 private boolean verbose = false;
38
39 @Option(name = "--prune", aliases = {
40 "-p" }, usage = "usage_pruneStaleTrackingRefs")
41 private boolean prune;
42
43 @Option(name = "--push", usage = "usage_pushUrls")
44 private boolean push;
45
46 @Argument(index = 0, metaVar = "metaVar_command")
47 private String command;
48
49 @Argument(index = 1, metaVar = "metaVar_remoteName")
50 private String name;
51
52 @Argument(index = 2, metaVar = "metaVar_uriish")
53 private String uri;
54
55
56 @Override
57 protected void run() {
58 try (Git git = new Git(db)) {
59 if (command == null) {
60 RemoteListCommand cmd = git.remoteList();
61 List<RemoteConfig> remotes = cmd.call();
62 print(remotes);
63 return;
64 }
65 switch (command) {
66 case "add":
67 RemoteAddCommand add = git.remoteAdd();
68 add.setName(name);
69 add.setUri(new URIish(uri));
70 add.call();
71 break;
72 case "remove":
73 case "rm":
74 RemoteRemoveCommand rm = git.remoteRemove();
75 rm.setRemoteName(name);
76 rm.call();
77 break;
78 case "set-url":
79 RemoteSetUrlCommand remoteSetUrl = git.remoteSetUrl();
80 remoteSetUrl.setRemoteName(name);
81 remoteSetUrl.setRemoteUri(new URIish(uri));
82 remoteSetUrl.setUriType(push ? UriType.PUSH : UriType.FETCH);
83 remoteSetUrl.call();
84 break;
85 case "update":
86 Fetch fetch = new Fetch();
87 fetch.init(db, gitdir);
88 StringWriter osw = new StringWriter();
89 fetch.outw = new ThrowingPrintWriter(osw);
90 StringWriter esw = new StringWriter();
91 fetch.errw = new ThrowingPrintWriter(esw);
92 List<String> fetchArgs = new ArrayList<>();
93 if (verbose) {
94 fetchArgs.add("--verbose");
95 }
96 if (prune) {
97 fetchArgs.add("--prune");
98 }
99 if (name != null) {
100 fetchArgs.add(name);
101 }
102 fetch.execute(fetchArgs.toArray(new String[0]));
103 fetch.outw.flush();
104 fetch.errw.flush();
105 outw.println(osw.toString());
106 errw.println(esw.toString());
107 break;
108 default:
109 throw new JGitInternalException(MessageFormat
110 .format(CLIText.get().unknownSubcommand, command));
111 }
112 } catch (Exception e) {
113 throw die(e.getMessage(), e);
114 }
115 }
116
117
118 @Override
119 public void printUsage(String message, CmdLineParser clp)
120 throws IOException {
121 errw.println(message);
122 errw.println("jgit remote [--verbose (-v)] [--help (-h)]");
123 errw.println("jgit remote add name uri-ish [--help (-h)]");
124 errw.println("jgit remote remove name [--help (-h)]");
125 errw.println("jgit remote rm name [--help (-h)]");
126 errw.println(
127 "jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]");
128 errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]");
129
130 errw.println();
131 clp.printUsage(errw, getResourceBundle());
132 errw.println();
133
134 errw.flush();
135 }
136
137 private void print(List<RemoteConfig> remotes) throws IOException {
138 for (RemoteConfig remote : remotes) {
139 String remoteName = remote.getName();
140 if (verbose) {
141 List<URIish> fetchURIs = remote.getURIs();
142 List<URIish> pushURIs = remote.getPushURIs();
143
144 String fetchURI = "";
145 if (!fetchURIs.isEmpty()) {
146 fetchURI = fetchURIs.get(0).toString();
147 } else if (!pushURIs.isEmpty()) {
148 fetchURI = pushURIs.get(0).toString();
149 }
150
151 String pushURI = "";
152 if (!pushURIs.isEmpty()) {
153 pushURI = pushURIs.get(0).toString();
154 } else if (!fetchURIs.isEmpty()) {
155 pushURI = fetchURIs.get(0).toString();
156 }
157
158 outw.println(
159 String.format("%s\t%s (fetch)", remoteName, fetchURI));
160 outw.println(
161 String.format("%s\t%s (push)", remoteName, pushURI));
162 } else {
163 outw.println(remoteName);
164 }
165 }
166 }
167
168 }