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 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Arrays;
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  30. import org.eclipse.jgit.lib.Constants;
  31. import org.eclipse.jgit.lib.FileMode;
  32. import org.eclipse.jgit.lib.ObjectId;
  33. import org.eclipse.jgit.lib.PersonIdent;
  34. import org.eclipse.jgit.lib.Repository;
  35. import org.eclipse.jgit.lib.RepositoryCache;
  36. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  37. import org.eclipse.jgit.revplot.PlotCommit;
  38. import org.eclipse.jgit.revplot.PlotCommitList;
  39. import org.eclipse.jgit.revplot.PlotLane;
  40. import org.eclipse.jgit.revplot.PlotWalk;
  41. import org.eclipse.jgit.revwalk.RevCommit;
  42. import org.eclipse.jgit.revwalk.RevTree;
  43. import org.eclipse.jgit.util.FS;
  44. import org.eclipse.jgit.util.FileUtils;
  45. import org.junit.Test;
  46. import com.gitblit.Constants.SearchType;
  47. import com.gitblit.GitBlit;
  48. import com.gitblit.Keys;
  49. import com.gitblit.models.GitNote;
  50. import com.gitblit.models.PathModel;
  51. import com.gitblit.models.PathModel.PathChangeModel;
  52. import com.gitblit.models.RefModel;
  53. import com.gitblit.utils.CompressionUtils;
  54. import com.gitblit.utils.JGitUtils;
  55. import com.gitblit.utils.JnaUtils;
  56. import com.gitblit.utils.StringUtils;
  57. public class JGitUtilsTest {
  58. @Test
  59. public void testDisplayName() throws Exception {
  60. assertEquals("Napoleon Bonaparte",
  61. JGitUtils.getDisplayName(new PersonIdent("Napoleon Bonaparte", "")));
  62. assertEquals("<someone@somewhere.com>",
  63. JGitUtils.getDisplayName(new PersonIdent("", "someone@somewhere.com")));
  64. assertEquals("Napoleon Bonaparte <someone@somewhere.com>",
  65. JGitUtils.getDisplayName(new PersonIdent("Napoleon Bonaparte",
  66. "someone@somewhere.com")));
  67. }
  68. @Test
  69. public void testFindRepositories() {
  70. List<String> list = JGitUtils.getRepositoryList(null, false, true, -1, null);
  71. assertEquals(0, list.size());
  72. list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true, -1, null));
  73. assertEquals(0, list.size());
  74. list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null));
  75. assertTrue("No repositories found in " + GitBlitSuite.REPOSITORIES, list.size() > 0);
  76. }
  77. @Test
  78. public void testFindExclusions() {
  79. List<String> list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null);
  80. assertTrue("Missing jgit repository?!", list.contains("test/jgit.git"));
  81. list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/jgit\\.git"));
  82. assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));
  83. list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/*"));
  84. assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));
  85. list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList(".*jgit.*"));
  86. assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));
  87. assertFalse("Repository exclusion failed!", list.contains("working/jgit"));
  88. assertFalse("Repository exclusion failed!", list.contains("working/jgit2"));
  89. }
  90. @Test
  91. public void testOpenRepository() throws Exception {
  92. Repository repository = GitBlitSuite.getHelloworldRepository();
  93. repository.close();
  94. assertNotNull("Could not find repository!", repository);
  95. }
  96. @Test
  97. public void testFirstCommit() throws Exception {
  98. assertEquals(new Date(0), JGitUtils.getFirstChange(null, null));
  99. Repository repository = GitBlitSuite.getHelloworldRepository();
  100. RevCommit commit = JGitUtils.getFirstCommit(repository, null);
  101. Date firstChange = JGitUtils.getFirstChange(repository, null);
  102. repository.close();
  103. assertNotNull("Could not get first commit!", commit);
  104. assertEquals("Incorrect first commit!", "f554664a346629dc2b839f7292d06bad2db4aece",
  105. commit.getName());
  106. assertTrue(firstChange.equals(new Date(commit.getCommitTime() * 1000L)));
  107. }
  108. @Test
  109. public void testLastCommit() throws Exception {
  110. assertEquals(new Date(0), JGitUtils.getLastChange(null).when);
  111. Repository repository = GitBlitSuite.getHelloworldRepository();
  112. assertTrue(JGitUtils.getCommit(repository, null) != null);
  113. Date date = JGitUtils.getLastChange(repository).when;
  114. repository.close();
  115. assertNotNull("Could not get last repository change date!", date);
  116. }
  117. @Test
  118. public void testCreateRepository() throws Exception {
  119. String[] repositories = { "NewTestRepository.git", "NewTestRepository" };
  120. for (String repositoryName : repositories) {
  121. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
  122. repositoryName);
  123. File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
  124. FS.DETECTED);
  125. assertNotNull(repository);
  126. assertFalse(JGitUtils.hasCommits(repository));
  127. assertNull(JGitUtils.getFirstCommit(repository, null));
  128. assertEquals(folder.lastModified(), JGitUtils.getFirstChange(repository, null)
  129. .getTime());
  130. assertEquals(folder.lastModified(), JGitUtils.getLastChange(repository).when.getTime());
  131. assertNull(JGitUtils.getCommit(repository, null));
  132. repository.close();
  133. RepositoryCache.close(repository);
  134. FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
  135. }
  136. }
  137. @Test
  138. public void testCreateRepositoryShared() throws Exception {
  139. String[] repositories = { "NewSharedTestRepository.git" };
  140. for (String repositoryName : repositories) {
  141. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
  142. repositoryName, "group");
  143. File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
  144. FS.DETECTED);
  145. assertNotNull(repository);
  146. assertFalse(JGitUtils.hasCommits(repository));
  147. assertNull(JGitUtils.getFirstCommit(repository, null));
  148. assertEquals("1", repository.getConfig().getString("core", null, "sharedRepository"));
  149. assertTrue(folder.exists());
  150. if (! JnaUtils.isWindows()) {
  151. int mode = JnaUtils.getFilemode(folder);
  152. assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
  153. assertEquals(JnaUtils.S_IRWXG, mode & JnaUtils.S_IRWXG);
  154. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");
  155. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
  156. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
  157. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
  158. }
  159. repository.close();
  160. RepositoryCache.close(repository);
  161. FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
  162. }
  163. }
  164. @Test
  165. public void testCreateRepositorySharedCustom() throws Exception {
  166. String[] repositories = { "NewSharedTestRepository.git" };
  167. for (String repositoryName : repositories) {
  168. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
  169. repositoryName, "660");
  170. File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
  171. FS.DETECTED);
  172. assertNotNull(repository);
  173. assertFalse(JGitUtils.hasCommits(repository));
  174. assertNull(JGitUtils.getFirstCommit(repository, null));
  175. assertEquals("0660", repository.getConfig().getString("core", null, "sharedRepository"));
  176. assertTrue(folder.exists());
  177. if (! JnaUtils.isWindows()) {
  178. int mode = JnaUtils.getFilemode(folder);
  179. assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
  180. assertEquals(JnaUtils.S_IRWXG, mode & JnaUtils.S_IRWXG);
  181. assertEquals(0, mode & JnaUtils.S_IRWXO);
  182. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");
  183. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
  184. assertEquals(0, mode & JnaUtils.S_IRWXO);
  185. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
  186. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
  187. assertEquals(0, mode & JnaUtils.S_IRWXO);
  188. }
  189. repository.close();
  190. RepositoryCache.close(repository);
  191. FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
  192. }
  193. }
  194. @Test
  195. public void testCreateRepositorySharedSgidParent() throws Exception {
  196. if (! JnaUtils.isWindows()) {
  197. String repositoryAll = "NewTestRepositoryAll.git";
  198. String repositoryUmask = "NewTestRepositoryUmask.git";
  199. String sgidParent = "sgid";
  200. File parent = new File(GitBlitSuite.REPOSITORIES, sgidParent);
  201. File folder = null;
  202. boolean parentExisted = parent.exists();
  203. try {
  204. if (!parentExisted) {
  205. assertTrue("Could not create SGID parent folder.", parent.mkdir());
  206. }
  207. int mode = JnaUtils.getFilemode(parent);
  208. assertTrue(mode > 0);
  209. assertEquals(0, JnaUtils.setFilemode(parent, mode | JnaUtils.S_ISGID | JnaUtils.S_IWGRP));
  210. Repository repository = JGitUtils.createRepository(parent, repositoryAll, "all");
  211. folder = FileKey.resolve(new File(parent, repositoryAll), FS.DETECTED);
  212. assertNotNull(repository);
  213. assertEquals("2", repository.getConfig().getString("core", null, "sharedRepository"));
  214. assertTrue(folder.exists());
  215. mode = JnaUtils.getFilemode(folder);
  216. assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
  217. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");
  218. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
  219. assertEquals(JnaUtils.S_IROTH, mode & JnaUtils.S_IRWXO);
  220. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
  221. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
  222. assertEquals(JnaUtils.S_IROTH, mode & JnaUtils.S_IRWXO);
  223. repository.close();
  224. RepositoryCache.close(repository);
  225. repository = JGitUtils.createRepository(parent, repositoryUmask, "umask");
  226. folder = FileKey.resolve(new File(parent, repositoryUmask), FS.DETECTED);
  227. assertNotNull(repository);
  228. assertEquals(null, repository.getConfig().getString("core", null, "sharedRepository"));
  229. assertTrue(folder.exists());
  230. mode = JnaUtils.getFilemode(folder);
  231. assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
  232. repository.close();
  233. RepositoryCache.close(repository);
  234. }
  235. finally {
  236. FileUtils.delete(new File(parent, repositoryAll), FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
  237. FileUtils.delete(new File(parent, repositoryUmask), FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
  238. if (!parentExisted) {
  239. FileUtils.delete(parent, FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
  240. }
  241. }
  242. }
  243. }
  244. @Test
  245. public void testRefs() throws Exception {
  246. Repository repository = GitBlitSuite.getJGitRepository();
  247. Map<ObjectId, List<RefModel>> map = JGitUtils.getAllRefs(repository);
  248. repository.close();
  249. assertTrue(map.size() > 0);
  250. for (Map.Entry<ObjectId, List<RefModel>> entry : map.entrySet()) {
  251. List<RefModel> list = entry.getValue();
  252. for (RefModel ref : list) {
  253. if (ref.displayName.equals("refs/tags/spearce-gpg-pub")) {
  254. assertEquals("refs/tags/spearce-gpg-pub", ref.toString());
  255. assertEquals("8bbde7aacf771a9afb6992434f1ae413e010c6d8", ref.getObjectId()
  256. .getName());
  257. assertEquals("spearce@spearce.org", ref.getAuthorIdent().getEmailAddress());
  258. assertTrue(ref.getShortMessage().startsWith("GPG key"));
  259. assertTrue(ref.getFullMessage().startsWith("GPG key"));
  260. assertEquals(Constants.OBJ_BLOB, ref.getReferencedObjectType());
  261. } else if (ref.displayName.equals("refs/tags/v0.12.1")) {
  262. assertTrue(ref.isAnnotatedTag());
  263. }
  264. }
  265. }
  266. }
  267. @Test
  268. public void testBranches() throws Exception {
  269. Repository repository = GitBlitSuite.getJGitRepository();
  270. assertTrue(JGitUtils.getLocalBranches(repository, true, 0).size() == 0);
  271. for (RefModel model : JGitUtils.getLocalBranches(repository, true, -1)) {
  272. assertTrue(model.getName().startsWith(Constants.R_HEADS));
  273. assertTrue(model.equals(model));
  274. assertFalse(model.equals(""));
  275. assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
  276. + model.getName().hashCode());
  277. assertTrue(model.getShortMessage().equals(model.getShortMessage()));
  278. }
  279. for (RefModel model : JGitUtils.getRemoteBranches(repository, true, -1)) {
  280. assertTrue(model.getName().startsWith(Constants.R_REMOTES));
  281. assertTrue(model.equals(model));
  282. assertFalse(model.equals(""));
  283. assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
  284. + model.getName().hashCode());
  285. assertTrue(model.getShortMessage().equals(model.getShortMessage()));
  286. }
  287. assertTrue(JGitUtils.getRemoteBranches(repository, true, 8).size() == 8);
  288. repository.close();
  289. }
  290. @Test
  291. public void testTags() throws Exception {
  292. Repository repository = GitBlitSuite.getJGitRepository();
  293. assertTrue(JGitUtils.getTags(repository, true, 5).size() == 5);
  294. for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
  295. if (model.getObjectId().getName().equals("d28091fb2977077471138fe97da1440e0e8ae0da")) {
  296. assertTrue("Not an annotated tag!", model.isAnnotatedTag());
  297. }
  298. assertTrue(model.getName().startsWith(Constants.R_TAGS));
  299. assertTrue(model.equals(model));
  300. assertFalse(model.equals(""));
  301. assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
  302. + model.getName().hashCode());
  303. }
  304. repository.close();
  305. repository = GitBlitSuite.getGitectiveRepository();
  306. for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
  307. if (model.getObjectId().getName().equals("035254295a9bba11f72b1f9d6791a6b957abee7b")) {
  308. assertFalse(model.isAnnotatedTag());
  309. assertTrue(model.getAuthorIdent().getEmailAddress().equals("kevinsawicki@gmail.com"));
  310. assertEquals("Add scm and issue tracker elements to pom.xml\n", model.getFullMessage());
  311. }
  312. }
  313. repository.close();
  314. }
  315. @Test
  316. public void testCommitNotes() throws Exception {
  317. Repository repository = GitBlitSuite.getJGitRepository();
  318. RevCommit commit = JGitUtils.getCommit(repository,
  319. "690c268c793bfc218982130fbfc25870f292295e");
  320. List<GitNote> list = JGitUtils.getNotesOnCommit(repository, commit);
  321. repository.close();
  322. assertTrue(list.size() > 0);
  323. assertEquals("183474d554e6f68478a02d9d7888b67a9338cdff", list.get(0).notesRef
  324. .getReferencedObjectId().getName());
  325. }
  326. @Test
  327. public void testRelinkHEAD() throws Exception {
  328. Repository repository = GitBlitSuite.getJGitRepository();
  329. // confirm HEAD is master
  330. String currentRef = JGitUtils.getHEADRef(repository);
  331. assertEquals("refs/heads/master", currentRef);
  332. List<String> availableHeads = JGitUtils.getAvailableHeadTargets(repository);
  333. assertTrue(availableHeads.size() > 0);
  334. // set HEAD to stable-1.2
  335. JGitUtils.setHEADtoRef(repository, "refs/heads/stable-1.2");
  336. currentRef = JGitUtils.getHEADRef(repository);
  337. assertEquals("refs/heads/stable-1.2", currentRef);
  338. // restore HEAD to master
  339. JGitUtils.setHEADtoRef(repository, "refs/heads/master");
  340. currentRef = JGitUtils.getHEADRef(repository);
  341. assertEquals("refs/heads/master", currentRef);
  342. repository.close();
  343. }
  344. @Test
  345. public void testRelinkBranch() throws Exception {
  346. Repository repository = GitBlitSuite.getJGitRepository();
  347. // create/set the branch
  348. JGitUtils.setBranchRef(repository, "refs/heads/reftest", "3b358ce514ec655d3ff67de1430994d8428cdb04");
  349. assertEquals(1, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("3b358ce514ec655d3ff67de1430994d8428cdb04")).size());
  350. assertEquals(null, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("755dfdb40948f5c1ec79e06bde3b0a78c352f27f")));
  351. // reset the branch
  352. JGitUtils.setBranchRef(repository, "refs/heads/reftest", "755dfdb40948f5c1ec79e06bde3b0a78c352f27f");
  353. assertEquals(null, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("3b358ce514ec655d3ff67de1430994d8428cdb04")));
  354. assertEquals(1, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("755dfdb40948f5c1ec79e06bde3b0a78c352f27f")).size());
  355. // delete the branch
  356. assertTrue(JGitUtils.deleteBranchRef(repository, "refs/heads/reftest"));
  357. repository.close();
  358. }
  359. @Test
  360. public void testCreateOrphanedBranch() throws Exception {
  361. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, "orphantest");
  362. assertTrue(JGitUtils.createOrphanBranch(repository,
  363. "x" + Long.toHexString(System.currentTimeMillis()).toUpperCase(), null));
  364. FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
  365. }
  366. @Test
  367. public void testStringContent() throws Exception {
  368. Repository repository = GitBlitSuite.getHelloworldRepository();
  369. String contentA = JGitUtils.getStringContent(repository, (RevTree) null, "java.java");
  370. RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
  371. String contentB = JGitUtils.getStringContent(repository, commit.getTree(), "java.java");
  372. String contentC = JGitUtils.getStringContent(repository, commit.getTree(), "missing.txt");
  373. // manually construct a blob, calculate the hash, lookup the hash in git
  374. StringBuilder sb = new StringBuilder();
  375. sb.append("blob ").append(contentA.length()).append('\0');
  376. sb.append(contentA);
  377. String sha1 = StringUtils.getSHA1(sb.toString());
  378. String contentD = JGitUtils.getStringContent(repository, sha1);
  379. repository.close();
  380. assertTrue("ContentA is null!", contentA != null && contentA.length() > 0);
  381. assertTrue("ContentB is null!", contentB != null && contentB.length() > 0);
  382. assertTrue(contentA.equals(contentB));
  383. assertNull(contentC);
  384. assertTrue(contentA.equals(contentD));
  385. }
  386. @Test
  387. public void testFilesInCommit() throws Exception {
  388. Repository repository = GitBlitSuite.getHelloworldRepository();
  389. RevCommit commit = JGitUtils.getCommit(repository,
  390. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  391. List<PathChangeModel> paths = JGitUtils.getFilesInCommit(repository, commit);
  392. commit = JGitUtils.getCommit(repository, "af0e9b2891fda85afc119f04a69acf7348922830");
  393. List<PathChangeModel> deletions = JGitUtils.getFilesInCommit(repository, commit);
  394. commit = JGitUtils.getFirstCommit(repository, null);
  395. List<PathChangeModel> additions = JGitUtils.getFilesInCommit(repository, commit);
  396. List<PathChangeModel> latestChanges = JGitUtils.getFilesInCommit(repository, null);
  397. repository.close();
  398. assertTrue("No changed paths found!", paths.size() == 1);
  399. for (PathChangeModel path : paths) {
  400. assertTrue("PathChangeModel hashcode incorrect!",
  401. path.hashCode() == (path.commitId.hashCode() + path.path.hashCode()));
  402. assertTrue("PathChangeModel equals itself failed!", path.equals(path));
  403. assertFalse("PathChangeModel equals string failed!", path.equals(""));
  404. }
  405. assertEquals(ChangeType.DELETE, deletions.get(0).changeType);
  406. assertEquals(ChangeType.ADD, additions.get(0).changeType);
  407. assertTrue(latestChanges.size() > 0);
  408. }
  409. @Test
  410. public void testFilesInPath() throws Exception {
  411. assertEquals(0, JGitUtils.getFilesInPath(null, null, null).size());
  412. Repository repository = GitBlitSuite.getHelloworldRepository();
  413. List<PathModel> files = JGitUtils.getFilesInPath(repository, null, null);
  414. repository.close();
  415. assertTrue(files.size() > 10);
  416. }
  417. @Test
  418. public void testDocuments() throws Exception {
  419. Repository repository = GitBlitSuite.getTicgitRepository();
  420. List<String> extensions = GitBlit.getStrings(Keys.web.markdownExtensions);
  421. List<PathModel> markdownDocs = JGitUtils.getDocuments(repository, extensions);
  422. List<PathModel> markdownDocs2 = JGitUtils.getDocuments(repository,
  423. Arrays.asList(new String[] { ".mkd", ".md" }));
  424. List<PathModel> allFiles = JGitUtils.getDocuments(repository, null);
  425. repository.close();
  426. assertTrue(markdownDocs.size() > 0);
  427. assertTrue(markdownDocs2.size() > 0);
  428. assertTrue(allFiles.size() > markdownDocs.size());
  429. }
  430. @Test
  431. public void testFileModes() throws Exception {
  432. assertEquals("drwxr-xr-x", JGitUtils.getPermissionsFromMode(FileMode.TREE.getBits()));
  433. assertEquals("-rw-r--r--",
  434. JGitUtils.getPermissionsFromMode(FileMode.REGULAR_FILE.getBits()));
  435. assertEquals("-rwxr-xr-x",
  436. JGitUtils.getPermissionsFromMode(FileMode.EXECUTABLE_FILE.getBits()));
  437. assertEquals("symlink", JGitUtils.getPermissionsFromMode(FileMode.SYMLINK.getBits()));
  438. assertEquals("submodule", JGitUtils.getPermissionsFromMode(FileMode.GITLINK.getBits()));
  439. assertEquals("missing", JGitUtils.getPermissionsFromMode(FileMode.MISSING.getBits()));
  440. }
  441. @Test
  442. public void testRevlog() throws Exception {
  443. assertTrue(JGitUtils.getRevLog(null, 0).size() == 0);
  444. List<RevCommit> commits = JGitUtils.getRevLog(null, 10);
  445. assertEquals(0, commits.size());
  446. Repository repository = GitBlitSuite.getHelloworldRepository();
  447. // get most recent 10 commits
  448. commits = JGitUtils.getRevLog(repository, 10);
  449. assertEquals(10, commits.size());
  450. // test paging and offset by getting the 10th most recent commit
  451. RevCommit lastCommit = JGitUtils.getRevLog(repository, null, 9, 1).get(0);
  452. assertEquals(lastCommit, commits.get(9));
  453. // grab the two most recent commits to java.java
  454. commits = JGitUtils.getRevLog(repository, null, "java.java", 0, 2);
  455. assertEquals(2, commits.size());
  456. // grab the commits since 2008-07-15
  457. commits = JGitUtils.getRevLog(repository, null,
  458. new SimpleDateFormat("yyyy-MM-dd").parse("2008-07-15"));
  459. assertEquals(12, commits.size());
  460. repository.close();
  461. }
  462. @Test
  463. public void testRevLogRange() throws Exception {
  464. Repository repository = GitBlitSuite.getHelloworldRepository();
  465. List<RevCommit> commits = JGitUtils.getRevLog(repository,
  466. "fbd14fa6d1a01d4aefa1fca725792683800fc67e",
  467. "85a0e4087b8439c0aa6b1f4f9e08c26052ab7e87");
  468. repository.close();
  469. assertEquals(14, commits.size());
  470. }
  471. @Test
  472. public void testSearchTypes() throws Exception {
  473. assertEquals(SearchType.COMMIT, SearchType.forName("commit"));
  474. assertEquals(SearchType.COMMITTER, SearchType.forName("committer"));
  475. assertEquals(SearchType.AUTHOR, SearchType.forName("author"));
  476. assertEquals(SearchType.COMMIT, SearchType.forName("unknown"));
  477. assertEquals("commit", SearchType.COMMIT.toString());
  478. assertEquals("committer", SearchType.COMMITTER.toString());
  479. assertEquals("author", SearchType.AUTHOR.toString());
  480. }
  481. @Test
  482. public void testSearchRevlogs() throws Exception {
  483. assertEquals(0, JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0, 0).size());
  484. List<RevCommit> results = JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0,
  485. 3);
  486. assertEquals(0, results.size());
  487. // test commit message search
  488. Repository repository = GitBlitSuite.getHelloworldRepository();
  489. results = JGitUtils.searchRevlogs(repository, null, "java", SearchType.COMMIT, 0, 3);
  490. assertEquals(3, results.size());
  491. // test author search
  492. results = JGitUtils.searchRevlogs(repository, null, "timothy", SearchType.AUTHOR, 0, -1);
  493. assertEquals(1, results.size());
  494. // test committer search
  495. results = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER, 0, 10);
  496. assertEquals(10, results.size());
  497. // test paging and offset
  498. RevCommit commit = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER,
  499. 9, 1).get(0);
  500. assertEquals(results.get(9), commit);
  501. repository.close();
  502. }
  503. @Test
  504. public void testZip() throws Exception {
  505. assertFalse(CompressionUtils.zip(null, null, null, null));
  506. Repository repository = GitBlitSuite.getHelloworldRepository();
  507. File zipFileA = new File(GitBlitSuite.REPOSITORIES, "helloworld.zip");
  508. FileOutputStream fosA = new FileOutputStream(zipFileA);
  509. boolean successA = CompressionUtils.zip(repository, null, Constants.HEAD, fosA);
  510. fosA.close();
  511. File zipFileB = new File(GitBlitSuite.REPOSITORIES, "helloworld-java.zip");
  512. FileOutputStream fosB = new FileOutputStream(zipFileB);
  513. boolean successB = CompressionUtils.zip(repository, "java.java", Constants.HEAD, fosB);
  514. fosB.close();
  515. repository.close();
  516. assertTrue("Failed to generate zip file!", successA);
  517. assertTrue(zipFileA.length() > 0);
  518. zipFileA.delete();
  519. assertTrue("Failed to generate zip file!", successB);
  520. assertTrue(zipFileB.length() > 0);
  521. zipFileB.delete();
  522. }
  523. @Test
  524. public void testPlots() throws Exception {
  525. Repository repository = GitBlitSuite.getTicgitRepository();
  526. PlotWalk pw = new PlotWalk(repository);
  527. PlotCommitList<PlotLane> commits = new PlotCommitList<PlotLane>();
  528. commits.source(pw);
  529. commits.fillTo(25);
  530. for (PlotCommit<PlotLane> commit : commits) {
  531. System.out.println(commit);
  532. }
  533. repository.close();
  534. }
  535. }