You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OperationResult.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2007-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.transport;
  47. import java.util.Collection;
  48. import java.util.Collections;
  49. import java.util.Map;
  50. import java.util.SortedMap;
  51. import java.util.TreeMap;
  52. import org.eclipse.jgit.lib.Ref;
  53. /**
  54. * Class holding result of operation on remote repository. This includes refs
  55. * advertised by remote repo and local tracking refs updates.
  56. */
  57. public abstract class OperationResult {
  58. Map<String, Ref> advertisedRefs = Collections.emptyMap();
  59. URIish uri;
  60. final SortedMap<String, TrackingRefUpdate> updates = new TreeMap<>();
  61. StringBuilder messageBuffer;
  62. String peerUserAgent;
  63. /**
  64. * Get the URI this result came from.
  65. * <p>
  66. * Each transport instance connects to at most one URI at any point in time.
  67. *
  68. * @return the URI describing the location of the remote repository.
  69. */
  70. public URIish getURI() {
  71. return uri;
  72. }
  73. /**
  74. * Get the complete list of refs advertised by the remote.
  75. * <p>
  76. * The returned refs may appear in any order. If the caller needs these to
  77. * be sorted, they should be copied into a new array or List and then sorted
  78. * by the caller as necessary.
  79. *
  80. * @return available/advertised refs. Never null. Not modifiable. The
  81. * collection can be empty if the remote side has no refs (it is an
  82. * empty/newly created repository).
  83. */
  84. public Collection<Ref> getAdvertisedRefs() {
  85. return Collections.unmodifiableCollection(advertisedRefs.values());
  86. }
  87. /**
  88. * Get a single advertised ref by name.
  89. * <p>
  90. * The name supplied should be valid ref name. To get a peeled value for a
  91. * ref (aka <code>refs/tags/v1.0^{}</code>) use the base name (without
  92. * the <code>^{}</code> suffix) and look at the peeled object id.
  93. *
  94. * @param name
  95. * name of the ref to obtain.
  96. * @return the requested ref; null if the remote did not advertise this ref.
  97. */
  98. public final Ref getAdvertisedRef(String name) {
  99. return advertisedRefs.get(name);
  100. }
  101. /**
  102. * Get the status of all local tracking refs that were updated.
  103. *
  104. * @return unmodifiable collection of local updates. Never null. Empty if
  105. * there were no local tracking refs updated.
  106. */
  107. public Collection<TrackingRefUpdate> getTrackingRefUpdates() {
  108. return Collections.unmodifiableCollection(updates.values());
  109. }
  110. /**
  111. * Get the status for a specific local tracking ref update.
  112. *
  113. * @param localName
  114. * name of the local ref (e.g. "refs/remotes/origin/master").
  115. * @return status of the local ref; null if this local ref was not touched
  116. * during this operation.
  117. */
  118. public TrackingRefUpdate getTrackingRefUpdate(String localName) {
  119. return updates.get(localName);
  120. }
  121. void setAdvertisedRefs(URIish u, Map<String, Ref> ar) {
  122. uri = u;
  123. advertisedRefs = ar;
  124. }
  125. void add(TrackingRefUpdate u) {
  126. updates.put(u.getLocalName(), u);
  127. }
  128. /**
  129. * Get the additional messages, if any, returned by the remote process.
  130. * <p>
  131. * These messages are most likely informational or error messages, sent by
  132. * the remote peer, to help the end-user correct any problems that may have
  133. * prevented the operation from completing successfully. Application UIs
  134. * should try to show these in an appropriate context.
  135. *
  136. * @return the messages returned by the remote, most likely terminated by a
  137. * newline (LF) character. The empty string is returned if the
  138. * remote produced no additional messages.
  139. */
  140. public String getMessages() {
  141. return messageBuffer != null ? messageBuffer.toString() : ""; //$NON-NLS-1$
  142. }
  143. void addMessages(String msg) {
  144. if (msg != null && msg.length() > 0) {
  145. if (messageBuffer == null)
  146. messageBuffer = new StringBuilder();
  147. messageBuffer.append(msg);
  148. if (!msg.endsWith("\n")) //$NON-NLS-1$
  149. messageBuffer.append('\n');
  150. }
  151. }
  152. /**
  153. * Get the user agent advertised by the peer server, if available.
  154. *
  155. * @return advertised user agent, e.g. {@code "JGit/4.0"}. Null if the peer
  156. * did not advertise version information.
  157. * @since 4.0
  158. */
  159. public String getPeerUserAgent() {
  160. return peerUserAgent;
  161. }
  162. }