composer-patches icon indicating copy to clipboard operation
composer-patches copied to clipboard

New files created in wrong directory

Open fritzmg opened this issue 3 years ago • 2 comments

I am trying to create a patch file that creates a new file within the target package.

If I use

diff --git a/vendor/lorem/ipsum/foobar.php b/vendor/lorem/ipsum/foobar.php
new file mode 100644
index 0000000..33a9722
--- /dev/null
+++ b/vendor/lorem/ipsum/foobar.php
@@ -0,0 +1,1 @@
+<?php

The file will becreated in

vendor/lorem/ipsum/vendor/lorem/ipsum/foobar.php

instead of

vendor/lorem/ipsum/foobar.php

Am I missing something?

fritzmg avatar Feb 03 '22 10:02 fritzmg

Curiously, if I have multiple new files in the patch file, the new files will be created in

vendor/lorem/ipsum/
vendor/lorem/ipsum/lorem/ipsum/
vendor/lorem/ipsum/vendor/lorem/ipsum/
vendor/lorem/ipsum/b/vendor/lorem/ipsum/

fritzmg avatar Feb 03 '22 10:02 fritzmg

Ran into the same issue with 1.x ^^

pmzandbergen avatar Feb 21 '22 09:02 pmzandbergen

This is a result of the plugin attempting to guess patch depth. patch depth is no longer guessed: either use the defaults or explicitly say what you want.

main is also now very flexible wrt patch depth. You can define it in a number of ways:

  • Per-patch in the patch definition
  • (coming soon) Per-package in your composer.json
  • Per-package in Util::getDefaultPackagePatchDepth() (this is global for all composer-patches users -- just trying to have good defaults for ecosystems with different workflows like Drupal)
  • Global default as a fallback (which defaults to -p1).

cweagans avatar Feb 07 '23 22:02 cweagans

Hm, could you elaborate on what the "patch depth" actually means? The documentation mentions how to set it (per patch etc.) - but not really what it's for. What would be the correct patch depth for an example like in my original post (and why?)?

fritzmg avatar Apr 11 '23 13:04 fritzmg

https://unix.stackexchange.com/questions/26501/when-patching-whats-the-difference-between-arguments-p0-and-p1

Just want to be clear: the changes I mentioned in my last reply are only available on the main branch, not in the 1.x version of the plugin. There is no release on the main branch yet.

Assuming that the vendor/ directory is in the root of your project, you'd want -p0 because you want to strip 0 segments of the path from the beginning.

Another example: Git patches start with a header like this:

diff --git a/README.md b/README.md
index b60bf9a0..b1167652 100644
--- a/README.md
+++ b/README.md

Since most people don't have directories called a/ or b/ in the root of their project, you'd want -p1 so that you strip the a/ and the b/ from the paths before changing the contents of the file.

Having new files created everywhere is a result of the plugin trying to guess which patch depth to use, but if you're creating a new file, any depth will work and will result in the same file being created in different places on the filesystem.

cweagans avatar Apr 12 '23 17:04 cweagans

I see, thank you. I did realise that the aforementioned settings are only available in the main branch. However, even in version 1.7.3 I tried

{
  "extra": {
    "patchLevel": {
      "lorem/ipsum": "-p1"
    }
  }
}

with a patch file like above. But that only resulted in "could not apply patch".

fritzmg avatar Apr 12 '23 20:04 fritzmg