I'm using JGit to clone and interact with a Git repository hosted on Azure DevOps. My goal is to clone the master branch, checkout to my specified branch (branchName), update the <revision> value from the pom.xml file under the <properties> tag (using pomManipulations.modifyPomFile(pomFilePath)), and then push the updated local branch back to the remote repository of the same branch. While the revision value is fetched correctly, I'm encountering a RefNotFoundException when attempting to check out the branch.
Issue: The code successfully clones the repository and fetches the revision value from pom.xml, but when attempting to checkout the master branch, I receive a RefNotFoundException. This suggests that the branch reference cannot be resolved.
NOTE: The branch name I'm providing is correct and that branch also exists remotely in AzureDevops.
Here's the relevant portion of my code:
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class AzureDevOpsBranchUpdater {
private static final String PAT_FILE_PATH = "path/to/pat.txt"; // Update with the correct path
public String updatePom(String organization, String project, String repositoryName, String branchName, String Id) {
String repoUrl = "https://dev.azure.com/" + organization + "/" + project + "/_git/" + repositoryName;
String localRepoPath = "C:/Users/" + Id + "/AutomatePipeline/" + repositoryName;
String pomFilePath = localRepoPath + "/pom.xml";
try {
// Read the Personal Access Token (PAT)
String PAT = Files.readString(Paths.get(PAT_FILE_PATH)).trim();
// Clone the repository and checkout master
Git git = Git.cloneRepository()
.setURI(repoUrl)
.setDirectory(new File(localRepoPath))
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("", PAT))
.setBranch("master")
.call();
// Checkout the specified branch
git.checkout().setName(branchName).call();
// Pull the latest changes from master
git.fetch().setCredentialsProvider(new UsernamePasswordCredentialsProvider("", PAT)).call();
MergeResult mergeResult = git.merge()
.include(git.getRepository().findRef("master"))
.call();
if (!mergeResult.getMergeStatus().isSuccessful()) {
return "Merge conflicts occurred: " + mergeResult.getMergeStatus();
}
// Modify the pom.xml file
pomManipulations.modifyPomFile(pomFilePath);
// Commit the changes
git.add().addFilepattern("pom.xml").call();
git.commit().setMessage("Updated pom.xml").call();
// Push the changes to the remote branch
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider("", PAT)).call();
return "Branch "+ BRANCH +"'s pom.xml updated successfully";
} catch (GitAPIException | IOException e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
}
private String getRevisionFromPom(String pomFilePath) throws IOException {
XmlMapper xmlMapper = new XmlMapper();
JsonNode root = xmlMapper.readTree(new File(pomFilePath));
JsonNode propertiesNode = root.findPath("properties");
if (propertiesNode.isMissingNode()) {
return null; // <properties> tag not found
}
JsonNode revisionNode = propertiesNode.findPath("revision");
if (revisionNode.isMissingNode()) {
return null; // <revision> tag not found in the <properties> section
}
return revisionNode.asText(); // Return the revision value
}
}
Troubleshooting Steps Taken:
- Verified the branch name and existence in the remote repository.
- Ensured the Personal Access Token (PAT) has necessary permissions. Checked network connectivity and repository URL.
- Confirmed Git configurations are correct.
Question: What could be causing the RefNotFoundException during the checkout process, and how can I resolve it?