Example usage for org.eclipse.jgit.lib RefUpdate setRefLogMessage

List of usage examples for org.eclipse.jgit.lib RefUpdate setRefLogMessage

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib RefUpdate setRefLogMessage.

Prototype

public void setRefLogMessage(String msg, boolean appendStatus) 

Source Link

Document

Set the message to include in the reflog.

Usage

From source file:org.kuali.student.git.importer.FixImportRepo.java

License:Educational Community License

private static void deleteRef(Repository repo, String branchName, String revision) throws IOException {

    RefUpdate u = repo.updateRef(branchName, true);

    u.setForceUpdate(true);/*from   ww w  .ja v  a2 s. c o m*/

    String resultMessage = "deleting branch " + branchName + " at revision " + revision;

    u.setRefLogMessage(resultMessage, true);

    Result result = u.delete();

    log.info(resultMessage + " result = " + result);

}

From source file:org.kuali.student.git.model.ref.utils.GitRefUtils.java

License:Educational Community License

public static Ref createBranch(Repository repo, String absoluteBranchName, ObjectId commitId,
        boolean allowUpdate) throws BranchRefExistsException, IOException {

    if (!allowUpdate && repo.getRef(absoluteBranchName) != null)
        throw new BranchRefExistsException(absoluteBranchName + "already exists");

    RefUpdate update = repo.updateRef(absoluteBranchName);

    update.setNewObjectId(commitId);//from  www .j  a v  a  2  s  . c  o m
    update.setRefLogMessage("created new branch " + absoluteBranchName, true);

    Result result = update.forceUpdate();

    if (result.equals(Result.LOCK_FAILURE)) {
        log.warn("lockfailure updating " + absoluteBranchName + " to commitId = " + commitId);
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            //fall through
        }
        return createBranch(repo, absoluteBranchName, commitId, allowUpdate);
    }

    if (result == null || !(result.equals(Result.NEW) || result.equals(Result.FORCED)
            || result.equals(Result.FAST_FORWARD))) {
        throw new RuntimeException("failed to create new branch: " + absoluteBranchName);
    }

    Ref ref = repo.getRef(absoluteBranchName);

    return ref;
}

From source file:org.kuali.student.git.model.ref.utils.GitRefUtils.java

License:Educational Community License

public static Result createTagReference(Repository repo, String simpleTagName, ObjectId tagId)
        throws IOException {

    String refName = Constants.R_TAGS + simpleTagName;
    RefUpdate tagRef = repo.updateRef(refName);
    tagRef.setNewObjectId(tagId);/*from w w  w .j a v a  2s . c  o m*/
    tagRef.setForceUpdate(true);
    tagRef.setRefLogMessage("tagged " + simpleTagName, false);
    Result updateResult = tagRef.forceUpdate();

    return updateResult;

}

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

License:Apache License

/**
 * Create an orphaned branch in a repository.
 * /* w w w  .  j a  v a 2s  .c  o  m*/
 * @param repository
 * @param branchName
 * @param author
 *            if unspecified, Moxie will be the author of this new branch
 * @return true if successful
 */
