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> and others
6    *
7    * This program and the accompanying materials are made available under the
8    * terms of the Eclipse Distribution License v. 1.0 which is available at
9    * https://www.eclipse.org/org/documents/edl-v10.php.
10   *
11   * SPDX-License-Identifier: BSD-3-Clause
12   */
13  
14  package org.eclipse.jgit.transport;
15  
16  import java.util.Collection;
17  import java.util.Collections;
18  import java.util.Map;
19  import java.util.SortedMap;
20  import java.util.TreeMap;
21  
22  import org.eclipse.jgit.lib.Ref;
23  
24  /**
25   * Class holding result of operation on remote repository. This includes refs
26   * advertised by remote repo and local tracking refs updates.
27   */
28  public abstract class OperationResult {
29  
30  	Map<String, Ref> advertisedRefs = Collections.emptyMap();
31  
32  	URIish uri;
33  
34  	final SortedMap<String, TrackingRefUpdate> updates = new TreeMap<>();
35  
36  	StringBuilder messageBuffer;
37  
38  	String peerUserAgent;
39  
40  	/**
41  	 * Get the URI this result came from.
42  	 * <p>
43  	 * Each transport instance connects to at most one URI at any point in time.
44  	 *
45  	 * @return the URI describing the location of the remote repository.
46  	 */
47  	public URIish getURI() {
48  		return uri;
49  	}
50  
51  	/**
52  	 * Get the complete list of refs advertised by the remote.
53  	 * <p>
54  	 * The returned refs may appear in any order. If the caller needs these to
55  	 * be sorted, they should be copied into a new array or List and then sorted
56  	 * by the caller as necessary.
57  	 *
58  	 * @return available/advertised refs. Never null. Not modifiable. The
59  	 *         collection can be empty if the remote side has no refs (it is an
60  	 *         empty/newly created repository).
61  	 */
62  	public Collection<Ref> getAdvertisedRefs() {
63  		return Collections.unmodifiableCollection(advertisedRefs.values());
64  	}
65  
66  	/**
67  	 * Get a single advertised ref by name.
68  	 * <p>
69  	 * The name supplied should be valid ref name. To get a peeled value for a
70  	 * ref (aka <code>refs/tags/v1.0^{}</code>) use the base name (without
71  	 * the <code>^{}</code> suffix) and look at the peeled object id.
72  	 *
73  	 * @param name
74  	 *            name of the ref to obtain.
75  	 * @return the requested ref; null if the remote did not advertise this ref.
76  	 */
77  	public final Ref getAdvertisedRef(String name) {
78  		return advertisedRefs.get(name);
79  	}
80  
81  	/**
82  	 * Get the status of all local tracking refs that were updated.
83  	 *
84  	 * @return unmodifiable collection of local updates. Never null. Empty if
85  	 *         there were no local tracking refs updated.
86  	 */
87  	public Collection<TrackingRefUpdate> getTrackingRefUpdates() {
88  		return Collections.unmodifiableCollection(updates.values());
89  	}
90  
91  	/**
92  	 * Get the status for a specific local tracking ref update.
93  	 *
94  	 * @param localName
95  	 *            name of the local ref (e.g. "refs/remotes/origin/master").
96  	 * @return status of the local ref; null if this local ref was not touched
97  	 *         during this operation.
98  	 */
99  	public TrackingRefUpdate getTrackingRefUpdate(String localName) {
100 		return updates.get(localName);
101 	}
102 
103 	void setAdvertisedRefs(URIish u, Map<String, Ref> ar) {
104 		uri = u;
105 		advertisedRefs = ar;
106 	}
107 
108 	void add(TrackingRefUpdate u) {
109 		updates.put(u.getLocalName(), u);
110 	}
111 
112 	/**
113 	 * Get the additional messages, if any, returned by the remote process.
114 	 * <p>
115 	 * These messages are most likely informational or error messages, sent by
116 	 * the remote peer, to help the end-user correct any problems that may have
117 	 * prevented the operation from completing successfully. Application UIs
118 	 * should try to show these in an appropriate context.
119 	 *
120 	 * @return the messages returned by the remote, most likely terminated by a
121 	 *         newline (LF) character. The empty string is returned if the
122 	 *         remote produced no additional messages.
123 	 */
124 	public String getMessages() {
125 		return messageBuffer != null ? messageBuffer.toString() : ""; //$NON-NLS-1$
126 	}
127 
128 	void addMessages(String msg) {
129 		if (msg != null && msg.length() > 0) {
130 			if (messageBuffer == null)
131 				messageBuffer = new StringBuilder();
132 			messageBuffer.append(msg);
133 			if (!msg.endsWith("\n")) //$NON-NLS-1$
134 				messageBuffer.append('\n');
135 		}
136 	}
137 
138 	/**
139 	 * Get the user agent advertised by the peer server, if available.
140 	 *
141 	 * @return advertised user agent, e.g. {@code "JGit/4.0"}. Null if the peer
142 	 *         did not advertise version information.
143 	 * @since 4.0
144 	 */
145 	public String getPeerUserAgent() {
146 		return peerUserAgent;
147 	}
148 }