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
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
;
32 * Various methods to compute label for different Git repository elements.
34 public class GitLabels
{
40 * @return a description of the ref, or null if the ref does not have a
43 public static String
getRefDescription(Ref ref
) {
44 String name
= ref
.getName();
45 if (name
.equals(Constants
.HEAD
)) {
47 return UIText
.GitLabelProvider_RefDescriptionHeadSymbolic
;
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
;
61 * Format the branch tracking status suitable for displaying in decorations
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();
81 sb
.append(status
.getBehindCount());
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)})
92 * @return a styled string for the repository
95 public static StyledString
getStyledLabel(Repository repository
)
97 RepositoryUtil repositoryUtil
= Activator
.getDefault()
100 StyledString string
= new StyledString();
102 IndexDiffCacheEntry entry
= org
.eclipse
.egit
.core
.Activator
103 .getDefault().getIndexDiffCache()
104 .getIndexDiffCacheEntry(repository
);
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())) {
119 string
.append(repositoryUtil
.getRepositoryName(repository
));
121 String branch
= repositoryUtil
.getShortBranch(repository
);
122 if (branch
!= null) {
124 string
.append('[', StyledString
.DECORATIONS_STYLER
);
125 string
.append(branch
, StyledString
.DECORATIONS_STYLER
);
127 BranchTrackingStatus trackingStatus
= BranchTrackingStatus
.of(
129 if (trackingStatus
!= null
130 && (trackingStatus
.getAheadCount() != 0 || trackingStatus
131 .getBehindCount() != 0)) {
132 String formattedTrackingStatus
= GitLabels
133 .formatBranchTrackingStatus(trackingStatus
);
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
);
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)}
159 * @return repository label
161 public static StyledString
getStyledLabelSafe(Repository repository
) {
163 return getStyledLabel(repository
);
164 } catch (IOException e
) {
165 logLabelRetrievalFailure(repository
, e
);
167 return new StyledString(getPlainShortLabel(repository
));
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.
181 * IOException thrown by {@link #getStyledLabel(Repository)} is logged.
185 * @return element's label
187 public static StyledString
getStyledLabelExtendedSafe(Object element
) {
188 Repository repo
= asRepository(element
);
192 StyledString text
= getStyledLabel(repo
);
193 text
.append(getLabelExtension(repo
),
194 StyledString
.QUALIFIER_STYLER
);
196 } catch (IOException e
) {
197 logLabelRetrievalFailure(element
, e
);
200 return new StyledString(getPlainShortLabelExtended(element
));
205 * @return label computed by {@linkplain #getPlainShortLabel(Object)} with
206 * appended path to the repository if element represents a
209 public static String
getPlainShortLabelExtended(Object element
) {
210 return getPlainShortLabel(element
) + getLabelExtension(element
);
213 private static void logLabelRetrievalFailure(Object element
, IOException e
) {
215 NLS
.bind(UIText
.GitLabelProvider_UnableToRetrieveLabel
,
216 element
.toString()), e
);
219 private static String
getLabelExtension(Object element
) {
220 Repository repo
= asRepository(element
);
223 return " - " + getRepositoryAbsolutePath(repo
); //$NON-NLS-1$
225 return ""; //$NON-NLS-1$
230 * @return simple short label of the Git element (repository, reference,
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();