summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/git/StagingMergeDelegate.java
blob: 64e920834ac4039bdf9907b6b7c59ef39aa2ad9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.git;

import com.google.gerrit.reviewdb.AbstractEntity;
import com.google.gerrit.reviewdb.ApprovalCategory;
import com.google.gerrit.reviewdb.Branch;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.server.git.MergeOp.MergeDelegate;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import com.google.inject.Inject;

import java.util.List;

/**
 * MergeOp variation for staging merges.
 *
 */
public class StagingMergeDelegate implements MergeDelegate {
  /**
   * Factory interface for creating delegates.
   */
  public interface Factory {
    StagingMergeDelegate create();
  }

  private final SchemaFactory<ReviewDb> reviewDbFactory;

  @Inject
  public StagingMergeDelegate(final SchemaFactory<ReviewDb> reviewDbFactory) {
    this.reviewDbFactory = reviewDbFactory;
  }

  @Override
  public List<Change> createMergeList(final Branch.NameKey destBranch)
      throws MergeException {
    ReviewDb reviewDb = null;
    try {
      // Open the review database.
      reviewDb = reviewDbFactory.open();

      // List all changes with STAGING status in the destination branch.
      Branch.NameKey sourceBranch = StagingUtil.getSourceBranch(destBranch);
      List<Change> inStaging = reviewDb.changes().staging(sourceBranch).toList();
      return inStaging;
    } catch (OrmException e) {
      throw new MergeException("Cannot query the database", e);
    } finally {
      // Close the review database.
      if (reviewDb != null) {
        reviewDb.close();
      }
    }
  }

  @Override
  public ApprovalCategory.Id getRequiredApprovalCategory() {
    return ApprovalCategory.STAGING;
  }

  @Override
  public String getMessageForMergeStatus(final CommitMergeStatus status,
      final CodeReviewCommit commit) {
    switch (status) {
      case CLEAN_MERGE: {
        return
          "Change has been successfully merged into the staging branch.";
      }
      case CLEAN_PICK: {
        return
            "Change has been successfully cherry-picked to the staging branch as " + commit.name()
                + ".";
      }
      case PATH_CONFLICT: {
        return
            "Your change could not be merged due to a path conflict.\n"
                + "\n"
                + "Please rebase the change locally against the destination branch and upload a new patch set.\n"
                + "\n"
                + "If the conflict is with another change, wait until it gets integrated before rebasing.";
      }
      case CRISS_CROSS_MERGE: {
        return
            "Your change requires a recursive merge to resolve.\n"
                + "\n"
                + "Please rebase the change locally against the destination branch and upload a new patch set.\n"
                + "\n"
                + "If the conflict is with another change, wait until it gets integrated before rebasing.";
      }
      case CANNOT_CHERRY_PICK_ROOT: {
        return
            "Cannot cherry-pick an initial commit onto an existing branch.\n"
                + "\n"
                + "Please rebase the change locally against the destination branch and upload a new patch set.\n"
                + "\n"
                + "If the conflict is with another change, wait until it gets integrated before rebasing.";
      }
      case NOT_FAST_FORWARD: {
        return
            "Project policy requires all submissions to be a fast-forward.\n"
                + "\n"
                + "Please rebase the change locally against the destination branch and upload a new patch set.\n"
                + "\n"
                + "If the conflict is with another change, wait until it gets integrated before rebasing.";
      }
      default: {
        return null;
      }
    }
  }

  @Override
  public String toString() {
    return "staging";
  }

  @Override
  public AbstractEntity.Status getStatus() {
    return Change.Status.STAGED;
  }

  @Override
  public boolean rebuildStaging() {
    return false;
  }
}