Search in sources :

Example 31 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class Schema_123 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    ListMultimap<Account.Id, Change.Id> imports = MultimapBuilder.hashKeys().arrayListValues().build();
    try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT account_id, change_id FROM starred_changes")) {
        while (rs.next()) {
            Account.Id accountId = new Account.Id(rs.getInt(1));
            Change.Id changeId = new Change.Id(rs.getInt(2));
            imports.put(accountId, changeId);
        }
    }
    if (imports.isEmpty()) {
        return;
    }
    try (Repository git = repoManager.openRepository(allUsersName);
        RevWalk rw = new RevWalk(git)) {
        BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
        ObjectId id = StarredChangesUtil.writeLabels(git, StarredChangesUtil.DEFAULT_LABELS);
        for (Map.Entry<Account.Id, Change.Id> e : imports.entries()) {
            bru.addCommand(new ReceiveCommand(ObjectId.zeroId(), id, RefNames.refsStarredChanges(e.getValue(), e.getKey())));
        }
        bru.execute(rw, new TextProgressMonitor());
    } catch (IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ObjectId(org.eclipse.jgit.lib.ObjectId) Statement(java.sql.Statement) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ResultSet(java.sql.ResultSet) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 32 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class Schema_148 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    try (Repository repo = repoManager.openRepository(allUsersName);
        RevWalk rw = new RevWalk(repo);
        ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId rev = ExternalIdReader.readRevision(repo);
        NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
        boolean dirty = false;
        for (Note note : noteMap) {
            byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(ExternalIdReader.MAX_NOTE_SZ);
            try {
                ExternalId extId = ExternalId.parse(note.getName(), raw);
                if (needsUpdate(extId)) {
                    ExternalIdsUpdate.upsert(rw, ins, noteMap, extId);
                    dirty = true;
                }
            } catch (ConfigInvalidException e) {
                ui.message(String.format("Warning: Ignoring invalid external ID note %s", note.getName()));
            }
        }
        if (dirty) {
            ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, serverUser, serverUser);
        }
    } catch (IOException e) {
        throw new OrmException("Failed to update external IDs", e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) Note(org.eclipse.jgit.notes.Note) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) NoteMap(org.eclipse.jgit.notes.NoteMap) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 33 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gitiles by GerritCodeReview.

the class DescribeServlet method describe.

private String describe(Repository repo, GitilesView view, HttpServletRequest req, HttpServletResponse res) throws IOException {
    if (!getBooleanParam(view, CONTAINS_PARAM)) {
        res.setStatus(SC_BAD_REQUEST);
        return null;
    }
    ObjectId id = resolve(repo, view, req, res);
    if (id == null) {
        return null;
    }
    String name;
    try (Git git = new Git(repo)) {
        NameRevCommand cmd = nameRevCommand(git, id, req, res);
        if (cmd == null) {
            return null;
        }
        name = cmd.call().get(id);
    } catch (GitAPIException e) {
        throw new IOException(e);
    }
    if (name == null) {
        res.setStatus(SC_NOT_FOUND);
        return null;
    }
    return name;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) ObjectId(org.eclipse.jgit.lib.ObjectId) NameRevCommand(org.eclipse.jgit.api.NameRevCommand) IOException(java.io.IOException)

Example 34 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class UploadArchive method runImpl.

