Florian Zschocke
2013-08-20 e85277e1de9f59ac45df5ffd84c5d9be0e4d20d2
src/main/java/com/gitblit/utils/RefLogUtils.java
@@ -20,6 +20,7 @@
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@@ -41,6 +42,7 @@
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefRename;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result;
@@ -95,6 +97,21 @@
         parameters.add(0, repository.getDirectory().getAbsolutePath());
      }
      LOGGER.error(MessageFormat.format(pattern, parameters.toArray()), t);
   }
   /**
    * Returns true if the repository has a reflog branch.
    *
    * @param repository
    * @return true if the repository has a reflog branch
    */
   public static boolean hasRefLogBranch(Repository repository) {
      try {
         return repository.getRef(GB_REFLOG) != null;
      } catch(Exception e) {
         LOGGER.error("failed to determine hasRefLogBranch", e);
      }
      return false;
   }
   /**
@@ -156,6 +173,37 @@
   }
   
   /**
    * Logs a ref deletion.
    *
    * @param user
    * @param repository
    * @param ref
    * @return true, if the update was successful
    */
   public static boolean deleteRef(UserModel user, Repository repository, Ref ref) {
      try {
         if (ref == null) {
            return false;
         }
         RefModel reflogBranch = getRefLogBranch(repository);
         if (reflogBranch == null) {
            return false;
         }
         List<RevCommit> log = JGitUtils.getRevLog(repository, reflogBranch.getName(), ref.getName(), 0, 1);
         if (log.isEmpty()) {
            // this ref is not in the reflog branch
            return false;
         }
         ReceiveCommand cmd = new ReceiveCommand(ref.getObjectId(), ObjectId.zeroId(), ref.getName());
         return updateRefLog(user, repository, Arrays.asList(cmd));
      } catch (Throwable t) {
         error(t, repository, "Failed to commit reflog entry to {0}");
      }
      return false;
   }
   /**
    * Updates the reflog with the received commands.
    * 
    * @param user
@@ -184,7 +232,7 @@
            PersonIdent ident;
            if (UserModel.ANONYMOUS.equals(user)) {
               // anonymous push
               ident = new PersonIdent("anonymous", "anonymous");
               ident = new PersonIdent(user.username + "/" + user.username, user.username);
            } else {
               // construct real pushing account
               ident =   new PersonIdent(MessageFormat.format("{0}/{1}", user.getDisplayName(), user.username),
@@ -394,9 +442,13 @@
         UserModel user = newUserModelFrom(push.getAuthorIdent());
         Date date = push.getAuthorIdent().getWhen();
         
         RefLogEntry log = new RefLogEntry(repositoryName, date, user);
         list.add(log);
         RefLogEntry log = new RefLogEntry(repositoryName, date, user);
         List<PathChangeModel> changedRefs = JGitUtils.getFilesInCommit(repository, push);
         if (changedRefs.isEmpty()) {
            // skip empty commits
            continue;
         }
         list.add(log);
         for (PathChangeModel change : changedRefs) {
            switch (change.changeType) {
            case DELETE:
@@ -410,6 +462,10 @@
               String oldId = fields[1];
               String newId = fields[2];
               log.updateRef(change.path, ReceiveCommand.Type.valueOf(fields[0]), oldId, newId);
               if (ObjectId.zeroId().getName().equals(newId)) {
                  // ref deletion
                  continue;
               }
               List<RevCommit> pushedCommits = JGitUtils.getRevLog(repository, oldId, newId);
               for (RevCommit pushedCommit : pushedCommits) {
                  RepositoryCommit repoCommit = log.addCommit(change.path, pushedCommit);
@@ -548,16 +604,21 @@
        Map<String, DailyLogEntry> dailydigests = new HashMap<String, DailyLogEntry>();
        String linearParent = null;
        for (RefModel local : JGitUtils.getLocalBranches(repository, true, -1)) {
           if (!local.getDate().after(minimumDate)) {
            // branch not recently updated
              continue;
           }
            String branch = local.getName();
            List<RevCommit> commits = JGitUtils.getRevLog(repository, branch, minimumDate);
            for (RevCommit commit : commits) {
            List<RepositoryCommit> commits = CommitCache.instance().getCommits(repositoryName, repository,  branch, minimumDate);
            linearParent = null;
            for (RepositoryCommit commit : commits) {
               if (linearParent != null) {
                  if (!commit.getName().equals(linearParent)) {
                     // only follow linear branch commits
                     continue;
                  }
               }
                Date date = JGitUtils.getCommitDate(commit);
                Date date = commit.getCommitDate();
                String dateStr = df.format(date);
                if (!dailydigests.containsKey(dateStr)) {
                    dailydigests.put(dateStr, new DailyLogEntry(repositoryName, date));
@@ -571,7 +632,7 @@
                   digest.updateRef(branch, ReceiveCommand.Type.UPDATE, linearParent, commit.getName());
                }
                
                RepositoryCommit repoCommit = digest.addCommit(branch, commit);
                RepositoryCommit repoCommit = digest.addCommit(commit);
                if (repoCommit != null) {
                   List<RefModel> matchedRefs = allRefs.get(commit.getId());
                    repoCommit.setRefs(matchedRefs);
@@ -587,7 +648,8 @@
                                }
                                RefLogEntry tagEntry = tags.get(dateStr);
                                tagEntry.updateRef(ref.getName(), ReceiveCommand.Type.CREATE);
                                tagEntry.addCommit(ref.getName(), commit);
                                RepositoryCommit rc = repoCommit.clone(ref.getName());
                                tagEntry.addCommit(rc);
                            } else if (ref.getName().startsWith(Constants.R_PULL)) {
                                // treat pull requests as special events in the log
                                if (!pulls.containsKey(dateStr)) {
@@ -597,7 +659,8 @@
                                }
                                RefLogEntry pullEntry = pulls.get(dateStr);
                                pullEntry.updateRef(ref.getName(), ReceiveCommand.Type.CREATE);
                                pullEntry.addCommit(ref.getName(), commit);
                                RepositoryCommit rc = repoCommit.clone(ref.getName());
                                pullEntry.addCommit(rc);
                            }
                        }
                    }