Moved creation of GerritPersonIdent to a separate provider.

It's first step to make GerritServer focused on providing one specific
functionality.

Since now classes that need PersonIdent that represents Gerrit itself can
ask for it using @GerritPersonIdent annotation or by depending on Provider
instance and calling get() method.

Change-Id: I9be482c6afdfc5cf4ab44efcb9262a04b3a543a5
Signed-off-by: Grzegorz Kossakowski <grek@google.com>
This commit is contained in:
Grzegorz Kossakowski
2009-09-09 19:09:26 -07:00
parent 43b3d1a9ad
commit 5490b1c037
7 changed files with 100 additions and 26 deletions

View File

@@ -26,6 +26,7 @@ import com.google.gerrit.client.reviewdb.PatchSetApproval;
import com.google.gerrit.client.reviewdb.Project;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.GerritServer;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.config.CanonicalWebUrl;
@@ -143,6 +144,7 @@ public class MergeOp {
@CanonicalWebUrl @Nullable final Provider<String> cwu,
final ApprovalTypes approvalTypes, final PatchSetInfoFactory psif,
final IdentifiedUser.GenericFactory iuf,
@GerritPersonIdent final PersonIdent myIdent,
@Assisted final Branch.NameKey branch) {
server = gs;
schemaFactory = sf;
@@ -156,7 +158,7 @@ public class MergeOp {
patchSetInfoFactory = psif;
identifiedUserFactory = iuf;
myIdent = server.newGerritPersonIdent();
this.myIdent = myIdent;
destBranch = branch;
toMerge = new ArrayList<CodeReviewCommit>();
status = new HashMap<Change.Id, CommitMergeStatus>();

View File

@@ -0,0 +1,30 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
/**
* Marker on a {@link org.spearce.jgit.lib.PersonIdent} pointing to the identity representing Gerrit
* server itself.
*/
@Retention(RUNTIME)
@BindingAnnotation
public @interface GerritPersonIdent {
}

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.spearce.jgit.lib.Config;
import org.spearce.jgit.lib.PersonIdent;
import org.spearce.jgit.lib.UserConfig;
/** Provides {@link PersonIdent} annotated with {@link GerritPersonIdent}. */
@Singleton
public class GerritPersonIdentProvider implements Provider<PersonIdent> {
private final String name;
private final String email;
@Inject
public GerritPersonIdentProvider(@GerritServerConfig final Config cfg) {
String name = cfg.getString("user", null, "name");
if (name == null) {
name = "Gerrit Code Review";
}
this.name = name;
email = cfg.get(UserConfig.KEY).getCommitterEmail();
}
@Override
public PersonIdent get() {
return new PersonIdent(name, email);
}
}

View File

@@ -27,10 +27,8 @@ import org.spearce.jgit.errors.RepositoryNotFoundException;
import org.spearce.jgit.lib.Config;
import org.spearce.jgit.lib.Constants;
import org.spearce.jgit.lib.LockFile;
import org.spearce.jgit.lib.PersonIdent;
import org.spearce.jgit.lib.Repository;
import org.spearce.jgit.lib.RepositoryCache;
import org.spearce.jgit.lib.UserConfig;
import org.spearce.jgit.lib.RepositoryCache.FileKey;
import java.io.File;
@@ -41,14 +39,12 @@ import java.io.IOException;
public class GerritServer {
private static final Logger log = LoggerFactory.getLogger(GerritServer.class);
private final File sitePath;
private final Config gerritConfigFile;
private final File basepath;
@Inject
GerritServer(final SystemConfig sConfig, @SitePath final File path,
@GerritServerConfig final Config cfg, final AuthConfig authConfig) {
sitePath = path;
gerritConfigFile = cfg;
final String basePath = cfg.getString("gerrit", null, "basepath");
if (basePath != null) {
@@ -62,10 +58,6 @@ public class GerritServer {
}
}
private Config getGerritConfig() {
return gerritConfigFile;
}
/**
* Get (or open) a repository by name.
*
@@ -183,16 +175,4 @@ public class GerritServer {
return false; // is a reasonable name
}
/** Get a new identity representing this Gerrit server in Git. */
public PersonIdent newGerritPersonIdent() {
String name = getGerritConfig().getString("user", null, "name");
if (name == null) {
name = "Gerrit Code Review";
}
String email = getGerritConfig().get(UserConfig.KEY).getCommitterEmail();
if (email == null || email.length() == 0) {
email = "gerrit@localhost";
}
return new PersonIdent(name, email);
}
}

View File

@@ -30,6 +30,8 @@ import com.google.gerrit.git.ReplicationQueue;
import com.google.gerrit.git.WorkQueue;
import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.FileTypeRegistry;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.GerritPersonIdentProvider;
import com.google.gerrit.server.GerritServer;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.MimeUtilFileTypeRegistry;
@@ -63,6 +65,7 @@ import com.google.inject.Injector;
import com.google.inject.Module;
import org.spearce.jgit.lib.Config;
import org.spearce.jgit.lib.PersonIdent;
import java.util.ArrayList;
import java.util.List;
@@ -118,6 +121,9 @@ public class GerritGlobalModule extends FactoryModule {
bind(String.class).annotatedWith(CanonicalWebUrl.class).toProvider(
CanonicalWebUrlProvider.class);
bind(PersonIdent.class).annotatedWith(GerritPersonIdent.class).toProvider(
GerritPersonIdentProvider.class);
bind(CachePool.class);
install(AccountByEmailCache.module());
install(AccountCache.module());

View File

@@ -25,6 +25,7 @@ import com.google.gerrit.client.reviewdb.PatchSetInfo;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.reviewdb.StarredChange;
import com.google.gerrit.client.reviewdb.UserIdentity;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.GerritServer;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache;
@@ -104,6 +105,11 @@ public abstract class OutgoingEmail {
@CanonicalWebUrl
@Nullable
private Provider<String> urlProvider;
@Inject
@GerritPersonIdent
private PersonIdent gerritIdent;
private ProjectState projectState;
protected OutgoingEmail(final Change c, final String mc) {
@@ -260,8 +266,7 @@ public abstract class OutgoingEmail {
return toAddress(fromId);
}
final PersonIdent pi = server.newGerritPersonIdent();
return new Address(pi.getName(), pi.getEmailAddress());
return new Address(gerritIdent.getName(), gerritIdent.getEmailAddress());
}
private void setListIdHeader() {

View File

@@ -42,6 +42,7 @@ import com.google.gerrit.client.rpc.NoSuchAccountException;
import com.google.gerrit.git.PatchSetImporter;
import com.google.gerrit.git.ReplicationQueue;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.config.CanonicalWebUrl;
@@ -160,6 +161,10 @@ final class Receive extends AbstractGitCommand {
@Nullable
private String canonicalWebUrl;
@Inject
@GerritPersonIdent
private PersonIdent gerritIdent;
private ReceivePack rp;
private PersonIdent refLogIdent;
private ReceiveCommand newChange;
@@ -1171,10 +1176,9 @@ final class Receive extends AbstractGitCommand {
// This seems to happen all too often, due to users not paying any
// attention to what they are doing.
//
final PersonIdent serverIdent = server.newGerritPersonIdent();
if (c.getParentCount() > 1
&& author.getName().equals(serverIdent.getName())
&& author.getEmailAddress().equals(serverIdent.getEmailAddress())) {
&& author.getName().equals(gerritIdent.getName())
&& author.getEmailAddress().equals(gerritIdent.getEmailAddress())) {
reject(cmd, "do not amend merges not made by you");
return false;
}