Skip to content

Commit 005a65d

Browse files
committed
Simplify when repository sizes are calculated (issue-295)
1 parent 6a28f6d commit 005a65d

File tree

3 files changed

+36
-56
lines changed

3 files changed

+36
-56
lines changed

releases.moxie

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ r19: {
1212
fixes:
1313
- Fixed Gitblit Authority startup failures when using alternate user services (issue-280)
1414
- Manually redirect after branch deletion (issue 282)
15+
- Simplify when repository size is calculated to ensure we have one IF we want one (issue-295)
1516
- Fixed anonymous LDAP connections (issue-297)
1617
- Improved branch deletion-reflog interaction
1718
- Encode page url parameters as UTF-8
@@ -30,6 +31,7 @@ r19: {
3031
- Ori Livneh
3132
- Florian Zschocke
3233
- Tito Nobre
34+
- Hugo Questroy
3335
settings:
3436
- { name: 'web.activityDurationMaximum', defaultValue: 30 }
3537
- { name: 'realm.htpasswd.userFile', defaultValue: '${baseFolder}/htpasswd' }

src/main/java/com/gitblit/GitBlit.java

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,22 +1497,13 @@ public List<String> getRepositoryList() {
14971497
} else {
14981498
// we are caching this list
14991499
String msg = "{0} repositories identified in {1} msecs";
1500-
1501-
// optionally (re)calculate repository sizes
15021500
if (getBoolean(Keys.web.showRepositorySizes, true)) {
1503-
ByteFormat byteFormat = new ByteFormat();
1501+
// optionally (re)calculate repository sizes
15041502
msg = "{0} repositories identified with calculated folder sizes in {1} msecs";
1505-
for (String repository : repositories) {
1506-
RepositoryModel model = getRepositoryModel(repository);
1507-
if (!model.skipSizeCalculation) {
1508-
model.size = byteFormat.format(calculateSize(model));
1509-
}
1510-
}
1511-
} else {
1512-
// update cache
1513-
for (String repository : repositories) {
1514-
getRepositoryModel(repository);
1515-
}
1503+
}
1504+
1505+
for (String repository : repositories) {
1506+
getRepositoryModel(repository);
15161507
}
15171508

15181509
// rebuild fork networks
@@ -1607,23 +1598,6 @@ public List<RepositoryModel> getRepositoryModels(UserModel user) {
16071598
}
16081599
}
16091600
}
1610-
if (getBoolean(Keys.web.showRepositorySizes, true)) {
1611-
int repoCount = 0;
1612-
long startTime = System.currentTimeMillis();
1613-
ByteFormat byteFormat = new ByteFormat();
1614-
for (RepositoryModel model : repositories) {
1615-
if (!model.skipSizeCalculation) {
1616-
repoCount++;
1617-
model.size = byteFormat.format(calculateSize(model));
1618-
}
1619-
}
1620-
long duration = System.currentTimeMillis() - startTime;
1621-
if (duration > 250) {
1622-
// only log calcualtion time if > 250 msecs
1623-
logger.info(MessageFormat.format("{0} repository sizes calculated in {1} msecs",
1624-
repoCount, duration));
1625-
}
1626-
}
16271601
long duration = System.currentTimeMillis() - methodStart;
16281602
logger.info(MessageFormat.format("{0} repository models loaded for {1} in {2} msecs",
16291603
repositories.size(), user == null ? "anonymous" : user.username, duration));
@@ -1706,13 +1680,7 @@ public RepositoryModel getRepositoryModel(String repositoryName) {
17061680
model.hasCommits = JGitUtils.hasCommits(r);
17071681
}
17081682

1709-
LastChange lc = JGitUtils.getLastChange(r);
1710-
model.lastChange = lc.when;
1711-
model.lastChangeAuthor = lc.who;
1712-
if (!model.skipSizeCalculation) {
1713-
ByteFormat byteFormat = new ByteFormat();
1714-
model.size = byteFormat.format(calculateSize(model));
1715-
}
1683+
updateLastChangeFields(r, model);
17161684
}
17171685
r.close();
17181686

@@ -2011,10 +1979,6 @@ private RepositoryModel loadRepositoryModel(String repositoryName) {
20111979
// is symlinked. Use the provided repository name.
20121980
model.name = repositoryName;
20131981
}
2014-
model.hasCommits = JGitUtils.hasCommits(r);
2015-
LastChange lc = JGitUtils.getLastChange(r);
2016-
model.lastChange = lc.when;
2017-
model.lastChangeAuthor = lc.who;
20181982
model.projectPath = StringUtils.getFirstPathElement(repositoryName);
20191983

