Overview

Sometimes while applying patches, it may face offsets mismatch and results in failure. This makes your build verification also to fail.
You can get similar error in console logs for patch failures.

ERROR: Command Error: exit status: 1 Output:
Applying patch index.patch
patching file source/Styles/xb3/code/index.php
Hunk #1 succeeded at 22 (offset 21 lines).
Hunk #2 succeeded at 32 (offset 21 lines).
Hunk #3 succeeded at 73 (offset 21 lines).
Hunk #4 succeeded at 183 (offset 27 lines).
Hunk #5 succeeded at 195 (offset 27 lines).
Hunk #6 succeeded at 245 (offset 37 lines).
Hunk #7 succeeded at 307 (offset 37 lines).
Hunk #8 succeeded at 460 (offset 52 lines).
Hunk #9 succeeded at 469 (offset 52 lines).
Hunk #10 FAILED at 431.
Hunk #11 FAILED at 445.
Hunk #12 FAILED at 455.
3 out of 12 hunks FAILED -- rejects in file source/Styles/xb3/code/index.php
Patch index.patch does not apply (enforce with -f)

From the logs, you can find out that patch is not applied to the source file properly. In such scenarios you will have to update or refersh the patch.

Step 1 :

1.Find the repo for the patch file.(For ex : here the patch file is index.patch.)

2.Clone the repo. For ex:

3.Checkout the branch for which you want to create the patch.

4.Find the location of the patch file in the repo.

          ./meta-pacexf3/recipes-ccsp/ccsp/ccsp-webui/index.patch

Step 2 :

1.Next find the repo for the actual source file to which the patch file was getting patched. ( For ex: here the source file is index.php)

2.clone the repo

3.checkout the required branch

4.Cherry-pick the required changes also

5.After cherry-picking you can verify the changes in the source file.

Step 3 :

1.create another directory, and clone the source code repo (as in step 2). For ex :

2.checkout the required branch

3.Copy the patch file from the patch repo (step 1) to the current directory.

           cmpnt_build_custom_pre_arm.mk   CONTRIBUTING.md       debug_scripts      LICENSE         NOTICE         scripts
           cmpnt_build_custom_pre_pc.mk           COPYING                        index.patch          Makefile.orig     README       source

4.Apply the patch to the source file/files.

5.It should be applied successfully. In case of observing any failure when apply the patch to the source file/files then skip this step. If no failures observed then take a backup for the file/files got patched. For ex : 

6.In case of observing any failure when apply the patch to the source file/files, then this may expects some other patch to be applied first. In such case,

            Example: Repo Init for - Project: <clone_url> Branch: 2003_sprint Manifest: ciscoxb3-3939B

            $ mkdir cisco_intel_repo

            $ cd cisco_intel_repo

          $ repo init -u <clone_url> -m ciscoxb3-3939B.xml -b 2003_sprint

          $ repo sync -j4 --no-clone-bundle

           $ grep -irn "Filename" meta-*          

           $ cp source/Styles/xb3/code/index.php source/Styles/xb3/code/index_bk.php      

Step 4 :

1.Now you can use "Meld tool" to compare between files to refresh the patch. Here you can compare between the source file generated in step 2 (which will have the required changes) and the source file generated in step-3 (which will have the patch appied on to it).

2.During comparing between source files make sure that you take only the required changes (changes available in actual source file step-2) to the patched file generated in step-3.

3.Once all changes are taken , you can verify the patch by checking the option file/format as patch in the tool. Save the updated file and copy it to your repo (repo generated in step-2)

4.Now in your repo , you will have 2 source file (for ex: one will be index.php --original file with the required changes and index_bk.php --updated file with patch applied and also your changes).

5.In terminal , you can use command "diff -ruN  file1 file2 > new_patch_file.patch" to generate a  new patch.

6.For ex : diff -ruN index.php index_bk.php > new_index.patch

7.In case patch file has more than one file, then append the difference using diff -ruN next_file nextfile_bk.php >> new_index.patch

Step 5 :

1.Open the newly created patch file, update the file location correctly and save it. For ex :

--- git/source/Styles/xb3/code/index.php 2019-05-20 05:56:54.047078876 +0000
+++ git.1/source/Styles/xb3/code/index.php 2019-05-20 06:26:56.000000000 +0000

Step 6 :

1.In order to verify the newly created patch, you can create a temporary folder, clone the repo, checkout the required branch. Now copy the latest patch (new_index.patch) here. 

2.In the terminal, give the command  patch -p1 <  new_index.patch, will apply the patch to the source file. It should not fail.

step 7 :

1.Now for pushing the latest patch, clone the repo for patch (step-1)

2.checkout the required branch.

3.copy the latest patch(new_index.patch in this case) to the actual patch file available in the repo.

4.It will show the file as modified. Perform git add.

5.Update if any commit message has to be added and try to push the changes.

6.It fails for commit message upload. For ex : you may get error like this :

7.Run the command mention in logs.

8.This will push the changes successfully to the branch. You can open the gerrit link and verify.

9.Put the same topic name for the patch and the source file , trigger the verification.