Skip to content

Commit

Permalink
Merge pull request #18613 from pkryger/allow_homebrew_prefix_in_exter…
Browse files Browse the repository at this point in the history
…nal_patches

Allow HOMEBREW_PREFIX replacement in external patches
  • Loading branch information
MikeMcQuaid authored Oct 27, 2024
2 parents 59d56f8 + 2225af5 commit e4fb339
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Library/Homebrew/patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def contents
end
end

# A string containing a patch.
# A file containing a patch.
class ExternalPatch
extend Forwardable

Expand Down Expand Up @@ -140,7 +140,12 @@ def apply
patch_files.each do |patch_file|
ohai "Applying #{patch_file}"
patch_file = patch_dir/patch_file
safe_system "patch", "-g", "0", "-f", "-#{strip}", "-i", patch_file
Utils.safe_popen_write("patch", "-g", "0", "-f", "-#{strip}") do |p|
File.foreach(patch_file) do |line|
data = line.gsub("@@HOMEBREW_PREFIX@@", HOMEBREW_PREFIX)
p.write(data)
end
end
end
end
end
Expand Down
27 changes: 26 additions & 1 deletion Library/Homebrew/test/patching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
TESTBALL_PATCHES_URL = "file://#{TEST_FIXTURE_DIR}/tarballs/testball-0.1-patches.tgz".freeze
PATCH_URL_A = "file://#{TEST_FIXTURE_DIR}/patches/noop-a.diff".freeze
PATCH_URL_B = "file://#{TEST_FIXTURE_DIR}/patches/noop-b.diff".freeze
PATCH_URL_D = "file://#{TEST_FIXTURE_DIR}/patches/noop-d.diff".freeze
PATCH_A_CONTENTS = File.read("#{TEST_FIXTURE_DIR}/patches/noop-a.diff").freeze
PATCH_B_CONTENTS = File.read("#{TEST_FIXTURE_DIR}/patches/noop-b.diff").freeze
APPLY_A = "noop-a.diff"
APPLY_B = "noop-b.diff"
APPLY_C = "noop-c.diff"
APPLY_D = "noop-d.diff"
# rubocop:enable RSpec/LeakyConstantDeclaration,Lint/ConstantDefinitionInBlock

url TESTBALL_URL
Expand All @@ -39,6 +41,18 @@ def formula(name = "formula_name", path: Formulary.core_path(name), spec: :stabl
end
end

matcher :be_patched_with_homebrew_prefix do
match do |formula|
formula.brew do
formula.patch
s = File.read("libexec/NOOP")
expect(s).not_to include("NOOP"), "libexec/NOOP was not patched as expected"
expect(s).not_to include("@@HOMEBREW_PREFIX@@"), "libexec/NOOP was not patched as expected"
expect(s).to include(HOMEBREW_PREFIX.to_s), "libexec/NOOP was not patched as expected"
end
end
end

matcher :have_its_resource_patched do
match do |formula|
formula.brew do
Expand Down Expand Up @@ -226,6 +240,17 @@ def formula(name = "formula_name", path: Formulary.core_path(name), spec: :stabl
end

f.brew { |formula, _staging| formula.patch }
end.to raise_error(BuildError)
end.to raise_error(Errno::ENOENT)
end

specify "patch_dsl_with_homebrew_prefix" do
expect(
formula do
patch do
url PATCH_URL_D
sha256 PATCH_D_SHA256
end
end,
).to be_patched_with_homebrew_prefix
end
end
10 changes: 10 additions & 0 deletions Library/Homebrew/test/support/fixtures/patches/noop-d.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
diff --git a/libexec/NOOP b/libexec/NOOP
index bfdda4c..e08d8f4 100755
--- a/libexec/NOOP
+++ b/libexec/NOOP
@@ -1,2 +1,2 @@
#!/bin/bash
-echo NOOP
\ No newline at end of file
+echo @@HOMEBREW_PREFIX@@
\ No newline at end of file
1 change: 1 addition & 0 deletions Library/Homebrew/test/support/lib/startup/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
TESTBALL_PATCHES_SHA256 = "799c2d551ac5c3a5759bea7796631a7906a6a24435b52261a317133a0bfb34d9"
PATCH_A_SHA256 = "83404f4936d3257e65f176c4ffb5a5b8d6edd644a21c8d8dcc73e22a6d28fcfa"
PATCH_B_SHA256 = "57958271bb802a59452d0816e0670d16c8b70bdf6530bcf6f78726489ad89b90"
PATCH_D_SHA256 = "07c72c4463339e6e2ce235f3b26e316d4940017bf4b5236e27e757a44d67636c"

TEST_SHA256 = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
2 changes: 2 additions & 0 deletions docs/Formula-Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ patch :p0, "..."

In embedded patches, the string "HOMEBREW\_PREFIX" is replaced with the value of the constant `HOMEBREW_PREFIX` before the patch is applied.

In external patches, the string "@@HOMEBREW\_PREFIX@@" is replaced with the value of the constant `HOMEBREW_PREFIX` before the patch is applied.

### Creating the diff

```sh
Expand Down

0 comments on commit e4fb339

Please sign in to comment.