# Apply patch

## Simplified procedure (Majority of cases)

{% hint style="success" %}
The support service:

* attached a patch file (with a .**patch** or .**diff** extension) to their response
* or provided you with a link to the same type of file
  * example: **`https://xxx/bug-fix.diff`**
  * or: **`https://xxx/bug-fix.patch`**
    {% endhint %}

### Place the patch in the correct location

Depending on where the encountered bug is, the **patch** may be intended for the **core** or a **plugin**. It is therefore necessary to place the **patch** in the **correct location** before applying it.

***

#### The **patch** concerns the **GLPI core**

* A **file** was provided to you: Move the previously created file to the GLPI root folder.
* A **link** was provided to you: Go to the GLPI root folder and run the following command to download the patch:
  * wget <https://xxx/bug-fix.diff>
* Then **check that the file is present:**

![Alt text](/files/jXQQbsTJE6QE1mJkwxMz)

***

#### The **patch** concerns a Plugin

* A **file** was provided to you: Move the previously created file to the plugin root folder.
* A **link** was provided to you: Go to the plugin root folder and run the following command to download the patch:
  * wget <https://xxx/bug-fix.diff>
* Then **check that the file is present:**

![Alt text](/files/9V7feYj4t9qPiRGdFY7O)

***

### Test the patch

Before appling patch, you have to test it with this command and parameter --dry-run

```
grep -v "phpunit/" buf-fix.diff | patch -p1 --dry-run
```

{% hint style="info" %}
Does your system indicate that the **patch** command is unknown? [click here](#check-that-the-patch-binary-is-installed)
{% endhint %}

#### Test is failed ❌

In case of an error you should have this

```
patching file inc/config.class.php
Hunk #1 FAILED at 400.
1 out of 1 hunk FAILED -- saving rejects to file inc/config.class.php.rej
```

Many things can lead to failure :

* Your version of GLPI is different from the version used to make the patch
* The file that must be modified by the patch has already been previously

{% hint style="danger" %}
**Stop here and contact support, explaining your issue (feel free to include screenshots, GLPI version, command used, etc.)**
{% endhint %}

#### Test is successfull ✅

In case of a success you should have this

```
patching file inc/config.class.php
```

or this

```
patching file inc/config.class.php
Hunk #1 succeeded at 891 with fuzz 1 (offset 23 lines).
Hunk #2 succeeded at 1301 with fuzz 2 (offset 41 lines).
```

***

### Apply the **patch**

Test successful ✅? You can now actually apply the **patch** with this command

```
grep -v "phpunit/" buf-fix.diff | patch -p1
```

**Congratulation the patch is applied !**

***

## Special cases (rare)

{% hint style="success" %}
The support service:

* asks you to **revert / undo the patch**
* provided you with a **commit number**
* provided you with a **link to a PR**
  {% endhint %}

### Cancel a patch

To cancel a patch, enter the command :

```
patch -p1 < bug-fix.diff -R
```

## You have a commit number

Service support give you this commit number ex: adc9876

* First, you need to retrieve changes from commit
* Go to **`https://github.com/glpi-project/glpi/commit/ + COMMIT_NUMBER`**

{% hint style="success" %}
**Example**

**`https://github.com/glpi-project/glpi/commit/adc9876`**

<img src="/files/utYYOs2o8NxLukVBDTna" alt="" data-size="original">
{% endhint %}

* Then, transform the commit into a patch by adding **`.diff`** at the end of url.

Like this **`https://github.com/glpi-project/glpi/commit/adc9876.diff`**

<div align="left"><img src="/files/JfnxL7EhxTmzALv9G7sD" alt="Alt text"></div>

* Copy the patch URL and run a wget on your GLPI server `wget https://github.com/glpi-project/glpi/commit/adc9876.diff`

{% hint style="info" %}
**Info**

If it's a core patch, perform the `wget` directly at the root of GLPI.&#x20;

If it's a plugin patch, perform the `wget` directly in the folder of the plugin concerned.&#x20;

\
Finally, go to [apply patch](#apply-the-patch) section
{% endhint %}

## You have PR number (Pull Request)

Service support give you a PR number **4770**

* First, you need to retrieve the PR
* Go to **`https://github.com/glpi-project/glpi/pull/ + PR_NUMBER`**

{% hint style="success" %}
**Example**

**`https://github.com/glpi-project/glpi/pull/4770`**&#x20;

![Alt text](/files/HA1kpQvJw1yJspvT617S)
{% endhint %}

* First , transform the PR into a patch by adding **`.diff`** at the end of url.

Like this <https://github.com/glpi-project/glpi/pull/4770.diff>

![Alt text](/files/lwOVStJ7DbIDXonjSGo4)

* Copy the patch URL and run a `wget` on your GLPI server `wget https://github.com/glpi-project/glpi/pull/4770.diff`

{% hint style="info" %}
**Info**

If it's a core patch, perform the `wget` directly at the root of GLPI.&#x20;

If it's a plugin patch, perform the `wget` directly in the folder of the plugin concerned.&#x20;

\
Finally, go to [apply patch](#apply-the-patch) section
{% endhint %}

Finally, go to [apply patch](#apply-the-patch) section

***

### Check that the patch binary is installed

* To known if it installed, just find the version of the binary patch installed

```
patch -v
```

If it installed you should have this

```
GNU patch 2.7.6
Copyright (C) 2003, 2009-2012 Free Software Foundation, Inc.
Copyright (C) 1988 Larry Wall

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Larry Wall and Paul Eggert
```

If not you should have this

```
command not found: patch
```

* Install it with this command

**Ubuntu/Debian**

```
apt-get install patch
```

**CentOS/RHEL**

```
yum install patch
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.glpi-project.org/tutorials/procedures/appliquer_un_patch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
