Ejemplo n.º 1
0
  /**
   * Pop the next most recent object.
   *
   * @return next most recent object; null if traversal is over.
   * @throws MissingObjectException one or or more of the next objects are not available from the
   *     object database, but were thought to be candidates for traversal. This usually indicates a
   *     broken link.
   * @throws IncorrectObjectTypeException one or or more of the objects in a tree do not match the
   *     type indicated.
   * @throws IOException a pack file or loose object could not be read.
   */
  public RevObject nextObject()
      throws MissingObjectException, IncorrectObjectTypeException, IOException {
    if (last != null) treeWalk = last instanceof RevTree ? enter(last) : treeWalk.next();

    while (!treeWalk.eof()) {
      final FileMode mode = treeWalk.getEntryFileMode();
      switch (mode.getObjectType()) {
        case Constants.OBJ_BLOB:
          {
            treeWalk.getEntryObjectId(idBuffer);
            final RevBlob o = lookupBlob(idBuffer);
            if ((o.flags & SEEN) != 0) break;
            o.flags |= SEEN;
            if (shouldSkipObject(o)) break;
            last = o;
            return o;
          }
        case Constants.OBJ_TREE:
          {
            treeWalk.getEntryObjectId(idBuffer);
            final RevTree o = lookupTree(idBuffer);
            if ((o.flags & SEEN) != 0) break;
            o.flags |= SEEN;
            if (shouldSkipObject(o)) break;
            last = o;
            return o;
          }
        default:
          if (FileMode.GITLINK.equals(mode)) break;
          treeWalk.getEntryObjectId(idBuffer);
          throw new CorruptObjectException(
              MessageFormat.format(
                  JGitText.get().corruptObjectInvalidMode3,
                  mode,
                  idBuffer.name(),
                  treeWalk.getEntryPathString(),
                  currentTree.name()));
      }

      treeWalk = treeWalk.next();
    }

    last = null;
    for (; ; ) {
      final RevObject o = pendingObjects.next();
      if (o == null) return null;
      if ((o.flags & SEEN) != 0) continue;
      o.flags |= SEEN;
      if (shouldSkipObject(o)) continue;
      if (o instanceof RevTree) {
        currentTree = (RevTree) o;
        treeWalk = treeWalk.resetRoot(db, currentTree, curs);
      }
      return o;
    }
  }
Ejemplo n.º 2
0
 private CanonicalTreeParser enter(RevObject tree) throws IOException {
   CanonicalTreeParser p = treeWalk.createSubtreeIterator0(db, tree, curs);
   if (p.eof()) {
     // We can't tolerate the subtree being an empty tree, as
     // that will break us out early before we visit all names.
     // If it is, advance to the parent's next record.
     //
     return treeWalk.next();
   }
   return p;
 }
Ejemplo n.º 3
0
  private void markTreeUninteresting(final RevTree tree)
      throws MissingObjectException, IncorrectObjectTypeException, IOException {
    if ((tree.flags & UNINTERESTING) != 0) return;
    tree.flags |= UNINTERESTING;

    treeWalk = treeWalk.resetRoot(db, tree, curs);
    while (!treeWalk.eof()) {
      final FileMode mode = treeWalk.getEntryFileMode();
      final int sType = mode.getObjectType();

      switch (sType) {
        case Constants.OBJ_BLOB:
          {
            treeWalk.getEntryObjectId(idBuffer);
            lookupBlob(idBuffer).flags |= UNINTERESTING;
            break;
          }
        case Constants.OBJ_TREE:
          {
            treeWalk.getEntryObjectId(idBuffer);
            final RevTree t = lookupTree(idBuffer);
            if ((t.flags & UNINTERESTING) == 0) {
              t.flags |= UNINTERESTING;
              treeWalk = treeWalk.createSubtreeIterator0(db, t, curs);
              continue;
            }
            break;
          }
        default:
          if (FileMode.GITLINK.equals(mode)) break;
          treeWalk.getEntryObjectId(idBuffer);
          throw new CorruptObjectException(
              MessageFormat.format(
                  JGitText.get().corruptObjectInvalidMode3,
                  mode,
                  idBuffer.name(),
                  treeWalk.getEntryPathString(),
                  tree));
      }

      treeWalk = treeWalk.next();
    }
  }
