Example usage for org.eclipse.jgit.api TagCommand setName

List of usage examples for org.eclipse.jgit.api TagCommand setName

Introduction

In this page you can find the example usage for org.eclipse.jgit.api TagCommand setName.

Prototype

public TagCommand setName(String name) 

Source Link

Document

Set the tag name.

Usage

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * creates a tag in a repository/*from  w w w .j  a  v a2  s.c o m*/
 *
 * @param repository
 * @param objectId, the ref the tag points towards
 * @param tagger, the person tagging the object
 * @param tag, the string label
 * @param message, the string message
 * @return boolean, true if operation was successful, otherwise false
 */
public static boolean createTag(Repository repository, String objectId, PersonIdent tagger, String tag,
        String message) {
    try {
        Git gitClient = Git.open(repository.getDirectory());
        TagCommand tagCommand = gitClient.tag();
        tagCommand.setTagger(tagger);
        tagCommand.setMessage(message);
        if (objectId != null) {
            RevObject revObj = getCommit(repository, objectId);
            tagCommand.setObjectId(revObj);
        }
        tagCommand.setName(tag);
        Ref call = tagCommand.call();
        return call != null ? true : false;
    } catch (Exception e) {
        error(e, repository, "Failed to create tag {1} in repository {0}", objectId, tag);
    }
    return false;
}

From source file:com.microsoft.gittf.core.util.TagUtil.java

License:Open Source License

/**
 * Creates a tag//from   www.  j  a  va 2  s  . co m
 * 
 * @param repository
 *        the git repository
 * @param commitID
 *        the commit id to tag
 * @param tagName
 *        the tag name
 * @param tagOwner
 *        the tag owner
 * @return
 */
public static boolean createTag(final Repository repository, ObjectId commitID, String tagName,
        PersonIdent tagOwner) {
    final RevWalk walker = new RevWalk(repository);
    try {
        /* Look up the object to tag */
        RevObject objectToTag = walker.lookupCommit(commitID);

        /* Create a tag command */
        TagCommand tagCommand = new Git(repository).tag();
        tagCommand.setName(tagName);
        tagCommand.setObjectId(objectToTag);
        tagCommand.setForceUpdate(true);
        tagCommand.setTagger(tagOwner);

        /* Call the tag command */
        Ref tagRef = tagCommand.call();

        if (tagRef == null || tagRef == ObjectId.zeroId()) {
            log.warn("Failed to tag commit."); //$NON-NLS-1$

            return false;
        }

        return true;
    } catch (Exception e) {
        // this is not a critical failure so we can still continue with the
        // operation even if tagging failed.

        log.error(e);

        return false;
    } finally {
        if (walker != null) {
            walker.release();
        }
    }
}

From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java

License:Open Source License

@Override
public void tag(final LocalRepoBean repoBean, final String tag) throws GitException {
    try {/*from w  w w.ja  v  a  2  s. c o m*/
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(repoBean.getGitConfigDirectory().toFile()).findGitDir()
                .build();
        Git git = new org.eclipse.jgit.api.Git(repository);
        TagCommand tagCommand = git.tag();
        tagCommand.setName(tag);
        tagCommand.call();
        repository.close();
    } catch (GitAPIException | IOException e) {
        LOGGER.error(e);
        throw new GitException(e);
    }
}

From source file:com.worldline.easycukes.scm.utils.GitHelper.java

License:Open Source License

/**
 * Create a new tag in the local git repository
 * (git checkout tagname) and finally pushes new branch on the remote repository (git push)
 *
 * @param directory the directory in which the local git repository is located
 * @param username  the username to be used while pushing
 * @param password  the password matching with the provided username to be used
 *                  for authentication/*w ww .  j  av  a2s  . c  o m*/
 * @param message   the commit message to be used    
 */