20201984
StoredConfig config = r.getConfig();
@@ -2076,6 +2040,8 @@ private RepositoryModel loadRepositoryModel(String repositoryName) {
20762040
model.HEAD = JGitUtils.getHEADRef(r);
20772041
model.availableRefs = JGitUtils.getAvailableHeadTargets(r);
20782042
model.sparkleshareId = JGitUtils.getSparkleshareId(r);
2043+
model.hasCommits = JGitUtils.hasCommits(r);
2044+
updateLastChangeFields(r, model);
20792045
r.close();
20802046

20812047
if (StringUtils.isEmpty(model.originRepository) && model.origin != null && model.origin.startsWith("file://")) {
@@ -2271,21 +2237,31 @@ private ForkModel getForkModel(String repository) {
22712237
}
22722238

22732239
/**
2274-
* Returns the size in bytes of the repository. Gitblit caches the
2275-
* repository sizes to reduce the performance penalty of recursive
2276-
* calculation. The cache is updated if the repository has been changed
2277-
* since the last calculation.
2240+
* Updates the last changed fields and optionally calculates the size of the
2241+
* repository. Gitblit caches the repository sizes to reduce the performance
2242+
* penalty of recursive calculation. The cache is updated if the repository
2243+
* has been changed since the last calculation.
22782244
*
22792245
* @param model
2280-
* @return size in bytes
2246+
* @return size in bytes of the repository
22812247
*/
2282-
public long calculateSize(RepositoryModel model) {
2283-
if (repositorySizeCache.hasCurrent(model.name, model.lastChange)) {
2284-
return repositorySizeCache.getObject(model.name);
2248+
public long updateLastChangeFields(Repository r, RepositoryModel model) {
2249+
LastChange lc = JGitUtils.getLastChange(r);
2250+
model.lastChange = lc.when;
2251+
model.lastChangeAuthor = lc.who;
2252+
2253+
if (!getBoolean(Keys.web.showRepositorySizes, true) || model.skipSizeCalculation) {
2254+
model.size = null;
2255+
return 0L;
2256+
}
2257+
if (!repositorySizeCache.hasCurrent(model.name, model.lastChange)) {
2258+
File gitDir = r.getDirectory();
2259+
long sz = com.gitblit.utils.FileUtils.folderSize(gitDir);
2260+
repositorySizeCache.updateObject(model.name, model.lastChange, sz);
22852261
}
2286-
File gitDir = FileKey.resolve(new File(repositoriesFolder, model.name), FS.DETECTED);
2287-
long size = com.gitblit.utils.FileUtils.folderSize(gitDir);
2288-
repositorySizeCache.updateObject(model.name, model.lastChange, size);
2262+
long size = repositorySizeCache.getObject(model.name);
2263+
ByteFormat byteFormat = new ByteFormat();
2264+
model.size = byteFormat.format(size);
22892265
return size;
22902266
}
22912267

src/test/java/com/gitblit/tests/GitBlitTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.List;
2424

25+
import org.eclipse.jgit.lib.Repository;
2526
import org.junit.Test;
2627

2728
import com.gitblit.Constants.AccessRestrictionType;
@@ -40,11 +41,12 @@ public void testRepositoryModel() throws Exception {
4041
"Missing Helloworld repository!",
4142
repositories.contains(GitBlitSuite.getHelloworldRepository().getDirectory()
4243
.getName()));
43-
RepositoryModel model = GitBlit.self().getRepositoryModel(
44-
GitBlitSuite.getHelloworldRepository().getDirectory().getName());
44+
Repository r = GitBlitSuite.getHelloworldRepository();
45+
RepositoryModel model = GitBlit.self().getRepositoryModel(r.getDirectory().getName());
4546
assertTrue("Helloworld model is null!", model != null);
4647
assertEquals(GitBlitSuite.getHelloworldRepository().getDirectory().getName(), model.name);
47-
assertTrue(GitBlit.self().calculateSize(model) > 22000L);
48+
assertTrue(GitBlit.self().updateLastChangeFields(r, model) > 22000L);
49+
r.close();
4850
}
4951

5052
@Test

0 commit comments

Comments
 (0)