Package org.eclipse.jgit.storage.file

Examples of org.eclipse.jgit.storage.file.FileBasedConfig


      metadata.serverHostname = Constants.NAME;
    }

    // set default values from config file
    File certificatesConfigFile = new File(folder, X509Utils.CA_CONFIG);
    FileBasedConfig config = new FileBasedConfig(certificatesConfigFile, FS.detect());
    if (certificatesConfigFile.exists()) {
      try {
        config.load();
      } catch (Exception e) {
        Utils.showException(GitblitAuthority.this, e);
      }
      NewCertificateConfig certificateConfig = NewCertificateConfig.KEY.parse(config);
      certificateConfig.update(metadata);
View Full Code Here


    }
  }

  private void updateAuthorityConfig(UserCertificateModel ucm) {
    File certificatesConfigFile = new File(folder, X509Utils.CA_CONFIG);
    FileBasedConfig config = new FileBasedConfig(certificatesConfigFile, FS.detect());
    if (certificatesConfigFile.exists()) {
      try {
        config.load();
      } catch (Exception e) {
        Utils.showException(GitblitAuthority.this, e);
      }
    }
    ucm.update(config);
    try {
      config.save();
    } catch (Exception e) {
      Utils.showException(GitblitAuthority.this, e);
    }
  }
