aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/googlesource/gerrit/plugins/qtcodereview/QtCherryPickPatch.java
blob: 73eb7ce960d6512598c91c0c61a40b28254876c1 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// Copyright (C) 2019 The Qt Company
//

package com.googlesource.gerrit.plugins.qtcodereview;

import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.api.changes.NotifyHandling;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MergeConflictException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.change.PatchSetInserter;
import com.google.gerrit.server.git.CodeReviewCommit;
import com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.MergeUtil;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.NoSuchRefException;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.submit.IntegrationException;
import com.google.gerrit.server.submit.MergeIdenticalTreeException;
import com.google.gerrit.server.update.BatchUpdate;
import com.google.gerrit.server.update.UpdateException;
import com.google.gerrit.server.util.time.TimeUtil;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;

@Singleton
public class QtCherryPickPatch {

    private static final FluentLogger logger = FluentLogger.forEnclosingClass();

    private final Provider<ReviewDb> dbProvider;
    private final BatchUpdate.Factory batchUpdateFactory;
    private final GitRepositoryManager gitManager;
    private final Provider<IdentifiedUser> user;
    private final PatchSetInserter.Factory patchSetInserterFactory;
    private final MergeUtil.Factory mergeUtilFactory;
    private final ProjectCache projectCache;
    private final QtChangeUpdateOp.Factory qtUpdateFactory;

    @Inject
    QtCherryPickPatch(Provider<ReviewDb> dbProvider,
                      BatchUpdate.Factory batchUpdateFactory,
                      GitRepositoryManager gitManager,
                      Provider<IdentifiedUser> user,
                      PatchSetInserter.Factory patchSetInserterFactory,
                      MergeUtil.Factory mergeUtilFactory,
                      ProjectCache projectCache,
                      QtChangeUpdateOp.Factory qtUpdateFactory) {
        this.dbProvider = dbProvider;
        this.batchUpdateFactory = batchUpdateFactory;
        this.gitManager = gitManager;
        this.user = user;
        this.patchSetInserterFactory = patchSetInserterFactory;
        this.mergeUtilFactory = mergeUtilFactory;
        this.projectCache = projectCache;
        this.qtUpdateFactory = qtUpdateFactory;
    }

    public CodeReviewCommit cherryPickPatch(ChangeData changeData,
                                            Project.NameKey project,
                                            ObjectId sourceId,
                                            ObjectId destId,
                                            boolean allowFastForward,
                                            Change.Status newStatus,
                                            String defaultMessage,
                                            String inputMessage,
                                            String tag)
                                            throws IntegrationException {

        IdentifiedUser identifiedUser = user.get();
        try (Repository git = gitManager.openRepository(project);
             ObjectInserter oi = git.newObjectInserter();
             ObjectReader reader = oi.newReader();
             CodeReviewRevWalk revWalk = CodeReviewCommit.newRevWalk(reader)) {

            if (!git.hasObject(sourceId)) throw new NoSuchRefException("Invalid source objectId: " + sourceId);
            if (!git.hasObject(destId)) throw new NoSuchRefException("Invalid destination objectid: " + destId);

            RevCommit baseCommit = revWalk.parseCommit(destId);
            CodeReviewCommit commitToCherryPick = revWalk.parseCommit(sourceId);

            List parents = Arrays.asList(commitToCherryPick.getParents());
            if (allowFastForward == true && parents.contains(baseCommit) && commitToCherryPick.getParentCount() < 2) {
                logger.atInfo().log("qtcodereview: cherrypick fast forward %s on top of %s", sourceId, destId);
                return commitToCherryPick;
            }

            Timestamp now = TimeUtil.nowTs();
            PersonIdent committerIdent = commitToCherryPick.getCommitterIdent();

            commitToCherryPick.setPatchsetId(changeData.currentPatchSet().getId());
            commitToCherryPick.setNotes(changeData.notes());

            CodeReviewCommit cherryPickCommit;
            boolean mergeCommit = false;

            ProjectState projectState = projectCache.checkedGet(project);
            if (projectState == null) throw new NoSuchProjectException(project);

            MergeUtil mergeUtil = mergeUtilFactory.create(projectState, true);
            if (commitToCherryPick.getParentCount() > 1) {
                // Merge commit cannot be cherrypicked
                logger.atInfo().log("qtcodereview: merge commit detected %s", commitToCherryPick);
                mergeCommit = true;
                RevCommit commit = QtUtil.merge(committerIdent,
                                                git, oi,
                                                revWalk,
                                                commitToCherryPick,
                                                baseCommit,
                                                true /* merge always */);
                cherryPickCommit = revWalk.parseCommit(commit);
            } else {
                String commitMessage = mergeUtil.createCommitMessageOnSubmit(commitToCherryPick, baseCommit);
                commitMessage += " "; // This ensures unique SHA1 is generated, otherwise old is reused
                cherryPickCommit = mergeUtil.createCherryPickFromCommit(oi,
                                                                        git.getConfig(),
                                                                        baseCommit,
                                                                        commitToCherryPick,
                                                                        committerIdent,
                                                                        commitMessage,
                                                                        revWalk,
                                                                        0,
                                                                        true,   // ignoreIdenticalTree
                                                                        false); // allowConflicts
            }

            boolean patchSetNotChanged = cherryPickCommit.equals(commitToCherryPick);
            if (!patchSetNotChanged) {
                logger.atInfo().log("qtcodereview: new patch %s -> %s", commitToCherryPick, cherryPickCommit);
                oi.flush();
            }
            BatchUpdate bu = batchUpdateFactory.create(dbProvider.get(), project, identifiedUser, now);
            bu.setRepository(git, revWalk, oi);
            if (!patchSetNotChanged && !mergeCommit) {
                Change.Id changeId = insertPatchSet(bu, git, changeData.notes(), cherryPickCommit);
                bu.addOp(changeData.getId(), qtUpdateFactory.create(newStatus,
                                                                    defaultMessage,
                                                                    inputMessage,
                                                                    tag,
                                                                    commitToCherryPick));
                logger.atInfo().log("qtcodereview: cherrypick new patch %s for %s", cherryPickCommit.toObjectId(), changeId);
            } else {
                bu.addOp(changeData.getId(), qtUpdateFactory.create(newStatus,
                                                                    defaultMessage,
                                                                    inputMessage,
                                                                    tag,
                                                                    null));
            }

            bu.execute();
            logger.atInfo().log("qtcodereview: cherrypick done %s", changeData.getId());
            return cherryPickCommit;
        } catch (Exception e) {
            throw new IntegrationException("Cherry pick failed: " + e.getMessage());
        }
    }

    private Change.Id insertPatchSet(BatchUpdate bu,
                                     Repository git,
                                     ChangeNotes destNotes,
                                     CodeReviewCommit cherryPickCommit)
                                     throws IOException, OrmException, BadRequestException, ConfigInvalidException {
        Change destChange = destNotes.getChange();
        PatchSet.Id psId = ChangeUtil.nextPatchSetId(git, destChange.currentPatchSetId());
        PatchSetInserter inserter = patchSetInserterFactory.create(destNotes, psId, cherryPickCommit);
        inserter.setNotify(NotifyHandling.NONE)
                .setAllowClosed(true);
                // .setCopyApprovals(true) doesn't work, so copying done in QtChangeUpdateOp
        bu.addOp(destChange.getId(), inserter);
        return destChange.getId();
    }

}