| name | patch-management |
| description | Create, manage, and maintain patches for Drupal modules using unified diff format. Use when creating patches from code changes, re-rolling patches for new versions, downloading merge request patches, or cleaning up patch files. |
Patch Management
Create, manage, and maintain patches for Drupal modules using unified diff format.
Prerequisites
- Git installed
- Access to module source code
- Drupal project with composer
Instructions
Creating Patches from Git
From uncommitted changes
cd web/modules/contrib/module_name
git diff > ../../../patches/module_name-fix-description.patch
From specific commits
git format-patch -1 abc1234 --stdout > patches/module-commit-fix.patch
From branch comparison
git diff main..feature-branch > patches/module-feature.patch
Patch File Format
Unified diff format:
@@ -10,7 +10,7 @@
public function example() {
- return $old_value;
+ return $new_value;
}
Downloading Merge Request Patches
From Drupal.org GitLab:
curl -o patches/module-issue.patch \
"https://git.drupalcode.org/project/module/-/merge_requests/123.diff"
Better approach: Download and save locally with timestamp:
curl -o patches/module--$(date +%Y-%m-%d)--issue-123.patch \
"https://git.drupalcode.org/project/module/-/merge_requests/123.diff"
Re-rolling Patches
When a patch no longer applies:
- Get the original issue context
head -20 patches/old-patch.patch
- Apply patch with conflicts
cd web/modules/contrib/module_name
git apply --3way ../../../patches/old-patch.patch
- Resolve conflicts and create new patch
git add .
git diff HEAD > ../../../patches/module--$(date +%Y-%m-%d)--issue-reroll.patch
Composer commands for patch cleanup
vardot/varbase-patches registers Composer-native commands (these replace the older Drush commands previously shipped in varbase_core):
composer varbase-patches:cleanup:patches
composer varbase-patches:cleanup:patches-file
Patch Storage Best Practices
- Never delete applied patches - Keep for reference
- Use descriptive names with date and issue number
- Store in
patches/ directory next to composer.json
- Document patch purpose in composer.json description
Directory Structure
project/
├── composer.json
├── patches/
│ ├── drupal-core--2024-01-09--3049332-85.patch
│ ├── paragraphs--2024-02-04--3419073-3.patch
│ └── README.md # Document patches
└── web/
Examples
Example 1: Create patch for PHP 8.4 fix
vim web/modules/contrib/module/src/Service.php
cd web/modules/contrib/module
git diff src/Service.php > ../../../../patches/module--$(date +%Y-%m-%d)--php84-nullable.patch
Example 2: Apply and test patch
cd web/modules/contrib/module
git apply --check ../../../../patches/module-fix.patch
git apply ../../../../patches/module-fix.patch
Example 3: Convert MR to local patch
curl -o patches/module--$(date +%Y-%m-%d)--3456789-mr-42.patch \
"https://git.drupalcode.org/project/module/-/merge_requests/42.diff"
Troubleshooting
| Issue | Solution |
|---|
| Patch won't apply | Re-roll for current version |
| Whitespace errors | Use git apply --whitespace=fix |
| Wrong patch level | Try -p1 or -p2 option |
| Already applied | Remove or add to patches-ignore |