| | |
| | | import java.util.Set;
|
| | |
|
| | | import org.eclipse.jgit.lib.Constants;
|
| | | import org.eclipse.jgit.lib.PersonIdent;
|
| | | import org.eclipse.jgit.revwalk.RevCommit;
|
| | | import org.eclipse.jgit.transport.ReceiveCommand;
|
| | |
|
| | | import com.gitblit.utils.StringUtils;
|
| | |
|
| | | /**
|
| | | * Model class to represent a push into a repository.
|
| | |
| | |
|
| | | private final Set<RepositoryCommit> commits;
|
| | |
|
| | | private final Map<String, ReceiveCommand.Type> refUpdates;
|
| | | protected final Map<String, ReceiveCommand.Type> refUpdates;
|
| | | |
| | | protected final Map<String, String> refIdChanges;
|
| | | |
| | | private int authorCount;
|
| | |
|
| | | /**
|
| | | * Constructor for specified duration of push from start date.
|
| | |
| | | this.user = user;
|
| | | this.commits = new LinkedHashSet<RepositoryCommit>();
|
| | | this.refUpdates = new HashMap<String, ReceiveCommand.Type>();
|
| | | this.refIdChanges = new HashMap<String, String>();
|
| | | this.authorCount = -1;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | if (!refUpdates.containsKey(ref)) {
|
| | | refUpdates.put(ref, type);
|
| | | }
|
| | | }
|
| | | |
| | | /**
|
| | | * Tracks the change type for the specified ref.
|
| | | * |
| | | * @param ref
|
| | | * @param type
|
| | | * @param oldId
|
| | | * @param newId
|
| | | */
|
| | | public void updateRef(String ref, ReceiveCommand.Type type, String oldId, String newId) {
|
| | | if (!refUpdates.containsKey(ref)) {
|
| | | refUpdates.put(ref, type);
|
| | | refIdChanges.put(ref, oldId + "-" + newId);
|
| | | }
|
| | | }
|
| | | |
| | | /**
|
| | | * Returns the old id of a ref.
|
| | | * |
| | | * @param ref
|
| | | * @return the old id
|
| | | */
|
| | | public String getOldId(String ref) {
|
| | | String change = refIdChanges.get(ref);
|
| | | if (StringUtils.isEmpty(change)) {
|
| | | return null;
|
| | | }
|
| | | return change.split("-")[0];
|
| | | }
|
| | |
|
| | | /**
|
| | | * Returns the new id of a ref
|
| | | * |
| | | * @param ref
|
| | | * @return the new id
|
| | | */
|
| | | public String getNewId(String ref) {
|
| | | String change = refIdChanges.get(ref);
|
| | | if (StringUtils.isEmpty(change)) {
|
| | | return null;
|
| | | }
|
| | | return change.split("-")[1];
|
| | | }
|
| | | |
| | | /**
|
| | | * Returns the change type of the ref change.
|
| | | * |
| | | * @param ref
|
| | | * @return the change type for the ref
|
| | | */
|
| | | public ReceiveCommand.Type getChangeType(String ref) {
|
| | | ReceiveCommand.Type type = refUpdates.get(ref);
|
| | | return type;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | public RepositoryCommit addCommit(String branch, RevCommit commit) {
|
| | | RepositoryCommit commitModel = new RepositoryCommit(repository, branch, commit);
|
| | | if (commits.add(commitModel)) {
|
| | | authorCount = -1;
|
| | | return commitModel;
|
| | | }
|
| | | return null;
|
| | | }
|
| | | |
| | | /**
|
| | | * Adds a a list of repository commits. This is used to construct discrete
|
| | | * ref push log entries
|
| | | * |
| | | * @param commits
|
| | | */
|
| | | public void addCommits(List<RepositoryCommit> list) {
|
| | | commits.addAll(list);
|
| | | authorCount = -1;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | | |
| | | /**
|
| | | * Returns true if this ref has been rewound.
|
| | | * |
| | | * @param ref
|
| | | * @return true if this is a non-fastforward ref update
|
| | | */
|
| | | public boolean isNonFastForward(String ref) {
|
| | | ReceiveCommand.Type type = refUpdates.get(ref);
|
| | | if (type == null) {
|
| | | return false;
|
| | | }
|
| | | return ReceiveCommand.Type.UPDATE_NONFASTFORWARD.equals(type);
|
| | | }
|
| | |
|
| | | /**
|
| | | * Returns true if this ref has been deleted.
|
| | | * |
| | | * @param ref
|
| | | * @return true if this is a delete ref update
|
| | | */
|
| | | public boolean isDelete(String ref) {
|
| | | ReceiveCommand.Type type = refUpdates.get(ref);
|
| | | if (type == null) {
|
| | | return false;
|
| | | }
|
| | | return ReceiveCommand.Type.DELETE.equals(type);
|
| | | }
|
| | | |
| | | /**
|
| | | * Returns the list of refs changed by the push.
|
| | | * |
| | | * @return a list of refs
|
| | | */
|
| | | public List<String> getChangedRefs() {
|
| | | return new ArrayList<String>(refUpdates.keySet());
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | return list;
|
| | | }
|
| | |
|
| | | public int getAuthorCount() {
|
| | | if (authorCount == -1) {
|
| | | Set<String> authors = new HashSet<String>();
|
| | | for (RepositoryCommit commit : commits) {
|
| | | String name = commit.getAuthorIdent().getName();
|
| | | authors.add(name);
|
| | | }
|
| | | authorCount = authors.size();
|
| | | }
|
| | | return authorCount;
|
| | | }
|
| | | |
| | | /**
|
| | | * The total number of commits in the push.
|
| | | *
|
| | |
| | | Collections.sort(list);
|
| | | return list;
|
| | | }
|
| | | |
| | | public PersonIdent getCommitterIdent() {
|
| | | return new PersonIdent(user.getDisplayName(), user.emailAddress == null ? user.username : user.emailAddress);
|
| | | }
|
| | |
|
| | | public PersonIdent getAuthorIdent() {
|
| | | return getCommitterIdent();
|
| | | }
|
| | |
|
| | | @Override
|
| | | public int compareTo(PushLogEntry o) {
|