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

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

Introduction

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

Prototype

public void setRefLogIdent(PersonIdent pi) 

Source Link

Document

Set the identity of the user appearing in the reflog.

Usage

From source file:com.gitblit.git.PatchsetReceivePack.java

License:Apache License

private RefUpdate updateRef(String ref, ObjectId newId, PatchsetType type) {
    ObjectId ticketRefId = ObjectId.zeroId();
    try {/*ww  w. ja va2  s .  c o  m*/
        ticketRefId = getRepository().resolve(ref);
    } catch (Exception e) {
        // ignore
    }

    try {
        RefUpdate ru = getRepository().updateRef(ref, false);
        ru.setRefLogIdent(getRefLogIdent());
        switch (type) {
        case Amend:
        case Rebase:
        case Rebase_Squash:
        case Squash:
            ru.setForceUpdate(true);
            break;
        default:
            break;
        }

        ru.setExpectedOldObjectId(ticketRefId);
        ru.setNewObjectId(newId);
        RefUpdate.Result result = ru.update(getRevWalk());
        if (result == RefUpdate.Result.LOCK_FAILURE) {
            sendError("Failed to obtain lock when updating {0}:{1}", repository.name, ref);
            sendError("Perhaps an administrator should remove {0}/{1}.lock?", getRepository().getDirectory(),
                    ref);
            return null;
        }
        return ru;
    } catch (IOException e) {
        LOGGER.error("failed to update ref " + ref, e);
        sendError("There was an error updating ref {0}:{1}", repository.name, ref);
    }
    return null;
}

From source file:com.google.gerrit.gpg.PublicKeyStore.java

License:Apache License

/**
 * Save pending keys to the store.//  w ww. j a v a  2 s .  c  o m
 * <p>
 * One commit is created and the ref updated. The pending list is cleared if
 * and only if the ref update succeeds, which allows for easy retries in case
 * of lock failure.
 *
 * @param cb commit builder with at least author and identity populated; tree
 *     and parent are ignored.
 * @return result of the ref update.
 */
public RefUpdate.Result save(CommitBuilder cb) throws PGPException, IOException {
    if (toAdd.isEmpty() && toRemove.isEmpty()) {
        return RefUpdate.Result.NO_CHANGE;
    }
    if (reader == null) {
        load();
    }
    if (notes == null) {
        notes = NoteMap.newEmptyMap();
    }
    ObjectId newTip;
    try (ObjectInserter ins = repo.newObjectInserter()) {
        for (PGPPublicKeyRing keyRing : toAdd.values()) {
            saveToNotes(ins, keyRing);
        }
        for (Fingerprint fp : toRemove) {
            deleteFromNotes(ins, fp);
        }
        cb.setTreeId(notes.writeTree(ins));
        if (cb.getTreeId().equals(tip != null ? tip.getTree() : EMPTY_TREE)) {
            return RefUpdate.Result.NO_CHANGE;
        }

        if (tip != null) {
            cb.setParentId(tip);
        }
        if (cb.getMessage() == null) {
            int n = toAdd.size() + toRemove.size();
            cb.setMessage(String.format("Update %d public key%s", n, n != 1 ? "s" : ""));
        }
        newTip = ins.insert(cb);
        ins.flush();
    }

    RefUpdate ru = repo.updateRef(PublicKeyStore.REFS_GPG_KEYS);
    ru.setExpectedOldObjectId(tip);
    ru.setNewObjectId(newTip);
    ru.setRefLogIdent(cb.getCommitter());
    ru.setRefLogMessage("Store public keys", true);
    RefUpdate.Result result = ru.update();
    reset();
    switch (result) {
    case FAST_FORWARD:
    case NEW:
    case NO_CHANGE:
        toAdd.clear();
        toRemove.clear();
        break;
    default:
        break;
    }
    return result;
}

From source file:com.google.gerrit.httpd.rpc.project.AddBranch.java

License:Apache License

