View Javadoc
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  
47  package org.eclipse.jgit.transport;
48  
49  import java.util.Collection;
50  import java.util.Collections;
51  import java.util.Map;
52  import java.util.SortedMap;
53  import java.util.TreeMap;
54  
55  import org.eclipse.jgit.lib.Ref;
56  
57  /**
58   * Class holding result of operation on remote repository. This includes refs
59   * advertised by remote repo and local tracking refs updates.
60   */
61  public abstract class OperationResult {
62  
63  	Map<String, Ref> advertisedRefs = Collections.emptyMap();
64  
65  	URIish uri;
66  
67  	final SortedMap<String, TrackingRefUpdate> updates = new TreeMap<>();
68  
69  	StringBuilder messageBuffer;
70  
71  	String peerUserAgent;
72  
73  	/**
74  	 * Get the URI this result came from.
75  	 * <p>
76  	 * Each transport instance connects to at most one URI at any point in time.
77  	 *
78  	 * @return the URI describing the location of the remote repository.
79  	 */
80  	public URIish getURI() {
81  		return uri;
82  	}
83  
84  	/**
85  	 * Get the complete list of refs advertised by the remote.
86  	 * <p>
87  	 * The returned refs may appear in any order. If the caller needs these to
88  	 * be sorted, they should be copied into a new array or List and then sorted
89  	 * by the caller as necessary.
90  	 *
91  	 * @return available/advertised refs. Never null. Not modifiable. The
92  	 *         collection can be empty if the remote side has no refs (it is an
93  	 *         empty/newly created repository).
94  	 */
95  	public Collection<Ref> getAdvertisedRefs() {
96  		return Collections.unmodifiableCollection(advertisedRefs.values());
97  	}
98  
99  	/**
100 	 * Get a single advertised ref by name.
101 	 * <p>
102 	 * The name supplied should be valid ref name. To get a peeled value for a
103 	 * ref (aka <code>refs/tags/v1.0^{}</code>) use the base name (without
104 	 * the <code>^{}</code> suffix) and look at the peeled object id.
105 	 *
106 	 * @param name
107 	 *            name of the ref to obtain.
108 	 * @return the requested ref; null if the remote did not advertise this ref.
109 	 */
110 	public final Ref getAdvertisedRef(String name) {
111 		return advertisedRefs.get(name);
112 	}
113 
114 	/**
115 	 * Get the status of all local tracking refs that were updated.
116 	 *
117 	 * @return unmodifiable collection of local updates. Never null. Empty if
118 	 *         there were no local tracking refs updated.
119 	 */
120 	public Collection<TrackingRefUpdate> getTrackingRefUpdates() {
121 		return Collections.unmodifiableCollection(updates.values());
122 	}
123 
124 	/**
125 	 * Get the status for a specific local tracking ref update.
126 	 *
127 	 * @param localName
128 	 *            name of the local ref (e.g. "refs/remotes/origin/master").
129 	 * @return status of the local ref; null if this local ref was not touched
130 	 *         during this operation.
131 	 */
132 	public TrackingRefUpdate getTrackingRefUpdate(String localName) {
133 		return updates.get(localName);
134 	}
135 
136 	void setAdvertisedRefs(URIish u, Map<String, Ref> ar) {
137 		uri = u;
138 		advertisedRefs = ar;
139 	}
140 
141 	void add(TrackingRefUpdate u) {
142 		updates.put(u.getLocalName(), u);
143 	}
144 
145 	/**
146 	 * Get the additional messages, if any, returned by the remote process.
147 	 * <p>
148 	 * These messages are most likely informational or error messages, sent by
149 	 * the remote peer, to help the end-user correct any problems that may have
150 	 * prevented the operation from completing successfully. Application UIs
151 	 * should try to show these in an appropriate context.
152 	 *
153 	 * @return the messages returned by the remote, most likely terminated by a
154 	 *         newline (LF) character. The empty string is returned if the
155 	 *         remote produced no additional messages.
156 	 */
157 	public String getMessages() {
158 		return messageBuffer != null ? messageBuffer.toString() : ""; //$NON-NLS-1$
159 	}
160 
161 	void addMessages(String msg) {
162 		if (msg != null && msg.length() > 0) {
163 			if (messageBuffer == null)
164 				messageBuffer = new StringBuilder();
165 			messageBuffer.append(msg);
166 			if (!msg.endsWith("\n")) //$NON-NLS-1$
167 				messageBuffer.append('\n');
168 		}
169 	}
170 
171 	/**
172 	 * Get the user agent advertised by the peer server, if available.
173 	 *
174 	 * @return advertised user agent, e.g. {@code "JGit/4.0"}. Null if the peer
175 	 *         did not advertise version information.
176 	 * @since 4.0
177 	 */
178 	public String getPeerUserAgent() {
179 		return peerUserAgent;
180 	}
181 }