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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/*
* Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.pgm;
import java.io.IOException;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RemoteAddCommand;
import org.eclipse.jgit.api.RemoteListCommand;
import org.eclipse.jgit.api.RemoteRemoveCommand;
import org.eclipse.jgit.api.RemoteSetUrlCommand;
import org.eclipse.jgit.api.RemoteSetUrlCommand.UriType;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.pgm.opt.CmdLineParser;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.io.ThrowingPrintWriter;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@Command(common = false, usage = "usage_Remote")
class Remote extends TextBuiltin {
@Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
private boolean verbose = false;
@Option(name = "--prune", aliases = {
"-p" }, usage = "usage_pruneStaleTrackingRefs")
private boolean prune;
@Option(name = "--push", usage = "usage_pushUrls")
private boolean push;
@Argument(index = 0, metaVar = "metaVar_command")
private String command;
@Argument(index = 1, metaVar = "metaVar_remoteName")
private String name;
@Argument(index = 2, metaVar = "metaVar_uriish")
private String uri;
/** {@inheritDoc} */
@Override
protected void run() {
try (Git git = new Git(db)) {
if (command == null) {
RemoteListCommand cmd = git.remoteList();
List<RemoteConfig> remotes = cmd.call();
print(remotes);
return;
}
switch (command) {
case "add": //$NON-NLS-1$
RemoteAddCommand add = git.remoteAdd();
add.setName(name);
add.setUri(new URIish(uri));
add.call();
break;
case "remove": //$NON-NLS-1$
case "rm": //$NON-NLS-1$
RemoteRemoveCommand rm = git.remoteRemove();
rm.setRemoteName(name);
rm.call();
break;
case "set-url": //$NON-NLS-1$
RemoteSetUrlCommand remoteSetUrl = git.remoteSetUrl();
remoteSetUrl.setRemoteName(name);
remoteSetUrl.setRemoteUri(new URIish(uri));
remoteSetUrl.setUriType(push ? UriType.PUSH : UriType.FETCH);
remoteSetUrl.call();
break;
case "update": //$NON-NLS-1$
Fetch fetch = new Fetch();
fetch.init(db, gitdir);
StringWriter osw = new StringWriter();
fetch.outw = new ThrowingPrintWriter(osw);
StringWriter esw = new StringWriter();
fetch.errw = new ThrowingPrintWriter(esw);
List<String> fetchArgs = new ArrayList<>();
if (verbose) {
fetchArgs.add("--verbose"); //$NON-NLS-1$
}
if (prune) {
fetchArgs.add("--prune"); //$NON-NLS-1$
}
if (name != null) {
fetchArgs.add(name);
}
fetch.execute(fetchArgs.toArray(new String[0]));
fetch.outw.flush();
fetch.errw.flush();
outw.println(osw.toString());
errw.println(esw.toString());
break;
default:
throw new JGitInternalException(MessageFormat
.format(CLIText.get().unknownSubcommand, command));
}
} catch (Exception e) {
throw die(e.getMessage(), e);
}
}
/** {@inheritDoc} */
@Override
public void printUsage(String message, CmdLineParser clp)
throws IOException {
errw.println(message);
errw.println("jgit remote [--verbose (-v)] [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote add name uri-ish [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote remove name [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote rm name [--help (-h)]"); //$NON-NLS-1$
errw.println(
"jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]"); //$NON-NLS-1$
errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]"); //$NON-NLS-1$
errw.println();
clp.printUsage(errw, getResourceBundle());
errw.println();
errw.flush();
}
private void print(List<RemoteConfig> remotes) throws IOException {
for (RemoteConfig remote : remotes) {
String remoteName = remote.getName();
if (verbose) {
List<URIish> fetchURIs = remote.getURIs();
List<URIish> pushURIs = remote.getPushURIs();
String fetchURI = ""; //$NON-NLS-1$
if (!fetchURIs.isEmpty()) {
fetchURI = fetchURIs.get(0).toString();
} else if (!pushURIs.isEmpty()) {
fetchURI = pushURIs.get(0).toString();
}
String pushURI = ""; //$NON-NLS-1$
if (!pushURIs.isEmpty()) {
pushURI = pushURIs.get(0).toString();
} else if (!fetchURIs.isEmpty()) {
pushURI = fetchURIs.get(0).toString();
}
outw.println(
String.format("%s\t%s (fetch)", remoteName, fetchURI)); //$NON-NLS-1$
outw.println(
String.format("%s\t%s (push)", remoteName, pushURI)); //$NON-NLS-1$
} else {
outw.println(remoteName);
}
}
}
}
|