Skip to content

Commit 5b1513a

Browse files
pszlazakmsohn
authored andcommitted
Align request policies with CGit
CGit defines the SHA request policies using a bitmask that represents which policy is implied by another policy. For example, in CGit the ALLOW_TIP_SHA1 is 0x01 and ALLOW_REACHABLE_SHA1 is 0x02, which are associated to two different bit in a 3-bit value. The ALLOW_ANY_SHA1 value is 0x07 which denotes a different policy that implies the previous two ones, because is represented with a 3-bit bitmask having all ones. Associate the JGit RequestPolicy enum to the same CGit bitmask values and use the same logic for the purpose of advertising the server capabilities. The JGit code becomes easier to read and associate with its counterpart in CGit, especially during the capabilities advertising phase. Also add a new utility method RequestPolicy.implies() which is more readable than a direct bitmask and operator. Bug: jgit-68 Change-Id: I932150dca1211ba9c8c34a523f13e84d7390063b (cherry picked from commit 1519c14)
1 parent c824610 commit 5b1513a

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

+25-13
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ public class UploadPack implements Closeable {
118118
/** Policy the server uses to validate client requests */
119119
public enum RequestPolicy {
120120
/** Client may only ask for objects the server advertised a reference for. */
121-
ADVERTISED,
121+
ADVERTISED(0x08),
122122

123123
/**
124124
* Client may ask for any commit reachable from a reference advertised by
125125
* the server.
126126
*/
127-
REACHABLE_COMMIT,
127+
REACHABLE_COMMIT(0x02),
128128

129129
/**
130130
* Client may ask for objects that are the tip of any reference, even if not
@@ -134,18 +134,34 @@ public enum RequestPolicy {
134134
*
135135
* @since 3.1
136136
*/
137-
TIP,
137+
TIP(0x01),
138138

139139
/**
140140
* Client may ask for any commit reachable from any reference, even if that
141-
* reference wasn't advertised.
141+
* reference wasn't advertised, implies REACHABLE_COMMIT and TIP.
142142
*
143143
* @since 3.1
144144
*/
145-
REACHABLE_COMMIT_TIP,
145+
REACHABLE_COMMIT_TIP(0x03),
146146

147-
/** Client may ask for any SHA-1 in the repository. */
148-
ANY;
147+
/** Client may ask for any SHA-1 in the repository, implies REACHABLE_COMMIT_TIP. */
148+
ANY(0x07);
149+
150+
private final int bitmask;
151+
152+
RequestPolicy(int bitmask) {
153+
this.bitmask = bitmask;
154+
}
155+
156+
/**
157+
* Check if the current policy implies another, based on its bitmask.
158+
*
159+
* @param implied the implied policy based on its bitmask.
160+
* @return true if the policy is implied.
161+
*/
162+
public boolean implies(RequestPolicy implied) {
163+
return (bitmask & implied.bitmask) != 0;
164+
}
149165
}
150166

151167
/**
@@ -1629,13 +1645,9 @@ public void sendAdvertisedRefs(RefAdvertiser adv,
16291645
if (!biDirectionalPipe)
16301646
adv.advertiseCapability(OPTION_NO_DONE);
16311647
RequestPolicy policy = getRequestPolicy();
1632-
if (policy == RequestPolicy.TIP
1633-
|| policy == RequestPolicy.REACHABLE_COMMIT_TIP
1634-
|| policy == null)
1648+
if (policy == null || policy.implies(RequestPolicy.TIP))
16351649
adv.advertiseCapability(OPTION_ALLOW_TIP_SHA1_IN_WANT);
1636-
if (policy == RequestPolicy.REACHABLE_COMMIT
1637-
|| policy == RequestPolicy.REACHABLE_COMMIT_TIP
1638-
|| policy == null)
1650+
if (policy == null || policy.implies(RequestPolicy.REACHABLE_COMMIT))
16391651
adv.advertiseCapability(OPTION_ALLOW_REACHABLE_SHA1_IN_WANT);
16401652
adv.advertiseCapability(OPTION_AGENT, UserAgent.get());
16411653
if (transferConfig.isAllowFilter()) {

0 commit comments

Comments
 (0)