1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.lib;
13
14 import java.net.URISyntaxException;
15
16 import org.eclipse.jgit.transport.RefSpec;
17 import org.eclipse.jgit.transport.RemoteConfig;
18
19
20
21
22 public class BranchConfig {
23
24
25
26
27
28
29 public enum BranchRebaseMode implements Config.ConfigEnum {
30
31
32 REBASE("true"),
33
34 PRESERVE("preserve"),
35
36 INTERACTIVE("interactive"),
37
38 NONE("false");
39
40 private final String configValue;
41
42 private BranchRebaseMode(String configValue) {
43 this.configValue = configValue;
44 }
45
46 @Override
47 public String toConfigValue() {
48 return configValue;
49 }
50
51 @Override
52 public boolean matchConfigValue(String s) {
53 return configValue.equals(s);
54 }
55 }
56
57
58
59
60
61
62
63 public static final String LOCAL_REPOSITORY = ".";
64
65 private final Config config;
66 private final String branchName;
67
68
69
70
71
72
73
74
75
76
77 public BranchConfig(Config config, String branchName) {
78 this.config = config;
79 this.branchName = branchName;
80 }
81
82
83
84
85
86
87
88 public String getTrackingBranch() {
89 String remote = getRemoteOrDefault();
90 String mergeRef = getMerge();
91 if (remote == null || mergeRef == null)
92 return null;
93
94 if (isRemoteLocal())
95 return mergeRef;
96
97 return findRemoteTrackingBranch(remote, mergeRef);
98 }
99
100
101
102
103
104
105
106
107 public String getRemoteTrackingBranch() {
108 String remote = getRemoteOrDefault();
109 String mergeRef = getMerge();
110 if (remote == null || mergeRef == null)
111 return null;
112
113 return findRemoteTrackingBranch(remote, mergeRef);
114 }
115
116
117
118
119
120
121
122
123
124 public boolean isRemoteLocal() {
125 return LOCAL_REPOSITORY.equals(getRemote());
126 }
127
128
129
130
131
132
133
134
135 public String getRemote() {
136 return config.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
137 branchName, ConfigConstants.CONFIG_KEY_REMOTE);
138 }
139
140
141
142
143
144
145
146
147 public String getMerge() {
148 return config.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
149 branchName, ConfigConstants.CONFIG_KEY_MERGE);
150 }
151
152
153
154
155
156
157
158 public boolean isRebase() {
159 return getRebaseMode() != BranchRebaseMode.NONE;
160 }
161
162
163
164
165
166
167
168 public BranchRebaseMode getRebaseMode() {
169 return config.getEnum(BranchRebaseMode.values(),
170 ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
171 ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.NONE);
172 }
173
174
175
176
177
178
179
180
181
182
183
184 private String findRemoteTrackingBranch(String remote, String mergeRef) {
185 RemoteConfig remoteConfig;
186 try {
187 remoteConfig = new RemoteConfig(config, remote);
188 } catch (URISyntaxException e) {
189 return null;
190 }
191 for (RefSpec refSpec : remoteConfig.getFetchRefSpecs()) {
192 if (refSpec.matchSource(mergeRef)) {
193 RefSpec expanded = refSpec.expandFromSource(mergeRef);
194 return expanded.getDestination();
195 }
196 }
197 return null;
198 }
199
200 private String getRemoteOrDefault() {
201 String remote = getRemote();
202 if (remote == null) {
203 return Constants.DEFAULT_REMOTE_NAME;
204 }
205 return remote;
206 }
207 }