Improve "Crear parches con Git"

This commit is contained in:
Jesús 2020-05-26 13:06:17 -05:00
parent 0369b9048b
commit bf37900118
No known key found for this signature in database
GPG Key ID: F6EE7BC59A315766
2 changed files with 123 additions and 126 deletions

View File

@ -42,7 +42,7 @@ So, to modify our script, text or source code, we must first
create the directory `a` and `b`
:::bash
mkdir a b
$ mkdir a b
In directory `a` we will put the unmodified file or files,
and in directory `b` the modified one.
@ -51,7 +51,7 @@ and in directory `b` the modified one.
Run:
```bash
git diff --no-prefix --no-index --no-renames --binary a b > patched.patch
$ git diff --no-prefix --no-index --no-renames --binary a b > patched.patch
```
+ --no-prefix: Do not show any source or destination prefix.
@ -71,7 +71,7 @@ depending on the case.
then we will use this command:
:::bash
patch -p1 -i /ruta/del/parche.diff
$ patch -p1 -i /ruta/del/parche.diff
2. With binary files: That is, things like already compiled executable programs,
PNG images, JPEG, Gif, etc. other than plain text. In general you will be able
@ -79,7 +79,7 @@ depending on the case.
"GIT binary patch". In this case we will apply the patch as follows:
:::bash
git apply -v /ruta/del/parche.diff
$ git apply -v /ruta/del/parche.diff
## The issue with diff and not making directories a and b
Now, going back to what I said earlier about why this is important,
@ -107,50 +107,49 @@ In the first one, I will create the files that I put as an example
**script.sh:**
```bash
#!/bin/bash
echo "Hello world"
```
#!bash
#!/bin/bash
echo "Hello world"
**script.sh.new:**
```bash
#!/bin/sh
echo "Hello world"
echo "This is a patched file :D"
```
#!sh
#!/bin/sh
echo "Hello world"
echo "This is a patched file :D"
Now we will do what most internet tutorials tell you to do:
:::bash
diff -u script.sh script.sh.new
$ diff -u script.sh script.sh.new
And it looks like this:
```diff
--- script.sh 2018-03-16 15:52:49.887087539 -0300
+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
```
#!diff
--- script.sh 2018-03-16 15:52:49.887087539 -0300
+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
Everything apparently fine, but now let's apply that patch
```bash
$ diff -u script.sh script.sh.new | patch -p1 -i /dev/stdin
```
```diff
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- script.sh 2018-03-16 15:52:49.887087539 -0300
|+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
--------------------------
File to patch:
```
#!diff
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- script.sh 2018-03-16 15:52:49.887087539 -0300
|+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
--------------------------
File to patch:
It fails being that I am in the same directory as `script.sh{.new}`,
so this is fixed using the create directories `a/` and `b/` hack.
However, this does not turn out point 2 and 3. Let's go for it.
@ -169,17 +168,17 @@ Okay, now let's make the patch with diff:
$ diff -ur a b
```
```diff
Sólo en b: binary_file.bin
diff -ur a/script.sh b/script.sh
--- a/script.sh 2018-03-16 15:37:27.513802777 -0300
+++ b/script.sh 2018-03-16 15:41:17.717123987 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
```
#!diff
Only in b: binary_file.bin
diff -ur a/script.sh b/script.sh
--- a/script.sh 2018-03-16 15:37:27.513802777 -0300
+++ b/script.sh 2018-03-16 15:41:17.717123987 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
And what is said in point 2 is true, it does not put the new file,
it tells you "Only in b" or if there is a file that is in `a/` but not in `b/`
(that is to say, surely you removed it from your fork), you will
@ -194,27 +193,27 @@ See what happens if I use `git` instead of `diff`:
$ git diff --no-prefix --no-index --no-renames --binary a b
```
```diff
diff --git b/binary_file.bin b/binary_file.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1ce3c1c596d7a7f400b0cc89bda5a41eed2780c5
GIT binary patch
literal 73
pcmd-HXHZUIU{c}EWl|AfLZWk+R0P|Ad@#)bSHb~R0-{lr003gr3L5|b
#!diff
diff --git b/binary_file.bin b/binary_file.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1ce3c1c596d7a7f400b0cc89bda5a41eed2780c5
GIT binary patch
literal 73
pcmd-HXHZUIU{c}EWl|AfLZWk+R0P|Ad@#)bSHb~R0-{lr003gr3L5|b
literal 0
HcmV?d00001
literal 0
HcmV?d00001
diff --git a/script.sh b/script.sh
index da049c4..3d351f5 100644
--- a/script.sh
+++ b/script.sh
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
diff --git a/script.sh b/script.sh
index da049c4..3d351f5 100644
--- a/script.sh
+++ b/script.sh
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
```
Now I did consider the non-existent binary file in `a/` but tangible in `b/`.
Note that in this particular case, as I explained earlier, when dealing with
binary files that only git supports (see the message "GIT binary patch") you

View File