public static void createTag(@NonNull File directory, String tagName, String username, String password,
        String message) {

    try {
        final Git git = Git.open(directory);

        final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider(
                username, password);

        TagCommand tagCommand = git.tag();
        tagCommand.setName(tagName);
        tagCommand.setMessage(message);
        tagCommand.call();
        log.info("Tag created");

        // and then commit
        final PersonIdent author = new PersonIdent(username, "");
        git.commit().setCommitter(author).setMessage(message).setAuthor(author).call();
        log.info(message);

        git.push().setCredentialsProvider(userCredential).call();
        log.info("Pushed the changes in remote Git repository...");
    } catch (final GitAPIException | IOException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.ajoberstar.gradle.git.tasks.GitTag.java

License:Apache License

/**
 * Tags the HEAD./*from   ww  w  .j  a  va  2s .  c o  m*/
 */
@TaskAction
public void tag() {
    TagCommand cmd = getGit().tag();
    cmd.setName(getTagName());
    cmd.setMessage(getMessage());
    cmd.setSigned(getSign());
    cmd.setForceUpdate(getForce());
    if (tagger != null) {
        cmd.setTagger(getTagger());
    }

    try {
        cmd.call();
    } catch (ConcurrentRefUpdateException e) {
        throw new GradleException("Another process is accessing or updating the ref.", e);
    } catch (InvalidTagNameException e) {
        throw new GradleException("Invalid tag name: " + getTagName(), e);
    } catch (NoHeadException e) {
        throw new GradleException("Cannot tag without a HEAD revision.", e);
    } catch (GitAPIException e) {
        throw new GradleException("Problem with tag.", e);
    }
}

From source file:org.moxie.utils.JGitUtils.java

License:Apache License

public static String commitFiles(File dir, List<String> files, String message, String tagName,
        String tagMessage) throws IOException, GitAPIException {
    Git git = Git.open(dir);//from w  ww  .ja  va2s  .  co m
    AddCommand add = git.add();
    for (String file : files) {
        add.addFilepattern(file);
    }
    add.call();

    // execute the commit
    CommitCommand commit = git.commit();
    commit.setMessage(message);
    RevCommit revCommit = commit.call();

    if (!StringUtils.isEmpty(tagName) && !StringUtils.isEmpty(tagMessage)) {
        // tag the commit
        TagCommand tagCommand = git.tag();
        tagCommand.setName(tagName);
        tagCommand.setMessage(tagMessage);
        tagCommand.setForceUpdate(true);
        tagCommand.setObjectId(revCommit);
        tagCommand.call();
    }
    git.getRepository().close();
    return revCommit.getId().getName();
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public TagRef tagRepo(String tagName) {
    try {//from   w  w w  .  j  av  a  2 s . c  o  m
        if (repository == null) {
            initRepository();
        }
        TagCommand tag = new Git(repository).tag();
        LOGGER.debug("Tagging HEAD with name '{}'", tagName);
        return new GitTagRef(tag.setName(tagName).call());
    } catch (Exception e) {
        throw new ScmException(e);
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public TagRef tagRepo(String tagName, CommitRef ref) {
    try {//  w ww  . j  a va2  s .  c o  m
        if (repository == null) {
            initRepository();
        }
        AnyObjectId commitRef = repository.resolve(ref.getStringRepresentation());
        if (commitRef == null) {
            LOGGER.debug("Couldnt resolve reference {} in repository", ref.getStringRepresentation());
            return null;
        }
        RevWalk walk = new RevWalk(repository);
        RevCommit revCommit = walk.parseCommit(commitRef);
        TagCommand tag = new Git(repository).tag();
        tag.setName(tagName).setObjectId(revCommit);
        LOGGER.debug("Tagging revision {} with name '{}'", ref.getStringRepresentation(), tagName);
        return new GitTagRef(tag.call());
    } catch (Exception e) {
        throw new ScmException(e);
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImplTest.java

License:Apache License

@Test
public void getCommitRefForTagRef_shouldReturnTaggedCommitRef() throws Exception {
    localRepository = RepositoryFixture.createRepository(localDirectory);
    RevWalk walk = new RevWalk(localRepository);
    RevCommit head = walk.lookupCommit(localRepository.resolve(Constants.HEAD));
    String tagName = "newTag";
    TagCommand tagCommand = new Git(localRepository).tag();
    TagRef tag = new GitTagRef(tagCommand.setName(tagName).call());
    assertThat(tag, notNullValue());/*from  w ww . j a  v  a 2 s.  c  o  m*/
    assertThat(tagName, is(tag.getTagName()));
    CommitRef commitRef = service.getCommitRefForTag(tag);
    assertThat(head.name(), is(commitRef.getStringRepresentation()));
}

From source file:org.webcat.core.git.GitRepository.java

License:Open Source License

public GitRef createTagForObject(String name, ObjectId objectId) {
    TagCommand tag = new Git(repository).tag();
    tag.setName(name);

    if (objectId == null) {
        tag.setObjectId(null);//from w w w .  j  av  a2  s. co  m
    } else {
        RevWalk revWalk = new RevWalk(repository);

        try {
            RevCommit commit = revWalk.parseCommit(objectId);
            tag.setObjectId(commit);
        } catch (Exception e) {
            return null;
        } finally {
            revWalk.release();
        }
    }

    try {
        tag.call();
        return refWithName(Constants.R_TAGS + name);
    } catch (Exception e) {
        return null;
    }
}