public static boolean createOrphanBranch(Repository repository, String branchName, PersonIdent author) {
    boolean success = false;
    String message = "Created branch " + branchName;
    if (author == null) {
        author = new PersonIdent("Moxie", "moxie@localhost");
    }
    try {
        ObjectInserter odi = repository.newObjectInserter();
        try {
            // Create a blob object to insert into a tree
            ObjectId blobId = odi.insert(Constants.OBJ_BLOB, message.getBytes(Constants.CHARACTER_ENCODING));

            // Create a tree object to reference from a commit
            TreeFormatter tree = new TreeFormatter();
            tree.append("NEWBRANCH", FileMode.REGULAR_FILE, blobId);
            ObjectId treeId = odi.insert(tree);

            // Create a commit object
            CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage(message);
            commit.setTreeId(treeId);

            // Insert the commit into the repository
            ObjectId commitId = odi.insert(commit);
            odi.flush();

            RevWalk revWalk = new RevWalk(repository);
            try {
                RevCommit revCommit = revWalk.parseCommit(commitId);
                if (!branchName.startsWith("refs/")) {
                    branchName = "refs/heads/" + branchName;
                }
                RefUpdate ru = repository.updateRef(branchName);
                ru.setNewObjectId(commitId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    success = true;
                    break;
                default:
                    success = false;
                }
            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return success;
}

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

License:Apache License

public static void updateGhPages(File repositoryFolder, File sourceFolder, boolean obliterate) {
    String ghpages = "refs/heads/gh-pages";
    try {/*w w w .  j  a v  a  2  s. co  m*/
        File gitDir = FileKey.resolve(repositoryFolder, FS.DETECTED);
        Repository repository = new FileRepository(gitDir);

        ObjectId objectId = repository.resolve(ghpages);
        if (objectId == null) {
            JGitUtils.createOrphanBranch(repository, "gh-pages", null);
        }

        System.out.println("Updating gh-pages branch...");
        ObjectId headId = repository.resolve(ghpages + "^{commit}");
        ObjectInserter odi = repository.newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            DirCache index = createIndex(repository, headId, sourceFolder, obliterate);
            ObjectId indexTreeId = index.writeTree(odi);

            // Create a commit object
            PersonIdent author = new PersonIdent("Moxie", "moxie@localhost");
            CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage("updated pages");
            commit.setParentId(headId);
            commit.setTreeId(indexTreeId);

            // Insert the commit into the repository
            ObjectId commitId = odi.insert(commit);
            odi.flush();

            RevWalk revWalk = new RevWalk(repository);
            try {
                RevCommit revCommit = revWalk.parseCommit(commitId);
                RefUpdate ru = repository.updateRef(ghpages);
                ru.setNewObjectId(commitId);
                ru.setExpectedOldObjectId(headId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            ghpages, commitId.toString(), rc));
                }
            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
        System.out.println("gh-pages updated.");
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:org.nbgit.client.CommitBuilder.java

License:Open Source License

private boolean updateRef(RefUpdate ru, ObjectId id) throws IOException {
    ru.setNewObjectId(id);/*from   w w  w .ja  v a2 s.  co  m*/
    ru.setRefLogMessage(buildReflogMessage(), false);
    ru.update();
    return ru.getOldObjectId() != null ? ru.getResult() == RefUpdate.Result.FAST_FORWARD
            : ru.getResult() == RefUpdate.Result.NEW;
}

From source file:org.test.RewriteGitHistory.java

License:Apache License

/**
 * Apply the commit on the current branch and update the head pointer.
 * //from  w w  w  .ja v a  2  s . c o m
 * @param commitBuilder
 * @throws IOException
 * @throws NoHeadException
 */
protected ObjectId executeCommit(CommitBuilder commitBuilder) throws NoHeadException, IOException {

    ObjectInserter inserter = repository.getObjectDatabase().newInserter();

    ObjectId newBaseId = null;

    try {
        newBaseId = inserter.insert(commitBuilder);

        inserter.flush();

        Ref head = repository.getRef(Constants.HEAD);

        if (head == null)
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);

        // determine the current HEAD and the commit it is referring to
        ObjectId headId = repository.resolve(Constants.HEAD + "^{commit}");
        RevWalk revWalk = new RevWalk(repository);
        try {
            RevCommit newCommit = revWalk.parseCommit(newBaseId);
            RefUpdate ru = repository.updateRef(Constants.HEAD);
            ru.setNewObjectId(newBaseId);
            ru.setRefLogMessage("commit : " + newCommit.getShortMessage(), false);

            ru.setExpectedOldObjectId(headId);
            Result rc = ru.update();

            log.info("rc.type = " + rc.name());

        } finally {
            revWalk.release();
        }

    } finally {
        inserter.release();
    }

    return newBaseId;

}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.RemoveRemote.java

License:Apache License

public void execute() {
    try {//from  w w w .  j  av  a  2 s  .  c  om
        // AF-1715: Cleaning origin to prevent errors while importing the new generated repo.
        git.getRepository().getConfig().unsetSection("remote", remote);
        git.getRepository().getConfig().save();
        RefUpdate updateRef = git.getRepository().updateRef(ref, false);
        updateRef.setRefLogMessage(ref + " packed-ref deleted", false);
        updateRef.setForceUpdate(true);
        updateRef.delete();
    } catch (Exception e) {
        throw new GitException("Error when trying to remove remote", e);
    }
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.SimpleRefUpdateCommand.java

License:Apache License

public void execute() throws IOException, ConcurrentRefUpdateException {
    final ObjectId headId = git.getLastCommit(Constants.R_HEADS + name);
    final RefUpdate ru = git.getRepository().updateRef(Constants.R_HEADS + name);
    if (headId == null) {
        ru.setExpectedOldObjectId(ObjectId.zeroId());
    } else {/* w  ww .  j a  va2s .c  o m*/
        ru.setExpectedOldObjectId(headId);
    }
    ru.setNewObjectId(commit.getId());
    ru.setRefLogMessage(commit.getShortMessage(), false);
    forceUpdate(ru, commit.getId());
}

From source file:org.uberfire.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static boolean commit(final Git git, final String branchName, final CommitInfo commitInfo,
        final boolean amend, final ObjectId _originId, final CommitContent content) {
    boolean hadEffecitiveCommit = true;
    final PersonIdent author = buildPersonIdent(git, commitInfo.getName(), commitInfo.getEmail(),
            commitInfo.getTimeZone(), commitInfo.getWhen());

    try {/*  w  ww .j a v a 2 s .co m*/
        final ObjectInserter odi = git.getRepository().newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");

            final ObjectId originId;
            if (_originId == null) {
                originId = git.getRepository().resolve(branchName + "^{commit}");
            } else {
                originId = _originId;
            }

            final DirCache index;
            if (content instanceof DefaultCommitContent) {
                index = createTemporaryIndex(git, originId, (DefaultCommitContent) content);
            } else if (content instanceof MoveCommitContent) {
                index = createTemporaryIndex(git, originId, (MoveCommitContent) content);
            } else if (content instanceof CopyCommitContent) {
                index = createTemporaryIndex(git, originId, (CopyCommitContent) content);
            } else if (content instanceof RevertCommitContent) {
                index = createTemporaryIndex(git, originId);
            } else {
                index = null;
            }

            if (index != null) {
                final ObjectId indexTreeId = index.writeTree(odi);

                // Create a commit object
                final CommitBuilder commit = new CommitBuilder();
                commit.setAuthor(author);
                commit.setCommitter(author);
                commit.setEncoding(Constants.CHARACTER_ENCODING);
                commit.setMessage(commitInfo.getMessage());
                //headId can be null if the repository has no commit yet
                if (headId != null) {
                    if (amend) {
                        final List<ObjectId> parents = new LinkedList<ObjectId>();
                        final RevCommit previousCommit = new RevWalk(git.getRepository()).parseCommit(headId);
                        final RevCommit[] p = previousCommit.getParents();
                        for (final RevCommit revCommit : p) {
                            parents.add(0, revCommit.getId());
                        }
                        commit.setParentIds(parents);
                    } else {
                        commit.setParentId(headId);
                    }
                }
                commit.setTreeId(indexTreeId);

                // Insert the commit into the repository
                final ObjectId commitId = odi.insert(commit);
                odi.flush();

                final RevWalk revWalk = new RevWalk(git.getRepository());
                try {
                    final RevCommit revCommit = revWalk.parseCommit(commitId);
                    final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName);
                    if (headId == null) {
                        ru.setExpectedOldObjectId(ObjectId.zeroId());
                    } else {
                        ru.setExpectedOldObjectId(headId);
                    }
                    ru.setNewObjectId(commitId);
                    ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                    final RefUpdate.Result rc = ru.forceUpdate();
                    switch (rc) {
                    case NEW:
                    case FORCED:
                    case FAST_FORWARD:
                        break;
                    case REJECTED:
                    case LOCK_FAILURE:
                        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(),
                                rc);
                    default:
                        throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                                Constants.HEAD, commitId.toString(), rc));
                    }

                } finally {
                    revWalk.release();
                }
            } else {
                hadEffecitiveCommit = false;
            }
        } finally {
            odi.release();
        }
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
    return hadEffecitiveCommit;
}