@@ -308,59 +308,73 @@ private ChangeData findChangeFromList(String changeId, List<ChangeData> changes)
308
308
// Step backwards from the ref and return change list in the same order
309
309
private List <ChangeData > arrangeOrderLikeInRef (
310
310
Repository git , ObjectId refObj , ObjectId tipObj , List <ChangeData > changeList )
311
- throws MissingObjectException , IOException {
311
+ throws Exception {
312
312
List <ChangeData > results = new ArrayList <ChangeData >();
313
- if (refObj .equals (tipObj )) return results ;
314
-
315
- RevWalk revWalk = new RevWalk (git );
316
- RevCommit branchHead = revWalk .parseCommit (tipObj );
317
- RevCommit commit = revWalk .parseCommit (refObj );
318
313
int count = 0 ;
319
- do {
320
- count ++;
321
- String changeId = getChangeId (commit );
314
+ logger .atInfo ().log ("Arranging change order to match original" );
322
315
323
- if (commit .getParentCount () == 0 ) {
324
- commit = null ; // something is going wrong, just exit
325
- } else {
326
- if (changeId == null && commit .getParentCount () > 1 ) {
327
- changeId = getChangeId (revWalk .parseCommit (commit .getParent (1 )));
316
+ try {
317
+ if (refObj .equals (tipObj )) return results ;
318
+
319
+ RevWalk revWalk = new RevWalk (git );
320
+ RevCommit branchHead = revWalk .parseCommit (tipObj );
321
+ RevCommit commit = revWalk .parseCommit (refObj );
322
+
323
+ do {
324
+ count ++;
325
+ String changeId = getChangeId (commit );
326
+
327
+ if (commit .getParentCount () == 0 ) {
328
+ commit = null ; // something is going wrong, just exit
329
+ } else {
330
+ if (changeId == null && commit .getParentCount () > 1 ) {
331
+ changeId = getChangeId (revWalk .parseCommit (commit .getParent (1 )));
332
+ }
333
+ ChangeData change = findChangeFromList (changeId , changeList );
334
+ if (change != null ) results .add (0 , change );
335
+
336
+ // It can always be trusted that parent in index 0 is the correct one
337
+ commit = revWalk .parseCommit (commit .getParent (0 ));
328
338
}
329
- ChangeData change = findChangeFromList (changeId , changeList );
330
- if (change != null ) results .add (0 , change );
339
+ } while (commit != null && !revWalk .isMergedInto (commit , branchHead ) && count < 100 );
340
+ } catch (Exception e ) {
341
+ throw new Exception ("arranging change order failed: " + e .getMessage ());
342
+ }
331
343
332
- // It can always be trusted that parent in index 0 is the correct one
333
- commit = revWalk .parseCommit (commit .getParent (0 ));
334
- }
335
- } while (commit != null && !revWalk .isMergedInto (commit , branchHead ) && count < 100 );
344
+ if (count == 100 ) throw new Exception ("arranging change order failed: too many commits" );
336
345
337
- if (count == 100 ) return null ;
338
346
return results ;
339
347
}
340
348
341
349
private ObjectId pickChangesToStagingRef (
342
350
Repository git , final Project .NameKey projectKey , List <ChangeData > changes , ObjectId tipObj )
343
- throws IOException , IntegrationException {
351
+ throws Exception {
344
352
ObjectId newId = tipObj ;
345
- for (ChangeData item : changes ) {
346
- Change change = item .change ();
347
- logger .atInfo ().log ("qtcodereview: rebuilding add %s" , change );
348
- PatchSet p = item .currentPatchSet ();
349
- ObjectId srcId = git .resolve (p .commitId ().name ());
350
- newId =
351
- qtCherryPickPatch
352
- .cherryPickPatch (
353
- item ,
354
- projectKey ,
355
- srcId ,
356
- newId ,
357
- false , // allowFastForward
358
- null , // newStatus
359
- null , // defaultMessage
360
- null , // inputMessage
361
- TAG_CI // tag
362
- )
363
- .toObjectId ();
353
+ logger .atInfo ().log ("Cherry-picking changes on top of %s" , tipObj .name ());
354
+
355
+ try {
356
+ for (ChangeData item : changes ) {
357
+ Change change = item .change ();
358
+ logger .atInfo ().log ("cherry-picking %s" , change .getKey ());
359
+ PatchSet p = item .currentPatchSet ();
360
+ ObjectId srcId = git .resolve (p .commitId ().name ());
361
+ newId =
362
+ qtCherryPickPatch
363
+ .cherryPickPatch (
364
+ item ,
365
+ projectKey ,
366
+ srcId ,
367
+ newId ,
368
+ false , // allowFastForward
369
+ null , // newStatus
370
+ null , // defaultMessage
371
+ null , // inputMessage
372
+ TAG_CI // tag
373
+ )
374
+ .toObjectId ();
375
+ }
376
+ } catch (Exception e ) {
377
+ throw new Exception ("cherry-picking changes failed: " + e .getMessage ());
364
378
}
365
379
return newId ;
366
380
}
@@ -370,34 +384,46 @@ private ObjectId findReusableStagingHead(
370
384
Repository git ,
371
385
ObjectId stagingHead ,
372
386
ObjectId branchHead ,
373
- List <ChangeData > stagedChanges )
374
- throws MissingObjectException , IOException {
375
-
387
+ List <ChangeData > stagedChanges ) {
388
+ ObjectId reusableHead = null ;
389
+ logger . atInfo (). log ( "Finding reusable staging commit" );
376
390
if (stagingHead .equals (branchHead )) return branchHead ;
377
391
378
- ObjectId reusableHead = null ;
379
- RevWalk revWalk = new RevWalk (git );
380
- RevCommit branch = revWalk .parseCommit (branchHead );
381
- RevCommit commit = revWalk .parseCommit (stagingHead );
382
- if (!revWalk .isMergedInto (branch , commit )) return branchHead ;
392
+ try {
393
+ RevWalk revWalk = new RevWalk (git );
394
+ RevCommit branch = revWalk .parseCommit (branchHead );
395
+ RevCommit commit = revWalk .parseCommit (stagingHead );
396
+ logger .atInfo ().log ("Branch head: " + branch .name ());
397
+ logger .atInfo ().log ("Staging head: " + commit .name ());
398
+
399
+ if (!revWalk .isMergedInto (branch , commit )) return branchHead ;
400
+
401
+ int count = 0 ;
402
+ do {
403
+ count ++;
404
+ String changeId = getChangeId (commit );
405
+ ChangeData change = findChangeFromList (changeId , stagedChanges );
406
+ if (change != null ) {
407
+ if (reusableHead == null ) reusableHead = commit ;
408
+ } else reusableHead = null ;
409
+
410
+ if (commit .getParentCount () > 0 ) {
411
+ // It can always be trusted that parent in index 0 is the correct one
412
+ commit = revWalk .parseCommit (commit .getParent (0 ));
413
+ } else commit = null ;
414
+ } while (commit != null && !commit .equals (branchHead ) && count < 100 );
415
+
416
+ if (count == 100 ) throw new Exception ("can't find ref, too many commits" );
417
+ } catch (Exception e ) {
418
+ reusableHead = null ;
419
+ logger .atSevere ().log ("Finding reusable staging commit failed: %s" , e .getMessage ());
420
+ }
421
+
422
+ if (reusableHead == null ) {
423
+ reusableHead = branchHead ;
424
+ logger .atInfo ().log ("Reusable staging commit not found" );
425
+ }
383
426
384
- int count = 0 ;
385
- do {
386
- count ++;
387
- String changeId = getChangeId (commit );
388
- ChangeData change = findChangeFromList (changeId , stagedChanges );
389
- if (change != null ) {
390
- if (reusableHead == null ) reusableHead = commit ;
391
- } else reusableHead = null ;
392
-
393
- if (commit .getParentCount () > 0 ) {
394
- // It can always be trusted that parent in index 0 is the correct one
395
- commit = revWalk .parseCommit (commit .getParent (0 ));
396
- } else commit = null ;
397
-
398
- } while (commit != null && !commit .equals (branchHead ) && count < 100 );
399
-
400
- if (reusableHead == null ) reusableHead = branchHead ;
401
427
return reusableHead ;
402
428
}
403
429
@@ -416,6 +442,8 @@ public void rebuildStagingBranch(
416
442
ObjectId newStageRef = null ;
417
443
String stagingBranchName = null ;
418
444
445
+ logger .atInfo ().log ("Rebuilding %s" , stagingBranchKey .branch ());
446
+
419
447
try {
420
448
stagingBranchName = stagingBranchKey .branch ();
421
449
oldStageRef = git .resolve (stagingBranchName );
@@ -425,33 +453,31 @@ public void rebuildStagingBranch(
425
453
changes_staged = query .byBranchStatus (destBranchShortKey , Change .Status .STAGED );
426
454
} catch (IOException e ) {
427
455
logger .atSevere ().log (
428
- "qtcodereview: rebuild staging ref %s db query failed. Exception %s" ,
456
+ "rebuild staging ref %s db query failed. Exception %s" ,
429
457
stagingBranchKey , e );
430
458
throw new MergeConflictException ("fatal: " + e .getMessage ());
431
459
}
432
460
433
461
try {
434
- logger .atInfo ().log (
435
- "qtcodereview: rebuild staging ref reset %s back to %s" ,
436
- stagingBranchKey , destBranchShortKey );
462
+ logger .atInfo ().log ("staging ref %s reseted to %s" , stagingBranchKey . branch (),
463
+ destBranchShortKey . branch ());
464
+
437
465
Result result = QtUtil .createStagingBranch (git , destBranchShortKey );
438
466
if (result == null )
439
- throw new NoSuchRefException ("Cannot create staging ref: " + stagingBranchName );
440
- logger .atInfo ().log (
441
- "qtcodereview: rebuild staging ref reset to %s with result %s" , branchRef , result );
467
+ throw new NoSuchRefException ("Cannot create staging ref %s" + stagingBranchName );
468
+
442
469
newStageRef = findReusableStagingHead (git , oldStageRef , branchRef , changes_staged );
443
- logger .atInfo ().log ("qtcodereview: rebuild staging reused staging ref is %s" , newStageRef );
470
+ logger .atInfo ().log ("reused staging ref is %s" , newStageRef .name ());
471
+
444
472
changes_to_cherrypick = arrangeOrderLikeInRef (git , oldStageRef , newStageRef , changes_staged );
445
- } catch (NoSuchRefException | IOException e ) {
446
- logger .atSevere ().log (
447
- "qtcodereview: rebuild staging ref reset %s failed. Exception %s" , stagingBranchKey , e );
448
- throw new MergeConflictException ("fatal: " + e .getMessage ());
449
- }
473
+ String changeStr = "" ;
474
+ for (ChangeData item : changes_to_cherrypick ) changeStr += " " + item .change ().getKey ();
475
+ logger .atInfo ().log ("changes to be cherry-picked: %s" , changeStr );
450
476
451
- try {
452
477
newStageRef = pickChangesToStagingRef (git , projectKey , changes_to_cherrypick , newStageRef );
453
478
} catch (Exception e ) {
454
- logger .atInfo ().log ("qtcodereview: rebuild staging ref %s merge conflict" , stagingBranchKey );
479
+ logger .atSevere ().log ("rebuild staging ref %s failed: %s" , stagingBranchKey .branch (),
480
+ e .getMessage ());
455
481
newStageRef = branchRef ;
456
482
String message =
457
483
"Merge conflict in staging branch. Status changed back to new. Please stage again." ;
@@ -461,14 +487,13 @@ public void rebuildStagingBranch(
461
487
try (BatchUpdate u = updateFactory .create (projectKey , user , TimeUtil .nowTs ())) {
462
488
for (ChangeData item : changes_staged ) {
463
489
Change change = item .change ();
464
- logger .atInfo ().log (
465
- "qtcodereview: staging ref rebuild merge conflict. Change %s back to NEW" , change );
490
+ logger .atInfo ().log ("change %s back to NEW" , change .getKey ());
466
491
u .addOp (change .getId (), op );
467
492
}
468
493
u .execute ();
494
+
469
495
} catch (UpdateException | RestApiException ex ) {
470
- logger .atSevere ().log (
471
- "qtcodereview: staging ref rebuild. Failed to update change status %s" , ex );
496
+ logger .atSevere ().log ("Failed to update change status %s" , ex );
472
497
}
473
498
474
499
for (ChangeData item : changes_staged ) {
@@ -482,6 +507,7 @@ public void rebuildStagingBranch(
482
507
RefUpdate refUpdate = git .updateRef (stagingBranchName );
483
508
refUpdate .setNewObjectId (newStageRef );
484
509
refUpdate .update ();
510
+ logger .atInfo ().log ("Ref %s points now to %s" , stagingBranchKey .branch (), newStageRef .name ());
485
511
486
512
// send ref updated event only if it changed
487
513
if (!newStageRef .equals (oldStageRef )) {
0 commit comments