@Override
public ListBranchesResult call() throws NoSuchProjectException, InvalidNameException, InvalidRevisionException,
        IOException, BranchCreationNotAllowedException {
    final ProjectControl projectControl = projectControlFactory.controlFor(projectName);

    String refname = branchName;//  w  w w .j  a  v  a2s.  com
    while (refname.startsWith("/")) {
        refname = refname.substring(1);
    }
    if (!refname.startsWith(Constants.R_REFS)) {
        refname = Constants.R_HEADS + refname;
    }
    if (!Repository.isValidRefName(refname)) {
        throw new InvalidNameException();
    }
    if (refname.startsWith(ReceiveCommits.NEW_CHANGE)) {
        throw new BranchCreationNotAllowedException(ReceiveCommits.NEW_CHANGE);
    }

    final Branch.NameKey name = new Branch.NameKey(projectName, refname);
    final RefControl refControl = projectControl.controlForRef(name);
    final Repository repo = repoManager.openRepository(projectName);
    try {
        final ObjectId revid = parseStartingRevision(repo);
        final RevWalk rw = verifyConnected(repo, revid);
        RevObject object = rw.parseAny(revid);

        if (refname.startsWith(Constants.R_HEADS)) {
            // Ensure that what we start the branch from is a commit. If we
            // were given a tag, deference to the commit instead.
            //
            try {
                object = rw.parseCommit(object);
            } catch (IncorrectObjectTypeException notCommit) {
                throw new IllegalStateException(startingRevision + " not a commit");
            }
        }

        if (!refControl.canCreate(rw, object)) {
            throw new IllegalStateException("Cannot create " + refname);
        }

        try {
            final RefUpdate u = repo.updateRef(refname);
            u.setExpectedOldObjectId(ObjectId.zeroId());
            u.setNewObjectId(object.copy());
            u.setRefLogIdent(identifiedUser.newRefLogIdent());
            u.setRefLogMessage("created via web from " + startingRevision, false);
            final RefUpdate.Result result = u.update(rw);
            switch (result) {
            case FAST_FORWARD:
            case NEW:
            case NO_CHANGE:
                replication.scheduleUpdate(name.getParentKey(), refname);
                hooks.doRefUpdatedHook(name, u, identifiedUser.getAccount());
                break;
            default: {
                throw new IOException(result.name());
            }
            }
        } catch (IOException err) {
            log.error("Cannot create branch " + name, err);
            throw err;
        }
    } finally {
        repo.close();
    }

    return listBranchesFactory.create(projectName).call();
}

From source file:com.google.gerrit.pgm.init.AllProjectsConfig.java

License:Apache License

private void updateRef(Repository repo, PersonIdent ident, ObjectId newRevision, String refLogMsg)
        throws IOException {
    RefUpdate ru = repo.updateRef(getRefName());
    ru.setRefLogIdent(ident);
    ru.setNewObjectId(newRevision);/* w w  w  . j  av a 2s .com*/
    ru.setExpectedOldObjectId(revision);
    ru.setRefLogMessage(refLogMsg, false);
    RefUpdate.Result r = ru.update();
    switch (r) {
    case FAST_FORWARD:
    case NEW:
    case NO_CHANGE:
        break;
    default:
        throw new IOException("Failed to update " + getRefName() + " of " + project + ": " + r.name());
    }
}

From source file:com.google.gerrit.pgm.init.api.VersionedMetaDataOnInit.java

License:Apache License

private void updateRef(Repository repo, PersonIdent ident, ObjectId newRevision, String refLogMsg)
        throws IOException {
    RefUpdate ru = repo.updateRef(getRefName());
    ru.setRefLogIdent(ident);
    ru.setNewObjectId(newRevision);//  www  . j  a  va2 s  .  c  o  m
    ru.setExpectedOldObjectId(revision);
    ru.setRefLogMessage(refLogMsg, false);
    RefUpdate.Result r = ru.update();
    switch (r) {
    case FAST_FORWARD:
    case NEW:
    case NO_CHANGE:
        break;
    case FORCED:
    case IO_FAILURE:
    case LOCK_FAILURE:
    case NOT_ATTEMPTED:
    case REJECTED:
    case REJECTED_CURRENT_BRANCH:
    case RENAMED:
    default:
        throw new IOException("Failed to update " + getRefName() + " of " + project + ": " + r.name());
    }
}

From source file:com.google.gerrit.server.account.AccountsUpdate.java

License:Apache License

public static void createUserBranch(Repository repo, ObjectInserter oi, PersonIdent committerIdent,
        PersonIdent authorIdent, Account account) throws IOException {
    ObjectId id = createInitialEmptyCommit(oi, committerIdent, authorIdent, account.getRegisteredOn());

    String refName = RefNames.refsUsers(account.getId());
    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(ObjectId.zeroId());
    ru.setNewObjectId(id);/*  ww w.  jav a  2 s  . c o m*/
    ru.setForceUpdate(true);
    ru.setRefLogIdent(committerIdent);
    ru.setRefLogMessage("Create Account", true);
    Result result = ru.update();
    if (result != Result.NEW) {
        throw new IOException(String.format("Failed to update ref %s: %s", refName, result.name()));
    }
}

From source file:com.google.gerrit.server.account.AccountsUpdate.java

License:Apache License

