From e26d9338ddc5f0f0440947e60013a57b82043783 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 30 Oct 2012 17:01:57 -0400
Subject: [PATCH] Refactor GC period into an integer for simpler translations

---
 src/com/gitblit/GitBlit.java                         |   25 +++++++++++-
 src/com/gitblit/wicket/pages/EditRepositoryPage.java |   30 +++++++++++++-
 src/com/gitblit/GCExecutor.java                      |    4 -
 src/com/gitblit/models/RepositoryModel.java          |    2 
 src/com/gitblit/client/EditRepositoryDialog.java     |   15 +++++++
 distrib/gitblit.properties                           |   10 ++--
 6 files changed, 72 insertions(+), 14 deletions(-)

diff --git a/distrib/gitblit.properties b/distrib/gitblit.properties
index 4343229..411699f 100644
--- a/distrib/gitblit.properties
+++ b/distrib/gitblit.properties
@@ -110,6 +110,8 @@
 
 # Enable JGit-based garbage collection. (!!EXPERIMENTAL!!)
 #
+# USE AT YOUR OWN RISK!
+#
 # If enabled, the garbage collection executor scans all repositories once a day
 # at the hour of your choosing.  The GC executor will take each repository "offline",
 # one-at-a-time, to check if the repository satisfies it's GC trigger requirements.
@@ -120,8 +122,6 @@
 # Gitblit's GC Executor MAY NOT PLAY NICE with the other Git kids on the block,
 # especially on Windows systems, so if you are using other tools please coordinate
 # their usage with your GC Executor schedule or do not use this feature.
-#
-# Use this feature at your own risk!
 #
 # The GC algorithm complex and the JGit team advises caution when using their
 # young implementation of GC.
@@ -148,8 +148,8 @@
 # SINCE 1.2.0
 git.defaultGarbageCollectionThreshold = 500k
 
-# The default period between GCs for a repository.  If the total filesize of the
-# loose object exceeds *git.garbageCollectionThreshold* or the repository's
+# The default period, in days, between GCs for a repository.  If the total filesize
+# of the loose object exceeds *git.garbageCollectionThreshold* or the repository's
 # custom threshold, this period will be short-circuited. 
 #
 # e.g. if a repository collects 100KB of loose objects every day with a 500KB
@@ -167,7 +167,7 @@
 # The minimum value is 1 day since the GC Executor only runs once a day.
 #
 # SINCE 1.2.0
-git.defaultGarbageCollectionPeriod = 7 days
+git.defaultGarbageCollectionPeriod = 7
 
 # Number of bytes of a pack file to load into memory in a single read operation.
 # This is the "page size" of the JGit buffer cache, used for all pack access
