Package org.eclipse.jgit.lib

Examples of org.eclipse.jgit.lib.Repository


    }

    TemporaryBuffer.Heap buf =
        new TemporaryBuffer.Heap(args.settings.maximumDiffSize);
    DiffFormatter fmt = new DiffFormatter(buf);
    Repository git;
    try {
      git = args.server.openRepository(change.getProject());
    } catch (IOException e) {
      log.error("Cannot open repository to format patch", e);
      return "";
    }
    try {
      fmt.setRepository(git);
      fmt.setDetectRenames(true);
      fmt.format(patchList.getOldId(), patchList.getNewId());
      return RawParseUtils.decode(buf.toByteArray());
    } catch (IOException e) {
      if (JGitText.get().inMemoryBufferLimitExceeded.equals(e.getMessage())) {
        return "";
      }
      log.error("Cannot format patch", e);
      return "";
    } finally {
      fmt.release();
      git.close();
    }
  }
View Full Code Here


      names.addAll(gitManager.list());
    }

    boolean error = false;
    for (Project.NameKey project : names) {
      Repository git = gitManager.openRepository(project);
      try {
        switch (jarFactory.create(git).call()) {
          case NO_RULES:
            if (!all || projectNames.contains(project.get())) {
              System.err.println("error: No rules.pl in " + project.get());
              error = true;
            }
            break;

          case COMPILED:
            if (!quiet) {
              System.out.format("Compiled %-60s ... SUCCESS", project.get());
              System.out.println();
            }
            break;
        }
      } catch (CompileException err) {
        if (showStackTrace) {
          err.printStackTrace();
        } else {
          System.err.println("fatal: " + err.getMessage());
        }
        error = true;
      } finally {
        git.close();
      }
    }

    return !error ? 0 : 1;
  }
View Full Code Here

  }

  private void export(ReviewDb db, Project.NameKey project, List<Change> changes)
      throws IOException, OrmException, CodeReviewNoteCreationException,
      InterruptedException {
    final Repository git;
    try {
      git = gitManager.openRepository(project);
    } catch (RepositoryNotFoundException e) {
      return;
    }
    try {
      CreateCodeReviewNotes notes = codeReviewNotesFactory.create(db, git);
      try {
        notes.loadBase();
        for (Change change : changes) {
          monitor.update(1);
          PatchSet ps = db.patchSets().get(change.currentPatchSetId());
          if (ps == null) {
            continue;
          }
          notes.add(change, ObjectId.fromString(ps.getRevision().get()));
        }
        notes.commit("Exported prior reviews from Gerrit Code Review\n");
        notes.updateRef();
      } finally {
        notes.release();
      }
    } finally {
      git.close();
    }
  }
View Full Code Here

    return 0;
  }

  private void scan(ReviewDb db, Change change) {
    final Project.NameKey project = change.getDest().getParentKey();
    final Repository git;
    try {
      git = gitManager.openRepository(project);
    } catch (IOException e) {
      return;
    }
    try {
      PatchSet ps = db.patchSets().get(change.currentPatchSetId());
      if (ps == null || ps.getRevision() == null
          || ps.getRevision().get() == null) {
        return;
      }
      ChangeUtil.updateTrackingIds(db, change, footers, parse(git, ps)
          .getFooterLines());
    } catch (OrmException error) {
      System.err.println("ERR " + error.getMessage());
    } catch (IOException error) {
      System.err.println("ERR Cannot scan " + change.getId() + ": "
          + error.getMessage());
    } finally {
      git.close();
    }
  }
View Full Code Here

    if (groups.isEmpty()) {
      return;
    }
    ui.message("Moved account_groups.email_only_authors to 'Email Reviewers' capability");

    Repository git;
    try {
      git = mgr.openRepository(allProjects);
    } catch (IOException e) {
      throw new OrmException(e);
    }
    try {
      MetaDataUpdate md =
          new MetaDataUpdate(GitReferenceUpdated.DISABLED, allProjects, git);
      md.getCommitBuilder().setAuthor(serverUser);
      md.getCommitBuilder().setCommitter(serverUser);

      ProjectConfig config = ProjectConfig.read(md);
      AccessSection section =
          config.getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true);
      Permission capability =
          section.getPermission(GlobalCapability.EMAIL_REVIEWERS, true);
      for (GroupReference group : groups) {
        capability.getRule(config.resolve(group), true).setDeny();
      }

      md.setMessage("Upgrade to Gerrit Code Review schema 64\n");
      config.commit(md);
    } catch (IOException e) {
      throw new OrmException(e);
    } catch (ConfigInvalidException e) {
      throw new OrmException(e);
    } finally {
      git.close();
    }
  }
