• Get Review Board
  • What's New
  • Products
  • Review Board Code review, image review, and document review
  • Documentation
  • Release Notes
  • Power Pack Enterprise integrations, reports, and enhanced document review
  • Try for 60 Days
  • Purchase
  • RBCommons Review Board as a Service, hosted by us
  • Pricing
  • RBTools Command line tools and Python API for Review Board
  • Documentation
  • Release Notes
  • Review Bot Automated code review, connecting tools you already use
  • Documentation
  • Release Notes
  • RB Gateway Manage Git and Mercurial repositories in your network
  • Documentation
  • Release Notes
  • Learn and Explore
  • What is Code Review?
  • Documentation
  • Frequently Asked Questions
  • Support Options
  • Third-Party Integrations
  • Demo
  • Review Board RBTools Power Pack Review Bot Djblets RB Gateway
    1. RBTools 5.x
    2. Version 5.x
    3. Version 4.x
    4. Version 3.x
    5. Version 2.0
    6. Version 1.0
    7. Version 0.7
    8. Version 0.6
    9. Version 0.5
    10. RBTools Python API
    11. Module and Class References
    12. rbtools.diffs.patcher
  • Home
  • Installing RBTools
  • rbt Command
  • Configuration
  • Authenticating to Review Board
  • Repository Configuration
  • Per-User Configuration
  • Team Foundation Server Configuration
  • Commands
  • alias
  • api-get
  • attach
  • clear-cache
  • close
  • diff
  • install
  • land
  • list-repo-types
  • login
  • logout
  • patch
  • post
  • publish
  • review
  • setup-completion
  • setup-repo
  • stamp
  • status
  • status-update
  • RBTools Workflows
  • Using RBTools With Cliosoft SOS
  • Using RBTools with Git
  • Using RBTools with HCL VersionVault and IBM ClearCase
  • Using RBTools With Perforce
  • RBTools Python API
  • Overview of the Python API Client
  • Tutorial
  • Resource-Specific Functionality
  • Module and Class References
  • rbtools
  • rbtools.deprecation
  • rbtools.api
  • rbtools.api.cache
  • rbtools.api.capabilities
  • rbtools.api.client
  • rbtools.api.decode
  • rbtools.api.decorators
  • rbtools.api.errors
  • rbtools.api.factory
  • rbtools.api.request
  • rbtools.api.resource
  • rbtools.api.transport
  • rbtools.api.transport.sync
  • rbtools.api.utils
  • rbtools.clients
  • rbtools.clients.base
  • rbtools.clients.base.patch
  • rbtools.clients.base.registry
  • rbtools.clients.base.repository
  • rbtools.clients.base.scmclient
  • rbtools.clients.errors
  • rbtools.clients.bazaar
  • rbtools.clients.clearcase
  • rbtools.clients.cvs
  • rbtools.clients.git
  • rbtools.clients.mercurial
  • rbtools.clients.perforce
  • rbtools.clients.plastic
  • rbtools.clients.sos
  • rbtools.clients.svn
  • rbtools.clients.tfs
  • rbtools.config
  • rbtools.config.config
  • rbtools.config.loader
  • rbtools.diffs
  • rbtools.diffs.patches
  • rbtools.diffs.patcher
  • rbtools.diffs.tools
  • rbtools.diffs.tools.backends
  • rbtools.diffs.tools.backends.gnu
  • rbtools.diffs.tools.base
  • rbtools.diffs.tools.base.diff_file_result
  • rbtools.diffs.tools.base.diff_tool
  • rbtools.diffs.tools.errors
  • rbtools.diffs.tools.registry
  • rbtools.diffs.writers
  • rbtools.commands
  • rbtools.commands.main
  • rbtools.commands
  • rbtools.commands.base
  • rbtools.commands.base.commands
  • rbtools.commands.base.errors
  • rbtools.commands.base.options
  • rbtools.commands.base.output
  • rbtools.commands.alias
  • rbtools.commands.api_get
  • rbtools.commands.attach
  • rbtools.commands.clearcache
  • rbtools.commands.close
  • rbtools.commands.diff
  • rbtools.commands.info
  • rbtools.commands.install
  • rbtools.commands.land
  • rbtools.commands.list_repo_types
  • rbtools.commands.login
  • rbtools.commands.logout
  • rbtools.commands.patch
  • rbtools.commands.post
  • rbtools.commands.publish
  • rbtools.commands.review
  • rbtools.commands.setup_completion
  • rbtools.commands.setup_repo
  • rbtools.commands.stamp
  • rbtools.commands.status
  • rbtools.commands.status_update
  • rbtools.hooks
  • rbtools.hooks.common
  • rbtools.hooks.git
  • rbtools.testing
  • rbtools.testing.api
  • rbtools.testing.api.payloads
  • rbtools.testing.api.transport
  • rbtools.testing.commands
  • rbtools.testing.testcase
  • rbtools.testing.transport
  • rbtools.utils
  • rbtools.utils.aliases
  • rbtools.utils.browser
  • rbtools.utils.checks
  • rbtools.utils.commands
  • rbtools.utils.console
  • rbtools.utils.diffs
  • rbtools.utils.encoding
  • rbtools.utils.errors
  • rbtools.utils.filesystem
  • rbtools.utils.graphs
  • rbtools.utils.mimetypes
  • rbtools.utils.process
  • rbtools.utils.repository
  • rbtools.utils.review_request
  • rbtools.utils.source_tree
  • rbtools.utils.users
  • Glossary
  • General Index
  • Python Module Index
  • Release Notes
  • rbtools.diffs.patcher¶

    Support for applying patches to a local source tree.

    New in version 5.1.

    Classes

    ParsedPatchOutput

    Parsed output from a patch command.

    Patcher(*, patches[, dest_path, ...])

    Applies patches and optionally commits to source trees.

    PatcherKwargs

    Keyword arguments available when instantiating a Patcher.

    class rbtools.diffs.patcher.ParsedPatchOutput[source]¶

    Bases: TypedDict

    Parsed output from a patch command.

    This is designed to collect information from parsing an unstructured patch output stream. The default Patcher supports pulling information from GNU Patch and Apple Patch commands, though this data can be returned from any patch tool.

    New in version 5.1.

    conflicting_files: list[str]¶

    A list of conflicting filenames.

    has_empty_files: bool¶

    Whether the patch indicates any empty files were potentially found.

    has_partial_applied_files: bool¶

    Whether the patch may have partially-applied files.

    patched_files: list[str]¶

    A list of filenames for files patched or attempted to be patched.

    This may include files that had conflicts and coud not be fully patched.

    fatal_error: NotRequired[Optional[str]]¶

    Any fatal error text present in the stream.

    __closed__ = False¶
    __extra_items__ = None¶
    __mutable_keys__ = frozenset({'conflicting_files', 'fatal_error', 'has_empty_files', 'has_partial_applied_files', 'patched_files'})¶
    __optional_keys__ = frozenset({})¶
    __orig_bases__ = (<function TypedDict>,)¶
    __readonly_keys__ = frozenset({})¶
    __required_keys__ = frozenset({'conflicting_files', 'fatal_error', 'has_empty_files', 'has_partial_applied_files', 'patched_files'})¶
    __total__ = True¶
    class rbtools.diffs.patcher.PatcherKwargs[source]¶

    Bases: TypedDict

    Keyword arguments available when instantiating a Patcher.

    New in version 5.1.

    patches: Sequence[Patch]¶

    The list of patches to apply (or revert), in order.

    dest_path: NotRequired[Optional[Path]]¶

    The local path where patches will be applied.

    If not provided, this will patch in the current directory by default.

    repository_info: NotRequired[Optional[RepositoryInfo]]¶

    Information on the current source code repository.

    revert: NotRequired[bool]¶

    Whether to revert the patches.

    squash: NotRequired[bool]¶

    Whether to squash the patches together into one patch.

    __closed__ = False¶
    __extra_items__ = None¶
    __mutable_keys__ = frozenset({'dest_path', 'patches', 'repository_info', 'revert', 'squash'})¶
    __optional_keys__ = frozenset({})¶
    __orig_bases__ = (<function TypedDict>,)¶
    __readonly_keys__ = frozenset({})¶
    __required_keys__ = frozenset({'dest_path', 'patches', 'repository_info', 'revert', 'squash'})¶
    __total__ = True¶
    class rbtools.diffs.patcher.Patcher(*, patches: Sequence[Patch], dest_path: Optional[Path] = None, repository_info: Optional[RepositoryInfo] = None, revert: bool = False, squash: bool = False)[source]¶

    Bases: object

    Applies patches and optionally commits to source trees.

    This takes a list of patches and criteria for applying those patches and attempts to apply them to the source tree. After construction, consumers can call patch() to apply the patches.

    By default, this will apply the patches one-by-one using the patch command. Subclasses may override the patching behavior to use native SCM patching capabilities, or to apply multiple patches in one go.

    Depending on the implementation, this will also allow callers to commit those patches to the repository. They must first check for can_commit and then call prepare_for_commit() prior to beginning the patch() operation.

    New in version 5.1.

    can_commit: bool = False¶

    Whether this patcher is capable of committing to a repository.

    This can be enabled by SCM-specific patchers to enable committing after applying patches.

    can_patch_empty_files: bool = False¶

    Whether empty files can be patched.

    By default, empty files cannot be patched. Subclasses must set this to True if they are capable of applying empty patches.

    __init__(*, patches: Sequence[Patch], dest_path: Optional[Path] = None, repository_info: Optional[RepositoryInfo] = None, revert: bool = False, squash: bool = False) → None[source]¶

    Initialize the patcher.

    Parameters:
    • patches (list of rbtools.diffs.patches.Patch) – The list of patches to apply (or revert), in order.

    • dest_path (pathlib.Path, optional) –

      The local path where patches will be applied.

      If not provided, this will patch in the current directory by default.

    • repository (rbtools.clients.base.repository.RepositoryInfo) –

      Information on the current source code repository.

      This may be needed in order to apply patches correctly to some kinds of repositories.

    • revert (bool, optional) – Whether to revert the patches.

    • squash (bool, optional) – Whether to squash the patches together into one patch.

    dest_path: Path¶

    The local path where patches will be applied.

    patches: Sequence[Patch]¶

    The patches to apply (or revert), in order.

    repository_info: Optional[RepositoryInfo]¶

    Information on the current source code repository.

    This may be needed in order to apply patches correctly to some kinds of repositories.

    revert: bool¶

    Whether the patches will be reverted.

    squash: bool¶

    Whether the patches will be squashed together into one patch.

    applied_patch_results: list[PatchResult]¶

    The list of any results from applied patches.

    commit: bool¶

    Whether patches will be committed after applying.

    run_commit_editor: bool¶

    Whether the user’s editor will be opened as part of making a commit.

    prepare_for_commit(*, default_author: Optional[PatchAuthor] = None, default_message: Optional[str] = None, review_request: Optional[ReviewRequestResource] = None, run_commit_editor: bool = False) → None[source]¶

    Prepare the patching process to commit applied changes.

    Once called, the patcher will be responsible for taking any applied changes and turning it into one or more commits.

    This only works if can_commit is True.

    The caller is responsible for providing either a default author and commit message, or a review request containing defaults to use. These will be used if the patch doesn’t contain that information already.

    Parameters:
    • default_author (rbtools.diffs.patches.PatchAuthor, optional) – The default author to use for commits.

    • default_message (str, optional) – The default message to use for commits.

    • review_request (rbtools.api.resource.ReviewRequestResource, optional) – The review request to use for a default author and message.

    • run_commit_editor (bool, optional) – Whether to run the user’s editor to edit a commit message before making the commit.

    patch() → Iterator[PatchResult][source]¶

    Applies the patches to the tree.

    This is the primary method used to apply the patches. It will handle applying the patches, returning results, and raising exceptions on errors.

    This method may only be called once.

    Yields:

    rbtools.diffs.patches.PatchResult – The result of each patch application, whether the patch applied successfully or with normal patch failures.

    Raises:

    rbtools.diffs.errors.ApplyPatchResult – There was an error attempting to apply a patch. This won’t be raised simply for conflicts or normal patch failures. It may be raised for errors encountered during the patching process.

    apply_patches() → Iterator[PatchResult][source]¶

    Internal function to apply the patches.

    This will handle applying all patches provided to the patcher.

    Subclasses should override this if providing batch-patching logic. They would also be responsible for handling commits, if requested by the caller and supported by the subclass.

    This must only be called internally by the patcher.

    Yields:

    rbtools.diffs.patches.PatchResult – The result of each patch application, whether the patch applied successfully or with normal patch failures.

    Raises:

    rbtools.diffs.errors.ApplyPatchResult – There was an error attempting to apply a patch. This won’t be raised simply for conflicts or normal patch failures. It may be raised for errors encountered during the patching process.

    apply_single_patch(*, patch: Patch, patch_num: int) → PatchResult[source]¶

    Internal function to apply a single patch.

    This will take a single patch and apply it. It may be applied to files that already contain other modifications or have had other patches applied to it.

    Subclasses that can apply patches one-by-one may override this to apply patches using SCM-specific methods.

    This must only be called internally by the patcher.

    Parameters:
    • patch (rbtools.diffs.patches.Patch) – The patch to apply, opened for reading.

    • patch_num (int) – The 1-based index of this patch in the full list of patches.

    Returns:

    The result of the patch application, whether the patch applied successfully or with normal patch failures.

    Return type:

    rbtools.diffs.patches.PatchResult

    Raises:

    rbtools.diffs.errors.ApplyPatchResult – There was an error attempting to apply the patch. This won’t be raised simply for conflicts or normal patch failures. It may be raised for errors encountered during the patching process.

    get_default_prefix_level(*, patch: Patch) → Optional[int][source]¶

    Return the default path prefix strip level for a patch.

    This function determines how much of a path to strip by default, if an explicit value isn’t given.

    Subclasses can override this to provide a different default.

    Parameters:

    patch (rbtools.diffs.patches.Patch) – The path to generate a default prefix strip level for.

    Returns:

    The prefix strip level, or None if a clear one could not be determined.

    Return type:

    int

    parse_patch_output(patch_output: bytes) → ParsedPatchOutput[source]¶

    Parse the patch command’s output for useful information.

    This will parse the standard output from a supported patch command to return information that can be used to identify patched files, partially-applied files, conflicting files, and error messages.

    It’s only used if the patcher uses the default patch application logic or if it’s called explicitly.

    Parameters:

    patch_output (bytes) – The patch output to parse.

    Returns:

    The parsed data found from the patch output.

    Return type:

    ParsedPatchOutput

    create_commit(*, patch_result: PatchResult, run_commit_editor: bool) → None[source]¶

    Internal method to create a commit based on a patch result.

    Subclasses must implement this and set can_commit if it supports creating commits based on changes in the working tree.

    This must only be called internally by the patcher.

    Parameters:
    • patch_result (rbtools.diffs.patches.PatchResult) – The patch result containing the patch/patches to commit.

    • run_commit_editor (bool) – Whether to run the configured commit editor to alter the commit message.

    Raises:

    rbtools.diffs.errors.ApplyPatchResult – There was an error attempting to commit the patch.

    apply_patch_for_empty_files(patch: Patch) → bool[source]¶

    Apply an empty file patch to a file.

    Normally, a patching tool can’t apply an empty patch to a file (which may be done to create a file, delete a file, or change metadata for a file). Subclasses can override this and enable can_patch_empty_files to opt into special logic for applying empty-file patches.

    The logic for applying the patch is entirely up to the subclass, and is not required for a patcher.

    Parameters:

    patch (rbtools.diffs.patches.Patch) – The opened patch to check and possibly apply.

    Returns:

    True if there are empty files in the patch that were applied. False if there were no empty files or the files could not be applied (which will lead to an error).

    Return type:

    bool

    Raises:

    rbtools.diffs.errors.ApplyPatchError – There was an error while applying the patch.

    Keep up with the latest Review Board releases, security updates, and helpful information.

    About
    News
    Demo
    RBCommons Hosting
    Integrations
    Happy Users
    Support Options
    Documentation
    FAQ
    User Manual
    RBTools
    Administration Guide
    Power Pack
    Release Notes
    Downloads
    Review Board
    RBTools
    Djblets
    Power Pack
    Package Store
    PGP Signatures
    Contributing
    Bug Tracker
    Submit Patches
    Development Setup
    Wiki
    Follow Us
    Mailing Lists
    Reddit
    Twitter
    Mastodon
    Facebook
    YouTube

    Copyright © 2006-2025 Beanbag, Inc. All rights reserved.

    Terms of Service — Privacy Policy — AI Ethics Policy — Branding

    On this page

    • [Top]
    • ParsedPatchOutput
      • ParsedPatchOutput.conflicting_files
      • ParsedPatchOutput.has_empty_files
      • ParsedPatchOutput.has_partial_applied_files
      • ParsedPatchOutput.patched_files
      • ParsedPatchOutput.fatal_error
      • ParsedPatchOutput.__closed__
      • ParsedPatchOutput.__extra_items__
      • ParsedPatchOutput.__mutable_keys__
      • ParsedPatchOutput.__optional_keys__
      • ParsedPatchOutput.__orig_bases__
      • ParsedPatchOutput.__readonly_keys__
      • ParsedPatchOutput.__required_keys__
      • ParsedPatchOutput.__total__
    • PatcherKwargs
      • PatcherKwargs.patches
      • PatcherKwargs.dest_path
      • PatcherKwargs.repository_info
      • PatcherKwargs.revert
      • PatcherKwargs.squash
      • PatcherKwargs.__closed__
      • PatcherKwargs.__extra_items__
      • PatcherKwargs.__mutable_keys__
      • PatcherKwargs.__optional_keys__
      • PatcherKwargs.__orig_bases__
      • PatcherKwargs.__readonly_keys__
      • PatcherKwargs.__required_keys__
      • PatcherKwargs.__total__
    • Patcher
      • Patcher.can_commit
      • Patcher.can_patch_empty_files
      • Patcher.__init__()
      • Patcher.dest_path
      • Patcher.patches
      • Patcher.repository_info
      • Patcher.revert
      • Patcher.squash
      • Patcher.applied_patch_results
      • Patcher.commit
      • Patcher.run_commit_editor
      • Patcher.prepare_for_commit()
      • Patcher.patch()
      • Patcher.apply_patches()
      • Patcher.apply_single_patch()
      • Patcher.get_default_prefix_level()
      • Patcher.parse_patch_output()
      • Patcher.create_commit()
      • Patcher.apply_patch_for_empty_files()