diff options
author | Matthias Sohn | 2016-02-26 22:47:40 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2016-02-26 22:47:42 +0000 |
commit | 6514255fa916a2722d3899b639a7c001551d09af (patch) | |
tree | ccf4df5b8d8b4ff223507aeb9312a4e94ec4bc50 | |
parent | 599f8ad058190b3b976c5ad1b9b9491f5c1a4e1a (diff) | |
parent | ccb233b1f564a97cf9e09660cdd8270195b1372d (diff) | |
download | egit-6514255fa916a2722d3899b639a7c001551d09af.tar.gz egit-6514255fa916a2722d3899b639a7c001551d09af.tar.xz egit-6514255fa916a2722d3899b639a7c001551d09af.zip |
Merge "RepositoriesViewLabelProvider: mark dirty submodules"
5 files changed, 116 insertions, 29 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java index 803a93bad6..7a108e5c85 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryCache.java @@ -71,11 +71,14 @@ public class RepositoryCache { public synchronized Repository lookupRepository(final File gitDir) throws IOException { prune(repositoryCache); - Reference<Repository> r = repositoryCache.get(gitDir); + // Make sure we have a normalized path without .. segments here. + File normalizedGitDir = new Path(gitDir.getAbsolutePath()).toFile(); + Reference<Repository> r = repositoryCache.get(normalizedGitDir); Repository d = r != null ? r.get() : null; if (d == null) { - d = FileRepositoryBuilder.create(gitDir); - repositoryCache.put(gitDir, new WeakReference<Repository>(d)); + d = FileRepositoryBuilder.create(normalizedGitDir); + repositoryCache.put(normalizedGitDir, + new WeakReference<Repository>(d)); } return d; } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java index 5d97bcd317..70053f4253 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java @@ -35,6 +35,8 @@ import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.variables.IStringVariableManager; import org.eclipse.core.variables.VariablesPlugin; import org.eclipse.egit.core.internal.CoreText; +import org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry; +import org.eclipse.egit.core.internal.indexdiff.IndexDiffData; import org.eclipse.egit.core.project.RepositoryMapping; import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.annotations.Nullable; @@ -662,4 +664,20 @@ public class RepositoryUtil { } return false; } + + /** + * Determines whether the given {@link Repository} has any changes by + * checking the {@link IndexDiffCacheEntry} of the repository. + * + * @param repository + * to check + * @return {@code true} if the repository has any changes, {@code false} + * otherwise + */ + public static boolean hasChanges(@NonNull Repository repository) { + IndexDiffCacheEntry entry = Activator.getDefault().getIndexDiffCache() + .getIndexDiffCacheEntry(repository); + IndexDiffData data = entry != null ? entry.getIndexDiff() : null; + return data != null && data.hasChanges(); + } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffData.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffData.java index 3b518a6809..ef43a7fb48 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffData.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffData.java @@ -349,6 +349,20 @@ public class IndexDiffData { } /** + * Determines whether this {@link IndexDiffData} does contain any changes. + * + * @return {@code true} if there are changes; {@code false} otherwise + */ + public boolean hasChanges() { + return !(getAdded().isEmpty() // + && getChanged().isEmpty() // + && getRemoved().isEmpty() // + && getUntracked().isEmpty() // + && getModified().isEmpty() // + && getMissing().isEmpty()); + } + + /** * @return the changed files */ @NonNull diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/GitLabels.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/GitLabels.java index c30d6d1ae4..b7be02f35e 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/GitLabels.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/GitLabels.java @@ -13,14 +13,13 @@ package org.eclipse.egit.ui.internal; import java.io.IOException; import org.eclipse.egit.core.RepositoryUtil; -import org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry; -import org.eclipse.egit.core.internal.indexdiff.IndexDiffData; import org.eclipse.egit.ui.Activator; import org.eclipse.egit.ui.internal.clone.ProjectRecord; import org.eclipse.egit.ui.internal.repository.tree.RefNode; import org.eclipse.egit.ui.internal.synchronize.model.GitModelObject; import org.eclipse.egit.ui.internal.synchronize.model.GitModelRepository; import org.eclipse.jface.viewers.StyledString; +import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.lib.BranchTrackingStatus; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; @@ -84,6 +83,24 @@ public class GitLabels { } /** + * Returns a {@link StyledString} that is initialized with "> " if the + * repository has any changes, empty otherwise. + * + * @param repository + * to get the string for + * @return the {@link StyledString} + */ + public static @NonNull StyledString getChangedPrefix( + @NonNull Repository repository) { + StyledString string = new StyledString(); + if (RepositoryUtil.hasChanges(repository)) { + string.append('>', StyledString.DECORATIONS_STYLER); + string.append(' '); + } + return string; + } + + /** * Computes detailed repository label that consists of repository name, * state, checked-out branch and it's status (returned by * {@linkplain #formatBranchTrackingStatus(BranchTrackingStatus)}) @@ -92,29 +109,13 @@ public class GitLabels { * @return a styled string for the repository * @throws IOException */ - public static StyledString getStyledLabel(Repository repository) + public static @NonNull StyledString getStyledLabel( + @NonNull Repository repository) throws IOException { RepositoryUtil repositoryUtil = Activator.getDefault() .getRepositoryUtil(); - StyledString string = new StyledString(); - - IndexDiffCacheEntry entry = org.eclipse.egit.core.Activator - .getDefault().getIndexDiffCache() - .getIndexDiffCacheEntry(repository); - if (entry != null) { - IndexDiffData indexDiffData = entry.getIndexDiff(); - if (indexDiffData != null - && (!indexDiffData.getAdded().isEmpty() - || !indexDiffData.getChanged().isEmpty() - || !indexDiffData.getRemoved().isEmpty() - || !indexDiffData.getUntracked().isEmpty() - || !indexDiffData.getModified().isEmpty() || !indexDiffData - .getMissing().isEmpty())) { - string.append('>', StyledString.DECORATIONS_STYLER); - string.append(' '); - } - } + StyledString string = getChangedPrefix(repository); string.append(repositoryUtil.getRepositoryName(repository)); @@ -158,7 +159,8 @@ public class GitLabels { * @param repository * @return repository label */ - public static StyledString getStyledLabelSafe(Repository repository) { + public static @NonNull StyledString getStyledLabelSafe( + @NonNull Repository repository) { try { return getStyledLabel(repository); } catch (IOException e) { @@ -184,7 +186,8 @@ public class GitLabels { * @param element * @return element's label */ - public static StyledString getStyledLabelExtendedSafe(Object element) { + public static @NonNull StyledString getStyledLabelExtendedSafe( + Object element) { Repository repo = asRepository(element); if (repo != null) { diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewLabelProvider.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewLabelProvider.java index fe1c2248e2..92cfa03193 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewLabelProvider.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewLabelProvider.java @@ -21,6 +21,7 @@ import org.eclipse.core.commands.IStateListener; import org.eclipse.core.commands.State; import org.eclipse.core.runtime.IPath; import org.eclipse.egit.core.Activator; +import org.eclipse.egit.core.RepositoryUtil; import org.eclipse.egit.ui.internal.CommonUtils; import org.eclipse.egit.ui.internal.GitLabels; import org.eclipse.egit.ui.internal.ResourcePropertyTester; @@ -30,6 +31,7 @@ import org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode; import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode; import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNodeType; import org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode; +import org.eclipse.egit.ui.internal.repository.tree.SubmodulesNode; import org.eclipse.egit.ui.internal.repository.tree.TagNode; import org.eclipse.egit.ui.internal.repository.tree.command.ToggleBranchCommitCommand; import org.eclipse.jface.resource.CompositeImageDescriptor; @@ -46,6 +48,7 @@ import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.submodule.SubmoduleWalk; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; @@ -273,8 +276,12 @@ public class RepositoriesViewLabelProvider extends ColumnLabelProvider * @return styled string */ protected StyledString getStyledTextForSubmodule(RepositoryTreeNode node) { - StyledString string = new StyledString(); Repository repository = (Repository) node.getObject(); + if (repository == null) { + return new StyledString(); + } + StyledString string = GitLabels.getChangedPrefix(repository); + String path = Repository.stripWorkDir(node.getParent().getRepository() .getWorkTree(), repository.getWorkTree()); string.append(path); @@ -331,6 +338,48 @@ public class RepositoriesViewLabelProvider extends ColumnLabelProvider return string; } + /** + * Gets the {@link StyledString} for a {@link SubmodulesNode}. + * + * @param node + * to get the text for + * @return the {@link StyledString} + */ + protected StyledString getStyledTextForSubmodules(SubmodulesNode node) { + String label = getSimpleText(node); + if (label == null) { + return new StyledString(); + } + StyledString styled = new StyledString(label); + Repository repository = node.getRepository(); + if (repository != null) { + boolean hasChanges = false; + try (SubmoduleWalk walk = SubmoduleWalk.forIndex(repository)) { + while (!hasChanges && walk.next()) { + Repository submodule = walk.getRepository(); + if (submodule != null) { + Repository cached = org.eclipse.egit.core.Activator + .getDefault().getRepositoryCache() + .lookupRepository(submodule.getDirectory() + .getAbsoluteFile()); + hasChanges = cached != null + && RepositoryUtil.hasChanges(cached); + submodule.close(); + } + } + } catch (IOException e) { + hasChanges = false; + } + if (hasChanges) { + StyledString prefixed = new StyledString(); + prefixed.append('>', StyledString.DECORATIONS_STYLER); + prefixed.append(' ').append(styled); + return prefixed; + } + } + return styled; + } + @Override public StyledString getStyledText(Object element) { if (!(element instanceof RepositoryTreeNode)) @@ -402,6 +451,8 @@ public class RepositoriesViewLabelProvider extends ColumnLabelProvider return getStyledTextForTag((TagNode) node); case STASHED_COMMIT: return getStyledTextForCommit((StashedCommitNode) node); + case SUBMODULES: + return getStyledTextForSubmodules((SubmodulesNode) node); case PUSH: // fall through case FETCH: @@ -426,8 +477,6 @@ public class RepositoriesViewLabelProvider extends ColumnLabelProvider // fall through case REMOTE: // fall through - case SUBMODULES: - // fall through case STASH: // fall through case ERROR: { |