@Override
protected void runImpl() throws IOException, PermissionBackendException, Failure {
    PacketLineOut packetOut = new PacketLineOut(out);
    packetOut.setFlushOnEnd(true);
    packetOut.writeString("ACK");
    packetOut.end();
    try {
        // Parse Git arguments
        readArguments();
        ArchiveFormat f = allowedFormats.getExtensions().get("." + options.format);
        if (f == null) {
            throw new Failure(3, "fatal: upload-archive not permitted");
        }
        // Find out the object to get from the specified reference and paths
        ObjectId treeId = repo.resolve(options.treeIsh);
        if (treeId == null) {
            throw new Failure(4, "fatal: reference not found");
        }
        // Verify the user has permissions to read the specified tree.
        if (!canRead(treeId)) {
            throw new Failure(5, "fatal: cannot perform upload-archive operation");
        }
        // The archive is sent in DATA sideband channel
        try (SideBandOutputStream sidebandOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA, SideBandOutputStream.MAX_BUF, out)) {
            new ArchiveCommand(repo).setFormat(f.name()).setFormatOptions(getFormatOptions(f)).setTree(treeId).setPaths(options.path.toArray(new String[0])).setPrefix(options.prefix).setOutputStream(sidebandOut).call();
            sidebandOut.flush();
        } catch (GitAPIException e) {
            throw new Failure(7, "fatal: git api exception, " + e);
        }
    } catch (Failure f) {
        // Report the error in ERROR sideband channel
        try (SideBandOutputStream sidebandError = new SideBandOutputStream(SideBandOutputStream.CH_ERROR, SideBandOutputStream.MAX_BUF, out)) {
            sidebandError.write(f.getMessage().getBytes(UTF_8));
            sidebandError.flush();
        }
        throw f;
    } finally {
        // In any case, cleanly close the packetOut channel
        packetOut.end();
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) PacketLineOut(org.eclipse.jgit.transport.PacketLineOut) ArchiveFormat(com.google.gerrit.server.change.ArchiveFormat) ObjectId(org.eclipse.jgit.lib.ObjectId) SideBandOutputStream(org.eclipse.jgit.transport.SideBandOutputStream) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand)

Example 35 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class AbstractPushForReview method publishCommentsOnPushPublishesDraftsOnMultipleChanges.

@Test
public void publishCommentsOnPushPublishesDraftsOnMultipleChanges() throws Exception {
    ObjectId initialHead = testRepo.getRepository().resolve("HEAD");
    List<RevCommit> commits = createChanges(2, "refs/for/master");
    String id1 = byCommit(commits.get(0)).change().getKey().get();
    String id2 = byCommit(commits.get(1)).change().getKey().get();
    CommentInfo c1 = addDraft(id1, commits.get(0).name(), newDraft(FILE_NAME, 1, "comment1"));
    CommentInfo c2 = addDraft(id2, commits.get(1).name(), newDraft(FILE_NAME, 1, "comment2"));
    assertThat(getPublishedComments(id1)).isEmpty();
    assertThat(getPublishedComments(id2)).isEmpty();
    amendChanges(initialHead, commits, "refs/for/master%publish-comments");
    Collection<CommentInfo> cs1 = getPublishedComments(id1);
    assertThat(cs1.stream().map(c -> c.message)).containsExactly("comment1");
    assertThat(cs1.stream().map(c -> c.id)).containsExactly(c1.id);
    assertThat(getLastMessage(id1)).isEqualTo("Uploaded patch set 2: Commit message was updated.\n\n(1 comment)");
    Collection<CommentInfo> cs2 = getPublishedComments(id2);
    assertThat(cs2.stream().map(c -> c.message)).containsExactly("comment2");
    assertThat(cs2.stream().map(c -> c.id)).containsExactly(c2.id);
    assertThat(getLastMessage(id2)).isEqualTo("Uploaded patch set 2: Commit message was updated.\n\n(1 comment)");
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommentInfo(com.google.gerrit.extensions.common.CommentInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

ObjectId (org.eclipse.jgit.lib.ObjectId)357 Test (org.junit.Test)128 RevCommit (org.eclipse.jgit.revwalk.RevCommit)125 RevWalk (org.eclipse.jgit.revwalk.RevWalk)86 IOException (java.io.IOException)63 Repository (org.eclipse.jgit.lib.Repository)63 Change (com.google.gerrit.reviewdb.client.Change)41 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)40 ArrayList (java.util.ArrayList)36 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)34 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)34 Ref (org.eclipse.jgit.lib.Ref)34 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)33 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)26 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)23 NoteMap (org.eclipse.jgit.notes.NoteMap)22 Map (java.util.Map)21 OrmException (com.google.gwtorm.server.OrmException)20 ObjectReader (org.eclipse.jgit.lib.ObjectReader)20