| | |
| | | import org.eclipse.jgit.lib.ObjectId;
|
| | | import org.eclipse.jgit.lib.PersonIdent;
|
| | | import org.eclipse.jgit.lib.Repository;
|
| | | import org.eclipse.jgit.lib.RepositoryCache.FileKey;
|
| | | import org.eclipse.jgit.revwalk.RevCommit;
|
| | | import org.eclipse.jgit.util.FS;
|
| | | import org.eclipse.jgit.util.FileUtils;
|
| | |
|
| | | import com.gitblit.GitBlit;
|
| | | import com.gitblit.Keys;
|
| | | import com.gitblit.models.GitNote;
|
| | | import com.gitblit.models.PathModel;
|
| | | import com.gitblit.models.PathModel.PathChangeModel;
|
| | | import com.gitblit.models.RefModel;
|
| | | import com.gitblit.utils.JGitUtils;
|
| | | import com.gitblit.utils.JGitUtils.SearchType;
|
| | | import com.gitblit.utils.StringUtils;
|
| | |
|
| | | public class JGitUtilsTest extends TestCase {
|
| | |
|
| | |
| | | }
|
| | |
|
| | | public void testLastCommit() throws Exception {
|
| | | assertTrue(JGitUtils.getLastChange(null).equals(new Date(0)));
|
| | | assertTrue(JGitUtils.getLastChange(null, null).equals(new Date(0)));
|
| | |
|
| | | Repository repository = GitBlitSuite.getHelloworldRepository();
|
| | | assertTrue(JGitUtils.getCommit(repository, null) != null);
|
| | | Date date = JGitUtils.getLastChange(repository);
|
| | | Date date = JGitUtils.getLastChange(repository, null);
|
| | | repository.close();
|
| | | assertTrue("Could not get last repository change date!", date != null);
|
| | | }
|
| | |
| | | public void testCreateRepository() throws Exception {
|
| | | String[] repositories = { "NewTestRepository.git", "NewTestRepository" };
|
| | | for (String repositoryName : repositories) {
|
| | | boolean isBare = repositoryName.endsWith(".git");
|
| | | Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
|
| | | repositoryName, isBare);
|
| | | File folder;
|
| | | if (isBare) {
|
| | | folder = new File(GitBlitSuite.REPOSITORIES, repositoryName);
|
| | | } else {
|
| | | folder = new File(GitBlitSuite.REPOSITORIES, repositoryName + "/.git");
|
| | | }
|
| | | repositoryName);
|
| | | File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
|
| | | FS.DETECTED);
|
| | | assertTrue(repository != null);
|
| | | assertFalse(JGitUtils.hasCommits(repository));
|
| | | assertTrue(JGitUtils.getFirstCommit(repository, null) == null);
|
| | | assertTrue(JGitUtils.getFirstChange(repository, null).getTime() == folder
|
| | | .lastModified());
|
| | | assertTrue(JGitUtils.getLastChange(repository).getTime() == folder.lastModified());
|
| | | assertTrue(JGitUtils.getLastChange(repository, null).getTime() == folder.lastModified());
|
| | | assertTrue(JGitUtils.getCommit(repository, null) == null);
|
| | | repository.close();
|
| | | assertTrue(GitBlit.self().deleteRepository(repositoryName));
|
| | |
| | | }
|
| | |
|
| | | public void testRefs() throws Exception {
|
| | | Repository repository = GitBlitSuite.getTicgitRepository();
|
| | | Map<ObjectId, List<String>> map = JGitUtils.getAllRefs(repository);
|
| | | Repository repository = GitBlitSuite.getJGitRepository();
|
| | | Map<ObjectId, List<RefModel>> map = JGitUtils.getAllRefs(repository);
|
| | | repository.close();
|
| | | assertTrue(map.size() > 0);
|
| | | for (Map.Entry<ObjectId, List<RefModel>> entry : map.entrySet()) {
|
| | | List<RefModel> list = entry.getValue();
|
| | | for (RefModel ref : list) {
|
| | | if (ref.displayName.equals("refs/tags/spearce-gpg-pub")) {
|
| | | assertTrue(ref.toString().equals("refs/tags/spearce-gpg-pub"));
|
| | | assertTrue(ref.getObjectId().getName()
|
| | | .equals("8bbde7aacf771a9afb6992434f1ae413e010c6d8"));
|
| | | assertTrue(ref.getAuthorIdent().getEmailAddress().equals("spearce@spearce.org"));
|
| | | assertTrue(ref.getShortMessage().startsWith("GPG key"));
|
| | | assertTrue(ref.getFullMessage().startsWith("GPG key"));
|
| | | assertTrue(ref.getReferencedObjectType() == Constants.OBJ_BLOB);
|
| | | } else if (ref.displayName.equals("refs/tags/v0.12.1")) {
|
| | | assertTrue(ref.isAnnotatedTag());
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | public void testBranches() throws Exception {
|
| | | Repository repository = GitBlitSuite.getTicgitRepository();
|
| | | for (RefModel model : JGitUtils.getLocalBranches(repository, -1)) {
|
| | | Repository repository = GitBlitSuite.getJGitRepository();
|
| | | assertTrue(JGitUtils.getLocalBranches(repository, true, 0).size() == 0);
|
| | | for (RefModel model : JGitUtils.getLocalBranches(repository, true, -1)) {
|
| | | assertTrue(model.getName().startsWith(Constants.R_HEADS));
|
| | | assertTrue(model.equals(model));
|
| | | assertFalse(model.equals(""));
|
| | | assertTrue(model.hashCode() == model.getCommitId().hashCode()
|
| | | assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
|
| | | + model.getName().hashCode());
|
| | | assertTrue(model.getShortLog().equals(model.commit.getShortMessage()));
|
| | | assertTrue(model.getShortMessage().equals(model.getShortMessage()));
|
| | | }
|
| | | for (RefModel model : JGitUtils.getRemoteBranches(repository, -1)) {
|
| | | for (RefModel model : JGitUtils.getRemoteBranches(repository, true, -1)) {
|
| | | assertTrue(model.getName().startsWith(Constants.R_REMOTES));
|
| | | assertTrue(model.equals(model));
|
| | | assertFalse(model.equals(""));
|
| | | assertTrue(model.hashCode() == model.getCommitId().hashCode()
|
| | | assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
|
| | | + model.getName().hashCode());
|
| | | assertTrue(model.getShortLog().equals(model.commit.getShortMessage()));
|
| | | assertTrue(model.getShortMessage().equals(model.getShortMessage()));
|
| | | }
|
| | | assertTrue(JGitUtils.getRemoteBranches(repository, 10).size() == 10);
|
| | | assertTrue(JGitUtils.getRemoteBranches(repository, true, 8).size() == 8);
|
| | | repository.close();
|
| | | }
|
| | |
|
| | | public void testTags() throws Exception {
|
| | | Repository repository = GitBlitSuite.getTicgitRepository();
|
| | | for (RefModel model : JGitUtils.getTags(repository, -1)) {
|
| | | if (model.getObjectId().getName().equals("283035e4848054ff1803cb0e690270787dc92399")) {
|
| | | Repository repository = GitBlitSuite.getJGitRepository();
|
| | | assertTrue(JGitUtils.getTags(repository, true, 5).size() == 5);
|
| | | for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
|
| | | if (model.getObjectId().getName().equals("d28091fb2977077471138fe97da1440e0e8ae0da")) {
|
| | | assertTrue("Not an annotated tag!", model.isAnnotatedTag());
|
| | | }
|
| | | assertTrue(model.getName().startsWith(Constants.R_TAGS));
|
| | | assertTrue(model.equals(model));
|
| | | assertFalse(model.equals(""));
|
| | | assertTrue(model.hashCode() == model.getCommitId().hashCode()
|
| | | assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
|
| | | + model.getName().hashCode());
|
| | | assertTrue(model.getShortLog().equals(model.commit.getShortMessage()));
|
| | | }
|
| | | repository.close();
|
| | |
|
| | | repository = GitBlitSuite.getBluezGnomeRepository();
|
| | | for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
|
| | | if (model.getObjectId().getName().equals("728643ec0c438c77e182898c2f2967dbfdc231c8")) {
|
| | | assertFalse(model.isAnnotatedTag());
|
| | | assertTrue(model.getAuthorIdent().getEmailAddress().equals("marcel@holtmann.org"));
|
| | | assertTrue(model.getFullMessage().equals(
|
| | | "Update changelog and bump version number\n"));
|
| | | }
|
| | | }
|
| | | repository.close();
|
| | | }
|
| | |
|
| | | public void testCommitNotes() throws Exception {
|
| | | // Repository repository = new FileRepository(new File("c:/projects/git/jgit.git/.git"));
|
| | | // RevCommit commit = JGitUtils.getCommit(repository,
|
| | | // "ada903085d1b4ef8c79e3e2d91f49fee7e188f53");
|
| | | // List<GitNote> list = JGitUtils.getNotesOnCommit(repository, commit);
|
| | | // repository.close();
|
| | | // assertTrue(list.size() > 0);
|
| | | Repository repository = GitBlitSuite.getJGitRepository();
|
| | | RevCommit commit = JGitUtils.getCommit(repository,
|
| | | "690c268c793bfc218982130fbfc25870f292295e");
|
| | | List<GitNote> list = JGitUtils.getNotesOnCommit(repository, commit);
|
| | | repository.close();
|
| | | assertTrue(list.size() > 0);
|
| | | assertTrue(list.get(0).notesRef.getReferencedObjectId().getName()
|
| | | .equals("183474d554e6f68478a02d9d7888b67a9338cdff"));
|
| | | }
|
| | |
|
| | | public void testCreateOrphanedBranch() throws Exception {
|
| | | Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, "orphantest");
|
| | | assertTrue(JGitUtils.createOrphanBranch(repository,
|
| | | "x" + Long.toHexString(System.currentTimeMillis()).toUpperCase()));
|
| | | FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
|
| | | }
|
| | |
|
| | | public void testStringContent() throws Exception {
|
| | | Repository repository = GitBlitSuite.getHelloworldRepository();
|
| | | String contentA = JGitUtils.getRawContentAsString(repository, null, "java.java");
|
| | | String contentA = JGitUtils.getStringContent(repository, null, "java.java");
|
| | | RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
|
| | | String contentB = JGitUtils.getRawContentAsString(repository, commit, "java.java");
|
| | | String contentC = JGitUtils.getRawContentAsString(repository, commit, "missing.txt");
|
| | | String contentB = JGitUtils.getStringContent(repository, commit.getTree(), "java.java");
|
| | | String contentC = JGitUtils.getStringContent(repository, commit.getTree(), "missing.txt");
|
| | |
|
| | | // manually construct a blob, calculate the hash, lookup the hash in git
|
| | | StringBuilder sb = new StringBuilder();
|
| | | sb.append("blob ").append(contentA.length()).append('\0');
|
| | | sb.append(contentA);
|
| | | String sha1 = StringUtils.getSHA1(sb.toString());
|
| | | String contentD = JGitUtils.getStringContent(repository, sha1);
|
| | | repository.close();
|
| | | assertTrue("ContentA is null!", contentA != null && contentA.length() > 0);
|
| | | assertTrue("ContentB is null!", contentB != null && contentB.length() > 0);
|
| | | assertTrue(contentA.equals(contentB));
|
| | | assertTrue(contentC == null);
|
| | | assertTrue(contentA.equals(contentD));
|
| | | }
|
| | |
|
| | | public void testFilesInCommit() throws Exception {
|
| | |
| | | }
|
| | |
|
| | | public void testRevlog() throws Exception {
|
| | | assertTrue(JGitUtils.getRevLog(null, 0).size() == 0);
|
| | | List<RevCommit> commits = JGitUtils.getRevLog(null, 10);
|
| | | assertTrue(commits.size() == 0);
|
| | |
|
| | |
| | | }
|
| | |
|
| | | public void testSearchRevlogs() throws Exception {
|
| | | assertTrue(JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0, 0).size() == 0);
|
| | | List<RevCommit> results = JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0,
|
| | | 3);
|
| | | assertTrue(results.size() == 0);
|
| | |
| | | assertTrue(zipFileB.length() > 0);
|
| | | zipFileB.delete();
|
| | | }
|
| | |
|
| | | } |