Class BatchRefUpdate

  • Direct Known Subclasses:
    ReftableBatchRefUpdate

    public class BatchRefUpdate
    extends Object
    Batch of reference updates to be applied to a repository.

    The batch update is primarily useful in the transport code, where a client or server is making changes to more than one reference at a time.

    • Field Detail

      • MAX_WAIT

        protected static final Duration MAX_WAIT
        Maximum delay the calling thread will tolerate while waiting for a MonotonicClock to resolve associated ProposedTimestamps.

        A default of 5 seconds was chosen by guessing. A common assumption is clock skew between machines on the same LAN using an NTP server also on the same LAN should be under 5 seconds. 5 seconds is also not that long for a large `git push` operation to complete.

        Since:
        4.9
    • Constructor Detail

      • BatchRefUpdate

        protected BatchRefUpdate​(RefDatabase refdb)
        Initialize a new batch update.
        Parameters:
        refdb - the reference database of the repository to be updated.
    • Method Detail

      • isAllowNonFastForwards

        public boolean isAllowNonFastForwards()
        Whether the batch update will permit a non-fast-forward update to an existing reference.
        Returns:
        true if the batch update will permit a non-fast-forward update to an existing reference.
      • setAllowNonFastForwards

        public BatchRefUpdate setAllowNonFastForwards​(boolean allow)
        Set if this update wants to permit a forced update.
        Parameters:
        allow - true if this update batch should ignore merge tests.
        Returns:
        this.
      • getRefLogIdent

        public PersonIdent getRefLogIdent()
        Get identity of the user making the change in the reflog.
        Returns:
        identity of the user making the change in the reflog.
      • setRefLogIdent

        public BatchRefUpdate setRefLogIdent​(PersonIdent pi)
        Set the identity of the user appearing in the reflog.

        The timestamp portion of the identity is ignored. A new identity with the current timestamp will be created automatically when the update occurs and the log record is written.

        Parameters:
        pi - identity of the user. If null the identity will be automatically determined based on the repository configuration.
        Returns:
        this.
      • getRefLogMessage

        @Nullable
        public String getRefLogMessage()
        Get the message to include in the reflog.
        Returns:
        message the caller wants to include in the reflog; null if the update should not be logged.
      • isRefLogIncludingResult

        public boolean isRefLogIncludingResult()
        Check whether the reflog message should include the result of the update, such as fast-forward or force-update.

        Describes the default for commands in this batch that do not override it with ReceiveCommand.setRefLogMessage(String, boolean).

        Returns:
        true if the message should include the result.
      • setRefLogMessage

        public BatchRefUpdate setRefLogMessage​(String msg,
                                               boolean appendStatus)
        Set the message to include in the reflog.

        Repository implementations may limit which reflogs are written by default, based on the project configuration. If a repo is not configured to write logs for this ref by default, setting the message alone may have no effect. To indicate that the repo should write logs for this update in spite of configured defaults, use setForceRefLog(boolean).

        Describes the default for commands in this batch that do not override it with ReceiveCommand.setRefLogMessage(String, boolean).

        Parameters:
        msg - the message to describe this change. If null and appendStatus is false, the reflog will not be updated.
        appendStatus - true if the status of the ref change (fast-forward or forced-update) should be appended to the user supplied message.
        Returns:
        this.
      • disableRefLog

        public BatchRefUpdate disableRefLog()
        Don't record this update in the ref's associated reflog.

        Equivalent to setRefLogMessage(null, false).

        Returns:
        this.
      • setForceRefLog

        public BatchRefUpdate setForceRefLog​(boolean force)
        Force writing a reflog for the updated ref.
        Parameters:
        force - whether to force.
        Returns:
        this
        Since:
        4.9
      • isRefLogDisabled

        public boolean isRefLogDisabled()
        Check whether log has been disabled by disableRefLog().
        Returns:
        true if disabled.
      • isForceRefLog

        protected boolean isForceRefLog()
        Check whether the reflog should be written regardless of repo defaults.
        Returns:
        whether force writing is enabled.
        Since:
        4.9
      • setAtomic

        public BatchRefUpdate setAtomic​(boolean atomic)
        Request that all updates in this batch be performed atomically.

        When atomic updates are used, either all commands apply successfully, or none do. Commands that might have otherwise succeeded are rejected with REJECTED_OTHER_REASON.

        This method only works if the underlying ref database supports atomic transactions, i.e. RefDatabase.performsAtomicTransactions() returns true. Calling this method with true if the underlying ref database does not support atomic transactions will cause all commands to fail with REJECTED_OTHER_REASON.

        Parameters:
        atomic - whether updates should be atomic.
        Returns:
        this
        Since:
        4.4
      • isAtomic

        public boolean isAtomic()
        Whether updates should be atomic.
        Returns:
        atomic whether updates should be atomic.
        Since:
        4.4
      • setPushCertificate

        public void setPushCertificate​(PushCertificate cert)
        Set a push certificate associated with this update.

        This usually includes commands to update the refs in this batch, but is not required to.

        Parameters:
        cert - push certificate, may be null.
        Since:
        4.1
      • getPushCertificate

        protected PushCertificate getPushCertificate()
        Set the push certificate associated with this update.

        This usually includes commands to update the refs in this batch, but is not required to.

        Returns:
        push certificate, may be null.
        Since:
        4.1
      • getCommands

        public List<ReceiveCommand> getCommands()
        Get commands this update will process.
        Returns:
        commands this update will process.
      • addCommand

        public BatchRefUpdate addCommand​(ReceiveCommand cmd)
        Add a single command to this batch update.
        Parameters:
        cmd - the command to add, must not be null.
        Returns:
        this.
      • addCommand

        public BatchRefUpdate addCommand​(ReceiveCommand... cmd)
        Add commands to this batch update.
        Parameters:
        cmd - the commands to add, must not be null.
        Returns:
        this.
      • addCommand

        public BatchRefUpdate addCommand​(Collection<ReceiveCommand> cmd)
        Add commands to this batch update.
        Parameters:
        cmd - the commands to add, must not be null.
        Returns:
        this.
      • setPushOptions

        protected void setPushOptions​(List<String> options)
        Set push options associated with this update.

        Implementations must call this at the top of execute(RevWalk, ProgressMonitor, List).

        Parameters:
        options - options passed to execute.
        Since:
        4.9
      • getProposedTimestamps

        public List<ProposedTimestamp> getProposedTimestamps()
        Get list of timestamps the batch must wait for.
        Returns:
        list of timestamps the batch must wait for.
        Since:
        4.6
      • execute

        public void execute​(RevWalk walk,
                            ProgressMonitor monitor,
                            List<String> options)
                     throws IOException
        Execute this batch update.

        The default implementation of this method performs a sequential reference update over each reference.

        Implementations must respect the atomicity requirements of the underlying database as described in setAtomic(boolean) and RefDatabase.performsAtomicTransactions().

        Parameters:
        walk - a RevWalk to parse tags in case the storage system wants to store them pre-peeled, a common performance optimization.
        monitor - progress monitor to receive update status on.
        options - a list of option strings; set null to execute without
        Throws:
        IOException - the database is unable to accept the update. Individual command status must be tested to determine if there is a partial failure, or a total failure.
        Since:
        4.5
      • blockUntilTimestamps

        protected boolean blockUntilTimestamps​(Duration maxWait)
        Wait for timestamps to be in the past, aborting commands on timeout.
        Parameters:
        maxWait - maximum amount of time to wait for timestamps to resolve.
        Returns:
        true if timestamps were successfully waited for; false if commands were aborted.
        Since:
        4.6
      • execute

        public void execute​(RevWalk walk,
                            ProgressMonitor monitor)
                     throws IOException
        Execute this batch update without option strings.
        Parameters:
        walk - a RevWalk to parse tags in case the storage system wants to store them pre-peeled, a common performance optimization.
        monitor - progress monitor to receive update status on.
        Throws:
        IOException - the database is unable to accept the update. Individual command status must be tested to determine if there is a partial failure, or a total failure.
      • getPrefixes

        protected static Collection<String> getPrefixes​(String name)
        Get all path prefixes of a ref name.
        Parameters:
        name - ref name.
        Returns:
        path prefixes of the ref name. For refs/heads/foo, returns refs and refs/heads.
        Since:
        4.9
      • addPrefixesTo

        protected static void addPrefixesTo​(String name,
                                            Collection<String> out)
        Add prefixes of a ref name to an existing collection.
        Parameters:
        name - ref name.
        out - path prefixes of the ref name. For refs/heads/foo, returns refs and refs/heads.
        Since:
        4.9
      • newUpdate

        protected RefUpdate newUpdate​(ReceiveCommand cmd)
                               throws IOException
        Create a new RefUpdate copying the batch settings.
        Parameters:
        cmd - specific command the update should be created to copy.
        Returns:
        a single reference update command.
        Throws:
        IOException - the reference database cannot make a new update object for the given reference.
      • isRefLogDisabled

        protected boolean isRefLogDisabled​(ReceiveCommand cmd)
        Check whether reflog is disabled for a command.
        Parameters:
        cmd - specific command.
        Returns:
        whether the reflog is disabled, taking into account the state from this instance as well as overrides in the given command.
        Since:
        4.9
      • getRefLogMessage

        protected String getRefLogMessage​(ReceiveCommand cmd)
        Get reflog message for a command.
        Parameters:
        cmd - specific command.
        Returns:
        reflog message, taking into account the state from this instance as well as overrides in the given command.
        Since:
        4.9
      • isRefLogIncludingResult

        protected boolean isRefLogIncludingResult​(ReceiveCommand cmd)
        Check whether the reflog message for a command should include the result.
        Parameters:
        cmd - specific command.
        Returns:
        whether the reflog message should show the result, taking into account the state from this instance as well as overrides in the given command.
        Since:
        4.9
      • isForceRefLog

        protected boolean isForceRefLog​(ReceiveCommand cmd)
        Check whether the reflog for a command should be written regardless of repo defaults.
        Parameters:
        cmd - specific command.
        Returns:
        whether force writing is enabled.
        Since:
        4.9