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

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