Make diff preferences defaults configurable
Read diff preferences from All-Users refs/users/default and update the default values with those preferences. Change-Id: I813ad9976f7e73190fb68f373e77f596dcd6537c
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
package com.google.gerrit.server.account;
|
||||
|
||||
import static com.google.gerrit.server.config.ConfigUtil.loadSection;
|
||||
import static com.google.gerrit.server.config.ConfigUtil.skipField;
|
||||
|
||||
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
@@ -31,11 +32,17 @@ import com.google.inject.Singleton;
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
@Singleton
|
||||
public class GetDiffPreferences implements RestReadView<AccountResource> {
|
||||
private static final Logger log =
|
||||
LoggerFactory.getLogger(GetDiffPreferences.class);
|
||||
|
||||
private final Provider<CurrentUser> self;
|
||||
private final Provider<AllUsersName> allUsersName;
|
||||
private final GitRepositoryManager gitMgr;
|
||||
@@ -66,13 +73,41 @@ public class GetDiffPreferences implements RestReadView<AccountResource> {
|
||||
DiffPreferencesInfo in)
|
||||
throws IOException, ConfigInvalidException, RepositoryNotFoundException {
|
||||
try (Repository git = gitMgr.openRepository(allUsersName)) {
|
||||
// Load all users prefs.
|
||||
VersionedAccountPreferences dp =
|
||||
VersionedAccountPreferences.forDefault();
|
||||
dp.load(git);
|
||||
DiffPreferencesInfo allUserPrefs = new DiffPreferencesInfo();
|
||||
loadSection(dp.getConfig(), UserConfigSections.DIFF, null, allUserPrefs,
|
||||
DiffPreferencesInfo.defaults(), in);
|
||||
|
||||
// Load user prefs
|
||||
VersionedAccountPreferences p =
|
||||
VersionedAccountPreferences.forUser(id);
|
||||
p.load(git);
|
||||
DiffPreferencesInfo prefs = new DiffPreferencesInfo();
|
||||
loadSection(p.getConfig(), UserConfigSections.DIFF, null, prefs,
|
||||
DiffPreferencesInfo.defaults(), in);
|
||||
updateDefaults(allUserPrefs), in);
|
||||
return prefs;
|
||||
}
|
||||
}
|
||||
|
||||
private static DiffPreferencesInfo updateDefaults(DiffPreferencesInfo input) {
|
||||
DiffPreferencesInfo result = DiffPreferencesInfo.defaults();
|
||||
try {
|
||||
for (Field field : input.getClass().getDeclaredFields()) {
|
||||
if (skipField(field)) {
|
||||
continue;
|
||||
}
|
||||
Object newVal = field.get(input);
|
||||
if (newVal != null) {
|
||||
field.set(result, newVal);
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
log.warn("Cannot get default diff preferences from All-Users", e);
|
||||
return DiffPreferencesInfo.defaults();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,16 +382,16 @@ public class ConfigUtil {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static boolean skipField(Field field) {
|
||||
int modifiers = field.getModifiers();
|
||||
return Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers);
|
||||
}
|
||||
|
||||
private static boolean isCollectionOrMap(Class<?> t) {
|
||||
return Collection.class.isAssignableFrom(t)
|
||||
|| Map.class.isAssignableFrom(t);
|
||||
}
|
||||
|
||||
private static boolean skipField(Field field) {
|
||||
int modifiers = field.getModifiers();
|
||||
return Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers);
|
||||
}
|
||||
|
||||
private static boolean isString(Class<?> t) {
|
||||
return String.class == t;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user