|
1 | 1 | /*
|
2 |
| - * Copyright (C) 2011, 2013 Chris Aniszczyk <[email protected]> and others |
| 2 | + * Copyright (C) 2011, 2022 Chris Aniszczyk <[email protected]> and others |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials are made available under the
|
5 | 5 | * terms of the Eclipse Distribution License v. 1.0 which is available at
|
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.IOException;
|
21 | 21 | import java.net.URISyntaxException;
|
| 22 | +import java.time.Instant; |
22 | 23 | import java.util.Collections;
|
23 | 24 | import java.util.List;
|
24 | 25 | import java.util.Map;
|
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
25 | 28 | import java.util.stream.Stream;
|
| 29 | +import java.util.stream.StreamSupport; |
26 | 30 |
|
27 | 31 | import org.eclipse.jgit.api.ListBranchCommand.ListMode;
|
28 | 32 | import org.eclipse.jgit.api.errors.GitAPIException;
|
|
40 | 44 | import org.eclipse.jgit.lib.StoredConfig;
|
41 | 45 | import org.eclipse.jgit.revwalk.RevBlob;
|
42 | 46 | import org.eclipse.jgit.revwalk.RevCommit;
|
| 47 | +import org.eclipse.jgit.revwalk.RevObject; |
43 | 48 | import org.eclipse.jgit.submodule.SubmoduleStatus;
|
44 | 49 | import org.eclipse.jgit.submodule.SubmoduleStatusType;
|
45 | 50 | import org.eclipse.jgit.submodule.SubmoduleWalk;
|
@@ -895,6 +900,234 @@ public void testCloneWithHeadSymRefIsNonMasterCopy() throws IOException, GitAPIE
|
895 | 900 | assertEquals("refs/heads/test-copy", git2.getRepository().getFullBranch());
|
896 | 901 | }
|
897 | 902 |
|
| 903 | + @Test |
| 904 | + public void testCloneRepositoryWithDepth() throws IOException, JGitInternalException, GitAPIException { |
| 905 | + File directory = createTempDirectory("testCloneRepositoryWithDepth"); |
| 906 | + CloneCommand command = Git.cloneRepository(); |
| 907 | + command.setDirectory(directory); |
| 908 | + command.setURI(fileUri()); |
| 909 | + command.setDepth(1); |
| 910 | + command.setBranchesToClone(Set.of("refs/heads/test")); |
| 911 | + Git git2 = command.call(); |
| 912 | + addRepoToClose(git2.getRepository()); |
| 913 | + |
| 914 | + List<RevCommit> log = StreamSupport.stream(git2.log().all().call().spliterator(), false) |
| 915 | + .collect(Collectors.toList()); |
| 916 | + assertEquals(1, log.size()); |
| 917 | + RevCommit commit = log.get(0); |
| 918 | + assertEquals(Set.of(commit.getId()), |
| 919 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 920 | + assertEquals("Second commit", commit.getFullMessage()); |
| 921 | + assertEquals(0, commit.getParentCount()); |
| 922 | + } |
| 923 | + |
| 924 | + @Test |
| 925 | + public void testCloneRepositoryWithDepthAndAllBranches() throws IOException, JGitInternalException, GitAPIException { |
| 926 | + File directory = createTempDirectory("testCloneRepositoryWithDepthAndAllBranches"); |
| 927 | + CloneCommand command = Git.cloneRepository(); |
| 928 | + command.setDirectory(directory); |
| 929 | + command.setURI(fileUri()); |
| 930 | + command.setDepth(1); |
| 931 | + command.setCloneAllBranches(true); |
| 932 | + Git git2 = command.call(); |
| 933 | + addRepoToClose(git2.getRepository()); |
| 934 | + |
| 935 | + List<RevCommit> log = StreamSupport.stream(git2.log().all().call().spliterator(), false) |
| 936 | + .collect(Collectors.toList()); |
| 937 | + assertEquals(2, log.size()); |
| 938 | + assertEquals(log.stream().map(RevCommit::getId).collect(Collectors.toSet()), |
| 939 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 940 | + assertEquals(List.of("Second commit", "Initial commit"), |
| 941 | + log.stream().map(RevCommit::getFullMessage).collect(Collectors.toList())); |
| 942 | + for (RevCommit commit : log) { |
| 943 | + assertEquals(0, commit.getParentCount()); |
| 944 | + } |
| 945 | + } |
| 946 | + |
| 947 | + @Test |
| 948 | + public void testCloneRepositoryWithDepth2() throws Exception { |
| 949 | + RevCommit parent = tr.git().log().call().iterator().next(); |
| 950 | + RevCommit commit = tr.commit() |
| 951 | + .parent(parent) |
| 952 | + .message("Third commit") |
| 953 | + .add("test.txt", "Hello world") |
| 954 | + .create(); |
| 955 | + tr.update("refs/heads/test", commit); |
| 956 | + |
| 957 | + File directory = createTempDirectory("testCloneRepositoryWithDepth2"); |
| 958 | + CloneCommand command = Git.cloneRepository(); |
| 959 | + command.setDirectory(directory); |
| 960 | + command.setURI(fileUri()); |
| 961 | + command.setDepth(2); |
| 962 | + command.setBranchesToClone(Set.of("refs/heads/test")); |
| 963 | + Git git2 = command.call(); |
| 964 | + addRepoToClose(git2.getRepository()); |
| 965 | + |
| 966 | + List<RevCommit> log = StreamSupport |
| 967 | + .stream(git2.log().all().call().spliterator(), false) |
| 968 | + .collect(Collectors.toList()); |
| 969 | + assertEquals(2, log.size()); |
| 970 | + assertEquals(Set.of(parent.getId()), |
| 971 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 972 | + assertEquals(List.of("Third commit", "Second commit"), log.stream() |
| 973 | + .map(RevCommit::getFullMessage).collect(Collectors.toList())); |
| 974 | + assertEquals(List.of(Integer.valueOf(1), Integer.valueOf(0)), |
| 975 | + log.stream().map(RevCommit::getParentCount) |
| 976 | + .collect(Collectors.toList())); |
| 977 | + } |
| 978 | + |
| 979 | + @Test |
| 980 | + public void testCloneRepositoryWithDepthAndFetch() throws Exception { |
| 981 | + File directory = createTempDirectory("testCloneRepositoryWithDepthAndFetch"); |
| 982 | + CloneCommand command = Git.cloneRepository(); |
| 983 | + command.setDirectory(directory); |
| 984 | + command.setURI(fileUri()); |
| 985 | + command.setDepth(1); |
| 986 | + command.setBranchesToClone(Set.of("refs/heads/test")); |
| 987 | + Git git2 = command.call(); |
| 988 | + addRepoToClose(git2.getRepository()); |
| 989 | + |
| 990 | + RevCommit parent = tr.git().log().call().iterator().next(); |
| 991 | + RevCommit commit = tr.commit() |
| 992 | + .parent(parent) |
| 993 | + .message("Third commit") |
| 994 | + .add("test.txt", "Hello world") |
| 995 | + .create(); |
| 996 | + tr.update("refs/heads/test", commit); |
| 997 | + |
| 998 | + git2.fetch().call(); |
| 999 | + |
| 1000 | + List<RevCommit> log = StreamSupport |
| 1001 | + .stream(git2.log().all().call().spliterator(), false) |
| 1002 | + .collect(Collectors.toList()); |
| 1003 | + assertEquals(2, log.size()); |
| 1004 | + assertEquals(Set.of(parent.getId()), |
| 1005 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 1006 | + assertEquals(List.of("Third commit", "Second commit"), log.stream() |
| 1007 | + .map(RevCommit::getFullMessage).collect(Collectors.toList())); |
| 1008 | + assertEquals(List.of(Integer.valueOf(1), Integer.valueOf(0)), |
| 1009 | + log.stream().map(RevCommit::getParentCount) |
| 1010 | + .collect(Collectors.toList())); |
| 1011 | + } |
| 1012 | + |
| 1013 | + @Test |
| 1014 | + public void testCloneRepositoryWithDepthAndFetchWithDepth() throws Exception { |
| 1015 | + File directory = createTempDirectory("testCloneRepositoryWithDepthAndFetchWithDepth"); |
| 1016 | + CloneCommand command = Git.cloneRepository(); |
| 1017 | + command.setDirectory(directory); |
| 1018 | + command.setURI(fileUri()); |
| 1019 | + command.setDepth(1); |
| 1020 | + command.setBranchesToClone(Set.of("refs/heads/test")); |
| 1021 | + Git git2 = command.call(); |
| 1022 | + addRepoToClose(git2.getRepository()); |
| 1023 | + |
| 1024 | + RevCommit parent = tr.git().log().call().iterator().next(); |
| 1025 | + RevCommit commit = tr.commit() |
| 1026 | + .parent(parent) |
| 1027 | + .message("Third commit") |
| 1028 | + .add("test.txt", "Hello world") |
| 1029 | + .create(); |
| 1030 | + tr.update("refs/heads/test", commit); |
| 1031 | + |
| 1032 | + git2.fetch().setDepth(1).call(); |
| 1033 | + |
| 1034 | + List<RevCommit> log = StreamSupport |
| 1035 | + .stream(git2.log().all().call().spliterator(), false) |
| 1036 | + .collect(Collectors.toList()); |
| 1037 | + assertEquals(2, log.size()); |
| 1038 | + assertEquals( |
| 1039 | + log.stream().map(RevObject::getId).collect(Collectors.toSet()), |
| 1040 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 1041 | + assertEquals(List.of("Third commit", "Second commit"), log.stream() |
| 1042 | + .map(RevCommit::getFullMessage).collect(Collectors.toList())); |
| 1043 | + assertEquals(List.of(Integer.valueOf(0), Integer.valueOf(0)), |
| 1044 | + log.stream().map(RevCommit::getParentCount) |
| 1045 | + .collect(Collectors.toList())); |
| 1046 | + } |
| 1047 | + |
| 1048 | + @Test |
| 1049 | + public void testCloneRepositoryWithDepthAndFetchUnshallow() throws Exception { |
| 1050 | + File directory = createTempDirectory("testCloneRepositoryWithDepthAndFetchUnshallow"); |
| 1051 | + CloneCommand command = Git.cloneRepository(); |
| 1052 | + command.setDirectory(directory); |
| 1053 | + command.setURI(fileUri()); |
| 1054 | + command.setDepth(1); |
| 1055 | + command.setBranchesToClone(Set.of("refs/heads/test")); |
| 1056 | + Git git2 = command.call(); |
| 1057 | + addRepoToClose(git2.getRepository()); |
| 1058 | + |
| 1059 | + git2.fetch().setUnshallow(true).call(); |
| 1060 | + |
| 1061 | + List<RevCommit> log = StreamSupport |
| 1062 | + .stream(git2.log().all().call().spliterator(), false) |
| 1063 | + .collect(Collectors.toList()); |
| 1064 | + assertEquals(2, log.size()); |
| 1065 | + assertEquals(Set.of(), |
| 1066 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 1067 | + assertEquals(List.of("Second commit", "Initial commit"), log.stream() |
| 1068 | + .map(RevCommit::getFullMessage).collect(Collectors.toList())); |
| 1069 | + assertEquals(List.of(Integer.valueOf(1), Integer.valueOf(0)), |
| 1070 | + log.stream().map(RevCommit::getParentCount) |
| 1071 | + .collect(Collectors.toList())); |
| 1072 | + } |
| 1073 | + |
| 1074 | + @Test |
| 1075 | + public void testCloneRepositoryWithShallowSince() throws Exception { |
| 1076 | + RevCommit commit = tr.commit() |
| 1077 | + .parent(tr.git().log().call().iterator().next()) |
| 1078 | + .message("Third commit").add("test.txt", "Hello world") |
| 1079 | + .create(); |
| 1080 | + tr.update("refs/heads/test", commit); |
| 1081 | + |
| 1082 | + File directory = createTempDirectory("testCloneRepositoryWithShallowSince"); |
| 1083 | + CloneCommand command = Git.cloneRepository(); |
| 1084 | + command.setDirectory(directory); |
| 1085 | + command.setURI(fileUri()); |
| 1086 | + command.setShallowSince(Instant.ofEpochSecond(commit.getCommitTime())); |
| 1087 | + command.setBranchesToClone(Set.of("refs/heads/test")); |
| 1088 | + Git git2 = command.call(); |
| 1089 | + addRepoToClose(git2.getRepository()); |
| 1090 | + |
| 1091 | + List<RevCommit> log = StreamSupport |
| 1092 | + .stream(git2.log().all().call().spliterator(), false) |
| 1093 | + .collect(Collectors.toList()); |
| 1094 | + assertEquals(1, log.size()); |
| 1095 | + assertEquals(Set.of(commit.getId()), |
| 1096 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 1097 | + assertEquals("Third commit", log.get(0).getFullMessage()); |
| 1098 | + assertEquals(0, log.get(0).getParentCount()); |
| 1099 | + } |
| 1100 | + |
| 1101 | + @Test |
| 1102 | + public void testCloneRepositoryWithShallowExclude() throws Exception { |
| 1103 | + RevCommit parent = tr.git().log().call().iterator().next(); |
| 1104 | + tr.update("refs/heads/test", |
| 1105 | + tr.commit() |
| 1106 | + .parent(parent) |
| 1107 | + .message("Third commit") |
| 1108 | + .add("test.txt", "Hello world") |
| 1109 | + .create()); |
| 1110 | + |
| 1111 | + File directory = createTempDirectory("testCloneRepositoryWithShallowExclude"); |
| 1112 | + CloneCommand command = Git.cloneRepository(); |
| 1113 | + command.setDirectory(directory); |
| 1114 | + command.setURI(fileUri()); |
| 1115 | + command.addShallowExclude(parent.getId()); |
| 1116 | + command.setBranchesToClone(Set.of("refs/heads/test")); |
| 1117 | + Git git2 = command.call(); |
| 1118 | + addRepoToClose(git2.getRepository()); |
| 1119 | + |
| 1120 | + List<RevCommit> log = StreamSupport |
| 1121 | + .stream(git2.log().all().call().spliterator(), false) |
| 1122 | + .collect(Collectors.toList()); |
| 1123 | + assertEquals(1, log.size()); |
| 1124 | + RevCommit commit = log.get(0); |
| 1125 | + assertEquals(Set.of(commit.getId()), |
| 1126 | + git2.getRepository().getObjectDatabase().getShallowCommits()); |
| 1127 | + assertEquals("Third commit", commit.getFullMessage()); |
| 1128 | + assertEquals(0, commit.getParentCount()); |
| 1129 | + } |
| 1130 | + |
898 | 1131 | private void assertTagOption(Repository repo, TagOpt expectedTagOption)
|
899 | 1132 | throws URISyntaxException {
|
900 | 1133 | RemoteConfig remoteConfig = new RemoteConfig(
|
|
0 commit comments