summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm
diff options
context:
space:
mode:
authorKaloyan Raev <kaloyan.r@zend.com>2014-01-06 14:40:47 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2015-12-11 22:54:48 +0100
commite23521f0d9040d49dead2ce6fbfab6bde3b7b2c6 (patch)
treefd07c73c9a2e982ff97d6e853a34c231d653c3c4 /org.eclipse.jgit.pgm
parent6dfad98d7237236661414f96eb302ed1013a2ecc (diff)
downloadjgit-e23521f0d9040d49dead2ce6fbfab6bde3b7b2c6.tar.gz
jgit-e23521f0d9040d49dead2ce6fbfab6bde3b7b2c6.zip
Add remote command to JGit CLI
Supported subcommands are: - <none> (lists available remotes) - add - remove - set-url - update Supported options are: --verbose --push --prune Bug: 481316 Change-Id: I57c34ed6daabb7d308bc383b17c1ef4af433e714 Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.pgm')
-rw-r--r--org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin1
-rw-r--r--org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties3
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java197
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java1
4 files changed, 202 insertions, 0 deletions
diff --git a/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin b/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin
index e1b05491b7..c13f63e80f 100644
--- a/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin
+++ b/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin
@@ -24,6 +24,7 @@ org.eclipse.jgit.pgm.MergeBase
org.eclipse.jgit.pgm.Push
org.eclipse.jgit.pgm.ReceivePack
org.eclipse.jgit.pgm.Reflog
+org.eclipse.jgit.pgm.Remote
org.eclipse.jgit.pgm.Repo
org.eclipse.jgit.pgm.Reset
org.eclipse.jgit.pgm.RevList
diff --git a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
index 8aeb7e8753..335336da28 100644
--- a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
+++ b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
@@ -188,6 +188,7 @@ treeIsRequired=argument tree is required
tooManyRefsGiven=Too many refs given
unknownIoErrorStdout=An unknown I/O error occurred on standard output
unknownMergeStrategy=unknown merge strategy {0} specified
+unknownSubcommand=Unknown subcommand: {0}
unmergedPaths=Unmerged paths:
unsupportedOperation=Unsupported operation: {0}
untrackedFiles=Untracked files:
@@ -222,6 +223,7 @@ usage_MergeBase=Find as good common ancestors as possible for a merge
usage_MergesTwoDevelopmentHistories=Merges two development histories
usage_ReadDirCache= Read the DirCache 100 times
usage_RebuildCommitGraph=Recreate a repository from another one's commit graph
+usage_Remote=Manage set of tracked repositories
usage_RepositoryToReadFrom=Repository to read from
usage_RepositoryToReceiveInto=Repository to receive into
usage_RevList=List commit objects in reverse chronological order
@@ -329,6 +331,7 @@ usage_performFsckStyleChecksOnReceive=perform fsck style checks on receive
usage_portNumberToListenOn=port number to listen on
usage_printOnlyBranchesThatContainTheCommit=print only branches that contain the commit
usage_pruneStaleTrackingRefs=prune stale tracking refs
+usage_pushUrls=push URLs are manipulated
usage_quiet=don't show progress messages
usage_recordChangesToRepository=Record changes to the repository
usage_recurseIntoSubtrees=recurse into subtrees
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java
new file mode 100644
index 0000000000..70868e920e
--- /dev/null
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+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.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;
+
+ @Override
+ protected void run() throws Exception {
+ try (Git git = new Git(db)) {
+ if (command == null) {
+ RemoteListCommand cmd = git.remoteList();
+ List<RemoteConfig> remotes = cmd.call();
+ print(remotes);
+ } else if ("add".equals(command)) { //$NON-NLS-1$
+ RemoteAddCommand cmd = git.remoteAdd();
+ cmd.setName(name);
+ cmd.setUri(new URIish(uri));
+ cmd.call();
+ } else if ("remove".equals(command) || "rm".equals(command)) { //$NON-NLS-1$ //$NON-NLS-2$
+ RemoteRemoveCommand cmd = git.remoteRemove();
+ cmd.setName(name);
+ cmd.call();
+ } else if ("set-url".equals(command)) { //$NON-NLS-1$
+ RemoteSetUrlCommand cmd = git.remoteSetUrl();
+ cmd.setName(name);
+ cmd.setUri(new URIish(uri));
+ cmd.setPush(push);
+ cmd.call();
+ } else if ("update".equals(command)) { //$NON-NLS-1$
+ // reuse fetch command for basic implementation of remote update
+ Fetch fetch = new Fetch();
+ fetch.init(db, gitdir);
+
+ // redirect the output stream
+ StringWriter osw = new StringWriter();
+ fetch.outw = new ThrowingPrintWriter(osw);
+ // redirect the error stream
+ 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[fetchArgs.size()]));
+
+ // flush the streams
+ fetch.outw.flush();
+ fetch.errw.flush();
+ outw.println(osw.toString());
+ errw.println(esw.toString());
+ } else {
+ throw new JGitInternalException(MessageFormat
+ .format(CLIText.get().unknownSubcommand, command));
+ }
+ }
+ }
+
+ @Override
+ public void printUsageAndExit(final String message, final 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();
+ throw die(true);
+ }
+
+ 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);
+ }
+ }
+ }
+
+}
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java
index ce2b10c98e..f5d581ad01 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java
@@ -247,6 +247,7 @@ public class CLIText extends TranslationBundle {
/***/ public String treeIsRequired;
/***/ public char[] unknownIoErrorStdout;
/***/ public String unknownMergeStrategy;
+ /***/ public String unknownSubcommand;
/***/ public String unmergedPaths;
/***/ public String unsupportedOperation;
/***/ public String untrackedFiles;