/plugins/git4idea/src/git4idea/history/browser/CachedRefs.java

https://bitbucket.org/nbargnesi/idea · Java · 154 lines · 87 code · 22 blank · 45 comment · 9 complexity · 35f557a0a3887397a1b537768e34980b MD5 · raw file

  1. /*
  2. * Copyright 2000-2011 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package git4idea.history.browser;
  17. import com.intellij.openapi.util.Pair;
  18. import com.intellij.util.containers.MultiMap;
  19. import git4idea.GitBranch;
  20. import git4idea.branch.GitBranchesCollection;
  21. import git4idea.history.wholeTree.AbstractHash;
  22. import java.util.*;
  23. /**
  24. * Created by IntelliJ IDEA.
  25. * User: Irina.Chernushina
  26. * Date: 12/1/11
  27. * Time: 10:20 PM
  28. */
  29. public class CachedRefs implements SymbolicRefsI {
  30. private GitBranchesCollection myCollection;
  31. private GitBranch myCurrentBranch;
  32. private String myTrackedRemoteName;
  33. private String myUsername;
  34. private AbstractHash myHeadHash;
  35. private TreeSet<String> myLocalBranches;
  36. private TreeSet<String> myRemoteBranches;
  37. // hash to
  38. private final MultiMap<String, Pair<SymbolicRefs.Kind, String>> myRefsMap;
  39. public CachedRefs() {
  40. myRefsMap = new MultiMap<String, Pair<SymbolicRefs.Kind, String>>();
  41. }
  42. public void setCurrentBranch(GitBranch currentBranch) {
  43. myCurrentBranch = currentBranch;
  44. }
  45. public void setCollection(GitBranchesCollection collection) {
  46. myCollection = collection;
  47. /*GitBranch branch = myCollection.getCurrentBranch();
  48. if (branch != null) {
  49. myRefsMap.putValue(branch.getHash(), new Pair<SymbolicRefs.Kind, String>(SymbolicRefs.Kind.LOCAL, branch.getName()));
  50. }
  51. Collection<GitBranch> branches = myCollection.getLocalBranches();
  52. for (GitBranch localBranch : branches) {
  53. myRefsMap.putValue(localBranch.getHash(), new Pair<SymbolicRefs.Kind, String>(SymbolicRefs.Kind.LOCAL, localBranch.getName()));
  54. }
  55. Collection<GitBranch> remoteBranches = myCollection.getRemoteBranches();
  56. for (GitBranch remoteBranch : remoteBranches) {
  57. myRefsMap.putValue(remoteBranch.getHash(), new Pair<SymbolicRefs.Kind, String>(SymbolicRefs.Kind.REMOTE, remoteBranch.getName()));
  58. }*/
  59. }
  60. /*public List<Pair<SymbolicRefs.Kind, String>> getRefsForCommit(final GitCommit gitCommit) {
  61. final List<Pair<SymbolicRefs.Kind, String>> result = new ArrayList<Pair<SymbolicRefs.Kind, String>>();
  62. Collection<Pair<SymbolicRefs.Kind, String>> pairs = myRefsMap.get(gitCommit.getHash().getValue());
  63. if (pairs != null) {
  64. result.addAll(pairs);
  65. }
  66. if (myHeadHash != null && myHeadHash.equals(gitCommit.getShortHash())) {
  67. result.add(new Pair<SymbolicRefs.Kind, String>(SymbolicRefs.Kind.TAG, "HEAD"));
  68. }
  69. return result;
  70. }*/
  71. public void setTrackedRemoteName(String trackedRemoteName) {
  72. myTrackedRemoteName = trackedRemoteName;
  73. }
  74. public void setUsername(String username) {
  75. myUsername = username;
  76. }
  77. public void setHeadHash(AbstractHash headHash) {
  78. myHeadHash = headHash;
  79. }
  80. public TreeSet<String> getLocalBranches() {
  81. if (myLocalBranches == null) {
  82. Collection<GitBranch> branches = myCollection.getLocalBranches();
  83. myLocalBranches = new TreeSet<String>();
  84. for (GitBranch branch : branches) {
  85. myLocalBranches.add(branch.getName());
  86. }
  87. }
  88. return myLocalBranches;
  89. }
  90. public TreeSet<String> getRemoteBranches() {
  91. if (myRemoteBranches == null) {
  92. Collection<GitBranch> branches = myCollection.getRemoteBranches();
  93. myRemoteBranches = new TreeSet<String>();
  94. for (GitBranch branch : branches) {
  95. myRemoteBranches.add(branch.getName());
  96. }
  97. }
  98. return myRemoteBranches;
  99. }
  100. @Override
  101. public String getCurrentName() {
  102. return myCurrentBranch == null ? null : myCurrentBranch.getName();
  103. }
  104. @Override
  105. public GitBranch getCurrent() {
  106. return myCurrentBranch;
  107. }
  108. @Override
  109. public SymbolicRefs.Kind getKind(String s) {
  110. if (getLocalBranches().contains(s)) return SymbolicRefs.Kind.LOCAL;
  111. if (getRemoteBranches().contains(s)) return SymbolicRefs.Kind.REMOTE;
  112. return SymbolicRefs.Kind.TAG;
  113. }
  114. @Override
  115. public String getTrackedRemoteName() {
  116. return myTrackedRemoteName;
  117. }
  118. @Override
  119. public String getUsername() {
  120. return myUsername;
  121. }
  122. @Override
  123. public AbstractHash getHeadHash() {
  124. return myHeadHash;
  125. }
  126. public Collection<GitBranch> getLocal() {
  127. return myCollection.getLocalBranches();
  128. }
  129. public Collection<GitBranch> getRemote() {
  130. return myCollection.getRemoteBranches();
  131. }
  132. }