09 March 2020

blog qt git

Comment reproduire un changement dans un commit d'une branche dans une autre ?

Génération d'un patch

Dans la branche ou l'on veut noter les changements appliqués dans le dernier commit :

git format-patch -1 HEAD

Un fichier est créé :

0001-empty-user-barcode-fix.patch

0001 est un numéro attribué par la commande, empty-user-barcode-fix est le nom de la branche.

On obtient le patch suivant :

From c4719964670bbf01a227c3be3e071528a17cd96a Mon Sep 17 00:00:00 2001
From: Hans-Nikolas Locher <hnlocher@cst.fr>
Date: Thu, 26 Mar 2020 11:55:01 +0100
Subject: [PATCH] empty user barcode fix

---
 src/main/java/struts/action/UserAction.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/struts/action/UserAction.java b/src/main/java/struts/action/UserAction.java
index 82c5ff3..97bfa1f 100644
--- a/src/main/java/struts/action/UserAction.java
+++ b/src/main/java/struts/action/UserAction.java
@@ -79,7 +79,7 @@ public class UserAction extends DispatchAction {
 			for (User u:userForm.getUsers()){
 				if(u.getIduser()==idUser){
 
-					String codeBarreUser=u.getBarcodeBadge().toUpperCase().trim();
+					String codeBarreUser = (u.getBarcodeBadge() != null )?u.getBarcodeBadge().toUpperCase().trim():"";
 
 					if(droitModif(request.getUserPrincipal().getName(), u.getRole())){
 						userForm.setNom(u.getLastName());
-- 
2.9.0.windows.1

Application du patch sur une autre branche

On peut désormais se déplacer sur l'autre branche :

git checkout my-other-branch

Si l'on applique sans l'option -p1, patch cherche le fichier :

$ patch -p0 -i 0001-empty-user-barcode-fix.patch                                                    can't find file to patch at input line 14
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|From c4719964670bbf01a227c3be3e071528a17cd96a Mon Sep 17 00:00:00 2001
|From: Hans-Nikolas Locher <hnlocher@cst.fr>
|Date: Thu, 26 Mar 2020 11:55:01 +0100
|Subject: [PATCH] empty user barcode fix
|
|---
| src/main/java/struts/action/UserAction.java | 2 +-
| 1 file changed, 1 insertion(+), 1 deletion(-)
|
|diff --git a/src/main/java/struts/action/UserAction.java b/src/main/java/struts/action/UserAction.java
|index 82c5ff3..97bfa1f 100644
|--- a/src/main/java/struts/action/UserAction.java
|+++ b/src/main/java/struts/action/UserAction.java
--------------------------
File to patch:
[CTRL]+[C]

Donc en faisant :

patch -p1 -i 0001-empty-user-barcode-fix.patch

J'obtient dans un premier temps :

UserAction.java.rej

qui contient :

--- src/main/java/struts/action/UserAction.java
+++ src/main/java/struts/action/UserAction.java
@@ -79,7 +79,7 @@ public class UserAction extends DispatchAction {
 			for (User u:userForm.getUsers()){
 				if(u.getIduser()==idUser){
 
-					String codeBarreUser=u.getBarcodeBadge().toUpperCase().trim();
+					String codeBarreUser = (u.getBarcodeBadge() != null )?u.getBarcodeBadge().toUpperCase().trim():"";
 
 					if(droitModif(request.getUserPrincipal().getName(), u.getRole())){
 						userForm.setNom(u.getLastName());

C'est à dire la "charge" du patch.

En mettant des fins de ligne windows dans le patch :

patch -p1 -i 0001-empty-user-barcode-fix.patch
(Stripping trailing CRs from patch; use --binary to disable.)
patching file src/main/java/struts/action/UserAction.java

Les fins de ligne ne sont pas préservées, les fins de ligne windows ([CR][LF]) sont remplaçées par des fins de lignes unix ([LF]).

Enfin, si l'on change les fins de ligne du patch le patch en CR/LF et que l'on ajoute l'option --binary:

$ patch -p1 --binary -i 0001-empty-user-barcode-fix.patch
patching file src/main/java/struts/action/UserAction.java

Permet d'avoir le patch appliqué et les CR/LF maintenus tels qu'ils étaient, et donc de ne pas polluer l'historique avec des changements non signifiants à toutes les lignes...