View Full Code Here

      // generate CA & web certificates, create certificate stores
      X509Metadata metadata = new X509Metadata("localhost", params.storePassword);
      // set default certificate values from config file
      if (certificatesConf.exists()) {
        FileBasedConfig config = new FileBasedConfig(certificatesConf, FS.detect());
        try {
          config.load();
        } catch (Exception e) {
          logger.error("Error parsing " + certificatesConf, e);
        }
        NewCertificateConfig certificateConfig = NewCertificateConfig.KEY.parse(config);
        certificateConfig.update(metadata);
      }

      metadata.notAfter = new Date(System.currentTimeMillis() + 10*TimeUtils.ONEYEAR);
      X509Utils.prepareX509Infrastructure(metadata, baseFolder, new X509Log() {
        @Override
        public void log(String message) {
          BufferedWriter writer = null;
          try {
            writer = new BufferedWriter(new FileWriter(new File(baseFolder, X509Utils.CERTS + File.separator + "log.txt"), true));
            writer.write(MessageFormat.format("{0,date,yyyy-MM-dd HH:mm}: {1}", new Date(), message));
            writer.newLine();
            writer.flush();
          } catch (Exception e) {
            LoggerFactory.getLogger(GitblitAuthority.class).error("Failed to append log entry!", e);
          } finally {
            if (writer != null) {
              try {
                writer.close();
              } catch (IOException e) {
              }
            }
          }
        }
      });

      if (serverKeyStore.exists()) {
        /*
         * HTTPS
         */
        logger.info("Setting up HTTPS transport on port " + params.securePort);
        GitblitSslContextFactory factory = new GitblitSslContextFactory(params.alias,
            serverKeyStore, serverTrustStore, params.storePassword, caRevocationList);
        if (params.requireClientCertificates) {
          factory.setNeedClientAuth(true);
        } else {
          factory.setWantClientAuth(true);
        }

        ServerConnector connector = new ServerConnector(server, factory);
        connector.setSoLingerTime(-1);
        connector.setIdleTimeout(30000);
        connector.setPort(params.securePort);
        String bindInterface = settings.getString(Keys.server.httpsBindInterface, null);
        if (!StringUtils.isEmpty(bindInterface)) {
          logger.warn(MessageFormat.format(
              "Binding HTTPS transport on port {0,number,0} to {1}", params.securePort,
              bindInterface));
          connector.setHost(bindInterface);
        }
        if (params.securePort < 1024 && !isWindows()) {
          logger.warn("Gitblit needs to run with ROOT permissions for ports < 1024!");
        }

        server.addConnector(connector);
      } else {
        logger.warn("Failed to find or load Keystore?");
        logger.warn("HTTPS transport DISABLED.");
      }
    }

    // conditionally configure the http transport
    if (params.port > 0) {
      /*
       * HTTP
       */
      logger.info("Setting up HTTP transport on port " + params.port);

      HttpConfiguration httpConfig = new HttpConfiguration();
      if (params.port > 0 && params.securePort > 0 && settings.getBoolean(Keys.server.redirectToHttpsPort, true)) {
        httpConfig.setSecureScheme("https");
        httpConfig.setSecurePort(params.securePort);
      }
          httpConfig.setSendServerVersion(false);
          httpConfig.setSendDateHeader(false);

      ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
      connector.setSoLingerTime(-1);
      connector.setIdleTimeout(30000);
      connector.setPort(params.port);
      String bindInterface = settings.getString(Keys.server.httpBindInterface, null);
      if (!StringUtils.isEmpty(bindInterface)) {
        logger.warn(MessageFormat.format("Binding HTTP transport on port {0,number,0} to {1}",
            params.port, bindInterface));
        connector.setHost(bindInterface);
      }
      if (params.port < 1024 && !isWindows()) {
        logger.warn("Gitblit needs to run with ROOT permissions for ports < 1024!");
      }

      server.addConnector(connector);
    }

    // tempDir is where the embedded Gitblit web application is expanded and
    // where Jetty creates any necessary temporary files
    File tempDir = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, baseFolder, params.temp);
    if (tempDir.exists()) {
      try {
        FileUtils.delete(tempDir, FileUtils.RECURSIVE | FileUtils.RETRY);
      } catch (IOException x) {
        logger.warn("Failed to delete temp dir " + tempDir.getAbsolutePath(), x);
      }
    }
    if (!tempDir.mkdirs()) {
      logger.warn("Failed to create temp dir " + tempDir.getAbsolutePath());
    }

    // Get the execution path of this class
    // We use this to set the WAR path.
    ProtectionDomain protectionDomain = GitBlitServer.class.getProtectionDomain();
    URL location = protectionDomain.getCodeSource().getLocation();

    // Root WebApp Context
    WebAppContext rootContext = new WebAppContext();
    rootContext.setContextPath(settings.getString(Keys.server.contextPath, "/"));
    rootContext.setServer(server);
    rootContext.setWar(location.toExternalForm());
    rootContext.setTempDirectory(tempDir);

    // Set cookies HttpOnly so they are not accessible to JavaScript engines
    HashSessionManager sessionManager = new HashSessionManager();
    sessionManager.setHttpOnly(true);
    // Use secure cookies if only serving https
    sessionManager.setSecureRequestOnly(params.port <= 0 && params.securePort > 0);
    rootContext.getSessionHandler().setSessionManager(sessionManager);

    // Ensure there is a defined User Service
    String realmUsers = params.userService;
    if (StringUtils.isEmpty(realmUsers)) {
      logger.error(MessageFormat.format("PLEASE SPECIFY {0}!!", Keys.realm.userService));
      return;
    }

    // Override settings from the command-line
    settings.overrideSetting(Keys.realm.userService, params.userService);
    settings.overrideSetting(Keys.git.repositoriesFolder, params.repositoriesFolder);
    settings.overrideSetting(Keys.git.daemonPort, params.gitPort);
    settings.overrideSetting(Keys.git.sshPort, params.sshPort);

    // Start up an in-memory LDAP server, if configured
    try {
      if (!StringUtils.isEmpty(params.ldapLdifFile)) {
        File ldifFile = new File(params.ldapLdifFile);
        if (ldifFile != null && ldifFile.exists()) {
          URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap.server));
          String firstLine = new Scanner(ldifFile).nextLine();
          String rootDN = firstLine.substring(4);
          String bindUserName = settings.getString(Keys.realm.ldap.username, "");
          String bindPassword = settings.getString(Keys.realm.ldap.password, "");

          // Get the port
          int port = ldapUrl.getPort();
          if (port == -1)
            port = 389;

          InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(rootDN);
          config.addAdditionalBindCredentials(bindUserName, bindPassword);
          config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("default", port));
          config.setSchema(null);

          InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
          ds.importFromLDIF(true, new LDIFReader(ldifFile));
          ds.startListening();

