@Test public void testCheckout() throws IOException, ScmException, NoHeadException, GitAPIException { final Path localGitRepos = targetDir.resolve("local-git-repos"); final Path srcdepsTestArtifactDirectory = localGitRepos.resolve("srcdeps-test-artifact"); SrcdepsCoreUtils.deleteDirectory(srcdepsTestArtifactDirectory); final String remoteGitUri = "https: final String mornigBranch = "morning-branch"; try (Git git = Git.cloneRepository().setURI(remoteGitUri).setDirectory(srcdepsTestArtifactDirectory.toFile()) .setCloneAllBranches(true).call()) { git.checkout().setName(mornigBranch).setCreateBranch(true).setStartPoint("origin/" + mornigBranch).call(); } final String localGitUri = srcdepsTestArtifactDirectory.resolve(".git").toUri().toString(); Path dir = targetDir.resolve("test-repo"); SrcdepsCoreUtils.ensureDirectoryExistsAndEmpty(dir); BuildRequest cloningRequest = BuildRequest.builder() .scmRepositoryId("test") .encoding(StandardCharsets.UTF_8) .output(LineConsumer::dummy) .srcVersion(SrcVersion.parse("0.0.1-SRC-tag-0.0.1")) .dependentProjectRootDirectory(dir) .projectRootDirectory(dir) .scmUrl("git:" + localGitUri) .versionsMavenPluginVersion(Maven.getDefaultVersionsMavenPluginVersion()) .gradleModelTransformer(CharStreamSource.defaultModelTransformer()) .build(); JGitScm jGitScm = new JGitScm(); String commitId = jGitScm.checkout(cloningRequest); Assert.assertEquals("19ef91ed30fd8b1a459803ee0c279dcf8e236184", commitId); assertCommit(dir, "19ef91ed30fd8b1a459803ee0c279dcf8e236184"); BuildRequest fetchingRequest = BuildRequest.builder() .scmRepositoryId("test") .encoding(StandardCharsets.UTF_8) .output(LineConsumer::dummy) .srcVersion(SrcVersion.parse("0.0.1-SRC-revision-0a5ab902099b24c2b13ed1dad8c5f537458bcc89")) .dependentProjectRootDirectory(dir) .projectRootDirectory(dir) .scmUrl("git:" + localGitUri) .versionsMavenPluginVersion(Maven.getDefaultVersionsMavenPluginVersion()) .gradleModelTransformer(CharStreamSource.defaultModelTransformer()) .build(); commitId = jGitScm.checkout(fetchingRequest); Assert.assertEquals("0a5ab902099b24c2b13ed1dad8c5f537458bcc89", commitId); assertCommit(dir, "0a5ab902099b24c2b13ed1dad8c5f537458bcc89"); BuildRequest fetchBranchRequest = BuildRequest.builder() .scmRepositoryId("test") .encoding(StandardCharsets.UTF_8) .output(LineConsumer::dummy) .srcVersion(SrcVersion.parse("0.0.1-SRC-branch-morning-branch")) .dependentProjectRootDirectory(dir) .projectRootDirectory(dir) .scmUrl("git:" + localGitUri) .versionsMavenPluginVersion(Maven.getDefaultVersionsMavenPluginVersion()) .gradleModelTransformer(CharStreamSource.defaultModelTransformer()) .build(); commitId = jGitScm.checkout(fetchBranchRequest); Assert.assertEquals("a84403b6fb44c5a588a9fe39d939c977e1e5c6a4", commitId); assertCommit(dir, "a84403b6fb44c5a588a9fe39d939c977e1e5c6a4"); final Path testTxtPath = srcdepsTestArtifactDirectory.resolve("test.txt"); Files.write(testTxtPath, "Test".getBytes(StandardCharsets.UTF_8)); String expectedCommit = null; try (Git git = Git.init().setDirectory(srcdepsTestArtifactDirectory.toFile()).call()) { git.add().addFilepattern("test.txt").call(); expectedCommit = git.commit().setMessage("Added test.txt").call().getId().getName(); } commitId = jGitScm.checkout(fetchBranchRequest); Assert.assertEquals(expectedCommit, commitId); try (Git git = Git.init().setDirectory(srcdepsTestArtifactDirectory.toFile()).call()) { git.reset().setMode(ResetType.HARD).setRef("a84403b6fb44c5a588a9fe39d939c977e1e5c6a4").call(); } commitId = jGitScm.checkout(fetchBranchRequest); Assert.assertEquals("a84403b6fb44c5a588a9fe39d939c977e1e5c6a4", commitId); assertCommit(dir, "a84403b6fb44c5a588a9fe39d939c977e1e5c6a4"); }
@Override public String checkout(BuildRequest request) throws ScmException { final Path dir = request.getProjectRootDirectory(); int i = 0; final List<String> urls = request.getScmUrls(); try (Git git = openGit(request.getScmRepositoryId(), dir)) { for (String url : urls) { final String useUrl = stripUriPrefix(url); final String result = fetchAndReset(request.getScmRepositoryId(), useUrl, i, urls.size(), request.getSrcVersion(), dir, git); if (result != null) { return result; } i++; } } throw new ScmException( String.format("Could not checkout [%s] from URLs %s", request.getSrcVersion(), request.getScmUrls())); }
JGitScm implements Scm { @Override public String checkout(BuildRequest request) throws ScmException { final Path dir = request.getProjectRootDirectory(); int i = 0; final List<String> urls = request.getScmUrls(); try (Git git = openGit(request.getScmRepositoryId(), dir)) { for (String url : urls) { final String useUrl = stripUriPrefix(url); final String result = fetchAndReset(request.getScmRepositoryId(), useUrl, i, urls.size(), request.getSrcVersion(), dir, git); if (result != null) { return result; } i++; } } throw new ScmException( String.format("Could not checkout [%s] from URLs %s", request.getSrcVersion(), request.getScmUrls())); } }
JGitScm implements Scm { @Override public String checkout(BuildRequest request) throws ScmException { final Path dir = request.getProjectRootDirectory(); int i = 0; final List<String> urls = request.getScmUrls(); try (Git git = openGit(request.getScmRepositoryId(), dir)) { for (String url : urls) { final String useUrl = stripUriPrefix(url); final String result = fetchAndReset(request.getScmRepositoryId(), useUrl, i, urls.size(), request.getSrcVersion(), dir, git); if (result != null) { return result; } i++; } } throw new ScmException( String.format("Could not checkout [%s] from URLs %s", request.getSrcVersion(), request.getScmUrls())); }  }
JGitScm implements Scm { @Override public String checkout(BuildRequest request) throws ScmException { final Path dir = request.getProjectRootDirectory(); int i = 0; final List<String> urls = request.getScmUrls(); try (Git git = openGit(request.getScmRepositoryId(), dir)) { for (String url : urls) { final String useUrl = stripUriPrefix(url); final String result = fetchAndReset(request.getScmRepositoryId(), useUrl, i, urls.size(), request.getSrcVersion(), dir, git); if (result != null) { return result; } i++; } } throw new ScmException( String.format("Could not checkout [%s] from URLs %s", request.getSrcVersion(), request.getScmUrls())); }  static String getScmGitPrefix(); static String getSrcdepsWorkingBranch(); @Override String checkout(BuildRequest request); @Override boolean supports(String url); }
JGitScm implements Scm { @Override public String checkout(BuildRequest request) throws ScmException { final Path dir = request.getProjectRootDirectory(); int i = 0; final List<String> urls = request.getScmUrls(); try (Git git = openGit(request.getScmRepositoryId(), dir)) { for (String url : urls) { final String useUrl = stripUriPrefix(url); final String result = fetchAndReset(request.getScmRepositoryId(), useUrl, i, urls.size(), request.getSrcVersion(), dir, git); if (result != null) { return result; } i++; } } throw new ScmException( String.format("Could not checkout [%s] from URLs %s", request.getSrcVersion(), request.getScmUrls())); }  static String getScmGitPrefix(); static String getSrcdepsWorkingBranch(); @Override String checkout(BuildRequest request); @Override boolean supports(String url);  }