5
5
package com .googlesource .gerrit .plugins .qtcodereview ;
6
6
7
7
import com .google .common .flogger .FluentLogger ;
8
- import com .google .gerrit .extensions .api .changes .NotifyHandling ;
9
8
import com .google .gerrit .extensions .restapi .BadRequestException ;
10
9
import com .google .gerrit .extensions .restapi .MergeConflictException ;
11
10
import com .google .gerrit .extensions .restapi .MethodNotAllowedException ;
12
11
import com .google .gerrit .extensions .restapi .RestApiException ;
13
12
import com .google .gerrit .reviewdb .client .Change ;
14
13
import com .google .gerrit .reviewdb .client .PatchSet ;
15
14
import com .google .gerrit .reviewdb .client .Project ;
16
- import com .google .gerrit .server .ChangeUtil ;
17
15
import com .google .gerrit .server .IdentifiedUser ;
18
- import com .google .gerrit .server .change .NotifyResolver ;
19
- import com .google .gerrit .server .change .PatchSetInserter ;
20
16
import com .google .gerrit .server .git .CodeReviewCommit ;
21
17
import com .google .gerrit .server .git .CodeReviewCommit .CodeReviewRevWalk ;
22
18
import com .google .gerrit .server .git .GitRepositoryManager ;
23
19
import com .google .gerrit .server .git .MergeUtil ;
24
- import com .google .gerrit .server .notedb .ChangeNotes ;
25
20
import com .google .gerrit .server .project .NoSuchProjectException ;
26
21
import com .google .gerrit .server .project .NoSuchRefException ;
27
22
import com .google .gerrit .server .project .ProjectCache ;
35
30
import com .google .inject .Inject ;
36
31
import com .google .inject .Provider ;
37
32
import com .google .inject .Singleton ;
38
- import java .io .IOException ;
39
33
import java .sql .Timestamp ;
40
34
import java .util .Arrays ;
41
35
import java .util .Date ;
42
36
import java .util .List ;
43
- import org .eclipse .jgit .errors .ConfigInvalidException ;
44
37
import org .eclipse .jgit .lib .ObjectId ;
45
38
import org .eclipse .jgit .lib .ObjectInserter ;
46
39
import org .eclipse .jgit .lib .ObjectReader ;
@@ -56,7 +49,6 @@ public class QtCherryPickPatch {
56
49
private final BatchUpdate .Factory batchUpdateFactory ;
57
50
private final GitRepositoryManager gitManager ;
58
51
private final Provider <IdentifiedUser > user ;
59
- private final PatchSetInserter .Factory patchSetInserterFactory ;
60
52
private final MergeUtil .Factory mergeUtilFactory ;
61
53
private final ProjectCache projectCache ;
62
54
private final QtChangeUpdateOp .Factory qtUpdateFactory ;
@@ -65,14 +57,12 @@ public class QtCherryPickPatch {
65
57
QtCherryPickPatch (BatchUpdate .Factory batchUpdateFactory ,
66
58
GitRepositoryManager gitManager ,
67
59
Provider <IdentifiedUser > user ,
68
- PatchSetInserter .Factory patchSetInserterFactory ,
69
60
MergeUtil .Factory mergeUtilFactory ,
70
61
ProjectCache projectCache ,
71
62
QtChangeUpdateOp .Factory qtUpdateFactory ) {
72
63
this .batchUpdateFactory = batchUpdateFactory ;
73
64
this .gitManager = gitManager ;
74
65
this .user = user ;
75
- this .patchSetInserterFactory = patchSetInserterFactory ;
76
66
this .mergeUtilFactory = mergeUtilFactory ;
77
67
this .projectCache = projectCache ;
78
68
this .qtUpdateFactory = qtUpdateFactory ;
@@ -114,7 +104,6 @@ public CodeReviewCommit cherryPickPatch(ChangeData changeData,
114
104
commitToCherryPick .setNotes (changeData .notes ());
115
105
116
106
CodeReviewCommit cherryPickCommit ;
117
- boolean mergeCommit = false ;
118
107
119
108
ProjectState projectState = projectCache .checkedGet (project );
120
109
if (projectState == null ) throw new NoSuchProjectException (project );
@@ -123,14 +112,21 @@ public CodeReviewCommit cherryPickPatch(ChangeData changeData,
123
112
if (commitToCherryPick .getParentCount () > 1 ) {
124
113
// Merge commit cannot be cherrypicked
125
114
logger .atInfo ().log ("qtcodereview: merge commit detected %s" , commitToCherryPick );
126
- mergeCommit = true ;
127
- RevCommit commit = QtUtil .merge (committerIdent ,
128
- git , oi ,
129
- revWalk ,
130
- commitToCherryPick ,
131
- baseCommit ,
132
- true /* merge always */ );
133
- cherryPickCommit = revWalk .parseCommit (commit );
115
+
116
+ if (commitToCherryPick .getParent (0 ).equals (baseCommit )) {
117
+ // allow fast forward, when parent index 0 is correct
118
+ logger .atInfo ().log ("qtcodereview: merge commit fast forwarded" );
119
+ cherryPickCommit = commitToCherryPick ;
120
+ } else {
121
+ logger .atInfo ().log ("qtcodereview: merge of merge created" );
122
+ RevCommit commit = QtUtil .merge (committerIdent ,
123
+ git , oi ,
124
+ revWalk ,
125
+ commitToCherryPick ,
126
+ baseCommit ,
127
+ true );
128
+ cherryPickCommit = revWalk .parseCommit (commit );
129
+ }
134
130
} else {
135
131
String commitMessage = mergeUtil .createCommitMessageOnSubmit (commitToCherryPick , baseCommit );
136
132
cherryPickCommit = mergeUtil .createCherryPickFromCommit (oi ,
@@ -147,30 +143,17 @@ public CodeReviewCommit cherryPickPatch(ChangeData changeData,
147
143
148
144
boolean patchSetNotChanged = cherryPickCommit .equals (commitToCherryPick );
149
145
if (!patchSetNotChanged ) {
150
- logger .atInfo ().log ("qtcodereview: new patch %s -> %s" , commitToCherryPick , cherryPickCommit );
146
+ logger .atInfo ().log ("qtcodereview: %s cherrypicked as %s" , commitToCherryPick , cherryPickCommit );
151
147
oi .flush ();
152
148
}
153
149
Timestamp commitTimestamp = new Timestamp (committerIdent .getWhen ().getTime ());
154
150
BatchUpdate bu = batchUpdateFactory .create (project , identifiedUser , commitTimestamp );
155
- bu .setRepository (git , revWalk , oi );
156
- if (!patchSetNotChanged && !mergeCommit ) {
157
- Change .Id changeId = insertPatchSet (bu , git , changeData .notes (), cherryPickCommit );
158
- bu .addOp (changeData .getId (), qtUpdateFactory .create (newStatus ,
159
- null ,
160
- defaultMessage ,
161
- inputMessage ,
162
- tag ,
163
- commitToCherryPick ));
164
- logger .atInfo ().log ("qtcodereview: cherrypick new patch %s for %s" , cherryPickCommit .toObjectId (), changeId );
165
- } else {
166
- bu .addOp (changeData .getId (), qtUpdateFactory .create (newStatus ,
167
- null ,
168
- defaultMessage ,
169
- inputMessage ,
170
- tag ,
171
- null ));
172
- }
173
-
151
+ bu .addOp (changeData .getId (), qtUpdateFactory .create (newStatus ,
152
+ null ,
153
+ defaultMessage ,
154
+ inputMessage ,
155
+ tag ,
156
+ null ));
174
157
bu .execute ();
175
158
logger .atInfo ().log ("qtcodereview: cherrypick done %s" , changeData .getId ());
176
159
return cherryPickCommit ;
@@ -179,19 +162,4 @@ public CodeReviewCommit cherryPickPatch(ChangeData changeData,
179
162
}
180
163
}
181
164
182
- private Change .Id insertPatchSet (BatchUpdate bu ,
183
- Repository git ,
184
- ChangeNotes destNotes ,
185
- CodeReviewCommit cherryPickCommit )
186
- throws IOException , BadRequestException , ConfigInvalidException {
187
- Change destChange = destNotes .getChange ();
188
- PatchSet .Id psId = ChangeUtil .nextPatchSetId (git , destChange .currentPatchSetId ());
189
- PatchSetInserter inserter = patchSetInserterFactory .create (destNotes , psId , cherryPickCommit );
190
- inserter .setAllowClosed (true );
191
- // .setCopyApprovals(true) doesn't work, so copying done in QtChangeUpdateOp
192
- bu .setNotify (NotifyResolver .Result .none ());
193
- bu .addOp (destChange .getId (), inserter );
194
- return destChange .getId ();
195
- }
196
-
197
165
}
0 commit comments