@ -36,7 +36,7 @@ Entonces, para modificar nuestro script, texto o código fuente primero hay que
crear el directorio `a` y `b`
:::bash
mkdir a b
$ mkdir a b
En el directorio `a` pondremos el o los archivos sin modificar, y en el
directorio `b` el modificado.
@ -45,7 +45,7 @@ directorio `b` el modificado.
Ejecuta:
```bash
git diff --no-prefix --no-index --no-renames --binary a b > parche.patch
$ git diff --no-prefix --no-index --no-renames --binary a b > parche.patch
```
+ --no-prefix: No mostrar ningún prefijo de origen o destino.
@ -65,7 +65,7 @@ dependiendo del caso.
entonces usaremos este comando:
:::bash
patch -p1 -i /ruta/del/parche.diff
$ patch -p1 -i /ruta/del/parche.diff
2. Con archivos binarios: Es decir, cosas como programas ejecutables ya compilados,
imágenes PNG, JPEG, Gif, etc. que no sean texto plano. En general podrás identificar
@ -73,7 +73,7 @@ dependiendo del caso.
En este caso aplicaremos el parche de la siguiente manera:
:::bash
git apply -v /ruta/del/parche.diff
$ git apply -v /ruta/del/parche.diff
## El problema con diff y no hacer directorios a y b
Ahora, regresando a lo que decía anteriormente sobre por qué esto es importante,
@ -98,50 +98,49 @@ En el primero, crearé los archivos que puse de ejemplo (valga la redundancia) y
**script.sh:**
```bash
#!/bin/bash
echo "Hello world"
```
#!sh
#!/bin/bash
echo "Hello world"
**script.sh.new:**
```bash
#!/bin/sh
echo "Hello world"
echo "This is a patched file :D"
```
#!sh
#!/bin/sh
echo "Hello world"
echo "This is a patched file :D"
Ahora haremos lo que la mayoría de tutoriales de internet te dicen que hagas:
:::bash
diff -u script.sh script.sh.new
$ diff -u script.sh script.sh.new
Y me queda así:
```diff
--- script.sh 2018-03-16 15:52:49.887087539 -0300
+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
```
#!diff
--- script.sh 2018-03-16 15:52:49.887087539 -0300
+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
Todo aparentemente bien, pero ahora apliquemos dicho parche
```bash
$ diff -u script.sh script.sh.new | patch -p1 -i /dev/stdin
```
```diff
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- script.sh 2018-03-16 15:52:49.887087539 -0300
|+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
--------------------------
File to patch:
```
#!diff
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- script.sh 2018-03-16 15:52:49.887087539 -0300
|+++ script.sh.new 2018-03-16 15:53:02.490420209 -0300
--------------------------
File to patch:
Falla siendo que estoy en el mismo directorio que `script.sh{.new}`, de modo que
esto se corrige usando el hack de crear los directorios `a/` y `b/`.
Sin embargo, esto no resulve el punto 2 y 3. Vamos a por ello.
@ -160,17 +159,17 @@ Bien, ahora hagamos el parche con diff:
$ diff -ur a b
```
```diff
Sólo en b: archivo_binario.bin
diff -ur a/script.sh b/script.sh
--- a/script.sh 2018-03-16 15:37:27.513802777 -0300
+++ b/script.sh 2018-03-16 15:41:17.717123987 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
```
#!diff
Sólo en b: archivo_binario.bin
diff -ur a/script.sh b/script.sh
--- a/script.sh 2018-03-16 15:37:27.513802777 -0300
+++ b/script.sh 2018-03-16 15:41:17.717123987 -0300
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
Y se cumple lo que decía en el punto 2, no te pone el archivo nuevo,
te dice "Sólo en b" o si hay un fichero que está en `a/` pero no en `b/`
(es decir, seguro que lo eliminaste de tu fork), te saldrá el mensaje
@ -185,27 +184,26 @@ Mira lo que pasa si uso `git` en vez de `diff`:
$ git diff --no-prefix --no-index --no-renames --binary a b
```
```diff
diff --git b/archivo_binario.bin b/archivo_binario.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1ce3c1c596d7a7f400b0cc89bda5a41eed2780c5
GIT binary patch
literal 73
pcmd-HXHZUIU{c}EWl|AfLZWk+R0P|Ad@#)bSHb~R0-{lr003gr3L5|b
#!diff
diff --git b/archivo_binario.bin b/archivo_binario.bin
new file mode 100644
index 0000000000000000000000000000000000000000..1ce3c1c596d7a7f400b0cc89bda5a41eed2780c5
GIT binary patch
literal 73
pcmd-HXHZUIU{c}EWl|AfLZWk+R0P|Ad@#)bSHb~R0-{lr003gr3L5|b
literal 0
HcmV?d00001
literal 0
HcmV?d00001
diff --git a/script.sh b/script.sh
index da049c4..3d351f5 100644
--- a/script.sh
+++ b/script.sh
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
```
diff --git a/script.sh b/script.sh
index da049c4..3d351f5 100644
--- a/script.sh
+++ b/script.sh
@@ -1,2 +1,3 @@
-#!/bin/bash
+#!/bin/sh
echo "Hello world"
+echo "This is a patched file :D"
Ahora sí me consideró el archivo binario inexistente en `a/` pero tangible en `b/`.
Noten que en este caso particular, como ya expliqué anteriormente, al tratar con