Skip to content

Commit 2e518c7

Browse files
committed
Refactor logging and exception handling when rebuilding staging ref
Change-Id: I613036582ef285b3bcff0c69dcc078b8beaf5274 Reviewed-by: Daniel Smith <[email protected]>
1 parent a222a78 commit 2e518c7

File tree

1 file changed

+111
-85
lines changed
  • src/main/java/com/googlesource/gerrit/plugins/qtcodereview

1 file changed

+111
-85
lines changed

src/main/java/com/googlesource/gerrit/plugins/qtcodereview/QtUtil.java

Lines changed: 111 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -308,59 +308,73 @@ private ChangeData findChangeFromList(String changeId, List<ChangeData> changes)
308308
// Step backwards from the ref and return change list in the same order
309309
private List<ChangeData> arrangeOrderLikeInRef(
310310
Repository git, ObjectId refObj, ObjectId tipObj, List<ChangeData> changeList)
311-
throws MissingObjectException, IOException {
311+
throws Exception {
312312
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);
318313
int count = 0;
319-
do {
320-
count++;
321-
String changeId = getChangeId(commit);
314+
logger.atInfo().log("Arranging change order to match original");
322315

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));
328338
}
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+
}
331343

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");
336345

337-
if (count == 100) return null;
338346
return results;
339347
}
340348

341349
private ObjectId pickChangesToStagingRef(
342350
Repository git, final Project.NameKey projectKey, List<ChangeData> changes, ObjectId tipObj)
343-
throws IOException, IntegrationException {
351+
throws Exception {
344352
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());
364378
}
365379
return newId;
366380
}
@@ -370,34 +384,46 @@ private ObjectId findReusableStagingHead(
370384
Repository git,
371385
ObjectId stagingHead,
372386
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");
376390
if (stagingHead.equals(branchHead)) return branchHead;
377391

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+
}
383426

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;
401427
return reusableHead;
402428
}
403429

@@ -416,6 +442,8 @@ public void rebuildStagingBranch(
416442
ObjectId newStageRef = null;
417443
String stagingBranchName = null;
418444

445+
logger.atInfo().log("Rebuilding %s", stagingBranchKey.branch());
446+
419447
try {
420448
stagingBranchName = stagingBranchKey.branch();
421449
oldStageRef = git.resolve(stagingBranchName);
@@ -425,33 +453,31 @@ public void rebuildStagingBranch(
425453
changes_staged = query.byBranchStatus(destBranchShortKey, Change.Status.STAGED);
426454
} catch (IOException e) {
427455
logger.atSevere().log(
428-
"qtcodereview: rebuild staging ref %s db query failed. Exception %s",
456+
"rebuild staging ref %s db query failed. Exception %s",
429457
stagingBranchKey, e);
430458
throw new MergeConflictException("fatal: " + e.getMessage());
431459
}
432460

433461
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+
437465
Result result = QtUtil.createStagingBranch(git, destBranchShortKey);
438466
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+
442469
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+
444472
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);
450476

451-
try {
452477
newStageRef = pickChangesToStagingRef(git, projectKey, changes_to_cherrypick, newStageRef);
453478
} 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());
455481
newStageRef = branchRef;
456482
String message =
457483
"Merge conflict in staging branch. Status changed back to new. Please stage again.";
@@ -461,14 +487,13 @@ public void rebuildStagingBranch(
461487
try (BatchUpdate u = updateFactory.create(projectKey, user, TimeUtil.nowTs())) {
462488
for (ChangeData item : changes_staged) {
463489
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());
466491
u.addOp(change.getId(), op);
467492
}
468493
u.execute();
494+
469495
} 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);
472497
}
473498

474499
for (ChangeData item : changes_staged) {
@@ -482,6 +507,7 @@ public void rebuildStagingBranch(
482507
RefUpdate refUpdate = git.updateRef(stagingBranchName);
483508
refUpdate.setNewObjectId(newStageRef);
484509
refUpdate.update();
510+
logger.atInfo().log("Ref %s points now to %s", stagingBranchKey.branch(), newStageRef.name());
485511

486512
// send ref updated event only if it changed
487513
if (!newStageRef.equals(oldStageRef)) {

0 commit comments

Comments
 (0)