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 package org.eclipse.jgit.lib;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNull;
48
49 import org.eclipse.jgit.junit.RepositoryTestCase;
50 import org.eclipse.jgit.junit.TestRepository;
51 import org.eclipse.jgit.revwalk.RevCommit;
52 import org.eclipse.jgit.revwalk.RevWalk;
53 import org.junit.Test;
54
55 public class BranchTrackingStatusTest extends RepositoryTestCase {
56 private TestRepository<Repository> util;
57
58 protected RevWalk rw;
59
60 @Override
61 public void setUp() throws Exception {
62 super.setUp();
63 util = new TestRepository<>(db);
64 StoredConfig config = util.getRepository().getConfig();
65 config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master",
66 ConfigConstants.CONFIG_KEY_REMOTE, "origin");
67 config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master",
68 ConfigConstants.CONFIG_KEY_MERGE, "refs/heads/master");
69 config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
70 "fetch", "+refs/heads/*:refs/remotes/origin/*");
71 }
72
73 @Test
74 public void shouldWorkInNormalCase() throws Exception {
75 RevCommit remoteTracking = util.branch("refs/remotes/origin/master")
76 .commit().create();
77 util.branch("master").commit().parent(remoteTracking).create();
78 util.branch("master").commit().create();
79
80 BranchTrackingStatus status = BranchTrackingStatus.of(
81 util.getRepository(), "master");
82 assertEquals(2, status.getAheadCount());
83 assertEquals(0, status.getBehindCount());
84 assertEquals("refs/remotes/origin/master",
85 status.getRemoteTrackingBranch());
86 }
87
88 @Test
89 public void shouldWorkWithoutMergeBase() throws Exception {
90 util.branch("refs/remotes/origin/master").commit().create();
91 util.branch("master").commit().create();
92
93 BranchTrackingStatus status = BranchTrackingStatus.of(util.getRepository(), "master");
94 assertEquals(1, status.getAheadCount());
95 assertEquals(1, status.getBehindCount());
96 }
97
98 @Test
99 public void shouldReturnNullWhenBranchDoesntExist() throws Exception {
100 BranchTrackingStatus status = BranchTrackingStatus.of(
101 util.getRepository(), "doesntexist");
102
103 assertNull(status);
104 }
105 }