Search in sources :

Example 66 with Repository

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

the class Schema_147 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    try (Repository repo = repoManager.openRepository(allUsersName)) {
        Set<Account.Id> accountIdsFromReviewDb = db.accounts().all().toList().stream().map(a -> a.getId()).collect(toSet());
        Set<Account.Id> accountIdsFromUserBranches = repo.getRefDatabase().getRefs(RefNames.REFS_USERS).values().stream().map(r -> Account.Id.fromRef(r.getName())).filter(Objects::nonNull).collect(toSet());
        accountIdsFromUserBranches.removeAll(accountIdsFromReviewDb);
        for (Account.Id accountId : accountIdsFromUserBranches) {
            AccountsUpdate.deleteUserBranch(repo, serverIdent, accountId);
        }
    } catch (IOException e) {
        throw new OrmException("Failed to delete user branches for non-existing accounts.", e);
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) OrmException(com.google.gwtorm.server.OrmException) Inject(com.google.inject.Inject) Set(java.util.Set) IOException(java.io.IOException) Objects(java.util.Objects) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Provider(com.google.inject.Provider) SQLException(java.sql.SQLException) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) RefNames(com.google.gerrit.reviewdb.client.RefNames) Account(com.google.gerrit.reviewdb.client.Account) Collectors.toSet(java.util.stream.Collectors.toSet) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) Repository(org.eclipse.jgit.lib.Repository) AccountsUpdate(com.google.gerrit.server.account.AccountsUpdate) Account(com.google.gerrit.reviewdb.client.Account) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException)

Example 67 with Repository

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

the class Schema_119 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    JdbcSchema schema = (JdbcSchema) db;
    Connection connection = schema.getConnection();
    String tableName = "accounts";
    String emailStrategy = "email_strategy";
    Set<String> columns = schema.getDialect().listColumns(connection, tableName);
    Map<Account.Id, GeneralPreferencesInfo> imports = new HashMap<>();
    try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
        ResultSet rs = stmt.executeQuery("select " + "account_id, " + "maximum_page_size, " + "show_site_header, " + "use_flash_clipboard, " + "download_url, " + "download_command, " + (columns.contains(emailStrategy) ? emailStrategy + ", " : "copy_self_on_email, ") + "date_format, " + "time_format, " + "relative_date_in_change_table, " + "diff_view, " + "size_bar_in_change_table, " + "legacycid_in_change_table, " + "review_category_strategy, " + "mute_common_path_prefixes " + "from " + tableName)) {
        while (rs.next()) {
            GeneralPreferencesInfo p = new GeneralPreferencesInfo();
            Account.Id accountId = new Account.Id(rs.getInt(1));
            p.changesPerPage = (int) rs.getShort(2);
            p.showSiteHeader = toBoolean(rs.getString(3));
            p.useFlashClipboard = toBoolean(rs.getString(4));
            p.downloadScheme = convertToModernNames(rs.getString(5));
            p.downloadCommand = toDownloadCommand(rs.getString(6));
            p.emailStrategy = toEmailStrategy(rs.getString(7), columns.contains(emailStrategy));
            p.dateFormat = toDateFormat(rs.getString(8));
            p.timeFormat = toTimeFormat(rs.getString(9));
            p.relativeDateInChangeTable = toBoolean(rs.getString(10));
            p.diffView = toDiffView(rs.getString(11));
            p.sizeBarInChangeTable = toBoolean(rs.getString(12));
            p.legacycidInChangeTable = toBoolean(rs.getString(13));
            p.reviewCategoryStrategy = toReviewCategoryStrategy(rs.getString(14));
            p.muteCommonPathPrefixes = toBoolean(rs.getString(15));
            p.defaultBaseForMerges = GeneralPreferencesInfo.defaults().defaultBaseForMerges;
            imports.put(accountId, p);
        }
    }
    if (imports.isEmpty()) {
        return;
    }
    try (Repository git = mgr.openRepository(allUsersName);
        RevWalk rw = new RevWalk(git)) {
        BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
        for (Map.Entry<Account.Id, GeneralPreferencesInfo> e : imports.entrySet()) {
            try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
                md.getCommitBuilder().setAuthor(serverUser);
                md.getCommitBuilder().setCommitter(serverUser);
                VersionedAccountPreferences p = VersionedAccountPreferences.forUser(e.getKey());
                p.load(md);
                storeSection(p.getConfig(), UserConfigSections.GENERAL, null, e.getValue(), GeneralPreferencesInfo.defaults());
                p.commit(md);
            }
        }
        bru.execute(rw, NullProgressMonitor.INSTANCE);
    } catch (ConfigInvalidException | IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) HashMap(java.util.HashMap) Statement(java.sql.Statement) Connection(java.sql.Connection) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ResultSet(java.sql.ResultSet) GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo) VersionedAccountPreferences(com.google.gerrit.server.account.VersionedAccountPreferences) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 68 with Repository

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

the class UnfusedNoteDbBatchUpdate method executeChangeOps.

