2626import org .eclipse .jgit .api .*;
2727import org .eclipse .jgit .api .errors .GitAPIException ;
2828import org .eclipse .jgit .diff .DiffFormatter ;
29- import org .eclipse .jgit .lib .ObjectId ;
30- import org .eclipse .jgit .lib .ObjectLoader ;
31- import org .eclipse .jgit .lib .ObjectReader ;
32- import org .eclipse .jgit .lib .Repository ;
29+ import org .eclipse .jgit .errors .IncorrectObjectTypeException ;
30+ import org .eclipse .jgit .errors .MissingObjectException ;
31+ import org .eclipse .jgit .lib .*;
3332import org .eclipse .jgit .revwalk .RevCommit ;
3433import org .eclipse .jgit .revwalk .RevTree ;
3534import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
4241import java .io .ByteArrayOutputStream ;
4342import java .io .File ;
4443import java .io .IOException ;
45- import java .util .Iterator ;
44+ import java .util .List ;
45+ import java .util .Properties ;
4646
4747/**
4848 * @understands versioning cruise-config
@@ -63,7 +63,7 @@ public class ConfigRepository {
6363 private Repository gitRepo ;
6464
6565 @ Autowired
66- public ConfigRepository (SystemEnvironment systemEnvironment ) throws IOException {
66+ public ConfigRepository (SystemEnvironment systemEnvironment ) throws IOException {
6767 this .systemEnvironment = systemEnvironment ;
6868 workingDir = this .systemEnvironment .getConfigRepoDir ();
6969 File configRepoDir = new File (workingDir , ".git" );
@@ -79,13 +79,13 @@ public Repository getGitRepo() {
7979 public void initialize () throws IOException {
8080 if (!gitRepo .getDirectory ().exists ()) {
8181 gitRepo .create ();
82- }
83- else {
82+ } else {
8483 cleanAndResetToMaster ();
8584 }
8685 }
8786
88- @ Deprecated // used in test only
87+ @ Deprecated
88+ // used in test only
8989 Git git () {
9090 return git ;
9191 }
@@ -144,7 +144,7 @@ public GoConfigRevision call() throws GitAPIException {
144144 }
145145
146146 public RevCommit getRevCommitForMd5 (String md5 ) throws GitAPIException {
147- if (md5 == null )
147+ if (md5 == null )
148148 throw new NullArgumentException ("md5" );
149149
150150 final String expectedPart = GoConfigRevision .Fragment .md5 .represent (GoConfigRevision .esc (md5 ));
@@ -157,14 +157,14 @@ public RevCommit getRevCommitForMd5(String md5) throws GitAPIException {
157157 throw new IllegalArgumentException (String .format ("There is no config version corresponding to md5: '%s'" , md5 ));
158158 }
159159
160- RevCommit getRevCommitForCommitSHA (String commitSHA ) throws GitAPIException {
161- for (RevCommit revision : revisions ()) {
162- if (revision .getName ().equals (commitSHA )) {
163- return revision ;
164- }
165- }
166- throw new IllegalArgumentException (String .format ("There is no commit corresponding to SHA: '%s'" , commitSHA ));
167- }
160+ RevCommit getRevCommitForCommitSHA (String commitSHA ) throws GitAPIException {
161+ for (RevCommit revision : revisions ()) {
162+ if (revision .getName ().equals (commitSHA )) {
163+ return revision ;
164+ }
165+ }
166+ throw new IllegalArgumentException (String .format ("There is no commit corresponding to SHA: '%s'" , commitSHA ));
167+ }
168168
169169 public GoConfigRevision getCurrentRevision () {
170170 return doLocked (new ThrowingFn <GoConfigRevision , RuntimeException >() {
@@ -191,25 +191,25 @@ public RevCommit getCurrentRevCommit() throws GitAPIException {
191191 }
192192 }
193193
194- public GoConfigRevisions getCommits (final int pageSize , final int offset ) throws Exception {
195- return doLocked (new ThrowingFn <GoConfigRevisions , RuntimeException >() {
196- public GoConfigRevisions call () {
197- GoConfigRevisions goConfigRevisions = new GoConfigRevisions ();
198- try {
199- LogCommand command = git .log ().setMaxCount (pageSize ).setSkip (offset );
200- Iterable <RevCommit > revisions = command .call ();
201- for (RevCommit revision : revisions ) {
202- GoConfigRevision goConfigRevision = new GoConfigRevision (null , revision .getFullMessage ());
203- goConfigRevision .setCommitSHA (revision .name ());
204- goConfigRevisions .add (goConfigRevision );
205- }
206- } catch (Exception e ) {
207- // ignore
208- }
209- return goConfigRevisions ;
210- }
211- });
212- }
194+ public GoConfigRevisions getCommits (final int pageSize , final int offset ) throws Exception {
195+ return doLocked (new ThrowingFn <GoConfigRevisions , RuntimeException >() {
196+ public GoConfigRevisions call () {
197+ GoConfigRevisions goConfigRevisions = new GoConfigRevisions ();
198+ try {
199+ LogCommand command = git .log ().setMaxCount (pageSize ).setSkip (offset );
200+ Iterable <RevCommit > revisions = command .call ();
201+ for (RevCommit revision : revisions ) {
202+ GoConfigRevision goConfigRevision = new GoConfigRevision (null , revision .getFullMessage ());
203+ goConfigRevision .setCommitSHA (revision .name ());
204+ goConfigRevisions .add (goConfigRevision );
205+ }
206+ } catch (Exception e ) {
207+ // ignore
208+ }
209+ return goConfigRevisions ;
210+ }
211+ });
212+ }
213213
214214 private GoConfigRevision getGoConfigRevision (final RevCommit revision ) {
215215 return new GoConfigRevision (contentFromTree (revision .getTree ()), revision .getFullMessage ());
@@ -249,31 +249,31 @@ public String configChangesFor(final String laterMD5, final String earlierMD5) t
249249 public String call () throws GitAPIException {
250250 RevCommit laterCommit = null ;
251251 RevCommit earlierCommit = null ;
252- if (!StringUtil .isBlank (laterMD5 )) {
252+ if (!StringUtil .isBlank (laterMD5 )) {
253253 laterCommit = getRevCommitForMd5 (laterMD5 );
254254 }
255- if (!StringUtil .isBlank (earlierMD5 ))
255+ if (!StringUtil .isBlank (earlierMD5 ))
256256 earlierCommit = getRevCommitForMd5 (earlierMD5 );
257257 return findDiffBetweenTwoRevisions (laterCommit , earlierCommit );
258258 }
259259 });
260260 }
261261
262- public String configChangesForCommits (final String fromRevision , final String toRevision ) throws GitAPIException {
263- return doLocked (new ThrowingFn <String , GitAPIException >() {
264- public String call () throws GitAPIException {
265- RevCommit laterCommit = null ;
266- RevCommit earlierCommit = null ;
267- if (!StringUtil .isBlank (fromRevision )) {
268- laterCommit = getRevCommitForCommitSHA (fromRevision );
269- }
270- if (!StringUtil .isBlank (toRevision )) {
271- earlierCommit = getRevCommitForCommitSHA (toRevision );
272- }
273- return findDiffBetweenTwoRevisions (laterCommit , earlierCommit );
274- }
275- });
276- }
262+ public String configChangesForCommits (final String fromRevision , final String toRevision ) throws GitAPIException {
263+ return doLocked (new ThrowingFn <String , GitAPIException >() {
264+ public String call () throws GitAPIException {
265+ RevCommit laterCommit = null ;
266+ RevCommit earlierCommit = null ;
267+ if (!StringUtil .isBlank (fromRevision )) {
268+ laterCommit = getRevCommitForCommitSHA (fromRevision );
269+ }
270+ if (!StringUtil .isBlank (toRevision )) {
271+ earlierCommit = getRevCommitForCommitSHA (toRevision );
272+ }
273+ return findDiffBetweenTwoRevisions (laterCommit , earlierCommit );
274+ }
275+ });
276+ }
277277
278278 String findDiffBetweenTwoRevisions (RevCommit laterCommit , RevCommit earlierCommit ) throws GitAPIException {
279279 if (laterCommit == null || earlierCommit == null ) {
@@ -384,7 +384,7 @@ void cleanAndResetToMaster() throws IOException {
384384 }
385385
386386 public void garbageCollect () throws Exception {
387- if (!systemEnvironment .get (SystemEnvironment .GO_CONFIG_REPO_PERIODIC_GC )){
387+ if (!systemEnvironment .get (SystemEnvironment .GO_CONFIG_REPO_PERIODIC_GC )) {
388388 return ;
389389 }
390390 doLocked (new VoidThrowingFn <Exception >() {
@@ -404,8 +404,31 @@ public void run() throws Exception {
404404 public long getLooseObjectCount () throws Exception {
405405 return doLocked (new ThrowingFn <Long , GitAPIException >() {
406406 public Long call () throws GitAPIException {
407- return (Long ) git . gc (). getStatistics ().get ("numberOfLooseObjects" );
407+ return (Long ) getStatistics ().get ("numberOfLooseObjects" );
408408 }
409409 });
410410 }
411+
412+ public Properties getStatistics () throws GitAPIException {
413+ // not inside a doLocked/synchronized block because we don't want to block the server status service.
414+ return git .gc ().getStatistics ();
415+ }
416+
417+ public Long commitCountOnMaster () throws GitAPIException , IncorrectObjectTypeException , MissingObjectException {
418+ // not inside a doLocked/synchronized block because we don't want to block the server status service.
419+ // we do a `git branch` because we switch branches as part of normal git operations,
420+ // and we don't care about number of commits on those branches.
421+ List <Ref > branches = git .branchList ().call ();
422+ for (Ref branch : branches ) {
423+ if (branch .getName ().equals ("refs/heads/master" )) {
424+ Iterable <RevCommit > commits = git .log ().add (branch .getObjectId ()).call ();
425+ long count = 0 ;
426+ for (RevCommit commit : commits ) {
427+ count ++;
428+ }
429+ return count ;
430+ }
431+ }
432+ return Long .valueOf (-1 );
433+ }
411434}
0 commit comments