Skip to content

Commit

Permalink
Align request policies with CGit
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
pszlazak authored and msohn committed Nov 21, 2024
1 parent c824610 commit 5b1513a
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public class UploadPack implements Closeable {
/** Policy the server uses to validate client requests */
public enum RequestPolicy {
/** Client may only ask for objects the server advertised a reference for. */
ADVERTISED,
ADVERTISED(0x08),

/**
* Client may ask for any commit reachable from a reference advertised by
* the server.
*/
REACHABLE_COMMIT,
REACHABLE_COMMIT(0x02),

/**
* Client may ask for objects that are the tip of any reference, even if not
Expand All @@ -134,18 +134,34 @@ public enum RequestPolicy {
*
* @since 3.1
*/
TIP,
TIP(0x01),

/**
* Client may ask for any commit reachable from any reference, even if that
* reference wasn't advertised.
* reference wasn't advertised, implies REACHABLE_COMMIT and TIP.
*
* @since 3.1
*/
REACHABLE_COMMIT_TIP,
REACHABLE_COMMIT_TIP(0x03),

/** Client may ask for any SHA-1 in the repository. */
ANY;
/** Client may ask for any SHA-1 in the repository, implies REACHABLE_COMMIT_TIP. */
ANY(0x07);

private final int bitmask;

RequestPolicy(int bitmask) {
this.bitmask = bitmask;
}

/**
* Check if the current policy implies another, based on its bitmask.
*
* @param implied the implied policy based on its bitmask.
* @return true if the policy is implied.
*/
public boolean implies(RequestPolicy implied) {
return (bitmask & implied.bitmask) != 0;
}
}

/**
Expand Down Expand Up @@ -1629,13 +1645,9 @@ public void sendAdvertisedRefs(RefAdvertiser adv,
if (!biDirectionalPipe)
adv.advertiseCapability(OPTION_NO_DONE);
RequestPolicy policy = getRequestPolicy();
if (policy == RequestPolicy.TIP
|| policy == RequestPolicy.REACHABLE_COMMIT_TIP
|| policy == null)
if (policy == null || policy.implies(RequestPolicy.TIP))
adv.advertiseCapability(OPTION_ALLOW_TIP_SHA1_IN_WANT);
if (policy == RequestPolicy.REACHABLE_COMMIT
|| policy == RequestPolicy.REACHABLE_COMMIT_TIP
|| policy == null)
if (policy == null || policy.implies(RequestPolicy.REACHABLE_COMMIT))
adv.advertiseCapability(OPTION_ALLOW_REACHABLE_SHA1_IN_WANT);
adv.advertiseCapability(OPTION_AGENT, UserAgent.get());
if (transferConfig.isAllowFilter()) {
Expand Down

0 comments on commit 5b1513a

Please sign in to comment.