Example usage for org.eclipse.jgit.lib ObjectId fromString

List of usage examples for org.eclipse.jgit.lib ObjectId fromString

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectId fromString.

Prototype

public static ObjectId fromString(String str) 

Source Link

Document

Convert an ObjectId from hex characters.

Usage

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

License:Apache License

@Test
public void patchSetObjectAndRefMissing() throws Exception {
    Change c = insertChange();//from w  ww.  j  a v  a  2 s  . co m
    PatchSet ps = newPatchSet(c.currentPatchSetId(),
            ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"), userId);
    db.patchSets().insert(singleton(ps));

    assertProblems(c, "Ref missing: " + ps.getId().toRefName(),
            "Object missing: patch set 1: deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
}

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

License:Apache License

@Test
public void patchSetObjectAndRefMissingWithFix() throws Exception {
    Change c = insertChange();//from w w  w  .  j  av a  2 s . c  om
    PatchSet ps = newPatchSet(c.currentPatchSetId(),
            ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"), userId);
    db.patchSets().insert(singleton(ps));

    String refName = ps.getId().toRefName();
    List<ProblemInfo> problems = checker.check(c, new FixInput()).problems();
    ProblemInfo p = problems.get(0);
    assertThat(p.message).isEqualTo("Ref missing: " + refName);
    assertThat(p.status).isNull();
}

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

License:Apache License

@Test
public void patchSetRefMissing() throws Exception {
    Change c = insertChange();//from  w  w w  .  j  av  a2  s. com
    PatchSet ps = insertPatchSet(c);
    String refName = ps.getId().toRefName();
    repo.update("refs/other/foo", ObjectId.fromString(ps.getRevision().get()));
    deleteRef(refName);

    assertProblems(c, "Ref missing: " + refName);
}

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

License:Apache License

@Test
public void patchSetRefMissingWithFix() throws Exception {
    Change c = insertChange();//from  w w  w  .  j  a  v  a 2 s  .  c o m
    PatchSet ps = insertPatchSet(c);
    String refName = ps.getId().toRefName();
    repo.update("refs/other/foo", ObjectId.fromString(ps.getRevision().get()));
    deleteRef(refName);

    List<ProblemInfo> problems = checker.check(c, new FixInput()).problems();
    ProblemInfo p = problems.get(0);
    assertThat(p.message).isEqualTo("Ref missing: " + refName);
    assertThat(p.status).isEqualTo(ProblemInfo.Status.FIXED);
    assertThat(p.outcome).isEqualTo("Repaired patch set ref");

    assertThat(repo.getRepository().getRef(refName).getObjectId().name()).isEqualTo(ps.getRevision().get());
}

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

License:Apache License

private PatchSet insertMissingPatchSet(Change c, String id) throws Exception {
    PatchSet ps = newPatchSet(c.currentPatchSetId(), ObjectId.fromString(id), userId);
    db.patchSets().insert(singleton(ps));
    return ps;//  w ww  . j  a va  2 s .  co m
}

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

License:Apache License

private void updatePatchSetRef(PatchSet ps) throws Exception {
    repo.update(ps.getId().toRefName(), ObjectId.fromString(ps.getRevision().get()));
}

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

License:Apache License

@Override
public Response<ChangeInfo> apply(TopLevelResource parent, ChangeInfo input) throws AuthException, OrmException,
        BadRequestException, UnprocessableEntityException, IOException, InvalidChangeOperationException,
        ResourceNotFoundException, MethodNotAllowedException, ResourceConflictException {
    if (Strings.isNullOrEmpty(input.project)) {
        throw new BadRequestException("project must be non-empty");
    }/*from  www .  j a v a 2 s . c o  m*/

    if (Strings.isNullOrEmpty(input.branch)) {
        throw new BadRequestException("branch must be non-empty");
    }

    if (Strings.isNullOrEmpty(input.subject)) {
        throw new BadRequestException("commit message must be non-empty");
    }

    if (input.status != null) {
        if (input.status != ChangeStatus.NEW && input.status != ChangeStatus.DRAFT) {
            throw new BadRequestException("unsupported change status");
        }

        if (!allowDrafts && input.status == ChangeStatus.DRAFT) {
            throw new MethodNotAllowedException("draft workflow is disabled");
        }
    }

    String refName = RefNames.fullName(input.branch);
    ProjectResource rsrc = projectsCollection.parse(input.project);

    Capable r = rsrc.getControl().canPushToAtLeastOneRef();
    if (r != Capable.OK) {
        throw new AuthException(r.getMessage());
    }

    RefControl refControl = rsrc.getControl().controlForRef(refName);
    if (!refControl.canUpload() || !refControl.canRead()) {
        throw new AuthException("cannot upload review");
    }

    Project.NameKey project = rsrc.getNameKey();
    try (Repository git = gitManager.openRepository(project); RevWalk rw = new RevWalk(git)) {
        ObjectId parentCommit;
        List<String> groups;
        if (input.baseChange != null) {
            List<Change> changes = changeUtil.findChanges(input.baseChange);
            if (changes.size() != 1) {
                throw new InvalidChangeOperationException("Base change not found: " + input.baseChange);
            }
            Change change = Iterables.getOnlyElement(changes);
            if (!rsrc.getControl().controlFor(change).isVisible(db.get())) {
                throw new InvalidChangeOperationException("Base change not found: " + input.baseChange);
            }
            PatchSet ps = db.get().patchSets()
                    .get(new PatchSet.Id(change.getId(), change.currentPatchSetId().get()));
            parentCommit = ObjectId.fromString(ps.getRevision().get());
            groups = ps.getGroups();
        } else {
            Ref destRef = git.getRefDatabase().exactRef(refName);
            if (destRef == null) {
                throw new UnprocessableEntityException(String.format("Branch %s does not exist.", refName));
            }
            parentCommit = destRef.getObjectId();
            groups = null;
        }
        RevCommit mergeTip = rw.parseCommit(parentCommit);

        Timestamp now = TimeUtil.nowTs();
        IdentifiedUser me = (IdentifiedUser) userProvider.get();
        PersonIdent author = me.newCommitterIdent(now, serverTimeZone);

        ObjectId id = ChangeIdUtil.computeChangeId(mergeTip.getTree(), mergeTip, author, author, input.subject);
        String commitMessage = ChangeIdUtil.insertId(input.subject, id);

        RevCommit c = newCommit(git, rw, author, mergeTip, commitMessage);

        Change change = new Change(getChangeId(id, c), new Change.Id(db.get().nextChangeId()),
                me.getAccountId(), new Branch.NameKey(project, refName), now);

        ChangeInserter ins = changeInserterFactory.create(refControl.getProjectControl(), change, c);

        ChangeMessage msg = new ChangeMessage(
                new ChangeMessage.Key(change.getId(), ChangeUtil.messageUUID(db.get())), me.getAccountId(),
                ins.getPatchSet().getCreatedOn(), ins.getPatchSet().getId());
        msg.setMessage(String.format("Uploaded patch set %s.", ins.getPatchSet().getPatchSetId()));

        ins.setMessage(msg);
        validateCommit(git, refControl, c, me, ins);
        updateRef(git, rw, c, change, ins.getPatchSet());

        String topic = input.topic;
        if (topic != null) {
            topic = Strings.emptyToNull(topic.trim());
        }
        change.setTopic(topic);
        ins.setDraft(input.status != null && input.status == ChangeStatus.DRAFT);
        ins.setGroups(groups);
        ins.insert();

        ChangeJson json = jsonFactory.create(ChangeJson.NO_OPTIONS);
        return Response.created(json.format(change.getId()));
    }
}

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

License:Apache License

@Override
public Response<ChangeInfo> apply(ChangeResource req, MergePatchSetInput in)
        throws OrmException, IOException, InvalidChangeOperationException, RestApiException, UpdateException {
    if (in.merge == null) {
        throw new BadRequestException("merge field is required");
    }//from   w  w w  .  j  a  va 2 s.  c o m

    MergeInput merge = in.merge;
    if (Strings.isNullOrEmpty(merge.source)) {
        throw new BadRequestException("merge.source must be non-empty");
    }

    ChangeControl ctl = req.getControl();
    if (!ctl.isVisible(db.get())) {
        throw new InvalidChangeOperationException("Base change not found: " + req.getId());
    }
    PatchSet ps = psUtil.current(db.get(), ctl.getNotes());
    if (!ctl.canAddPatchSet(db.get())) {
        throw new AuthException("cannot add patch set");
    }

    ProjectControl projectControl = ctl.getProjectControl();
    Change change = ctl.getChange();
    Project.NameKey project = change.getProject();
    Branch.NameKey dest = change.getDest();
    try (Repository git = gitManager.openRepository(project);
            ObjectInserter oi = git.newObjectInserter();
            ObjectReader reader = oi.newReader();
            RevWalk rw = new RevWalk(reader)) {

        RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, merge.source);
        if (!projectControl.canReadCommit(db.get(), git, sourceCommit)) {
            throw new ResourceNotFoundException("cannot find source commit: " + merge.source + " to merge.");
        }

        RevCommit currentPsCommit = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));

        Timestamp now = TimeUtil.nowTs();
        IdentifiedUser me = user.get().asIdentifiedUser();
        PersonIdent author = me.newCommitterIdent(now, serverTimeZone);

        RevCommit newCommit = createMergeCommit(in, projectControl, dest, git, oi, rw, currentPsCommit,
                sourceCommit, author, ObjectId.fromString(change.getKey().get().substring(1)));

        PatchSet.Id nextPsId = ChangeUtil.nextPatchSetId(ps.getId());
        PatchSetInserter psInserter = patchSetInserterFactory.create(ctl, nextPsId, newCommit);
        try (BatchUpdate bu = batchUpdateFactory.create(db.get(), project, me, now)) {
            bu.setRepository(git, rw, oi);
            bu.addOp(ctl.getId(), psInserter.setMessage("Uploaded patch set " + nextPsId.get() + ".")
                    .setDraft(ps.isDraft()).setNotify(NotifyHandling.NONE));
            bu.execute();
        }

        ChangeJson json = jsonFactory.create(ListChangesOption.CURRENT_REVISION);
        return Response.ok(json.format(psInserter.getChange()));
    }
}

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

License:Apache License

private boolean isPatchSetMerged(ChangeContext ctx, PatchSet patchSet) throws IOException {
    Optional<ObjectId> destId = ctx.getRepoView().getRef(ctx.getChange().getDest().get());
    if (!destId.isPresent()) {
        return false;
    }/*from www  .j a v  a 2  s .  c o  m*/

    RevWalk revWalk = ctx.getRevWalk();
    ObjectId objectId = ObjectId.fromString(patchSet.getRevision().get());
    return revWalk.isMergedInto(revWalk.parseCommit(objectId), revWalk.parseCommit(destId.get()));
}

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

License:Apache License

@Override
public BinaryResult apply(FileResource rsrc)
        throws ResourceNotFoundException, IOException, NoSuchChangeException, OrmException {
    String path = rsrc.getPatchKey().get();
    ProjectState projectState = rsrc.getRevision().getControl().getProjectControl().getProjectState();
    ObjectId revstr = ObjectId.fromString(rsrc.getRevision().getPatchSet().getRevision().get());
    return fileContentUtil.downloadContent(projectState, revstr, path, suffix);
}