private Map<Change.Id, ChangeResult> executeChangeOps(boolean dryrun) throws Exception {
    logDebug("Executing change ops");
    Map<Change.Id, ChangeResult> result = Maps.newLinkedHashMapWithExpectedSize(ops.keySet().size());
    initRepository();
    Repository repo = repoView.getRepository();
    // update as in executeUpdateRepo.
    try (ObjectInserter ins = repo.newObjectInserter();
        ObjectReader reader = ins.newReader();
        RevWalk rw = new RevWalk(reader);
        NoteDbUpdateManager updateManager = updateManagerFactory.create(project).setChangeRepo(repo, rw, ins, new ChainedReceiveCommands(repo))) {
        if (user.isIdentifiedUser()) {
            updateManager.setRefLogIdent(user.asIdentifiedUser().newRefLogIdent(when, tz));
        }
        for (Map.Entry<Change.Id, Collection<BatchUpdateOp>> e : ops.asMap().entrySet()) {
            Change.Id id = e.getKey();
            ChangeContextImpl ctx = newChangeContext(id);
            boolean dirty = false;
            logDebug("Applying {} ops for change {}", e.getValue().size(), id);
            for (BatchUpdateOp op : e.getValue()) {
                dirty |= op.updateChange(ctx);
            }
            if (!dirty) {
                logDebug("No ops reported dirty, short-circuiting");
                result.put(id, ChangeResult.SKIPPED);
                continue;
            }
            for (ChangeUpdate u : ctx.updates.values()) {
                updateManager.add(u);
            }
            if (ctx.deleted) {
                logDebug("Change {} was deleted", id);
                updateManager.deleteChange(id);
                result.put(id, ChangeResult.DELETED);
            } else {
                result.put(id, ChangeResult.UPSERTED);
            }
        }
        if (!dryrun) {
            logDebug("Executing NoteDb updates");
            updateManager.execute();
        }
    }
    return result;
}
Also used : NoteDbUpdateManager(com.google.gerrit.server.notedb.NoteDbUpdateManager) Change(com.google.gerrit.reviewdb.client.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Collection(java.util.Collection) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RequestId(com.google.gerrit.server.util.RequestId) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 69 with Repository

use of org.eclipse.jgit.lib.Repository 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 70 with Repository

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

the class Schema_124 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    ListMultimap<Account.Id, AccountSshKey> imports = MultimapBuilder.hashKeys().arrayListValues().build();
    try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "seq, " + "ssh_public_key, " + "valid " + "FROM account_ssh_keys")) {
        while (rs.next()) {
            Account.Id accountId = new Account.Id(rs.getInt(1));
            int seq = rs.getInt(2);
            String sshPublicKey = rs.getString(3);
            AccountSshKey key = new AccountSshKey(new AccountSshKey.Id(accountId, seq), sshPublicKey);
            boolean valid = toBoolean(rs.getString(4));
            if (!valid) {
                key.setInvalid();
            }
            imports.put(accountId, key);
        }
    }
    if (imports.isEmpty()) {
        return;
    }
    try (Repository git = repoManager.openRepository(allUsersName);
        RevWalk rw = new RevWalk(git)) {
        BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
        for (Map.Entry<Account.Id, Collection<AccountSshKey>> e : imports.asMap().entrySet()) {
            try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
                md.getCommitBuilder().setAuthor(serverUser);
                md.getCommitBuilder().setCommitter(serverUser);
                VersionedAuthorizedKeys authorizedKeys = new VersionedAuthorizedKeys(new SimpleSshKeyCreator(), e.getKey());
                authorizedKeys.load(md);
                authorizedKeys.setKeys(fixInvalidSequenceNumbers(e.getValue()));
                authorizedKeys.commit(md);
            }
        }
        bru.execute(rw, NullProgressMonitor.INSTANCE);
    } catch (ConfigInvalidException | IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) Statement(java.sql.Statement) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) SimpleSshKeyCreator(com.google.gerrit.server.account.VersionedAuthorizedKeys.SimpleSshKeyCreator) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ResultSet(java.sql.ResultSet) Collection(java.util.Collection) VersionedAuthorizedKeys(com.google.gerrit.server.account.VersionedAuthorizedKeys) Map(java.util.Map) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)326 IOException (java.io.IOException)103 RevWalk (org.eclipse.jgit.revwalk.RevWalk)102 Test (org.junit.Test)81 RevCommit (org.eclipse.jgit.revwalk.RevCommit)76 ObjectId (org.eclipse.jgit.lib.ObjectId)72 File (java.io.File)43 TestRepository (org.eclipse.jgit.junit.TestRepository)40 Change (com.google.gerrit.reviewdb.client.Change)39 OrmException (com.google.gwtorm.server.OrmException)39 Ref (org.eclipse.jgit.lib.Ref)35 Project (com.google.gerrit.reviewdb.client.Project)32 ArrayList (java.util.ArrayList)31 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)27 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)26 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 Map (java.util.Map)23 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)23 RepositoryModel (com.gitblit.models.RepositoryModel)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)20