The Unified and Autonomous Development Platform
midwork
robin shine45300395e574
diff --git a/commons.git/src/main/java/com/pmease/commons/git/AheadBehind.java b/commons.git/src/main/java/com/pmease/commons/git/AheadBehind.javadeleted file mode 100644index 348d799767..0000000000--- a/commons.git/src/main/java/com/pmease/commons/git/AheadBehind.java+++ /dev/null@@ -1,40 +0,0 @@-package com.pmease.commons.git;--import java.io.Serializable;--import com.google.common.base.Objects;--@SuppressWarnings("serial")-public class AheadBehind implements Serializable {-- private int ahead;-- private int behind;-- public int getAhead() {- return ahead;- }-- public void setAhead(int ahead) {- this.ahead = ahead;- }-- public int getBehind() {- return behind;- }-- public void setBehind(int behind) {- this.behind = behind;- }-- @Override- public String toString() {- return "Ahead: " + ahead + ", behind: " + behind;- }-- @Override- public int hashCode() {- return Objects.hashCode(ahead, behind);- }--}diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.javaindex f8ceeaad73..03e15d4463 100644--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java@@ -17,7 +17,6 @@ import com.google.common.collect.Lists;import com.pmease.commons.git.command.AddCommand;import com.pmease.commons.git.command.AddNoteCommand;import com.pmease.commons.git.command.AddSubModuleCommand;-import com.pmease.commons.git.command.AheadBehindCommand;import com.pmease.commons.git.command.BlameCommand;import com.pmease.commons.git.command.CalcMergeBaseCommand;import com.pmease.commons.git.command.CheckoutCommand;@@ -289,11 +288,6 @@ public class Git implements Serializable {return new IsAncestorCommand(repoDir).ancestor(ancestor).descendant(descendant).call();}- public Map<String, AheadBehind> getAheadBehinds(String baseRev, String... compareRevs) {- AheadBehindCommand cmd = new AheadBehindCommand(repoDir);- return cmd.baseCommit(baseRev).compareRevs(compareRevs).call();- }-/*** Show content of specified path of specified revision.*diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.javadeleted file mode 100644index ae90dcf2d9..0000000000--- a/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java+++ /dev/null@@ -1,254 +0,0 @@-package com.pmease.commons.git.command;--import java.io.File;-import java.util.ArrayList;-import java.util.HashMap;-import java.util.HashSet;-import java.util.List;-import java.util.Map;-import java.util.Set;-import java.util.Stack;-import java.util.StringTokenizer;-import java.util.concurrent.atomic.AtomicInteger;--import org.slf4j.Logger;-import org.slf4j.LoggerFactory;--import com.google.common.base.Preconditions;-import com.pmease.commons.git.AheadBehind;-import com.pmease.commons.git.GitUtils;-import com.pmease.commons.util.execution.Commandline;-import com.pmease.commons.util.execution.LineConsumer;--public class AheadBehindCommand extends GitCommand<Map<String, AheadBehind>> {-- private static final Logger logger = LoggerFactory.getLogger(AheadBehindCommand.class);-- private String baseRev;-- private String[] compareRevs;-- public AheadBehindCommand(File repoDir) {- super(repoDir);- }-- public AheadBehindCommand baseCommit(String baseRev) {- this.baseRev = Preconditions.checkNotNull(baseRev);- return this;- }-- public AheadBehindCommand compareRevs(String... compareRevs) {- this.compareRevs = Preconditions.checkNotNull(compareRevs);- return this;- }-- @Override- public Map<String, AheadBehind> call() {- Preconditions.checkNotNull(baseRev);- Preconditions.checkArgument(compareRevs != null);-- if (compareRevs.length > 1) {- final List<String> nonHashRevs = new ArrayList<>();- final Map<String, String> revHashes = new HashMap<>();- if (GitUtils.isHash(baseRev))- revHashes.put(baseRev, baseRev);- else- nonHashRevs.add(baseRev);- for (String each: compareRevs) {- if (GitUtils.isHash(each))- revHashes.put(each, each);- else- nonHashRevs.add(each);- }-- Commandline cmd = cmd();- cmd.addArgs("rev-parse");- for (String each: nonHashRevs)- cmd.addArgs(each);-- final AtomicInteger index = new AtomicInteger(0);- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- revHashes.put(nonHashRevs.get(index.getAndIncrement()), line);- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- logger.error(line);- }-- }).checkReturnCode();-- cmd = cmd();- cmd.addArgs("rev-list", "--parents", "^" + revHashes.get(baseRev));- for (String each: compareRevs)- cmd.addArgs(revHashes.get(each));-- final Map<String, Set<String>> commits = new HashMap<>();- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- parseCommitLine(commits, line);- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- logger.error(line);- }-- }).checkReturnCode();-- Map<String, AheadBehind> aheadBehinds = new HashMap<>();- for (String each: compareRevs) {- AheadBehind ab = new AheadBehind();- ab.setAhead(findAncestors(commits, revHashes.get(each)).size());- aheadBehinds.put(each, ab);- }-- cmd = cmd();- cmd.addArgs("merge-base", "--all", "--octopus");- for (String each: compareRevs)- cmd.addArgs(revHashes.get(each));-- final Set<String> mergeBases = new HashSet<>();- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- StringTokenizer tokenizer = new StringTokenizer(line);- while (tokenizer.hasMoreTokens())- mergeBases.add(tokenizer.nextToken());- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- logger.error(line);- }-- }).checkReturnCode();-- cmd = cmd();- cmd.addArgs("rev-list");- for (String each: mergeBases)- cmd.addArgs("^" + each);-- cmd.addArgs(baseRev);-- final Set<String> mergeBaseBehindBase = new HashSet<>();- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- mergeBaseBehindBase.add(line);- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- logger.error(line);- }-- }).checkReturnCode();-- cmd = cmd();- cmd.addArgs("rev-list", "--parents");- for (String each: mergeBases)- cmd.addArgs("^" + each);- for (String each: compareRevs)- cmd.addArgs(revHashes.get(each));-- commits.clear();- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- parseCommitLine(commits, line);- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- logger.error(line);- }-- }).checkReturnCode();-- for (Map.Entry<String, AheadBehind> entry: aheadBehinds.entrySet()) {- String branchHash = revHashes.get(entry.getKey());- Set<String> copyOfMergeBaseBehindBase = new HashSet<>(mergeBaseBehindBase);- copyOfMergeBaseBehindBase.removeAll(findAncestors(commits, branchHash));- entry.getValue().setBehind(copyOfMergeBaseBehindBase.size());- }-- return aheadBehinds;- } else if (compareRevs.length == 1) {- Commandline cmd = cmd();- cmd.addArgs("rev-list", "--left-right", "--count");- cmd.addArgs(baseRev + "..." + compareRevs[0]);-- final AheadBehind aheadBehind = new AheadBehind();- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- StringTokenizer tokenizer = new StringTokenizer(line);- aheadBehind.setBehind(Integer.parseInt(tokenizer.nextToken()));- aheadBehind.setAhead(Integer.parseInt(tokenizer.nextToken()));- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- logger.error(line);- }-- }).checkReturnCode();-- Map<String, AheadBehind> aheadBehinds = new HashMap<>();- aheadBehinds.put(compareRevs[0], aheadBehind);- return aheadBehinds;- } else {- return new HashMap<String, AheadBehind>();- }- }-- private void parseCommitLine(Map<String, Set<String>> commits, String line) {- Set<String> parents = new HashSet<>();- StringTokenizer tokenizer = new StringTokenizer(line);- String commit = tokenizer.nextToken();- while (tokenizer.hasMoreTokens())- parents.add(tokenizer.nextToken());- commits.put(commit, parents);- }-- private Set<String> findAncestors(Map<String, Set<String>> commits, String commit) {- Set<String> ancestors = new HashSet<>();- Stack<String> stack = new Stack<>();- stack.push(commit);- while (!stack.isEmpty()) {- commit = stack.pop();- if (!ancestors.contains(commit)) {- Set<String> parents = commits.get(commit);- if (parents != null) {- ancestors.add(commit);- for (String parent: parents)- stack.push(parent);- }- }- }- return ancestors;- }--}diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.javaindex fef6c38ae1..c0e32d6232 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java@@ -1,990 +1,990 @@-package com.pmease.gitplex.core.model;--import java.io.File;-import java.io.IOException;-import java.io.InputStream;-import java.io.Serializable;-import java.util.ArrayList;-import java.util.Collection;-import java.util.Collections;-import java.util.Comparator;-import java.util.Date;-import java.util.HashMap;-import java.util.HashSet;-import java.util.List;-import java.util.Map;-import java.util.Set;-import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.locks.ReadWriteLock;--import javax.annotation.Nullable;-import javax.persistence.Column;-import javax.persistence.Entity;-import javax.persistence.FetchType;-import javax.persistence.JoinColumn;-import javax.persistence.Lob;-import javax.persistence.ManyToOne;-import javax.persistence.OneToMany;-import javax.persistence.Table;-import javax.persistence.UniqueConstraint;-import javax.validation.Valid;-import javax.validation.constraints.NotNull;--import org.apache.commons.lang3.SerializationUtils;-import org.eclipse.jgit.diff.DiffEntry;-import org.eclipse.jgit.diff.DiffFormatter;-import org.eclipse.jgit.errors.RevisionSyntaxException;-import org.eclipse.jgit.internal.storage.file.FileRepository;-import org.eclipse.jgit.lib.AnyObjectId;-import org.eclipse.jgit.lib.Constants;-import org.eclipse.jgit.lib.FileMode;-import org.eclipse.jgit.lib.ObjectId;-import org.eclipse.jgit.lib.ObjectLoader;-import org.eclipse.jgit.lib.Ref;-import org.eclipse.jgit.lib.RepositoryCache;-import org.eclipse.jgit.lib.RepositoryCache.FileKey;-import org.eclipse.jgit.revwalk.LastCommitsOfChildren;-import org.eclipse.jgit.revwalk.LastCommitsOfChildren.Value;-import org.eclipse.jgit.revwalk.RevCommit;-import org.eclipse.jgit.revwalk.RevObject;-import org.eclipse.jgit.revwalk.RevTag;-import org.eclipse.jgit.revwalk.RevTree;-import org.eclipse.jgit.revwalk.RevWalk;-import org.eclipse.jgit.revwalk.filter.RevFilter;-import org.eclipse.jgit.treewalk.TreeWalk;-import org.eclipse.jgit.treewalk.filter.OrTreeFilter;-import org.eclipse.jgit.treewalk.filter.PathFilter;-import org.eclipse.jgit.treewalk.filter.TreeFilter;-import org.eclipse.jgit.util.FS;-import org.eclipse.jgit.util.io.NullOutputStream;-import org.hibernate.annotations.Cache;-import org.hibernate.annotations.CacheConcurrencyStrategy;-import org.hibernate.annotations.OnDelete;-import org.hibernate.annotations.OnDeleteAction;-import org.hibernate.validator.constraints.NotEmpty;--import com.google.common.base.Objects;-import com.google.common.base.Optional;-import com.google.common.base.Preconditions;-import com.pmease.commons.git.Blob;-import com.pmease.commons.git.BlobIdent;-import com.pmease.commons.git.Commit;-import com.pmease.commons.git.Git;-import com.pmease.commons.git.GitUtils;-import com.pmease.commons.git.Submodule;-import com.pmease.commons.git.exception.NotFileException;-import com.pmease.commons.git.exception.ObjectNotExistException;-import com.pmease.commons.hibernate.AbstractEntity;-import com.pmease.commons.loader.AppLoader;-import com.pmease.commons.util.FileUtils;-import com.pmease.commons.util.LockUtils;-import com.pmease.commons.util.StringUtils;-import com.pmease.commons.wicket.editable.annotation.Editable;-import com.pmease.commons.wicket.editable.annotation.Markdown;-import com.pmease.gitplex.core.GitPlex;-import com.pmease.gitplex.core.gatekeeper.AndGateKeeper;-import com.pmease.gitplex.core.gatekeeper.GateKeeper;-import com.pmease.gitplex.core.listeners.RefListener;-import com.pmease.gitplex.core.manager.StorageManager;-import com.pmease.gitplex.core.permission.object.ProtectedObject;-import com.pmease.gitplex.core.permission.object.UserBelonging;-import com.pmease.gitplex.core.validation.RepositoryName;--@Entity-@Table(uniqueConstraints={@UniqueConstraint(columnNames={"owner", "name"})})-@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)-@Editable-@SuppressWarnings("serial")-public class Repository extends AbstractEntity implements UserBelonging {-- private static final String FQN_SEPARATOR = "/";-- public static final String REFS_GITPLEX = "refs/gitplex/";-- private static final int LAST_COMMITS_CACHE_THRESHOLD = 1000;-- private static final int MAX_READ_BLOB_SIZE = 5*1024*1024;-- @ManyToOne(fetch=FetchType.LAZY)- @JoinColumn(nullable=false)- private User owner;-- @ManyToOne(fetch=FetchType.LAZY)- @JoinColumn(nullable=true)- private Repository forkedFrom;-- @Column(nullable=false)- private String name;-- private String description;-- @Lob- @Column(nullable=false, length=65535)- private ArrayList<GateKeeper> gateKeepers = new ArrayList<>();-- @Lob- @Column(nullable=false, length=65535)- private ArrayList<IntegrationPolicy> integrationPolicies = new ArrayList<>();-- @Column(nullable=false)- private Date createdAt = new Date();-- @OneToMany(mappedBy="targetRepo")- @OnDelete(action=OnDeleteAction.CASCADE)- private Collection<PullRequest> incomingRequests = new ArrayList<>();-- @OneToMany(mappedBy="sourceRepo")- private Collection<PullRequest> outgoingRequests = new ArrayList<>();-- @OneToMany(mappedBy="repository")- @OnDelete(action=OnDeleteAction.CASCADE)- private Collection<Authorization> authorizations = new ArrayList<>();-- @OneToMany(mappedBy="forkedFrom")- private Collection<Repository> forks = new ArrayList<>();-- private transient Map<BlobIdent, Blob> blobCache;-- private transient Map<DiffKey, List<DiffEntry>> diffCache;-- private transient Map<String, Commit> commitCache;-- private transient Map<String, Optional<ObjectId>> objectIdCache;-- private transient Map<String, Map<String, Ref>> prefixRefsCache;-- private transient Map<AnyObjectId, RevObject> revObjectCache;-- private transient Map<RevObject, Optional<RevCommit>> revCommitCache;-- private transient Map<String, Optional<Ref>> refCache;-- private transient String defaultBranch;-- public User getOwner() {- return owner;- }-- public void setOwner(User owner) {- this.owner = owner;- }-- @Editable(order=100, description=- "Specify name of the repository. It will be used to identify the repository when accessing via Git.")- @RepositoryName- @NotEmpty- public String getName() {- return name;- }-- public void setName(String name) {- this.name = name;- }-- @Editable(order=200, description="Specify description of the repository.")- @Markdown- public String getDescription() {- return description;- }-- public void setDescription(String description) {- this.description = description;- }-- @NotNull- @Valid- public List<GateKeeper> getGateKeepers() {- return gateKeepers;- }-- public void setGateKeepers(ArrayList<GateKeeper> gateKeepers) {- this.gateKeepers = gateKeepers;- }-- @Valid- public List<IntegrationPolicy> getIntegrationPolicies() {- return integrationPolicies;- }-- public void setIntegrationPolicies(ArrayList<IntegrationPolicy> integrationPolicies) {- this.integrationPolicies = integrationPolicies;- }-- public Date getCreatedAt() {- return createdAt;- }-- public void setCreatedAt(Date createdAt) {- this.createdAt = createdAt;- }-- @Override- public User getUser() {- return getOwner();- }-- public Collection<PullRequest> getIncomingRequests() {- return incomingRequests;- }-- public void setIncomingRequests(Collection<PullRequest> incomingRequests) {- this.incomingRequests = incomingRequests;- }-- public Collection<PullRequest> getOutgoingRequests() {- return outgoingRequests;- }-- public void setOutgoingRequests(Collection<PullRequest> outgoingRequests) {- this.outgoingRequests = outgoingRequests;- }-- public Collection<Authorization> getAuthorizations() {- return authorizations;- }-- public void setAuthorizations(Collection<Authorization> authorizations) {- this.authorizations = authorizations;- }-- public Repository getForkedFrom() {- return forkedFrom;- }-- public void setForkedFrom(Repository forkedFrom) {- this.forkedFrom = forkedFrom;- }-- public Collection<Repository> getForks() {- return forks;- }-- public void setForks(Collection<Repository> forks) {- this.forks = forks;- }-- public FileRepository openAsJGitRepo() {- try {- return (FileRepository) RepositoryCache.open(FileKey.exact(git().repoDir(), FS.DETECTED), true);- } catch (IOException e) {- throw new RuntimeException(e);- }- }-- public List<Ref> getBranchRefs() {- List<Ref> refs = new ArrayList<Ref>(getRefs(Constants.R_HEADS).values());- Collections.sort(refs, new Comparator<Ref>() {-- @Override- public int compare(Ref o1, Ref o2) {- if (o1.getObjectId().equals(o2.getObjectId())) {- return o1.getName().compareTo(o2.getName());- } else {- RevCommit commit1 = getRevCommit(o1.getObjectId());- RevCommit commit2 = getRevCommit(o2.getObjectId());- return commit2.getCommitTime() - commit1.getCommitTime();- }- }-- });- return refs;- }-- public List<Ref> getTagRefs() {- List<Ref> refs = new ArrayList<>();- for (Ref ref: getRefs(Constants.R_TAGS).values()) {- if (getRevCommit(ref.getObjectId(), false) != null)- refs.add(ref);- }- Collections.sort(refs, new Comparator<Ref>() {-- @Override- public int compare(Ref o1, Ref o2) {- RevObject obj1 = getRevObject(o1.getObjectId());- RevObject obj2 = getRevObject(o2.getObjectId());- if (obj1 instanceof RevTag && obj2 instanceof RevTag) {- RevTag tag1 = (RevTag) obj1;- RevTag tag2 = (RevTag) obj2;- if (tag1.getTaggerIdent() != null && tag2.getTaggerIdent() != null)- return tag2.getTaggerIdent().getWhen().compareTo(tag1.getTaggerIdent().getWhen());- }- RevCommit commit1 = getRevCommit(o1.getObjectId());- RevCommit commit2 = getRevCommit(o2.getObjectId());- if (commit1.getId().equals(commit2.getId()))- return o1.getName().compareTo(o2.getName());- else- return commit2.getCommitTime() - commit1.getCommitTime();- }-- });- return refs;- }-- @Override- public boolean has(ProtectedObject object) {- if (object instanceof Repository) {- Repository repository = (Repository) object;- return repository.getId().equals(getId());- } else {- return false;- }- }-- public String getFQN() {- return getOwner().getName() + FQN_SEPARATOR + getName();- }-- public static String getNameByFQN(String repositoryFQN) {- return StringUtils.substringAfterLast(repositoryFQN, FQN_SEPARATOR);- }-- public static String getUserNameByFQN(String repositoryFQN) {- return StringUtils.substringBeforeLast(repositoryFQN, FQN_SEPARATOR);- }-- @Override- public String toString() {- return getFQN();- }-- public Git git() {- return new Git(AppLoader.getInstance(StorageManager.class).getRepoDir(this));- }-- /**- * Whether or not specified git represents a valid repository git. This can be used to tell- * apart a GitPlex repository git from some other Git repositories.- *- * @return- * <tt>true</tt> if valid; <tt>false</tt> otherwise- */- public boolean isValid() {- return isUpdateHookValid() && isPostReceiveHookValid();- }-- public boolean isUpdateHookValid() {- File updateHook = new File(git().repoDir(), "hooks/update");- if (!updateHook.exists())- return false;-- try {- String content = FileUtils.readFileToString(updateHook);- if (!content.contains("GITPLEX_USER_ID"))- return false;- } catch (IOException e) {- throw new RuntimeException(e);- }-- if (!updateHook.canExecute())- return false;-- return true;- }-- public boolean isPostReceiveHookValid() {- File postReceiveHook = new File(git().repoDir(), "hooks/post-receive");- if (!postReceiveHook.exists())- return false;-- try {- String content = FileUtils.readFileToString(postReceiveHook);- if (!content.contains("GITPLEX_USER_ID"))- return false;- } catch (IOException e) {- throw new RuntimeException(e);- }-- if (!postReceiveHook.canExecute())- return false;-- return true;- }-- public GateKeeper getGateKeeper() {- AndGateKeeper andGateKeeper = new AndGateKeeper();- for (GateKeeper each: getGateKeepers())- andGateKeeper.getGateKeepers().add(each);- return andGateKeeper;- }-- /**- * Find fork root of this repository.- *- * @return- * fork root of this repository, or <tt>null</tt> if the repository is not- * forked from any other repository- */- public @Nullable Repository findForkRoot() {- if (forkedFrom != null) {- Repository forkedRoot = forkedFrom.findForkRoot();- if (forkedRoot != null)- return forkedRoot;- else- return forkedFrom;- } else {- return null;- }- }-- /**- * Find all descendant repositories forking from current repository.- *- * @return- * all descendant repositories forking from current repository- */- public List<Repository> findForkDescendants() {- List<Repository> descendants = new ArrayList<>();- for (Repository fork: getForks()) {- descendants.add(fork);- descendants.addAll(fork.findForkDescendants());- }-- return descendants;- }-- /**- * Find all comparable repositories of current repository. Comparable repositories can- * be connected via forks, and can be compared/pulled.- *- * @return- * comparable repositories of current repository, with current repository also- * included in the collection- */- public List<Repository> findAffinals() {- List<Repository> affinals = new ArrayList<Repository>();- Repository forkRoot = findForkRoot();- if (forkRoot != null) {- affinals.add(forkRoot);- affinals.addAll(forkRoot.findForkDescendants());- } else {- affinals.add(this);- affinals.addAll(findForkDescendants());- }- return affinals;- }-- public RevCommit getMergeBase(String ancestor, String descendant) {- try ( FileRepository jgitRepo = openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo)) {- ObjectId ancestorId = getObjectId(ancestor);- ObjectId descendantId = getObjectId(descendant);- revWalk.setRevFilter(RevFilter.MERGE_BASE);- RevCommit ancestorCommit = getRevCommit(ancestorId);- RevCommit descendantCommit = getRevCommit(descendantId);-- // we should look up commit again as markStart requires that the commit- // should be resolved in the same revWalk. Also we should not look up- // against ancestorId directly as it might be id of an annotated tag- revWalk.markStart(revWalk.lookupCommit(ancestorCommit.getId()));- revWalk.markStart(revWalk.lookupCommit(descendantCommit.getId()));- return Preconditions.checkNotNull(revWalk.next());- } catch (IOException e) {- throw new RuntimeException(e);- }- }-- public boolean isAncestor(String ancestor, String descendant) {- try ( FileRepository jgitRepo = openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo)) {- ObjectId ancestorId = getObjectId(ancestor);- ObjectId descendantId = getObjectId(descendant);- revWalk.setRevFilter(RevFilter.MERGE_BASE);- RevCommit ancestorCommit = getRevCommit(ancestorId);- RevCommit descendantCommit = getRevCommit(descendantId);-- // we should look up commit again as markStart requires that the commit- // should be resolved in the same revWalk. Also we should not look up- // against ancestorId directly as it might be id of an annotated tag- revWalk.markStart(revWalk.lookupCommit(ancestorCommit.getId()));- revWalk.markStart(revWalk.lookupCommit(descendantCommit.getId()));- return ancestorId.equals(Preconditions.checkNotNull(revWalk.next().getId()));- } catch (IOException e) {- throw new RuntimeException(e);- }- }-- public String getUrl() {- return GitPlex.getInstance().guessServerUrl() + "/" + getFQN();- }-- public String getDefaultBranch() {- if (defaultBranch == null)- defaultBranch = git().resolveDefaultBranch();- return defaultBranch;- }-- public String defaultBranchIfNull(@Nullable String revision) {- if (revision != null)- return revision;- else- return getDefaultBranch();- }-- private Blob readBlob(ObjectLoader objectLoader, BlobIdent ident) {- long blobSize = objectLoader.getSize();- if (blobSize > MAX_READ_BLOB_SIZE) {- try (InputStream is = objectLoader.openStream()) {- byte[] bytes = new byte[MAX_READ_BLOB_SIZE];- is.read(bytes);- return new Blob(ident, bytes, blobSize);- } catch (IOException e) {- throw new RuntimeException(e);- }- } else {- return new Blob(ident, objectLoader.getCachedBytes());- }- }-- private Map<BlobIdent, Blob> getBlobCache() {- if (blobCache == null) {- synchronized(this) {- if (blobCache == null)- blobCache = new ConcurrentHashMap<>();- }- }- return blobCache;- }-- /**- * Read blob content and cache result in repository in case the same blob- * content is requested again.- *- * We made this method thread-safe as we are using ForkJoinPool to calculate- * diffs of multiple blob changes concurrently, and this method will be- * accessed concurrently in that special case.- *- * @param blobIdent- * ident of the blob- * @return- * blob of specified blob ident- * @throws- * ObjectNotExistException if blob of specified ident can not be found in repository- *- */- public Blob getBlob(BlobIdent blobIdent) {- Preconditions.checkArgument(blobIdent.revision!=null && blobIdent.path!=null && blobIdent.mode!=null,- "Revision, path and mode of ident param should be specified");-- Blob blob = getBlobCache().get(blobIdent);- if (blob == null) {- if (blobIdent.id != null) {- try (FileRepository jgitRepo = openAsJGitRepo()) {- if (blobIdent.isGitLink()) {- String url = getSubmodules(blobIdent.revision).get(blobIdent.path);- if (url == null)- throw new ObjectNotExistException("Unable to find submodule '" + blobIdent.path + "' in .gitmodules");- blob = new Blob(blobIdent, new Submodule(url, blobIdent.id).toString().getBytes());- } else if (blobIdent.isTree()) {- throw new NotFileException("Path '" + blobIdent.path + "' is a tree");- } else {- ObjectLoader objectLoader = jgitRepo.open(ObjectId.fromString(blobIdent.id), Constants.OBJ_BLOB);- blob = readBlob(objectLoader, blobIdent);- }- getBlobCache().put(blobIdent, blob);- } catch (IOException e) {- throw new RuntimeException(e);- }- } else {- try ( FileRepository jgitRepo = openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo)) {- ObjectId commitId = getObjectId(blobIdent.revision);- RevTree revTree = revWalk.parseCommit(commitId).getTree();- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, blobIdent.path, revTree);- if (treeWalk != null) {- if (blobIdent.isGitLink()) {- String url = getSubmodules(blobIdent.revision).get(blobIdent.path);- if (url == null)- throw new ObjectNotExistException("Unable to find submodule '" + blobIdent.path + "' in .gitmodules");- String hash = treeWalk.getObjectId(0).name();- blob = new Blob(blobIdent, new Submodule(url, hash).toString().getBytes());- } else if (blobIdent.isTree()) {- throw new NotFileException("Path '" + blobIdent.path + "' is a tree");- } else {- ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));- blob = readBlob(objectLoader, blobIdent);- }- getBlobCache().put(blobIdent, blob);- } else {- throw new ObjectNotExistException("Unable to find blob path '" + blobIdent.path + "' in revision '" + blobIdent.revision + "'");- }- } catch (IOException e) {- throw new RuntimeException(e);- }- }- }- return blob;- }-- public InputStream getInputStream(BlobIdent ident) {- try ( FileRepository jgitRepo = openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo)) {- ObjectId commitId = getObjectId(ident.revision);- RevTree revTree = revWalk.parseCommit(commitId).getTree();- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, ident.path, revTree);- if (treeWalk != null) {- ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));- return objectLoader.openStream();- } else {- throw new ObjectNotExistException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");- }- } catch (IOException e) {- throw new RuntimeException(e);- }- }-- /**- * Get cached object id of specified revision.- *- * @param revision- * revision to resolve object id for- * @param mustExist- * true to have the method throwing exception instead- * of returning null if the revision does not exist- * @return- * object id of specified revision, or <tt>null</tt> if revision- * does not exist and mustExist is specified as false- */- @Nullable- public ObjectId getObjectId(String revision, boolean mustExist) {- if (objectIdCache == null)- objectIdCache = new HashMap<>();-- Optional<ObjectId> optional = objectIdCache.get(revision);- if (optional == null) {- ObjectId objectId;- try (FileRepository jgitRepo = openAsJGitRepo()) {- objectId = jgitRepo.resolve(revision);- } catch (RevisionSyntaxException | IOException e) {- if (!mustExist) {- objectId = null;- } else {- throw new RuntimeException(e);- }- }- optional = Optional.fromNullable(objectId);- objectIdCache.put(revision, optional);- }- if (mustExist && !optional.isPresent())- throw new ObjectNotExistException("Unable to find revision '" + revision + "'");- return optional.orNull();- }-- public ObjectId getObjectId(String revision) {- return getObjectId(revision, true);- }-- public void cacheObjectId(String revision, @Nullable ObjectId objectId) {- if (objectIdCache == null)- objectIdCache = new HashMap<>();-- objectIdCache.put(revision, Optional.fromNullable(objectId));- }-- public List<DiffEntry> getDiffs(String oldRev, String newRev, boolean detectRenames, String...paths) {- if (diffCache == null)- diffCache = new HashMap<>();-- DiffKey key = new DiffKey(oldRev, newRev, detectRenames, paths);- List<DiffEntry> diffs = diffCache.get(key);- if (diffs == null) {- try ( FileRepository jgitRepo = openAsJGitRepo();- DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE);) {- diffFormatter.setRepository(jgitRepo);- diffFormatter.setDetectRenames(detectRenames);- AnyObjectId oldCommitId = getObjectId(oldRev);- AnyObjectId newCommitId = getObjectId(newRev);- if (paths.length >= 2) {- List<TreeFilter> pathFilters = new ArrayList<>();- for (String path: paths)- pathFilters.add(PathFilter.create(path));- diffFormatter.setPathFilter(OrTreeFilter.create(pathFilters));- } else if (paths.length == 1) {- diffFormatter.setPathFilter(PathFilter.create(paths[0]));- }- diffs = new ArrayList<>();- for (DiffEntry entry: diffFormatter.scan(oldCommitId, newCommitId)) {- if (!Objects.equal(entry.getOldPath(), entry.getNewPath())- || !Objects.equal(entry.getOldMode(), entry.getNewMode())- || entry.getOldId()==null || !entry.getOldId().isComplete()- || entry.getNewId()== null || !entry.getNewId().isComplete()- || !entry.getOldId().equals(entry.getNewId())) {- diffs.add(entry);- }- }- } catch (IOException e) {- throw new RuntimeException(e);- }- diffCache.put(key, diffs);- }- return diffs;- }-- public Commit getCommit(String commitHash) {- if (commitCache == null)- commitCache = new HashMap<>();-- Commit commit = commitCache.get(commitHash);- if (commit == null) {- commit = git().showRevision(commitHash);- commitCache.put(commitHash, commit);- }- return commit;- }-- public void cacheCommits(List<Commit> commits) {- if (commitCache == null)- commitCache = new HashMap<>();-- for (Commit commit: commits)- commitCache.put(commit.getHash(), commit);- }-- public LastCommitsOfChildren getLastCommitsOfChildren(String revision, @Nullable String path) {- if (path == null)- path = "";-- final File cacheDir = new File(- GitPlex.getInstance(StorageManager.class).getCacheDir(this),- "last_commits/" + path + "/gitplex_last_commits");-- final ReadWriteLock lock;- try {- lock = LockUtils.getReadWriteLock(cacheDir.getCanonicalPath());- } catch (IOException e) {- throw new RuntimeException(e);- }-- final Set<ObjectId> commitIds = new HashSet<>();-- lock.readLock().lock();- try {- if (cacheDir.exists()) {- for (String each: cacheDir.list())- commitIds.add(ObjectId.fromString(each));- }- } finally {- lock.readLock().unlock();- }-- org.eclipse.jgit.revwalk.LastCommitsOfChildren.Cache cache;- if (!commitIds.isEmpty()) {- cache = new org.eclipse.jgit.revwalk.LastCommitsOfChildren.Cache() {-- @SuppressWarnings("unchecked")- @Override- public Map<String, Value> getLastCommitsOfChildren(ObjectId commitId) {- if (commitIds.contains(commitId)) {- lock.readLock().lock();- try {- byte[] bytes = FileUtils.readFileToByteArray(new File(cacheDir, commitId.name()));- return (Map<String, Value>) SerializationUtils.deserialize(bytes);- } catch (IOException e) {- throw new RuntimeException(e);- } finally {- lock.readLock().unlock();- }- } else {- return null;- }- }-- };- } else {- cache = null;- }-- final AnyObjectId commitId = getObjectId(revision);-- try (FileRepository jgitRepo = openAsJGitRepo()) {- long time = System.currentTimeMillis();- LastCommitsOfChildren lastCommits = new LastCommitsOfChildren(jgitRepo, commitId, path, cache);- long elapsed = System.currentTimeMillis()-time;- if (elapsed > LAST_COMMITS_CACHE_THRESHOLD) {- lock.writeLock().lock();- try {- if (!cacheDir.exists())- FileUtils.createDir(cacheDir);- FileUtils.writeByteArrayToFile(- new File(cacheDir, commitId.name()),- SerializationUtils.serialize(lastCommits));- } catch (IOException e) {- throw new RuntimeException(e);- } finally {- lock.writeLock().unlock();- }- }- return lastCommits;- }- }-- @Nullable- public Ref getRef(String revision) {- if (refCache == null)- refCache = new HashMap<>();- Optional<Ref> optional = refCache.get(revision);- if (optional == null) {- try (FileRepository jgitRepo = openAsJGitRepo()) {- optional = Optional.fromNullable(jgitRepo.getRef(revision));- } catch (IOException e) {- throw new RuntimeException(e);- }- refCache.put(revision, optional);- }- return optional.orNull();- }-- public RevObject getRevObject(AnyObjectId revId) {- if (revObjectCache == null)- revObjectCache = new HashMap<>();- RevObject revObject = revObjectCache.get(revId);- if (revObject == null) {- try ( FileRepository jgitRepo = openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo);) {- revObject = revWalk.parseAny(revId);- revObjectCache.put(revId, revObject);- } catch (IOException e) {- throw new RuntimeException(e);- }- }- return revObject;- }-- public RevCommit getRevCommit(RevObject revObject) {- return getRevCommit(revObject, true);- }-- @Nullable- public RevCommit getRevCommit(RevObject revObject, boolean mustBeCommit) {- if (revCommitCache == null)- revCommitCache = new HashMap<>();- Optional<RevCommit> optional = revCommitCache.get(revObject);- if (optional == null) {- try ( FileRepository jgitRepo = openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo);) {- RevObject peeled = revWalk.peel(revObject);- if (peeled instanceof RevCommit)- optional = Optional.of((RevCommit) peeled);- else- optional = Optional.absent();- } catch (IOException e) {- throw new RuntimeException(e);- }- revCommitCache.put(revObject, optional);- }- if (mustBeCommit)- return optional.get();- else- return optional.orNull();- }-- @Nullable- public RevCommit getRevCommit(AnyObjectId revId, boolean mustExist) {- return getRevCommit(getRevObject(revId), mustExist);- }-- @Nullable- public RevCommit getRevCommit(String revision, boolean mustExist) {- ObjectId id = getObjectId(revision, mustExist);- if (id != null)- return getRevCommit(id, mustExist);- else- return null;- }-- public RevCommit getRevCommit(String revision) {- return getRevCommit(revision, true);- }-- public RevCommit getRevCommit(AnyObjectId revId) {- return getRevCommit(getRevObject(revId), true);- }-- public Map<String, Ref> getRefs(String prefix) {- if (prefixRefsCache == null)- prefixRefsCache = new HashMap<>();-- Map<String, Ref> cached = prefixRefsCache.get(prefix);- if (cached == null) {- try (FileRepository jgitRepo = openAsJGitRepo()) {- cached = jgitRepo.getRefDatabase().getRefs(prefix);- prefixRefsCache.put(prefix, cached);- } catch (IOException e) {- throw new RuntimeException(e);- }- }- return cached;- }-- public Map<String, String> getSubmodules(String revision) {- Map<String, String> submodules = new HashMap<>();-- Blob blob = getBlob(new BlobIdent(revision, ".gitmodules", FileMode.REGULAR_FILE.getBits()));- String content = new String(blob.getBytes());-- String path = null;- String url = null;-- for (String line: StringUtils.splitAndTrim(content, "\r\n")) {- if (line.startsWith("[") && line.endsWith("]")) {- if (path != null && url != null)- submodules.put(path, url);-- path = url = null;- } else if (line.startsWith("path")) {- path = StringUtils.substringAfter(line, "=").trim();- } else if (line.startsWith("url")) {- url = StringUtils.substringAfter(line, "=").trim();- }- }- if (path != null && url != null)- submodules.put(path, url);-- return submodules;- }-- public String getRevisionFQN(String revision) {- return getFQN() + ":" + revision;- }-- public void deleteBranch(String branch) {- String refName = GitUtils.branch2ref(branch);- git().deleteRef(refName);- for (RefListener listener: GitPlex.getExtensions(RefListener.class))- listener.onRefUpdate(this, refName, null);- }-- private static class DiffKey implements Serializable {- String oldRev;-- String newRev;-- String[] paths;-- boolean detectRenames;-- DiffKey(String oldRev, String newRev, boolean detectRenames, String...paths) {- this.oldRev = oldRev;- this.newRev = newRev;- this.detectRenames = detectRenames;- this.paths = paths;- }-- public boolean equals(Object other) {- if (!(other instanceof DiffKey))- return false;- if (this == other)- return true;- DiffKey otherKey = (DiffKey) other;- return Objects.equal(oldRev, otherKey.oldRev)- && Objects.equal(newRev, otherKey.newRev)- && Objects.equal(paths, otherKey.paths)- && Objects.equal(detectRenames, otherKey.detectRenames);- }-- public int hashCode() {- return Objects.hashCode(oldRev, newRev, paths, detectRenames);- }-- }--}+package com.pmease.gitplex.core.model;++import java.io.File;+import java.io.IOException;+import java.io.InputStream;+import java.io.Serializable;+import java.util.ArrayList;+import java.util.Collection;+import java.util.Collections;+import java.util.Comparator;+import java.util.Date;+import java.util.HashMap;+import java.util.HashSet;+import java.util.List;+import java.util.Map;+import java.util.Set;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.locks.ReadWriteLock;++import javax.annotation.Nullable;+import javax.persistence.Column;+import javax.persistence.Entity;+import javax.persistence.FetchType;+import javax.persistence.JoinColumn;+import javax.persistence.Lob;+import javax.persistence.ManyToOne;+import javax.persistence.OneToMany;+import javax.persistence.Table;+import javax.persistence.UniqueConstraint;+import javax.validation.Valid;+import javax.validation.constraints.NotNull;++import org.apache.commons.lang3.SerializationUtils;+import org.eclipse.jgit.diff.DiffEntry;+import org.eclipse.jgit.diff.DiffFormatter;+import org.eclipse.jgit.errors.RevisionSyntaxException;+import org.eclipse.jgit.internal.storage.file.FileRepository;+import org.eclipse.jgit.lib.AnyObjectId;+import org.eclipse.jgit.lib.Constants;+import org.eclipse.jgit.lib.FileMode;+import org.eclipse.jgit.lib.ObjectId;+import org.eclipse.jgit.lib.ObjectLoader;+import org.eclipse.jgit.lib.Ref;+import org.eclipse.jgit.lib.RepositoryCache;+import org.eclipse.jgit.lib.RepositoryCache.FileKey;+import org.eclipse.jgit.revwalk.LastCommitsOfChildren;+import org.eclipse.jgit.revwalk.LastCommitsOfChildren.Value;+import org.eclipse.jgit.revwalk.RevCommit;+import org.eclipse.jgit.revwalk.RevObject;+import org.eclipse.jgit.revwalk.RevTag;+import org.eclipse.jgit.revwalk.RevTree;+import org.eclipse.jgit.revwalk.RevWalk;+import org.eclipse.jgit.revwalk.filter.RevFilter;+import org.eclipse.jgit.treewalk.TreeWalk;+import org.eclipse.jgit.treewalk.filter.OrTreeFilter;+import org.eclipse.jgit.treewalk.filter.PathFilter;+import org.eclipse.jgit.treewalk.filter.TreeFilter;+import org.eclipse.jgit.util.FS;+import org.eclipse.jgit.util.io.NullOutputStream;+import org.hibernate.annotations.Cache;+import org.hibernate.annotations.CacheConcurrencyStrategy;+import org.hibernate.annotations.OnDelete;+import org.hibernate.annotations.OnDeleteAction;+import org.hibernate.validator.constraints.NotEmpty;++import com.google.common.base.Objects;+import com.google.common.base.Optional;+import com.google.common.base.Preconditions;+import com.pmease.commons.git.Blob;+import com.pmease.commons.git.BlobIdent;+import com.pmease.commons.git.Commit;+import com.pmease.commons.git.Git;+import com.pmease.commons.git.GitUtils;+import com.pmease.commons.git.Submodule;+import com.pmease.commons.git.exception.NotFileException;+import com.pmease.commons.git.exception.ObjectNotExistException;+import com.pmease.commons.hibernate.AbstractEntity;+import com.pmease.commons.loader.AppLoader;+import com.pmease.commons.util.FileUtils;+import com.pmease.commons.util.LockUtils;+import com.pmease.commons.util.StringUtils;+import com.pmease.commons.wicket.editable.annotation.Editable;+import com.pmease.commons.wicket.editable.annotation.Markdown;+import com.pmease.gitplex.core.GitPlex;+import com.pmease.gitplex.core.gatekeeper.AndGateKeeper;+import com.pmease.gitplex.core.gatekeeper.GateKeeper;+import com.pmease.gitplex.core.listeners.RefListener;+import com.pmease.gitplex.core.manager.StorageManager;+import com.pmease.gitplex.core.permission.object.ProtectedObject;+import com.pmease.gitplex.core.permission.object.UserBelonging;+import com.pmease.gitplex.core.validation.RepositoryName;++@Entity+@Table(uniqueConstraints={@UniqueConstraint(columnNames={"owner", "name"})})+@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)+@Editable+@SuppressWarnings("serial")+public class Repository extends AbstractEntity implements UserBelonging {++ private static final String FQN_SEPARATOR = "/";++ public static final String REFS_GITPLEX = "refs/gitplex/";++ private static final int LAST_COMMITS_CACHE_THRESHOLD = 1000;++ private static final int MAX_READ_BLOB_SIZE = 5*1024*1024;++ @ManyToOne(fetch=FetchType.LAZY)+ @JoinColumn(nullable=false)+ private User owner;++ @ManyToOne(fetch=FetchType.LAZY)+ @JoinColumn(nullable=true)+ private Repository forkedFrom;++ @Column(nullable=false)+ private String name;++ private String description;++ @Lob+ @Column(nullable=false, length=65535)+ private ArrayList<GateKeeper> gateKeepers = new ArrayList<>();++ @Lob+ @Column(nullable=false, length=65535)+ private ArrayList<IntegrationPolicy> integrationPolicies = new ArrayList<>();++ @Column(nullable=false)+ private Date createdAt = new Date();++ @OneToMany(mappedBy="targetRepo")+ @OnDelete(action=OnDeleteAction.CASCADE)+ private Collection<PullRequest> incomingRequests = new ArrayList<>();++ @OneToMany(mappedBy="sourceRepo")+ private Collection<PullRequest> outgoingRequests = new ArrayList<>();++ @OneToMany(mappedBy="repository")+ @OnDelete(action=OnDeleteAction.CASCADE)+ private Collection<Authorization> authorizations = new ArrayList<>();++ @OneToMany(mappedBy="forkedFrom")+ private Collection<Repository> forks = new ArrayList<>();++ private transient Map<BlobIdent, Blob> blobCache;++ private transient Map<DiffKey, List<DiffEntry>> diffCache;++ private transient Map<String, Commit> commitCache;++ private transient Map<String, Optional<ObjectId>> objectIdCache;++ private transient Map<String, Map<String, Ref>> prefixRefsCache;++ private transient Map<AnyObjectId, RevObject> revObjectCache;++ private transient Map<RevObject, Optional<RevCommit>> revCommitCache;++ private transient Map<String, Optional<Ref>> refCache;++ private transient String defaultBranch;++ public User getOwner() {+ return owner;+ }++ public void setOwner(User owner) {+ this.owner = owner;+ }++ @Editable(order=100, description=+ "Specify name of the repository. It will be used to identify the repository when accessing via Git.")+ @RepositoryName+ @NotEmpty+ public String getName() {+ return name;+ }++ public void setName(String name) {+ this.name = name;+ }++ @Editable(order=200, description="Specify description of the repository.")+ @Markdown+ public String getDescription() {+ return description;+ }++ public void setDescription(String description) {+ this.description = description;+ }++ @NotNull+ @Valid+ public List<GateKeeper> getGateKeepers() {+ return gateKeepers;+ }++ public void setGateKeepers(ArrayList<GateKeeper> gateKeepers) {+ this.gateKeepers = gateKeepers;+ }++ @Valid+ public List<IntegrationPolicy> getIntegrationPolicies() {+ return integrationPolicies;+ }++ public void setIntegrationPolicies(ArrayList<IntegrationPolicy> integrationPolicies) {+ this.integrationPolicies = integrationPolicies;+ }++ public Date getCreatedAt() {+ return createdAt;+ }++ public void setCreatedAt(Date createdAt) {+ this.createdAt = createdAt;+ }++ @Override+ public User getUser() {+ return getOwner();+ }++ public Collection<PullRequest> getIncomingRequests() {+ return incomingRequests;+ }++ public void setIncomingRequests(Collection<PullRequest> incomingRequests) {+ this.incomingRequests = incomingRequests;+ }++ public Collection<PullRequest> getOutgoingRequests() {+ return outgoingRequests;+ }++ public void setOutgoingRequests(Collection<PullRequest> outgoingRequests) {+ this.outgoingRequests = outgoingRequests;+ }++ public Collection<Authorization> getAuthorizations() {+ return authorizations;+ }++ public void setAuthorizations(Collection<Authorization> authorizations) {+ this.authorizations = authorizations;+ }++ public Repository getForkedFrom() {+ return forkedFrom;+ }++ public void setForkedFrom(Repository forkedFrom) {+ this.forkedFrom = forkedFrom;+ }++ public Collection<Repository> getForks() {+ return forks;+ }++ public void setForks(Collection<Repository> forks) {+ this.forks = forks;+ }++ public FileRepository openAsJGitRepo() {+ try {+ return (FileRepository) RepositoryCache.open(FileKey.exact(git().repoDir(), FS.DETECTED), true);+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }++ public List<Ref> getBranchRefs() {+ List<Ref> refs = new ArrayList<Ref>(getRefs(Constants.R_HEADS).values());+ Collections.sort(refs, new Comparator<Ref>() {++ @Override+ public int compare(Ref o1, Ref o2) {+ if (o1.getObjectId().equals(o2.getObjectId())) {+ return o1.getName().compareTo(o2.getName());+ } else {+ RevCommit commit1 = getRevCommit(o1.getObjectId());+ RevCommit commit2 = getRevCommit(o2.getObjectId());+ return commit2.getCommitTime() - commit1.getCommitTime();+ }+ }++ });+ return refs;+ }++ public List<Ref> getTagRefs() {+ List<Ref> refs = new ArrayList<>();+ for (Ref ref: getRefs(Constants.R_TAGS).values()) {+ if (getRevCommit(ref.getObjectId(), false) != null)+ refs.add(ref);+ }+ Collections.sort(refs, new Comparator<Ref>() {++ @Override+ public int compare(Ref o1, Ref o2) {+ RevObject obj1 = getRevObject(o1.getObjectId());+ RevObject obj2 = getRevObject(o2.getObjectId());+ if (obj1 instanceof RevTag && obj2 instanceof RevTag) {+ RevTag tag1 = (RevTag) obj1;+ RevTag tag2 = (RevTag) obj2;+ if (tag1.getTaggerIdent() != null && tag2.getTaggerIdent() != null)+ return tag2.getTaggerIdent().getWhen().compareTo(tag1.getTaggerIdent().getWhen());+ }+ RevCommit commit1 = getRevCommit(o1.getObjectId());+ RevCommit commit2 = getRevCommit(o2.getObjectId());+ if (commit1.getId().equals(commit2.getId()))+ return o1.getName().compareTo(o2.getName());+ else+ return commit2.getCommitTime() - commit1.getCommitTime();+ }++ });+ return refs;+ }++ @Override+ public boolean has(ProtectedObject object) {+ if (object instanceof Repository) {+ Repository repository = (Repository) object;+ return repository.getId().equals(getId());+ } else {+ return false;+ }+ }++ public String getFQN() {+ return getOwner().getName() + FQN_SEPARATOR + getName();+ }++ public static String getNameByFQN(String repositoryFQN) {+ return StringUtils.substringAfterLast(repositoryFQN, FQN_SEPARATOR);+ }++ public static String getUserNameByFQN(String repositoryFQN) {+ return StringUtils.substringBeforeLast(repositoryFQN, FQN_SEPARATOR);+ }++ @Override+ public String toString() {+ return getFQN();+ }++ public Git git() {+ return new Git(AppLoader.getInstance(StorageManager.class).getRepoDir(this));+ }++ /**+ * Whether or not specified git represents a valid repository git. This can be used to tell+ * apart a GitPlex repository git from some other Git repositories.+ *+ * @return+ * <tt>true</tt> if valid; <tt>false</tt> otherwise+ */+ public boolean isValid() {+ return isUpdateHookValid() && isPostReceiveHookValid();+ }++ public boolean isUpdateHookValid() {+ File updateHook = new File(git().repoDir(), "hooks/update");+ if (!updateHook.exists())+ return false;++ try {+ String content = FileUtils.readFileToString(updateHook);+ if (!content.contains("GITPLEX_USER_ID"))+ return false;+ } catch (IOException e) {+ throw new RuntimeException(e);+ }++ if (!updateHook.canExecute())+ return false;++ return true;+ }++ public boolean isPostReceiveHookValid() {+ File postReceiveHook = new File(git().repoDir(), "hooks/post-receive");+ if (!postReceiveHook.exists())+ return false;++ try {+ String content = FileUtils.readFileToString(postReceiveHook);+ if (!content.contains("GITPLEX_USER_ID"))+ return false;+ } catch (IOException e) {+ throw new RuntimeException(e);+ }++ if (!postReceiveHook.canExecute())+ return false;++ return true;+ }++ public GateKeeper getGateKeeper() {+ AndGateKeeper andGateKeeper = new AndGateKeeper();+ for (GateKeeper each: getGateKeepers())+ andGateKeeper.getGateKeepers().add(each);+ return andGateKeeper;+ }++ /**+ * Find fork root of this repository.+ *+ * @return+ * fork root of this repository, or <tt>null</tt> if the repository is not+ * forked from any other repository+ */+ public @Nullable Repository findForkRoot() {+ if (forkedFrom != null) {+ Repository forkedRoot = forkedFrom.findForkRoot();+ if (forkedRoot != null)+ return forkedRoot;+ else+ return forkedFrom;+ } else {+ return null;+ }+ }++ /**+ * Find all descendant repositories forking from current repository.+ *+ * @return+ * all descendant repositories forking from current repository+ */+ public List<Repository> findForkDescendants() {+ List<Repository> descendants = new ArrayList<>();+ for (Repository fork: getForks()) {+ descendants.add(fork);+ descendants.addAll(fork.findForkDescendants());+ }++ return descendants;+ }++ /**+ * Find all comparable repositories of current repository. Comparable repositories can+ * be connected via forks, and can be compared/pulled.+ *+ * @return+ * comparable repositories of current repository, with current repository also+ * included in the collection+ */+ public List<Repository> findAffinals() {+ List<Repository> affinals = new ArrayList<Repository>();+ Repository forkRoot = findForkRoot();+ if (forkRoot != null) {+ affinals.add(forkRoot);+ affinals.addAll(forkRoot.findForkDescendants());+ } else {+ affinals.add(this);+ affinals.addAll(findForkDescendants());+ }+ return affinals;+ }++ public RevCommit getMergeBase(String ancestor, String descendant) {+ try ( FileRepository jgitRepo = openAsJGitRepo();+ RevWalk revWalk = new RevWalk(jgitRepo)) {+ ObjectId ancestorId = getObjectId(ancestor);+ ObjectId descendantId = getObjectId(descendant);+ revWalk.setRevFilter(RevFilter.MERGE_BASE);+ RevCommit ancestorCommit = getRevCommit(ancestorId);+ RevCommit descendantCommit = getRevCommit(descendantId);++ // we should look up commit again as markStart requires that the commit+ // should be resolved in the same revWalk. Also we should not look up+ // against ancestorId directly as it might be id of an annotated tag+ revWalk.markStart(revWalk.lookupCommit(ancestorCommit.getId()));+ revWalk.markStart(revWalk.lookupCommit(descendantCommit.getId()));+ return Preconditions.checkNotNull(revWalk.next());+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }++ public boolean isAncestor(String ancestor, String descendant) {+ try ( FileRepository jgitRepo = openAsJGitRepo();+ RevWalk revWalk = new RevWalk(jgitRepo)) {+ ObjectId ancestorId = getObjectId(ancestor);+ ObjectId descendantId = getObjectId(descendant);+ revWalk.setRevFilter(RevFilter.MERGE_BASE);+ RevCommit ancestorCommit = getRevCommit(ancestorId);+ RevCommit descendantCommit = getRevCommit(descendantId);++ // we should look up commit again as markStart requires that the commit+ // should be resolved in the same revWalk. Also we should not look up+ // against ancestorId directly as it might be id of an annotated tag+ revWalk.markStart(revWalk.lookupCommit(ancestorCommit.getId()));+ revWalk.markStart(revWalk.lookupCommit(descendantCommit.getId()));+ return ancestorId.equals(Preconditions.checkNotNull(revWalk.next().getId()));+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }++ public String getUrl() {+ return GitPlex.getInstance().guessServerUrl() + "/" + getFQN();+ }++ public String getDefaultBranch() {+ if (defaultBranch == null)+ defaultBranch = git().resolveDefaultBranch();+ return defaultBranch;+ }++ public String defaultBranchIfNull(@Nullable String revision) {+ if (revision != null)+ return revision;+ else+ return getDefaultBranch();+ }++ private Blob readBlob(ObjectLoader objectLoader, BlobIdent ident) {+ long blobSize = objectLoader.getSize();+ if (blobSize > MAX_READ_BLOB_SIZE) {+ try (InputStream is = objectLoader.openStream()) {+ byte[] bytes = new byte[MAX_READ_BLOB_SIZE];+ is.read(bytes);+ return new Blob(ident, bytes, blobSize);+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ } else {+ return new Blob(ident, objectLoader.getCachedBytes());+ }+ }++ private Map<BlobIdent, Blob> getBlobCache() {+ if (blobCache == null) {+ synchronized(this) {+ if (blobCache == null)+ blobCache = new ConcurrentHashMap<>();+ }+ }+ return blobCache;+ }++ /**+ * Read blob content and cache result in repository in case the same blob+ * content is requested again.+ *+ * We made this method thread-safe as we are using ForkJoinPool to calculate+ * diffs of multiple blob changes concurrently, and this method will be+ * accessed concurrently in that special case.+ *+ * @param blobIdent+ * ident of the blob+ * @return+ * blob of specified blob ident+ * @throws+ * ObjectNotExistException if blob of specified ident can not be found in repository+ *+ */+ public Blob getBlob(BlobIdent blobIdent) {+ Preconditions.checkArgument(blobIdent.revision!=null && blobIdent.path!=null && blobIdent.mode!=null,+ "Revision, path and mode of ident param should be specified");++ Blob blob = getBlobCache().get(blobIdent);+ if (blob == null) {+ if (blobIdent.id != null) {+ try (FileRepository jgitRepo = openAsJGitRepo()) {+ if (blobIdent.isGitLink()) {+ String url = getSubmodules(blobIdent.revision).get(blobIdent.path);+ if (url == null)+ throw new ObjectNotExistException("Unable to find submodule '" + blobIdent.path + "' in .gitmodules");+ blob = new Blob(blobIdent, new Submodule(url, blobIdent.id).toString().getBytes());+ } else if (blobIdent.isTree()) {+ throw new NotFileException("Path '" + blobIdent.path + "' is a tree");+ } else {+ ObjectLoader objectLoader = jgitRepo.open(ObjectId.fromString(blobIdent.id), Constants.OBJ_BLOB);+ blob = readBlob(objectLoader, blobIdent);+ }+ getBlobCache().put(blobIdent, blob);+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ } else {+ try ( FileRepository jgitRepo = openAsJGitRepo();+ RevWalk revWalk = new RevWalk(jgitRepo)) {+ ObjectId commitId = getObjectId(blobIdent.revision);+ RevTree revTree = revWalk.parseCommit(commitId).getTree();+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, blobIdent.path, revTree);+ if (treeWalk != null) {+ if (blobIdent.isGitLink()) {+ String url = getSubmodules(blobIdent.revision).get(blobIdent.path);+ if (url == null)+ throw new ObjectNotExistException("Unable to find submodule '" + blobIdent.path + "' in .gitmodules");+ String hash = treeWalk.getObjectId(0).name();+ blob = new Blob(blobIdent, new Submodule(url, hash).toString().getBytes());+ } else if (blobIdent.isTree()) {+ throw new NotFileException("Path '" + blobIdent.path + "' is a tree");+ } else {+ ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));+ blob = readBlob(objectLoader, blobIdent);+ }+ getBlobCache().put(blobIdent, blob);+ } else {+ throw new ObjectNotExistException("Unable to find blob path '" + blobIdent.path + "' in revision '" + blobIdent.revision + "'");+ }+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ }+ return blob;+ }++ public InputStream getInputStream(BlobIdent ident) {+ try ( FileRepository jgitRepo = openAsJGitRepo();+ RevWalk revWalk = new RevWalk(jgitRepo)) {+ ObjectId commitId = getObjectId(ident.revision);+ RevTree revTree = revWalk.parseCommit(commitId).getTree();+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, ident.path, revTree);+ if (treeWalk != null) {+ ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));+ return objectLoader.openStream();+ } else {+ throw new ObjectNotExistException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");+ }+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }++ /**+ * Get cached object id of specified revision.+ *+ * @param revision+ * revision to resolve object id for+ * @param mustExist+ * true to have the method throwing exception instead+ * of returning null if the revision does not exist+ * @return+ * object id of specified revision, or <tt>null</tt> if revision+ * does not exist and mustExist is specified as false+ */+ @Nullable+ public ObjectId getObjectId(String revision, boolean mustExist) {+ if (objectIdCache == null)+ objectIdCache = new HashMap<>();++ Optional<ObjectId> optional = objectIdCache.get(revision);+ if (optional == null) {+ ObjectId objectId;+ try (FileRepository jgitRepo = openAsJGitRepo()) {+ objectId = jgitRepo.resolve(revision);+ } catch (RevisionSyntaxException | IOException e) {+ if (!mustExist) {+ objectId = null;+ } else {+ throw new RuntimeException(e);+ }+ }+ optional = Optional.fromNullable(objectId);+ objectIdCache.put(revision, optional);+ }+ if (mustExist && !optional.isPresent())+ throw new ObjectNotExistException("Unable to find revision '" + revision + "'");+ return optional.orNull();+ }++ public ObjectId getObjectId(String revision) {+ return getObjectId(revision, true);+ }++ public void cacheObjectId(String revision, @Nullable ObjectId objectId) {+ if (objectIdCache == null)+ objectIdCache = new HashMap<>();++ objectIdCache.put(revision, Optional.fromNullable(objectId));+ }++ public List<DiffEntry> getDiffs(String oldRev, String newRev, boolean detectRenames, String...paths) {+ if (diffCache == null)+ diffCache = new HashMap<>();++ DiffKey key = new DiffKey(oldRev, newRev, detectRenames, paths);+ List<DiffEntry> diffs = diffCache.get(key);+ if (diffs == null) {+ try ( FileRepository jgitRepo = openAsJGitRepo();+ DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE);) {+ diffFormatter.setRepository(jgitRepo);+ diffFormatter.setDetectRenames(detectRenames);+ AnyObjectId oldCommitId = getObjectId(oldRev);+ AnyObjectId newCommitId = getObjectId(newRev);+ if (paths.length >= 2) {+ List<TreeFilter> pathFilters = new ArrayList<>();+ for (String path: paths)+ pathFilters.add(PathFilter.create(path));+ diffFormatter.setPathFilter(OrTreeFilter.create(pathFilters));+ } else if (paths.length == 1) {+ diffFormatter.setPathFilter(PathFilter.create(paths[0]));+ }+ diffs = new ArrayList<>();+ for (DiffEntry entry: diffFormatter.scan(oldCommitId, newCommitId)) {+ if (!Objects.equal(entry.getOldPath(), entry.getNewPath())+ || !Objects.equal(entry.getOldMode(), entry.getNewMode())+ || entry.getOldId()==null || !entry.getOldId().isComplete()+ || entry.getNewId()== null || !entry.getNewId().isComplete()+ || !entry.getOldId().equals(entry.getNewId())) {+ diffs.add(entry);+ }+ }+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ diffCache.put(key, diffs);+ }+ return diffs;+ }++ public Commit getCommit(String commitHash) {+ if (commitCache == null)+ commitCache = new HashMap<>();++ Commit commit = commitCache.get(commitHash);+ if (commit == null) {+ commit = git().showRevision(commitHash);+ commitCache.put(commitHash, commit);+ }+ return commit;+ }++ public void cacheCommits(List<Commit> commits) {+ if (commitCache == null)+ commitCache = new HashMap<>();++ for (Commit commit: commits)+ commitCache.put(commit.getHash(), commit);+ }++ public LastCommitsOfChildren getLastCommitsOfChildren(String revision, @Nullable String path) {+ if (path == null)+ path = "";++ final File cacheDir = new File(+ GitPlex.getInstance(StorageManager.class).getCacheDir(this),+ "last_commits/" + path + "/gitplex_last_commits");++ final ReadWriteLock lock;+ try {+ lock = LockUtils.getReadWriteLock(cacheDir.getCanonicalPath());+ } catch (IOException e) {+ throw new RuntimeException(e);+ }++ final Set<ObjectId> commitIds = new HashSet<>();++ lock.readLock().lock();+ try {+ if (cacheDir.exists()) {+ for (String each: cacheDir.list())+ commitIds.add(ObjectId.fromString(each));+ }+ } finally {+ lock.readLock().unlock();+ }++ org.eclipse.jgit.revwalk.LastCommitsOfChildren.Cache cache;+ if (!commitIds.isEmpty()) {+ cache = new org.eclipse.jgit.revwalk.LastCommitsOfChildren.Cache() {++ @SuppressWarnings("unchecked")+ @Override+ public Map<String, Value> getLastCommitsOfChildren(ObjectId commitId) {+ if (commitIds.contains(commitId)) {+ lock.readLock().lock();+ try {+ byte[] bytes = FileUtils.readFileToByteArray(new File(cacheDir, commitId.name()));+ return (Map<String, Value>) SerializationUtils.deserialize(bytes);+ } catch (IOException e) {+ throw new RuntimeException(e);+ } finally {+ lock.readLock().unlock();+ }+ } else {+ return null;+ }+ }++ };+ } else {+ cache = null;+ }++ final AnyObjectId commitId = getObjectId(revision);++ try (FileRepository jgitRepo = openAsJGitRepo()) {+ long time = System.currentTimeMillis();+ LastCommitsOfChildren lastCommits = new LastCommitsOfChildren(jgitRepo, commitId, path, cache);+ long elapsed = System.currentTimeMillis()-time;+ if (elapsed > LAST_COMMITS_CACHE_THRESHOLD) {+ lock.writeLock().lock();+ try {+ if (!cacheDir.exists())+ FileUtils.createDir(cacheDir);+ FileUtils.writeByteArrayToFile(+ new File(cacheDir, commitId.name()),+ SerializationUtils.serialize(lastCommits));+ } catch (IOException e) {+ throw new RuntimeException(e);+ } finally {+ lock.writeLock().unlock();+ }+ }+ return lastCommits;+ }+ }++ @Nullable+ public Ref getRef(String revision) {+ if (refCache == null)+ refCache = new HashMap<>();+ Optional<Ref> optional = refCache.get(revision);+ if (optional == null) {+ try (FileRepository jgitRepo = openAsJGitRepo()) {+ optional = Optional.fromNullable(jgitRepo.getRef(revision));+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ refCache.put(revision, optional);+ }+ return optional.orNull();+ }++ public RevObject getRevObject(AnyObjectId revId) {+ if (revObjectCache == null)+ revObjectCache = new HashMap<>();+ RevObject revObject = revObjectCache.get(revId);+ if (revObject == null) {+ try ( FileRepository jgitRepo = openAsJGitRepo();+ RevWalk revWalk = new RevWalk(jgitRepo);) {+ revObject = revWalk.parseAny(revId);+ revObjectCache.put(revId, revObject);+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ return revObject;+ }++ public RevCommit getRevCommit(RevObject revObject) {+ return getRevCommit(revObject, true);+ }++ @Nullable+ public RevCommit getRevCommit(RevObject revObject, boolean mustExist) {+ if (revCommitCache == null)+ revCommitCache = new HashMap<>();+ Optional<RevCommit> optional = revCommitCache.get(revObject);+ if (optional == null) {+ try ( FileRepository jgitRepo = openAsJGitRepo();+ RevWalk revWalk = new RevWalk(jgitRepo);) {+ RevObject peeled = revWalk.peel(revObject);+ if (peeled instanceof RevCommit)+ optional = Optional.of((RevCommit) peeled);+ else+ optional = Optional.absent();+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ revCommitCache.put(revObject, optional);+ }+ if (mustExist)+ return optional.get();+ else+ return optional.orNull();+ }++ @Nullable+ public RevCommit getRevCommit(AnyObjectId revId, boolean mustExist) {+ return getRevCommit(getRevObject(revId), mustExist);+ }++ @Nullable+ public RevCommit getRevCommit(String revision, boolean mustExist) {+ ObjectId id = getObjectId(revision, mustExist);+ if (id != null)+ return getRevCommit(id, mustExist);+ else+ return null;+ }++ public RevCommit getRevCommit(String revision) {+ return getRevCommit(revision, true);+ }++ public RevCommit getRevCommit(AnyObjectId revId) {+ return getRevCommit(getRevObject(revId), true);+ }++ public Map<String, Ref> getRefs(String prefix) {+ if (prefixRefsCache == null)+ prefixRefsCache = new HashMap<>();++ Map<String, Ref> cached = prefixRefsCache.get(prefix);+ if (cached == null) {+ try (FileRepository jgitRepo = openAsJGitRepo()) {+ cached = jgitRepo.getRefDatabase().getRefs(prefix);+ prefixRefsCache.put(prefix, cached);+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ return cached;+ }++ public Map<String, String> getSubmodules(String revision) {+ Map<String, String> submodules = new HashMap<>();++ Blob blob = getBlob(new BlobIdent(revision, ".gitmodules", FileMode.REGULAR_FILE.getBits()));+ String content = new String(blob.getBytes());++ String path = null;+ String url = null;++ for (String line: StringUtils.splitAndTrim(content, "\r\n")) {+ if (line.startsWith("[") && line.endsWith("]")) {+ if (path != null && url != null)+ submodules.put(path, url);++ path = url = null;+ } else if (line.startsWith("path")) {+ path = StringUtils.substringAfter(line, "=").trim();+ } else if (line.startsWith("url")) {+ url = StringUtils.substringAfter(line, "=").trim();+ }+ }+ if (path != null && url != null)+ submodules.put(path, url);++ return submodules;+ }++ public String getRevisionFQN(String revision) {+ return getFQN() + ":" + revision;+ }++ public void deleteBranch(String branch) {+ String refName = GitUtils.branch2ref(branch);+ git().deleteRef(refName);+ for (RefListener listener: GitPlex.getExtensions(RefListener.class))+ listener.onRefUpdate(this, refName, null);+ }++ private static class DiffKey implements Serializable {+ String oldRev;++ String newRev;++ String[] paths;++ boolean detectRenames;++ DiffKey(String oldRev, String newRev, boolean detectRenames, String...paths) {+ this.oldRev = oldRev;+ this.newRev = newRev;+ this.detectRenames = detectRenames;+ this.paths = paths;+ }++ public boolean equals(Object other) {+ if (!(other instanceof DiffKey))+ return false;+ if (this == other)+ return true;+ DiffKey otherKey = (DiffKey) other;+ return Objects.equal(oldRev, otherKey.oldRev)+ && Objects.equal(newRev, otherKey.newRev)+ && Objects.equal(paths, otherKey.paths)+ && Objects.equal(detectRenames, otherKey.detectRenames);+ }++ public int hashCode() {+ return Objects.hashCode(oldRev, newRev, paths, detectRenames);+ }++ }++}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/branches/AheadBehind.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/branches/AheadBehind.javanew file mode 100644index 0000000000..5e2ab99ac6--- /dev/null+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/branches/AheadBehind.java@@ -0,0 +1,25 @@+package com.pmease.gitplex.web.page.repository.branches;++import java.io.Serializable;++@SuppressWarnings("serial")+public class AheadBehind implements Serializable {++ private final int ahead;++ private final int behind;++ public AheadBehind(int ahead, int behind) {+ this.ahead = ahead;+ this.behind = behind;+ }++ public int getAhead() {+ return ahead;+ }++ public int getBehind() {+ return behind;+ }++}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/branches/RepoBranchesPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/branches/RepoBranchesPage.javaindex ea5d16e62b..11a494c014 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/branches/RepoBranchesPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/branches/RepoBranchesPage.java@@ -1,5 +1,6 @@package com.pmease.gitplex.web.page.repository.branches;+import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;@@ -33,11 +34,13 @@ import org.apache.wicket.model.LoadableDetachableModel;import org.apache.wicket.model.Model;import org.apache.wicket.request.mapper.parameter.PageParameters;import org.apache.wicket.request.resource.CssResourceReference;+import org.eclipse.jgit.internal.storage.file.FileRepository;+import org.eclipse.jgit.lib.ObjectId;import org.eclipse.jgit.lib.Ref;import org.eclipse.jgit.revwalk.RevCommit;+import org.eclipse.jgit.revwalk.RevWalk;import com.google.common.base.Preconditions;-import com.pmease.commons.git.AheadBehind;import com.pmease.commons.git.GitUtils;import com.pmease.commons.hibernate.dao.Dao;import com.pmease.commons.util.StringUtils;@@ -91,26 +94,46 @@ public class RepoBranchesPage extends RepositoryPage {private final IModel<Map<String, AheadBehind>> aheadBehindsModel =new LoadableDetachableModel<Map<String, AheadBehind>>() {+ @SuppressWarnings("unused")@Overrideprotected Map<String, AheadBehind> load() {- List<String> compareHashes = new ArrayList<>();+ List<ObjectId> compareIds = new ArrayList<>();List<Ref> branchesInView = branchesView.getModelObject();for (long i=branchesView.getFirstItemOffset(); i<branchesInView.size(); i++) {if (i-branchesView.getFirstItemOffset() >= branchesView.getItemsPerPage())break;Ref ref = branchesInView.get((int)i);- if (!GitUtils.ref2branch(ref.getName()).equals(getBaseBranch()))- compareHashes.add(ref.getObjectId().name());+ compareIds.add(ref.getObjectId());}++ ObjectId baseId = getRepository().getObjectId(GitUtils.branch2ref(getBaseBranch()));- String baseHash = getRepository().getObjectId(GitUtils.branch2ref(getBaseBranch())).name();- Map<String, AheadBehind> aheadBehinds = getRepository().git().getAheadBehinds(- baseHash,- compareHashes.toArray(new String[compareHashes.size()]));- for (String compareHash: compareHashes)- aheadBehinds.put(compareHash, new AheadBehind());- aheadBehinds.put(baseHash, new AheadBehind());-+ Map<String, AheadBehind> aheadBehinds = new HashMap<>();+ try ( FileRepository jgitRepo = getRepository().openAsJGitRepo();+ RevWalk revWalk = new RevWalk(jgitRepo);) {++ RevCommit baseCommit = revWalk.lookupCommit(baseId);+ for (ObjectId compareId: compareIds) {+ RevCommit compareCommit = revWalk.lookupCommit(compareId);+ revWalk.markUninteresting(baseCommit);+ revWalk.markStart(compareCommit);+ int ahead = 0;+ for (RevCommit commit: revWalk)+ ahead++;+ revWalk.reset();++ revWalk.markUninteresting(compareCommit);+ revWalk.markStart(baseCommit);+ int behind = 0;+ for (RevCommit commit: revWalk)+ behind++;+ revWalk.reset();++ aheadBehinds.put(compareId.name(), new AheadBehind(ahead, behind));+ }+ } catch (IOException e) {+ throw new RuntimeException(e);+ }return aheadBehinds;}};diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.javaindex 9019491ada..b401de27d1 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java@@ -1,113 +1,23 @@-package com.pmease.gitplex.web.page.test;--import java.io.File;-import java.io.IOException;-import java.util.concurrent.atomic.AtomicInteger;--import org.apache.wicket.markup.html.link.Link;-import org.eclipse.jgit.internal.storage.file.FileRepository;-import org.eclipse.jgit.lib.ObjectId;-import org.eclipse.jgit.revwalk.RevCommit;-import org.eclipse.jgit.revwalk.RevWalk;-import org.eclipse.jgit.revwalk.filter.RevFilter;--import com.pmease.commons.hibernate.dao.Dao;-import com.pmease.commons.util.execution.Commandline;-import com.pmease.commons.util.execution.LineConsumer;-import com.pmease.gitplex.core.GitPlex;-import com.pmease.gitplex.core.manager.AuxiliaryManager;-import com.pmease.gitplex.core.model.Repository;-import com.pmease.gitplex.web.page.base.BasePage;--@SuppressWarnings("serial")-public class TestPage extends BasePage {-- @Override- protected void onInitialize() {- super.onInitialize();-- add(new Link<Void>("test") {-- @Override- public void onClick() {- Repository repo = GitPlex.getInstance(Dao.class).load(Repository.class, 1L);-- long time = System.currentTimeMillis();- Commandline cmd = new Commandline("git");- cmd.addArgs("merge-base", "--all", "master", "branch1", "branch2",- "branch3", "branch4", "branch5", "branch6", "branch7",- "branch8", "branch9");- cmd.workingDir(new File("w:\\temp\\gitplex_storage\\repositories\\1"));- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- System.out.println(line);- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- System.err.println(line);- }-- });-- System.out.println(System.currentTimeMillis()-time);- time = System.currentTimeMillis();-- try( FileRepository jgitRepo = repo.openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo);) {- revWalk.setRevFilter(RevFilter.MERGE_BASE);- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("master")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch1")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch2")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch3")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch4")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch5")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch6")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch7")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch8")));- revWalk.markStart(revWalk.lookupCommit(jgitRepo.resolve("branch9")));-- for (RevCommit commit: revWalk) {- System.out.println(commit.name());- }- } catch (IOException e) {- e.printStackTrace();- }-- System.out.println(System.currentTimeMillis()-time);- time = System.currentTimeMillis();- cmd = new Commandline("git");- cmd.addArgs("rev-list", "^0fc2f137226eff4c9dd90864dda5c237474c3ec5");- cmd.workingDir(new File("w:\\temp\\gitplex_storage\\repositories\\1"));- final AtomicInteger commits = new AtomicInteger(0);- cmd.execute(new LineConsumer() {-- @Override- public void consume(String line) {- commits.incrementAndGet();- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- System.err.println(line);- }-- });- System.out.println(commits.get());- System.out.println(System.currentTimeMillis()-time);-- time = System.currentTimeMillis();- System.out.println(GitPlex.getInstance(AuxiliaryManager.class).getDescendants(repo, ObjectId.fromString("0fc2f137226eff4c9dd90864dda5c237474c3ec5")).size());- System.out.println(System.currentTimeMillis()-time);- }-- });- }--}+package com.pmease.gitplex.web.page.test;++import org.apache.wicket.markup.html.link.Link;++import com.pmease.gitplex.web.page.base.BasePage;++@SuppressWarnings("serial")+public class TestPage extends BasePage {++ @Override+ protected void onInitialize() {+ super.onInitialize();++ add(new Link<Void>("test") {++ @Override+ public void onClick() {++ }+ });+ }++}