Skip to content

Commit

Permalink
Update working with patches doc
Browse files Browse the repository at this point in the history
Fixed a few typos, updated the procedure,
and formatted the doc.

Test: N/A

Bug: b/266660664
Change-Id: I94df37b84099e678150db809b199465c2bdefbce
GitOrigin-RevId: 0722528a7ab93d6957600d89dab836327a6610cd
  • Loading branch information
bozydarsz authored and copybara-github committed Sep 11, 2023
1 parent 8fa0314 commit 78786d6
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions third_party/patches/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
# Working with patches

Patch files are used to apply the changes to repositories imported with Bazel's [`http_archive`](https://docs.bazel.build/versions/main/repo/http.html) rule.
Patch files are used to apply the changes to repositories imported with Bazel's
[`http_archive`](https://docs.bazel.build/versions/main/repo/http.html) rule.

## Generating the patch files

The patch files are generated with `git diff` commmand which displays the diff between the workspace and the last committed version.
The patch files are generated with `git diff` command which displays the diff
between the workspace and the last committed version.

Typical worklfow for generating the patch files is described on example of `llvm` repository below. The repository is imported into reclient WORKSPACE from https://github.com/llvm/llvm-project/. `WORKSPACE` file defines the commit that should be downloaded [here](https://github.com/bazelbuild/reclient/tree/main/WORKSPACE;l=47).
Typical workflow for generating the patch files is described on example of
`llvm` repository below. The repository is imported into reclient WORKSPACE from
https://github.com/llvm/llvm-project/ and
[`settings.bzl`](https://github.com/bazelbuild/reclient/tree/main/settings.bzl)
file defines the commit that should be downloaded.

To generate the patch file, the user should:
1. Clone the remote repository by running `git clone https://github.com/llvm/llvm-project/`
2. Checkout the same commit that's imported by reclient (e.g. `git checkout 39e2e3bddbf459d16b00969af1415ebee11fcbdc`)
3. Make the necessary changes in `llvm-project` respository
4. Generate the patch file by running `git diff > <re-client-workspace>/third_party/patches/<patch-name>.patch`

1. Clone the remote repository by running `git clone
https://github.com/llvm/llvm-project/`
1. Checkout the same commit that's imported by reclient (e.g. `git checkout
39e2e3bddbf459d16b00969af1415ebee11fcbdc`)
1. Make the necessary changes in `llvm-project` repository
1. Generate the patch file by running `git diff >
<re-client-workspace>/third_party/patches/<patch-name>.patch`

## Applying patches

Patches are applied after `http_archive` extracts the archive. To add the patch that you'd like to apply, add it to patches argument of `http_archive` rule. Example:
Patches are applied after `http_archive` extracts the archive. To add the patch
that you'd like to apply, add it to patches argument of the `http_archive` rule.
Example:

```
...
patch_args = ["-p1"],
Expand All @@ -27,18 +39,38 @@ patches = [
],
...
```

**Notes:**
1. `patch_args = ["-p1"]` is required when applying patch files generated by `git diff` command. (see [`http_archive` documentation](https://docs.bazel.build/versions/main/repo/http.html) for more details).
2. Patches are applied in the same order as they're declared in the `patches` argument (`patches = ["//1.patch", "//2.patch"]` may produce different results than `patches = ["//2.patch", "//1.patch"]`).

1. `patch_args = ["-p1"]` is required when applying patch files generated by
`git diff` command. (see
[`http_archive` documentation](https://docs.bazel.build/versions/main/repo/http.html)
for more details).
1. Patches are applied in the same order as they're declared in the `patches`
argument (`patches = ["//1.patch", "//2.patch"]` may produce different
results than `patches = ["//2.patch", "//1.patch"]`).

## Updating the patched repository

Sometimes, especially after updating the version imported by `http_archive` rule, applying existing patches may fail. In such cases it's useful to try to apply the patch in your local repository and manually fix the changes that were rejected by following steps below (`llvm` repo used as an example):
1. Clone the remote repository by running `git clone https://github.com/llvm/llvm-project/`
2. Checkout the same commit that's imported by reclient (e.g. `git checkout 39e2e3bddbf459d16b00969af1415ebee11fcbdc`)
3. Try to apply the patch with `git apply --reject --whitespace=fix <re-client-workspace>/third_party/patches/<patch-name>.patch`. If the command failed to apply some of the changes, `<patch-name>.patch.rej` file will be generated.
4. Review the content of `<patch-name>.patch.rej` file and apply the changes manually
5. Remove `<patch-name>.patch.rej`
6. Generate updated patch file by running `git diff > <re-client-workspace>/third_party/patches/<patch-name>.patch`
Sometimes, especially after updating the version imported by `http_archive`
rule, applying existing patches may fail. In such cases it's useful to try to
apply the patch in your local repository and manually fix the changes that were
rejected by following steps below (`llvm` repo used as an example):

1. Clone the remote repository by running `git clone
https://github.com/llvm/llvm-project/`
1. Checkout the same commit that's imported by reclient (e.g. `git checkout
39e2e3bddbf459d16b00969af1415ebee11fcbdc`)
1. Try to apply the patch with `git apply --reject --whitespace=fix
<re-client-workspace>/third_party/patches/<patch-name>.patch`. If the
command failed to apply some of the changes, `<patch-name>.patch.rej` file
will be generated.
1. Review the content of `<patch-name>.patch.rej` file and apply the changes
manually
1. Remove `<patch-name>.patch.rej`
1. Generate updated patch file by running `git diff >
<re-client-workspace>/third_party/patches/<patch-name>.patch`

**Note:** Please note that the order of patches matters and they should be applied in the same order that they were declared in the `patches` parameter of `http_archive` rule.
**Note:** Please note that the order of patches matters and they should be
applied in the same order that they were declared in the `patches` parameter of
`http_archive` rule.

0 comments on commit 78786d6

Please sign in to comment.