Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LsRemoteCommandTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2011, 2013 Chris Aniszczyk <caniszczyk@gmail.com> and others. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.util.Collection;
  16. import java.util.Optional;
  17. import org.eclipse.jgit.junit.RepositoryTestCase;
  18. import org.eclipse.jgit.lib.Constants;
  19. import org.eclipse.jgit.lib.Ref;
  20. import org.eclipse.jgit.lib.RefUpdate;
  21. import org.junit.Test;
  22. public class LsRemoteCommandTest extends RepositoryTestCase {
  23. private Git git;
  24. @Override
  25. public void setUp() throws Exception {
  26. super.setUp();
  27. git = new Git(db);
  28. // commit something
  29. writeTrashFile("Test.txt", "Hello world");
  30. git.add().addFilepattern("Test.txt").call();
  31. git.commit().setMessage("Initial commit").call();
  32. // create a test branch and switch to it
  33. git.branchCreate().setName("test").call();
  34. RefUpdate rup = db.updateRef(Constants.HEAD);
  35. rup.link("refs/heads/test");
  36. // tags
  37. git.tag().setName("tag1").call();
  38. git.tag().setName("tag2").call();
  39. git.tag().setName("tag3").call();
  40. }
  41. @Test
  42. public void testLsRemote() throws Exception {
  43. File directory = createTempDirectory("testRepository");
  44. CloneCommand command = Git.cloneRepository();
  45. command.setDirectory(directory);
  46. command.setURI(fileUri());
  47. command.setCloneAllBranches(true);
  48. Git git2 = command.call();
  49. addRepoToClose(git2.getRepository());
  50. LsRemoteCommand lsRemoteCommand = git2.lsRemote();
  51. Collection<Ref> refs = lsRemoteCommand.call();
  52. assertNotNull(refs);
  53. assertEquals(6, refs.size());
  54. }
  55. @Test
  56. public void testLsRemoteWithTags() throws Exception {
  57. File directory = createTempDirectory("testRepository");
  58. CloneCommand command = Git.cloneRepository();
  59. command.setDirectory(directory);
  60. command.setURI(fileUri());
  61. command.setCloneAllBranches(true);
  62. Git git2 = command.call();
  63. addRepoToClose(git2.getRepository());
  64. LsRemoteCommand lsRemoteCommand = git2.lsRemote();
  65. lsRemoteCommand.setTags(true);
  66. Collection<Ref> refs = lsRemoteCommand.call();
  67. assertNotNull(refs);
  68. assertEquals(3, refs.size());
  69. }
  70. @Test
  71. public void testLsRemoteWithHeads() throws Exception {
  72. File directory = createTempDirectory("testRepository");
  73. CloneCommand command = Git.cloneRepository();
  74. command.setDirectory(directory);
  75. command.setURI(fileUri());
  76. command.setCloneAllBranches(true);
  77. Git git2 = command.call();
  78. addRepoToClose(git2.getRepository());
  79. LsRemoteCommand lsRemoteCommand = git2.lsRemote();
  80. lsRemoteCommand.setHeads(true);
  81. Collection<Ref> refs = lsRemoteCommand.call();
  82. assertNotNull(refs);
  83. assertEquals(2, refs.size());
  84. }
  85. @Test
  86. public void testLsRemoteWithoutLocalRepository() throws Exception {
  87. String uri = fileUri();
  88. Collection<Ref> refs = Git.lsRemoteRepository().setRemote(uri).setHeads(true).call();
  89. assertNotNull(refs);
  90. assertEquals(2, refs.size());
  91. }
  92. @Test
  93. public void testLsRemoteWithSymRefs() throws Exception {
  94. File directory = createTempDirectory("testRepository");
  95. CloneCommand command = Git.cloneRepository();
  96. command.setDirectory(directory);
  97. command.setURI(fileUri());
  98. command.setCloneAllBranches(true);
  99. Git git2 = command.call();
  100. addRepoToClose(git2.getRepository());
  101. LsRemoteCommand lsRemoteCommand = git2.lsRemote();
  102. Collection<Ref> refs = lsRemoteCommand.call();
  103. assertNotNull(refs);
  104. assertEquals(6, refs.size());
  105. Optional<Ref> headRef = refs.stream().filter(ref -> ref.getName().equals(Constants.HEAD)).findFirst();
  106. assertTrue("expected a HEAD Ref", headRef.isPresent());
  107. assertTrue("expected HEAD Ref to be a Symbolic", headRef.get().isSymbolic());
  108. assertEquals("refs/heads/test", headRef.get().getTarget().getName());
  109. }
  110. private String fileUri() {
  111. return "file://" + git.getRepository().getWorkTree().getAbsolutePath();
  112. }
  113. }