# Configuring new code calculation

By default, your project applies the baseline for new code (global-level new code definition). This means it consistently uses the baseline for new code. Consequently, any modifications to the baseline will automatically be applied to your project. You can apply a specific new code definition for your project instead.

By default, any branch in your project applies the project’s new code definition. You can apply a specific new code definition instead.

For more information, see [about-new-code](https://docs.sonarsource.com/sonarqube-server/user-guide/about-new-code "mention").

## Setting a specific new code definition for your project or branch <a href="#setting-specific-new-code-definition-for-project" id="setting-specific-new-code-definition-for-project"></a>

As a project administrator, you can set the new code definition for your project or branch in the UI (except the Specific analysis option) or using the Web API, at creation time or anytime later as explained below.

{% hint style="info" %}
For more compliance with the Sonar recommended settings, the Specific analysis option can only be set using the Web API, as it would require frequent user action to be kept up to date.
{% endhint %}

{% hint style="info" %}
You can configure the Reference branch option through an analysis parameter on the CI/CD host. See **Additional setup and recommendations > If using Reference branch option** below.
{% endhint %}

### In the SonarQube Server <a href="#in-the-ui" id="in-the-ui"></a>

#### New code definition for a project

To set a specific new code definition for your project or branch in the SonarQube:

<figure><img src="https://2744305742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VWSqvZ4eaBLWvA6epdv%2Fuploads%2FDWi0uBmsj8UPXKHMNmG2%2Fproject-new-code-settings.png?alt=media&#x26;token=92a20d1a-9009-40fd-aaff-edfae520fbd0" alt=""><figcaption></figcaption></figure>

1\. Retrieve your project. See [retrieving-projects](https://docs.sonarsource.com/sonarqube-server/user-guide/viewing-projects/retrieving-projects "mention") for details.

2\. Go to **Project Settings > New Code**.

3\. In **Project definition of new code** section, select **Custom** and then the option you want to apply to your project. The options are **Previous version**, **Number of days**, and **Reference branch**.

#### New code definition for a branch

To apply a specific setting to a branch:

1. Navigate to the **Set a specific setting for a branch** section at the bottom of the page.
2. In the **Actions** column of the branch, select the edit icon. The **New Code for \<branch>** dialog opens.
3. Select the new code option you want to apply to the branch. The options are **Previous version**, **Number of days**, and **Reference branch**.
4. Click **Save**.

### Via the Web API <a href="#via-the-web-api" id="via-the-web-api"></a>

Use the [api/new\_code\_periods/set](https://next.sonarqube.com/sonarqube/web_api/api/new_code_periods/set) endpoint by specifying:

* To set a specific definition for your project: the project
* To set a specific definition for a branch: the project and branch.

## Additional setup and recommendations <a href="#additional-setup-and-recommendations" id="additional-setup-and-recommendations"></a>

Make sure to follow the recommendations about the [Checked-out code](https://app.gitbook.com/s/69lEOGGgOhCpumODGD9v/analyzing-source-code/scanners/scanner-environment/verifying-code-checkout-step "mention") and [SCM integration](https://app.gitbook.com/s/69lEOGGgOhCpumODGD9v/analyzing-source-code/scm-integration "mention").

### If using any option except Reference branch <a href="#if-using-any-option-except-reference-branch" id="if-using-any-option-except-reference-branch"></a>

When using any new code option other than Reference branch, we recommend completing your merges using the fast-forward option without a merge commit; examples include GitHub’s squash and merge or rebase and merge options. That way, blame for merged commits will always have a more recent commit date.

### If using Previous version option <a href="#if-using-previous-version-option" id="if-using-previous-version-option"></a>

The current version of a project is determined in different ways depending on the build system:

* If the analysis is done using the [sonarscanner-for-maven](https://docs.sonarsource.com/sonarqube-server/analyzing-source-code/scanners/sonarscanner-for-maven "mention"), then SonarQube Server reads the version from the `pom.xml` file.
* If the analysis is done with the [sonarscanner-for-gradle](https://docs.sonarsource.com/sonarqube-server/analyzing-source-code/scanners/sonarscanner-for-gradle "mention") then SonarQube Server reads the version from the `build.gradle` file.
* In all other cases, you must explicitly specify the version by setting the analysis parameter `sonar.projectVersion`. For more information, see [#project-information](https://docs.sonarsource.com/sonarqube-server/analyzing-source-code/analysis-parameters/parameters-not-settable-in-ui#project-information "mention").

### If using Reference branch option <a href="#if-using-reference-branch-option" id="if-using-reference-branch-option"></a>

To make the Reference branch option work properly:

* The same branch name should be used in the SCM repository and SonarQube.
* The reference branch is fetched in the CI/CD host’s local repository (This is usually done through the cloning of the remote repository by the CI pipeline).
* The code in the CI/CD host’s local repository matches the code in the remote repository.

You can use the `sonar.newCode.referenceBranch` property to apply the Reference branch option to the analysis of a branch, overriding the global and project-level new code definition set in the UI. This property specifies the reference branch value. This setting is particularly useful during the first analysis when the branch to be analyzed does not exist yet in SonarQube (i.e. you cannot set a new code definition for this branch in SonarQube).

## Related pages <a href="#related-pages" id="related-pages"></a>

* [about-new-code](https://docs.sonarsource.com/sonarqube-server/user-guide/about-new-code "mention")
* [setting-new-code-definition-at-global-level](https://docs.sonarsource.com/sonarqube-server/instance-administration/analysis-functions/setting-new-code-definition-at-global-level "mention") at the instance level
