[git] get rid of dummy hash: GitBranch does not have hash anymore

This commit is contained in:
Julia Beliaeva
2015-12-13 18:25:24 +03:00
parent eda1db5132
commit f88f0629f4
22 changed files with 149 additions and 131 deletions
+7 -20
View File
@@ -17,11 +17,11 @@ package git4idea;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.vcs.log.Hash;
import com.intellij.vcs.log.impl.HashImpl;
import git4idea.branch.GitBranchUtil;
import git4idea.repo.GitRepository;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* <p>Represents a Git branch, local or remote.</p>
@@ -44,30 +44,17 @@ public abstract class GitBranch extends GitReference {
@NonNls public static final String REFS_HEADS_PREFIX = "refs/heads/"; // Prefix for local branches ({@value})
@NonNls public static final String REFS_REMOTES_PREFIX = "refs/remotes/"; // Prefix for remote branches ({@value})
private static final Logger LOG = Logger.getInstance(GitBranch.class);
/**
* @deprecated All usages should be reviewed and substituted with actual GitBranch objects with Hashes retrieved from the GitRepository.
*/
@Deprecated
public static final Hash DUMMY_HASH = HashImpl.build("");
@Nullable
public static final Hash DUMMY_HASH = null;
private static final Logger LOG = Logger.getInstance(GitBranch.class);
@NotNull private final Hash myHash;
protected GitBranch(@NotNull String name, @NotNull Hash hash) {
protected GitBranch(@NotNull String name) {
super(GitBranchUtil.stripRefsPrefix(name));
myHash = hash;
}
/**
* <p>Returns the hash on which this branch is reference to.</p>
*
* <p>In certain cases (which are to be eliminated in the future) it may be empty,
* if this information wasn't supplied to the GitBranch constructor.</p>
*/
@NotNull
public Hash getHash() {
return myHash;
}
/**
@@ -121,7 +108,7 @@ public abstract class GitBranch extends GitReference {
@NotNull
public String toLogString() {
return String.format("%s:%s:%s", getFullName(), getHash(), isRemote() ? "remote" : "local");
return String.format("%s:%s", getFullName(), isRemote() ? "remote" : "local");
}
}
@@ -15,7 +15,6 @@
*/
package git4idea;
import com.intellij.vcs.log.Hash;
import git4idea.repo.GitBranchTrackInfo;
import git4idea.repo.GitRepository;
import org.jetbrains.annotations.NotNull;
@@ -26,8 +25,8 @@ import org.jetbrains.annotations.Nullable;
*/
public class GitLocalBranch extends GitBranch {
public GitLocalBranch(@NotNull String name, @NotNull Hash hash) {
super(name, hash);
public GitLocalBranch(@NotNull String name) {
super(name);
}
@Override
@@ -15,7 +15,6 @@
*/
package git4idea;
import com.intellij.vcs.log.Hash;
import git4idea.repo.GitRemote;
import org.jetbrains.annotations.NotNull;
@@ -24,8 +23,8 @@ import org.jetbrains.annotations.NotNull;
*/
public abstract class GitRemoteBranch extends GitBranch {
protected GitRemoteBranch(@NotNull String name, @NotNull Hash hash) {
super(name, hash);
protected GitRemoteBranch(@NotNull String name) {
super(name);
}
/**
@@ -19,14 +19,20 @@ import com.intellij.vcs.log.Hash;
import git4idea.branch.GitBranchUtil;
import git4idea.repo.GitRemote;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class GitStandardRemoteBranch extends GitRemoteBranch {
@NotNull private final GitRemote myRemote;
@NotNull private final String myNameAtRemote;
public GitStandardRemoteBranch(@NotNull GitRemote remote, @NotNull String nameAtRemote, @NotNull Hash hash) {
super(formStandardName(remote, GitBranchUtil.stripRefsPrefix(nameAtRemote)), hash);
@Deprecated
public GitStandardRemoteBranch(@NotNull GitRemote remote, @NotNull String nameAtRemote, @Nullable Hash hash) {
this(remote, nameAtRemote);
}
public GitStandardRemoteBranch(@NotNull GitRemote remote, @NotNull String nameAtRemote) {
super(formStandardName(remote, GitBranchUtil.stripRefsPrefix(nameAtRemote)));
myRemote = remote;
myNameAtRemote = GitBranchUtil.stripRefsPrefix(nameAtRemote);
}
@@ -15,7 +15,6 @@
*/
package git4idea;
import com.intellij.vcs.log.Hash;
import git4idea.repo.GitRemote;
import org.jetbrains.annotations.NotNull;
@@ -31,8 +30,8 @@ import org.jetbrains.annotations.NotNull;
*/
public class GitSvnRemoteBranch extends GitRemoteBranch {
public GitSvnRemoteBranch(@NotNull String fullName, @NotNull Hash hash) {
super(fullName, hash);
public GitSvnRemoteBranch(@NotNull String fullName) {
super(fullName);
}
@NotNull
+1 -1
View File
@@ -726,7 +726,7 @@ public class GitUtil {
@NotNull GitRemote remote,
@NotNull String branchName) {
GitRemoteBranch remoteBranch = findRemoteBranch(repository, remote, branchName);
return ObjectUtils.notNull(remoteBranch, new GitStandardRemoteBranch(remote, branchName, GitBranch.DUMMY_HASH));
return ObjectUtils.notNull(remoteBranch, new GitStandardRemoteBranch(remote, branchName));
}
@Nullable
@@ -123,7 +123,7 @@ public class GitBranchUtil {
try {
String name = handler.run();
if (!name.equals("HEAD")) {
return new GitLocalBranch(name, GitBranch.DUMMY_HASH);
return new GitLocalBranch(name);
}
else {
return null;
@@ -180,12 +180,12 @@ public class GitBranchUtil {
}
if (".".equals(remoteName)) {
return new GitSvnRemoteBranch(branch, GitBranch.DUMMY_HASH);
return new GitSvnRemoteBranch(branch);
}
GitRemote remote = findRemoteByNameOrLogError(project, root, remoteName);
if (remote == null) return null;
return new GitStandardRemoteBranch(remote, branch, GitBranch.DUMMY_HASH);
return new GitStandardRemoteBranch(remote, branch);
}
@Nullable
@@ -17,6 +17,7 @@ package git4idea.branch;
import com.intellij.openapi.util.Condition;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.Hash;
import git4idea.GitBranch;
import git4idea.GitLocalBranch;
import git4idea.GitRemoteBranch;
@@ -25,50 +26,60 @@ import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* <p>
* Storage for local, remote and current branches.
* The reason of creating this special collection is that
* in the terms of performance, they are detected by {@link git4idea.repo.GitRepositoryReader} at once;
* and also usually both sets of branches are needed by components, but are treated differently,
* so it is more convenient to have them separated, but in a single container.
* </p>
*
* Storage for local, remote and current branches.
* The reason of creating this special collection is that
* in the terms of performance, they are detected by {@link git4idea.repo.GitRepositoryReader} at once;
* and also usually both sets of branches are needed by components, but are treated differently,
* so it is more convenient to have them separated, but in a single container.
* </p>
*
* @author Kirill Likhodedov
*/
public final class GitBranchesCollection {
public static final GitBranchesCollection EMPTY = new GitBranchesCollection(Collections.<GitLocalBranch>emptyList(),
Collections.<GitRemoteBranch>emptyList());
private final Collection<GitLocalBranch> myLocalBranches;
private final Collection<GitRemoteBranch> myRemoteBranches;
public static final GitBranchesCollection EMPTY =
new GitBranchesCollection(Collections.<GitLocalBranch, Hash>emptyMap(), Collections.<GitRemoteBranch, Hash>emptyMap());
public GitBranchesCollection(@NotNull Collection<GitLocalBranch> localBranches, @NotNull Collection<GitRemoteBranch> remoteBranches) {
@NotNull
private final Map<GitLocalBranch, Hash> myLocalBranches;
@NotNull
private final Map<GitRemoteBranch, Hash> myRemoteBranches;
public GitBranchesCollection(@NotNull Map<GitLocalBranch, Hash> localBranches, @NotNull Map<GitRemoteBranch, Hash> remoteBranches) {
myRemoteBranches = remoteBranches;
myLocalBranches = localBranches;
}
@NotNull
public Collection<GitLocalBranch> getLocalBranches() {
return Collections.unmodifiableCollection(myLocalBranches);
return Collections.unmodifiableCollection(myLocalBranches.keySet());
}
@NotNull
public Collection<GitRemoteBranch> getRemoteBranches() {
return Collections.unmodifiableCollection(myRemoteBranches);
return Collections.unmodifiableCollection(myRemoteBranches.keySet());
}
@Nullable
public Hash getHash(@NotNull GitBranch branch) {
if (branch instanceof GitLocalBranch) return myLocalBranches.get(branch);
if (branch instanceof GitRemoteBranch) return myRemoteBranches.get(branch);
return null;
}
@Nullable
public GitLocalBranch findLocalBranch(@NotNull String name) {
return findByName(myLocalBranches, name);
return findByName(myLocalBranches.keySet(), name);
}
@Nullable
public GitBranch findBranchByName(@NotNull String name) {
GitLocalBranch branch = findByName(myLocalBranches, name);
return branch != null ? branch : findByName(myRemoteBranches, name);
GitLocalBranch branch = findByName(myLocalBranches.keySet(), name);
return branch != null ? branch : findByName(myRemoteBranches.keySet(), name);
}
@Nullable
@@ -35,6 +35,7 @@ import com.intellij.vcs.log.impl.LogDataImpl;
import com.intellij.vcs.log.util.StopWatch;
import git4idea.*;
import git4idea.branch.GitBranchUtil;
import git4idea.branch.GitBranchesCollection;
import git4idea.config.GitVersionSpecialty;
import git4idea.history.GitHistoryUtils;
import git4idea.repo.GitRepository;
@@ -351,15 +352,19 @@ public class GitLogProvider implements VcsLogProvider {
StopWatch sw = StopWatch.start("readBranches in " + repository.getRoot().getName());
VirtualFile root = repository.getRoot();
repository.update();
Collection<GitLocalBranch> localBranches = repository.getBranches().getLocalBranches();
Collection<GitRemoteBranch> remoteBranches = repository.getBranches().getRemoteBranches();
GitBranchesCollection branches = repository.getBranches();
Collection<GitLocalBranch> localBranches = branches.getLocalBranches();
Collection<GitRemoteBranch> remoteBranches = branches.getRemoteBranches();
Set<VcsRef> refs = new THashSet<VcsRef>(localBranches.size() + remoteBranches.size());
for (GitLocalBranch localBranch : localBranches) {
refs.add(myVcsObjectsFactory.createRef(localBranch.getHash(), localBranch.getName(), GitRefManager.LOCAL_BRANCH, root));
Hash hash = branches.getHash(localBranch);
assert hash != null;
refs.add(myVcsObjectsFactory.createRef(hash, localBranch.getName(), GitRefManager.LOCAL_BRANCH, root));
}
for (GitRemoteBranch remoteBranch : remoteBranches) {
refs.add(
myVcsObjectsFactory.createRef(remoteBranch.getHash(), remoteBranch.getNameForLocalOperations(), GitRefManager.REMOTE_BRANCH, root));
Hash hash = branches.getHash(remoteBranch);
assert hash != null;
refs.add(myVcsObjectsFactory.createRef(hash, remoteBranch.getNameForLocalOperations(), GitRefManager.REMOTE_BRANCH, root));
}
String currentRevision = repository.getCurrentRevision();
if (currentRevision != null) { // null => fresh repository
@@ -136,7 +136,7 @@ public class GitPushSupport extends PushSupport<GitRepository, GitPushSource, Gi
if (existingRemoteBranch != null) {
return new GitPushTarget(existingRemoteBranch, false);
}
return new GitPushTarget(new GitStandardRemoteBranch(remote, currentBranch.getName(), GitBranch.DUMMY_HASH), true);
return new GitPushTarget(new GitStandardRemoteBranch(remote, currentBranch.getName()), true);
}
@NotNull
@@ -100,7 +100,7 @@ public class GitPushTarget implements PushTarget {
if (existingRemoteBranch != null) {
return new GitPushTarget(existingRemoteBranch, false);
}
GitRemoteBranch rb = new GitStandardRemoteBranch(remote, branchName, GitBranch.DUMMY_HASH);
GitRemoteBranch rb = new GitStandardRemoteBranch(remote, branchName);
return new GitPushTarget(rb, true);
}
@@ -15,7 +15,6 @@
*/
package git4idea.push;
import git4idea.GitBranch;
import git4idea.GitRemoteBranch;
import git4idea.repo.GitRemote;
import org.jetbrains.annotations.NotNull;
@@ -28,7 +27,7 @@ class GitSpecialRefRemoteBranch extends GitRemoteBranch {
private final GitRemote myRemote;
public GitSpecialRefRemoteBranch(@NotNull String ref, @NotNull GitRemote remote) {
super(ref, GitBranch.DUMMY_HASH);
super(ref);
myRef = ref;
myRemote = remote;
}
@@ -427,12 +427,12 @@ public class GitRebaseDialog extends DialogWrapper {
else {
mergeBranch = GitBranchUtil.stripRefsPrefix(mergeBranch);
if (remote.equals(".")) {
trackedBranch = new GitSvnRemoteBranch(mergeBranch, GitBranch.DUMMY_HASH);
trackedBranch = new GitSvnRemoteBranch(mergeBranch);
}
else {
GitRemote r = GitBranchUtil.findRemoteByNameOrLogError(myProject, root, remote);
if (r != null) {
trackedBranch = new GitStandardRemoteBranch(r, mergeBranch, GitBranch.DUMMY_HASH);
trackedBranch = new GitStandardRemoteBranch(r, mergeBranch);
}
}
}
@@ -16,25 +16,26 @@
package git4idea.repo;
import com.intellij.dvcs.repo.Repository;
import com.intellij.vcs.log.Hash;
import git4idea.GitLocalBranch;
import git4idea.GitRemoteBranch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Map;
class GitBranchState {
@Nullable private final String currentRevision;
@Nullable private final GitLocalBranch currentBranch;
@NotNull private final Repository.State state;
@NotNull private final Collection<GitLocalBranch> localBranches;
@NotNull private final Collection<GitRemoteBranch> remoteBranches;
@NotNull private final Map<GitLocalBranch, Hash> localBranches;
@NotNull private final Map<GitRemoteBranch, Hash> remoteBranches;
GitBranchState(@Nullable String currentRevision,
@Nullable GitLocalBranch currentBranch,
@NotNull Repository.State state,
@NotNull Collection<GitLocalBranch> localBranches,
@NotNull Collection<GitRemoteBranch> remoteBranches) {
@NotNull Map<GitLocalBranch, Hash> localBranches,
@NotNull Map<GitRemoteBranch, Hash> remoteBranches) {
this.currentRevision = currentRevision;
this.currentBranch = currentBranch;
this.state = state;
@@ -58,12 +59,12 @@ class GitBranchState {
}
@NotNull
public Collection<GitLocalBranch> getLocalBranches() {
public Map<GitLocalBranch, Hash> getLocalBranches() {
return localBranches;
}
@NotNull
public Collection<GitRemoteBranch> getRemoteBranches() {
public Map<GitRemoteBranch, Hash> getRemoteBranches() {
return remoteBranches;
}
}
@@ -16,6 +16,7 @@
package git4idea.repo;
import com.intellij.dvcs.repo.Repository;
import com.intellij.vcs.log.Hash;
import git4idea.GitBranch;
import git4idea.GitLocalBranch;
import git4idea.GitRemoteBranch;
@@ -24,9 +25,7 @@ import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.*;
/**
* @author Kirill Likhodedov
@@ -37,19 +36,23 @@ public class GitRepoInfo {
@Nullable private final String myCurrentRevision;
@NotNull private final Repository.State myState;
@NotNull private final Set<GitRemote> myRemotes;
@NotNull private final Set<GitLocalBranch> myLocalBranches;
@NotNull private final Set<GitRemoteBranch> myRemoteBranches;
@NotNull private final Map<GitLocalBranch, Hash> myLocalBranches;
@NotNull private final Map<GitRemoteBranch, Hash> myRemoteBranches;
@NotNull private final Set<GitBranchTrackInfo> myBranchTrackInfos;
public GitRepoInfo(@Nullable GitLocalBranch currentBranch, @Nullable String currentRevision, @NotNull Repository.State state,
@NotNull Collection<GitRemote> remotes, @NotNull Collection<GitLocalBranch> localBranches,
@NotNull Collection<GitRemoteBranch> remoteBranches, @NotNull Collection<GitBranchTrackInfo> branchTrackInfos) {
public GitRepoInfo(@Nullable GitLocalBranch currentBranch,
@Nullable String currentRevision,
@NotNull Repository.State state,
@NotNull Collection<GitRemote> remotes,
@NotNull Map<GitLocalBranch, Hash> localBranches,
@NotNull Map<GitRemoteBranch, Hash> remoteBranches,
@NotNull Collection<GitBranchTrackInfo> branchTrackInfos) {
myCurrentBranch = currentBranch;
myCurrentRevision = currentRevision;
myState = state;
myRemotes = new LinkedHashSet<GitRemote>(remotes);
myLocalBranches = new LinkedHashSet<GitLocalBranch>(localBranches);
myRemoteBranches = new LinkedHashSet<GitRemoteBranch>(remoteBranches);
myLocalBranches = new LinkedHashMap<GitLocalBranch, Hash>(localBranches);
myRemoteBranches = new LinkedHashMap<GitRemoteBranch, Hash>(remoteBranches);
myBranchTrackInfos = new LinkedHashSet<GitBranchTrackInfo>(branchTrackInfos);
}
@@ -64,15 +67,21 @@ public class GitRepoInfo {
}
@NotNull
public Collection<GitLocalBranch> getLocalBranches() {
public Map<GitLocalBranch, Hash> getLocalBranchesWithHashes() {
return myLocalBranches;
}
@NotNull
public Collection<GitRemoteBranch> getRemoteBranches() {
public Map<GitRemoteBranch, Hash> getRemoteBranchesWithHashes() {
return myRemoteBranches;
}
@NotNull
@Deprecated
public Collection<GitRemoteBranch> getRemoteBranches() {
return myRemoteBranches.keySet();
}
@NotNull
public Collection<GitBranchTrackInfo> getBranchTrackInfos() {
return myBranchTrackInfos;
@@ -120,34 +129,37 @@ public class GitRepoInfo {
@Override
public String toString() {
return String.format("GitRepoInfo{current=%s, remotes=%s, localBranches=%s, remoteBranches=%s, trackInfos=%s}",
myCurrentBranch, myRemotes, myLocalBranches, myRemoteBranches, myBranchTrackInfos);
return String
.format("GitRepoInfo{current=%s, remotes=%s, localBranches=%s, remoteBranches=%s, trackInfos=%s}", myCurrentBranch, myRemotes,
myLocalBranches, myRemoteBranches, myBranchTrackInfos);
}
private static <T extends GitBranch> boolean areEqual(Collection<T> c1, Collection<T> c2) {
private static <T extends GitBranch> boolean areEqual(Map<T, Hash> c1, Map<T, Hash> c2) {
// GitBranch has perverted equals contract (see the comment there)
// until GitBranch is created only from a single place with correctly defined Hash, we can't change its equals
THashSet<GitBranch> set1 = new THashSet<GitBranch>(c1, new BranchesComparingStrategy());
THashSet<GitBranch> set2 = new THashSet<GitBranch>(c2, new BranchesComparingStrategy());
THashSet<Map.Entry<? extends GitBranch, Hash>> set1 =
new THashSet<Map.Entry<? extends GitBranch, Hash>>(c1.entrySet(), new BranchesComparingStrategy());
THashSet<Map.Entry<? extends GitBranch, Hash>> set2 =
new THashSet<Map.Entry<? extends GitBranch, Hash>>(c2.entrySet(), new BranchesComparingStrategy());
return set1.equals(set2);
}
private static class BranchesComparingStrategy implements TObjectHashingStrategy<GitBranch> {
private static class BranchesComparingStrategy implements TObjectHashingStrategy<Map.Entry<? extends GitBranch, Hash>> {
@Override
public int computeHashCode(@NotNull GitBranch branch) {
return 31 * branch.getName().hashCode() + branch.getHash().hashCode();
public int computeHashCode(@NotNull Map.Entry<? extends GitBranch, Hash> branchEntry) {
return 31 * branchEntry.getKey().getName().hashCode() + branchEntry.getValue().hashCode();
}
@Override
public boolean equals(@NotNull GitBranch b1, @NotNull GitBranch b2) {
public boolean equals(@NotNull Map.Entry<? extends GitBranch, Hash> b1, @NotNull Map.Entry<? extends GitBranch, Hash> b2) {
if (b1 == b2) {
return true;
}
if (b1.getClass() != b2.getClass()) {
return false;
}
return b1.getName().equals(b2.getName()) && b1.getHash().equals(b2.getHash());
return b1.getKey().getName().equals(b2.getKey().getName()) && b2.getValue().equals(b2.getValue());
}
}
@@ -155,7 +155,7 @@ public class GitRepositoryImpl extends RepositoryImpl implements GitRepository {
@NotNull
public GitBranchesCollection getBranches() {
GitRepoInfo info = myInfo;
return new GitBranchesCollection(info.getLocalBranches(), info.getRemoteBranches());
return new GitBranchesCollection(info.getLocalBranchesWithHashes(), info.getRemoteBranchesWithHashes());
}
@Override
@@ -198,7 +198,7 @@ public class GitRepositoryImpl extends RepositoryImpl implements GitRepository {
GitConfig config = GitConfig.read(myPlatformFacade, configFile);
Collection<GitRemote> remotes = config.parseRemotes();
GitBranchState state = myReader.readState(remotes);
Collection<GitBranchTrackInfo> trackInfos = config.parseTrackInfos(state.getLocalBranches(), state.getRemoteBranches());
Collection<GitBranchTrackInfo> trackInfos = config.parseTrackInfos(state.getLocalBranches().keySet(), state.getRemoteBranches().keySet());
return new GitRepoInfo(state.getCurrentBranch(), state.getCurrentRevision(), state.getState(), remotes,
state.getLocalBranches(), state.getRemoteBranches(), trackInfos);
}
@@ -79,8 +79,8 @@ class GitRepositoryReader {
@NotNull
GitBranchState readState(@NotNull Collection<GitRemote> remotes) {
Pair<Set<GitLocalBranch>, Set<GitRemoteBranch>> branches = readBranches(remotes);
Set<GitLocalBranch> localBranches = branches.first;
Pair<Map<GitLocalBranch, Hash>, Map<GitRemoteBranch, Hash>> branches = readBranches(remotes);
Map<GitLocalBranch, Hash> localBranches = branches.first;
HeadInfo headInfo = readHead();
Repository.State state = readRepositoryState(headInfo);
@@ -92,11 +92,11 @@ class GitRepositoryReader {
currentRevision = headInfo.content;
}
else if (!localBranches.isEmpty()) {
currentBranch = findCurrentBranch(headInfo, state, localBranches);
currentRevision = getCurrentRevision(headInfo, currentBranch);
currentBranch = findCurrentBranch(headInfo, state, localBranches.keySet());
currentRevision = getCurrentRevision(headInfo, currentBranch == null ? null : localBranches.get(currentBranch));
}
else if (headInfo.content != null) {
currentBranch = new GitLocalBranch(headInfo.content, GitBranch.DUMMY_HASH);
currentBranch = new GitLocalBranch(headInfo.content);
currentRevision = null;
}
else {
@@ -107,16 +107,16 @@ class GitRepositoryReader {
}
@Nullable
private static String getCurrentRevision(@NotNull HeadInfo headInfo, @Nullable GitLocalBranch currentBranch) {
private static String getCurrentRevision(@NotNull HeadInfo headInfo, @Nullable Hash currentBranchHash) {
String currentRevision;
if (!headInfo.isBranch) {
currentRevision = headInfo.content;
}
else if (currentBranch == null) {
else if (currentBranchHash == null) {
currentRevision = null;
}
else {
currentRevision = currentBranch.getHash().asString();
currentRevision = currentBranchHash.asString();
}
return currentRevision;
}
@@ -220,7 +220,7 @@ class GitRepositoryReader {
}
@NotNull
private Pair<Set<GitLocalBranch>, Set<GitRemoteBranch>> readBranches(@NotNull Collection<GitRemote> remotes) {
private Pair<Map<GitLocalBranch, Hash>, Map<GitRemoteBranch, Hash>> readBranches(@NotNull Collection<GitRemote> remotes) {
Map<String, String> data = readBranchRefsFromFiles();
Map<String, Hash> resolvedRefs = resolveRefs(data);
return createBranchesFromData(remotes, resolvedRefs);
@@ -236,20 +236,20 @@ class GitRepositoryReader {
}
@NotNull
private static Pair<Set<GitLocalBranch>, Set<GitRemoteBranch>> createBranchesFromData(@NotNull Collection<GitRemote> remotes,
@NotNull Map<String, Hash> data) {
Set<GitLocalBranch> localBranches = ContainerUtil.newHashSet();
Set<GitRemoteBranch> remoteBranches = ContainerUtil.newHashSet();
private static Pair<Map<GitLocalBranch, Hash>, Map<GitRemoteBranch, Hash>> createBranchesFromData(@NotNull Collection<GitRemote> remotes,
@NotNull Map<String, Hash> data) {
Map<GitLocalBranch, Hash> localBranches = ContainerUtil.newHashMap();
Map<GitRemoteBranch, Hash> remoteBranches = ContainerUtil.newHashMap();
for (Map.Entry<String, Hash> entry : data.entrySet()) {
String refName = entry.getKey();
Hash hash = entry.getValue();
if (refName.startsWith(REFS_HEADS_PREFIX)) {
localBranches.add(new GitLocalBranch(refName, hash));
localBranches.put(new GitLocalBranch(refName), hash);
}
else if (refName.startsWith(REFS_REMOTES_PREFIX)) {
GitRemoteBranch remoteBranch = parseRemoteBranch(refName, hash, remotes);
GitRemoteBranch remoteBranch = parseRemoteBranch(refName, remotes);
if (remoteBranch != null) {
remoteBranches.add(remoteBranch);
remoteBranches.put(remoteBranch, hash);
}
}
else {
@@ -295,13 +295,12 @@ class GitRepositoryReader {
@Nullable
private static GitRemoteBranch parseRemoteBranch(@NotNull String fullBranchName,
@NotNull Hash hash,
@NotNull Collection<GitRemote> remotes) {
String stdName = GitBranchUtil.stripRefsPrefix(fullBranchName);
int slash = stdName.indexOf('/');
if (slash == -1) { // .git/refs/remotes/my_branch => git-svn
return new GitSvnRemoteBranch(fullBranchName, hash);
return new GitSvnRemoteBranch(fullBranchName);
}
else {
GitRemote remote;
@@ -319,9 +318,9 @@ class GitRepositoryReader {
LOG.debug(String.format("No remote found with the name [%s]. All remotes: %s", remoteName, remotes));
GitRemote fakeRemote = new GitRemote(remoteName, ContainerUtil.<String>emptyList(), Collections.<String>emptyList(),
Collections.<String>emptyList(), Collections.<String>emptyList());
return new GitStandardRemoteBranch(fakeRemote, branchName, hash);
return new GitStandardRemoteBranch(fakeRemote, branchName);
}
return new GitStandardRemoteBranch(remote, branchName, hash);
return new GitStandardRemoteBranch(remote, branchName);
}
}
@@ -125,7 +125,7 @@ abstract class GitPushOperationBaseTest : GitPlatformTest() {
if (target == null) {
val firstSlash = to.indexOf('/')
val remote = GitUtil.findRemoteByName(repository, to.substring(0, firstSlash))!!
target = GitStandardRemoteBranch(remote, to.substring(firstSlash + 1), GitBranch.DUMMY_HASH)
target = GitStandardRemoteBranch(remote, to.substring(firstSlash + 1))
newBranch = true
}
else {
@@ -206,14 +206,14 @@ public class GitPushResultNotificationTest extends GitPlatformTest {
}
private static GitLocalBranch from(String from) {
return new GitLocalBranch(from, GitBranch.DUMMY_HASH);
return new GitLocalBranch(from);
}
private static GitRemoteBranch to(String to) {
int firstSlash = to.indexOf('/');
GitRemote remote = new GitRemote(to.substring(0, firstSlash), Collections.<String>emptyList(), Collections.<String>emptyList(),
Collections.<String>emptyList(), Collections.<String>emptyList());
return new GitStandardRemoteBranch(remote, to.substring(firstSlash + 1), GitBranch.DUMMY_HASH);
return new GitStandardRemoteBranch(remote, to.substring(firstSlash + 1));
}
private GitPushResultNotification notification(GitPushRepoResult singleResult) {
@@ -65,7 +65,7 @@ public class GitConfigTest extends GitPlatformTest {
File gitDir = new File(myProjectPath, ".git");
GitConfig config = GitConfig.read(myPlatformFacade, new File(gitDir, "config"));
GitBranchState state = new GitRepositoryReader(gitDir).readState(config.parseRemotes());
Collection<GitBranchTrackInfo> trackInfos = config.parseTrackInfos(state.getLocalBranches(), state.getRemoteBranches());
Collection<GitBranchTrackInfo> trackInfos = config.parseTrackInfos(state.getLocalBranches().keySet(), state.getRemoteBranches().keySet());
assertTrue("Couldn't find correct a#branch tracking information among: [" + trackInfos + "]",
ContainerUtil.exists(trackInfos, new Condition<GitBranchTrackInfo>() {
@Override
@@ -213,8 +213,8 @@ public class GitConfigTest extends GitPlatformTest {
String remoteBranchAtRemote = info[2];
String remoteBranchHere = info[3];
boolean merge = info[4].equals("merge");
remotes.add(new GitBranchTrackInfo(new GitLocalBranch(branch, GitBranch.DUMMY_HASH),
new GitStandardRemoteBranch(remote, remoteBranchAtRemote, GitBranch.DUMMY_HASH),
remotes.add(new GitBranchTrackInfo(new GitLocalBranch(branch),
new GitStandardRemoteBranch(remote, remoteBranchAtRemote),
merge));
}
return remotes;
@@ -62,7 +62,7 @@ public class GitRepositoryReaderNewTest extends GitSingleRepoTest {
final String INVALID_REMOTE_BRANCH = "master";
git("update-ref refs/remotes/" + INVALID_REMOTE + "/" + INVALID_REMOTE_BRANCH + " HEAD");
Collection<GitRemoteBranch> remoteBranches = readState().getRemoteBranches();
Collection<GitRemoteBranch> remoteBranches = readState().getRemoteBranches().keySet();
assertTrue("Remote branch not found", ContainerUtil.exists(remoteBranches, new Condition<GitRemoteBranch>() {
@Override
public boolean value(GitRemoteBranch branch) {
@@ -42,6 +42,7 @@ import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@RunWith(Parameterized.class)
public class GitRepositoryReaderTest extends GitPlatformTest {
@@ -140,21 +141,21 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
GitBranchState state = myRepositoryReader.readState(remotes);
assertEquals("HEAD revision is incorrect", readHead(myTempDir), state.getCurrentRevision());
assertEqualBranches(readCurrentBranch(myTempDir), state.getCurrentBranch());
assertEqualBranches(readCurrentBranch(myTempDir), state.getCurrentBranch(), state.getLocalBranches().get(state.getCurrentBranch()));
assertBranches(state.getLocalBranches(), readBranches(myTempDir, true));
assertBranches(state.getRemoteBranches(), readBranches(myTempDir, false));
}
private static void assertEqualBranches(@NotNull Branch expected, @NotNull GitLocalBranch actual) {
private static void assertEqualBranches(@NotNull Branch expected, @NotNull GitLocalBranch actual, @NotNull Hash hash) {
assertEquals(expected.name, actual.getName());
assertEquals("Incorrect hash of branch " + actual.getName(), expected.hash, actual.getHash());
assertEquals("Incorrect hash of branch " + actual.getName(), expected.hash, hash);
}
private static void assertBranches(Collection<? extends GitBranch> actualBranches, Collection<Branch> expectedBranches) {
VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker<GitBranch, Branch>() {
private static void assertBranches(Map<? extends GitBranch, Hash> actualBranches, Collection<Branch> expectedBranches) {
VcsTestUtil.assertEqualCollections(actualBranches.entrySet(), expectedBranches, new VcsTestUtil.EqualityChecker<Map.Entry<? extends GitBranch, Hash>, Branch>() {
@Override
public boolean areEqual(GitBranch actual, Branch expected) {
return branchesAreEqual(actual, expected);
public boolean areEqual(Map.Entry<? extends GitBranch, Hash> actual, Branch expected) {
return branchesAreEqual(actual.getKey(), actual.getValue(), expected);
}
});
}
@@ -169,8 +170,8 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
return branches;
}
private static boolean branchesAreEqual(GitBranch actual, Branch expected) {
return actual.getFullName().equals(expected.name) && actual.getHash().equals(expected.hash);
private static boolean branchesAreEqual(GitBranch actualBranch, Hash actualHash, Branch expected) {
return actualBranch.getFullName().equals(expected.name) && actualHash.equals(expected.hash);
}
private static class Branch {