Browse Source

Change techique for custom properties to fall in line with reading / writing of all other properties

tags/v1.0.0
John Crygier 12 years ago
parent
commit
4a5a55229b

+ 4
- 0
src/com/gitblit/Constants.java View File

@@ -72,6 +72,10 @@ public class Constants {
public static final String DEFAULT_BRANCH = "default";
public static String CUSTOM_DEFINED_PROP_SECTION = "gitblit";
public static String CUSTOM_DEFINED_PROP_SUBSECTION = "customDefinedProperties";
public static String getGitBlitVersion() {
return NAME + " v" + VERSION;
}

+ 11
- 0
src/com/gitblit/GitBlit.java View File

@@ -857,6 +857,12 @@ public class GitBlit implements ServletContextListener {
"gitblit", null, "mailingList")));
model.indexedBranches = new ArrayList<String>(Arrays.asList(config.getStringList(
"gitblit", null, "indexBranch")));
// Custom defined properties
model.userDefinedProperties = new HashMap<String, String>();
for (String aProperty : config.getNames(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION)) {
model.userDefinedProperties.put(aProperty, config.getString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, aProperty));
}
}
model.HEAD = JGitUtils.getHEADRef(r);
model.availableRefs = JGitUtils.getAvailableHeadTargets(r);
@@ -1103,6 +1109,11 @@ public class GitBlit implements ServletContextListener {
updateList(config, "postReceiveScript", repository.postReceiveScripts);
updateList(config, "mailingList", repository.mailingLists);
updateList(config, "indexBranch", repository.indexedBranches);
// User Defined Properties
for (Entry<String, String> singleProperty : repository.userDefinedProperties.entrySet()) {
config.setString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, singleProperty.getKey(), singleProperty.getValue());
}
try {
config.save();

+ 2
- 43
src/com/gitblit/models/RepositoryModel.java View File

@@ -19,17 +19,11 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.FederationStrategy;
import com.gitblit.GitBlit;
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.StringUtils;
/**
@@ -42,11 +36,6 @@ import com.gitblit.utils.StringUtils;
public class RepositoryModel implements Serializable, Comparable<RepositoryModel> {
private static final long serialVersionUID = 1L;
public static String CUSTOM_DEFINED_PROP_SECTION = "gitblit";
public static String CUSTOM_DEFINED_PROP_SUBSECTION = "customDefinedProperties";
private final Logger logger = LoggerFactory.getLogger(RepositoryModel.class);
// field names are reflectively mapped in EditRepository page
public String name;
@@ -75,6 +64,7 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel
public List<String> preReceiveScripts;
public List<String> postReceiveScripts;
public List<String> mailingLists;
public Map<String, String> userDefinedProperties;
private String displayName;
public RepositoryModel() {
@@ -103,37 +93,6 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel
}
return localBranches;
}
public String getCustomProperty(String propertyKey) {
try {
Repository r = GitBlit.self().getRepository(name);
StoredConfig config = JGitUtils.readConfig(r);
return config.getString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey);
} catch (Exception e) {
logger.error("Error getting Custom Property", e);
return null;
}
}
public String setCustomProperty(String propertyKey, String propertyValue) {
try {
Repository r = GitBlit.self().getRepository(name);
StoredConfig config = JGitUtils.readConfig(r);
String oldValue = config.getString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey);
config.setString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey, propertyValue);
config.save();
return oldValue;
} catch (Exception e) {
logger.error("Error getting Custom Property", e);
return null;
}
}
@Override
public String toString() {

+ 23
- 18
tests/com/gitblit/tests/RepositoryModelTest.java View File

@@ -1,6 +1,6 @@
package com.gitblit.tests;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
@@ -10,6 +10,7 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.gitblit.Constants;
import com.gitblit.GitBlit;
import com.gitblit.models.RepositoryModel;
import com.gitblit.utils.JGitUtils;
@@ -24,11 +25,11 @@ public class RepositoryModelTest {
public static void startGitBlit() throws Exception {
wasStarted = GitBlitSuite.startGitblit() == false;
oldSection = RepositoryModel.CUSTOM_DEFINED_PROP_SECTION;
oldSubSection = RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION;
oldSection = Constants.CUSTOM_DEFINED_PROP_SECTION;
oldSubSection = Constants.CUSTOM_DEFINED_PROP_SUBSECTION;
RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = "RepositoryModelTest";
RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = "RepositoryModelTestSubSection";
Constants.CUSTOM_DEFINED_PROP_SECTION = "RepositoryModelTest";
Constants.CUSTOM_DEFINED_PROP_SUBSECTION = "RepositoryModelTestSubSection";
}
@AfterClass
@@ -36,8 +37,8 @@ public class RepositoryModelTest {
if (wasStarted == false)
GitBlitSuite.stopGitblit();
RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = oldSection;
RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = oldSubSection;
Constants.CUSTOM_DEFINED_PROP_SECTION = oldSection;
Constants.CUSTOM_DEFINED_PROP_SUBSECTION = oldSubSection;
}
@Before
@@ -45,9 +46,9 @@ public class RepositoryModelTest {
Repository r = GitBlitSuite.getHelloworldRepository();
StoredConfig config = JGitUtils.readConfig(r);
config.unsetSection(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION);
config.setString(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION, "commitMessageRegEx", "\\d");
config.setString(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION, "anotherProperty", "Hello");
config.unsetSection(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION);
config.setString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, "commitMessageRegEx", "\\d");
config.setString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, "anotherProperty", "Hello");
config.save();
}
@@ -57,7 +58,7 @@ public class RepositoryModelTest {
Repository r = GitBlitSuite.getHelloworldRepository();
StoredConfig config = JGitUtils.readConfig(r);
config.unsetSection(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION);
config.unsetSection(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION);
config.save();
}

@@ -66,8 +67,8 @@ public class RepositoryModelTest {
RepositoryModel model = GitBlit.self().getRepositoryModel(
GitBlitSuite.getHelloworldRepository().getDirectory().getName());
assertEquals("\\d", model.getCustomProperty("commitMessageRegEx"));
assertEquals("Hello", model.getCustomProperty("anotherProperty"));
assertEquals("\\d", model.userDefinedProperties.get("commitMessageRegEx"));
assertEquals("Hello", model.userDefinedProperties.get("anotherProperty"));
}
@Test
@@ -75,13 +76,17 @@ public class RepositoryModelTest {
RepositoryModel model = GitBlit.self().getRepositoryModel(
GitBlitSuite.getHelloworldRepository().getDirectory().getName());
assertEquals("\\d", model.getCustomProperty("commitMessageRegEx"));
assertEquals("Hello", model.getCustomProperty("anotherProperty"));
assertEquals("\\d", model.userDefinedProperties.get("commitMessageRegEx"));
assertEquals("Hello", model.userDefinedProperties.get("anotherProperty"));
assertEquals("Hello", model.setCustomProperty("anotherProperty", "GoodBye"));
assertEquals("Hello", model.userDefinedProperties.put("anotherProperty", "GoodBye"));
GitBlit.self().updateRepositoryModel(model.name, model, false);
assertEquals("\\d", model.getCustomProperty("commitMessageRegEx"));
assertEquals("GoodBye", model.getCustomProperty("anotherProperty"));
model = GitBlit.self().getRepositoryModel(
GitBlitSuite.getHelloworldRepository().getDirectory().getName());
assertEquals("\\d", model.userDefinedProperties.get("commitMessageRegEx"));
assertEquals("GoodBye", model.userDefinedProperties.get("anotherProperty"));
}

}

Loading…
Cancel
Save