1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.lib;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18
19 import org.eclipse.jgit.errors.ConfigInvalidException;
20 import org.junit.Test;
21
22 public class BranchConfigTest {
23
24 @Test
25 public void getRemoteTrackingBranchShouldHandleNormalCase() {
26 Config c = parse(""
27 + "[remote \"origin\"]\n"
28 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
29 + "[branch \"master\"]\n"
30 + " remote = origin\n"
31 + " merge = refs/heads/master\n");
32
33 BranchConfig branchConfig = new BranchConfig(c, "master");
34 assertEquals("refs/remotes/origin/master",
35 branchConfig.getRemoteTrackingBranch());
36 }
37
38 @Test
39 public void getRemoteTrackingBranchShouldHandleOtherMapping() {
40 Config c = parse(""
41 + "[remote \"test\"]\n"
42 + " fetch = +refs/foo/*:refs/remotes/origin/foo/*\n"
43 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
44 + " fetch = +refs/other/*:refs/remotes/origin/other/*\n"
45 + "[branch \"master\"]\n"
46 + " remote = test\n"
47 + " merge = refs/foo/master\n" + "\n");
48
49 BranchConfig branchConfig = new BranchConfig(c, "master");
50 assertEquals("refs/remotes/origin/foo/master",
51 branchConfig.getRemoteTrackingBranch());
52 }
53
54 @Test
55 public void getRemoteTrackingBranchShouldReturnNullWithoutFetchSpec() {
56 Config c = parse(""
57 + "[remote \"origin\"]\n"
58 + " fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
59 + "[branch \"master\"]\n"
60 + " remote = origin\n"
61 + " merge = refs/heads/master\n");
62 BranchConfig branchConfig = new BranchConfig(c, "master");
63 assertNull(branchConfig.getRemoteTrackingBranch());
64 }
65
66 @Test
67 public void getRemoteTrackingBranchShouldReturnNullWithoutMergeBranch() {
68 Config c = parse(""
69 + "[remote \"origin\"]\n"
70 + " fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
71 + "[branch \"master\"]\n"
72 + " remote = origin\n");
73 BranchConfig branchConfig = new BranchConfig(c, "master");
74 assertNull(branchConfig.getRemoteTrackingBranch());
75 }
76
77 @Test
78 public void getTrackingBranchShouldReturnMergeBranchForLocalBranch() {
79 Config c = parse(""
80 + "[remote \"origin\"]\n"
81 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
82 + "[branch \"master\"]\n"
83 + " remote = .\n"
84 + " merge = refs/heads/master\n");
85
86 BranchConfig branchConfig = new BranchConfig(c, "master");
87 assertEquals("refs/heads/master",
88 branchConfig.getTrackingBranch());
89 }
90
91 @Test
92 public void getTrackingBranchShouldReturnNullWithoutMergeBranchForLocalBranch() {
93 Config c = parse(""
94 + "[remote \"origin\"]\n"
95 + " fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
96 + "[branch \"master\"]\n"
97 + " remote = .\n");
98 BranchConfig branchConfig = new BranchConfig(c, "master");
99 assertNull(branchConfig.getTrackingBranch());
100 }
101
102 @Test
103 public void getTrackingBranchShouldHandleNormalCaseForRemoteTrackingBranch() {
104 Config c = parse(""
105 + "[remote \"origin\"]\n"
106 + " fetch = +refs/heads/*:refs/remotes/origin/*\n"
107 + "[branch \"master\"]\n"
108 + " remote = origin\n"
109 + " merge = refs/heads/master\n");
110
111 BranchConfig branchConfig = new BranchConfig(c, "master");
112 assertEquals("refs/remotes/origin/master",
113 branchConfig.getTrackingBranch());
114 }
115
116 @Test
117 public void isRebase() {
118 Config c = parse(""
119 + "[branch \"undefined\"]\n"
120 + "[branch \"false\"]\n"
121 + " rebase = false\n"
122 + "[branch \"true\"]\n"
123 + " rebase = true\n");
124
125 assertFalse(new BranchConfig(c, "undefined").isRebase());
126 assertFalse(new BranchConfig(c, "false").isRebase());
127 assertTrue(new BranchConfig(c, "true").isRebase());
128 }
129
130 private static Config parse(String content) {
131 final Config c = new Config(null);
132 try {
133 c.fromText(content);
134 } catch (ConfigInvalidException e) {
135 throw new RuntimeException(e);
136 }
137 return c;
138 }
139 }