Class MergeCommandBuilder

java.lang.Object
sonia.scm.repository.api.MergeCommandBuilder

public class MergeCommandBuilder extends Object
Use this MergeCommandBuilder to merge two branches of a repository (executeMerge()) or to check if the branches could be merged without conflicts (dryRun()). To do so, you have to specify the name of the target branch (setTargetBranch(String)) and the name of the branch that should be merged (setBranchToMerge(String)). Additionally you can specify an author that should be used for the commit (setAuthor(Person)) and a message template (setMessageTemplate(String)) if you are not doing a dry run only. If no author is specified, the logged in user and a default message will be used instead. To actually merge feature_branch into integration_branch do this:

     repositoryService.getMergeCommand()
       .setBranchToMerge("feature_branch")
       .setTargetBranch("integration_branch")
       .executeMerge();
 
If the merge is successful, the result will look like this:

                           O    <- Merge result (new head of integration_branch)
                           |\
                           | \
 old integration_branch -> O  O <- feature_branch
                           |  |
                           O  O
 
To check whether they can be merged without conflicts beforehand do this:

     repositoryService.getMergeCommand()
       .setBranchToMerge("feature_branch")
       .setTargetBranch("integration_branch")
       .dryRun()
       .isMergeable();
 
Keep in mind that you should always check the result of a merge even though you may have done a dry run beforehand, because the branches can change between the dry run and the actual merge.
Since:
2.0.0
  • Method Details

    • isSupported

      public boolean isSupported(MergeStrategy strategy)
      Use this to check if merge-strategy is supported by mergeCommand.
      Returns:
      boolean.
    • getSupportedMergeStrategies

      public Set<MergeStrategy> getSupportedMergeStrategies()
      Use this to get a Set of all supported merge strategies by merge command.
      Returns:
      boolean.
    • setBranchToMerge

      public MergeCommandBuilder setBranchToMerge(String branchToMerge)
      Use this to set the branch that should be merged into the target branch. This is mandatory.
      Returns:
      This builder instance.
    • setTargetBranch

      public MergeCommandBuilder setTargetBranch(String targetBranch)
      Use this to set the target branch the other branch should be merged into. This is mandatory.
      Returns:
      This builder instance.
    • setAuthor

      @Deprecated public MergeCommandBuilder setAuthor(Person author)
      Deprecated.
      Use setAuthor(DisplayUser) instead to enable fallback email computation.
      Use this to set the author of the merge commit manually. If this is omitted, the currently logged-in user will be used instead. This is optional and for executeMerge() only.
      Returns:
      This builder instance.
    • setAuthor

      public MergeCommandBuilder setAuthor(DisplayUser author)
      Use this to set the author of the merge commit manually. If this is omitted, the currently logged-in user will be used instead. If the given user object does not have an email address, we will use EMail to compute a fallback address. This is optional and for executeMerge() only.
      Returns:
      This builder instance.
    • disableSigning

      public MergeCommandBuilder disableSigning()
      Disables adding a verifiable signature to the merge commit.
      Returns:
      This builder instance.
      Since:
      2.4.0
    • setMergeStrategy

      public MergeCommandBuilder setMergeStrategy(MergeStrategy strategy)
      Use this to set the strategy of the merge commit manually. This is optional and for executeMerge() only.
      Returns:
      This builder instance.
    • setMessageTemplate

      public MergeCommandBuilder setMessageTemplate(String messageTemplate)
      Use this to set a template for the commit message. If no message is set, a default message will be used. You can use the placeholder {0} for the branch to be merged and {1} for the target branch, eg.:
      
       ...setMessageTemplate("Merge of {0} into {1}")...
       
      This is optional and for executeMerge() only.
      Returns:
      This builder instance.
    • setMessage

      public MergeCommandBuilder setMessage(String message)
      Use this to set the commit message. If no message is set, the message template will be used. This is optional and for executeMerge() only.
      Returns:
      This builder instance.
    • reset

      public MergeCommandBuilder reset()
      Use this to reset the command.
      Returns:
      This builder instance.
    • executeMerge

      public MergeCommandResult executeMerge()
      Use this to actually do the merge. If an automatic merge is not possible, MergeCommandResult.isSuccess() will return false.
      Returns:
      The result of the merge.
    • dryRun

      public MergeDryRunCommandResult dryRun()
      Use this to check whether the given branches can be merged automatically. If this is possible, MergeDryRunCommandResult.isMergeable() will return true.
      Returns:
      The result whether the given branches can be merged automatically.
    • conflicts

      public MergeConflictResult conflicts()
      Use this to compute concrete conflicts for a merge.
      Returns:
      A result containing all conflicts for the merge.