View Full Code Here

  }

  @Override
  protected void migrateData(ReviewDb db, UpdateUI ui)
      throws OrmException, SQLException {
    Repository git;
    try {
      git = mgr.openRepository(allProjects);
    } catch (IOException e) {
      throw new OrmException(e);
    }
    try {
      MetaDataUpdate md =
          new MetaDataUpdate(GitReferenceUpdated.DISABLED, allProjects, git);
      ProjectConfig config = ProjectConfig.read(md);
      Map<Integer, ContributorAgreement> agreements = getAgreementToAdd(db, config);
      if (agreements.isEmpty()) {
        return;
      }
      ui.message("Moved contributor agreements to project.config");

      // Create the auto verify groups.
      List<AccountGroup.UUID> adminGroupUUIDs = getAdministrateServerGroups(db, config);
      for (ContributorAgreement agreement : agreements.values()) {
        if (agreement.getAutoVerify() != null) {
          getOrCreateGroupForIndividuals(db, config, adminGroupUUIDs, agreement);
        }
      }

      // Scan AccountAgreement
      long minTime = addAccountAgreements(db, config, adminGroupUUIDs, agreements);

      ProjectConfig base = ProjectConfig.read(md, null);
      for (ContributorAgreement agreement : agreements.values()) {
        base.replace(agreement);
      }
      base.getAccountsSection().setSameGroupVisibility(
          config.getAccountsSection().getSameGroupVisibility());

      BatchMetaDataUpdate batch = base.openUpdate(md);
      try {
        // Scan AccountGroupAgreement
        List<AccountGroupAgreement> groupAgreements =
            getAccountGroupAgreements(db, agreements);

        // Find the earliest change
        for (AccountGroupAgreement aga : groupAgreements) {
          minTime = Math.min(minTime, aga.getTime());
        }
        minTime -= 60 * 1000; // 1 Minute

        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(new PersonIdent(serverUser, new Date(minTime)));
        commit.setCommitter(new PersonIdent(serverUser, new Date(minTime)));
        commit.setMessage("Add the ContributorAgreements for upgrade to Gerrit Code Review schema 65\n");
        batch.write(commit);

        for (AccountGroupAgreement aga : groupAgreements) {
          AccountGroup group = db.accountGroups().get(aga.groupId);
          if (group == null) {
            continue;
          }

          ContributorAgreement agreement = agreements.get(aga.claId);
          agreement.getAccepted().add(new PermissionRule(config.resolve(group)));
          base.replace(agreement);

          PersonIdent ident = null;
          if (aga.reviewedBy != null) {
            Account ua = db.accounts().get(aga.reviewedBy);
            if (ua != null) {
              String name = ua.getFullName();
              String email = ua.getPreferredEmail();

              if (email == null || email.isEmpty()) {
                // No preferred email is configured. Use a generic identity so we
                // don't leak an address the user may have given us, but doesn't
                // necessarily want to publish through Git records.
                //
                String user = ua.getUserName();
                if (user == null || user.isEmpty()) {
                  user = "account-" + ua.getId().toString();
                }

                String host = SystemReader.getInstance().getHostname();
                email = user + "@" + host;
              }

              if (name == null || name.isEmpty()) {
                final int at = email.indexOf('@');
                if (0 < at) {
                  name = email.substring(0, at);
                } else {
                  name = anonymousCowardName;
                }
              }

              ident = new PersonIdent(name, email, new Date(aga.getTime()), TimeZone.getDefault());
            }
          }
          if (ident == null) {
            ident = new PersonIdent(serverUser, new Date(aga.getTime()));
          }

          // Build the commits such that it keeps track of the date added and
          // who added it.
          commit = new CommitBuilder();
          commit.setAuthor(ident);
          commit.setCommitter(new PersonIdent(serverUser, new Date(aga.getTime())));

          String msg = String.format("Accept %s contributor agreement for %s\n",
              agreement.getName(), group.getName());
          if (!Strings.isNullOrEmpty(aga.reviewComments)) {
            msg += "\n" + aga.reviewComments + "\n";
          }
          commit.setMessage(msg);
          batch.write(commit);
        }

        // Merge the agreements with the other data in project.config.
        commit = new CommitBuilder();
        commit.setAuthor(serverUser);
        commit.setCommitter(serverUser);
        commit.setMessage("Upgrade to Gerrit Code Review schema 65\n");
        commit.addParentId(config.getRevision());
        batch.write(config, commit);

        // Save the the final metadata.
        batch.commitAt(config.getRevision());
      } finally {
        batch.close();
      }
    } catch (IOException e) {
      throw new OrmException(e);
    } catch (ConfigInvalidException e) {
      throw new OrmException(e);
    } finally {
      git.close();
    }
  }
