Skip to content

Commit 1519c14

Browse files
lucamilanesiomsohn
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: I6b2649b06623a3b8226ee8413e4f1f58ad8ea28b
1 parent f9addab commit 1519c14

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ public class UploadPack implements Closeable {
117117
/** Policy the server uses to validate client requests */
118118
public enum RequestPolicy {
119119
/** Client may only ask for objects the server advertised a reference for. */
120-
ADVERTISED,
120+
ADVERTISED(0x08),
121121

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

128128
/**
129129
* Client may ask for objects that are the tip of any reference, even if not
@@ -133,18 +133,34 @@ public enum RequestPolicy {
133133
*
134134
* @since 3.1
135135
*/
136-
TIP,
136+
TIP(0x01),
137137

138138
/**
139139
* Client may ask for any commit reachable from any reference, even if that
140-
* reference wasn't advertised.
140+
* reference wasn't advertised, implies REACHABLE_COMMIT and TIP.
141141
*
142142
* @since 3.1
143143
*/
144-
REACHABLE_COMMIT_TIP,
144+
REACHABLE_COMMIT_TIP(0x03),
145145

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

150166
/**
@@ -1582,13 +1598,9 @@ public void sendAdvertisedRefs(RefAdvertiser adv,
15821598
if (!biDirectionalPipe)
15831599
adv.advertiseCapability(OPTION_NO_DONE);
15841600
RequestPolicy policy = getRequestPolicy();
1585-
if (policy == RequestPolicy.TIP
1586-
|| policy == RequestPolicy.REACHABLE_COMMIT_TIP
1587-
|| policy == null)
1601+
if (policy == null || policy.implies(RequestPolicy.TIP))
15881602
adv.advertiseCapability(OPTION_ALLOW_TIP_SHA1_IN_WANT);
1589-
if (policy == RequestPolicy.REACHABLE_COMMIT
1590-
|| policy == RequestPolicy.REACHABLE_COMMIT_TIP
1591-
|| policy == null)
1603+
if (policy == null || policy.implies(RequestPolicy.REACHABLE_COMMIT))
15921604
adv.advertiseCapability(OPTION_ALLOW_REACHABLE_SHA1_IN_WANT);
15931605
adv.advertiseCapability(OPTION_AGENT, UserAgent.get());
15941606
if (transferConfig.isAllowFilter()) {

0 commit comments

Comments
 (0)