]> source.dussan.org Git - jgit.git/commitdiff
PushCommand: Allow adding any reference string 05/2705/2
authorShawn O. Pearce <spearce@spearce.org>
Mon, 14 Mar 2011 15:14:46 +0000 (08:14 -0700)
committerChris Aniszczyk <caniszczyk@gmail.com>
Tue, 15 Mar 2011 14:53:23 +0000 (09:53 -0500)
The simplified form of add(String) makes it easier for applications
to pass down user input and allow PushCommand to convert it to the
internal RefSpec object.

Change-Id: Ibd2e95852db0e52ea4a36032942c4c42a7fb4261
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit/src/org/eclipse/jgit/api/PushCommand.java

index d3512cc0cc80f7c33d39b0a240a12cc055f67854..40bade8840b8272fd40ef8f3dc697ff6cfcdd4be 100644 (file)
@@ -317,6 +317,45 @@ public class PushCommand extends GitCommand<Iterable<PushResult>> {
                return this;
        }
 
+       /**
+        * Add a reference to push.
+        *
+        * @param ref
+        *            the source reference. The remote name will match.
+        * @return {@code this}.
+        */
+       public PushCommand add(Ref ref) {
+               refSpecs.add(new RefSpec(ref.getLeaf().getName()));
+               return this;
+       }
+
+       /**
+        * Add a reference to push.
+        *
+        * @param nameOrSpec
+        *            any reference name, or a reference specification.
+        * @return {@code this}.
+        * @throws JGitInternalException
+        *             the reference name cannot be resolved.
+        */
+       public PushCommand add(String nameOrSpec) throws JGitInternalException {
+               if (0 <= nameOrSpec.indexOf(':')) {
+                       refSpecs.add(new RefSpec(nameOrSpec));
+               } else {
+                       Ref src;
+                       try {
+                               src = repo.getRef(nameOrSpec);
+                       } catch (IOException e) {
+                               throw new JGitInternalException(
+                                               JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
+                                               e);
+                       }
+                       if (src != null)
+                               add(src);
+               }
+               return this;
+       }
+
        /**
         * @return the dry run preference for the push operation
         */