How to apply all git .patch files from /patches folder - npm

5.9k views Asked by At

TL,DR;

I'm looking for a git command, bash script or npm method (script) to take all .patch files in a folder and run

git apply --ignore-whitespace patches/{filename}

What I've tried so far:

git am --ignore-whitespace patches/*.patch

Context (what I'll use it for)

Currently working on a large project, using quite a number of libraries. We're using webpack, frontend is Angular.js, backend on Yii2. Because devs use both linux and windows, the project is compiled inside a docker image.

Up so far, whenever we needed to make a change to a library, we forked it and specified the fork in package.json:

"package-name":"git+https://github.com/{git-user}/{package-name}"

and... it works.

I recently found out about an arguably better way to apply patches to modules, which is patch-package. In short, it avoids forking, saving the changes in a .patch file.

However, creating the patch file is not enough. On deployment, it also needs to be applied before building. Which translates into the following line in the deployment script:

docker exec -it {container-name} bash -c "git apply --ignore-whitespace patches/{package-name}+{package-version}.patch"

which has to run before the one running npm run build.

And, again, it works.


Question

I'm looking for a way to automate taking each file inside patches/ folder and apply them, whithout having to specify them one by one. So that whenever a new .patch is pushed in the patches folder (or an existing one is modified) it gets automatically applied to the package before build, without having to change the deployment script.

I found this question which seems to suggest

git am --ignore-whitespace patches/*.patch

might do the trick (inside the docker console, of course). However, the man page on git-am says:

Apply a series of patches from a mailbox

What mailbox? I want to apply changes from a folder. Trying the am command, the console complains about not knowing who I am (wants email and password).

Do I need to treat it as a simple case of creating a bash script (.sh file), reading all files from the folder and running multiple git apply commands in a for?

Is there a more "git"-ish way/command of doing it?
Would it make more sense to do it using an npm script?

2

There are 2 answers

2
ash On BEST ANSWER

git am might work, but for me it complains that it can't work out what format the patch is in, presumably because it's meant for applying patches from mail.

Why not just use git apply?

Instead of this:

git am --ignore-whitespace patches/*.patch

do this:

git apply --ignore-whitespace patches/*.patch
0
Renel Chesak On

This worked for me.

cat ./patches/*patch | git am

It feeds the contents of the patch files to the git am command.