View Full Code Here

    ResultSet rs = stmt.executeQuery("SELECT * FROM projects ORDER BY name");
    while (rs.next()) {
      final String name = rs.getString("name");
      final Project.NameKey nameKey = new Project.NameKey(name);

      Repository git;
      try {
        git = mgr.openRepository(nameKey);
      } catch (RepositoryNotFoundException notFound) {
        // A repository may be missing if this project existed only to store
        // inheritable permissions. For example 'All-Projects'.
        try {
          git = mgr.createRepository(nameKey);
        } catch (IOException err) {
          throw new OrmException("Cannot create repository " + name, err);
        }
      } catch (IOException e) {
        throw new OrmException(e);
      }
      try {
        MetaDataUpdate md =
            new MetaDataUpdate(GitReferenceUpdated.DISABLED, nameKey, git);
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);

        ProjectConfig config = ProjectConfig.read(md);
        loadProject(rs, config.getProject());
        config.getAccessSections().clear();
        convertRights(config);

        // Grant out read on the config branch by default.
        //
        if (config.getProject().getNameKey().equals(systemConfig.wildProjectName)) {
          AccessSection meta = config.getAccessSection(GitRepositoryManager.REF_CONFIG, true);
          Permission read = meta.getPermission(READ, true);
          read.getRule(config.resolve(projectOwners), true);
        }

        md.setMessage("Import project configuration from SQL\n");
        config.commit(md);
      } catch (ConfigInvalidException err) {
        throw new OrmException("Cannot read project " + name, err);
      } catch (IOException err) {
        throw new OrmException("Cannot export project " + name, err);
      } finally {
        git.close();
      }
    }
    rs.close();
    stmt.close();
  }
View Full Code Here

  }

  @Override
  protected void migrateData(ReviewDb db, UpdateUI ui) {
    for (Project.NameKey name : mgr.list()) {
      Repository git;
      try {
        git = mgr.openRepository(name);
      } catch (RepositoryNotFoundException e) {
        ui.message("warning: Cannot open " + name.get());
        continue;
      }
      try {
        Map<String, Ref> all = git.getAllRefs();
        if (all.keySet().equals(keysOne) || all.keySet().equals(keysTwo)) {
          try {
            RefUpdate update = git.updateRef(Constants.HEAD);
            update.disableRefLog();
            update.link(GitRepositoryManager.REF_CONFIG);
          } catch (IOException err) {
            ui.message("warning: " + name.get() + ": Cannot update HEAD to "
                + GitRepositoryManager.REF_CONFIG + ": " + err.getMessage());
          }
        }
      } finally {
        git.close();
      }
    }
  }