diff --git a/src/com/gitblit/GCExecutor.java b/src/com/gitblit/GCExecutor.java
index c5fe43b..243cbb9 100644
--- a/src/com/gitblit/GCExecutor.java
+++ b/src/com/gitblit/GCExecutor.java
@@ -33,7 +33,6 @@
 
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.utils.FileUtils;
-import com.gitblit.utils.TimeUtils;
 
 /**
  * The GC executor handles periodic garbage collection in repositories.
@@ -162,14 +161,13 @@
 				RepoStatistics stats = gc.getStatistics();
 				
 				// determine if this is a scheduled GC
-				int gcPeriodInDays = TimeUtils.convertFrequencyToMinutes(model.gcPeriod)/(60*24);
 				Calendar cal = Calendar.getInstance();
 				cal.setTime(model.lastGC);
 				cal.set(Calendar.HOUR_OF_DAY, 0);
 				cal.set(Calendar.MINUTE, 0);
 				cal.set(Calendar.SECOND, 0);
 				cal.set(Calendar.MILLISECOND, 0);
-				cal.add(Calendar.DATE, gcPeriodInDays);
+				cal.add(Calendar.DATE, model.gcPeriod);
 				Date gcDate = cal.getTime();
 				boolean shouldCollectGarbage = now.after(gcDate);
 
diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java
index 6e587ca..e7b7bb9 100644
--- a/src/com/gitblit/GitBlit.java
+++ b/src/com/gitblit/GitBlit.java
@@ -1445,7 +1445,7 @@
 					Constants.CONFIG_GITBLIT, null, "federationSets")));
 			model.isFederated = getConfig(config, "isFederated", false);
 			model.gcThreshold = getConfig(config, "gcThreshold", settings.getString(Keys.git.defaultGarbageCollectionThreshold, "500KB"));
-			model.gcPeriod = getConfig(config, "gcPeriod", settings.getString(Keys.git.defaultGarbageCollectionPeriod, "7 days"));
+			model.gcPeriod = getConfig(config, "gcPeriod", settings.getInteger(Keys.git.defaultGarbageCollectionPeriod, 7));
 			try {
 				model.lastGC = new SimpleDateFormat(Constants.ISO8601).parse(getConfig(config, "lastGC", "1970-01-01'T'00:00:00Z"));
 			} catch (Exception e) {
@@ -1730,6 +1730,27 @@
 	private boolean getConfig(StoredConfig config, String field, boolean defaultValue) {
 		return config.getBoolean(Constants.CONFIG_GITBLIT, field, defaultValue);
 	}
+	
+	/**
+	 * Returns the gitblit string value for the specified key. If key is not
+	 * set, returns defaultValue.
+	 * 
+	 * @param config
+	 * @param field
+	 * @param defaultValue
+	 * @return field value or defaultValue
+	 */
+	private int getConfig(StoredConfig config, String field, int defaultValue) {
+		String value = config.getString(Constants.CONFIG_GITBLIT, null, field);
+		if (StringUtils.isEmpty(value)) {
+			return defaultValue;
+		}
+		try {
+			return Integer.parseInt(value);
+		} catch (Exception e) {
+		}
+		return defaultValue;
+	}
 
 	/**
 	 * Creates/updates the repository model keyed by reopsitoryName. Saves all
@@ -1896,7 +1917,7 @@
 				repository.federationStrategy.name());
 		config.setBoolean(Constants.CONFIG_GITBLIT, null, "isFederated", repository.isFederated);
 		config.setString(Constants.CONFIG_GITBLIT, null, "gcThreshold", repository.gcThreshold);
-		config.setString(Constants.CONFIG_GITBLIT, null, "gcPeriod", repository.gcPeriod);
+		config.setInt(Constants.CONFIG_GITBLIT, null, "gcPeriod", repository.gcPeriod);
 		if (repository.lastGC != null) {
 			config.setString(Constants.CONFIG_GITBLIT, null, "lastGC", new SimpleDateFormat(Constants.ISO8601).format(repository.lastGC));
 		}
diff --git a/src/com/gitblit/client/EditRepositoryDialog.java b/src/com/gitblit/client/EditRepositoryDialog.java
index 06621c2..d91d18d 100644
--- a/src/com/gitblit/client/EditRepositoryDialog.java
+++ b/src/com/gitblit/client/EditRepositoryDialog.java
@@ -120,6 +120,10 @@
 	private JComboBox ownerField;
 
 	private JComboBox headRefField;
+	
+	private JComboBox gcPeriod;
+	
+	private JTextField gcThreshold;
 
 	private RegistrantPermissionsPanel usersPalette;
 
@@ -193,6 +197,13 @@
 					anRepository.availableRefs.toArray());
 			headRefField.setSelectedItem(anRepository.HEAD);
 		}
+		
+		Integer []  gcPeriods =  { 1, 2, 3, 4, 5, 7, 10, 14 };
+		gcPeriod = new JComboBox(gcPeriods);
+		gcPeriod.setSelectedItem(anRepository.gcPeriod);
+		
+		gcThreshold = new JTextField(8);
+		gcThreshold.setText(anRepository.gcThreshold);
 
 		ownerField = new JComboBox();
 
@@ -288,6 +299,8 @@
 				.add(newFieldPanel(Translation.get("gb.origin"), originField));
 		fieldsPanel.add(newFieldPanel(Translation.get("gb.headRef"), headRefField));
 		fieldsPanel.add(newFieldPanel(Translation.get("gb.owner"), ownerField));
+		fieldsPanel.add(newFieldPanel(Translation.get("gb.gcPeriod"), gcPeriod));
+		fieldsPanel.add(newFieldPanel(Translation.get("gb.gcThreshold"), gcThreshold));
 
 		fieldsPanel.add(newFieldPanel(Translation.get("gb.enableTickets"),
 				useTickets));
@@ -534,6 +547,8 @@
 				: ownerField.getSelectedItem().toString();
 		repository.HEAD = headRefField.getSelectedItem() == null ? null
 				: headRefField.getSelectedItem().toString();
+		repository.gcPeriod = (Integer) gcPeriod.getSelectedItem();
+		repository.gcThreshold = gcThreshold.getText();
 		repository.useTickets = useTickets.isSelected();
 		repository.useDocs = useDocs.isSelected();
 		repository.showRemoteBranches = showRemoteBranches.isSelected();
diff --git a/src/com/gitblit/models/RepositoryModel.java b/src/com/gitblit/models/RepositoryModel.java
index 23ce9e3..ed9e718 100644
--- a/src/com/gitblit/models/RepositoryModel.java
+++ b/src/com/gitblit/models/RepositoryModel.java
@@ -77,7 +77,7 @@
 	public String originRepository;
 	public boolean verifyCommitter;
 	public String gcThreshold;
-	public String gcPeriod;
+	public int gcPeriod;
 	
 	public transient boolean isCollectingGarbage;
 	public Date lastGC;
diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
index 58fdf66..f791cf6 100644
--- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
@@ -389,9 +389,10 @@
 		}
 		form.add(new DropDownChoice<String>("HEAD", availableRefs).setEnabled(availableRefs.size() > 0));
 
-		List<String> gcPeriods = Arrays.asList("1 day", "2 days", "3 days", "4 days", "5 days", "7 days", "10 days", "14 days");
-		form.add(new DropDownChoice<String>("gcPeriod", gcPeriods));
-		form.add(new TextField<String>("gcThreshold"));
+		boolean gcEnabled = GitBlit.getBoolean(Keys.git.enableGarbageCollection, false); 
+		List<Integer> gcPeriods = Arrays.asList(1, 2, 3, 4, 5, 7, 10, 14 );
+		form.add(new DropDownChoice<Integer>("gcPeriod", gcPeriods, new GCPeriodRenderer()).setEnabled(gcEnabled));
+		form.add(new TextField<String>("gcThreshold").setEnabled(gcEnabled));
 
 		// federation strategies - remove ORIGIN choice if this repository has
 		// no origin.
@@ -619,4 +620,27 @@
 			return Integer.toString(index);
 		}
 	}
+	
+	private class GCPeriodRenderer implements IChoiceRenderer<Integer> {
+
+		private static final long serialVersionUID = 1L;
+
+		public GCPeriodRenderer() {
+		}
+
+		@Override
+		public String getDisplayValue(Integer value) {
+			if (value == 1) {
+				return getString("gb.duration.oneDay");
+			} else {
+				return MessageFormat.format(getString("gb.duration.days"), value);
+			}
+		}
+
+		@Override
+		public String getIdValue(Integer value, int index) {
+			return Integer.toString(index);
+		}
+	}
+	
 }

--
Gitblit v1.9.1