summaryrefslogtreecommitdiff
path: root/plugins/git4idea/tests/git4idea/log/GitRefManagerTest.java
blob: 39d5946c90ff6e8e89572ab9ffe110ee472136e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package git4idea.log;

import com.intellij.mock.MockVirtualFile;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.VcsRef;
import com.intellij.vcs.log.VcsRefType;
import com.intellij.vcs.log.impl.HashImpl;
import com.intellij.vcs.log.impl.VcsRefImpl;
import git4idea.GitLocalBranch;
import git4idea.GitRemoteBranch;
import git4idea.branch.GitBranchesCollection;
import git4idea.repo.*;
import git4idea.test.GitMockRepositoryManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class GitRefManagerTest extends UsefulTestCase {

  public static final MockVirtualFile MOCK_VIRTUAL_FILE = new MockVirtualFile("mockFile");

  public void testEmpty() {
    check(Collections.<VcsRef>emptyList(), Collections.<VcsRef>emptyList());
  }

  public void testSingle() {
    check(given("HEAD"),
          expect("HEAD"));
  }

  public void testHeadIsMoreImportantThanBranch() {
    check(given("master", "HEAD"),
          expect("HEAD", "master"));
  }

  public void testLocalBranchesAreComparedAsStrings() {
    check(given("release", "feature"),
          expect("feature", "release"));
  }

  public void testTagIsTheLessImportant() {
    check(given("tag/v1", "origin/master"),
          expect("origin/master", "tag/v1"));
  }

  public void testMasterIsMoreImportant() {
    check(given("feature", "master"),
          expect("master", "feature"));
  }

  public void testOriginMasterIsMoreImportant() {
    check(given("origin/master", "origin/aaa"),
          expect("origin/master", "origin/aaa"));
  }

  public void testRemoteBranchHavingTrackingBranchIsMoreImportant() {
    check(given("feature", "origin/aaa", "origin/feature"),
          expect("feature", "origin/feature", "origin/aaa"));
  }

  public void testSeveral1() {
    check(given("tag/v1", "feature", "HEAD", "master"),
          expect("HEAD", "master", "feature", "tag/v1"));
  }

  public void testSeveral2() {
    check(given("origin/master", "origin/great_feature", "tag/v1", "release", "HEAD", "master"),
          expect("HEAD", "master", "release", "origin/master", "origin/great_feature", "tag/v1"));
  }

  // may happen e.g. in multi-repo case
  public void testTwoMasters() {
    check(given("master", "master"),
          expect("master", "master"));
  }

  private static Collection<VcsRef> given(String... refs) {
    return convertToRefs(refs);
  }

  private static List<VcsRef> expect(String... refs) {
    return new ArrayList<VcsRef>(convertToRefs(refs));
  }

  private static List<VcsRef> convertToRefs(String[] refs) {
    return ContainerUtil.map(refs, new Function<String, VcsRef>() {
      @Override
      public VcsRef fun(String name) {
        return ref(name);
      }
    });
  }

  private static VcsRef ref(String name) {
    String randomHash = randomHash();
    if (isHead(name)) {
      return ref(randomHash, name, GitRefManager.HEAD);
    }
    if (isRemoteBranch(name)) {
      return ref(randomHash, name, GitRefManager.REMOTE_BRANCH);
    }
    if (isTag(name)) {
      return ref(randomHash, name, GitRefManager.TAG);
    }
    return ref(randomHash, name, GitRefManager.LOCAL_BRANCH);
  }

  private static String randomHash() {
    return String.valueOf(new Random().nextInt());
  }

  private static boolean isHead(String name) {
    return name.equals("HEAD");
  }

  private static boolean isTag(String name) {
    return name.startsWith("tag/");
  }

  private static boolean isRemoteBranch(String name) {
    return name.startsWith("origin/");
  }

  private static boolean isLocalBranch(String name) {
    return !isHead(name) && !isTag(name) && !isRemoteBranch(name);
  }

  private static VcsRef ref(String hash, String name, VcsRefType type) {
    return new VcsRefImpl(HashImpl.build(hash), name, type, MOCK_VIRTUAL_FILE);
  }

  private static void check(Collection<VcsRef> unsorted, List<VcsRef> expected) {
    // for the sake of simplicity we check only names of references
    List<VcsRef> actual = sort(unsorted);
    assertEquals("Collections size don't match", expected.size(), actual.size());
    for (int i = 0; i < actual.size(); i++) {
      assertEquals("Incorrect element at place " + i, expected.get(i).getName(), actual.get(i).getName());
    }
  }

  private static List<VcsRef> sort(final Collection<VcsRef> refs) {
    final GitMockRepositoryManager manager = new GitMockRepositoryManager();
    manager.add(new MockGitRepository() {
      @NotNull
      @Override
      public Collection<GitBranchTrackInfo> getBranchTrackInfos() {
        List<GitBranchTrackInfo> infos = new ArrayList<GitBranchTrackInfo>();
        List<VcsRef> remoteRefs = ContainerUtil.findAll(refs, new Condition<VcsRef>() {
          @Override
          public boolean value(VcsRef ref) {
            return isRemoteBranch(ref.getName());
          }
        });
        List<VcsRef> localRefs = ContainerUtil.findAll(refs, new Condition<VcsRef>() {
          @Override
          public boolean value(VcsRef ref) {
            return isLocalBranch(ref.getName());
          }
        });

        for (final VcsRef localRef : localRefs) {
          final VcsRef trackedRef = ContainerUtil.find(remoteRefs, new Condition<VcsRef>() {
            @Override
            public boolean value(VcsRef remoteRef) {
              return localRef.getName().equals(remoteRef.getName().substring("origin/".length()));
            }
          });
          if (trackedRef != null) {
            infos.add(new GitBranchTrackInfo(new GitLocalBranch(localRef.getName(), HashImpl.build(randomHash())),
                                             new GitRemoteBranch(trackedRef.getName(), HashImpl.build(randomHash())) {
                                               @NotNull
                                               @Override
                                               public String getNameForRemoteOperations() {
                                                 return trackedRef.getName().substring("origin/".length());
                                               }

                                               @NotNull
                                               @Override
                                               public String getNameForLocalOperations() {
                                                 return trackedRef.getName();
                                               }

                                               @NotNull
                                               @Override
                                               public GitRemote getRemote() {
                                                 return GitRemote.DOT;
                                               }

                                               @Override
                                               public boolean isRemote() {
                                                 return true;
                                               }
                                             }, true));
          }
        }
        return infos;
      }
    });
    return ContainerUtil.sorted(refs, new GitRefManager(manager).getComparator());
  }

  // TODO either use the real GitRepository, or move upwards and make more generic implementation
  private static class MockGitRepository implements GitRepository {
    @NotNull
    @Override
    public VirtualFile getGitDir() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public GitUntrackedFilesHolder getUntrackedFilesHolder() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public GitRepoInfo getInfo() {
      throw new UnsupportedOperationException();
    }

    @Nullable
    @Override
    public GitLocalBranch getCurrentBranch() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public GitBranchesCollection getBranches() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public Collection<GitRemote> getRemotes() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public Collection<GitBranchTrackInfo> getBranchTrackInfos() {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean isRebaseInProgress() {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean isOnBranch() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public VirtualFile getRoot() {
      return MOCK_VIRTUAL_FILE;
    }

    @NotNull
    @Override
    public String getPresentableUrl() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public Project getProject() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public State getState() {
      throw new UnsupportedOperationException();
    }

    @Nullable
    @Override
    public String getCurrentBranchName() {
      throw new UnsupportedOperationException();
    }

    @Nullable
    @Override
    public AbstractVcs getVcs() {
      return null;
    }

    @Nullable
    @Override
    public String getCurrentRevision() {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean isFresh() {
      throw new UnsupportedOperationException();
    }

    @Override
    public void update() {
      throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public String toLogString() {
      throw new UnsupportedOperationException();
    }

    @Override
    public void dispose() {
    }
  }
}