1616package com .gitblit .utils ;
1717
1818import java .io .IOException ;
19+ import java .text .DateFormat ;
1920import java .text .MessageFormat ;
21+ import java .text .SimpleDateFormat ;
2022import java .util .ArrayList ;
23+ import java .util .Arrays ;
2124import java .util .Collection ;
2225import java .util .Collections ;
2326import java .util .Date ;
5053import org .slf4j .Logger ;
5154import org .slf4j .LoggerFactory ;
5255
56+ import com .gitblit .models .DailyLogEntry ;
5357import com .gitblit .models .PathModel .PathChangeModel ;
5458import com .gitblit .models .PushLogEntry ;
5559import com .gitblit .models .RefModel ;
@@ -109,6 +113,25 @@ public static RefModel getPushLogBranch(Repository repository) {
109113 return null ;
110114 }
111115
116+ private static UserModel newUserModelFrom (PersonIdent ident ) {
117+ String name = ident .getName ();
118+ String username ;
119+ String displayname ;
120+ if (name .indexOf ('/' ) > -1 ) {
121+ int slash = name .indexOf ('/' );
122+ displayname = name .substring (0 , slash );
123+ username = name .substring (slash + 1 );
124+ } else {
125+ displayname = name ;
126+ username = ident .getEmailAddress ();
127+ }
128+
129+ UserModel user = new UserModel (username );
130+ user .displayName = displayname ;
131+ user .emailAddress = ident .getEmailAddress ();
132+ return user ;
133+ }
134+
112135 /**
113136 * Updates a push log.
114137 *
@@ -135,9 +158,15 @@ public static boolean updatePushLog(UserModel user, Repository repository,
135158 DirCache index = createIndex (repository , headId , commands );
136159 ObjectId indexTreeId = index .writeTree (odi );
137160
138- PersonIdent ident =
139- new PersonIdent (MessageFormat .format ("{0}/{1}" , user .getDisplayName (), user .username ),
161+ PersonIdent ident ;
162+ if (UserModel .ANONYMOUS .equals (user )) {
163+ // anonymous push
164+ ident = new PersonIdent ("anonymous" , "anonymous" );
165+ } else {
166+ // construct real pushing account
167+ ident = new PersonIdent (MessageFormat .format ("{0}/{1}" , user .getDisplayName (), user .username ),
140168 user .emailAddress == null ? user .username : user .emailAddress );
169+ }
141170
142171 // Create a commit object
143172 CommitBuilder commit = new CommitBuilder ();
@@ -339,23 +368,9 @@ public static List<PushLogEntry> getPushLog(String repositoryName, Repository re
339368 continue ;
340369 }
341370
342- String name = push .getAuthorIdent ().getName ();
343- String username ;
344- String displayname ;
345- if (name .indexOf ('/' ) > -1 ) {
346- int slash = name .indexOf ('/' );
347- displayname = name .substring (0 , slash );
348- username = name .substring (slash + 1 );
349- } else {
350- displayname = name ;
351- username = push .getAuthorIdent ().getEmailAddress ();
352- }
353-
354- UserModel user = new UserModel (username );
355- user .displayName = displayname ;
356- user .emailAddress = push .getAuthorIdent ().getEmailAddress ();
357-
371+ UserModel user = newUserModelFrom (push .getAuthorIdent ());
358372 Date date = push .getAuthorIdent ().getWhen ();
373+
359374 PushLogEntry log = new PushLogEntry (repositoryName , date , user );
360375 list .add (log );
361376 List <PathChangeModel > changedRefs = JGitUtils .getFilesInCommit (repository , push );
@@ -413,14 +428,22 @@ public static List<PushLogEntry> getPushLogByRef(String repositoryName, Reposito
413428 int maxCount ) {
414429 // break the push log into ref push logs and then merge them back into a list
415430 Map <String , List <PushLogEntry >> refMap = new HashMap <String , List <PushLogEntry >>();
416- for (PushLogEntry push : getPushLog (repositoryName , repository , offset , maxCount )) {
431+ List <PushLogEntry > pushes = getPushLog (repositoryName , repository , offset , maxCount );
432+ for (PushLogEntry push : pushes ) {
417433 for (String ref : push .getChangedRefs ()) {
418434 if (!refMap .containsKey (ref )) {
419435 refMap .put (ref , new ArrayList <PushLogEntry >());
420436 }
421437
422438 // construct new ref-specific push log entry
423- PushLogEntry refPush = new PushLogEntry (push .repository , push .date , push .user );
439+ PushLogEntry refPush ;
440+ if (push instanceof DailyLogEntry ) {
441+ // simulated push log from commits grouped by date
442+ refPush = new DailyLogEntry (push .repository , push .date );
443+ } else {
444+ // real push log entry
445+ refPush = new PushLogEntry (push .repository , push .date , push .user );
446+ }
424447 refPush .updateRef (ref , push .getChangeType (ref ), push .getOldId (ref ), push .getNewId (ref ));
425448 refPush .addCommits (push .getCommits (ref ));
426449 refMap .get (ref ).add (refPush );
@@ -451,15 +474,16 @@ public static List<PushLogEntry> getPushLogByRef(String repositoryName, Reposito
451474 public static List <PushLogEntry > getPushLogByRef (String repositoryName , Repository repository , Date minimumDate ) {
452475 // break the push log into ref push logs and then merge them back into a list
453476 Map <String , List <PushLogEntry >> refMap = new HashMap <String , List <PushLogEntry >>();
454- for (PushLogEntry push : getPushLog (repositoryName , repository , minimumDate )) {
477+ List <PushLogEntry > pushes = getPushLog (repositoryName , repository , minimumDate );
478+ for (PushLogEntry push : pushes ) {
455479 for (String ref : push .getChangedRefs ()) {
456480 if (!refMap .containsKey (ref )) {
457481 refMap .put (ref , new ArrayList <PushLogEntry >());
458482 }
459-
460- // construct new ref-specific push log entry
461- PushLogEntry refPush = new PushLogEntry (push .repository , push .date , push .user );
462- refPush .updateRef (ref , push .getChangeType (ref ), push .getOldId (ref ), push .getNewId (ref ));
483+
484+ // construct new ref-specific push log entry
485+ PushLogEntry refPush = new PushLogEntry (push .repository , push .date , push .user );
486+ refPush .updateRef (ref , push .getChangeType (ref ), push .getOldId (ref ), push .getNewId (ref ));
463487 refPush .addCommits (push .getCommits (ref ));
464488 refMap .get (ref ).add (refPush );
465489 }
@@ -476,4 +500,106 @@ public static List<PushLogEntry> getPushLogByRef(String repositoryName, Reposito
476500
477501 return refPushLog ;
478502 }
503+
504+ /**
505+ * Returns a commit log grouped by day.
506+ *
507+ * @param repositoryName
508+ * @param repository
509+ * @param minimumDate
510+ * @param offset
511+ * @param maxCount
512+ * if < 0, all pushes are returned.
513+ * @return a list of grouped commit log entries
514+ */
515+ public static List <DailyLogEntry > getDailyLog (String repositoryName , Repository repository ,
516+ Date minimumDate , int offset , int maxCount ) {
517+
518+ DateFormat df = new SimpleDateFormat ("yyyy-MM-dd" );
519+ // df.setTimeZone(timezone);
520+
521+ Map <ObjectId , List <RefModel >> allRefs = JGitUtils .getAllRefs (repository );
522+ Map <String , DailyLogEntry > tags = new HashMap <String , DailyLogEntry >();
523+ Map <String , DailyLogEntry > dailydigests = new HashMap <String , DailyLogEntry >();
524+ for (RefModel local : JGitUtils .getLocalBranches (repository , true , -1 )) {
525+ String branch = local .getName ();
526+ List <RevCommit > commits = JGitUtils .getRevLog (repository , branch , minimumDate );
527+ for (RevCommit commit : commits ) {
528+ Date date = JGitUtils .getCommitDate (commit );
529+ String dateStr = df .format (date );
530+ if (!dailydigests .containsKey (dateStr )) {
531+ dailydigests .put (dateStr , new DailyLogEntry (repositoryName , date ));
532+ }
533+ PushLogEntry digest = dailydigests .get (dateStr );
534+ digest .updateRef (branch , ReceiveCommand .Type .UPDATE , commit .getParents ()[0 ].getId ().getName (), commit .getName ());
535+ RepositoryCommit repoCommit = digest .addCommit (branch , commit );
536+ if (repoCommit != null ) {
537+ repoCommit .setRefs (allRefs .get (commit .getId ()));
538+ if (!ArrayUtils .isEmpty (repoCommit .getRefs ())) {
539+ // treat tags as special events in the log
540+ for (RefModel ref : repoCommit .getRefs ()) {
541+ if (ref .getName ().startsWith (Constants .R_TAGS )) {
542+ if (!tags .containsKey (dateStr )) {
543+ UserModel tagUser = newUserModelFrom (commit .getAuthorIdent ());
544+ Date tagDate = commit .getAuthorIdent ().getWhen ();
545+ tags .put (dateStr , new DailyLogEntry (repositoryName , tagDate , tagUser ));
546+ }
547+ PushLogEntry tagEntry = tags .get (dateStr );
548+ tagEntry .updateRef (ref .getName (), ReceiveCommand .Type .CREATE );
549+ tagEntry .addCommits (Arrays .asList (repoCommit ));
550+ }
551+ }
552+ }
553+ }
554+ }
555+ }
556+
557+ List <DailyLogEntry > list = new ArrayList <DailyLogEntry >(dailydigests .values ());
558+ list .addAll (tags .values ());
559+ Collections .sort (list );
560+ return list ;
561+ }
562+
563+ /**
564+ * Returns the list of commits separated by ref (e.g. each ref has it's own
565+ * PushLogEntry object for each day).
566+ *
567+ * @param repositoryName
568+ * @param repository
569+ * @param minimumDate
570+ * @return a list of push log entries separated by ref and date
571+ */
572+ public static List <DailyLogEntry > getDailyLogByRef (String repositoryName , Repository repository , Date minimumDate ) {
573+ // break the push log into ref push logs and then merge them back into a list
574+ Map <String , List <DailyLogEntry >> refMap = new HashMap <String , List <DailyLogEntry >>();
575+ List <DailyLogEntry > pushes = getDailyLog (repositoryName , repository , minimumDate , 0 , -1 );
576+ for (DailyLogEntry push : pushes ) {
577+ for (String ref : push .getChangedRefs ()) {
578+ if (!refMap .containsKey (ref )) {
579+ refMap .put (ref , new ArrayList <DailyLogEntry >());
580+ }
581+
582+ // construct new ref-specific push log entry
583+ DailyLogEntry refPush = new DailyLogEntry (push .repository , push .date , push .user );
584+ refPush .updateRef (ref , push .getChangeType (ref ), push .getOldId (ref ), push .getNewId (ref ));
585+ refPush .addCommits (push .getCommits (ref ));
586+ refMap .get (ref ).add (refPush );
587+ }
588+ }
589+
590+ // merge individual ref pushes into master list
591+ List <DailyLogEntry > refPushLog = new ArrayList <DailyLogEntry >();
592+ for (List <DailyLogEntry > refPush : refMap .values ()) {
593+ for (DailyLogEntry entry : refPush ) {
594+ if (entry .getCommitCount () > 0 ) {
595+ refPushLog .add (entry );
596+ }
597+ }
598+ }
599+
600+ // sort ref push log
601+ Collections .sort (refPushLog );
602+
603+ return refPushLog ;
604+ }
479605}
0 commit comments