下面列出了怎么用org.eclipse.jgit.transport.TagOpt的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void tagListTest() throws IOException, GitAPIException {
Git git = Git.open(new File(gitPath));
/*ListTagCommand listTagCommand = git.tagList();
List<Ref> call = listTagCommand.call();
for(Ref ref : call){
System.out.println(ref.getName());
}*/
//
FetchCommand fetchCommand = git.fetch().setTagOpt(TagOpt.FETCH_TAGS);
fetchCommand.setCredentialsProvider(usernamePasswordCredentialsProvider);
fetchCommand.call();
List<Ref> tags = git.tagList().call();
if(tags != null && tags.size() > 0) {
for(Ref tag : tags) {
System.out.println(tag.getName());
}
}
}
@Test
public void checkoutBranch() throws IOException, GitAPIException {
String branchName = "branch1";
Git git = Git.open(new File(gitPath));
FetchCommand fetchCommand = git.fetch().setTagOpt(TagOpt.FETCH_TAGS);
fetchCommand.setCredentialsProvider(usernamePasswordCredentialsProvider);
fetchCommand.call();
List<Ref> refs = git.branchList().call();
boolean exist = false;// is branch exist
for (Ref ref : refs) {
String branchNameHad = getBranchName(ref);
if (StringUtils.equals(branchName, branchNameHad)) {
exist = true;
}
}
if (exist) { // Exist to checkout
git.checkout().setName(branchName).call();
} else { // Not exist to checkout & create local branch
git.checkout().setCreateBranch(true).setName(branchName).setStartPoint("origin/" + branchName)
.setForceRefUpdate(true).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM).call();
}
PullCommand pullCommand2 = git.pull().setTagOpt(TagOpt.FETCH_TAGS);
pullCommand2.setCredentialsProvider(usernamePasswordCredentialsProvider);
pullCommand2.call();
System.out.println("success");
}
@Test
public void checkoutTag() throws IOException, GitAPIException {
String branchName = "tag2";
Git git = Git.open(new File(gitPath));
FetchCommand fetchCommand = git.fetch().setTagOpt(TagOpt.FETCH_TAGS);
fetchCommand.setCredentialsProvider(usernamePasswordCredentialsProvider);
fetchCommand.call();
/*FetchCommand fetch = git.fetch();
fetch.setCredentialsProvider(usernamePasswordCredentialsProvider);
fetch.call();*/
git.checkout().setName(branchName).call();
System.out.println("success");
}
/**
* Updates the given {@link Project}. Will either pull the latest changes or clone the project's repository if not
* already available.
*
* @param project must not be {@literal null}.
*/
public void update(Project project) {
Assert.notNull(project, "Project must not be null!");
logger.log(project, "Updating project…");
GitProject gitProject = new GitProject(project, server);
String repositoryName = gitProject.getRepositoryName();
doWithGit(project, git -> {
if (workspace.hasProjectDirectory(project)) {
logger.log(project, "Found existing repository %s. Obtaining latest changes…", repositoryName);
checkout(project, Branch.MASTER);
logger.log(project, "git fetch --tags");
git.fetch().setTagOpt(TagOpt.FETCH_TAGS).call();
} else {
clone(project);
}
});
logger.log(project, "Project update done!");
}