Default full_name to Git committer identity If a user has no full_name field default the value to the name used in the Git committer line of a newly pushed commit whose email address matches a verified email on the account. This may make it easier for users to get started with Gerrit. A user can create their account, configure their Git client, and upload their first change. Gerrit will automatically pick up the display name from what the user has configured to show in the project's Git history. Change-Id: Iedf20833916a495d22791165b38ba9a7c00bb69e
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java index 487b177..2c8663e 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java
@@ -17,7 +17,6 @@ import static com.google.gerrit.server.git.MultiProgressMonitor.UNKNOWN; import static com.google.gerrit.server.mail.MailUtil.getRecipientsFromApprovals; import static com.google.gerrit.server.mail.MailUtil.getRecipientsFromFooters; - import static org.eclipse.jgit.lib.Constants.R_HEADS; import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED; import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK; @@ -62,6 +61,7 @@ import com.google.gerrit.server.ChangeUtil; import com.google.gerrit.server.GerritPersonIdent; import com.google.gerrit.server.IdentifiedUser; +import com.google.gerrit.server.account.AccountCache; import com.google.gerrit.server.account.AccountResolver; import com.google.gerrit.server.change.ChangeInserter; import com.google.gerrit.server.change.ChangeResource; @@ -262,6 +262,7 @@ private final CommitValidators.Factory commitValidatorsFactory; private final TrackingFooters trackingFooters; private final TagCache tagCache; + private final AccountCache accountCache; private final ChangeInserter.Factory changeInserterFactory; private final WorkQueue workQueue; private final ListeningExecutorService changeUpdateExector; @@ -318,6 +319,7 @@ final ProjectCache projectCache, final GitRepositoryManager repoManager, final TagCache tagCache, + final AccountCache accountCache, final ChangeCache changeCache, final ChangeInserter.Factory changeInserterFactory, final CommitValidators.Factory commitValidatorsFactory, @@ -353,6 +355,7 @@ this.canonicalWebUrl = canonicalWebUrl; this.trackingFooters = trackingFooters; this.tagCache = tagCache; + this.accountCache = accountCache; this.changeInserterFactory = changeInserterFactory; this.commitValidatorsFactory = commitValidatorsFactory; this.workQueue = workQueue; @@ -2055,6 +2058,7 @@ return; } + boolean defaultName = Strings.isNullOrEmpty(currentUser.getAccount().getFullName()); final RevWalk walk = rp.getRevWalk(); walk.reset(); walk.sort(RevSort.NONE); @@ -2070,6 +2074,23 @@ } else if (!validCommit(ctl, cmd, c)) { break; } + + if (defaultName && currentUser.getEmailAddresses().contains( + c.getCommitterIdent().getEmailAddress())) { + try { + Account a = db.accounts().get(currentUser.getAccountId()); + if (a != null && Strings.isNullOrEmpty(a.getFullName())) { + a.setFullName(c.getCommitterIdent().getName()); + db.accounts().update(Collections.singleton(a)); + currentUser.getAccount().setFullName(a.getFullName()); + accountCache.evict(a.getId()); + } + } catch (OrmException e) { + log.warn("Cannot default full_name", e); + } finally { + defaultName = false; + } + } } } catch (IOException err) { cmd.setResult(REJECTED_MISSING_OBJECT);