James Moger
2013-11-20 8f1c9fd7e0f7ea3d7d0b87788eb92ba2f0f09d59
commit | author | age
f8bb95 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.models;
17
18 import java.io.Serializable;
402730 19 import java.text.MessageFormat;
79dd0b 20 import java.util.Date;
f8bb95 21 import java.util.List;
JM 22
79dd0b 23 import org.eclipse.jgit.lib.ObjectId;
f8bb95 24 import org.eclipse.jgit.lib.PersonIdent;
JM 25 import org.eclipse.jgit.revwalk.RevCommit;
26
27 /**
28  * Model class to represent a RevCommit, it's source repository, and the branch.
29  * This class is used by the activity page.
699e71 30  *
f8bb95 31  * @author James Moger
JM 32  */
33 public class RepositoryCommit implements Serializable, Comparable<RepositoryCommit> {
34
35     private static final long serialVersionUID = 1L;
36
37     public final String repository;
38
39     public final String branch;
40
41     private final RevCommit commit;
42
43     private List<RefModel> refs;
44
45     public RepositoryCommit(String repository, String branch, RevCommit commit) {
46         this.repository = repository;
47         this.branch = branch;
48         this.commit = commit;
49     }
50
51     public void setRefs(List<RefModel> refs) {
52         this.refs = refs;
53     }
54
55     public List<RefModel> getRefs() {
56         return refs;
57     }
58
79dd0b 59     public ObjectId getId() {
JM 60         return commit.getId();
61     }
62
f8bb95 63     public String getName() {
JM 64         return commit.getName();
65     }
66
67     public String getShortName() {
68         return commit.getName().substring(0, 8);
69     }
70
71     public String getShortMessage() {
72         return commit.getShortMessage();
73     }
699e71 74
79dd0b 75     public Date getCommitDate() {
JM 76         return new Date(commit.getCommitTime() * 1000L);
77     }
f8bb95 78
JM 79     public int getParentCount() {
80         return commit.getParentCount();
79dd0b 81     }
699e71 82
79dd0b 83     public RevCommit [] getParents() {
JM 84         return commit.getParents();
f8bb95 85     }
JM 86
87     public PersonIdent getAuthorIdent() {
88         return commit.getAuthorIdent();
89     }
90
91     public PersonIdent getCommitterIdent() {
92         return commit.getCommitterIdent();
93     }
699e71 94
f8bb95 95     @Override
JM 96     public boolean equals(Object o) {
97         if (o instanceof RepositoryCommit) {
98             RepositoryCommit commit = (RepositoryCommit) o;
99             return repository.equals(commit.repository) && getName().equals(commit.getName());
100         }
101         return false;
102     }
103
104     @Override
105     public int hashCode() {
106         return (repository + commit).hashCode();
107     }
108
109     @Override
110     public int compareTo(RepositoryCommit o) {
111         // reverse-chronological order
112         if (commit.getCommitTime() > o.commit.getCommitTime()) {
113             return -1;
114         } else if (commit.getCommitTime() < o.commit.getCommitTime()) {
115             return 1;
116         }
117         return 0;
118     }
699e71 119
79dd0b 120     public RepositoryCommit clone(String withRef) {
JM 121         return new RepositoryCommit(repository, withRef, commit);
122     }
699e71 123
402730 124     @Override
JM 125     public String toString() {
699e71 126         return MessageFormat.format("{0} {1} {2,date,yyyy-MM-dd HH:mm} {3} {4}",
402730 127                 getShortName(), branch, getCommitterIdent().getWhen(), getAuthorIdent().getName(),
JM 128                 getShortMessage());
129     }
f8bb95 130 }