public static void deleteUserBranch(Repository repo, PersonIdent refLogIdent, Account.Id accountId)
        throws IOException {
    String refName = RefNames.refsUsers(accountId);
    Ref ref = repo.exactRef(refName);
    if (ref == null) {
        return;/*from  ww w  .  j  ava2 s .co  m*/
    }

    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(ref.getObjectId());
    ru.setNewObjectId(ObjectId.zeroId());
    ru.setForceUpdate(true);
    ru.setRefLogIdent(refLogIdent);
    ru.setRefLogMessage("Delete Account", true);
    Result result = ru.delete();
    if (result != Result.FORCED) {
        throw new IOException(String.format("Failed to delete ref %s: %s", refName, result.name()));
    }
}

From source file:com.google.gerrit.server.account.externalids.ExternalIdsUpdate.java

License:Apache License

/** Commits updates to the external IDs. */
public static ObjectId commit(Repository repo, RevWalk rw, ObjectInserter ins, ObjectId rev, NoteMap noteMap,
        String commitMessage, PersonIdent committerIdent, PersonIdent authorIdent) throws IOException {
    CommitBuilder cb = new CommitBuilder();
    cb.setMessage(commitMessage);//w  w w .  ja v a 2  s.  co  m
    cb.setTreeId(noteMap.writeTree(ins));
    cb.setAuthor(authorIdent);
    cb.setCommitter(committerIdent);
    if (!rev.equals(ObjectId.zeroId())) {
        cb.setParentId(rev);
    } else {
        cb.setParentIds(); // Ref is currently nonexistent, commit has no parents.
    }
    if (cb.getTreeId() == null) {
        if (rev.equals(ObjectId.zeroId())) {
            cb.setTreeId(emptyTree(ins)); // No parent, assume empty tree.
        } else {
            RevCommit p = rw.parseCommit(rev);
            cb.setTreeId(p.getTree()); // Copy tree from parent.
        }
    }
    ObjectId commitId = ins.insert(cb);
    ins.flush();

    RefUpdate u = repo.updateRef(RefNames.REFS_EXTERNAL_IDS);
    u.setRefLogIdent(committerIdent);
    u.setRefLogMessage("Update external IDs", false);
    u.setExpectedOldObjectId(rev);
    u.setNewObjectId(commitId);
    RefUpdate.Result res = u.update();
    switch (res) {
    case NEW:
    case FAST_FORWARD:
    case NO_CHANGE:
    case RENAMED:
    case FORCED:
        break;
    case LOCK_FAILURE:
        throw new LockFailureException("Updating external IDs failed with " + res);
    case IO_FAILURE:
    case NOT_ATTEMPTED:
    case REJECTED:
    case REJECTED_CURRENT_BRANCH:
    default:
        throw new IOException("Updating external IDs failed with " + res);
    }
    return rw.parseCommit(commitId);
}

From source file:com.google.gerrit.server.change.ConsistencyChecker.java

License:Apache License

private void fixPatchSetRef(ProblemInfo p, PatchSet ps) {
    try {/*ww w . j av a  2  s. c  o  m*/
        RefUpdate ru = repo.updateRef(ps.getId().toRefName());
        ru.setForceUpdate(true);
        ru.setNewObjectId(ObjectId.fromString(ps.getRevision().get()));
        ru.setRefLogIdent(newRefLogIdent());
        ru.setRefLogMessage("Repair patch set ref", true);
        RefUpdate.Result result = ru.update();
        switch (result) {
        case NEW:
        case FORCED:
        case FAST_FORWARD:
        case NO_CHANGE:
            p.status = Status.FIXED;
            p.outcome = "Repaired patch set ref";
            return;
        default:
            p.status = Status.FIX_FAILED;
            p.outcome = "Failed to update patch set ref: " + result;
            return;
        }
    } catch (IOException e) {
        String msg = "Error fixing patch set ref";
        log.warn(msg + ' ' + ps.getId().toRefName(), e);
        p.status = Status.FIX_FAILED;
        p.outcome = msg;
    }
}

From source file:com.google.gerrit.server.edit.ChangeEditModifier.java

License:Apache License

private RefUpdate.Result update(Repository repo, IdentifiedUser me, String refName, RevWalk rw,
        ObjectId oldObjectId, ObjectId newEdit) throws IOException {
    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(oldObjectId);
    ru.setNewObjectId(newEdit);//w w  w .  j a  v a2  s  .co  m
    ru.setRefLogIdent(getRefLogIdent(me));
    ru.setRefLogMessage("inline edit (amend)", false);
    ru.setForceUpdate(true);
    RefUpdate.Result res = ru.update(rw);
    if (res != RefUpdate.Result.NEW && res != RefUpdate.Result.FORCED) {
        throw new IOException("update failed: " + ru);
    }
    return res;
}