Show that there are changes in "Git Repositories" view
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / GitLabels.java
blob6fa96fddc1c71a99af558ff82bf0f36d45c44b1b
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Benjamin Muskalla and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 * Contributors:
9 * Benjamin Muskalla <bmuskalla@tasktop.com> - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal;
13 import java.io.IOException;
15 import org.eclipse.egit.core.RepositoryUtil;
16 import org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry;
17 import org.eclipse.egit.core.internal.indexdiff.IndexDiffData;
18 import org.eclipse.egit.ui.Activator;
19 import org.eclipse.egit.ui.internal.clone.ProjectRecord;
20 import org.eclipse.egit.ui.internal.repository.tree.RefNode;
21 import org.eclipse.egit.ui.internal.synchronize.model.GitModelObject;
22 import org.eclipse.egit.ui.internal.synchronize.model.GitModelRepository;
23 import org.eclipse.jface.viewers.StyledString;
24 import org.eclipse.jgit.lib.BranchTrackingStatus;
25 import org.eclipse.jgit.lib.Constants;
26 import org.eclipse.jgit.lib.Ref;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.jgit.lib.RepositoryState;
29 import org.eclipse.osgi.util.NLS;
31 /**
32 * Various methods to compute label for different Git repository elements.
34 public class GitLabels {
35 private GitLabels() {
38 /**
39 * @param ref
40 * @return a description of the ref, or null if the ref does not have a
41 * description
43 public static String getRefDescription(Ref ref) {
44 String name = ref.getName();
45 if (name.equals(Constants.HEAD)) {
46 if (ref.isSymbolic())
47 return UIText.GitLabelProvider_RefDescriptionHeadSymbolic;
48 else
49 return UIText.GitLabelProvider_RefDescriptionHead;
50 } else if (name.equals(Constants.ORIG_HEAD))
51 return UIText.GitLabelProvider_RefDescriptionOrigHead;
52 else if (name.equals(Constants.FETCH_HEAD))
53 return UIText.GitLabelProvider_RefDescriptionFetchHead;
54 else if (name.equals(Constants.R_STASH))
55 return UIText.GitLabelProvider_RefDescriptionStash;
56 else
57 return null;
60 /**
61 * Format the branch tracking status suitable for displaying in decorations
62 * and labels.
64 * @param status
65 * @return the branch tracking status as a string
67 public static String formatBranchTrackingStatus(BranchTrackingStatus status) {
68 StringBuilder sb = new StringBuilder();
69 int ahead = status.getAheadCount();
70 int behind = status.getBehindCount();
71 if (ahead != 0) {
72 // UPWARDS ARROW
73 sb.append('\u2191');
74 sb.append(ahead);
76 if (behind != 0) {
77 if (sb.length() != 0)
78 sb.append(' ');
79 // DOWNWARDS ARROW
80 sb.append('\u2193');
81 sb.append(status.getBehindCount());
83 return sb.toString();
86 /**
87 * Computes detailed repository label that consists of repository name,
88 * state, checked-out branch and it's status (returned by
89 * {@linkplain #formatBranchTrackingStatus(BranchTrackingStatus)})
91 * @param repository
92 * @return a styled string for the repository
93 * @throws IOException
95 public static StyledString getStyledLabel(Repository repository)
96 throws IOException {
97 RepositoryUtil repositoryUtil = Activator.getDefault()
98 .getRepositoryUtil();
100 StyledString string = new StyledString();
102 IndexDiffCacheEntry entry = org.eclipse.egit.core.Activator
103 .getDefault().getIndexDiffCache()
104 .getIndexDiffCacheEntry(repository);
105 if (entry != null) {
106 IndexDiffData indexDiffData = entry.getIndexDiff();
107 if (indexDiffData != null
108 && (!indexDiffData.getAdded().isEmpty()
109 || !indexDiffData.getChanged().isEmpty()
110 || !indexDiffData.getRemoved().isEmpty()
111 || !indexDiffData.getUntracked().isEmpty()
112 || !indexDiffData.getModified().isEmpty() || !indexDiffData
113 .getMissing().isEmpty())) {
114 string.append('>');
115 string.append(' ');
119 string.append(repositoryUtil.getRepositoryName(repository));
121 String branch = repositoryUtil.getShortBranch(repository);
122 if (branch != null) {
123 string.append(' ');
124 string.append('[', StyledString.DECORATIONS_STYLER);
125 string.append(branch, StyledString.DECORATIONS_STYLER);
127 BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(
128 repository, branch);
129 if (trackingStatus != null
130 && (trackingStatus.getAheadCount() != 0 || trackingStatus
131 .getBehindCount() != 0)) {
132 String formattedTrackingStatus = GitLabels
133 .formatBranchTrackingStatus(trackingStatus);
134 string.append(' ');
135 string.append(formattedTrackingStatus,
136 StyledString.DECORATIONS_STYLER);
139 RepositoryState repositoryState = repository.getRepositoryState();
140 if (repositoryState != RepositoryState.SAFE) {
141 string.append(" - ", StyledString.DECORATIONS_STYLER); //$NON-NLS-1$
142 string.append(repositoryState.getDescription(),
143 StyledString.DECORATIONS_STYLER);
145 string.append(']', StyledString.DECORATIONS_STYLER);
148 return string;
152 * Tries to return label produced by
153 * {@linkplain #getStyledLabel(Repository)}. If
154 * {@linkplain #getStyledLabel(Repository)} throws <code>Exception</code>
155 * then logs it and falls back to {@linkplain #getPlainShortLabel(Object)}
156 * result.
158 * @param repository
159 * @return repository label
161 public static StyledString getStyledLabelSafe(Repository repository) {
162 try {
163 return getStyledLabel(repository);
164 } catch (IOException e) {
165 logLabelRetrievalFailure(repository, e);
167 return new StyledString(getPlainShortLabel(repository));
171 * <p>
172 * If the <code>element</code> represents a repository then tries to return
173 * it's styled label (created by {@link #getStyledLabel(Repository)}). If
174 * the <code>element</code> is not a repository or if
175 * {@link #getStyledLabel(Repository)} threw <code>IOException</code> then
176 * this method returns element's simple label (
177 * {@link #getPlainShortLabel(Object)}). If the element is a repository then
178 * the returned label is appended with absolute path to this repository.
179 * </p>
180 * <p>
181 * IOException thrown by {@link #getStyledLabel(Repository)} is logged.
182 * </p>
184 * @param element
185 * @return element's label
187 public static StyledString getStyledLabelExtendedSafe(Object element) {
188 Repository repo = asRepository(element);
190 if (repo != null) {
191 try {
192 StyledString text = getStyledLabel(repo);
193 text.append(getLabelExtension(repo),
194 StyledString.QUALIFIER_STYLER);
195 return text;
196 } catch (IOException e) {
197 logLabelRetrievalFailure(element, e);
200 return new StyledString(getPlainShortLabelExtended(element));
204 * @param element
205 * @return label computed by {@linkplain #getPlainShortLabel(Object)} with
206 * appended path to the repository if element represents a
207 * repository
209 public static String getPlainShortLabelExtended(Object element) {
210 return getPlainShortLabel(element) + getLabelExtension(element);
213 private static void logLabelRetrievalFailure(Object element, IOException e) {
214 Activator.logError(
215 NLS.bind(UIText.GitLabelProvider_UnableToRetrieveLabel,
216 element.toString()), e);
219 private static String getLabelExtension(Object element) {
220 Repository repo = asRepository(element);
222 if (repo != null)
223 return " - " + getRepositoryAbsolutePath(repo); //$NON-NLS-1$
224 else
225 return ""; //$NON-NLS-1$
229 * @param element
230 * @return simple short label of the Git element (repository, reference,
231 * changeset, etc.).
233 public static String getPlainShortLabel(Object element) {
234 if (element instanceof Repository)
235 return getRepositorySimpleLabel((Repository) element);
237 if (element instanceof RefNode)
238 return getRefNodeSimpleLabel((RefNode) element);
240 if (element instanceof Ref)
241 return ((Ref) element).getName();
243 if (element instanceof ProjectRecord)
244 return ((ProjectRecord) element).getProjectLabel();
246 if (element instanceof GitModelObject)
247 return ((GitModelObject) element).getName();
249 return (element != null ? element.toString() : ""); //$NON-NLS-1$
252 private static String getRefNodeSimpleLabel(RefNode refNode) {
253 return refNode.getObject().getName();
256 private static String getRepositorySimpleLabel(Repository repository) {
257 return Activator.getDefault().getRepositoryUtil()
258 .getRepositoryName(repository);
261 private static String getRepositoryAbsolutePath(Repository repository) {
262 return repository.getDirectory().getAbsolutePath();
265 private static Repository asRepository(Object element) {
266 if (element instanceof Repository)
267 return (Repository) element;
268 else if (element instanceof GitModelRepository)
269 return ((GitModelRepository) element).getRepository();
270 else
271 return null;