View Full Code Here

   */
  private synchronized void write() throws IOException {
    // Write a temporary copy of the users file
    File realmFileCopy = new File(realmFile.getAbsolutePath() + ".tmp");

    StoredConfig config = new FileBasedConfig(realmFileCopy, FS.detect());

    // write users
    for (UserModel model : users.values()) {
      if (!StringUtils.isEmpty(model.password)) {
        config.setString(USER, model.username, PASSWORD, model.password);
      }
      if (!StringUtils.isEmpty(model.cookie)) {
        config.setString(USER, model.username, COOKIE, model.cookie);
      }
      if (!StringUtils.isEmpty(model.displayName)) {
        config.setString(USER, model.username, DISPLAYNAME, model.displayName);
      }
      if (!StringUtils.isEmpty(model.emailAddress)) {
        config.setString(USER, model.username, EMAILADDRESS, model.emailAddress);
      }
      if (model.accountType != null) {
        config.setString(USER, model.username, ACCOUNTTYPE, model.accountType.name());
      }
      if (!StringUtils.isEmpty(model.organizationalUnit)) {
        config.setString(USER, model.username, ORGANIZATIONALUNIT, model.organizationalUnit);
      }
      if (!StringUtils.isEmpty(model.organization)) {
        config.setString(USER, model.username, ORGANIZATION, model.organization);
      }
      if (!StringUtils.isEmpty(model.locality)) {
        config.setString(USER, model.username, LOCALITY, model.locality);
      }
      if (!StringUtils.isEmpty(model.stateProvince)) {
        config.setString(USER, model.username, STATEPROVINCE, model.stateProvince);
      }
      if (!StringUtils.isEmpty(model.countryCode)) {
        config.setString(USER, model.username, COUNTRYCODE, model.countryCode);
      }
      if (model.disabled) {
        config.setBoolean(USER, model.username, DISABLED, true);
      }
      if (model.getPreferences() != null) {
        Locale locale = model.getPreferences().getLocale();
        if (locale != null) {
          String val;
          if (StringUtils.isEmpty(locale.getCountry())) {
            val = locale.getLanguage();
          } else {
            val = locale.getLanguage() + "_" + locale.getCountry();
          }
          config.setString(USER, model.username, LOCALE, val);
        }

        config.setBoolean(USER, model.username, EMAILONMYTICKETCHANGES, model.getPreferences().isEmailMeOnMyTicketChanges());

        if (model.getPreferences().getTransport() != null) {
          config.setString(USER, model.username, TRANSPORT, model.getPreferences().getTransport().name());
        }
      }

      // user roles
      List<String> roles = new ArrayList<String>();
      if (model.canAdmin) {
        roles.add(Constants.ADMIN_ROLE);
      }
      if (model.canFork) {
        roles.add(Constants.FORK_ROLE);
      }
      if (model.canCreate) {
        roles.add(Constants.CREATE_ROLE);
      }
      if (model.excludeFromFederation) {
        roles.add(Constants.NOT_FEDERATED_ROLE);
      }
      if (roles.size() == 0) {
        // we do this to ensure that user record with no password
        // is written.  otherwise, StoredConfig optimizes that account
        // away. :(
        roles.add(Constants.NO_ROLE);
      }
      config.setStringList(USER, model.username, ROLE, roles);

      // discrete repository permissions
      if (model.permissions != null && !model.canAdmin) {
        List<String> permissions = new ArrayList<String>();
        for (Map.Entry<String, AccessPermission> entry : model.permissions.entrySet()) {
          if (entry.getValue().exceeds(AccessPermission.NONE)) {
            permissions.add(entry.getValue().asRole(entry.getKey()));
          }
        }
        config.setStringList(USER, model.username, REPOSITORY, permissions);
      }

      // user preferences
      if (model.getPreferences() != null) {
        List<String> starred =  model.getPreferences().getStarredRepositories();
        if (starred.size() > 0) {
          config.setStringList(USER, model.username, STARRED, starred);
        }
      }
    }

    // write teams
    for (TeamModel model : teams.values()) {
      // team roles
      List<String> roles = new ArrayList<String>();
      if (model.canAdmin) {
        roles.add(Constants.ADMIN_ROLE);
      }
      if (model.canFork) {
        roles.add(Constants.FORK_ROLE);
      }
      if (model.canCreate) {
        roles.add(Constants.CREATE_ROLE);
      }
      if (roles.size() == 0) {
        // we do this to ensure that team record is written.
        // Otherwise, StoredConfig might optimizes that record away.
        roles.add(Constants.NO_ROLE);
      }
      config.setStringList(TEAM, model.name, ROLE, roles);
      if (model.accountType != null) {
        config.setString(TEAM, model.name, ACCOUNTTYPE, model.accountType.name());
      }

      if (!model.canAdmin) {
        // write team permission for non-admin teams
        if (model.permissions == null) {
          // null check on "final" repositories because JSON-sourced TeamModel
          // can have a null repositories object
          if (!ArrayUtils.isEmpty(model.repositories)) {
            config.setStringList(TEAM, model.name, REPOSITORY, new ArrayList<String>(
                model.repositories));
          }
        } else {
          // discrete repository permissions
          List<String> permissions = new ArrayList<String>();
          for (Map.Entry<String, AccessPermission> entry : model.permissions.entrySet()) {
            if (entry.getValue().exceeds(AccessPermission.NONE)) {
              // code:repository (e.g. RW+:~james/myrepo.git
              permissions.add(entry.getValue().asRole(entry.getKey()));
            }
          }
          config.setStringList(TEAM, model.name, REPOSITORY, permissions);
        }
      }

      // null check on "final" users because JSON-sourced TeamModel
      // can have a null users object
      if (!ArrayUtils.isEmpty(model.users)) {
        config.setStringList(TEAM, model.name, USER, new ArrayList<String>(model.users));
      }

      // null check on "final" mailing lists because JSON-sourced
      // TeamModel can have a null users object
      if (!ArrayUtils.isEmpty(model.mailingLists)) {
        config.setStringList(TEAM, model.name, MAILINGLIST, new ArrayList<String>(
            model.mailingLists));
      }

      // null check on "final" preReceiveScripts because JSON-sourced
      // TeamModel can have a null preReceiveScripts object
      if (!ArrayUtils.isEmpty(model.preReceiveScripts)) {
        config.setStringList(TEAM, model.name, PRERECEIVE, model.preReceiveScripts);
      }

      // null check on "final" postReceiveScripts because JSON-sourced
      // TeamModel can have a null postReceiveScripts object
      if (!ArrayUtils.isEmpty(model.postReceiveScripts)) {
        config.setStringList(TEAM, model.name, POSTRECEIVE, model.postReceiveScripts);
      }
    }

    config.save();
    // manually set the forceReload flag because not all JVMs support real
    // millisecond resolution of lastModified. (issue-55)
    forceReload = true;

    // If the write is successful, delete the current file and rename
View Full Code Here

      users.clear();
      cookies.clear();
      teams.clear();

      try {
        StoredConfig config = new FileBasedConfig(realmFile, FS.detect());
        config.load();
        Set<String> usernames = config.getSubsections(USER);
        for (String username : usernames) {
          UserModel user = new UserModel(username.toLowerCase());
          user.password = config.getString(USER, username, PASSWORD);
          user.displayName = config.getString(USER, username, DISPLAYNAME);
          user.emailAddress = config.getString(USER, username, EMAILADDRESS);
          user.accountType = AccountType.fromString(config.getString(USER, username, ACCOUNTTYPE));
          if (Constants.EXTERNAL_ACCOUNT.equals(user.password) && user.accountType.isLocal()) {
            user.accountType = AccountType.EXTERNAL;
          }
          user.disabled = config.getBoolean(USER, username, DISABLED, false);
          user.organizationalUnit = config.getString(USER, username, ORGANIZATIONALUNIT);
          user.organization = config.getString(USER, username, ORGANIZATION);
          user.locality = config.getString(USER, username, LOCALITY);
          user.stateProvince = config.getString(USER, username, STATEPROVINCE);
          user.countryCode = config.getString(USER, username, COUNTRYCODE);
          user.cookie = config.getString(USER, username, COOKIE);
          if (StringUtils.isEmpty(user.cookie) && !StringUtils.isEmpty(user.password)) {
            user.cookie = StringUtils.getSHA1(user.username + user.password);
          }

          // preferences
          user.getPreferences().setLocale(config.getString(USER, username, LOCALE));
          user.getPreferences().setEmailMeOnMyTicketChanges(config.getBoolean(USER, username, EMAILONMYTICKETCHANGES, true));
          user.getPreferences().setTransport(Transport.fromString(config.getString(USER, username, TRANSPORT)));

          // user roles
          Set<String> roles = new HashSet<String>(Arrays.asList(config.getStringList(
              USER, username, ROLE)));
          user.canAdmin = roles.contains(Constants.ADMIN_ROLE);
          user.canFork = roles.contains(Constants.FORK_ROLE);
          user.canCreate = roles.contains(Constants.CREATE_ROLE);
          user.excludeFromFederation = roles.contains(Constants.NOT_FEDERATED_ROLE);

          // repository memberships
          if (!user.canAdmin) {
            // non-admin, read permissions
            Set<String> repositories = new HashSet<String>(Arrays.asList(config
                .getStringList(USER, username, REPOSITORY)));
            for (String repository : repositories) {
              user.addRepositoryPermission(repository);
            }
          }

          // starred repositories
          Set<String> starred = new HashSet<String>(Arrays.asList(config
              .getStringList(USER, username, STARRED)));
          for (String repository : starred) {
            UserRepositoryPreferences prefs = user.getPreferences().getRepositoryPreferences(repository);
            prefs.starred = true;
          }

          // update cache
          users.put(user.username, user);
          if (!StringUtils.isEmpty(user.cookie)) {
            cookies.put(user.cookie, user);
          }
        }

        // load the teams
        Set<String> teamnames = config.getSubsections(TEAM);
        for (String teamname : teamnames) {
          TeamModel team = new TeamModel(teamname);
          Set<String> roles = new HashSet<String>(Arrays.asList(config.getStringList(
              TEAM, teamname, ROLE)));
          team.canAdmin = roles.contains(Constants.ADMIN_ROLE);
          team.canFork = roles.contains(Constants.FORK_ROLE);
          team.canCreate = roles.contains(Constants.CREATE_ROLE);
          team.accountType = AccountType.fromString(config.getString(TEAM, teamname, ACCOUNTTYPE));

          if (!team.canAdmin) {
            // non-admin team, read permissions
            team.addRepositoryPermissions(Arrays.asList(config.getStringList(TEAM, teamname,
                REPOSITORY)));
          }
          team.addUsers(Arrays.asList(config.getStringList(TEAM, teamname, USER)));
          team.addMailingLists(Arrays.asList(config.getStringList(TEAM, teamname,
              MAILINGLIST)));
          team.preReceiveScripts.addAll(Arrays.asList(config.getStringList(TEAM,
              teamname, PRERECEIVE)));
          team.postReceiveScripts.addAll(Arrays.asList(config.getStringList(TEAM,
              teamname, POSTRECEIVE)));

          teams.put(team.name.toLowerCase(), team);

          // set the teams on the users
View Full Code Here

      removeFromCachedRepositoryList(repositoryName);
      logger.error(MessageFormat.format("Repository \"{0}\" is missing! Removing from cache.", repositoryName));
      return null;
    }

    FileBasedConfig config = (FileBasedConfig) getRepositoryConfig(r);
    if (config.isOutdated()) {
      // reload model
      logger.debug(MessageFormat.format("Config for \"{0}\" has changed. Reloading model and updating cache.", repositoryName));
      model = loadRepositoryModel(model.name);
      removeFromCachedRepositoryList(model.name);
      addToCachedRepositoryList(model);
View Full Code Here

   * @param repository
   * @return a config object
   */
  private FileBasedConfig getConfig(Repository repository) {
    File file = new File(repository.getDirectory(), CONF_FILE);
    FileBasedConfig config = new FileBasedConfig(file, FS.detect());
    return config;
  }
View Full Code Here

   * @param repository
   * @return true of the on-disk index format is different than INDEX_VERSION
   */
  private boolean shouldReindex(Repository repository) {
    try {
      FileBasedConfig config = getConfig(repository);
      config.load();
      int indexVersion = config.getInt(CONF_INDEX, CONF_VERSION, 0);
      // reindex if versions do not match
      return indexVersion != INDEX_VERSION;
    } catch (Throwable t) {
    }
    return true;
View Full Code Here

    if (!deleteIndex(model.name)) {
      return result;
    }
    try {
      String [] encodings = storedSettings.getStrings(Keys.web.blobEncodings).toArray(new String[0]);
      FileBasedConfig config = getConfig(repository);
      Set<String> indexedCommits = new TreeSet<String>();
      IndexWriter writer = getIndexWriter(model.name);
      // build a quick lookup of tags
      Map<String, List<String>> tags = new HashMap<String, List<String>>();
      for (RefModel tag : JGitUtils.getTags(repository, false, -1)) {
        if (!tag.isAnnotatedTag()) {
          // skip non-annotated tags
          continue;
        }
        if (!tags.containsKey(tag.getObjectId().getName())) {
          tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>());
        }
        tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);
      }

      ObjectReader reader = repository.newObjectReader();

      // get the local branches
      List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);

      // sort them by most recently updated
      Collections.sort(branches, new Comparator<RefModel>() {
        @Override
        public int compare(RefModel ref1, RefModel ref2) {
          return ref2.getDate().compareTo(ref1.getDate());
        }
      });

      // reorder default branch to first position
      RefModel defaultBranch = null;
      ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository);
      for (RefModel branch :  branches) {
        if (branch.getObjectId().equals(defaultBranchId)) {
          defaultBranch = branch;
          break;
        }
      }
      branches.remove(defaultBranch);
      branches.add(0, defaultBranch);

      // walk through each branch
      for (RefModel branch : branches) {

        boolean indexBranch = false;
        if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH)
            && branch.equals(defaultBranch)) {
          // indexing "default" branch
          indexBranch = true;
        } else if (branch.getName().startsWith(com.gitblit.Constants.R_META)) {
          // skip internal meta branches
          indexBranch = false;
        } else {
          // normal explicit branch check
          indexBranch = model.indexedBranches.contains(branch.getName());
        }

        // if this branch is not specifically indexed then skip
        if (!indexBranch) {
          continue;
        }

        String branchName = branch.getName();
        RevWalk revWalk = new RevWalk(reader);
        RevCommit tip = revWalk.parseCommit(branch.getObjectId());
        String tipId = tip.getId().getName();

        String keyName = getBranchKey(branchName);
        config.setString(CONF_ALIAS, null, keyName, branchName);
        config.setString(CONF_BRANCH, null, keyName, tipId);

        // index the blob contents of the tree
        TreeWalk treeWalk = new TreeWalk(repository);
        treeWalk.addTree(tip.getTree());
        treeWalk.setRecursive(true);

        Map<String, ObjectId> paths = new TreeMap<String, ObjectId>();
        while (treeWalk.next()) {
          // ensure path is not in a submodule
          if (treeWalk.getFileMode(0) != FileMode.GITLINK) {
            paths.put(treeWalk.getPathString(), treeWalk.getObjectId(0));
          }
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] tmp = new byte[32767];

        RevWalk commitWalk = new RevWalk(reader);
        commitWalk.markStart(tip);

        RevCommit commit;
        while ((paths.size() > 0) && (commit = commitWalk.next()) != null) {
          TreeWalk diffWalk = new TreeWalk(reader);
          int parentCount = commit.getParentCount();
          switch (parentCount) {
          case 0:
            diffWalk.addTree(new EmptyTreeIterator());
            break;
          case 1:
            diffWalk.addTree(getTree(commitWalk, commit.getParent(0)));
            break;
          default:
            // skip merge commits
            continue;
          }
          diffWalk.addTree(getTree(commitWalk, commit));
          diffWalk.setFilter(ANY_DIFF);
          diffWalk.setRecursive(true);
          while ((paths.size() > 0) && diffWalk.next()) {
            String path = diffWalk.getPathString();
            if (!paths.containsKey(path)) {
              continue;
            }

            // remove path from set
            ObjectId blobId = paths.remove(path);
            result.blobCount++;

            // index the blob metadata
            String blobAuthor = getAuthor(commit);
            String blobCommitter = getCommitter(commit);
            String blobDate = DateTools.timeToString(commit.getCommitTime() * 1000L,
                Resolution.MINUTE);

            Document doc = new Document();
            doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.blob.name(), StringField.TYPE_STORED));
            doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED));
            doc.add(new Field(FIELD_COMMIT, commit.getName(), TextField.TYPE_STORED));
            doc.add(new Field(FIELD_PATH, path, TextField.TYPE_STORED));
            doc.add(new Field(FIELD_DATE, blobDate, StringField.TYPE_STORED));
            doc.add(new Field(FIELD_AUTHOR, blobAuthor, TextField.TYPE_STORED));
            doc.add(new Field(FIELD_COMMITTER, blobCommitter, TextField.TYPE_STORED));

            // determine extension to compare to the extension
            // blacklist
            String ext = null;
            String name = path.toLowerCase();
            if (name.indexOf('.') > -1) {
              ext = name.substring(name.lastIndexOf('.') + 1);
            }

            // index the blob content
            if (StringUtils.isEmpty(ext) || !excludedExtensions.contains(ext)) {
              ObjectLoader ldr = repository.open(blobId, Constants.OBJ_BLOB);
              InputStream in = ldr.openStream();
              int n;
              while ((n = in.read(tmp)) > 0) {
                os.write(tmp, 0, n);
              }
              in.close();
              byte[] content = os.toByteArray();
              String str = StringUtils.decodeString(content, encodings);
              doc.add(new Field(FIELD_CONTENT, str, TextField.TYPE_STORED));
              os.reset();
            }

            // add the blob to the index
            writer.addDocument(doc);
          }
        }

        os.close();

        // index the tip commit object
        if (indexedCommits.add(tipId)) {
          Document doc = createDocument(tip, tags.get(tipId));
          doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED));
          writer.addDocument(doc);
          result.commitCount += 1;
          result.branchCount += 1;
        }

        // traverse the log and index the previous commit objects
        RevWalk historyWalk = new RevWalk(reader);
        historyWalk.markStart(historyWalk.parseCommit(tip.getId()));
        RevCommit rev;
        while ((rev = historyWalk.next()) != null) {
          String hash = rev.getId().getName();
          if (indexedCommits.add(hash)) {
            Document doc = createDocument(rev, tags.get(hash));
            doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED));
            writer.addDocument(doc);
            result.commitCount += 1;
          }
        }
      }

      // finished
      reader.release();

      // commit all changes and reset the searcher
      config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION);
      config.save();
      writer.commit();
      resetIndexSearcher(model.name);
      result.success();
    } catch (Exception e) {
      logger.error("Exception while reindexing " + model.name, e);
View Full Code Here

   * @return IndexResult
   */
  private IndexResult updateIndex(RepositoryModel model, Repository repository) {
    IndexResult result = new IndexResult();
    try {
      FileBasedConfig config = getConfig(repository);
      config.load();

      // build a quick lookup of annotated tags
      Map<String, List<String>> tags = new HashMap<String, List<String>>();
      for (RefModel tag : JGitUtils.getTags(repository, false, -1)) {
        if (!tag.isAnnotatedTag()) {
          // skip non-annotated tags
          continue;
        }
        if (!tags.containsKey(tag.getObjectId().getName())) {
          tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>());
        }
        tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);
      }

      // detect branch deletion
      // first assume all branches are deleted and then remove each
      // existing branch from deletedBranches during indexing
      Set<String> deletedBranches = new TreeSet<String>();
      for (String alias : config.getNames(CONF_ALIAS)) {
        String branch = config.getString(CONF_ALIAS, null, alias);
        deletedBranches.add(branch);
      }

      // get the local branches
      List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);

      // sort them by most recently updated
      Collections.sort(branches, new Comparator<RefModel>() {
        @Override
        public int compare(RefModel ref1, RefModel ref2) {
          return ref2.getDate().compareTo(ref1.getDate());
        }
      });

      // reorder default branch to first position
      RefModel defaultBranch = null;
      ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository);
      for (RefModel branch :  branches) {
        if (branch.getObjectId().equals(defaultBranchId)) {
          defaultBranch = branch;
          break;
        }
      }
      branches.remove(defaultBranch);
      branches.add(0, defaultBranch);

      // walk through each branches
      for (RefModel branch : branches) {
        String branchName = branch.getName();

        boolean indexBranch = false;
        if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH)
            && branch.equals(defaultBranch)) {
          // indexing "default" branch
          indexBranch = true;
        } else if (branch.getName().startsWith(com.gitblit.Constants.R_META)) {
          // ignore internal meta branches
          indexBranch = false;
        } else {
          // normal explicit branch check
          indexBranch = model.indexedBranches.contains(branch.getName());
        }

        // if this branch is not specifically indexed then skip
        if (!indexBranch) {
          continue;
        }

        // remove this branch from the deletedBranches set
        deletedBranches.remove(branchName);

        // determine last commit
        String keyName = getBranchKey(branchName);
        String lastCommit = config.getString(CONF_BRANCH, null, keyName);

        List<RevCommit> revs;
        if (StringUtils.isEmpty(lastCommit)) {
          // new branch/unindexed branch, get all commits on branch
          revs = JGitUtils.getRevLog(repository, branchName, 0, -1);
        } else {
          // pre-existing branch, get changes since last commit
          revs = JGitUtils.getRevLog(repository, lastCommit, branchName);
        }

        if (revs.size() > 0) {
          result.branchCount += 1;
        }

        // reverse the list of commits so we start with the first commit
        Collections.reverse(revs);
        for (RevCommit commit : revs) {
          // index a commit
          result.add(index(model.name, repository, branchName, commit));
        }

        // update the config
        config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION);
        config.setString(CONF_ALIAS, null, keyName, branchName);
        config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());
        config.save();
      }

      // the deletedBranches set will normally be empty by this point
      // unless a branch really was deleted and no longer exists
      if (deletedBranches.size() > 0) {
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.storage.file.FileBasedConfig

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.