Ejemplo n.º 4
0
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    handleAuth(req);
    resp.setCharacterEncoding("UTF-8");
    final PrintWriter out = resp.getWriter();
    try {
      String pathInfo = req.getPathInfo();
      Pattern pattern = Pattern.compile("/([^/]*)(?:/([^/]*)(?:/(.*))?)?");
      Matcher matcher = pattern.matcher(pathInfo);
      matcher.matches();
      String projectName = null;
      String refName = null;
      String filePath = null;
      if (matcher.groupCount() > 0) {
        projectName = matcher.group(1);
        refName = matcher.group(2);
        filePath = matcher.group(3);
        if (projectName == null || projectName.equals("")) {
          projectName = null;
        } else {
          projectName = java.net.URLDecoder.decode(projectName, "UTF-8");
        }
        if (refName == null || refName.equals("")) {
          refName = null;
        } else {
          refName = java.net.URLDecoder.decode(refName, "UTF-8");
        }
        if (filePath == null || filePath.equals("")) {
          filePath = null;
        } else {
          filePath = java.net.URLDecoder.decode(filePath, "UTF-8");
        }
      }
      if (projectName != null) {
        if (filePath == null) filePath = "";
        NameKey projName = NameKey.parse(projectName);

        ProjectControl control;
        try {
          control = projControlFactory.controlFor(projName);
          if (!control.isVisible()) {
            log.debug("Project not visible!");
            resp.sendError(
                HttpServletResponse.SC_UNAUTHORIZED,
                "You need to be logged in to see private projects");
            return;
          }
        } catch (NoSuchProjectException e1) {
        }
        Repository repo = repoManager.openRepository(projName);
        if (refName == null) {
          JSONArray contents = new JSONArray();
          List<Ref> call;
          try {
            call = new Git(repo).branchList().call();
            Git git = new Git(repo);
            for (Ref ref : call) {
              JSONObject jsonObject = new JSONObject();
              try {
                jsonObject.put("name", ref.getName());
                jsonObject.put("type", "ref");
                jsonObject.put("size", "0");
                jsonObject.put("path", "");
                jsonObject.put("project", projectName);
                jsonObject.put("ref", ref.getName());
                lastCommit(git, null, ref.getObjectId(), jsonObject);
              } catch (JSONException e) {
              }
              contents.put(jsonObject);
            }
            String response = contents.toString();
            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "W/\"" + response.length() + "-" + response.hashCode() + "\"");
            log.debug(response);
            out.write(response);
          } catch (GitAPIException e) {
          }
        } else {
          Ref head = repo.getRef(refName);
          if (head == null) {
            JSONArray contents = new JSONArray();
            String response = contents.toString();
            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "W/\"" + response.length() + "-" + response.hashCode() + "\"");
            log.debug(response);
            out.write(response);
            return;
          }
          RevWalk walk = new RevWalk(repo);
          // add try catch to catch failures
          Git git = new Git(repo);
          RevCommit commit = walk.parseCommit(head.getObjectId());
          RevTree tree = commit.getTree();
          TreeWalk treeWalk = new TreeWalk(repo);
          treeWalk.addTree(tree);
          treeWalk.setRecursive(false);
          if (!filePath.equals("")) {
            PathFilter pathFilter = PathFilter.create(filePath);
            treeWalk.setFilter(pathFilter);
          }
          if (!treeWalk.next()) {
            CanonicalTreeParser canonicalTreeParser =
                treeWalk.getTree(0, CanonicalTreeParser.class);
            JSONArray contents = new JSONArray();
            if (canonicalTreeParser != null) {
              while (!canonicalTreeParser.eof()) {
                String path = canonicalTreeParser.getEntryPathString();
                FileMode mode = canonicalTreeParser.getEntryFileMode();
                listEntry(
                    path,
                    mode.equals(FileMode.TREE) ? "dir" : "file",
                    "0",
                    path,
                    projectName,
                    head.getName(),
                    git,
                    contents);
                canonicalTreeParser.next();
              }
            }
            String response = contents.toString();
            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "\"" + tree.getId().getName() + "\"");
            log.debug(response);
            out.write(response);
          } else {
            // if (treeWalk.isSubtree()) {
            // treeWalk.enterSubtree();
            // }

            JSONArray contents = getListEntries(treeWalk, repo, git, head, filePath, projectName);
            String response = contents.toString();

            resp.setContentType("application/json");
            resp.setHeader("Cache-Control", "no-cache");
            resp.setHeader("ETag", "\"" + tree.getId().getName() + "\"");
            log.debug(response);
            out.write(response);
          }
          walk.release();
          treeWalk.release();
        }
      }
    } catch (RepositoryNotFoundException e) {
      handleException(resp, e, 400);
    } catch (MissingObjectException e) {
      // example "Missing unknown 7035305927ca125757ecd8407e608f6dcf0bd8a5"
      // usually indicative of being unable to locate a commit from a submodule
      log.error(e.getMessage(), e);
      String msg =
          e.getMessage()
              + ".  This exception could have been caused by the use of a git submodule, "
              + "which is currently not supported by the repository browser.";
      handleException(resp, new Exception(msg), 501);
    } catch (IOException e) {
      handleException(resp, e, 500);
    } finally {
      out.close();
    }
  }