View Full Code Here

    }

    final BanCommitResult result = new BanCommitResult();

    final PersonIdent currentUserIdent = createPersonIdent();
    final Repository repo =
        repoManager.openRepository(projectControl.getProject().getNameKey());
    try {
      final RevWalk revWalk = new RevWalk(repo);
      final ObjectInserter inserter = repo.newObjectInserter();
      try {
        NoteMap baseNoteMap = null;
        RevCommit baseCommit = null;
        final Ref notesBranch = repo.getRef(REF_REJECT_COMMITS);
        if (notesBranch != null) {
          baseCommit = revWalk.parseCommit(notesBranch.getObjectId());
          baseNoteMap = NoteMap.read(revWalk.getObjectReader(), baseCommit);
        }

        final NoteMap ourNoteMap;
        if (baseCommit != null) {
          ourNoteMap = NoteMap.read(repo.newObjectReader(), baseCommit);
        } else {
          ourNoteMap = NoteMap.newEmptyMap();
        }

        for (final ObjectId commitToBan : commitsToBan) {
          try {
            revWalk.parseCommit(commitToBan);
          } catch (MissingObjectException e) {
            // ignore exception, also not existing commits can be banned
          } catch (IncorrectObjectTypeException e) {
            result.notACommit(commitToBan, e.getMessage());
            continue;
          }

          final Note note = ourNoteMap.getNote(commitToBan);
          if (note != null) {
            result.commitAlreadyBanned(commitToBan);
            continue;
          }

          final String noteContent = reason != null ? reason : "";
          final ObjectId noteContentId =
              inserter
                  .insert(Constants.OBJ_BLOB, noteContent.getBytes("UTF-8"));
          ourNoteMap.set(commitToBan, noteContentId);
          result.commitBanned(commitToBan);
        }

        if (result.getNewlyBannedCommits().isEmpty()) {
          return result;
        }

        final ObjectId ourCommit =
            commit(ourNoteMap, inserter, currentUserIdent, baseCommit, result,
                reason);

        updateRef(repo, revWalk, inserter, ourNoteMap, ourCommit, baseNoteMap,
            baseCommit);
      } finally {
        revWalk.release();
        inserter.release();
      }
    } finally {
      repo.close();
    }

    return result;
  }
View Full Code Here

    c.systemConfig().insert(Collections.singleton(s));
    return s;
  }

  private void initWildCardProject() throws IOException, ConfigInvalidException {
    Repository git;
    try {
      git = mgr.openRepository(allProjectsName);
    } catch (RepositoryNotFoundException notFound) {
      // A repository may be missing if this project existed only to store
      // inheritable permissions. For example 'All-Projects'.
      try {
        git = mgr.createRepository(allProjectsName);
        final RefUpdate u = git.updateRef(Constants.HEAD);
        u.link(GitRepositoryManager.REF_CONFIG);
      } catch (RepositoryNotFoundException err) {
        final String name = allProjectsName.get();
        throw new IOException("Cannot create repository " + name, err);
      }
    }
    try {
      MetaDataUpdate md =
          new MetaDataUpdate(GitReferenceUpdated.DISABLED, allProjectsName, git);
      md.getCommitBuilder().setAuthor(serverUser);
      md.getCommitBuilder().setCommitter(serverUser);

      ProjectConfig config = ProjectConfig.read(md);
      Project p = config.getProject();
      p.setDescription("Rights inherited by all other projects");
      p.setUseContributorAgreements(false);

      AccessSection cap = config.getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true);
      AccessSection all = config.getAccessSection(AccessSection.ALL, true);
      AccessSection heads = config.getAccessSection(AccessSection.HEADS, true);
      AccessSection meta = config.getAccessSection(GitRepositoryManager.REF_CONFIG, true);

      cap.getPermission(GlobalCapability.ADMINISTRATE_SERVER, true)
        .add(rule(config, admin));

      PermissionRule review = rule(config, registered);
      review.setRange(-1, 1);
      heads.getPermission(Permission.LABEL + "Code-Review", true).add(review);

      all.getPermission(Permission.READ, true) //
          .add(rule(config, admin));
      all.getPermission(Permission.READ, true) //
          .add(rule(config, anonymous));

      config.getAccessSection("refs/for/" + AccessSection.ALL, true) //
          .getPermission(Permission.PUSH, true) //
          .add(rule(config, registered));
      all.getPermission(Permission.FORGE_AUTHOR, true) //
          .add(rule(config, registered));

      Permission metaReadPermission = meta.getPermission(Permission.READ, true);
      metaReadPermission.setExclusiveGroup(true);
      metaReadPermission.add(rule(config, owners));

      md.setMessage("Initialized Gerrit Code Review " + Version.getVersion());
      config.commit(md);
    } finally {
      git.close();
    }
  }
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.lib.Repository

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.