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:
@@ -26,6 +26,7 @@ import com.google.gerrit.client.reviewdb.PatchSetApproval;
|
|||||||
import com.google.gerrit.client.reviewdb.Project;
|
import com.google.gerrit.client.reviewdb.Project;
|
||||||
import com.google.gerrit.client.reviewdb.ReviewDb;
|
import com.google.gerrit.client.reviewdb.ReviewDb;
|
||||||
import com.google.gerrit.server.ChangeUtil;
|
import com.google.gerrit.server.ChangeUtil;
|
||||||
|
import com.google.gerrit.server.GerritPersonIdent;
|
||||||
import com.google.gerrit.server.GerritServer;
|
import com.google.gerrit.server.GerritServer;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.config.CanonicalWebUrl;
|
import com.google.gerrit.server.config.CanonicalWebUrl;
|
||||||
@@ -143,6 +144,7 @@ public class MergeOp {
|
|||||||
@CanonicalWebUrl @Nullable final Provider<String> cwu,
|
@CanonicalWebUrl @Nullable final Provider<String> cwu,
|
||||||
final ApprovalTypes approvalTypes, final PatchSetInfoFactory psif,
|
final ApprovalTypes approvalTypes, final PatchSetInfoFactory psif,
|
||||||
final IdentifiedUser.GenericFactory iuf,
|
final IdentifiedUser.GenericFactory iuf,
|
||||||
|
@GerritPersonIdent final PersonIdent myIdent,
|
||||||
@Assisted final Branch.NameKey branch) {
|
@Assisted final Branch.NameKey branch) {
|
||||||
server = gs;
|
server = gs;
|
||||||
schemaFactory = sf;
|
schemaFactory = sf;
|
||||||
@@ -156,7 +158,7 @@ public class MergeOp {
|
|||||||
patchSetInfoFactory = psif;
|
patchSetInfoFactory = psif;
|
||||||
identifiedUserFactory = iuf;
|
identifiedUserFactory = iuf;
|
||||||
|
|
||||||
myIdent = server.newGerritPersonIdent();
|
this.myIdent = myIdent;
|
||||||
destBranch = branch;
|
destBranch = branch;
|
||||||
toMerge = new ArrayList<CodeReviewCommit>();
|
toMerge = new ArrayList<CodeReviewCommit>();
|
||||||
status = new HashMap<Change.Id, CommitMergeStatus>();
|
status = new HashMap<Change.Id, CommitMergeStatus>();
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -27,10 +27,8 @@ import org.spearce.jgit.errors.RepositoryNotFoundException;
|
|||||||
import org.spearce.jgit.lib.Config;
|
import org.spearce.jgit.lib.Config;
|
||||||
import org.spearce.jgit.lib.Constants;
|
import org.spearce.jgit.lib.Constants;
|
||||||
import org.spearce.jgit.lib.LockFile;
|
import org.spearce.jgit.lib.LockFile;
|
||||||
import org.spearce.jgit.lib.PersonIdent;
|
|
||||||
import org.spearce.jgit.lib.Repository;
|
import org.spearce.jgit.lib.Repository;
|
||||||
import org.spearce.jgit.lib.RepositoryCache;
|
import org.spearce.jgit.lib.RepositoryCache;
|
||||||
import org.spearce.jgit.lib.UserConfig;
|
|
||||||
import org.spearce.jgit.lib.RepositoryCache.FileKey;
|
import org.spearce.jgit.lib.RepositoryCache.FileKey;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -41,14 +39,12 @@ import java.io.IOException;
|
|||||||
public class GerritServer {
|
public class GerritServer {
|
||||||
private static final Logger log = LoggerFactory.getLogger(GerritServer.class);
|
private static final Logger log = LoggerFactory.getLogger(GerritServer.class);
|
||||||
private final File sitePath;
|
private final File sitePath;
|
||||||
private final Config gerritConfigFile;
|
|
||||||
private final File basepath;
|
private final File basepath;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
GerritServer(final SystemConfig sConfig, @SitePath final File path,
|
GerritServer(final SystemConfig sConfig, @SitePath final File path,
|
||||||
@GerritServerConfig final Config cfg, final AuthConfig authConfig) {
|
@GerritServerConfig final Config cfg, final AuthConfig authConfig) {
|
||||||
sitePath = path;
|
sitePath = path;
|
||||||
gerritConfigFile = cfg;
|
|
||||||
|
|
||||||
final String basePath = cfg.getString("gerrit", null, "basepath");
|
final String basePath = cfg.getString("gerrit", null, "basepath");
|
||||||
if (basePath != null) {
|
if (basePath != null) {
|
||||||
@@ -62,10 +58,6 @@ public class GerritServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Config getGerritConfig() {
|
|
||||||
return gerritConfigFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get (or open) a repository by name.
|
* Get (or open) a repository by name.
|
||||||
*
|
*
|
||||||
@@ -183,16 +175,4 @@ public class GerritServer {
|
|||||||
return false; // is a reasonable name
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ import com.google.gerrit.git.ReplicationQueue;
|
|||||||
import com.google.gerrit.git.WorkQueue;
|
import com.google.gerrit.git.WorkQueue;
|
||||||
import com.google.gerrit.server.AnonymousUser;
|
import com.google.gerrit.server.AnonymousUser;
|
||||||
import com.google.gerrit.server.FileTypeRegistry;
|
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.GerritServer;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.MimeUtilFileTypeRegistry;
|
import com.google.gerrit.server.MimeUtilFileTypeRegistry;
|
||||||
@@ -63,6 +65,7 @@ import com.google.inject.Injector;
|
|||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
|
|
||||||
import org.spearce.jgit.lib.Config;
|
import org.spearce.jgit.lib.Config;
|
||||||
|
import org.spearce.jgit.lib.PersonIdent;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -118,6 +121,9 @@ public class GerritGlobalModule extends FactoryModule {
|
|||||||
bind(String.class).annotatedWith(CanonicalWebUrl.class).toProvider(
|
bind(String.class).annotatedWith(CanonicalWebUrl.class).toProvider(
|
||||||
CanonicalWebUrlProvider.class);
|
CanonicalWebUrlProvider.class);
|
||||||
|
|
||||||
|
bind(PersonIdent.class).annotatedWith(GerritPersonIdent.class).toProvider(
|
||||||
|
GerritPersonIdentProvider.class);
|
||||||
|
|
||||||
bind(CachePool.class);
|
bind(CachePool.class);
|
||||||
install(AccountByEmailCache.module());
|
install(AccountByEmailCache.module());
|
||||||
install(AccountCache.module());
|
install(AccountCache.module());
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import com.google.gerrit.client.reviewdb.PatchSetInfo;
|
|||||||
import com.google.gerrit.client.reviewdb.ReviewDb;
|
import com.google.gerrit.client.reviewdb.ReviewDb;
|
||||||
import com.google.gerrit.client.reviewdb.StarredChange;
|
import com.google.gerrit.client.reviewdb.StarredChange;
|
||||||
import com.google.gerrit.client.reviewdb.UserIdentity;
|
import com.google.gerrit.client.reviewdb.UserIdentity;
|
||||||
|
import com.google.gerrit.server.GerritPersonIdent;
|
||||||
import com.google.gerrit.server.GerritServer;
|
import com.google.gerrit.server.GerritServer;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.account.AccountCache;
|
import com.google.gerrit.server.account.AccountCache;
|
||||||
@@ -104,6 +105,11 @@ public abstract class OutgoingEmail {
|
|||||||
@CanonicalWebUrl
|
@CanonicalWebUrl
|
||||||
@Nullable
|
@Nullable
|
||||||
private Provider<String> urlProvider;
|
private Provider<String> urlProvider;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@GerritPersonIdent
|
||||||
|
private PersonIdent gerritIdent;
|
||||||
|
|
||||||
private ProjectState projectState;
|
private ProjectState projectState;
|
||||||
|
|
||||||
protected OutgoingEmail(final Change c, final String mc) {
|
protected OutgoingEmail(final Change c, final String mc) {
|
||||||
@@ -260,8 +266,7 @@ public abstract class OutgoingEmail {
|
|||||||
return toAddress(fromId);
|
return toAddress(fromId);
|
||||||
}
|
}
|
||||||
|
|
||||||
final PersonIdent pi = server.newGerritPersonIdent();
|
return new Address(gerritIdent.getName(), gerritIdent.getEmailAddress());
|
||||||
return new Address(pi.getName(), pi.getEmailAddress());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setListIdHeader() {
|
private void setListIdHeader() {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import com.google.gerrit.client.rpc.NoSuchAccountException;
|
|||||||
import com.google.gerrit.git.PatchSetImporter;
|
import com.google.gerrit.git.PatchSetImporter;
|
||||||
import com.google.gerrit.git.ReplicationQueue;
|
import com.google.gerrit.git.ReplicationQueue;
|
||||||
import com.google.gerrit.server.ChangeUtil;
|
import com.google.gerrit.server.ChangeUtil;
|
||||||
|
import com.google.gerrit.server.GerritPersonIdent;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.account.AccountResolver;
|
import com.google.gerrit.server.account.AccountResolver;
|
||||||
import com.google.gerrit.server.config.CanonicalWebUrl;
|
import com.google.gerrit.server.config.CanonicalWebUrl;
|
||||||
@@ -160,6 +161,10 @@ final class Receive extends AbstractGitCommand {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String canonicalWebUrl;
|
private String canonicalWebUrl;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@GerritPersonIdent
|
||||||
|
private PersonIdent gerritIdent;
|
||||||
|
|
||||||
private ReceivePack rp;
|
private ReceivePack rp;
|
||||||
private PersonIdent refLogIdent;
|
private PersonIdent refLogIdent;
|
||||||
private ReceiveCommand newChange;
|
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
|
// This seems to happen all too often, due to users not paying any
|
||||||
// attention to what they are doing.
|
// attention to what they are doing.
|
||||||
//
|
//
|
||||||
final PersonIdent serverIdent = server.newGerritPersonIdent();
|
|
||||||
if (c.getParentCount() > 1
|
if (c.getParentCount() > 1
|
||||||
&& author.getName().equals(serverIdent.getName())
|
&& author.getName().equals(gerritIdent.getName())
|
||||||
&& author.getEmailAddress().equals(serverIdent.getEmailAddress())) {
|
&& author.getEmailAddress().equals(gerritIdent.getEmailAddress())) {
|
||||||
reject(cmd, "do not amend merges not made by you");
|
reject(cmd, "do not amend merges not made by you");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user