You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JGitUtilsTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  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.tests;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.util.Date;
  20. import java.util.List;
  21. import junit.framework.TestCase;
  22. import org.eclipse.jgit.lib.Constants;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.revwalk.RevBlob;
  25. import org.eclipse.jgit.revwalk.RevCommit;
  26. import org.eclipse.jgit.revwalk.RevObject;
  27. import org.eclipse.jgit.revwalk.RevTree;
  28. import org.eclipse.jgit.storage.file.FileRepository;
  29. import com.gitblit.utils.JGitUtils;
  30. import com.gitblit.utils.JGitUtils.DiffOutputType;
  31. import com.gitblit.wicket.models.PathModel.PathChangeModel;
  32. import com.gitblit.wicket.models.RefModel;
  33. import com.gitblit.wicket.models.TicketModel;
  34. public class JGitUtilsTest extends TestCase {
  35. private File repositoriesFolder = new File("c:/projects/git");
  36. private boolean exportAll = true;
  37. private boolean readNested = true;
  38. private List<String> getRepositories() {
  39. return JGitUtils.getRepositoryList(repositoriesFolder, exportAll, readNested);
  40. }
  41. private Repository getRepository() throws Exception {
  42. return new FileRepository(new File(repositoriesFolder, getRepositories().get(0)) + "/" + Constants.DOT_GIT);
  43. }
  44. public void testFindRepositories() {
  45. List<String> list = getRepositories();
  46. assertTrue("No repositories found in " + repositoriesFolder, list.size() > 0);
  47. }
  48. public void testOpenRepository() throws Exception {
  49. Repository r = getRepository();
  50. r.close();
  51. assertTrue("Could not find repository!", r != null);
  52. }
  53. public void testLastChangeRepository() throws Exception {
  54. Repository r = getRepository();
  55. Date date = JGitUtils.getLastChange(r);
  56. r.close();
  57. assertTrue("Could not get last repository change date!", date != null);
  58. }
  59. public void testFirstCommit() throws Exception {
  60. Repository r = getRepository();
  61. RevCommit commit = JGitUtils.getFirstCommit(r, null);
  62. r.close();
  63. assertTrue("Could not get first commit!", commit != null);
  64. System.out.println(commit.getName() + " " + commit.getShortMessage());
  65. }
  66. public void testRetrieveRevObject() throws Exception {
  67. Repository r = getRepository();
  68. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  69. RevTree tree = commit.getTree();
  70. RevObject object = JGitUtils.getRevObject(r, tree, "AUTHORS");
  71. r.close();
  72. assertTrue("Object is null!", object != null);
  73. }
  74. public void testRetrieveStringContent() throws Exception {
  75. Repository r = getRepository();
  76. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  77. RevTree tree = commit.getTree();
  78. RevBlob blob = (RevBlob) JGitUtils.getRevObject(r, tree, "AUTHORS");
  79. String content = JGitUtils.getRawContentAsString(r, blob);
  80. r.close();
  81. assertTrue("Content is null!", content != null);
  82. }
  83. public void testTicGit() throws Exception {
  84. Repository r = new FileRepository(new File(repositoriesFolder, "ticgit") + "/" + Constants.DOT_GIT);
  85. RefModel ticgit = JGitUtils.getTicketsBranch(r);
  86. assertTrue("Ticgit branch does not exist!", ticgit != null);
  87. List<TicketModel> tickets = JGitUtils.getTickets(r);
  88. assertTrue("No tickets found!", tickets.size() > 0);
  89. r.close();
  90. }
  91. public void testFilesInCommit() throws Exception {
  92. Repository r = getRepository();
  93. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  94. List<PathChangeModel> paths = JGitUtils.getFilesInCommit(r, commit);
  95. r.close();
  96. assertTrue("No changed paths found!", paths.size() > 0);
  97. }
  98. public void testCommitDiff() throws Exception {
  99. Repository r = getRepository();
  100. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  101. String diff = JGitUtils.getCommitDiff(r, commit, DiffOutputType.PLAIN);
  102. r.close();
  103. System.out.println(diff);
  104. }
  105. public void testZip() throws Exception {
  106. Repository r = new FileRepository(new File(repositoriesFolder, "gitblit.git/" + Constants.DOT_GIT));
  107. FileOutputStream fos = null;
  108. try {
  109. File zipFile = new File("c:/output.zip");
  110. zipFile.delete();
  111. fos = new FileOutputStream(zipFile);
  112. if (JGitUtils.zip(r, "src", Constants.HEAD, fos)) {
  113. System.out.println("zip = " + zipFile.length() + " bytes");
  114. } else {
  115. System.err.println("failed to generate zip file?!");
  116. }
  117. } finally {
  118. if (fos != null) {
  119. try {
  120. fos.close();
  121. } catch (Throwable t) {
  122. }
  123. }
  124. }
  125. }
  126. }