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 package org.eclipse.jgit.lib;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertTrue;
51
52 import org.eclipse.jgit.errors.ConfigInvalidException;
53 import org.junit.Test;
54
55 public class BranchConfigTest {
56
57 @Test
58 public void getRemoteTrackingBranchShouldHandleNormalCase() {
59 Config c = parse(""
60 + "[remote \"origin\"]\n"
61 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
62 + "[branch \"master\"]\n"
63 + " remote = origin\n"
64 + " merge = refs/heads/master\n");
65
66 BranchConfig branchConfig = new BranchConfig(c, "master");
67 assertEquals("refs/remotes/origin/master",
68 branchConfig.getRemoteTrackingBranch());
69 }
70
71 @Test
72 public void getRemoteTrackingBranchShouldHandleOtherMapping() {
73 Config c = parse(""
74 + "[remote \"test\"]\n"
75 + " fetch = +refs/foo/*:refs/remotes/origin/foo/*\n"
76 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
77 + " fetch = +refs/other/*:refs/remotes/origin/other/*\n"
78 + "[branch \"master\"]\n"
79 + " remote = test\n"
80 + " merge = refs/foo/master\n" + "\n");
81
82 BranchConfig branchConfig = new BranchConfig(c, "master");
83 assertEquals("refs/remotes/origin/foo/master",
84 branchConfig.getRemoteTrackingBranch());
85 }
86
87 @Test
88 public void getRemoteTrackingBranchShouldReturnNullWithoutFetchSpec() {
89 Config c = parse(""
90 + "[remote \"origin\"]\n"
91 + " fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
92 + "[branch \"master\"]\n"
93 + " remote = origin\n"
94 + " merge = refs/heads/master\n");
95 BranchConfig branchConfig = new BranchConfig(c, "master");
96 assertNull(branchConfig.getRemoteTrackingBranch());
97 }
98
99 @Test
100 public void getRemoteTrackingBranchShouldReturnNullWithoutMergeBranch() {
101 Config c = parse(""
102 + "[remote \"origin\"]\n"
103 + " fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
104 + "[branch \"master\"]\n"
105 + " remote = origin\n");
106 BranchConfig branchConfig = new BranchConfig(c, "master");
107 assertNull(branchConfig.getRemoteTrackingBranch());
108 }
109
110 @Test
111 public void getTrackingBranchShouldReturnMergeBranchForLocalBranch() {
112 Config c = parse(""
113 + "[remote \"origin\"]\n"
114 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
115 + "[branch \"master\"]\n"
116 + " remote = .\n"
117 + " merge = refs/heads/master\n");
118
119 BranchConfig branchConfig = new BranchConfig(c, "master");
120 assertEquals("refs/heads/master",
121 branchConfig.getTrackingBranch());
122 }
123
124 @Test
125 public void getTrackingBranchShouldReturnNullWithoutMergeBranchForLocalBranch() {
126 Config c = parse(""
127 + "[remote \"origin\"]\n"
128 + " fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
129 + "[branch \"master\"]\n"
130 + " remote = .\n");
131 BranchConfig branchConfig = new BranchConfig(c, "master");
132 assertNull(branchConfig.getTrackingBranch());
133 }
134
135 @Test
136 public void getTrackingBranchShouldHandleNormalCaseForRemoteTrackingBranch() {
137 Config c = parse(""
138 + "[remote \"origin\"]\n"
139 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
140 + "[branch \"master\"]\n"
141 + " remote = origin\n"
142 + " merge = refs/heads/master\n");
143
144 BranchConfig branchConfig = new BranchConfig(c, "master");
145 assertEquals("refs/remotes/origin/master",
146 branchConfig.getTrackingBranch());
147 }
148
149 @Test
150 public void isRebase() {
151 Config c = parse(""
152 + "[branch \"undefined\"]\n"
153 + "[branch \"false\"]\n"
154 + " rebase = false\n"
155 + "[branch \"true\"]\n"
156 + " rebase = true\n");
157
158 assertFalse(new BranchConfig(c, "undefined").isRebase());
159 assertFalse(new BranchConfig(c, "false").isRebase());
160 assertTrue(new BranchConfig(c, "true").isRebase());
161 }
162
163 private static Config parse(final String content) {
164 final Config c = new Config(null);
165 try {
166 c.fromText(content);
167 } catch (ConfigInvalidException e) {
168 throw new RuntimeException(e);
169 }
170 return c;
171 }
172 }